Skip to contentSkip to conversation

crypto: cast5 - use memcpy_and_pad() in cast5_setkey()

PATCH In review Thorsten Blum 4 participants 8 messages Sep 21 – Oct 2

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/5 crypto: cast5 - use memcpy_and_pad() in cast5_setkey()

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify cast5_setkey().

---
 crypto/cast5_generic.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
crypto/cast5_generic.c +1 -3
index f68330793e0c..1de0dc966a03 100644
@@ -483,9 +483,7 @@ int cast5_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
 
 	c->rr = key_len <= 10 ? 1 : 0;
 
-	memset(p_key, 0, 16);
-	memcpy(p_key, key, key_len);
-
+	memcpy_and_pad(p_key, sizeof(p_key), key, key_len, 0);
 
 	x[0] = be32_to_cpu(p_key[0]);
 	x[1] = be32_to_cpu(p_key[1]);
#1 lore raw

PATCH 2/5 crypto: cast6 - use memcpy_and_pad() in __cast6_setkey()

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify __cast6_setkey().

---
 crypto/cast6_generic.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
crypto/cast6_generic.c +1 -2
index 4c08c42646f0..9b6bdd2aa511 100644
@@ -112,8 +112,7 @@ int __cast6_setkey(struct cast6_ctx *c, const u8 *in_key, unsigned int key_len)
 	if (key_len % 4 != 0)
 		return -EINVAL;
 
-	memset(p_key, 0, 32);
-	memcpy(p_key, in_key, key_len);
+	memcpy_and_pad(p_key, sizeof(p_key), in_key, key_len, 0);
 
 	key[0] = be32_to_cpu(p_key[0]);		/* A */
 	key[1] = be32_to_cpu(p_key[1]);		/* B */
#2 lore raw

PATCH 3/5 crypto: ixp4xx - use memcpy_and_pad() in register_chain_var()

In reply to Thorsten Blum

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify register_chain_var().

---
 drivers/crypto/intel/ixp4xx/ixp4xx_crypto.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/crypto/intel/ixp4xx/ixp4xx_crypto.c +1 -2
index fdda04672454..33e8e887a415 100644
@@ -707,8 +707,7 @@ static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
 		return -EAGAIN;
 	}
 
-	memcpy(pad, key, key_len);
-	memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
+	memcpy_and_pad(pad, HMAC_PAD_BLOCKLEN, key, key_len, 0);
 	for (i = 0; i < HMAC_PAD_BLOCKLEN; i++)
 		pad[i] ^= xpad;
 
#4 lore raw

PATCH 4/5 crypto: octeontx - use memcpy_and_pad() in aead_hmac_init()

In reply to Thorsten Blum

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify aead_hmac_init().

---
 drivers/crypto/marvell/octeontx/otx_cptvf_algs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/crypto/marvell/octeontx/otx_cptvf_algs.c +1 -2
index 096be42e9d03..1becaf77b10c 100644
@@ -792,8 +792,7 @@ static int aead_hmac_init(struct crypto_aead *cipher,
 	ipad = ctx->ipad;
 	opad = ctx->opad;
 
-	memcpy(ipad, ctx->key, authkeylen);
-	memset(ipad + authkeylen, 0, bs - authkeylen);
+	memcpy_and_pad(ipad, bs, ctx->key, authkeylen, 0);
 	memcpy(opad, ipad, bs);
 
 	for (icount = 0; icount < bs; icount++) {
#5 lore raw

PATCH 5/5 crypto: octeontx2 - use memcpy_and_pad() in aead_hmac_init()

In reply to Thorsten Blum

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify aead_hmac_init().

---
 drivers/crypto/marvell/octeontx2/otx2_cptvf_algs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/crypto/marvell/octeontx2/otx2_cptvf_algs.c +1 -2
index 8d9f394d6b50..65ad29f1e47c 100644
@@ -882,8 +882,7 @@ static int aead_hmac_init(struct crypto_aead *cipher,
 	ipad = ctx->ipad;
 	opad = ctx->opad;
 
-	memcpy(ipad, ctx->key, authkeylen);
-	memset(ipad + authkeylen, 0, bs - authkeylen);
+	memcpy_and_pad(ipad, bs, ctx->key, authkeylen, 0);
 	memcpy(opad, ipad, bs);
 
 	for (icount = 0; icount < bs; icount++) {
maintainer
#6 lore raw
In reply to Thorsten Blum
Quoted 7 lines · Thorsten Blum

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify cast5_setkey().

---
 crypto/cast5_generic.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

All applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#7 lore raw
In reply to Thorsten Blum
Quoted 13 lines · On Mon, 21 Sep 2026 08:11:20 +0200 Thorsten Blum <[email protected]> wrote:

Use memcpy_and_pad() instead of separate memcpy() and memset() calls to simplify cast5_setkey().

---
 crypto/cast5_generic.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
crypto/cast5_generic.c +0 -0
index f68330793e0c..1de0dc966a03 100644

crypto/cast5_generic.c int cast5_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len) · line 486

Quoted 7 lines
crypto/cast5_generic.c +1 -3
@@ -483,9 +483,7 @@ int cast5_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
 
 	c->rr = key_len <= 10 ? 1 : 0;
 
-	memset(p_key, 0, 16);
-	memcpy(p_key, key, key_len);
-
+	memcpy_and_pad(p_key, sizeof(p_key), key, key_len, 0);

This will almost certainly be slower and larger.
The fixed size memset() will be two 8byte writes on 64bit, definitely cheaper than the calculations required for the memset.

More worrying in not verifying that the key_len isn't too big. Maybe the length is validated against cia_max_keysize somewhere, but there are whole lot of unrelated constants that have to match.

David

Quoted 4 lines
crypto/cast5_generic.c +0 -0
 	x[0] = be32_to_cpu(p_key[0]);
 	x[1] = be32_to_cpu(p_key[1]);