diff options
Diffstat (limited to 'cryptodev_cipher.c')
-rw-r--r-- | cryptodev_cipher.c | 379 |
1 files changed, 123 insertions, 256 deletions
diff --git a/cryptodev_cipher.c b/cryptodev_cipher.c index 5c54196..622684f 100644 --- a/cryptodev_cipher.c +++ b/cryptodev_cipher.c @@ -51,15 +51,12 @@ static void cryptodev_complete(struct crypto_async_request *req, int err) int cryptodev_cipher_init(struct cipher_data* out, const char* alg_name, uint8_t __user * key, size_t keylen) { uint8_t keyp[CRYPTO_CIPHER_MAX_KEY_LEN]; + struct ablkcipher_alg* alg; int ret; memset(out, 0, sizeof(*out)); - if (crypto_has_ablkcipher(alg_name, 0, 0) != 0) { - out->type = 2; - } else { - out->type = 1; - } + out->init = 1; if (unlikely(keylen > CRYPTO_CIPHER_MAX_KEY_LEN)) { dprintk(1,KERN_DEBUG,"Setting key failed for %s-%zu.\n", @@ -69,134 +66,78 @@ int cryptodev_cipher_init(struct cipher_data* out, const char* alg_name, uint8_t /* Copy the key from user and set to TFM. */ copy_from_user(keyp, key, keylen); - if (out->type == 1) { - struct blkcipher_alg* alg; - - out->u.blk.s = crypto_alloc_blkcipher(alg_name, 0, CRYPTO_ALG_ASYNC); - if (IS_ERR(out->u.blk.s)) { - dprintk(1,KERN_DEBUG,"%s: Failed to load cipher %s\n", __func__, - alg_name); - return -EINVAL; - } - - alg = crypto_blkcipher_alg(out->u.blk.s); - - if (alg != NULL) { - /* Was correct key length supplied? */ - if ((keylen < alg->min_keysize) || - (keylen > alg->max_keysize)) { - dprintk(1,KERN_DEBUG,"Wrong keylen '%zu' for algorithm '%s'. Use %u to %u.\n", - keylen, alg_name, alg->min_keysize, - alg->max_keysize); - ret = -EINVAL; - goto error; - } - } - - ret = crypto_blkcipher_setkey(out->u.blk.s, keyp, keylen); - if (ret) { - dprintk(1,KERN_DEBUG,"Setting key failed for %s-%zu.\n", - alg_name, keylen*8); - ret = -EINVAL; - goto error; - } - - out->blocksize = crypto_blkcipher_blocksize(out->u.blk.s); - out->ivsize = crypto_blkcipher_ivsize(out->u.blk.s); + out->async.s = crypto_alloc_ablkcipher(alg_name, 0, 0); + if (IS_ERR(out->async.s)) { + dprintk(1,KERN_DEBUG,"%s: Failed to load cipher %s\n", __func__, + alg_name); + return -EINVAL; + } - out->u.blk.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; - out->u.blk.desc.tfm = out->u.blk.s; + alg = crypto_ablkcipher_alg(out->async.s); - } else { /* async */ - struct ablkcipher_alg* alg; - - out->u.ablk.s = crypto_alloc_ablkcipher(alg_name, 0, 0); - if (IS_ERR(out->u.ablk.s)) { - dprintk(1,KERN_DEBUG,"%s: Failed to load cipher %s\n", __func__, - alg_name); - return -EINVAL; - } - - alg = crypto_ablkcipher_alg(out->u.ablk.s); - - if (alg != NULL) { - /* Was correct key length supplied? */ - if ((keylen < alg->min_keysize) || - (keylen > alg->max_keysize)) { - dprintk(1,KERN_DEBUG,"Wrong keylen '%zu' for algorithm '%s'. Use %u to %u.\n", - keylen, alg_name, alg->min_keysize, - alg->max_keysize); - ret = -EINVAL; - goto error; - } - } - - ret = crypto_ablkcipher_setkey(out->u.ablk.s, keyp, keylen); - if (ret) { - dprintk(1,KERN_DEBUG,"Setting key failed for %s-%zu.\n", - alg_name, keylen*8); + if (alg != NULL) { + /* Was correct key length supplied? */ + if ((keylen < alg->min_keysize) || + (keylen > alg->max_keysize)) { + dprintk(1,KERN_DEBUG,"Wrong keylen '%zu' for algorithm '%s'. Use %u to %u.\n", + keylen, alg_name, alg->min_keysize, + alg->max_keysize); ret = -EINVAL; goto error; } + } - out->blocksize = crypto_ablkcipher_blocksize(out->u.ablk.s); - out->ivsize = crypto_ablkcipher_ivsize(out->u.ablk.s); + ret = crypto_ablkcipher_setkey(out->async.s, keyp, keylen); + if (ret) { + dprintk(1,KERN_DEBUG,"Setting key failed for %s-%zu.\n", + alg_name, keylen*8); + ret = -EINVAL; + goto error; + } - out->u.ablk.async_result = kmalloc(sizeof(*out->u.ablk.async_result), GFP_KERNEL); - if (!out->u.ablk.async_result) { - ret = -ENOMEM; - goto error; - } + out->blocksize = crypto_ablkcipher_blocksize(out->async.s); + out->ivsize = crypto_ablkcipher_ivsize(out->async.s); - memset(out->u.ablk.async_result, 0, sizeof(*out->u.ablk.async_result)); - init_completion(&out->u.ablk.async_result->completion); + out->async.result = kmalloc(sizeof(*out->async.result), GFP_KERNEL); + if (!out->async.result) { + ret = -ENOMEM; + goto error; + } - out->u.ablk.async_request = ablkcipher_request_alloc(out->u.ablk.s, GFP_KERNEL); - if (!out->u.ablk.async_request) { - dprintk(1,KERN_ERR,"error allocating async crypto request\n"); - ret = -ENOMEM; - goto error; - } + memset(out->async.result, 0, sizeof(*out->async.result)); + init_completion(&out->async.result->completion); - ablkcipher_request_set_callback(out->u.ablk.async_request, CRYPTO_TFM_REQ_MAY_BACKLOG, - cryptodev_complete, out->u.ablk.async_result); + out->async.request = ablkcipher_request_alloc(out->async.s, GFP_KERNEL); + if (!out->async.request) { + dprintk(1,KERN_ERR,"error allocating async crypto request\n"); + ret = -ENOMEM; + goto error; } + ablkcipher_request_set_callback(out->async.request, CRYPTO_TFM_REQ_MAY_BACKLOG, + cryptodev_complete, out->async.result); + return 0; error: - if (out->type == 1) - crypto_free_blkcipher(out->u.blk.s); - else { - crypto_free_ablkcipher(out->u.ablk.s); - kfree(out->u.ablk.async_result); - ablkcipher_request_free(out->u.ablk.async_request); - } + crypto_free_ablkcipher(out->async.s); + kfree(out->async.result); + ablkcipher_request_free(out->async.request); return ret; } void cryptodev_cipher_deinit(struct cipher_data* cdata) { - if (cdata->type == 1) - crypto_free_blkcipher(cdata->u.blk.s); - else if (cdata->type == 2) { - crypto_free_ablkcipher(cdata->u.ablk.s); - kfree(cdata->u.ablk.async_result); - ablkcipher_request_free(cdata->u.ablk.async_request); - } - cdata->type = 0; + crypto_free_ablkcipher(cdata->async.s); + kfree(cdata->async.result); + ablkcipher_request_free(cdata->async.request); + + cdata->init = 0; } void cryptodev_cipher_set_iv(struct cipher_data* cdata, void __user* iv, size_t iv_size) { - if (cdata->type == 1) { - uint8_t _iv[EALG_MAX_BLOCK_LEN]; - - copy_from_user(_iv, iv, min(iv_size,sizeof(_iv))); - crypto_blkcipher_set_iv(cdata->u.blk.s, _iv, iv_size); - } else { - copy_from_user(cdata->u.ablk.iv, iv, min(iv_size,sizeof(cdata->u.ablk.iv))); - } + copy_from_user(cdata->async.iv, iv, min(iv_size,sizeof(cdata->async.iv))); } static inline int waitfor (struct cryptodev_result* cr, ssize_t ret) @@ -229,59 +170,36 @@ static inline int waitfor (struct cryptodev_result* cr, ssize_t ret) ssize_t cryptodev_cipher_encrypt( struct cipher_data* cdata, struct scatterlist *sg1, struct scatterlist *sg2, size_t len) { - if (cdata->type == 1) - return crypto_blkcipher_encrypt(&cdata->u.blk.desc, sg1, sg2, len); - else { - int ret; - - INIT_COMPLETION(cdata->u.ablk.async_result->completion); - ablkcipher_request_set_crypt(cdata->u.ablk.async_request, sg1, sg2, - len, cdata->u.ablk.iv); - ret = crypto_ablkcipher_encrypt(cdata->u.ablk.async_request); - - return waitfor(cdata->u.ablk.async_result,ret); - } + int ret; + + INIT_COMPLETION(cdata->async.result->completion); + ablkcipher_request_set_crypt(cdata->async.request, sg1, sg2, + len, cdata->async.iv); + ret = crypto_ablkcipher_encrypt(cdata->async.request); + + return waitfor(cdata->async.result,ret); } ssize_t cryptodev_cipher_decrypt( struct cipher_data* cdata, struct scatterlist *sg1, struct scatterlist *sg2, size_t len) { - if (cdata->type == 1) - return crypto_blkcipher_encrypt(&cdata->u.blk.desc, sg1, sg2, len); - else { - int ret; - - INIT_COMPLETION(cdata->u.ablk.async_result->completion); - ablkcipher_request_set_crypt(cdata->u.ablk.async_request, sg1, sg2, - len, cdata->u.ablk.iv); - ret = crypto_ablkcipher_decrypt(cdata->u.ablk.async_request); - - return waitfor(cdata->u.ablk.async_result, ret); - } + int ret; + + INIT_COMPLETION(cdata->async.result->completion); + ablkcipher_request_set_crypt(cdata->async.request, sg1, sg2, + len, cdata->async.iv); + ret = crypto_ablkcipher_decrypt(cdata->async.request); + + return waitfor(cdata->async.result, ret); } /* Hash functions */ -static int _crypto_has_ahash(const char* alg_name) -{ -uint32_t type = 0, mask = 0; - type &= ~CRYPTO_ALG_TYPE_MASK; - mask &= ~CRYPTO_ALG_TYPE_MASK; - type |= CRYPTO_ALG_TYPE_AHASH; - mask |= CRYPTO_ALG_TYPE_AHASH_MASK; - - return crypto_has_alg(alg_name, type, mask); -} - int cryptodev_hash_init( struct hash_data* hdata, const char* alg_name, int hmac_mode, void __user* mackey, size_t mackeylen) { int ret; uint8_t hkeyp[CRYPTO_HMAC_MAX_KEY_LEN]; - if (_crypto_has_ahash(alg_name) != 0) { - hdata->type = 2; - } else { - hdata->type = 1; - } + hdata->init = 1; if (mackeylen > CRYPTO_HMAC_MAX_KEY_LEN) { dprintk(1,KERN_DEBUG,"Setting hmac key failed for %s-%zu.\n", @@ -290,111 +208,68 @@ uint8_t hkeyp[CRYPTO_HMAC_MAX_KEY_LEN]; } copy_from_user(hkeyp, mackey, mackeylen); - if (hdata->type == 1) { - hdata->u.sync.s = crypto_alloc_hash(alg_name, 0, CRYPTO_ALG_ASYNC); - if (IS_ERR(hdata->u.sync.s)) { - dprintk(1,KERN_DEBUG,"%s: Failed to load transform for %s\n", __func__, - alg_name); - return -EINVAL; - } + hdata->async.s = crypto_alloc_ahash(alg_name, 0, 0); + if (IS_ERR(hdata->async.s)) { + dprintk(1,KERN_DEBUG,"%s: Failed to load transform for %s\n", __func__, + alg_name); + return -EINVAL; + } - /* Copy the key from user and set to TFM. */ - if (hmac_mode != 0) { + /* Copy the key from user and set to TFM. */ + if (hmac_mode != 0) { - ret = crypto_hash_setkey(hdata->u.sync.s, hkeyp, mackeylen); - if (ret) { - dprintk(1,KERN_DEBUG,"Setting hmac key failed for %s-%zu.\n", - alg_name, mackeylen*8); - ret = -EINVAL; - goto error; - } + ret = crypto_ahash_setkey(hdata->async.s, hkeyp, mackeylen); + if (ret) { + dprintk(1,KERN_DEBUG,"Setting hmac key failed for %s-%zu.\n", + alg_name, mackeylen*8); + ret = -EINVAL; + goto error; } + } - hdata->u.sync.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; - hdata->u.sync.desc.tfm = hdata->u.sync.s; - - hdata->digestsize = crypto_hash_digestsize(hdata->u.sync.s); - - } else { - hdata->u.async.s = crypto_alloc_ahash(alg_name, 0, 0); - if (IS_ERR(hdata->u.async.s)) { - dprintk(1,KERN_DEBUG,"%s: Failed to load transform for %s\n", __func__, - alg_name); - return -EINVAL; - } + hdata->digestsize = crypto_ahash_digestsize(hdata->async.s); - /* Copy the key from user and set to TFM. */ - if (hmac_mode != 0) { + hdata->async.result = kmalloc(sizeof(*hdata->async.result), GFP_KERNEL); + if (!hdata->async.result) { + ret = -ENOMEM; + goto error; + } - ret = crypto_ahash_setkey(hdata->u.async.s, hkeyp, mackeylen); - if (ret) { - dprintk(1,KERN_DEBUG,"Setting hmac key failed for %s-%zu.\n", - alg_name, mackeylen*8); - ret = -EINVAL; - goto error; - } - } + memset(hdata->async.result, 0, sizeof(*hdata->async.result)); + init_completion(&hdata->async.result->completion); - hdata->digestsize = crypto_ahash_digestsize(hdata->u.async.s); + hdata->async.request = ahash_request_alloc(hdata->async.s, GFP_KERNEL); + if (!hdata->async.request) { + dprintk(0,KERN_ERR,"error allocating async crypto request\n"); + ret = -ENOMEM; + goto error; + } - hdata->u.async.async_result = kmalloc(sizeof(*hdata->u.async.async_result), GFP_KERNEL); - if (!hdata->u.async.async_result) { - ret = -ENOMEM; - goto error; - } + ahash_request_set_callback(hdata->async.request, CRYPTO_TFM_REQ_MAY_BACKLOG, + cryptodev_complete, hdata->async.result); - memset(hdata->u.async.async_result, 0, sizeof(*hdata->u.async.async_result)); - init_completion(&hdata->u.async.async_result->completion); - hdata->u.async.async_request = ahash_request_alloc(hdata->u.async.s, GFP_KERNEL); - if (!hdata->u.async.async_request) { - dprintk(0,KERN_ERR,"error allocating async crypto request\n"); - ret = -ENOMEM; - goto error; - } - - ahash_request_set_callback(hdata->u.async.async_request, CRYPTO_TFM_REQ_MAY_BACKLOG, - cryptodev_complete, hdata->u.async.async_result); - - - } - return 0; error: - if (hdata->type == 1) - crypto_free_hash(hdata->u.sync.s); - else - crypto_free_ahash(hdata->u.async.s); + crypto_free_ahash(hdata->async.s); return ret; } void cryptodev_hash_deinit(struct hash_data* hdata) { - if (hdata->type == 1) - crypto_free_hash(hdata->u.sync.s); - else if (hdata->type == 2) - crypto_free_ahash(hdata->u.async.s); - hdata->type = 0; + crypto_free_ahash(hdata->async.s); + hdata->init = 0; } int cryptodev_hash_reset( struct hash_data* hdata) { int ret; - if (hdata->type == 1) { - ret = crypto_hash_init(&hdata->u.sync.desc); - if (unlikely(ret)) { - dprintk(0,KERN_ERR, - "error in crypto_hash_init()\n"); - return ret; - } - } else { - ret = crypto_ahash_init(hdata->u.async.async_request); - if (unlikely(ret)) { - dprintk(0,KERN_ERR, - "error in crypto_hash_init()\n"); - return ret; - } + ret = crypto_ahash_init(hdata->async.request); + if (unlikely(ret)) { + dprintk(0,KERN_ERR, + "error in crypto_hash_init()\n"); + return ret; } return 0; @@ -403,34 +278,26 @@ int ret; ssize_t cryptodev_hash_update( struct hash_data* hdata, struct scatterlist *sg, size_t len) { - if (hdata->type == 1) - return crypto_hash_update(&hdata->u.sync.desc, sg, len); - else { - int ret; - - INIT_COMPLETION(hdata->u.async.async_result->completion); - ahash_request_set_crypt(hdata->u.async.async_request, sg, NULL, - len); - - ret = crypto_ahash_update(hdata->u.async.async_request); - - return waitfor(hdata->u.async.async_result,ret); - } + int ret; + + INIT_COMPLETION(hdata->async.result->completion); + ahash_request_set_crypt(hdata->async.request, sg, NULL, + len); + + ret = crypto_ahash_update(hdata->async.request); + + return waitfor(hdata->async.result,ret); } int cryptodev_hash_final( struct hash_data* hdata, void* output) { - if (hdata->type == 1) - return crypto_hash_final(&hdata->u.sync.desc, output); - else { - int ret; - - INIT_COMPLETION(hdata->u.async.async_result->completion); - ahash_request_set_crypt(hdata->u.async.async_request, NULL, output, 0); - - ret = crypto_ahash_final(hdata->u.async.async_request); - - return waitfor(hdata->u.async.async_result,ret); - } + int ret; + + INIT_COMPLETION(hdata->async.result->completion); + ahash_request_set_crypt(hdata->async.request, NULL, output, 0); + + ret = crypto_ahash_final(hdata->async.request); + + return waitfor(hdata->async.result,ret); } |