@@ -12808,6 +12808,18 @@ int wc_AesGcmEncryptUpdate(Aes* aes, byte* out, const byte* in, word32 sz,
1280812808 ret = MISSING_IV;
1280912809 }
1281012810
12811+ /* Prevent overflow of aes->cSz and ->aSz. Per NIST SP 800-38D section
12812+ * 5.2.1.1, the maximum allowed ciphertext limit is 2^32 - 2 blocks, but we
12813+ * currently pass around the cunulative sizes in bytes as word32s, so we
12814+ * can't currently support the maximum allowed.
12815+ */
12816+ if ((ret == 0) &&
12817+ (((aes->cSz > 0xffffffff - sz)) ||
12818+ ((aes->aSz > 0xffffffff - authInSz))))
12819+ {
12820+ ret = AES_GCM_OVERFLOW_E;
12821+ }
12822+
1281112823 if ((ret == 0) && aes->ctrSet && (aes->aSz == 0) && (aes->cSz == 0)) {
1281212824 aes->invokeCtr[0]++;
1281312825 if (aes->invokeCtr[0] == 0) {
@@ -12950,6 +12962,18 @@ int wc_AesGcmDecryptUpdate(Aes* aes, byte* out, const byte* in, word32 sz,
1295012962 ret = MISSING_IV;
1295112963 }
1295212964
12965+ /* Prevent overflow of aes->cSz and ->aSz. Per NIST SP 800-38D section
12966+ * 5.2.1.1, the maximum allowed ciphertext limit is 2^32 - 2 blocks, but we
12967+ * currently pass around the cunulative sizes in bytes as word32s, so we
12968+ * can't currently support the maximum allowed.
12969+ */
12970+ if ((ret == 0) &&
12971+ (((aes->cSz > 0xffffffff - sz)) ||
12972+ ((aes->aSz > 0xffffffff - authInSz))))
12973+ {
12974+ ret = AES_GCM_OVERFLOW_E;
12975+ }
12976+
1295312977 if (ret == 0) {
1295412978 /* Decrypt with AAD and/or cipher text. */
1295512979 #ifdef WOLFSSL_AESNI
@@ -13586,6 +13610,17 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
1358613610 return BAD_FUNC_ARG;
1358713611 }
1358813612
13613+ lenSz = (byte)(WC_AES_BLOCK_SIZE - 1U - nonceSz);
13614+
13615+ /* With a large nonce, B[] runs out of room to represent inSz, and beyond
13616+ * that, the counter itself can wrap.
13617+ */
13618+ if ((lenSz < sizeof(inSz)) &&
13619+ (inSz >= ((word32)1 << (lenSz * 8))))
13620+ {
13621+ return AES_CCM_OVERFLOW_E;
13622+ }
13623+
1358913624#ifdef WOLF_CRYPTO_CB
1359013625 #ifndef WOLF_CRYPTO_CB_FIND
1359113626 if (aes->devId != INVALID_DEVID)
@@ -13602,7 +13637,7 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
1360213637
1360313638 XMEMSET(A, 0, sizeof(A));
1360413639 XMEMCPY(B+1, nonce, nonceSz);
13605- lenSz = (byte)(WC_AES_BLOCK_SIZE - 1U - nonceSz);
13640+
1360613641 B[0] = (byte)((authInSz > 0 ? 64 : 0)
1360713642 + (8 * (((byte)authTagSz - 2) / 2))
1360813643 + (lenSz - 1));
@@ -13739,6 +13774,17 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
1373913774 return BAD_FUNC_ARG;
1374013775 }
1374113776
13777+ lenSz = (byte)(WC_AES_BLOCK_SIZE - 1U - nonceSz);
13778+
13779+ /* With a large nonce, B[] runs out of room to represent inSz, and beyond
13780+ * that, the counter itself can wrap.
13781+ */
13782+ if ((lenSz < sizeof(inSz)) &&
13783+ (inSz >= ((word32)1 << (lenSz * 8))))
13784+ {
13785+ return AES_CCM_OVERFLOW_E;
13786+ }
13787+
1374213788#ifdef WOLF_CRYPTO_CB
1374313789 #ifndef WOLF_CRYPTO_CB_FIND
1374413790 if (aes->devId != INVALID_DEVID)
@@ -13757,7 +13803,6 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
1375713803 oSz = inSz;
1375813804 XMEMSET(A, 0, sizeof A);
1375913805 XMEMCPY(B+1, nonce, nonceSz);
13760- lenSz = (byte)(WC_AES_BLOCK_SIZE - 1U - nonceSz);
1376113806
1376213807 B[0] = (byte)(lenSz - 1U);
1376313808 for (i = 0; i < lenSz; i++)
0 commit comments