diff options
author | Miloslav Trmač <mitr@redhat.com> | 2010-07-23 17:52:29 +0200 |
---|---|---|
committer | Miloslav Trmač <mitr@redhat.com> | 2010-07-23 17:52:29 +0200 |
commit | 16ace6317db2c47b36b08b0addfe91356151c08d (patch) | |
tree | 3a3c051b43333fabe1116e45e512acadf11da3e8 /crypto/userspace/ncr-pk.c | |
parent | 893a38ffe67459db005bd1c7f129c04508cd015d (diff) | |
parent | d3d66b196cda9b100f3e3e0e742e0c2a76f2751a (diff) | |
download | kernel-crypto-16ace6317db2c47b36b08b0addfe91356151c08d.tar.gz kernel-crypto-16ace6317db2c47b36b08b0addfe91356151c08d.tar.xz kernel-crypto-16ace6317db2c47b36b08b0addfe91356151c08d.zip |
Merge branch 'standalone-master' into standalone-rename
Conflicts:
examples/Makefile
examples/ncr.c
examples/pk.c
examples/speed.c
Diffstat (limited to 'crypto/userspace/ncr-pk.c')
-rw-r--r-- | crypto/userspace/ncr-pk.c | 240 |
1 files changed, 69 insertions, 171 deletions
diff --git a/crypto/userspace/ncr-pk.c b/crypto/userspace/ncr-pk.c index 6b304bbb697..b95256ddc40 100644 --- a/crypto/userspace/ncr-pk.c +++ b/crypto/userspace/ncr-pk.c @@ -303,6 +303,18 @@ void ncr_pk_queue_deinit(void) destroy_workqueue(pk_wq); } +int ncr_key_params_get_sign_hash(ncr_algorithm_t algo, struct ncr_key_params_st * params) +{ + switch(algo) { + case NCR_ALG_RSA: + return params->params.rsa.sign_hash; + case NCR_ALG_DSA: + return params->params.dsa.sign_hash; + default: + return -EINVAL; + } +} + /* Encryption/Decryption */ @@ -318,6 +330,8 @@ int ncr_pk_cipher_init(ncr_algorithm_t algo, struct ncr_pk_ctx* ctx, struct ncr_key_params_st* params, struct key_item_st *key) { +int ret; + memset(ctx, 0, sizeof(*ctx)); if (key->algorithm != algo) { @@ -327,19 +341,24 @@ int ncr_pk_cipher_init(ncr_algorithm_t algo, ctx->algorithm = algo; ctx->key = key; - ctx->sign_hash = params->params.pk.sign_hash; + ret = ncr_key_params_get_sign_hash(algo, params); + if (ret < 0) { + err(); + return ret; + } + ctx->sign_hash = ret; switch(algo) { case NCR_ALG_RSA: - if (params->params.pk.type == RSA_PKCS1_V1_5) + if (params->params.rsa.type == RSA_PKCS1_V1_5) ctx->type = LTC_LTC_PKCS_1_V1_5; - else if (params->params.pk.type == RSA_PKCS1_OAEP) + else if (params->params.rsa.type == RSA_PKCS1_OAEP) ctx->type = LTC_LTC_PKCS_1_OAEP; - else if (params->params.pk.type == RSA_PKCS1_PSS) + else if (params->params.rsa.type == RSA_PKCS1_PSS) ctx->type = LTC_LTC_PKCS_1_PSS; - ctx->oaep_hash = params->params.pk.oaep_hash; - ctx->salt_len = params->params.pk.pss_salt; + ctx->oaep_hash = params->params.rsa.oaep_hash; + ctx->salt_len = params->params.rsa.pss_salt; break; case NCR_ALG_DSA: break; @@ -354,237 +373,121 @@ int ncr_pk_cipher_init(ncr_algorithm_t algo, } int ncr_pk_cipher_encrypt(const struct ncr_pk_ctx* ctx, - const struct scatterlist* isg, unsigned int isg_cnt, size_t isg_size, - struct scatterlist *osg, unsigned int osg_cnt, size_t* osg_size) + const void* input, size_t input_size, + void* output, size_t *output_size) { -int cret, ret; -unsigned long osize = *osg_size; -uint8_t* tmp; -void * input, *output; - - tmp = kmalloc(isg_size + *osg_size, GFP_KERNEL); - if (tmp == NULL) { - err(); - return -ENOMEM; - } - - ret = sg_copy_to_buffer((struct scatterlist*)isg, isg_cnt, tmp, isg_size); - if (ret != isg_size) { - err(); - ret = -EINVAL; - goto fail; - } - - input = tmp; - output = &tmp[isg_size]; - +int cret; +unsigned long osize = *output_size; switch(ctx->algorithm) { case NCR_ALG_RSA: - cret = rsa_encrypt_key_ex( input, isg_size, output, &osize, + cret = rsa_encrypt_key_ex( input, input_size, output, &osize, NULL, 0, ctx->oaep_hash, ctx->type, &ctx->key->key.pk.rsa); if (cret != CRYPT_OK) { + printk("cret: %d type: %d\n", cret, ctx->type); err(); - ret = tomerr(cret); - goto fail; - } - *osg_size = osize; - - ret = sg_copy_from_buffer(osg, osg_cnt, output, osize); - if (ret != osize) { - err(); - ret = -EINVAL; - goto fail; + return tomerr(cret); } - + *output_size = osize; break; case NCR_ALG_DSA: - ret = -EINVAL; - goto fail; + return -EINVAL; + break; default: err(); - ret = -EINVAL; - goto fail; + return -EINVAL; } - - ret = sg_copy_from_buffer(osg, osg_cnt, output, *osg_size); - if (ret != *osg_size) { - err(); - ret = -EINVAL; - goto fail; - } - - ret = 0; - -fail: - kfree(tmp); - return ret; + + return 0; } -int ncr_pk_cipher_decrypt(const struct ncr_pk_ctx* ctx, - const struct scatterlist* isg, unsigned int isg_cnt, size_t isg_size, - struct scatterlist *osg, unsigned int osg_cnt, size_t* osg_size) +int ncr_pk_cipher_decrypt(const struct ncr_pk_ctx* ctx, const void* input, size_t input_size, + void* output, size_t *output_size) { -int cret, ret; +int cret; +unsigned long osize = *output_size; int stat; -unsigned long osize = *osg_size; -uint8_t* tmp; -void * input, *output; - - tmp = kmalloc(isg_size + *osg_size, GFP_KERNEL); - if (tmp == NULL) { - err(); - return -ENOMEM; - } - - input = tmp; - output = &tmp[isg_size]; - - ret = sg_copy_to_buffer((struct scatterlist*)isg, isg_cnt, input, isg_size); - if (ret != isg_size) { - err(); - ret = -EINVAL; - goto fail; - } switch(ctx->algorithm) { case NCR_ALG_RSA: - cret = rsa_decrypt_key_ex( input, isg_size, output, &osize, + cret = rsa_decrypt_key_ex( input, input_size, output, &osize, NULL, 0, ctx->oaep_hash, ctx->type, &stat, &ctx->key->key.pk.rsa); if (cret != CRYPT_OK) { err(); - ret = tomerr(cret); - goto fail; + return tomerr(cret); } if (stat==0) { err(); - ret = -EINVAL; - goto fail; + return -EINVAL; } - *osg_size = osize; + *output_size = osize; break; case NCR_ALG_DSA: - ret = -EINVAL; - goto fail; + return -EINVAL; + break; default: err(); - ret = -EINVAL; - goto fail; + return -EINVAL; } - - ret = sg_copy_from_buffer(osg, osg_cnt, output, *osg_size); - if (ret != *osg_size) { - err(); - ret = -EINVAL; - goto fail; - } - - ret = 0; -fail: - kfree(tmp); - return ret; + return 0; } int ncr_pk_cipher_sign(const struct ncr_pk_ctx* ctx, - const struct scatterlist* isg, unsigned int isg_cnt, size_t isg_size, - struct scatterlist *osg, unsigned int osg_cnt, size_t* osg_size) + const void* input, size_t input_size, + void* output, size_t *output_size) { -int cret, ret; -unsigned long osize = *osg_size; -uint8_t* tmp; -void * input, *output; - - tmp = kmalloc(isg_size + *osg_size, GFP_KERNEL); - if (tmp == NULL) { - err(); - return -ENOMEM; - } - - input = tmp; - output = &tmp[isg_size]; - - ret = sg_copy_to_buffer((struct scatterlist*)isg, isg_cnt, input, isg_size); - if (ret != isg_size) { - err(); - ret = -EINVAL; - goto fail; - } +int cret; +unsigned long osize = *output_size; switch(ctx->algorithm) { case NCR_ALG_RSA: - cret = rsa_sign_hash_ex( input, isg_size, output, &osize, + cret = rsa_sign_hash_ex( input, input_size, output, &osize, ctx->type, ctx->sign_hash, ctx->salt_len, &ctx->key->key.pk.rsa); if (cret != CRYPT_OK) { err(); return tomerr(cret); } - *osg_size = osize; + *output_size = osize; break; case NCR_ALG_DSA: - cret = dsa_sign_hash( input, isg_size, output, &osize, + cret = dsa_sign_hash( input, input_size, output, &osize, &ctx->key->key.pk.dsa); if (cret != CRYPT_OK) { err(); return tomerr(cret); } - *osg_size = osize; + *output_size = osize; break; default: err(); - ret = -EINVAL; - goto fail; - } - - ret = sg_copy_from_buffer(osg, osg_cnt, output, *osg_size); - if (ret != *osg_size) { - err(); - ret = -EINVAL; - goto fail; + return -EINVAL; } - ret = 0; -fail: - kfree(tmp); - return ret; + return 0; } int ncr_pk_cipher_verify(const struct ncr_pk_ctx* ctx, - const struct scatterlist* sign_sg, unsigned int sign_sg_cnt, size_t sign_sg_size, + const void* signature, size_t signature_size, const void* hash, size_t hash_size, ncr_error_t* err) { -int cret, ret; +int cret; int stat; -uint8_t* sig; - - sig = kmalloc(sign_sg_size, GFP_KERNEL); - if (sig == NULL) { - err(); - return -ENOMEM; - } - - ret = sg_copy_to_buffer((struct scatterlist*)sign_sg, sign_sg_cnt, sig, sign_sg_size); - if (ret != sign_sg_size) { - err(); - ret = -EINVAL; - goto fail; - } switch(ctx->algorithm) { case NCR_ALG_RSA: - cret = rsa_verify_hash_ex( sig, sign_sg_size, + cret = rsa_verify_hash_ex( signature, signature_size, hash, hash_size, ctx->type, ctx->sign_hash, ctx->salt_len, &stat, &ctx->key->key.pk.rsa); if (cret != CRYPT_OK) { err(); - ret = tomerr(cret); - goto fail; + return tomerr(cret); } if (stat == 1) @@ -594,12 +497,11 @@ uint8_t* sig; break; case NCR_ALG_DSA: - cret = dsa_verify_hash( sig, sign_sg_size, + cret = dsa_verify_hash( signature, signature_size, hash, hash_size, &stat, &ctx->key->key.pk.dsa); if (cret != CRYPT_OK) { err(); - ret = tomerr(cret); - goto fail; + return tomerr(cret); } if (stat == 1) @@ -610,12 +512,8 @@ uint8_t* sig; break; default: err(); - ret = -EINVAL; - goto fail; + return -EINVAL; } - - ret = 0; -fail: - kfree(sig); - return ret; + + return 0; } |