crypto: stm32/cryp - check early input data
[ Upstream commit 39e6e699c7fb92bdb2617b596ca4a4ea35c5d2a7 ]
Some auto tests failed because driver wasn't returning the expected
error with some input size/iv value/tag size.
Now:
Return 0 early for empty buffer. (We don't need to start the engine for
an empty input buffer).
Accept any valid authsize for gcm(aes).
Return -EINVAL if iv for ccm(aes) is invalid.
Return -EINVAL if buffer size is a not a multiple of algorithm block size.
Fixes: 9e054ec21e
("crypto: stm32 - Support for STM32 CRYP crypto module")
Signed-off-by: Nicolas Toromanoff <nicolas.toromanoff@foss.st.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:

committed by
Greg Kroah-Hartman

parent
5deb24e503
commit
533af1621d
@@ -799,7 +799,20 @@ static int stm32_cryp_aes_aead_setkey(struct crypto_aead *tfm, const u8 *key,
|
|||||||
static int stm32_cryp_aes_gcm_setauthsize(struct crypto_aead *tfm,
|
static int stm32_cryp_aes_gcm_setauthsize(struct crypto_aead *tfm,
|
||||||
unsigned int authsize)
|
unsigned int authsize)
|
||||||
{
|
{
|
||||||
return authsize == AES_BLOCK_SIZE ? 0 : -EINVAL;
|
switch (authsize) {
|
||||||
|
case 4:
|
||||||
|
case 8:
|
||||||
|
case 12:
|
||||||
|
case 13:
|
||||||
|
case 14:
|
||||||
|
case 15:
|
||||||
|
case 16:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_ccm_setauthsize(struct crypto_aead *tfm,
|
static int stm32_cryp_aes_ccm_setauthsize(struct crypto_aead *tfm,
|
||||||
@@ -823,31 +836,61 @@ static int stm32_cryp_aes_ccm_setauthsize(struct crypto_aead *tfm,
|
|||||||
|
|
||||||
static int stm32_cryp_aes_ecb_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_aes_ecb_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % AES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_AES | FLG_ECB | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_AES | FLG_ECB | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_ecb_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_aes_ecb_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % AES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_AES | FLG_ECB);
|
return stm32_cryp_crypt(req, FLG_AES | FLG_ECB);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_cbc_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_aes_cbc_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % AES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_AES | FLG_CBC | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_AES | FLG_CBC | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_cbc_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_aes_cbc_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % AES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_AES | FLG_CBC);
|
return stm32_cryp_crypt(req, FLG_AES | FLG_CBC);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_ctr_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_aes_ctr_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_AES | FLG_CTR | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_AES | FLG_CTR | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_ctr_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_aes_ctr_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_AES | FLG_CTR);
|
return stm32_cryp_crypt(req, FLG_AES | FLG_CTR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -861,53 +904,122 @@ static int stm32_cryp_aes_gcm_decrypt(struct aead_request *req)
|
|||||||
return stm32_cryp_aead_crypt(req, FLG_AES | FLG_GCM);
|
return stm32_cryp_aead_crypt(req, FLG_AES | FLG_GCM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int crypto_ccm_check_iv(const u8 *iv)
|
||||||
|
{
|
||||||
|
/* 2 <= L <= 8, so 1 <= L' <= 7. */
|
||||||
|
if (iv[0] < 1 || iv[0] > 7)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_ccm_encrypt(struct aead_request *req)
|
static int stm32_cryp_aes_ccm_encrypt(struct aead_request *req)
|
||||||
{
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = crypto_ccm_check_iv(req->iv);
|
||||||
|
if (err)
|
||||||
|
return err;
|
||||||
|
|
||||||
return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM | FLG_ENCRYPT);
|
return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_aes_ccm_decrypt(struct aead_request *req)
|
static int stm32_cryp_aes_ccm_decrypt(struct aead_request *req)
|
||||||
{
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = crypto_ccm_check_iv(req->iv);
|
||||||
|
if (err)
|
||||||
|
return err;
|
||||||
|
|
||||||
return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM);
|
return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_des_ecb_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_des_ecb_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_DES | FLG_ECB | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_DES | FLG_ECB | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_des_ecb_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_des_ecb_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_DES | FLG_ECB);
|
return stm32_cryp_crypt(req, FLG_DES | FLG_ECB);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_des_cbc_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_des_cbc_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_DES | FLG_CBC | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_DES | FLG_CBC | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_des_cbc_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_des_cbc_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_DES | FLG_CBC);
|
return stm32_cryp_crypt(req, FLG_DES | FLG_CBC);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_tdes_ecb_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_tdes_ecb_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_tdes_ecb_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_tdes_ecb_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB);
|
return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_tdes_cbc_encrypt(struct skcipher_request *req)
|
static int stm32_cryp_tdes_cbc_encrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC | FLG_ENCRYPT);
|
return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC | FLG_ENCRYPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stm32_cryp_tdes_cbc_decrypt(struct skcipher_request *req)
|
static int stm32_cryp_tdes_cbc_decrypt(struct skcipher_request *req)
|
||||||
{
|
{
|
||||||
|
if (req->cryptlen % DES_BLOCK_SIZE)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (req->cryptlen == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC);
|
return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user