From 6552455968d5397c0e955a0a8bba99e15c05d4ab Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 10 Dec 2018 11:40:06 -0800 Subject: [PATCH] Minor improvements to the STM32 CubeMX AES-GCM logic. --- wolfcrypt/src/aes.c | 12 ++++++------ wolfcrypt/test/test.c | 7 ++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 9972ad0e1..76140a721 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -8224,7 +8224,7 @@ static WC_INLINE int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, } XMEMCPY(outPadded, in, sz); - if ((authInSz % AES_BLOCK_SIZE) != 0) { + if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) { /* Need to pad the AAD to a full block with zeros. */ authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE; authInPadded = (byte*)XMALLOC(authPadSz, aes->heap, @@ -8446,8 +8446,8 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, defined(WOLFSSL_STM32F7) || \ defined(WOLFSSL_STM32L4)) - /* additional argument checks - STM32 HW only supports 12 byte IV */ - if (ivSz == GCM_NONCE_MID_SZ) { + /* STM32 HW only supports 12 byte IV and 16 byte auth */ + if (ivSz == GCM_NONCE_MID_SZ && authInSz == AES_BLOCK_SIZE) { return wc_AesGcmEncrypt_STM32(aes, out, in, sz, iv, ivSz, authTag, authTagSz, authIn, authInSz); } @@ -8611,7 +8611,7 @@ static WC_INLINE int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out, } XMEMCPY(outPadded, in, sz); - if ((authInSz % AES_BLOCK_SIZE) != 0) { + if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) { /* Need to pad the AAD to a full block with zeros. */ authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE; authInPadded = (byte*)XMALLOC(authPadSz, aes->heap, @@ -8848,8 +8848,8 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz, defined(WOLFSSL_STM32F7) || \ defined(WOLFSSL_STM32L4)) - /* additional argument checks - STM32 HW only supports 12 byte IV */ - if (ivSz == GCM_NONCE_MID_SZ) { + /* STM32 HW only supports 12 byte IV and 16 byte auth */ + if (ivSz == GCM_NONCE_MID_SZ && authInSz == AES_BLOCK_SIZE) { return wc_AesGcmDecrypt_STM32(aes, out, in, sz, iv, ivSz, authTag, authTagSz, authIn, authInSz); } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 71127909a..617a85823 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -769,7 +769,8 @@ initDefaultName(); printf( "AES256 test passed!\n"); #endif #ifdef HAVE_AESGCM - #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO) && !defined(STM32_CRYPTO) + #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO) && \ + !defined(STM32_CRYPTO) if ( (ret = aesgcm_test()) != 0) return err_sys("AES-GCM test failed!\n", ret); else @@ -6630,8 +6631,8 @@ static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz, byte* plain, int plainSz, byte* cipher, int cipherSz, byte* aad, int aadSz, byte* tag, int tagSz) { -Aes enc; -Aes dec; + Aes enc; + Aes dec; byte resultT[AES_BLOCK_SIZE]; byte resultP[AES_BLOCK_SIZE * 3];