Skip to content

Commit 10a2f0b

Browse files
committed
crypto: bcm - Verify GCM/CCM key length in setkey
The setkey function for GCM/CCM algorithms didn't verify the key length before copying the key and subtracting the salt length. This patch delays the copying of the key til after the verification has been done. It also adds checks on the key length to ensure that it's at least as long as the salt. Fixes: 9d12ba8 ("crypto: brcm - Add Broadcom SPU driver") Cc: <stable@vger.kernel.org> Reported-by: kiyin(尹亮) <kiyin@tencent.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 789f900 commit 10a2f0b

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

drivers/crypto/bcm/cipher.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,6 @@ static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
28672867

28682868
ctx->enckeylen = keylen;
28692869
ctx->authkeylen = 0;
2870-
memcpy(ctx->enckey, key, ctx->enckeylen);
28712870

28722871
switch (ctx->enckeylen) {
28732872
case AES_KEYSIZE_128:
@@ -2883,6 +2882,8 @@ static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
28832882
goto badkey;
28842883
}
28852884

2885+
memcpy(ctx->enckey, key, ctx->enckeylen);
2886+
28862887
flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
28872888
ctx->authkeylen);
28882889
flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
@@ -2937,6 +2938,10 @@ static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
29372938
struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
29382939

29392940
flow_log("%s\n", __func__);
2941+
2942+
if (keylen < GCM_ESP_SALT_SIZE)
2943+
return -EINVAL;
2944+
29402945
ctx->salt_len = GCM_ESP_SALT_SIZE;
29412946
ctx->salt_offset = GCM_ESP_SALT_OFFSET;
29422947
memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
@@ -2965,6 +2970,10 @@ static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
29652970
struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
29662971

29672972
flow_log("%s\n", __func__);
2973+
2974+
if (keylen < GCM_ESP_SALT_SIZE)
2975+
return -EINVAL;
2976+
29682977
ctx->salt_len = GCM_ESP_SALT_SIZE;
29692978
ctx->salt_offset = GCM_ESP_SALT_OFFSET;
29702979
memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
@@ -2994,6 +3003,10 @@ static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
29943003
struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
29953004

29963005
flow_log("%s\n", __func__);
3006+
3007+
if (keylen < CCM_ESP_SALT_SIZE)
3008+
return -EINVAL;
3009+
29973010
ctx->salt_len = CCM_ESP_SALT_SIZE;
29983011
ctx->salt_offset = CCM_ESP_SALT_OFFSET;
29993012
memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);

0 commit comments

Comments
 (0)