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>
This commit is contained in:
Iuliana Prodan 2019-07-31 16:05:55 +03:00 committed by Herbert Xu
parent 65526f638e
commit bc67d04e75
2 changed files with 21 additions and 4 deletions

View File

@ -31,6 +31,23 @@ struct crypto_aes_ctx {
extern const u32 crypto_ft_tab[4][256] ____cacheline_aligned; extern const u32 crypto_ft_tab[4][256] ____cacheline_aligned;
extern const u32 crypto_it_tab[4][256] ____cacheline_aligned; extern const u32 crypto_it_tab[4][256] ____cacheline_aligned;
/*
* validate key length for AES algorithms
*/
static inline int aes_check_keylen(unsigned int keylen)
{
switch (keylen) {
case AES_KEYSIZE_128:
case AES_KEYSIZE_192:
case AES_KEYSIZE_256:
break;
default:
return -EINVAL;
}
return 0;
}
int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
unsigned int key_len); unsigned int key_len);

View File

@ -187,11 +187,11 @@ int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
{ {
u32 kwords = key_len / sizeof(u32); u32 kwords = key_len / sizeof(u32);
u32 rc, i, j; u32 rc, i, j;
int err;
if (key_len != AES_KEYSIZE_128 && err = aes_check_keylen(key_len);
key_len != AES_KEYSIZE_192 && if (err)
key_len != AES_KEYSIZE_256) return err;
return -EINVAL;
ctx->key_length = key_len; ctx->key_length = key_len;