forked from wolfSSL/wolfssl
Merge pull request #3723 from douzzer/AesCcmEncrypt-zero-inSz-null-in
AES-CCM null payload buffers with inSz zero
This commit is contained in:
@@ -7640,7 +7640,7 @@ int wc_AesCcmCheckTagSize(int sz)
|
|||||||
/* implemented in wolfcrypt/src/port/caam_aes.c */
|
/* implemented in wolfcrypt/src/port/caam_aes.c */
|
||||||
|
|
||||||
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
|
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
|
||||||
/* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
|
/* implemented in wolfcrypt/src/port/silabs/silabs_aes.c */
|
||||||
int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
|
int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
|
||||||
const byte* nonce, word32 nonceSz,
|
const byte* nonce, word32 nonceSz,
|
||||||
byte* authTag, word32 authTagSz,
|
byte* authTag, word32 authTagSz,
|
||||||
@@ -7679,6 +7679,10 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
|
|||||||
status_t status;
|
status_t status;
|
||||||
|
|
||||||
/* sanity check on arguments */
|
/* sanity check on arguments */
|
||||||
|
/* note, LTC_AES_EncryptTagCcm() doesn't allow null src or dst
|
||||||
|
* ptrs even if inSz is zero (ltc_aes_ccm_check_input_args()), so
|
||||||
|
* don't allow it here either.
|
||||||
|
*/
|
||||||
if (aes == NULL || out == NULL || in == NULL || nonce == NULL
|
if (aes == NULL || out == NULL || in == NULL || nonce == NULL
|
||||||
|| authTag == NULL || nonceSz < 7 || nonceSz > 13) {
|
|| authTag == NULL || nonceSz < 7 || nonceSz > 13) {
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
@@ -7879,8 +7883,8 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
|
|||||||
const word32 wordSz = (word32)sizeof(word32);
|
const word32 wordSz = (word32)sizeof(word32);
|
||||||
|
|
||||||
/* sanity check on arguments */
|
/* sanity check on arguments */
|
||||||
if (aes == NULL || out == NULL || in == NULL || nonce == NULL
|
if (aes == NULL || (inSz != 0 && (in == NULL || out == NULL)) ||
|
||||||
|| authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
|
nonce == NULL || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
|
||||||
authTagSz > AES_BLOCK_SIZE)
|
authTagSz > AES_BLOCK_SIZE)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
@@ -7981,9 +7985,9 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
|
|||||||
const word32 wordSz = (word32)sizeof(word32);
|
const word32 wordSz = (word32)sizeof(word32);
|
||||||
|
|
||||||
/* sanity check on arguments */
|
/* sanity check on arguments */
|
||||||
if (aes == NULL || out == NULL || in == NULL || nonce == NULL
|
if (aes == NULL || (inSz != 0 && (in == NULL || out == NULL)) ||
|
||||||
|| authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
|
nonce == NULL || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
|
||||||
authTagSz > AES_BLOCK_SIZE)
|
authTagSz > AES_BLOCK_SIZE)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
/* sanity check on tag size */
|
/* sanity check on tag size */
|
||||||
@@ -8075,7 +8079,8 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
|
|||||||
defined(ACVP_VECTOR_TESTING)
|
defined(ACVP_VECTOR_TESTING)
|
||||||
WOLFSSL_MSG("Preserve output for vector responses");
|
WOLFSSL_MSG("Preserve output for vector responses");
|
||||||
#else
|
#else
|
||||||
XMEMSET(out, 0, inSz);
|
if (inSz > 0)
|
||||||
|
XMEMSET(out, 0, inSz);
|
||||||
#endif
|
#endif
|
||||||
result = AES_CCM_AUTH_E;
|
result = AES_CCM_AUTH_E;
|
||||||
}
|
}
|
||||||
|
@@ -10239,6 +10239,12 @@ WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
|
|||||||
0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
|
0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* tag - authentication - empty plaintext */
|
||||||
|
WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
|
||||||
|
{
|
||||||
|
0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
|
||||||
|
};
|
||||||
|
|
||||||
byte t2[sizeof(t)];
|
byte t2[sizeof(t)];
|
||||||
byte p2[sizeof(p)];
|
byte p2[sizeof(p)];
|
||||||
byte c2[sizeof(c)];
|
byte c2[sizeof(c)];
|
||||||
@@ -10246,6 +10252,7 @@ WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
|
|||||||
byte pl2[sizeof(pl)];
|
byte pl2[sizeof(pl)];
|
||||||
byte cl2[sizeof(cl)];
|
byte cl2[sizeof(cl)];
|
||||||
byte tl2[sizeof(tl)];
|
byte tl2[sizeof(tl)];
|
||||||
|
byte t_empty2[sizeof(t_empty)];
|
||||||
|
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
@@ -10349,6 +10356,62 @@ WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
|
|||||||
if (XMEMCMP(pl, pl2, sizeof(pl2)))
|
if (XMEMCMP(pl, pl2, sizeof(pl2)))
|
||||||
ERROR_OUT(-6520, out);
|
ERROR_OUT(-6520, out);
|
||||||
|
|
||||||
|
/* test empty message as null input or output with nonzero inSz. */
|
||||||
|
result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
|
||||||
|
iv, sizeof(iv), t_empty2, sizeof(t_empty2),
|
||||||
|
a, sizeof(a));
|
||||||
|
if (result != BAD_FUNC_ARG)
|
||||||
|
ERROR_OUT(-6527, out);
|
||||||
|
result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
|
||||||
|
iv, sizeof(iv), t_empty2, sizeof(t_empty2),
|
||||||
|
a, sizeof(a));
|
||||||
|
if (result != BAD_FUNC_ARG)
|
||||||
|
ERROR_OUT(-6528, out);
|
||||||
|
result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
|
||||||
|
iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
|
||||||
|
sizeof(a));
|
||||||
|
if (result != BAD_FUNC_ARG)
|
||||||
|
ERROR_OUT(-6529, out);
|
||||||
|
result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
|
||||||
|
iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
|
||||||
|
sizeof(a));
|
||||||
|
if (result != BAD_FUNC_ARG)
|
||||||
|
ERROR_OUT(-6530, out);
|
||||||
|
|
||||||
|
/* test empty message as null input and output with zero inSz --
|
||||||
|
* must either succeed, or fail early with BAD_FUNC_ARG.
|
||||||
|
*/
|
||||||
|
result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
|
||||||
|
iv, sizeof(iv), t_empty2, sizeof(t_empty2),
|
||||||
|
a, sizeof(a));
|
||||||
|
if (result != BAD_FUNC_ARG) {
|
||||||
|
if (result != 0)
|
||||||
|
ERROR_OUT(-6521, out);
|
||||||
|
if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
|
||||||
|
ERROR_OUT(-6522, out);
|
||||||
|
|
||||||
|
result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
|
||||||
|
0 /* inSz */, iv, sizeof(iv), t_empty2,
|
||||||
|
sizeof(t_empty2), a, sizeof(a));
|
||||||
|
if (result != 0)
|
||||||
|
ERROR_OUT(-6523, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* test empty message as zero-length string -- must work. */
|
||||||
|
result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
|
||||||
|
sizeof(iv), t_empty2, sizeof(t_empty2), a,
|
||||||
|
sizeof(a));
|
||||||
|
if (result != 0)
|
||||||
|
ERROR_OUT(-6524, out);
|
||||||
|
if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
|
||||||
|
ERROR_OUT(-6525, out);
|
||||||
|
|
||||||
|
result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
|
||||||
|
iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
|
||||||
|
sizeof(a));
|
||||||
|
if (result != 0)
|
||||||
|
ERROR_OUT(-6526, out);
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
|
Reference in New Issue
Block a user