aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFuqian Huang <huangfq.daxian@gmail.com>2019-07-03 12:27:08 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2019-07-26 01:03:59 -0400
commitcc2a58f14fb88e753fbc38b658e320c3a62d8c47 (patch)
treee835794ef0fdf53b72e914da35c9fec6deffeede
parent97bcb161995548ad319c78b2533f998a7b92ab4c (diff)
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: Fuqian Huang <huangfq.daxian@gmail.com> Reviewed-by: Horia Geantă <horia.geanta@nxp.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--drivers/crypto/caam/caampkc.c11
-rw-r--r--drivers/crypto/virtio/virtio_crypto_algs.c4
2 files changed, 4 insertions, 11 deletions
diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index 80574106af29..2340f9441a80 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -867,7 +867,7 @@ static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
867 return ret; 867 return ret;
868 868
869 /* Copy key in DMA zone */ 869 /* Copy key in DMA zone */
870 rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); 870 rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
871 if (!rsa_key->e) 871 if (!rsa_key->e)
872 goto err; 872 goto err;
873 873
@@ -889,8 +889,6 @@ static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
889 rsa_key->e_sz = raw_key.e_sz; 889 rsa_key->e_sz = raw_key.e_sz;
890 rsa_key->n_sz = raw_key.n_sz; 890 rsa_key->n_sz = raw_key.n_sz;
891 891
892 memcpy(rsa_key->e, raw_key.e, raw_key.e_sz);
893
894 return 0; 892 return 0;
895err: 893err:
896 caam_rsa_free_key(rsa_key); 894 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 return ret; 969 return ret;
972 970
973 /* Copy key in DMA zone */ 971 /* Copy key in DMA zone */
974 rsa_key->d = kzalloc(raw_key.d_sz, GFP_DMA | GFP_KERNEL); 972 rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL);
975 if (!rsa_key->d) 973 if (!rsa_key->d)
976 goto err; 974 goto err;
977 975
978 rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); 976 rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
979 if (!rsa_key->e) 977 if (!rsa_key->e)
980 goto err; 978 goto err;
981 979
@@ -998,9 +996,6 @@ static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
998 rsa_key->e_sz = raw_key.e_sz; 996 rsa_key->e_sz = raw_key.e_sz;
999 rsa_key->n_sz = raw_key.n_sz; 997 rsa_key->n_sz = raw_key.n_sz;
1000 998
1001 memcpy(rsa_key->d, raw_key.d, raw_key.d_sz);
1002 memcpy(rsa_key->e, raw_key.e, raw_key.e_sz);
1003
1004 caam_rsa_set_priv_key_form(ctx, &raw_key); 999 caam_rsa_set_priv_key_form(ctx, &raw_key);
1005 1000
1006 return 0; 1001 return 0;
diff --git a/drivers/crypto/virtio/virtio_crypto_algs.c b/drivers/crypto/virtio/virtio_crypto_algs.c
index 10f266d462d6..42d19205166b 100644
--- a/drivers/crypto/virtio/virtio_crypto_algs.c
+++ b/drivers/crypto/virtio/virtio_crypto_algs.c
@@ -129,13 +129,11 @@ static int virtio_crypto_alg_ablkcipher_init_session(
129 * Avoid to do DMA from the stack, switch to using 129 * Avoid to do DMA from the stack, switch to using
130 * dynamically-allocated for the key 130 * dynamically-allocated for the key
131 */ 131 */
132 uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC); 132 uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC);
133 133
134 if (!cipher_key) 134 if (!cipher_key)
135 return -ENOMEM; 135 return -ENOMEM;
136 136
137 memcpy(cipher_key, key, keylen);
138
139 spin_lock(&vcrypto->ctrl_lock); 137 spin_lock(&vcrypto->ctrl_lock);
140 /* Pad ctrl header */ 138 /* Pad ctrl header */
141 vcrypto->ctrl.header.opcode = 139 vcrypto->ctrl.header.opcode =