Skip to content

Commit bc67d04

Browse files
iuliana-prodanherbertx
authored andcommitted
crypto: aes - helper function to validate key length for AES algorithms
Add inline helper function to check key length for AES algorithms. The key can be 128, 192 or 256 bits size. This function is used in the generic aes implementation. Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 65526f6 commit bc67d04

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎include/crypto/aes.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ struct crypto_aes_ctx {
3131
extern const u32 crypto_ft_tab[4][256] ____cacheline_aligned;
3232
extern const u32 crypto_it_tab[4][256] ____cacheline_aligned;
3333

34+
/*
35+
* validate key length for AES algorithms
36+
*/
37+
static inline int aes_check_keylen(unsigned int keylen)
38+
{
39+
switch (keylen) {
40+
case AES_KEYSIZE_128:
41+
case AES_KEYSIZE_192:
42+
case AES_KEYSIZE_256:
43+
break;
44+
default:
45+
return -EINVAL;
46+
}
47+
48+
return 0;
49+
}
50+
3451
int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
3552
unsigned int key_len);
3653

‎lib/crypto/aes.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
187187
{
188188
u32 kwords = key_len / sizeof(u32);
189189
u32 rc, i, j;
190+
int err;
190191

191-
if (key_len != AES_KEYSIZE_128 &&
192-
key_len != AES_KEYSIZE_192 &&
193-
key_len != AES_KEYSIZE_256)
194-
return -EINVAL;
192+
err = aes_check_keylen(key_len);
193+
if (err)
194+
return err;
195195

196196
ctx->key_length = key_len;
197197

0 commit comments

Comments
 (0)