Commit cc2a58f1 authored by Fuqian Huang's avatar Fuqian Huang Committed by Herbert Xu

crypto: drivers - Use kmemdup rather than duplicating its implementation

kmemdup is introduced to duplicate a region of memory in a neat way.
Rather than kmalloc/kzalloc + memcpy, which the programmer needs to
write the size twice (sometimes lead to mistakes), kmemdup improves
readability, leads to smaller code and also reduce the chances of mistakes.
Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy.
Signed-off-by: default avatarFuqian Huang <huangfq.daxian@gmail.com>
Reviewed-by: default avatarHoria Geantă <horia.geanta@nxp.com>
Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 97bcb161
...@@ -867,7 +867,7 @@ static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, ...@@ -867,7 +867,7 @@ static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
return ret; return ret;
/* Copy key in DMA zone */ /* Copy key in DMA zone */
rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
if (!rsa_key->e) if (!rsa_key->e)
goto err; goto err;
...@@ -889,8 +889,6 @@ static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, ...@@ -889,8 +889,6 @@ static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
rsa_key->e_sz = raw_key.e_sz; rsa_key->e_sz = raw_key.e_sz;
rsa_key->n_sz = raw_key.n_sz; rsa_key->n_sz = raw_key.n_sz;
memcpy(rsa_key->e, raw_key.e, raw_key.e_sz);
return 0; return 0;
err: err:
caam_rsa_free_key(rsa_key); caam_rsa_free_key(rsa_key);
...@@ -971,11 +969,11 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, ...@@ -971,11 +969,11 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
return ret; return ret;
/* Copy key in DMA zone */ /* Copy key in DMA zone */
rsa_key->d = kzalloc(raw_key.d_sz, GFP_DMA | GFP_KERNEL); rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL);
if (!rsa_key->d) if (!rsa_key->d)
goto err; goto err;
rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
if (!rsa_key->e) if (!rsa_key->e)
goto err; goto err;
...@@ -998,9 +996,6 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, ...@@ -998,9 +996,6 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
rsa_key->e_sz = raw_key.e_sz; rsa_key->e_sz = raw_key.e_sz;
rsa_key->n_sz = raw_key.n_sz; rsa_key->n_sz = raw_key.n_sz;
memcpy(rsa_key->d, raw_key.d, raw_key.d_sz);
memcpy(rsa_key->e, raw_key.e, raw_key.e_sz);
caam_rsa_set_priv_key_form(ctx, &raw_key); caam_rsa_set_priv_key_form(ctx, &raw_key);
return 0; return 0;
......
...@@ -129,13 +129,11 @@ static int virtio_crypto_alg_ablkcipher_init_session( ...@@ -129,13 +129,11 @@ static int virtio_crypto_alg_ablkcipher_init_session(
* Avoid to do DMA from the stack, switch to using * Avoid to do DMA from the stack, switch to using
* dynamically-allocated for the key * dynamically-allocated for the key
*/ */
uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC); uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC);
if (!cipher_key) if (!cipher_key)
return -ENOMEM; return -ENOMEM;
memcpy(cipher_key, key, keylen);
spin_lock(&vcrypto->ctrl_lock); spin_lock(&vcrypto->ctrl_lock);
/* Pad ctrl header */ /* Pad ctrl header */
vcrypto->ctrl.header.opcode = vcrypto->ctrl.header.opcode =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment