diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-12 12:57:11 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-12 12:59:56 +0200 |
commit | 4f7b10f59b97b45d42577c4d6d33958d9565aef7 (patch) | |
tree | 0f78a952c73985d7e2d852f90f7319a52db785de /ncr-pk.c | |
parent | 8d2faf007636a936a5346426f6fe5c251a1255bb (diff) | |
download | cryptodev-linux-4f7b10f59b97b45d42577c4d6d33958d9565aef7.tar.gz cryptodev-linux-4f7b10f59b97b45d42577c4d6d33958d9565aef7.tar.xz cryptodev-linux-4f7b10f59b97b45d42577c4d6d33958d9565aef7.zip |
Initial additions to have PK encryption/decryption. Separated operations
on keys to read/write to prevent overwriting a key while using it. Several
other cleanups.
Diffstat (limited to 'ncr-pk.c')
-rw-r--r-- | ncr-pk.c | 105 |
1 files changed, 104 insertions, 1 deletions
@@ -283,7 +283,6 @@ struct keygen_st st; return 0; } - int ncr_pk_queue_init(void) { pk_wq = @@ -301,3 +300,107 @@ void ncr_pk_queue_deinit(void) flush_workqueue(pk_wq); destroy_workqueue(pk_wq); } + +/* Encryption/Decryption + */ + +void ncr_pk_cipher_deinit(struct ncr_pk_ctx* ctx) +{ + ctx->key = NULL; +} + +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) +{ + memset(ctx, 0, sizeof(*ctx)); + + if (key->algorithm != algo) { + err(); + return -EINVAL; + } + + ctx->algorithm = algo; + ctx->key = key; + + switch(algo) { + case NCR_ALG_RSA: + if (params->params.rsa.type == RSA_PKCS1_V1_5) + ctx->type = LTC_LTC_PKCS_1_V1_5; + else + ctx->type = LTC_LTC_PKCS_1_OAEP; + + ctx->hash = params->params.rsa.hash; + break; + case NCR_ALG_DSA: + break; + default: + err(); + return -EINVAL; + } + + return 0; +} + +int ncr_pk_cipher_encrypt(const struct ncr_pk_ctx* ctx, + const void* input, size_t input_size, + void* output, size_t *output_size) +{ +int cret; +unsigned long osize = *output_size; + + switch(ctx->algorithm) { + case NCR_ALG_RSA: + cret = rsa_encrypt_key_ex( input, input_size, output, &osize, + NULL, 0, ctx->hash, ctx->type, &ctx->key->key.pk.rsa); + + if (cret != CRYPT_OK) { + err(); + return tomerr(cret); + } + *output_size = osize; + break; + case NCR_ALG_DSA: + return -EINVAL; + break; + default: + err(); + return -EINVAL; + } + + return 0; +} + +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; +unsigned long osize = *output_size; +int stat; + + switch(ctx->algorithm) { + case NCR_ALG_RSA: + cret = rsa_decrypt_key_ex( input, input_size, output, &osize, + NULL, 0, ctx->hash, ctx->type, &stat, &ctx->key->key.pk.rsa); + + if (cret != CRYPT_OK) { + err(); + return tomerr(cret); + } + + if (stat==0) { + err(); + return -EINVAL; + } + *output_size = osize; + break; + case NCR_ALG_DSA: + return -EINVAL; + break; + default: + err(); + return -EINVAL; + } + + return 0; +} |