Ignore:
Timestamp:
Jun 22, 2021, 9:00:19 PM (3 years ago)
Author:
coas-nagasima
Message:

WolfSSLとAzure IoT SDKを更新

Location:
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/aes.c

    r457 r464  
    2020 */
    2121
    22 
     22/*
     23
     24DESCRIPTION
     25This library provides the interfaces to the Advanced Encryption Standard (AES)
     26for encrypting and decrypting data. AES is the standard known for a symmetric
     27block cipher mechanism that uses n-bit binary string parameter key with 128-bits,
     28192-bits, and 256-bits of key sizes.
     29
     30*/
    2331#ifdef HAVE_CONFIG_H
    2432    #include <config.h>
     
    4553
    4654#include <wolfssl/wolfcrypt/aes.h>
     55
     56#ifdef WOLFSSL_AESNI
     57#include <wmmintrin.h>
     58#include <emmintrin.h>
     59#include <smmintrin.h>
     60#endif /* WOLFSSL_AESNI */
     61
    4762#include <wolfssl/wolfcrypt/cpuid.h>
    4863
    4964#ifdef WOLF_CRYPTO_CB
    5065    #include <wolfssl/wolfcrypt/cryptocb.h>
     66#endif
     67
     68#ifdef WOLFSSL_IMXRT_DCP
     69    #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
    5170#endif
    5271
     
    289308/* Define AES implementation includes and functions */
    290309#if defined(STM32_CRYPTO)
    291      /* STM32F2/F4/F7/L4 hardware AES support for ECB, CBC, CTR and GCM modes */
     310     /* STM32F2/F4/F7/L4/L5/H7/WB55 hardware AES support for ECB, CBC, CTR and GCM modes */
    292311
    293312#if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
     
    308327            return ret;
    309328
    310     #ifdef STM32_CRYPTO_AES_ONLY
     329        ret = wolfSSL_CryptHwMutexLock();
     330        if (ret != 0)
     331            return ret;
     332
     333    #if defined(STM32_HAL_V2)
     334        hcryp.Init.Algorithm  = CRYP_AES_ECB;
     335    #elif defined(STM32_CRYPTO_AES_ONLY)
    311336        hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
    312337        hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_ECB;
    313338        hcryp.Init.KeyWriteFlag  = CRYP_KEY_WRITE_ENABLE;
    314     #elif defined(STM32_HAL_V2)
    315         hcryp.Init.Algorithm  = CRYP_AES_ECB;
    316339    #endif
    317340        HAL_CRYP_Init(&hcryp);
    318341
    319     #ifdef STM32_CRYPTO_AES_ONLY
     342    #if defined(STM32_HAL_V2)
     343        ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
     344            (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
     345    #elif defined(STM32_CRYPTO_AES_ONLY)
    320346        ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
    321347            outBlock, STM32_HAL_TIMEOUT);
    322     #elif defined(STM32_HAL_V2)
    323         ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
    324             (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
    325348    #else
    326349        ret = HAL_CRYP_AESECB_Encrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
     
    332355        HAL_CRYP_DeInit(&hcryp);
    333356
    334     #else /* STD_PERI_LIB */
     357    #else /* Standard Peripheral Library */
    335358        ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
     359        if (ret != 0)
     360            return ret;
     361
     362        ret = wolfSSL_CryptHwMutexLock();
    336363        if (ret != 0)
    337364            return ret;
     
    370397        CRYP_Cmd(DISABLE);
    371398    #endif /* WOLFSSL_STM32_CUBEMX */
     399        wolfSSL_CryptHwMutexUnLock();
    372400
    373401        return ret;
     
    392420            return ret;
    393421
    394     #ifdef STM32_CRYPTO_AES_ONLY
    395         hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
     422        ret = wolfSSL_CryptHwMutexLock();
     423        if (ret != 0)
     424            return ret;
     425
     426    #if defined(STM32_HAL_V2)
     427        hcryp.Init.Algorithm  = CRYP_AES_ECB;
     428    #elif defined(STM32_CRYPTO_AES_ONLY)
     429        hcryp.Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
    396430        hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_ECB;
    397431        hcryp.Init.KeyWriteFlag  = CRYP_KEY_WRITE_ENABLE;
    398     #elif defined(STM32_HAL_V2)
    399         hcryp.Init.Algorithm  = CRYP_AES_ECB;
    400432    #endif
    401433        HAL_CRYP_Init(&hcryp);
    402434
    403     #ifdef STM32_CRYPTO_AES_ONLY
     435    #if defined(STM32_HAL_V2)
     436        ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
     437            (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
     438    #elif defined(STM32_CRYPTO_AES_ONLY)
    404439        ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
    405440            outBlock, STM32_HAL_TIMEOUT);
    406     #elif defined(STM32_HAL_V2)
    407         ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
    408             (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
    409441    #else
    410442        ret = HAL_CRYP_AESECB_Decrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
     
    416448        HAL_CRYP_DeInit(&hcryp);
    417449
    418     #else /* STD_PERI_LIB */
     450    #else /* Standard Peripheral Library */
    419451        ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
     452        if (ret != 0)
     453            return ret;
     454
     455        ret = wolfSSL_CryptHwMutexLock();
    420456        if (ret != 0)
    421457            return ret;
     
    463499        CRYP_Cmd(DISABLE);
    464500    #endif /* WOLFSSL_STM32_CUBEMX */
     501        wolfSSL_CryptHwMutexUnLock();
    465502
    466503        return ret;
     
    481518        #undef NEED_AES_TABLES
    482519        #undef GCM_TABLE
    483     #else
     520    #endif
     521
    484522        /* if LTC doesn't have GCM, use software with LTC AES ECB mode */
    485523        static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    486524        {
    487             wc_AesEncryptDirect(aes, outBlock, inBlock);
     525            word32 keySize = 0;
     526            byte* key = (byte*)aes->key;
     527            wc_AesGetKeySize(aes, &keySize);
     528
     529            if (wolfSSL_CryptHwMutexLock() == 0) {
     530                LTC_AES_EncryptEcb(LTC_BASE, inBlock, outBlock, AES_BLOCK_SIZE,
     531                    key, keySize);
     532                wolfSSL_CryptHwMutexUnLock();
     533            }
    488534            return 0;
    489535        }
     536        #ifdef HAVE_AES_DECRYPT
    490537        static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    491538        {
    492             wc_AesDecryptDirect(aes, outBlock, inBlock);
     539            word32 keySize = 0;
     540            byte* key = (byte*)aes->key;
     541            wc_AesGetKeySize(aes, &keySize);
     542
     543            if (wolfSSL_CryptHwMutexLock() == 0) {
     544                LTC_AES_DecryptEcb(LTC_BASE, inBlock, outBlock, AES_BLOCK_SIZE,
     545                    key, keySize, kLTC_EncryptKey);
     546                wolfSSL_CryptHwMutexUnLock();
     547            }
    493548            return 0;
    494549        }
    495     #endif
     550        #endif
     551
    496552#elif defined(FREESCALE_MMCAU)
    497553    /* Freescale mmCAU hardware AES support for Direct, CBC, CCM, GCM modes
     
    508564    static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    509565    {
    510         int ret;
    511 
    512     #ifdef FREESCALE_MMCAU_CLASSIC
    513         if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
    514             WOLFSSL_MSG("Bad cau_aes_encrypt alignment");
    515             return BAD_ALIGN_E;
    516         }
    517     #endif
    518 
    519         ret = wolfSSL_CryptHwMutexLock();
    520         if(ret == 0) {
     566        if (wolfSSL_CryptHwMutexLock() == 0) {
    521567        #ifdef FREESCALE_MMCAU_CLASSIC
     568            if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
     569                WOLFSSL_MSG("Bad cau_aes_encrypt alignment");
     570                return BAD_ALIGN_E;
     571            }
    522572            cau_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
    523573        #else
     
    527577            wolfSSL_CryptHwMutexUnLock();
    528578        }
    529         return ret;
     579        return 0;
    530580    }
    531581    #ifdef HAVE_AES_DECRYPT
    532582    static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    533583    {
    534         int ret;
    535 
    536     #ifdef FREESCALE_MMCAU_CLASSIC
    537         if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
    538             WOLFSSL_MSG("Bad cau_aes_decrypt alignment");
    539             return BAD_ALIGN_E;
    540         }
    541     #endif
    542 
    543         ret = wolfSSL_CryptHwMutexLock();
    544         if(ret == 0) {
     584        if (wolfSSL_CryptHwMutexLock() == 0) {
    545585        #ifdef FREESCALE_MMCAU_CLASSIC
     586            if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
     587                WOLFSSL_MSG("Bad cau_aes_decrypt alignment");
     588                return BAD_ALIGN_E;
     589            }
    546590            cau_aes_decrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
    547591        #else
     
    551595            wolfSSL_CryptHwMutexUnLock();
    552596        }
    553         return ret;
     597        return 0;
    554598    }
    555599    #endif /* HAVE_AES_DECRYPT */
     
    562606    static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    563607    {
     608        /* Thread mutex protection handled in Pic32Crypto */
    564609        return wc_Pic32AesCrypt(aes->key, aes->keylen, NULL, 0,
    565610            outBlock, inBlock, AES_BLOCK_SIZE,
     
    571616    static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    572617    {
     618        /* Thread mutex protection handled in Pic32Crypto */
    573619        return wc_Pic32AesCrypt(aes->key, aes->keylen, NULL, 0,
    574620            outBlock, inBlock, AES_BLOCK_SIZE,
     
    583629    static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    584630    {
    585         return nrf51_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
     631        int ret;
     632        ret = wolfSSL_CryptHwMutexLock();
     633        if (ret == 0) {
     634            ret = nrf51_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
     635            wolfSSL_CryptHwMutexUnLock();
     636        }
     637        return ret;
    586638    }
    587639
     
    598650    static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    599651    {
     652        /* Thread mutex protection handled in esp_aes_hw_InUse */
    600653        return wc_esp32AesEncrypt(aes, inBlock, outBlock);
    601654    }
     
    605658    static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
    606659    {
     660        /* Thread mutex protection handled in esp_aes_hw_InUse */
    607661       return wc_esp32AesDecrypt(aes, inBlock, outBlock);
    608662    }
     
    722776        {
    723777            int nr;
    724             Aes temp_key;
    725             __m128i *Key_Schedule = (__m128i*)aes->key;
    726             __m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
     778#ifdef WOLFSSL_SMALL_STACK
     779            Aes *temp_key;
     780#else
     781            Aes temp_key[1];
     782#endif
     783            __m128i *Key_Schedule;
     784            __m128i *Temp_Key_Schedule;
    727785
    728786            if (!userKey || !aes)
    729787                return BAD_FUNC_ARG;
    730788
    731             if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
     789#ifdef WOLFSSL_SMALL_STACK
     790            if ((temp_key = (Aes *)XMALLOC(sizeof *aes, aes->heap,
     791                                           DYNAMIC_TYPE_AES)) == NULL)
     792                return MEMORY_E;
     793#endif
     794
     795            if (AES_set_encrypt_key(userKey,bits,temp_key) == BAD_FUNC_ARG) {
     796#ifdef WOLFSSL_SMALL_STACK
     797                XFREE(temp_key, aes->heap, DYNAMIC_TYPE_AES);
     798#endif
    732799                return BAD_FUNC_ARG;
    733 
    734             nr = temp_key.rounds;
     800            }
     801
     802            Key_Schedule = (__m128i*)aes->key;
     803            Temp_Key_Schedule = (__m128i*)temp_key->key;
     804
     805            nr = temp_key->rounds;
    735806            aes->rounds = nr;
     807
     808            SAVE_VECTOR_REGISTERS();
    736809
    737810            Key_Schedule[nr] = Temp_Key_Schedule[0];
     
    758831            Key_Schedule[0] = Temp_Key_Schedule[nr];
    759832
     833            RESTORE_VECTOR_REGISTERS();
     834
     835#ifdef WOLFSSL_SMALL_STACK
     836            XFREE(temp_key, aes->heap, DYNAMIC_TYPE_AES);
     837#endif
     838
    760839            return 0;
    761840        }
     
    772851
    773852#elif defined(WOLFSSL_AFALG)
     853    /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
     854
    774855#elif defined(WOLFSSL_DEVCRYPTO_AES)
     856    /* implemented in wolfcrypt/src/port/devcrypto/devcrypto_aes.c */
    775857
    776858#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
     
    792874            int sz)
    793875    {
    794         uint32_t ret;
     876        word32 ret;
    795877
    796878        if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
     
    830912
    831913        if (ret != SSP_SUCCESS) {
    832            /* revert input */
     914            /* revert input */
    833915            ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz);
    834916            return WC_HW_E;
     
    850932            int sz)
    851933    {
    852         uint32_t ret;
     934        word32 ret;
    853935
    854936        if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
     
    901983        return 0;
    902984    }
    903 
    904     #endif
     985    #endif /* HAVE_AES_DECRYPT */
    905986
    906987    #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT)
     
    9271008#ifdef NEED_AES_TABLES
    9281009
    929 static const word32 rcon[] = {
     1010static const FLASH_QUALIFIER word32 rcon[] = {
    9301011    0x01000000, 0x02000000, 0x04000000, 0x08000000,
    9311012    0x10000000, 0x20000000, 0x40000000, 0x80000000,
     
    9351016
    9361017#ifndef WOLFSSL_AES_SMALL_TABLES
    937 static const word32 Te[4][256] = {
     1018static const FLASH_QUALIFIER word32 Te[4][256] = {
    9381019{
    9391020    0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
     
    12031284
    12041285#ifdef HAVE_AES_DECRYPT
    1205 static const word32 Td[4][256] = {
     1286static const FLASH_QUALIFIER word32 Td[4][256] = {
    12061287{
    12071288    0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
     
    14711552};
    14721553#endif /* HAVE_AES_DECRYPT */
    1473 #endif
     1554#endif /* WOLFSSL_AES_SMALL_TABLES */
    14741555
    14751556#ifdef HAVE_AES_DECRYPT
    14761557#if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) \
    1477                         || defined(WOLFSSL_AES_DIRECT)
    1478 static const byte Td4[256] =
     1558            || defined(WOLFSSL_AES_DIRECT)
     1559static const FLASH_QUALIFIER byte Td4[256] =
    14791560{
    14801561    0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
     
    15621643}
    15631644
     1645#if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
    15641646static word32 inv_col_mul(word32 t, int i9, int ib, int id, int ie)
    15651647{
     
    15721654}
    15731655#endif
    1574 
    1575 #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM)
     1656#endif
     1657
     1658#if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || \
     1659                                    defined(HAVE_AESCCM) || defined(HAVE_AESGCM)
    15761660
    15771661#ifndef WC_CACHE_LINE_SZ
     
    15861670
    15871671
     1672#ifndef WC_NO_CACHE_RESISTANT
    15881673#ifndef WOLFSSL_AES_SMALL_TABLES
    15891674/* load 4 Te Tables into cache by cache line stride */
     
    16141699}
    16151700#endif
     1701#endif
    16161702
    16171703/* Software AES - ECB Encrypt */
     
    16231709    const word32* rk = aes->key;
    16241710
     1711#ifdef DEBUG_WOLFSSL
    16251712    if (r > 7 || r == 0) {
    16261713        WOLFSSL_MSG("AesEncrypt encountered improper key, set it up");
    16271714        return;  /* stop instead of seg-faulting, set up your keys! */
    16281715    }
     1716#endif
    16291717
    16301718#ifdef WOLFSSL_AESNI
     
    16501738
    16511739            XMEMCPY(tmp_align, inBlock, AES_BLOCK_SIZE);
     1740            SAVE_VECTOR_REGISTERS();
    16521741            AES_ECB_encrypt(tmp_align, tmp_align, AES_BLOCK_SIZE,
    16531742                    (byte*)aes->key, aes->rounds);
     1743            RESTORE_VECTOR_REGISTERS();
    16541744            XMEMCPY(outBlock, tmp_align, AES_BLOCK_SIZE);
    16551745            XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
     
    16611751        }
    16621752
     1753        SAVE_VECTOR_REGISTERS();
    16631754        AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
    16641755                        aes->rounds);
     1756        RESTORE_VECTOR_REGISTERS();
    16651757
    16661758        return;
     
    16751767    AES_ECB_encrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE);
    16761768    return;
     1769#endif
     1770
     1771#if defined(WOLFSSL_IMXRT_DCP)
     1772    if (aes->keylen == 16) {
     1773        DCPAesEcbEncrypt(aes, outBlock, inBlock, AES_BLOCK_SIZE);
     1774        return;
     1775    }
    16771776#endif
    16781777
     
    16821781     */
    16831782    XMEMCPY(&s0, inBlock,                  sizeof(s0));
    1684     XMEMCPY(&s1, inBlock + sizeof(s0),    sizeof(s1));
     1783    XMEMCPY(&s1, inBlock +     sizeof(s0), sizeof(s1));
    16851784    XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
    16861785    XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
     
    17001799
    17011800#ifndef WOLFSSL_AES_SMALL_TABLES
     1801#ifndef WC_NO_CACHE_RESISTANT
    17021802    s0 |= PreFetchTe();
    1703 
     1803#endif
     1804
     1805#ifndef WOLFSSL_AES_NO_UNROLL
     1806/* Unroll the loop. */
     1807#define ENC_ROUND_T_S(o)                                          \
     1808    t0 = Te[0][GETBYTE(s0, 3)] ^ Te[1][GETBYTE(s1, 2)] ^          \
     1809         Te[2][GETBYTE(s2, 1)] ^ Te[3][GETBYTE(s3, 0)] ^ rk[o+4]; \
     1810    t1 = Te[0][GETBYTE(s1, 3)] ^ Te[1][GETBYTE(s2, 2)] ^          \
     1811         Te[2][GETBYTE(s3, 1)] ^ Te[3][GETBYTE(s0, 0)] ^ rk[o+5]; \
     1812    t2 = Te[0][GETBYTE(s2, 3)] ^ Te[1][GETBYTE(s3, 2)] ^          \
     1813         Te[2][GETBYTE(s0, 1)] ^ Te[3][GETBYTE(s1, 0)] ^ rk[o+6]; \
     1814    t3 = Te[0][GETBYTE(s3, 3)] ^ Te[1][GETBYTE(s0, 2)] ^          \
     1815         Te[2][GETBYTE(s1, 1)] ^ Te[3][GETBYTE(s2, 0)] ^ rk[o+7]
     1816#define ENC_ROUND_S_T(o)                                          \
     1817    s0 = Te[0][GETBYTE(t0, 3)] ^ Te[1][GETBYTE(t1, 2)] ^          \
     1818         Te[2][GETBYTE(t2, 1)] ^ Te[3][GETBYTE(t3, 0)] ^ rk[o+0]; \
     1819    s1 = Te[0][GETBYTE(t1, 3)] ^ Te[1][GETBYTE(t2, 2)] ^          \
     1820         Te[2][GETBYTE(t3, 1)] ^ Te[3][GETBYTE(t0, 0)] ^ rk[o+1]; \
     1821    s2 = Te[0][GETBYTE(t2, 3)] ^ Te[1][GETBYTE(t3, 2)] ^          \
     1822         Te[2][GETBYTE(t0, 1)] ^ Te[3][GETBYTE(t1, 0)] ^ rk[o+2]; \
     1823    s3 = Te[0][GETBYTE(t3, 3)] ^ Te[1][GETBYTE(t0, 2)] ^          \
     1824         Te[2][GETBYTE(t1, 1)] ^ Te[3][GETBYTE(t2, 0)] ^ rk[o+3]
     1825
     1826                       ENC_ROUND_T_S( 0);
     1827    ENC_ROUND_S_T( 8); ENC_ROUND_T_S( 8);
     1828    ENC_ROUND_S_T(16); ENC_ROUND_T_S(16);
     1829    ENC_ROUND_S_T(24); ENC_ROUND_T_S(24);
     1830    ENC_ROUND_S_T(32); ENC_ROUND_T_S(32);
     1831    if (r > 5) {
     1832        ENC_ROUND_S_T(40); ENC_ROUND_T_S(40);
     1833        if (r > 6) {
     1834            ENC_ROUND_S_T(48); ENC_ROUND_T_S(48);
     1835        }
     1836    }
     1837    rk += r * 8;
     1838#else
    17041839    /*
    17051840     * Nr - 1 full rounds:
     
    17621897            rk[3];
    17631898    }
     1899#endif
    17641900
    17651901    /*
     
    17931929        rk[3];
    17941930#else
     1931#ifndef WC_NO_CACHE_RESISTANT
    17951932    s0 |= PreFetchSBox();
     1933#endif
    17961934
    17971935    r *= 2;
     
    18802018
    18812019    XMEMCPY(outBlock,                  &s0, sizeof(s0));
    1882     XMEMCPY(outBlock + sizeof(s0),    &s1, sizeof(s1));
     2020    XMEMCPY(outBlock +     sizeof(s0), &s1, sizeof(s1));
    18832021    XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
    18842022    XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
     
    18912029     defined(WOLFSSL_AES_DIRECT)
    18922030
     2031#ifndef WC_NO_CACHE_RESISTANT
    18932032#ifndef WOLFSSL_AES_SMALL_TABLES
    18942033/* load 4 Td Tables into cache by cache line stride */
     
    19192058    return x;
    19202059}
     2060#endif
    19212061
    19222062/* Software AES - ECB Decrypt */
     
    19262066    word32 t0, t1, t2, t3;
    19272067    word32 r = aes->rounds >> 1;
    1928 
    19292068    const word32* rk = aes->key;
     2069
     2070#ifdef DEBUG_WOLFSSL
    19302071    if (r > 7 || r == 0) {
    19312072        WOLFSSL_MSG("AesDecrypt encountered improper key, set it up");
    19322073        return;  /* stop instead of seg-faulting, set up your keys! */
    19332074    }
     2075#endif
     2076
    19342077#ifdef WOLFSSL_AESNI
    19352078    if (haveAESNI && aes->use_aesni) {
     
    19462089        if ((const byte*)aes->tmp != inBlock)
    19472090            XMEMCPY(aes->tmp, inBlock, AES_BLOCK_SIZE);
     2091        SAVE_VECTOR_REGISTERS();
    19482092        AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
    19492093                        aes->rounds);
     2094        RESTORE_VECTOR_REGISTERS();
    19502095        return;
    19512096    }
     
    19582103#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
    19592104    return AES_ECB_decrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE);
     2105#endif
     2106#if defined(WOLFSSL_IMXRT_DCP)
     2107    if (aes->keylen == 16) {
     2108        DCPAesEcbDecrypt(aes, outBlock, inBlock, AES_BLOCK_SIZE);
     2109        return;
     2110    }
    19602111#endif
    19612112
     
    19822133
    19832134#ifndef WOLFSSL_AES_SMALL_TABLES
     2135#ifndef WC_NO_CACHE_RESISTANT
    19842136    s0 |= PreFetchTd();
     2137#endif
     2138
     2139#ifndef WOLFSSL_AES_NO_UNROLL
     2140/* Unroll the loop. */
     2141#define DEC_ROUND_T_S(o)                                          \
     2142    t0 = Td[0][GETBYTE(s0, 3)] ^ Td[1][GETBYTE(s3, 2)] ^          \
     2143         Td[2][GETBYTE(s2, 1)] ^ Td[3][GETBYTE(s1, 0)] ^ rk[o+4]; \
     2144    t1 = Td[0][GETBYTE(s1, 3)] ^ Td[1][GETBYTE(s0, 2)] ^          \
     2145         Td[2][GETBYTE(s3, 1)] ^ Td[3][GETBYTE(s2, 0)] ^ rk[o+5]; \
     2146    t2 = Td[0][GETBYTE(s2, 3)] ^ Td[1][GETBYTE(s1, 2)] ^          \
     2147         Td[2][GETBYTE(s0, 1)] ^ Td[3][GETBYTE(s3, 0)] ^ rk[o+6]; \
     2148    t3 = Td[0][GETBYTE(s3, 3)] ^ Td[1][GETBYTE(s2, 2)] ^          \
     2149         Td[2][GETBYTE(s1, 1)] ^ Td[3][GETBYTE(s0, 0)] ^ rk[o+7]
     2150#define DEC_ROUND_S_T(o)                                          \
     2151    s0 = Td[0][GETBYTE(t0, 3)] ^ Td[1][GETBYTE(t3, 2)] ^          \
     2152         Td[2][GETBYTE(t2, 1)] ^ Td[3][GETBYTE(t1, 0)] ^ rk[o+0]; \
     2153    s1 = Td[0][GETBYTE(t1, 3)] ^ Td[1][GETBYTE(t0, 2)] ^          \
     2154         Td[2][GETBYTE(t3, 1)] ^ Td[3][GETBYTE(t2, 0)] ^ rk[o+1]; \
     2155    s2 = Td[0][GETBYTE(t2, 3)] ^ Td[1][GETBYTE(t1, 2)] ^          \
     2156         Td[2][GETBYTE(t0, 1)] ^ Td[3][GETBYTE(t3, 0)] ^ rk[o+2]; \
     2157    s3 = Td[0][GETBYTE(t3, 3)] ^ Td[1][GETBYTE(t2, 2)] ^          \
     2158         Td[2][GETBYTE(t1, 1)] ^ Td[3][GETBYTE(t0, 0)] ^ rk[o+3]
     2159
     2160                       DEC_ROUND_T_S( 0);
     2161    DEC_ROUND_S_T( 8); DEC_ROUND_T_S( 8);
     2162    DEC_ROUND_S_T(16); DEC_ROUND_T_S(16);
     2163    DEC_ROUND_S_T(24); DEC_ROUND_T_S(24);
     2164    DEC_ROUND_S_T(32); DEC_ROUND_T_S(32);
     2165    if (r > 5) {
     2166        DEC_ROUND_S_T(40); DEC_ROUND_T_S(40);
     2167        if (r > 6) {
     2168            DEC_ROUND_S_T(48); DEC_ROUND_T_S(48);
     2169        }
     2170    }
     2171    rk += r * 8;
     2172#else
    19852173
    19862174    /*
     
    20442232            rk[3];
    20452233    }
     2234#endif
    20462235    /*
    20472236     * apply last round and
     
    20492238     */
    20502239
     2240#ifndef WC_NO_CACHE_RESISTANT
    20512241    t0 |= PreFetchTd4();
     2242#endif
    20522243
    20532244    s0 =
     
    20762267        rk[3];
    20772268#else
     2269#ifndef WC_NO_CACHE_RESISTANT
    20782270    s0 |= PreFetchTd4();
     2271#endif
    20792272
    20802273    r *= 2;
     
    22022395        aes->left = 0;
    22032396    #endif
    2204 
    22052397        return wc_AesSetIV(aes, iv);
    22062398    }
     
    23892581        aes->keylen = keylen;
    23902582        aes->rounds = keylen/4 + 6;
     2583        XMEMCPY(aes->key, userKey, keylen);
    23912584        ret = nrf51_aes_set_key(userKey);
    23922585
     
    24752668        XMEMCPY(aes->key, userKey, keylen);
    24762669
    2477         aes->ctx.key.pKey = (uint8_t*)aes->key;
     2670        aes->ctx.key.pKey = (byte*)aes->key;
    24782671        aes->ctx.key.keySize= keylen;
    24792672
     
    25172710    /* implemented in wolfcrypt/src/port/devcrypto/devcrypto_aes.c */
    25182711
     2712#elif defined(WOLFSSL_SILABS_SE_ACCEL)
     2713    /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
     2714
    25192715#else
    25202716
    25212717    /* Software AES - SetKey */
    25222718    static int wc_AesSetKeyLocal(Aes* aes, const byte* userKey, word32 keylen,
    2523                 const byte* iv, int dir)
     2719                const byte* iv, int dir, int checkKeyLen)
    25242720    {
    2525         word32 *rk = aes->key;
     2721        int ret;
     2722        word32 *rk;
    25262723    #ifdef NEED_AES_TABLES
    25272724        word32 temp;
    25282725        unsigned int i = 0;
    25292726    #endif
    2530 
    2531         #ifdef WOLFSSL_AESNI
    2532             aes->use_aesni = 0;
    2533         #endif /* WOLFSSL_AESNI */
    2534         #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
    2535             defined(WOLFSSL_AES_OFB)
    2536             aes->left = 0;
     2727    #ifdef WOLFSSL_IMX6_CAAM_BLOB
     2728        byte   local[32];
     2729        word32 localSz = 32;
     2730    #endif
     2731
     2732    #ifdef WOLFSSL_IMX6_CAAM_BLOB
     2733        if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
     2734            keylen == (24 + WC_CAAM_BLOB_SZ) ||
     2735            keylen == (32 + WC_CAAM_BLOB_SZ)) {
     2736            if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz) != 0) {
     2737                return BAD_FUNC_ARG;
     2738            }
     2739
     2740            /* set local values */
     2741            userKey = local;
     2742            keylen = localSz;
     2743        }
     2744    #endif
     2745
     2746    #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
     2747        (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
     2748        (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
     2749        #ifdef WOLF_CRYPTO_CB
     2750        if (aes->devId != INVALID_DEVID)
    25372751        #endif
     2752        {
     2753            if (keylen > sizeof(aes->devKey)) {
     2754                return BAD_FUNC_ARG;
     2755            }
     2756            XMEMCPY(aes->devKey, userKey, keylen);
     2757        }
     2758    #endif
     2759
     2760        if (checkKeyLen) {
     2761            if (keylen != 16 && keylen != 24 && keylen != 32) {
     2762                return BAD_FUNC_ARG;
     2763            }
     2764        #ifdef AES_MAX_KEY_SIZE
     2765            /* Check key length */
     2766            if (keylen > (AES_MAX_KEY_SIZE / 8)) {
     2767                return BAD_FUNC_ARG;
     2768            }
     2769        #endif
     2770        }
     2771
     2772    #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
     2773        defined(WOLFSSL_AES_OFB)
     2774        aes->left = 0;
     2775    #endif
    25382776
    25392777        aes->keylen = keylen;
    25402778        aes->rounds = (keylen/4) + 6;
    25412779
     2780    #ifdef WOLFSSL_AESNI
     2781        aes->use_aesni = 0;
     2782        if (checkAESNI == 0) {
     2783            haveAESNI  = Check_CPU_support_AES();
     2784            checkAESNI = 1;
     2785        }
     2786        if (haveAESNI) {
     2787            aes->use_aesni = 1;
     2788            if (iv)
     2789                XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
     2790            else
     2791                XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
     2792            if (dir == AES_ENCRYPTION)
     2793                return AES_set_encrypt_key(userKey, keylen * 8, aes);
     2794        #ifdef HAVE_AES_DECRYPT
     2795            else
     2796                return AES_set_decrypt_key(userKey, keylen * 8, aes);
     2797        #endif
     2798        }
     2799    #endif /* WOLFSSL_AESNI */
     2800
     2801        if (keylen > sizeof(aes->key)) {
     2802            return BAD_FUNC_ARG;
     2803        }
     2804        rk = aes->key;
    25422805        XMEMCPY(rk, userKey, keylen);
    25432806    #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
     
    25452808          defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES))
    25462809        ByteReverseWords(rk, rk, keylen);
     2810    #endif
     2811
     2812    #ifdef WOLFSSL_IMXRT_DCP
     2813        /* Implemented in wolfcrypt/src/port/nxp/dcp_port.c */
     2814        temp = 0;
     2815        if (keylen == 16)
     2816            temp = DCPAesSetKey(aes, userKey, keylen, iv, dir);
     2817        if (temp != 0)
     2818            return WC_HW_E;
    25472819    #endif
    25482820
     
    26592931            return BAD_FUNC_ARG;
    26602932        } /* switch */
     2933        ForceZero(&temp, sizeof(temp));
    26612934
    26622935    #if defined(HAVE_AES_DECRYPT)
     
    26722945                temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
    26732946            }
     2947            ForceZero(&temp, sizeof(temp));
    26742948        #if !defined(WOLFSSL_AES_SMALL_TABLES)
    26752949            /* apply the inverse MixColumn transform to all round keys but the
     
    27132987#endif
    27142988
    2715         return wc_AesSetIV(aes, iv);
    2716     }
    2717 
    2718     int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
    2719         const byte* iv, int dir)
    2720     {
    2721         int ret;
    2722     #if defined(AES_MAX_KEY_SIZE)
    2723         const word32 max_key_len = (AES_MAX_KEY_SIZE / 8);
    2724     #endif
    2725 
    2726     #ifdef WOLFSSL_IMX6_CAAM_BLOB
    2727         byte   local[32];
    2728         word32 localSz = 32;
    2729 
    2730         if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
    2731                 keylen == (24 + WC_CAAM_BLOB_SZ) ||
    2732                 keylen == (32 + WC_CAAM_BLOB_SZ)) {
    2733             if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz) != 0) {
    2734                 return BAD_FUNC_ARG;
    2735             }
    2736 
    2737             /* set local values */
    2738             userKey = local;
    2739             keylen = localSz;
    2740         }
    2741     #endif
    2742         if (aes == NULL ||
    2743                 !((keylen == 16) || (keylen == 24) || (keylen == 32))) {
    2744             return BAD_FUNC_ARG;
    2745         }
    2746 
    2747     #if defined(AES_MAX_KEY_SIZE)
    2748         /* Check key length */
    2749         if (keylen > max_key_len) {
    2750             return BAD_FUNC_ARG;
    2751         }
    2752     #endif
    2753         aes->keylen = keylen;
    2754         aes->rounds = keylen/4 + 6;
    2755 
    2756     #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
    2757         (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
    2758         (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
    2759         #ifdef WOLF_CRYPTO_CB
    2760         if (aes->devId != INVALID_DEVID)
    2761         #endif
    2762         {
    2763             XMEMCPY(aes->devKey, userKey, keylen);
    2764         }
    2765     #endif
    2766 
    2767     #ifdef WOLFSSL_AESNI
    2768         if (checkAESNI == 0) {
    2769             haveAESNI  = Check_CPU_support_AES();
    2770             checkAESNI = 1;
    2771         }
    2772         if (haveAESNI) {
    2773             #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB) || \
    2774                 defined(WOLFSSL_AES_OFB)
    2775                 aes->left = 0;
    2776             #endif /* WOLFSSL_AES_COUNTER */
    2777             aes->use_aesni = 1;
    2778             if (iv)
    2779                 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
    2780             else
    2781                 XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
    2782             if (dir == AES_ENCRYPTION)
    2783                 return AES_set_encrypt_key(userKey, keylen * 8, aes);
    2784         #ifdef HAVE_AES_DECRYPT
    2785             else
    2786                 return AES_set_decrypt_key(userKey, keylen * 8, aes);
    2787         #endif
    2788         }
    2789     #endif /* WOLFSSL_AESNI */
    2790 
    2791         ret = wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
     2989        ret = wc_AesSetIV(aes, iv);
    27922990
    27932991    #if defined(WOLFSSL_DEVCRYPTO) && \
     
    28012999    }
    28023000
     3001    int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
     3002        const byte* iv, int dir)
     3003    {
     3004        if (aes == NULL) {
     3005            return BAD_FUNC_ARG;
     3006        }
     3007        if (keylen > sizeof(aes->key)) {
     3008            return BAD_FUNC_ARG;
     3009        }
     3010
     3011        return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir, 1);
     3012    }
     3013
    28033014    #if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
    2804         /* AES-CTR and AES-DIRECT need to use this for key setup, no aesni yet */
     3015        /* AES-CTR and AES-DIRECT need to use this for key setup */
     3016        /* This function allows key sizes that are not 128/192/256 bits */
    28053017        int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
    28063018                            const byte* iv, int dir)
    28073019        {
    2808             int ret;
    2809 
    2810         #ifdef WOLFSSL_IMX6_CAAM_BLOB
    2811             byte   local[32];
    2812             word32 localSz = 32;
    2813 
    2814             if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
    2815              keylen == (24 + WC_CAAM_BLOB_SZ) ||
    2816              keylen == (32 + WC_CAAM_BLOB_SZ)) {
    2817                 if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz)
    2818                         != 0) {
    2819                     return BAD_FUNC_ARG;
    2820                 }
    2821 
    2822                 /* set local values */
    2823                 userKey = local;
    2824                 keylen = localSz;
     3020            if (aes == NULL) {
     3021                return BAD_FUNC_ARG;
    28253022            }
    2826         #endif
    2827             ret = wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
    2828 
    2829         #ifdef WOLFSSL_IMX6_CAAM_BLOB
    2830             ForceZero(local, sizeof(local));
    2831         #endif
    2832 
    2833             return ret;
     3023            if (keylen > sizeof(aes->key)) {
     3024                return BAD_FUNC_ARG;
     3025            }
     3026
     3027            return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir, 0);
    28343028        }
    28353029    #endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER */
     
    28473041    else
    28483042        XMEMSET(aes->reg,  0, AES_BLOCK_SIZE);
    2849 
    28503043    return 0;
    28513044}
     
    28563049        #error "Coldfire SEC doesn't yet support AES direct"
    28573050
    2858     #elif defined(FREESCALE_LTC)
    2859         /* Allow direct access to one block encrypt */
    2860         void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
    2861         {
    2862             byte *key;
    2863             uint32_t keySize;
    2864 
    2865             key = (byte*)aes->key;
    2866             wc_AesGetKeySize(aes, &keySize);
    2867 
    2868             LTC_AES_EncryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
    2869                 key, keySize);
    2870         }
    2871 
    2872         /* Allow direct access to one block decrypt */
    2873         void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
    2874         {
    2875             byte *key;
    2876             uint32_t keySize;
    2877 
    2878             key = (byte*)aes->key;
    2879             wc_AesGetKeySize(aes, &keySize);
    2880 
    2881             LTC_AES_DecryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
    2882                 key, keySize, kLTC_EncryptKey);
    2883         }
    2884 
    28853051    #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
    28863052        /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
     
    28923058        /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
    28933059
    2894     #elif defined(STM32_CRYPTO)
    2895         /* Allow direct access to one block encrypt */
    2896         void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
    2897         {
    2898             if (wolfSSL_CryptHwMutexLock() == 0) {
    2899                 wc_AesEncrypt(aes, in, out);
    2900                 wolfSSL_CryptHwMutexUnLock();
    2901             }
    2902         }
    2903         #ifdef HAVE_AES_DECRYPT
    2904         /* Allow direct access to one block decrypt */
    2905         void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
    2906         {
    2907             if (wolfSSL_CryptHwMutexLock() == 0) {
    2908                 wc_AesDecrypt(aes, in, out);
    2909                 wolfSSL_CryptHwMutexUnLock();
    2910             }
    2911         }
    2912         #endif /* HAVE_AES_DECRYPT */
    2913 
    2914     #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
    2915         !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
    2916        
    2917         /* Allow direct access to one block encrypt */
    2918         void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
    2919         {
    2920             wc_AesEncrypt(aes, in, out);
    2921         }
    2922         #ifdef HAVE_AES_DECRYPT
    2923         /* Allow direct access to one block decrypt */
    2924         void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
    2925         {
    2926             wc_AesDecrypt(aes, in, out);
    2927         }
    2928         #endif /* HAVE_AES_DECRYPT */
    29293060    #else
    29303061        /* Allow direct access to one block encrypt */
     
    29643095        }
    29653096
    2966     #ifdef STM32_CRYPTO_AES_ONLY
     3097    #if defined(STM32_HAL_V2)
     3098        hcryp.Init.Algorithm  = CRYP_AES_CBC;
     3099        ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
     3100    #elif defined(STM32_CRYPTO_AES_ONLY)
    29673101        hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
    29683102        hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_CBC;
    29693103        hcryp.Init.KeyWriteFlag  = CRYP_KEY_WRITE_ENABLE;
    2970     #elif defined(STM32_HAL_V2)
    2971         hcryp.Init.Algorithm  = CRYP_AES_CBC;
    2972         ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
    29733104    #endif
    29743105        hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
    29753106        HAL_CRYP_Init(&hcryp);
    29763107
    2977         while (blocks--) {
    2978         #ifdef STM32_CRYPTO_AES_ONLY
    2979             ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
    2980                 out, STM32_HAL_TIMEOUT);
    2981         #elif defined(STM32_HAL_V2)
    2982             ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
    2983                 (uint32_t*)out, STM32_HAL_TIMEOUT);
    2984         #else
    2985             ret = HAL_CRYP_AESCBC_Encrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
    2986                 out, STM32_HAL_TIMEOUT);
    2987         #endif
    2988             if (ret != HAL_OK) {
    2989                 ret = WC_TIMEOUT_E;
    2990                 break;
    2991             }
    2992 
    2993             /* store iv for next call */
    2994             XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
    2995 
    2996             sz  -= AES_BLOCK_SIZE;
    2997             in  += AES_BLOCK_SIZE;
    2998             out += AES_BLOCK_SIZE;
    2999         }
     3108    #if defined(STM32_HAL_V2)
     3109        ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, blocks * AES_BLOCK_SIZE,
     3110            (uint32_t*)out, STM32_HAL_TIMEOUT);
     3111    #elif defined(STM32_CRYPTO_AES_ONLY)
     3112        ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, blocks * AES_BLOCK_SIZE,
     3113            out, STM32_HAL_TIMEOUT);
     3114    #else
     3115        ret = HAL_CRYP_AESCBC_Encrypt(&hcryp, (uint8_t*)in, blocks * AES_BLOCK_SIZE,
     3116            out, STM32_HAL_TIMEOUT);
     3117    #endif
     3118        if (ret != HAL_OK) {
     3119            ret = WC_TIMEOUT_E;
     3120        }
     3121
     3122        /* store iv for next call */
     3123        XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
    30003124
    30013125        HAL_CRYP_DeInit(&hcryp);
     
    30243148        XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
    30253149
    3026     #ifdef STM32_CRYPTO_AES_ONLY
     3150    #if defined(STM32_HAL_V2)
     3151        hcryp.Init.Algorithm  = CRYP_AES_CBC;
     3152        ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
     3153    #elif defined(STM32_CRYPTO_AES_ONLY)
    30273154        hcryp.Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
    30283155        hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_CBC;
    30293156        hcryp.Init.KeyWriteFlag  = CRYP_KEY_WRITE_ENABLE;
    3030     #elif defined(STM32_HAL_V2)
    3031         hcryp.Init.Algorithm  = CRYP_AES_CBC;
    3032         ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
    30333157    #endif
    30343158
     
    30363160        HAL_CRYP_Init(&hcryp);
    30373161
    3038         while (blocks--) {
    3039         #ifdef STM32_CRYPTO_AES_ONLY
    3040             ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
    3041                 out, STM32_HAL_TIMEOUT);
    3042         #elif defined(STM32_HAL_V2)
    3043             ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
    3044                 (uint32_t*)out, STM32_HAL_TIMEOUT);
    3045         #else
    3046             ret = HAL_CRYP_AESCBC_Decrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
    3047                 out, STM32_HAL_TIMEOUT);
    3048         #endif
    3049             if (ret != HAL_OK) {
    3050                 ret = WC_TIMEOUT_E;
    3051                 break;
    3052             }
    3053 
    3054             /* store iv for next call */
    3055             XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
    3056 
    3057             in  += AES_BLOCK_SIZE;
    3058             out += AES_BLOCK_SIZE;
    3059         }
     3162    #if defined(STM32_HAL_V2)
     3163        ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in, blocks * AES_BLOCK_SIZE,
     3164            (uint32_t*)out, STM32_HAL_TIMEOUT);
     3165    #elif defined(STM32_CRYPTO_AES_ONLY)
     3166        ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, blocks * AES_BLOCK_SIZE,
     3167            out, STM32_HAL_TIMEOUT);
     3168    #else
     3169        ret = HAL_CRYP_AESCBC_Decrypt(&hcryp, (uint8_t*)in, blocks * AES_BLOCK_SIZE,
     3170            out, STM32_HAL_TIMEOUT);
     3171    #endif
     3172        if (ret != HAL_OK) {
     3173            ret = WC_TIMEOUT_E;
     3174        }
     3175
     3176        /* store iv for next call */
     3177        XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
    30603178
    30613179        HAL_CRYP_DeInit(&hcryp);
     
    30663184    #endif /* HAVE_AES_DECRYPT */
    30673185
    3068 #else /* STD_PERI_LIB */
     3186#else /* Standard Peripheral Library */
    30693187    int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
    30703188    {
     
    33383456    int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
    33393457    {
    3340         uint32_t keySize;
     3458        word32 keySize;
    33413459        status_t status;
    33423460        byte *iv, *enc_key;
     
    33513469        }
    33523470
     3471        status = wolfSSL_CryptHwMutexLock();
     3472        if (status != 0)
     3473            return status;
    33533474        status = LTC_AES_EncryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
    33543475            iv, enc_key, keySize);
     3476        wolfSSL_CryptHwMutexUnLock();
    33553477
    33563478        /* store iv for next call */
     
    33653487    int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
    33663488    {
    3367         uint32_t keySize;
     3489        word32 keySize;
    33683490        status_t status;
    33693491        byte* iv, *dec_key;
     
    33823504        XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
    33833505
     3506        status = wolfSSL_CryptHwMutexLock();
     3507        if (status != 0)
     3508            return status;
    33843509        status = LTC_AES_DecryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
    33853510            iv, dec_key, keySize, kLTC_EncryptKey);
     3511        wolfSSL_CryptHwMutexUnLock();
    33863512
    33873513        /* store IV for next call */
     
    34033529        byte temp_block[AES_BLOCK_SIZE];
    34043530
    3405         iv      = (byte*)aes->reg;
     3531        iv = (byte*)aes->reg;
    34063532
    34073533        while (blocks--) {
     
    34313557        byte temp_block[AES_BLOCK_SIZE];
    34323558
    3433         iv      = (byte*)aes->reg;
     3559        iv = (byte*)aes->reg;
    34343560
    34353561        while (blocks--) {
     
    35143640    int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
    35153641    {
    3516         return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out);
     3642        return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t*)in, sz, out);
    35173643    }
    35183644    int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
    35193645    {
    3520         return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out);
     3646        return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t*)in, sz, out);
    35213647    }
    35223648#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
     
    35283654#elif defined(WOLFSSL_DEVCRYPTO_CBC)
    35293655    /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
     3656
     3657#elif defined(WOLFSSL_SILABS_SE_ACCEL)
     3658    /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
    35303659
    35313660#else
     
    35393668            return BAD_FUNC_ARG;
    35403669        }
     3670
     3671        if (sz == 0) {
     3672            return 0;
     3673        }
     3674    #ifdef WOLFSSL_IMXRT_DCP
     3675        /* Implemented in wolfcrypt/src/port/nxp/dcp_port.c */
     3676        if (aes->keylen == 16)
     3677            return DCPAesCbcEncrypt(aes, out, in, sz);
     3678    #endif
    35413679
    35423680    #ifdef WOLF_CRYPTO_CB
     
    35933731                tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
    35943732                XMEMCPY(tmp_align, in, sz);
     3733                SAVE_VECTOR_REGISTERS();
    35953734                AES_CBC_encrypt(tmp_align, tmp_align, (byte*)aes->reg, sz,
    35963735                                                  (byte*)aes->key, aes->rounds);
     3736                RESTORE_VECTOR_REGISTERS();
    35973737                /* store iv for next call */
    35983738                XMEMCPY(aes->reg, tmp_align + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
     
    36073747            }
    36083748
     3749            SAVE_VECTOR_REGISTERS();
    36093750            AES_CBC_encrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
    36103751                            aes->rounds);
     3752            RESTORE_VECTOR_REGISTERS();
    36113753            /* store iv for next call */
    36123754            XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
     
    36383780            return BAD_FUNC_ARG;
    36393781        }
     3782
     3783        if (sz == 0) {
     3784            return 0;
     3785        }
     3786    #ifdef WOLFSSL_IMXRT_DCP
     3787        /* Implemented in wolfcrypt/src/port/nxp/dcp_port.c */
     3788        if (aes->keylen == 16)
     3789            return DCPAesCbcDecrypt(aes, out, in, sz);
     3790    #endif
    36403791
    36413792    #ifdef WOLF_CRYPTO_CB
     
    36843835            /* if input and output same will overwrite input iv */
    36853836            XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
     3837            SAVE_VECTOR_REGISTERS();
    36863838            #if defined(WOLFSSL_AESNI_BY4)
    36873839            AES_CBC_decrypt_by4(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
     
    36953847            #endif /* WOLFSSL_AESNI_BYx */
    36963848            /* store iv for next call */
     3849            RESTORE_VECTOR_REGISTERS();
    36973850            XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
    36983851            return 0;
     
    37413894        #endif
    37423895
     3896        #ifdef WOLFSSL_STM32_CUBEMX
     3897            ret = wc_Stm32_Aes_Init(aes, &hcryp);
     3898            if (ret != 0) {
     3899                return ret;
     3900            }
     3901
    37433902            ret = wolfSSL_CryptHwMutexLock();
    37443903            if (ret != 0) {
     
    37463905            }
    37473906
    3748         #ifdef WOLFSSL_STM32_CUBEMX
    3749             ret = wc_Stm32_Aes_Init(aes, &hcryp);
    3750             if (ret != 0) {
    3751                 wolfSSL_CryptHwMutexUnLock();
    3752                 return ret;
    3753             }
    3754 
    3755         #ifdef STM32_CRYPTO_AES_ONLY
     3907        #if defined(STM32_HAL_V2)
     3908            hcryp.Init.Algorithm  = CRYP_AES_CTR;
     3909            ByteReverseWords(iv, aes->reg, AES_BLOCK_SIZE);
     3910            hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)iv;
     3911        #elif defined(STM32_CRYPTO_AES_ONLY)
    37563912            hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
    37573913            hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_CTR;
    37583914            hcryp.Init.KeyWriteFlag  = CRYP_KEY_WRITE_ENABLE;
    37593915            hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
    3760         #elif defined(STM32_HAL_V2)
    3761             hcryp.Init.Algorithm  = CRYP_AES_CTR;
    3762             ByteReverseWords(iv, aes->reg, AES_BLOCK_SIZE);
    3763             hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)iv;
    37643916        #else
    37653917            hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
     
    37673919            HAL_CRYP_Init(&hcryp);
    37683920
    3769         #ifdef STM32_CRYPTO_AES_ONLY
     3921        #if defined(STM32_HAL_V2)
     3922            ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
     3923                (uint32_t*)out, STM32_HAL_TIMEOUT);
     3924        #elif defined(STM32_CRYPTO_AES_ONLY)
    37703925            ret = HAL_CRYPEx_AES(&hcryp, (byte*)in, AES_BLOCK_SIZE,
    37713926                out, STM32_HAL_TIMEOUT);
    3772         #elif defined(STM32_HAL_V2)
    3773             ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
    3774                 (uint32_t*)out, STM32_HAL_TIMEOUT);
    37753927        #else
    37763928            ret = HAL_CRYP_AESCTR_Encrypt(&hcryp, (byte*)in, AES_BLOCK_SIZE,
     
    37823934            HAL_CRYP_DeInit(&hcryp);
    37833935
    3784         #else /* STD_PERI_LIB */
     3936        #else /* Standard Peripheral Library */
    37853937            ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
    37863938            if (ret != 0) {
    3787                 wolfSSL_CryptHwMutexUnLock();
     3939                return ret;
     3940            }
     3941
     3942            ret = wolfSSL_CryptHwMutexLock();
     3943            if (ret != 0) {
    37883944                return ret;
    37893945            }
     
    38303986            /* disable crypto processor */
    38313987            CRYP_Cmd(DISABLE);
    3832 
    38333988        #endif /* WOLFSSL_STM32_CUBEMX */
    38343989
     
    38594014        int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
    38604015        {
    3861             uint32_t keySize;
     4016            int ret = 0;
     4017            word32 keySize;
    38624018            byte *iv, *enc_key;
    38634019            byte* tmp;
     
    38814037                wc_AesGetKeySize(aes, &keySize);
    38824038
     4039                ret = wolfSSL_CryptHwMutexLock();
     4040                if (ret != 0)
     4041                    return ret;
    38834042                LTC_AES_CryptCtr(LTC_BASE, in, out, sz,
    38844043                    iv, enc_key, keySize, (byte*)aes->tmp,
    38854044                    (uint32_t*)&aes->left);
     4045                wolfSSL_CryptHwMutexUnLock();
    38864046            }
    38874047
    3888             return 0;
     4048            return ret;
    38894049        }
    38904050
     
    38974057    #elif defined(WOLFSSL_DEVCRYPTO_AES)
    38984058        /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
    3899    
     4059
    39004060    #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
    39014061        !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
     
    40104170    #error "Coldfire SEC doesn't currently support AES-GCM mode"
    40114171
    4012 #elif defined(WOLFSSL_NRF51_AES)
    4013     #error "nRF51 doesn't currently support AES-GCM mode"
    4014 
    40154172#endif
    40164173
     
    40374194    }
    40384195}
    4039 #ifdef STM32_CRYPTO_AES_GCM
    4040 static WC_INLINE void DecrementGcmCounter(byte* inOutCtr)
    4041 {
    4042     int i;
    4043 
    4044     /* in network byte order so start at end and work back */
    4045     for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
    4046         if (--inOutCtr[i] != 0xFF)  /* we're done unless we underflow */
    4047             return;
    4048     }
    4049 }
    4050 #endif /* STM32_CRYPTO_AES_GCM */
    40514196#endif /* !FREESCALE_LTC_AES_GCM */
    40524197
    4053 #if defined(GCM_SMALL) || defined(GCM_TABLE)
     4198#if defined(GCM_SMALL) || defined(GCM_TABLE) || defined(GCM_TABLE_4BIT)
    40544199
    40554200static WC_INLINE void FlattenSzInBits(byte* buf, word32 sz)
     
    40864231}
    40874232
    4088 #endif /* defined(GCM_SMALL) || defined(GCM_TABLE) */
     4233#endif /* defined(GCM_SMALL) || defined(GCM_TABLE) || defined(GCM_TABLE_4BIT) */
    40894234
    40904235
     
    41114256
    41124257    XMEMSET(m[0], 0, AES_BLOCK_SIZE);
     4258}
     4259
     4260#elif defined(GCM_TABLE_4BIT)
     4261
     4262static WC_INLINE void Shift4_M0(byte *r8, byte* z8)
     4263{
     4264    int i;
     4265    for (i = 15; i > 0; i--)
     4266        r8[i] = (z8[i-1] << 4) | (z8[i] >> 4);
     4267    r8[0] = z8[0] >> 4;
     4268}
     4269
     4270static void GenerateM0(Aes* aes)
     4271{
     4272#if !defined(BIG_ENDIAN_ORDER) && !defined(WC_16BIT_CPU)
     4273    int i;
     4274#endif
     4275    byte (*m)[AES_BLOCK_SIZE] = aes->M0;
     4276
     4277    /* 0 times -> 0x0 */
     4278    XMEMSET(m[0x0], 0, AES_BLOCK_SIZE);
     4279    /* 1 times -> 0x8 */
     4280    XMEMCPY(m[0x8], aes->H, AES_BLOCK_SIZE);
     4281    /* 2 times -> 0x4 */
     4282    XMEMCPY(m[0x4], m[0x8], AES_BLOCK_SIZE);
     4283    RIGHTSHIFTX(m[0x4]);
     4284    /* 4 times -> 0x2 */
     4285    XMEMCPY(m[0x2], m[0x4], AES_BLOCK_SIZE);
     4286    RIGHTSHIFTX(m[0x2]);
     4287    /* 8 times -> 0x1 */
     4288    XMEMCPY(m[0x1], m[0x2], AES_BLOCK_SIZE);
     4289    RIGHTSHIFTX(m[0x1]);
     4290
     4291    /* 0x3 */
     4292    XMEMCPY(m[0x3], m[0x2], AES_BLOCK_SIZE);
     4293    xorbuf (m[0x3], m[0x1], AES_BLOCK_SIZE);
     4294
     4295    /* 0x5 -> 0x7 */
     4296    XMEMCPY(m[0x5], m[0x4], AES_BLOCK_SIZE);
     4297    xorbuf (m[0x5], m[0x1], AES_BLOCK_SIZE);
     4298    XMEMCPY(m[0x6], m[0x4], AES_BLOCK_SIZE);
     4299    xorbuf (m[0x6], m[0x2], AES_BLOCK_SIZE);
     4300    XMEMCPY(m[0x7], m[0x4], AES_BLOCK_SIZE);
     4301    xorbuf (m[0x7], m[0x3], AES_BLOCK_SIZE);
     4302
     4303    /* 0x9 -> 0xf */
     4304    XMEMCPY(m[0x9], m[0x8], AES_BLOCK_SIZE);
     4305    xorbuf (m[0x9], m[0x1], AES_BLOCK_SIZE);
     4306    XMEMCPY(m[0xa], m[0x8], AES_BLOCK_SIZE);
     4307    xorbuf (m[0xa], m[0x2], AES_BLOCK_SIZE);
     4308    XMEMCPY(m[0xb], m[0x8], AES_BLOCK_SIZE);
     4309    xorbuf (m[0xb], m[0x3], AES_BLOCK_SIZE);
     4310    XMEMCPY(m[0xc], m[0x8], AES_BLOCK_SIZE);
     4311    xorbuf (m[0xc], m[0x4], AES_BLOCK_SIZE);
     4312    XMEMCPY(m[0xd], m[0x8], AES_BLOCK_SIZE);
     4313    xorbuf (m[0xd], m[0x5], AES_BLOCK_SIZE);
     4314    XMEMCPY(m[0xe], m[0x8], AES_BLOCK_SIZE);
     4315    xorbuf (m[0xe], m[0x6], AES_BLOCK_SIZE);
     4316    XMEMCPY(m[0xf], m[0x8], AES_BLOCK_SIZE);
     4317    xorbuf (m[0xf], m[0x7], AES_BLOCK_SIZE);
     4318
     4319#if !defined(BIG_ENDIAN_ORDER) && !defined(WC_16BIT_CPU)
     4320    for (i = 0; i < 16; i++) {
     4321        Shift4_M0(m[16+i], m[i]);
     4322    }
     4323#endif
    41134324}
    41144325
     
    41594370    if (ret == 0) {
    41604371        wc_AesEncrypt(aes, iv, aes->H);
    4161     #ifdef GCM_TABLE
     4372    #if defined(GCM_TABLE) || defined(GCM_TABLE_4BIT)
    41624373        GenerateM0(aes);
    41634374    #endif /* GCM_TABLE */
     
    41804391    ForceZero(local, sizeof(local));
    41814392#endif
    4182 
    41834393    return ret;
    41844394}
     
    41964406void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
    41974407                     const unsigned char* addt, const unsigned char* ivec,
    4198                      unsigned char *tag, unsigned int nbytes,
    4199                      unsigned int abytes, unsigned int ibytes,
    4200                      unsigned int tbytes, const unsigned char* key, int nr)
     4408                     unsigned char *tag, word32 nbytes,
     4409                     word32 abytes, word32 ibytes,
     4410                     word32 tbytes, const unsigned char* key, int nr)
    42014411                     XASM_LINK("AES_GCM_encrypt");
    42024412#ifdef HAVE_INTEL_AVX1
    42034413void AES_GCM_encrypt_avx1(const unsigned char *in, unsigned char *out,
    42044414                          const unsigned char* addt, const unsigned char* ivec,
    4205                           unsigned char *tag, unsigned int nbytes,
    4206                           unsigned int abytes, unsigned int ibytes,
    4207                           unsigned int tbytes, const unsigned char* key,
     4415                          unsigned char *tag, word32 nbytes,
     4416                          word32 abytes, word32 ibytes,
     4417                          word32 tbytes, const unsigned char* key,
    42084418                          int nr)
    42094419                          XASM_LINK("AES_GCM_encrypt_avx1");
     
    42114421void AES_GCM_encrypt_avx2(const unsigned char *in, unsigned char *out,
    42124422                          const unsigned char* addt, const unsigned char* ivec,
    4213                           unsigned char *tag, unsigned int nbytes,
    4214                           unsigned int abytes, unsigned int ibytes,
    4215                           unsigned int tbytes, const unsigned char* key,
     4423                          unsigned char *tag, word32 nbytes,
     4424                          word32 abytes, word32 ibytes,
     4425                          word32 tbytes, const unsigned char* key,
    42164426                          int nr)
    42174427                          XASM_LINK("AES_GCM_encrypt_avx2");
     
    42224432void AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
    42234433                     const unsigned char* addt, const unsigned char* ivec,
    4224                      const unsigned char *tag, int nbytes, int abytes,
    4225                      int ibytes, int tbytes, const unsigned char* key, int nr,
    4226                      int* res)
     4434                     const unsigned char *tag, word32 nbytes, word32 abytes,
     4435                     word32 ibytes, word32 tbytes, const unsigned char* key,
     4436                     int nr, int* res)
    42274437                     XASM_LINK("AES_GCM_decrypt");
    42284438#ifdef HAVE_INTEL_AVX1
    42294439void AES_GCM_decrypt_avx1(const unsigned char *in, unsigned char *out,
    42304440                          const unsigned char* addt, const unsigned char* ivec,
    4231                           const unsigned char *tag, int nbytes, int abytes,
    4232                           int ibytes, int tbytes, const unsigned char* key,
    4233                           int nr, int* res)
     4441                          const unsigned char *tag, word32 nbytes,
     4442                          word32 abytes, word32 ibytes, word32 tbytes,
     4443                          const unsigned char* key, int nr, int* res)
    42344444                          XASM_LINK("AES_GCM_decrypt_avx1");
    42354445#ifdef HAVE_INTEL_AVX2
    42364446void AES_GCM_decrypt_avx2(const unsigned char *in, unsigned char *out,
    42374447                          const unsigned char* addt, const unsigned char* ivec,
    4238                           const unsigned char *tag, int nbytes, int abytes,
    4239                           int ibytes, int tbytes, const unsigned char* key,
    4240                           int nr, int* res)
     4448                          const unsigned char *tag, word32 nbytes,
     4449                          word32 abytes, word32 ibytes, word32 tbytes,
     4450                          const unsigned char* key, int nr, int* res)
    42414451                          XASM_LINK("AES_GCM_decrypt_avx2");
    42424452#endif /* HAVE_INTEL_AVX2 */
     
    45854795
    45864796
    4587 static void AES_GCM_encrypt(const unsigned char *in,
    4588                               unsigned char *out,
    4589                               const unsigned char* addt,
    4590                               const unsigned char* ivec,
    4591                               unsigned char *tag, unsigned int nbytes,
    4592                               unsigned int abytes, unsigned int ibytes,
    4593                               unsigned int tbytes,
    4594                               const unsigned char* key, int nr)
     4797static void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
     4798                            const unsigned char* addt,
     4799                            const unsigned char* ivec, unsigned char *tag,
     4800                            word32 nbytes, word32 abytes, word32 ibytes,
     4801                            wrd32 tbytes, const unsigned char* key, int nr)
    45954802{
    45964803    int i, j ,k;
     
    50135220        X = gfmul_shifted(X, H);
    50145221    }
    5015     tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
    5016     tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
     5222    tmp1 = _mm_insert_epi64(tmp1, ((word64)nbytes)*8, 0);
     5223    tmp1 = _mm_insert_epi64(tmp1, ((word64)abytes)*8, 1);
    50175224    X = _mm_xor_si128(X, tmp1);
    50185225    X = gfmul_shifted(X, H);
     
    50215228    /*_mm_storeu_si128((__m128i*)tag, T);*/
    50225229    XMEMCPY(tag, &T, tbytes);
     5230    ForceZero(&lastKey, sizeof(lastKey));
    50235231}
    50245232
    50255233#ifdef HAVE_AES_DECRYPT
    50265234
    5027 static void AES_GCM_decrypt(const unsigned char *in,
    5028                            unsigned char *out,
    5029                            const unsigned char* addt,
    5030                            const unsigned char* ivec,
    5031                            const unsigned char *tag, int nbytes, int abytes,
    5032                            int ibytes, word32 tbytes, const unsigned char* key,
    5033                            int nr, int* res)
     5235static void AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
     5236                            const unsigned char* addt,
     5237                            const unsigned char* ivec, const unsigned char *tag,
     5238                            word32 nbytes, word32 abytes, word32 ibytes,
     5239                            word32 tbytes, const unsigned char* key, int nr,
     5240                            int* res)
    50345241{
    50355242    int i, j ,k;
     
    53375544    }
    53385545
    5339     tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
    5340     tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
     5546    tmp1 = _mm_insert_epi64(tmp1, ((word64)nbytes)*8, 0);
     5547    tmp1 = _mm_insert_epi64(tmp1, ((word64)abytes)*8, 1);
    53415548    /* 128 x 128 Carryless Multiply */
    53425549    X = _mm_xor_si128(X, tmp1);
     
    53515558    else
    53525559        *res = 1; /* when successful returns 1 */
     5560    ForceZero(&lastKey, sizeof(lastKey));
    53535561}
    53545562
     
    55105718static void GMULT(byte *x, byte m[256][AES_BLOCK_SIZE])
    55115719{
     5720#if !defined(WORD64_AVAILABLE) || defined(BIG_ENDIAN_ORDER)
    55125721    int i, j;
    55135722    byte Z[AES_BLOCK_SIZE];
     
    55245733        }
    55255734
    5526         Z[0] = R[a][0];
     5735        Z[0]  = R[a][0];
    55275736        Z[1] ^= R[a][1];
    55285737    }
     
    55305739
    55315740    XMEMCPY(x, Z, AES_BLOCK_SIZE);
    5532 }
    5533 
     5741#else
     5742    byte Z[AES_BLOCK_SIZE + AES_BLOCK_SIZE];
     5743    byte a;
     5744    word64* pZ;
     5745    word64* pm;
     5746    word64* px = (word64*)(x);
     5747    int i;
     5748
     5749    pZ = (word64*)(Z + 15 + 1);
     5750    pm = (word64*)(m[x[15]]);
     5751    pZ[0] = pm[0];
     5752    pZ[1] = pm[1];
     5753    a = Z[16 + 15];
     5754    Z[15]  = R[a][0];
     5755    Z[16] ^= R[a][1];
     5756    for (i = 14; i > 0; i--) {
     5757        pZ = (word64*)(Z + i + 1);
     5758        pm = (word64*)(m[x[i]]);
     5759        pZ[0] ^= pm[0];
     5760        pZ[1] ^= pm[1];
     5761        a = Z[16 + i];
     5762        Z[i]    = R[a][0];
     5763        Z[i+1] ^= R[a][1];
     5764    }
     5765    pZ = (word64*)(Z + 1);
     5766    pm = (word64*)(m[x[0]]);
     5767    px[0] = pZ[0] ^ pm[0]; px[1] = pZ[1] ^ pm[1];
     5768#endif
     5769}
    55345770
    55355771void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
     
    55875823
    55885824/* end GCM_TABLE */
     5825#elif defined(GCM_TABLE_4BIT)
     5826
     5827/* remainder = x^7 + x^2 + x^1 + 1 => 0xe1
     5828 *  R shifts right a reverse bit pair of bytes such that:
     5829 *     R(b0, b1) => b1 = (b1 >> 1) | (b0 << 7); b0 >>= 1
     5830 *  0 => 0, 0, 0, 0 => R(R(R(00,00) ^ 00,00) ^ 00,00) ^ 00,00 = 00,00
     5831 *  8 => 0, 0, 0, 1 => R(R(R(00,00) ^ 00,00) ^ 00,00) ^ e1,00 = e1,00
     5832 *  4 => 0, 0, 1, 0 => R(R(R(00,00) ^ 00,00) ^ e1,00) ^ 00,00 = 70,80
     5833 *  2 => 0, 1, 0, 0 => R(R(R(00,00) ^ e1,00) ^ 00,00) ^ 00,00 = 38,40
     5834 *  1 => 1, 0, 0, 0 => R(R(R(e1,00) ^ 00,00) ^ 00,00) ^ 00,00 = 1c,20
     5835 *  To calculate te rest, XOR result for each bit.
     5836 *   e.g. 6 = 4 ^ 2 => 48,c0
     5837 *
     5838 * Second half is same values rotated by 4-bits.
     5839 */
     5840#if defined(BIG_ENDIAN_ORDER) || defined(WC_16BIT_CPU)
     5841static const byte R[16][2] = {
     5842    {0x00, 0x00}, {0x1c, 0x20}, {0x38, 0x40}, {0x24, 0x60},
     5843    {0x70, 0x80}, {0x6c, 0xa0}, {0x48, 0xc0}, {0x54, 0xe0},
     5844    {0xe1, 0x00}, {0xfd, 0x20}, {0xd9, 0x40}, {0xc5, 0x60},
     5845    {0x91, 0x80}, {0x8d, 0xa0}, {0xa9, 0xc0}, {0xb5, 0xe0},
     5846};
     5847#else
     5848static const word16 R[32] = {
     5849          0x0000,       0x201c,       0x4038,       0x6024,
     5850          0x8070,       0xa06c,       0xc048,       0xe054,
     5851          0x00e1,       0x20fd,       0x40d9,       0x60c5,
     5852          0x8091,       0xa08d,       0xc0a9,       0xe0b5,
     5853
     5854          0x0000,       0xc201,       0x8403,       0x4602,
     5855          0x0807,       0xca06,       0x8c04,       0x4e05,
     5856          0x100e,       0xd20f,       0x940d,       0x560c,
     5857          0x1809,       0xda08,       0x9c0a,       0x5e0b,
     5858};
     5859#endif
     5860
     5861/* Multiply in GF(2^128) defined by polynomial:
     5862 *   x^128 + x^7 + x^2 + x^1 + 1.
     5863 *
     5864 * H: hash key = encrypt(key, 0)
     5865 * x = x * H in field
     5866 *
     5867 * x: cumlative result
     5868 * m: 4-bit table
     5869 *    [0..15] * H
     5870 */
     5871#if defined(BIG_ENDIAN_ORDER) || defined(WC_16BIT_CPU)
     5872static void GMULT(byte *x, byte m[16][AES_BLOCK_SIZE])
     5873{
     5874    int i, j, n;
     5875    byte Z[AES_BLOCK_SIZE];
     5876    byte a;
     5877
     5878    XMEMSET(Z, 0, sizeof(Z));
     5879
     5880    for (i = 15; i >= 0; i--) {
     5881        for (n = 0; n < 2; n++) {
     5882            if (n == 0)
     5883                xorbuf(Z, m[x[i] & 0xf], AES_BLOCK_SIZE);
     5884            else {
     5885                xorbuf(Z, m[x[i] >> 4], AES_BLOCK_SIZE);
     5886                if (i == 0)
     5887                    break;
     5888            }
     5889            a = Z[15] & 0xf;
     5890
     5891            for (j = 15; j > 0; j--)
     5892                Z[j] = (Z[j-1] << 4) | (Z[j] >> 4);
     5893            Z[0] >>= 4;
     5894
     5895            Z[0] ^= R[a][0];
     5896            Z[1] ^= R[a][1];
     5897        }
     5898    }
     5899
     5900    XMEMCPY(x, Z, AES_BLOCK_SIZE);
     5901}
     5902#elif defined(WC_32BIT_CPU)
     5903static WC_INLINE void GMULT(byte *x, byte m[32][AES_BLOCK_SIZE])
     5904{
     5905    int i;
     5906    word32 z8[4] = {0, 0, 0, 0};
     5907    byte a;
     5908    word32* x8 = (word32*)x;
     5909    word32* m8;
     5910    byte xi;
     5911    word32 n7, n6, n5, n4, n3, n2, n1, n0;
     5912
     5913    for (i = 15; i > 0; i--) {
     5914        xi = x[i];
     5915
     5916        /* XOR in (msn * H) */
     5917        m8 = (word32*)m[xi & 0xf];
     5918        z8[0] ^= m8[0]; z8[1] ^= m8[1]; z8[2] ^= m8[2]; z8[3] ^= m8[3];
     5919
     5920        /* Cache top byte for remainder calculations - lost in rotate. */
     5921        a = z8[3] >> 24;
     5922
     5923        /* Rotate Z by 8-bits */
     5924        z8[3] = (z8[2] >> 24) | (z8[3] << 8);
     5925        z8[2] = (z8[1] >> 24) | (z8[2] << 8);
     5926        z8[1] = (z8[0] >> 24) | (z8[1] << 8);
     5927        z8[0] <<= 8;
     5928
     5929        /* XOR in (msn * remainder) [pre-rotated by 4 bits] */
     5930        z8[0] ^= (word32)R[16 + (a & 0xf)];
     5931
     5932        xi >>= 4;
     5933        /* XOR in next significant nibble (XORed with H) * remainder */
     5934        m8 = (word32*)m[xi];
     5935        a ^= (byte)(m8[3] >> 20);
     5936        z8[0] ^= (word32)R[a >> 4];
     5937
     5938        /* XOR in (next significant nibble * H) [pre-rotated by 4 bits] */
     5939        m8 = (word32*)m[16 + xi];
     5940        z8[0] ^= m8[0]; z8[1] ^= m8[1];
     5941        z8[2] ^= m8[2]; z8[3] ^= m8[3];
     5942    }
     5943
     5944    xi = x[0];
     5945
     5946    /* XOR in most significant nibble * H */
     5947    m8 = (word32*)m[xi & 0xf];
     5948    z8[0] ^= m8[0]; z8[1] ^= m8[1]; z8[2] ^= m8[2]; z8[3] ^= m8[3];
     5949
     5950    /* Cache top byte for remainder calculations - lost in rotate. */
     5951    a = (z8[3] >> 24) & 0xf;
     5952
     5953    /* Rotate z by 4-bits */
     5954    n7 = z8[3] & 0xf0f0f0f0ULL;
     5955    n6 = z8[3] & 0x0f0f0f0fULL;
     5956    n5 = z8[2] & 0xf0f0f0f0ULL;
     5957    n4 = z8[2] & 0x0f0f0f0fULL;
     5958    n3 = z8[1] & 0xf0f0f0f0ULL;
     5959    n2 = z8[1] & 0x0f0f0f0fULL;
     5960    n1 = z8[0] & 0xf0f0f0f0ULL;
     5961    n0 = z8[0] & 0x0f0f0f0fULL;
     5962    z8[3] = (n7 >> 4) | (n6 << 12) | (n4 >> 20);
     5963    z8[2] = (n5 >> 4) | (n4 << 12) | (n2 >> 20);
     5964    z8[1] = (n3 >> 4) | (n2 << 12) | (n0 >> 20);
     5965    z8[0] = (n1 >> 4) | (n0 << 12);
     5966
     5967    /* XOR in most significant nibble * remainder */
     5968    z8[0] ^= (word32)R[a];
     5969    /* XOR in next significant nibble * H */
     5970    m8 = (word32*)m[xi >> 4];
     5971    z8[0] ^= m8[0]; z8[1] ^= m8[1]; z8[2] ^= m8[2]; z8[3] ^= m8[3];
     5972
     5973    /* Write back result. */
     5974    x8[0] = z8[0]; x8[1] = z8[1]; x8[2] = z8[2]; x8[3] = z8[3];
     5975}
     5976#else
     5977static WC_INLINE void GMULT(byte *x, byte m[32][AES_BLOCK_SIZE])
     5978{
     5979    int i;
     5980    word64 z8[2] = {0, 0};
     5981    byte a;
     5982    word64* x8 = (word64*)x;
     5983    word64* m8;
     5984    word64 n0, n1, n2, n3;
     5985    byte xi;
     5986
     5987    for (i = 15; i > 0; i--) {
     5988        xi = x[i];
     5989
     5990        /* XOR in (msn * H) */
     5991        m8 = (word64*)m[xi & 0xf];
     5992        z8[0] ^= m8[0];
     5993        z8[1] ^= m8[1];
     5994
     5995        /* Cache top byte for remainder calculations - lost in rotate. */
     5996        a = z8[1] >> 56;
     5997
     5998        /* Rotate Z by 8-bits */
     5999        z8[1] = (z8[0] >> 56) | (z8[1] << 8);
     6000        z8[0] <<= 8;
     6001
     6002        /* XOR in (next significant nibble * H) [pre-rotated by 4 bits] */
     6003        m8 = (word64*)m[16 + (xi >> 4)];
     6004        z8[0] ^= m8[0];
     6005        z8[1] ^= m8[1];
     6006
     6007        /* XOR in (msn * remainder) [pre-rotated by 4 bits] */
     6008        z8[0] ^= (word64)R[16 + (a & 0xf)];
     6009        /* XOR in next significant nibble (XORed with H) * remainder */
     6010        m8 = (word64*)m[xi >> 4];
     6011        a ^= (byte)(m8[1] >> 52);
     6012        z8[0] ^= (word64)R[a >> 4];
     6013    }
     6014
     6015    xi = x[0];
     6016
     6017    /* XOR in most significant nibble * H */
     6018    m8 = (word64*)m[xi & 0xf];
     6019    z8[0] ^= m8[0];
     6020    z8[1] ^= m8[1];
     6021
     6022    /* Cache top byte for remainder calculations - lost in rotate. */
     6023    a = (z8[1] >> 56) & 0xf;
     6024
     6025    /* Rotate z by 4-bits */
     6026    n3 = z8[1] & 0xf0f0f0f0f0f0f0f0ULL;
     6027    n2 = z8[1] & 0x0f0f0f0f0f0f0f0fULL;
     6028    n1 = z8[0] & 0xf0f0f0f0f0f0f0f0ULL;
     6029    n0 = z8[0] & 0x0f0f0f0f0f0f0f0fULL;
     6030    z8[1] = (n3 >> 4) | (n2 << 12) | (n0 >> 52);
     6031    z8[0] = (n1 >> 4) | (n0 << 12);
     6032
     6033    /* XOR in next significant nibble * H */
     6034    m8 = (word64*)m[xi >> 4];
     6035    z8[0] ^= m8[0];
     6036    z8[1] ^= m8[1];
     6037    /* XOR in most significant nibble * remainder */
     6038    z8[0] ^= (word64)R[a];
     6039
     6040    /* Write back result. */
     6041    x8[0] = z8[0];
     6042    x8[1] = z8[1];
     6043}
     6044#endif
     6045
     6046void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
     6047    word32 cSz, byte* s, word32 sSz)
     6048{
     6049    byte x[AES_BLOCK_SIZE];
     6050    byte scratch[AES_BLOCK_SIZE];
     6051    word32 blocks, partial;
     6052
     6053    XMEMSET(x, 0, AES_BLOCK_SIZE);
     6054
     6055    /* Hash in A, the Additional Authentication Data */
     6056    if (aSz != 0 && a != NULL) {
     6057        blocks = aSz / AES_BLOCK_SIZE;
     6058        partial = aSz % AES_BLOCK_SIZE;
     6059        while (blocks--) {
     6060            xorbuf(x, a, AES_BLOCK_SIZE);
     6061            GMULT(x, aes->M0);
     6062            a += AES_BLOCK_SIZE;
     6063        }
     6064        if (partial != 0) {
     6065            XMEMSET(scratch, 0, AES_BLOCK_SIZE);
     6066            XMEMCPY(scratch, a, partial);
     6067            xorbuf(x, scratch, AES_BLOCK_SIZE);
     6068            GMULT(x, aes->M0);
     6069        }
     6070    }
     6071
     6072    /* Hash in C, the Ciphertext */
     6073    if (cSz != 0 && c != NULL) {
     6074        blocks = cSz / AES_BLOCK_SIZE;
     6075        partial = cSz % AES_BLOCK_SIZE;
     6076        while (blocks--) {
     6077            xorbuf(x, c, AES_BLOCK_SIZE);
     6078            GMULT(x, aes->M0);
     6079            c += AES_BLOCK_SIZE;
     6080        }
     6081        if (partial != 0) {
     6082            XMEMSET(scratch, 0, AES_BLOCK_SIZE);
     6083            XMEMCPY(scratch, c, partial);
     6084            xorbuf(x, scratch, AES_BLOCK_SIZE);
     6085            GMULT(x, aes->M0);
     6086        }
     6087    }
     6088
     6089    /* Hash in the lengths of A and C in bits */
     6090    FlattenSzInBits(&scratch[0], aSz);
     6091    FlattenSzInBits(&scratch[8], cSz);
     6092    xorbuf(x, scratch, AES_BLOCK_SIZE);
     6093    GMULT(x, aes->M0);
     6094
     6095    /* Copy the result into s. */
     6096    XMEMCPY(s, x, sSz);
     6097}
     6098
    55896099#elif defined(WORD64_AVAILABLE) && !defined(GCM_WORD32)
    55906100
     
    55956105    word64 V[2];
    55966106    int i, j;
     6107#ifdef AES_GCM_GMULT_CT
     6108    word64 v1;
     6109#endif
    55976110    V[0] = X[0];  V[1] = X[1];
    55986111
     
    56026115        for (j = 0; j < 64; j++)
    56036116        {
     6117#ifdef AES_GCM_GMULT_CT
     6118            word64 mask = 0 - (y >> 63);
     6119            Z[0] ^= V[0] & mask;
     6120            Z[1] ^= V[1] & mask;
     6121#else
    56046122            if (y & 0x8000000000000000ULL) {
    56056123                Z[0] ^= V[0];
    56066124                Z[1] ^= V[1];
    56076125            }
    5608 
     6126#endif
     6127
     6128#ifdef AES_GCM_GMULT_CT
     6129            v1 = (0 - (V[1] & 1)) & 0xE100000000000000ULL;
     6130            V[1] >>= 1;
     6131            V[1] |= V[0] << 63;
     6132            V[0] >>= 1;
     6133            V[0] ^= v1;
     6134#else
    56096135            if (V[1] & 0x0000000000000001) {
    56106136                V[1] >>= 1;
     
    56206146                V[0] >>= 1;
    56216147            }
     6148#endif
    56226149            y <<= 1;
    56236150        }
     
    59056432        return status;
    59066433
     6434    status = wolfSSL_CryptHwMutexLock();
     6435    if (status != 0)
     6436        return status;
     6437
    59076438    status = LTC_AES_EncryptTagGcm(LTC_BASE, in, out, sz, iv, ivSz,
    59086439        authIn, authInSz, (byte*)aes->key, keySize, authTag, authTagSz);
     6440    wolfSSL_CryptHwMutexUnLock();
    59096441
    59106442    return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
     
    59166448
    59176449/* this function supports inline encrypt */
     6450/* define STM32_AESGCM_PARTIAL for newer STM Cube HAL's with workaround
     6451   for handling partial packets to improve auth tag calculation performance by
     6452   using hardware */
    59186453static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz,
    59196454                                  const byte* iv, word32 ivSz,
     
    59286463#endif
    59296464    word32 keySize;
     6465#ifdef WOLFSSL_STM32_CUBEMX
    59306466    int status = HAL_OK;
    59316467    word32 blocks = sz / AES_BLOCK_SIZE;
     6468    word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)];
     6469#else
     6470    int status = SUCCESS;
     6471#endif
    59326472    word32 partial = sz % AES_BLOCK_SIZE;
    5933     byte tag[AES_BLOCK_SIZE];
    5934     byte partialBlock[AES_BLOCK_SIZE];
    5935     byte ctr[AES_BLOCK_SIZE];
     6473    word32 tag[AES_BLOCK_SIZE/sizeof(word32)];
     6474    word32 ctrInit[AES_BLOCK_SIZE/sizeof(word32)];
     6475    word32 ctr[AES_BLOCK_SIZE/sizeof(word32)];
     6476    word32 authhdr[AES_BLOCK_SIZE/sizeof(word32)];
    59366477    byte* authInPadded = NULL;
    5937     int authPadSz;
     6478    int authPadSz, wasAlloc = 0, useSwGhash = 0;
    59386479
    59396480    ret = wc_AesGetKeySize(aes, &keySize);
     
    59476488#endif
    59486489
    5949     ret = wolfSSL_CryptHwMutexLock();
    5950     if (ret != 0) {
    5951         return ret;
    5952     }
    5953 
    59546490    XMEMSET(ctr, 0, AES_BLOCK_SIZE);
    59556491    if (ivSz == GCM_NONCE_MID_SZ) {
     6492        byte* pCtr = (byte*)ctr;
    59566493        XMEMCPY(ctr, iv, ivSz);
    5957         ctr[AES_BLOCK_SIZE - 1] = 1;
     6494        pCtr[AES_BLOCK_SIZE - 1] = 1;
    59586495    }
    59596496    else {
    5960         GHASH(aes, NULL, 0, iv, ivSz, ctr, AES_BLOCK_SIZE);
    5961     }
    5962     /* Hardware requires counter + 1 */
    5963     IncrementGcmCounter(ctr);
    5964 
    5965     if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) {
    5966         /* Need to pad the AAD to a full block with zeros. */
    5967         authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
    5968         authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
    5969             DYNAMIC_TYPE_TMP_BUFFER);
    5970         if (authInPadded == NULL) {
    5971             wolfSSL_CryptHwMutexUnLock();
    5972             return MEMORY_E;
     6497        GHASH(aes, NULL, 0, iv, ivSz, (byte*)ctr, AES_BLOCK_SIZE);
     6498    }
     6499    XMEMCPY(ctrInit, ctr, sizeof(ctr)); /* save off initial counter for GMAC */
     6500
     6501    /* Authentication buffer - must be 4-byte multiple zero padded */
     6502    authPadSz = authInSz % sizeof(word32);
     6503    if (authPadSz != 0) {
     6504        authPadSz = authInSz + sizeof(word32) - authPadSz;
     6505        if (authPadSz <= sizeof(authhdr)) {
     6506            authInPadded = (byte*)authhdr;
     6507        }
     6508        else {
     6509            authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
     6510                DYNAMIC_TYPE_TMP_BUFFER);
     6511            if (authInPadded == NULL) {
     6512                wolfSSL_CryptHwMutexUnLock();
     6513                return MEMORY_E;
     6514            }
     6515            wasAlloc = 1;
    59736516        }
    59746517        XMEMSET(authInPadded, 0, authPadSz);
     
    59796522    }
    59806523
     6524    /* for cases where hardware cannot be used for authTag calculate it */
     6525    /* if IV is not 12 calculate GHASH using software */
     6526    if (ivSz != GCM_NONCE_MID_SZ
     6527#ifndef STM32_AESGCM_PARTIAL
     6528        /* or authIn is not a multiple of 4  */
     6529        || authPadSz != authInSz || sz == 0 || partial != 0
     6530#endif
     6531    ) {
     6532        useSwGhash = 1;
     6533    }
     6534
     6535    /* Hardware requires counter + 1 */
     6536    IncrementGcmCounter((byte*)ctr);
     6537
     6538    ret = wolfSSL_CryptHwMutexLock();
     6539    if (ret != 0) {
     6540        return ret;
     6541    }
    59816542#ifdef WOLFSSL_STM32_CUBEMX
    59826543    hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
    59836544    hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
    5984     hcryp.Init.HeaderSize = authInSz;
    5985 
    5986 #ifdef STM32_CRYPTO_AES_ONLY
     6545
     6546#if defined(STM32_HAL_V2)
     6547    hcryp.Init.Algorithm = CRYP_AES_GCM;
     6548    hcryp.Init.HeaderSize = authPadSz/sizeof(word32);
     6549    #ifdef STM32_AESGCM_PARTIAL
     6550    hcryp.Init.HeaderPadSize = authPadSz - authInSz;
     6551    #endif
     6552    ByteReverseWords(partialBlock, ctr, AES_BLOCK_SIZE);
     6553    hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
     6554    HAL_CRYP_Init(&hcryp);
     6555
     6556    /* GCM payload phase - can handle partial blocks */
     6557    status = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in,
     6558        (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
     6559    if (status == HAL_OK && !useSwGhash) {
     6560        /* Compute the authTag */
     6561        status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
     6562            STM32_HAL_TIMEOUT);
     6563    }
     6564#elif defined(STM32_CRYPTO_AES_ONLY)
    59876565    /* Set the CRYP parameters */
     6566    hcryp.Init.HeaderSize = authPadSz;
     6567    if (authPadSz == 0)
     6568        hcryp.Init.Header = NULL; /* cannot pass pointer here when authIn == 0 */
    59886569    hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_GCM_GMAC;
    59896570    hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
     
    59956576    if (status == HAL_OK) {
    59966577        /* GCM header phase */
    5997         hcryp.Init.GCMCMACPhase  = CRYP_HEADER_PHASE;
     6578        hcryp.Init.GCMCMACPhase = CRYP_HEADER_PHASE;
    59986579        status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
    59996580    }
    60006581    if (status == HAL_OK) {
    60016582        /* GCM payload phase - blocks */
    6002         hcryp.Init.GCMCMACPhase  = CRYP_PAYLOAD_PHASE;
     6583        hcryp.Init.GCMCMACPhase = CRYP_PAYLOAD_PHASE;
    60036584        if (blocks) {
    60046585            status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)in,
     
    60066587        }
    60076588    }
    6008     if (status == HAL_OK && (partial != 0 || blocks == 0)) {
     6589    if (status == HAL_OK && (partial != 0 || (sz > 0 && blocks == 0))) {
    60096590        /* GCM payload phase - partial remainder */
    60106591        XMEMSET(partialBlock, 0, sizeof(partialBlock));
    60116592        XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
    6012         status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial,
    6013             partialBlock, STM32_HAL_TIMEOUT);
     6593        status = HAL_CRYPEx_AES_Auth(&hcryp, (uint8_t*)partialBlock, partial,
     6594                (uint8_t*)partialBlock, STM32_HAL_TIMEOUT);
    60146595        XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
    60156596    }
    6016     if (status == HAL_OK) {
     6597    if (status == HAL_OK && !useSwGhash) {
    60176598        /* GCM final phase */
    60186599        hcryp.Init.GCMCMACPhase  = CRYP_FINAL_PHASE;
    6019         status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT);
    6020     }
    6021 #elif defined(STM32_HAL_V2)
    6022     hcryp.Init.Algorithm  = CRYP_AES_GCM;
    6023     ByteReverseWords((word32*)partialBlock, (word32*)ctr, AES_BLOCK_SIZE);
    6024     hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
    6025     HAL_CRYP_Init(&hcryp);
    6026 
    6027     /* GCM payload phase - can handle partial blocks */
    6028     status = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in,
    6029         (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
    6030     if (status == HAL_OK) {
    6031         /* Compute the authTag */
    6032         status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
    6033             STM32_HAL_TIMEOUT);
     6600        status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, (uint8_t*)tag, STM32_HAL_TIMEOUT);
    60346601    }
    60356602#else
     6603    hcryp.Init.HeaderSize = authPadSz;
    60366604    HAL_CRYP_Init(&hcryp);
    60376605    if (blocks) {
     
    60446612        XMEMSET(partialBlock, 0, sizeof(partialBlock));
    60456613        XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
    6046         status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, partialBlock, partial,
    6047             partialBlock, STM32_HAL_TIMEOUT);
     6614        status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, (uint8_t*)partialBlock, partial,
     6615            (uint8_t*)partialBlock, STM32_HAL_TIMEOUT);
    60486616        XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
    60496617    }
    6050     if (status == HAL_OK) {
     6618    if (status == HAL_OK && !useSwGhash) {
    60516619        /* Compute the authTag */
    6052         status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT);
     6620        status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, (uint8_t*)tag, STM32_HAL_TIMEOUT);
    60536621    }
    60546622#endif
     
    60586626    HAL_CRYP_DeInit(&hcryp);
    60596627
    6060 #else /* STD_PERI_LIB */
     6628#else /* Standard Peripheral Library */
    60616629    ByteReverseWords(keyCopy, (word32*)aes->key, keySize);
    60626630    status = CRYP_AES_GCM(MODE_ENCRYPT, (uint8_t*)ctr,
     
    60646632                         (uint8_t*)in,           sz,
    60656633                         (uint8_t*)authInPadded, authInSz,
    6066                          (uint8_t*)out,          tag);
     6634                         (uint8_t*)out,          (uint8_t*)tag);
    60676635    if (status != SUCCESS)
    60686636        ret = AES_GCM_AUTH_E;
    60696637#endif /* WOLFSSL_STM32_CUBEMX */
     6638    wolfSSL_CryptHwMutexUnLock();
    60706639
    60716640    if (ret == 0) {
    60726641        /* return authTag */
    60736642        if (authTag) {
    6074             /* STM32 GCM won't compute Auth correctly for partial or
    6075                 when IV != 12, so use software here */
    6076             if (sz == 0 || partial != 0 || ivSz != GCM_NONCE_MID_SZ) {
    6077                 DecrementGcmCounter(ctr); /* hardware requires +1, so subtract it */
     6643            if (useSwGhash) {
    60786644                GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
    6079                 wc_AesEncrypt(aes, ctr, tag);
     6645                wc_AesEncrypt(aes, (byte*)ctrInit, (byte*)tag);
    60806646                xorbuf(authTag, tag, authTagSz);
    60816647            }
    60826648            else {
     6649                /* use hardware calculated tag */
    60836650                XMEMCPY(authTag, tag, authTagSz);
    60846651            }
     
    60866653    }
    60876654
    6088     /* Free memory if not a multiple of AES_BLOCK_SZ */
    6089     if (authInPadded != authIn) {
     6655    /* Free memory */
     6656    if (wasAlloc) {
    60906657        XFREE(authInPadded, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
    60916658    }
    6092 
    6093     wolfSSL_CryptHwMutexUnLock();
    60946659
    60956660    return ret;
     
    61166681    const byte* p = in;
    61176682    byte* c = out;
    6118     byte counter[AES_BLOCK_SIZE];
    6119     byte initialCounter[AES_BLOCK_SIZE];
    6120     byte *ctr;
    6121     byte scratch[AES_BLOCK_SIZE];
     6683    ALIGN32 byte counter[AES_BLOCK_SIZE];
     6684    ALIGN32 byte initialCounter[AES_BLOCK_SIZE];
     6685    ALIGN32 byte scratch[AES_BLOCK_SIZE];
     6686
     6687    if (ivSz == GCM_NONCE_MID_SZ) {
     6688        /* Counter is IV with bottom 4 bytes set to: 0x00,0x00,0x00,0x01. */
     6689        XMEMCPY(counter, iv, ivSz);
     6690        XMEMSET(counter + GCM_NONCE_MID_SZ, 0,
     6691                                         AES_BLOCK_SIZE - GCM_NONCE_MID_SZ - 1);
     6692        counter[AES_BLOCK_SIZE - 1] = 1;
     6693    }
     6694    else {
     6695        /* Counter is GHASH of IV. */
    61226696#ifdef OPENSSL_EXTRA
    6123     word32 aadTemp;
    6124 #endif
    6125     ctr = counter;
    6126     XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
    6127     XMEMSET(scratch, 0, AES_BLOCK_SIZE);
    6128     if (ivSz == GCM_NONCE_MID_SZ) {
    6129         XMEMCPY(initialCounter, iv, ivSz);
    6130         initialCounter[AES_BLOCK_SIZE - 1] = 1;
    6131     }
    6132     else {
    6133 #ifdef OPENSSL_EXTRA
    6134         aadTemp = aes->aadLen;
     6697        word32 aadTemp = aes->aadLen;
    61356698        aes->aadLen = 0;
    61366699#endif
    6137         GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
     6700        GHASH(aes, NULL, 0, iv, ivSz, counter, AES_BLOCK_SIZE);
    61386701#ifdef OPENSSL_EXTRA
    61396702        aes->aadLen = aadTemp;
    61406703#endif
    61416704    }
    6142     XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
     6705    XMEMCPY(initialCounter, counter, AES_BLOCK_SIZE);
    61436706
    61446707#ifdef WOLFSSL_PIC32MZ_CRYPT
    61456708    if (blocks) {
    61466709        /* use initial IV for HW, but don't use it below */
    6147         XMEMCPY(aes->reg, ctr, AES_BLOCK_SIZE);
     6710        XMEMCPY(aes->reg, counter, AES_BLOCK_SIZE);
    61486711
    61496712        ret = wc_Pic32AesCrypt(
     
    61626725    if (c != p && blocks > 0) { /* can not handle inline encryption */
    61636726        while (blocks--) {
    6164             IncrementGcmCounter(ctr);
    6165             XMEMCPY(c, ctr, AES_BLOCK_SIZE);
     6727            IncrementGcmCounter(counter);
     6728            XMEMCPY(c, counter, AES_BLOCK_SIZE);
    61666729            c += AES_BLOCK_SIZE;
    61676730        }
     
    61756738    else
    61766739#endif /* HAVE_AES_ECB && !WOLFSSL_PIC32MZ_CRYPT */
    6177 
    6178     while (blocks--) {
    6179         IncrementGcmCounter(ctr);
    6180     #if !defined(WOLFSSL_PIC32MZ_CRYPT)
    6181         wc_AesEncrypt(aes, ctr, scratch);
    6182         xorbuf(scratch, p, AES_BLOCK_SIZE);
    6183         XMEMCPY(c, scratch, AES_BLOCK_SIZE);
    6184     #endif
    6185         p += AES_BLOCK_SIZE;
    6186         c += AES_BLOCK_SIZE;
     6740    {
     6741        while (blocks--) {
     6742            IncrementGcmCounter(counter);
     6743        #if !defined(WOLFSSL_PIC32MZ_CRYPT)
     6744            wc_AesEncrypt(aes, counter, scratch);
     6745            xorbufout(c, scratch, p, AES_BLOCK_SIZE);
     6746        #endif
     6747            p += AES_BLOCK_SIZE;
     6748            c += AES_BLOCK_SIZE;
     6749        }
    61876750    }
    61886751
    61896752    if (partial != 0) {
    6190         IncrementGcmCounter(ctr);
    6191         wc_AesEncrypt(aes, ctr, scratch);
    6192         xorbuf(scratch, p, partial);
    6193         XMEMCPY(c, scratch, partial);
     6753        IncrementGcmCounter(counter);
     6754        wc_AesEncrypt(aes, counter, scratch);
     6755        xorbufout(c, scratch, p, partial);
    61946756    }
    61956757    if (authTag) {
     
    62696831#endif /* WOLFSSL_ASYNC_CRYPT */
    62706832
     6833#ifdef WOLFSSL_SILABS_SE_ACCEL
     6834    return wc_AesGcmEncrypt_silabs(
     6835        aes, out, in, sz,
     6836        iv, ivSz,
     6837        authTag, authTagSz,
     6838        authIn, authInSz);
     6839#endif
     6840
    62716841#ifdef STM32_CRYPTO_AES_GCM
    6272     /* The STM standard peripheral library API's doesn't support partial blocks */
    6273     #ifdef STD_PERI_LIB
    6274     if (partial == 0)
    6275     #endif
    6276     {
    6277         return wc_AesGcmEncrypt_STM32(
    6278             aes, out, in, sz, iv, ivSz,
    6279             authTag, authTagSz, authIn, authInSz);
    6280     }
     6842    return wc_AesGcmEncrypt_STM32(
     6843        aes, out, in, sz, iv, ivSz,
     6844        authTag, authTagSz, authIn, authInSz);
    62816845#endif /* STM32_CRYPTO_AES_GCM */
    62826846
     
    62846848    #ifdef HAVE_INTEL_AVX2
    62856849    if (IS_INTEL_AVX2(intel_flags)) {
     6850        SAVE_VECTOR_REGISTERS();
    62866851        AES_GCM_encrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
    62876852                                 authTagSz, (const byte*)aes->key, aes->rounds);
     6853        RESTORE_VECTOR_REGISTERS();
    62886854        return 0;
    62896855    }
     
    62926858    #ifdef HAVE_INTEL_AVX1
    62936859    if (IS_INTEL_AVX1(intel_flags)) {
     6860        SAVE_VECTOR_REGISTERS();
    62946861        AES_GCM_encrypt_avx1(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
    62956862                                 authTagSz, (const byte*)aes->key, aes->rounds);
     6863        RESTORE_VECTOR_REGISTERS();
    62966864        return 0;
    62976865    }
     
    63416909    }
    63426910
     6911    status = wolfSSL_CryptHwMutexLock();
     6912    if (status != 0)
     6913        return status;
     6914
    63436915    status = LTC_AES_DecryptTagGcm(LTC_BASE, in, out, sz, iv, ivSz,
    63446916        authIn, authInSz, (byte*)aes->key, keySize, authTag, authTagSz);
     6917    wolfSSL_CryptHwMutexUnLock();
    63456918
    63466919    return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
     
    63596932    int ret;
    63606933#ifdef WOLFSSL_STM32_CUBEMX
     6934    int status = HAL_OK;
    63616935    CRYP_HandleTypeDef hcryp;
     6936    word32 blocks = sz / AES_BLOCK_SIZE;
    63626937#else
     6938    int status = SUCCESS;
    63636939    word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
    63646940#endif
    63656941    word32 keySize;
    6366     int status = HAL_OK;
    6367     word32 blocks = sz / AES_BLOCK_SIZE;
    63686942    word32 partial = sz % AES_BLOCK_SIZE;
    6369     byte tag[AES_BLOCK_SIZE];
    6370     byte partialBlock[AES_BLOCK_SIZE];
    6371     byte ctr[AES_BLOCK_SIZE];
     6943    word32 tag[AES_BLOCK_SIZE/sizeof(word32)];
     6944    word32 tagExpected[AES_BLOCK_SIZE/sizeof(word32)];
     6945    word32 partialBlock[AES_BLOCK_SIZE/sizeof(word32)];
     6946    word32 ctr[AES_BLOCK_SIZE/sizeof(word32)];
     6947    word32 authhdr[AES_BLOCK_SIZE/sizeof(word32)];
    63726948    byte* authInPadded = NULL;
    6373     int authPadSz;
     6949    int authPadSz, wasAlloc = 0, tagComputed = 0;
    63746950
    63756951    ret = wc_AesGetKeySize(aes, &keySize);
     
    63836959#endif
    63846960
     6961    XMEMSET(ctr, 0, AES_BLOCK_SIZE);
     6962    if (ivSz == GCM_NONCE_MID_SZ) {
     6963        byte* pCtr = (byte*)ctr;
     6964        XMEMCPY(ctr, iv, ivSz);
     6965        pCtr[AES_BLOCK_SIZE - 1] = 1;
     6966    }
     6967    else {
     6968        GHASH(aes, NULL, 0, iv, ivSz, (byte*)ctr, AES_BLOCK_SIZE);
     6969    }
     6970
     6971    /* Make copy of expected authTag, which could get corrupted in some
     6972     * Cube HAL versions without proper partial block support.
     6973     * For TLS blocks the authTag is after the output buffer, so save it */
     6974    XMEMCPY(tagExpected, authTag, authTagSz);
     6975
     6976    /* Authentication buffer - must be 4-byte multiple zero padded */
     6977    authPadSz = authInSz % sizeof(word32);
     6978    if (authPadSz != 0) {
     6979        authPadSz = authInSz + sizeof(word32) - authPadSz;
     6980    }
     6981    else {
     6982        authPadSz = authInSz;
     6983    }
     6984
     6985    /* for cases where hardware cannot be used for authTag calculate it */
     6986    /* if IV is not 12 calculate GHASH using software */
     6987    if (ivSz != GCM_NONCE_MID_SZ || sz == 0 || partial != 0
     6988#ifndef STM32_AESGCM_PARTIAL
     6989        /* or authIn is not a multiple of 4  */
     6990        || authPadSz != authInSz
     6991#endif
     6992    ) {
     6993                GHASH(aes, authIn, authInSz, in, sz, (byte*)tag, sizeof(tag));
     6994                wc_AesEncrypt(aes, (byte*)ctr, (byte*)partialBlock);
     6995                xorbuf(tag, partialBlock, sizeof(tag));
     6996                tagComputed = 1;
     6997    }
     6998
     6999    /* if using hardware for authentication tag make sure its aligned and zero padded */
     7000    if (authPadSz != authInSz && !tagComputed) {
     7001        if (authPadSz <= sizeof(authhdr)) {
     7002            authInPadded = (byte*)authhdr;
     7003        }
     7004        else {
     7005            authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
     7006                DYNAMIC_TYPE_TMP_BUFFER);
     7007            if (authInPadded == NULL) {
     7008                wolfSSL_CryptHwMutexUnLock();
     7009                return MEMORY_E;
     7010            }
     7011            wasAlloc = 1;
     7012        }
     7013        XMEMSET(authInPadded, 0, authPadSz);
     7014        XMEMCPY(authInPadded, authIn, authInSz);
     7015    } else {
     7016        authInPadded = (byte*)authIn;
     7017    }
     7018
     7019    /* Hardware requires counter + 1 */
     7020    IncrementGcmCounter((byte*)ctr);
     7021
    63857022    ret = wolfSSL_CryptHwMutexLock();
    63867023    if (ret != 0) {
    63877024        return ret;
    63887025    }
    6389 
    6390     XMEMSET(ctr, 0, AES_BLOCK_SIZE);
    6391     if (ivSz == GCM_NONCE_MID_SZ) {
    6392         XMEMCPY(ctr, iv, ivSz);
    6393         ctr[AES_BLOCK_SIZE - 1] = 1;
    6394     }
    6395     else {
    6396         GHASH(aes, NULL, 0, iv, ivSz, ctr, AES_BLOCK_SIZE);
    6397     }
    6398     /* Hardware requires counter + 1 */
    6399     IncrementGcmCounter(ctr);
    6400 
    6401     if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) {
    6402         /* Need to pad the AAD to a full block with zeros. */
    6403         authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
    6404         authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
    6405             DYNAMIC_TYPE_TMP_BUFFER);
    6406         if (authInPadded == NULL) {
    6407             wolfSSL_CryptHwMutexUnLock();
    6408             return MEMORY_E;
    6409         }
    6410         XMEMSET(authInPadded, 0, authPadSz);
    6411         XMEMCPY(authInPadded, authIn, authInSz);
    6412     } else {
    6413         authPadSz = authInSz;
    6414         authInPadded = (byte*)authIn;
    6415     }
    6416 
    64177026#ifdef WOLFSSL_STM32_CUBEMX
    64187027    hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
    64197028    hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
    6420     hcryp.Init.HeaderSize = authInSz;
    6421 
    6422 #ifdef STM32_CRYPTO_AES_ONLY
     7029
     7030#if defined(STM32_HAL_V2)
     7031    hcryp.Init.Algorithm = CRYP_AES_GCM;
     7032    hcryp.Init.HeaderSize = authPadSz/sizeof(word32);
     7033    #ifdef STM32_AESGCM_PARTIAL
     7034    hcryp.Init.HeaderPadSize = authPadSz - authInSz;
     7035    #endif
     7036    ByteReverseWords(partialBlock, ctr, AES_BLOCK_SIZE);
     7037    hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
     7038    HAL_CRYP_Init(&hcryp);
     7039
     7040    /* GCM payload phase - can handle partial blocks */
     7041    status = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in,
     7042        (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
     7043    if (status == HAL_OK && tagComputed == 0) {
     7044        /* Compute the authTag */
     7045        status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
     7046            STM32_HAL_TIMEOUT);
     7047    }
     7048#elif defined(STM32_CRYPTO_AES_ONLY)
    64237049    /* Set the CRYP parameters */
     7050    hcryp.Init.HeaderSize = authPadSz;
     7051    if (authPadSz == 0)
     7052        hcryp.Init.Header = NULL; /* cannot pass pointer when authIn == 0 */
    64247053    hcryp.Init.ChainingMode  = CRYP_CHAINMODE_AES_GCM_GMAC;
    64257054    hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
     
    64367065    if (status == HAL_OK) {
    64377066        /* GCM payload phase - blocks */
    6438         hcryp.Init.GCMCMACPhase  = CRYP_PAYLOAD_PHASE;
     7067        hcryp.Init.GCMCMACPhase = CRYP_PAYLOAD_PHASE;
    64397068        if (blocks) {
    64407069            status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)in,
     
    64427071        }
    64437072    }
    6444     if (status == HAL_OK && (partial != 0 || blocks == 0)) {
     7073    if (status == HAL_OK && (partial != 0 || (sz > 0 && blocks == 0))) {
    64457074        /* GCM payload phase - partial remainder */
    64467075        XMEMSET(partialBlock, 0, sizeof(partialBlock));
    64477076        XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
    6448         status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial,
    6449             partialBlock, STM32_HAL_TIMEOUT);
     7077        status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)partialBlock, partial,
     7078            (byte*)partialBlock, STM32_HAL_TIMEOUT);
    64507079        XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
    64517080    }
    6452     if (status == HAL_OK) {
     7081    if (status == HAL_OK && tagComputed == 0) {
    64537082        /* GCM final phase */
    64547083        hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE;
    6455         status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT);
    6456     }
    6457 #elif defined(STM32_HAL_V2)
    6458     hcryp.Init.Algorithm = CRYP_AES_GCM;
    6459     ByteReverseWords((word32*)partialBlock, (word32*)ctr, AES_BLOCK_SIZE);
    6460     hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
    6461     HAL_CRYP_Init(&hcryp);
    6462 
    6463     /* GCM payload phase - can handle partial blocks */
    6464     status = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in,
    6465         (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
    6466     if (status == HAL_OK) {
    6467         /* Compute the authTag */
    6468         status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
    6469             STM32_HAL_TIMEOUT);
     7084        status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, (byte*)tag, STM32_HAL_TIMEOUT);
    64707085    }
    64717086#else
     7087    hcryp.Init.HeaderSize = authPadSz;
    64727088    HAL_CRYP_Init(&hcryp);
    64737089    if (blocks) {
     
    64807096        XMEMSET(partialBlock, 0, sizeof(partialBlock));
    64817097        XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
    6482         status = HAL_CRYPEx_AESGCM_Decrypt(&hcryp, partialBlock, partial,
    6483             partialBlock, STM32_HAL_TIMEOUT);
     7098        status = HAL_CRYPEx_AESGCM_Decrypt(&hcryp, (byte*)partialBlock, partial,
     7099            (byte*)partialBlock, STM32_HAL_TIMEOUT);
    64847100        XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
    64857101    }
    6486     if (status == HAL_OK) {
     7102    if (status == HAL_OK && tagComputed == 0) {
    64877103        /* Compute the authTag */
    6488         status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT);
     7104        status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, (byte*)tag, STM32_HAL_TIMEOUT);
    64897105    }
    64907106#endif
     
    64957111    HAL_CRYP_DeInit(&hcryp);
    64967112
    6497 #else /* STD_PERI_LIB */
     7113#else /* Standard Peripheral Library */
    64987114    ByteReverseWords(keyCopy, (word32*)aes->key, aes->keylen);
    64997115
     
    65017117     * they are not block aligned, because this length (in bits) is used
    65027118     * in the final GHASH. */
     7119    XMEMSET(partialBlock, 0, sizeof(partialBlock)); /* use this to get tag */
    65037120    status = CRYP_AES_GCM(MODE_DECRYPT, (uint8_t*)ctr,
    65047121                         (uint8_t*)keyCopy,      keySize * 8,
    65057122                         (uint8_t*)in,           sz,
    65067123                         (uint8_t*)authInPadded, authInSz,
    6507                          (uint8_t*)out,          tag);
     7124                         (uint8_t*)out,          (uint8_t*)partialBlock);
    65087125    if (status != SUCCESS)
    65097126        ret = AES_GCM_AUTH_E;
     7127    if (tagComputed == 0)
     7128        XMEMCPY(tag, partialBlock, authTagSz);
    65107129#endif /* WOLFSSL_STM32_CUBEMX */
    6511 
    6512     /* STM32 GCM hardware only supports IV of 12 bytes, so use software for auth */
    6513     if (sz == 0 || ivSz != GCM_NONCE_MID_SZ) {
    6514         DecrementGcmCounter(ctr); /* hardware requires +1, so subtract it */
    6515         GHASH(aes, authIn, authInSz, in, sz, tag, sizeof(tag));
    6516         wc_AesEncrypt(aes, ctr, partialBlock);
    6517         xorbuf(tag, partialBlock, sizeof(tag));
    6518     }
    6519 
    6520     if (ConstantCompare(authTag, tag, authTagSz) != 0) {
     7130    wolfSSL_CryptHwMutexUnLock();
     7131
     7132    /* Check authentication tag */
     7133    if (ConstantCompare((const byte*)tagExpected, (byte*)tag, authTagSz) != 0) {
    65217134        ret = AES_GCM_AUTH_E;
    65227135    }
    65237136
    6524     /* Free memory if not a multiple of AES_BLOCK_SZ */
    6525     if (authInPadded != authIn) {
     7137    /* Free memory */
     7138    if (wasAlloc) {
    65267139        XFREE(authInPadded, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
    65277140    }
    6528 
    6529     wolfSSL_CryptHwMutexUnLock();
    65307141
    65317142    return ret;
     
    65527163    const byte* c = in;
    65537164    byte* p = out;
    6554     byte counter[AES_BLOCK_SIZE];
    6555     byte initialCounter[AES_BLOCK_SIZE];
    6556     byte *ctr;
    6557     byte scratch[AES_BLOCK_SIZE];
    6558     byte Tprime[AES_BLOCK_SIZE];
    6559     byte EKY0[AES_BLOCK_SIZE];
     7165    ALIGN32 byte counter[AES_BLOCK_SIZE];
     7166    ALIGN32 byte scratch[AES_BLOCK_SIZE];
     7167    ALIGN32 byte Tprime[AES_BLOCK_SIZE];
     7168    ALIGN32 byte EKY0[AES_BLOCK_SIZE];
     7169
     7170    if (ivSz == GCM_NONCE_MID_SZ) {
     7171        /* Counter is IV with bottom 4 bytes set to: 0x00,0x00,0x00,0x01. */
     7172        XMEMCPY(counter, iv, ivSz);
     7173        XMEMSET(counter + GCM_NONCE_MID_SZ, 0,
     7174                                         AES_BLOCK_SIZE - GCM_NONCE_MID_SZ - 1);
     7175        counter[AES_BLOCK_SIZE - 1] = 1;
     7176    }
     7177    else {
     7178        /* Counter is GHASH of IV. */
    65607179#ifdef OPENSSL_EXTRA
    6561     word32 aadTemp;
    6562 #endif
    6563     ctr = counter;
    6564     XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
    6565     if (ivSz == GCM_NONCE_MID_SZ) {
    6566         XMEMCPY(initialCounter, iv, ivSz);
    6567         initialCounter[AES_BLOCK_SIZE - 1] = 1;
    6568     }
    6569     else {
    6570 #ifdef OPENSSL_EXTRA
    6571         aadTemp = aes->aadLen;
     7180        word32 aadTemp = aes->aadLen;
    65727181        aes->aadLen = 0;
    65737182#endif
    6574         GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
     7183        GHASH(aes, NULL, 0, iv, ivSz, counter, AES_BLOCK_SIZE);
    65757184#ifdef OPENSSL_EXTRA
    65767185        aes->aadLen = aadTemp;
    65777186#endif
    65787187    }
    6579     XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
    6580 
    6581     /* Calc the authTag again using the received auth data and the cipher text */
     7188
     7189    /* Calc the authTag again using received auth data and the cipher text */
    65827190    GHASH(aes, authIn, authInSz, in, sz, Tprime, sizeof(Tprime));
    6583     wc_AesEncrypt(aes, ctr, EKY0);
     7191    wc_AesEncrypt(aes, counter, EKY0);
    65847192    xorbuf(Tprime, EKY0, sizeof(Tprime));
    65857193
     
    65987206    if (blocks) {
    65997207        /* use initial IV for HW, but don't use it below */
    6600         XMEMCPY(aes->reg, ctr, AES_BLOCK_SIZE);
     7208        XMEMCPY(aes->reg, counter, AES_BLOCK_SIZE);
    66017209
    66027210        ret = wc_Pic32AesCrypt(
     
    66157223    if (c != p && blocks > 0) { /* can not handle inline decryption */
    66167224        while (blocks--) {
    6617             IncrementGcmCounter(ctr);
    6618             XMEMCPY(p, ctr, AES_BLOCK_SIZE);
     7225            IncrementGcmCounter(counter);
     7226            XMEMCPY(p, counter, AES_BLOCK_SIZE);
    66197227            p += AES_BLOCK_SIZE;
    66207228        }
     
    66297237    else
    66307238#endif /* HAVE_AES_ECB && !PIC32MZ */
    6631     while (blocks--) {
    6632         IncrementGcmCounter(ctr);
    6633     #if !defined(WOLFSSL_PIC32MZ_CRYPT)
    6634         wc_AesEncrypt(aes, ctr, scratch);
    6635         xorbuf(scratch, c, AES_BLOCK_SIZE);
    6636         XMEMCPY(p, scratch, AES_BLOCK_SIZE);
    6637     #endif
    6638         p += AES_BLOCK_SIZE;
    6639         c += AES_BLOCK_SIZE;
     7239    {
     7240        while (blocks--) {
     7241            IncrementGcmCounter(counter);
     7242        #if !defined(WOLFSSL_PIC32MZ_CRYPT)
     7243            wc_AesEncrypt(aes, counter, scratch);
     7244            xorbufout(p, scratch, c, AES_BLOCK_SIZE);
     7245        #endif
     7246            p += AES_BLOCK_SIZE;
     7247            c += AES_BLOCK_SIZE;
     7248        }
    66407249    }
    66417250
    66427251    if (partial != 0) {
    6643         IncrementGcmCounter(ctr);
    6644         wc_AesEncrypt(aes, ctr, scratch);
     7252        IncrementGcmCounter(counter);
     7253        wc_AesEncrypt(aes, counter, scratch);
    66457254        xorbuf(scratch, c, partial);
    66467255        XMEMCPY(p, scratch, partial);
     
    67167325#endif /* WOLFSSL_ASYNC_CRYPT */
    67177326
     7327#ifdef WOLFSSL_SILABS_SE_ACCEL
     7328    return wc_AesGcmDecrypt_silabs(
     7329        aes, out, in, sz, iv, ivSz,
     7330        authTag, authTagSz, authIn, authInSz);
     7331
     7332#endif
     7333
    67187334#ifdef STM32_CRYPTO_AES_GCM
    67197335    /* The STM standard peripheral library API's doesn't support partial blocks */
    6720     #ifdef STD_PERI_LIB
    6721     if (partial == 0)
    6722     #endif
    6723     {
    6724         return wc_AesGcmDecrypt_STM32(
    6725             aes, out, in, sz, iv, ivSz,
    6726             authTag, authTagSz, authIn, authInSz);
    6727     }
     7336    return wc_AesGcmDecrypt_STM32(
     7337        aes, out, in, sz, iv, ivSz,
     7338        authTag, authTagSz, authIn, authInSz);
    67287339#endif /* STM32_CRYPTO_AES_GCM */
    67297340
     
    67317342    #ifdef HAVE_INTEL_AVX2
    67327343    if (IS_INTEL_AVX2(intel_flags)) {
     7344        SAVE_VECTOR_REGISTERS();
    67337345        AES_GCM_decrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
    67347346                                 authTagSz, (byte*)aes->key, aes->rounds, &res);
     7347        RESTORE_VECTOR_REGISTERS();
    67357348        if (res == 0)
    67367349            return AES_GCM_AUTH_E;
     
    67417354    #ifdef HAVE_INTEL_AVX1
    67427355    if (IS_INTEL_AVX1(intel_flags)) {
     7356        SAVE_VECTOR_REGISTERS();
    67437357        AES_GCM_decrypt_avx1(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
    67447358                                 authTagSz, (byte*)aes->key, aes->rounds, &res);
     7359        RESTORE_VECTOR_REGISTERS();
    67457360        if (res == 0)
    67467361            return AES_GCM_AUTH_E;
     
    68767491            byte* authTag, word32 authTagSz, WC_RNG* rng)
    68777492{
    6878     Aes aes;
     7493#ifdef WOLFSSL_SMALL_STACK
     7494    Aes *aes = NULL;
     7495#else
     7496    Aes aes[1];
     7497#endif
    68797498    int ret;
    68807499
     
    68857504    }
    68867505
    6887     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
     7506#ifdef WOLFSSL_SMALL_STACK
     7507    if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
     7508                              DYNAMIC_TYPE_AES)) == NULL)
     7509        return MEMORY_E;
     7510#endif
     7511
     7512    ret = wc_AesInit(aes, NULL, INVALID_DEVID);
    68887513    if (ret == 0) {
    6889         ret = wc_AesGcmSetKey(&aes, key, keySz);
     7514        ret = wc_AesGcmSetKey(aes, key, keySz);
    68907515        if (ret == 0)
    6891             ret = wc_AesGcmSetIV(&aes, ivSz, NULL, 0, rng);
     7516            ret = wc_AesGcmSetIV(aes, ivSz, NULL, 0, rng);
    68927517        if (ret == 0)
    6893             ret = wc_AesGcmEncrypt_ex(&aes, NULL, NULL, 0, iv, ivSz,
     7518            ret = wc_AesGcmEncrypt_ex(aes, NULL, NULL, 0, iv, ivSz,
    68947519                                  authTag, authTagSz, authIn, authInSz);
    6895         wc_AesFree(&aes);
    6896     }
    6897     ForceZero(&aes, sizeof(aes));
     7520        wc_AesFree(aes);
     7521    }
     7522    ForceZero(aes, sizeof *aes);
     7523#ifdef WOLFSSL_SMALL_STACK
     7524    XFREE(aes, NULL, DYNAMIC_TYPE_AES);
     7525#endif
    68987526
    68997527    return ret;
     
    69067534{
    69077535    int ret;
    6908 #ifndef NO_AES_DECRYPT
    6909     Aes aes;
     7536#ifdef HAVE_AES_DECRYPT
     7537#ifdef WOLFSSL_SMALL_STACK
     7538    Aes *aes = NULL;
     7539#else
     7540    Aes aes[1];
     7541#endif
    69107542
    69117543    if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
     
    69157547    }
    69167548
    6917     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
     7549#ifdef WOLFSSL_SMALL_STACK
     7550    if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
     7551                              DYNAMIC_TYPE_AES)) == NULL)
     7552        return MEMORY_E;
     7553#endif
     7554
     7555    ret = wc_AesInit(aes, NULL, INVALID_DEVID);
    69187556    if (ret == 0) {
    6919         ret = wc_AesGcmSetKey(&aes, key, keySz);
     7557        ret = wc_AesGcmSetKey(aes, key, keySz);
    69207558        if (ret == 0)
    6921             ret = wc_AesGcmDecrypt(&aes, NULL, NULL, 0, iv, ivSz,
     7559            ret = wc_AesGcmDecrypt(aes, NULL, NULL, 0, iv, ivSz,
    69227560                                  authTag, authTagSz, authIn, authInSz);
    6923         wc_AesFree(&aes);
    6924     }
    6925     ForceZero(&aes, sizeof(aes));
     7561        wc_AesFree(aes);
     7562    }
     7563    ForceZero(aes, sizeof *aes);
     7564#ifdef WOLFSSL_SMALL_STACK
     7565    XFREE(aes, NULL, DYNAMIC_TYPE_AES);
     7566#endif
    69267567#else
    69277568    (void)key;
     
    69717612}
    69727613
     7614
     7615/* Checks if the tag size is an accepted value based on RFC 3610 section 2
     7616 * returns 0 if tag size is ok
     7617 */
     7618int wc_AesCcmCheckTagSize(int sz)
     7619{
     7620    /* values here are from RFC 3610 section 2 */
     7621    if (sz != 4 && sz != 6 && sz != 8 && sz != 10 && sz != 12 && sz != 14
     7622            && sz != 16) {
     7623        WOLFSSL_MSG("Bad auth tag size AES-CCM");
     7624        return BAD_FUNC_ARG;
     7625    }
     7626    return 0;
     7627}
     7628
    69737629#ifdef WOLFSSL_ARMASM
    69747630    /* implementation located in wolfcrypt/src/port/arm/armv8-aes.c */
     
    69807636    /* implemented in wolfcrypt/src/port/caam_aes.c */
    69817637
     7638#elif defined(WOLFSSL_SILABS_SE_ACCEL)
     7639    /* implemented in wolfcrypt/src/port/silabs/silabs_aes.c */
     7640int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
     7641                   const byte* nonce, word32 nonceSz,
     7642                   byte* authTag, word32 authTagSz,
     7643                   const byte* authIn, word32 authInSz)
     7644{
     7645    return wc_AesCcmEncrypt_silabs(
     7646        aes, out, in, inSz,
     7647        nonce, nonceSz,
     7648        authTag, authTagSz,
     7649        authIn, authInSz);
     7650}
     7651
     7652#ifdef HAVE_AES_DECRYPT
     7653int  wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
     7654                   const byte* nonce, word32 nonceSz,
     7655                   const byte* authTag, word32 authTagSz,
     7656                   const byte* authIn, word32 authInSz)
     7657{
     7658    return wc_AesCcmDecrypt_silabs(
     7659        aes, out, in, inSz,
     7660        nonce, nonceSz,
     7661        authTag, authTagSz,
     7662        authIn, authInSz);
     7663}
     7664#endif
    69827665#elif defined(FREESCALE_LTC)
    69837666
     
    69897672{
    69907673    byte *key;
    6991     uint32_t keySize;
     7674    word32 keySize;
    69927675    status_t status;
    69937676
    69947677    /* sanity check on arguments */
     7678    /* note, LTC_AES_EncryptTagCcm() doesn't allow null src or dst
     7679     * ptrs even if inSz is zero (ltc_aes_ccm_check_input_args()), so
     7680     * don't allow it here either.
     7681     */
    69957682    if (aes == NULL || out == NULL || in == NULL || nonce == NULL
    6996             || authTag == NULL || nonceSz < 7 || nonceSz > 13)
     7683            || authTag == NULL || nonceSz < 7 || nonceSz > 13) {
    69977684        return BAD_FUNC_ARG;
     7685    }
     7686
     7687    if (wc_AesCcmCheckTagSize(authTagSz) != 0) {
     7688        return BAD_FUNC_ARG;
     7689    }
    69987690
    69997691    key = (byte*)aes->key;
     
    70047696    }
    70057697
     7698    status = wolfSSL_CryptHwMutexLock();
     7699    if (status != 0)
     7700        return status;
     7701
    70067702    status = LTC_AES_EncryptTagCcm(LTC_BASE, in, out, inSz,
    70077703        nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
     7704    wolfSSL_CryptHwMutexUnLock();
    70087705
    70097706    return (kStatus_Success == status) ? 0 : BAD_FUNC_ARG;
     
    70177714{
    70187715    byte *key;
    7019     uint32_t keySize;
     7716    word32 keySize;
    70207717    status_t status;
    70217718
    70227719    /* sanity check on arguments */
    70237720    if (aes == NULL || out == NULL || in == NULL || nonce == NULL
    7024             || authTag == NULL || nonceSz < 7 || nonceSz > 13)
     7721            || authTag == NULL || nonceSz < 7 || nonceSz > 13) {
    70257722        return BAD_FUNC_ARG;
     7723    }
    70267724
    70277725    key = (byte*)aes->key;
     
    70317729        return status;
    70327730    }
    7033 
     7731   
     7732    status = wolfSSL_CryptHwMutexLock();
     7733    if (status != 0)
     7734        return status;
    70347735    status = LTC_AES_DecryptTagCcm(LTC_BASE, in, out, inSz,
    70357736        nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
    7036 
    7037     if (status == kStatus_Success) {
    7038         return 0;
    7039     }
    7040     else {
     7737    wolfSSL_CryptHwMutexUnLock();
     7738
     7739    if (status != kStatus_Success) {
    70417740        XMEMSET(out, 0, inSz);
    70427741        return AES_CCM_AUTH_E;
    70437742    }
     7743    return 0;
    70447744}
    70457745#endif /* HAVE_AES_DECRYPT */
     
    71317831
    71327832    for (i = 0; i < lenSz; i++) {
    7133         if (++B[AES_BLOCK_SIZE * 1 - 1 - i] != 0) break;
    7134     }
    7135     B[AES_BLOCK_SIZE * 2 - 1] += 2;
    7136     if (B[AES_BLOCK_SIZE * 2 - 1] < 2) {
    7137         for (i = 1; i < lenSz; i++) {
    7138             if (++B[AES_BLOCK_SIZE * 2 - 1 - i] != 0) break;
    7139         }
    7140     }
    7141     B[AES_BLOCK_SIZE * 3 - 1] += 3;
    7142     if (B[AES_BLOCK_SIZE * 3 - 1] < 3) {
     7833        if (++B[AES_BLOCK_SIZE * 2 - 1 - i] != 0) break;
     7834    }
     7835    B[AES_BLOCK_SIZE * 3 - 1] += 2;
     7836    if (B[AES_BLOCK_SIZE * 3 - 1] < 2) {
    71437837        for (i = 1; i < lenSz; i++) {
    71447838            if (++B[AES_BLOCK_SIZE * 3 - 1 - i] != 0) break;
     7839        }
     7840    }
     7841    B[AES_BLOCK_SIZE * 4 - 1] += 3;
     7842    if (B[AES_BLOCK_SIZE * 4 - 1] < 3) {
     7843        for (i = 1; i < lenSz; i++) {
     7844            if (++B[AES_BLOCK_SIZE * 4 - 1 - i] != 0) break;
    71457845        }
    71467846    }
     
    71807880
    71817881    /* sanity check on arguments */
    7182     if (aes == NULL || out == NULL || in == NULL || nonce == NULL
    7183             || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
     7882    if (aes == NULL || (inSz != 0 && (in == NULL || out == NULL)) ||
     7883        nonce == NULL || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
    71847884            authTagSz > AES_BLOCK_SIZE)
    71857885        return BAD_FUNC_ARG;
     7886
     7887    /* sanity check on tag size */
     7888    if (wc_AesCcmCheckTagSize(authTagSz) != 0) {
     7889        return BAD_FUNC_ARG;
     7890    }
    71867891
    71877892    XMEMSET(A, 0, sizeof(A));
     
    72177922            AesCcmCtrIncSet4(B, lenSz);
    72187923
     7924            SAVE_VECTOR_REGISTERS();
    72197925            AES_ECB_encrypt(B, A, AES_BLOCK_SIZE * 4, (byte*)aes->key,
    72207926                            aes->rounds);
     7927            RESTORE_VECTOR_REGISTERS();
     7928
    72217929            xorbuf(A, in, AES_BLOCK_SIZE * 4);
    72227930            XMEMCPY(out, A, AES_BLOCK_SIZE * 4);
     
    72267934            out += AES_BLOCK_SIZE * 4;
    72277935
    7228             if (inSz < AES_BLOCK_SIZE * 4) {
    7229                 AesCcmCtrInc4(B, lenSz);
    7230             }
     7936            AesCcmCtrInc4(B, lenSz);
    72317937        }
    72327938    }
     
    72767982
    72777983    /* sanity check on arguments */
    7278     if (aes == NULL || out == NULL || in == NULL || nonce == NULL
    7279             || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
    7280             authTagSz > AES_BLOCK_SIZE)
     7984    if (aes == NULL || (inSz != 0 && (in == NULL || out == NULL)) ||
     7985        nonce == NULL || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
     7986        authTagSz > AES_BLOCK_SIZE)
    72817987        return BAD_FUNC_ARG;
     7988
     7989    /* sanity check on tag size */
     7990    if (wc_AesCcmCheckTagSize(authTagSz) != 0) {
     7991        return BAD_FUNC_ARG;
     7992    }
    72827993
    72837994    o = out;
     
    72968007            AesCcmCtrIncSet4(B, lenSz);
    72978008
     8009            SAVE_VECTOR_REGISTERS();
    72988010            AES_ECB_encrypt(B, A, AES_BLOCK_SIZE * 4, (byte*)aes->key,
    72998011                            aes->rounds);
     8012            RESTORE_VECTOR_REGISTERS();
     8013
    73008014            xorbuf(A, in, AES_BLOCK_SIZE * 4);
    73018015            XMEMCPY(o, A, AES_BLOCK_SIZE * 4);
     
    73058019            o += AES_BLOCK_SIZE * 4;
    73068020
    7307             if (oSz < AES_BLOCK_SIZE * 4) {
    7308                 AesCcmCtrInc4(B, lenSz);
    7309             }
     8021            AesCcmCtrInc4(B, lenSz);
    73108022        }
    73118023    }
     
    73608072         * Unfortunately, you need the decrypted data to calculate the
    73618073         * check value. */
    7362         XMEMSET(out, 0, inSz);
     8074        #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) && \
     8075            defined(ACVP_VECTOR_TESTING)
     8076            WOLFSSL_MSG("Preserve output for vector responses");
     8077        #else
     8078            if (inSz > 0)
     8079                XMEMSET(out, 0, inSz);
     8080        #endif
    73638081        result = AES_CCM_AUTH_E;
    73648082    }
     
    74768194    XMEMSET(&aes->ctx, 0, sizeof(aes->ctx));
    74778195#endif
     8196#if defined(WOLFSSL_IMXRT_DCP)
     8197    DCPAesInit(aes);
     8198#endif
     8199
     8200
    74788201#ifdef HAVE_AESGCM
    74798202#ifdef OPENSSL_EXTRA
     
    74968219
    74978220    if (ret == 0)
    7498         ret  = wc_AesInit(aes, heap, devId);
     8221        ret = wc_AesInit(aes, heap, devId);
    74998222    if (ret == 0) {
    75008223        XMEMCPY(aes->id, id, len);
    75018224        aes->idLen = len;
     8225        aes->labelLen = 0;
     8226    }
     8227
     8228    return ret;
     8229}
     8230
     8231int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
     8232{
     8233    int ret = 0;
     8234    int labelLen = 0;
     8235
     8236    if (aes == NULL || label == NULL)
     8237        ret = BAD_FUNC_ARG;
     8238    if (ret == 0) {
     8239        labelLen = (int)XSTRLEN(label);
     8240        if (labelLen == 0 || labelLen > AES_MAX_LABEL_LEN)
     8241            ret = BUFFER_E;
     8242    }
     8243
     8244    if (ret == 0)
     8245        ret = wc_AesInit(aes, heap, devId);
     8246    if (ret == 0) {
     8247        XMEMCPY(aes->label, label, labelLen);
     8248        aes->labelLen = labelLen;
     8249        aes->idLen = 0;
    75028250    }
    75038251
     
    75328280    ForceZero((byte*)aes->devKey, AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE);
    75338281#endif
     8282#if defined(WOLFSSL_IMXRT_DCP)
     8283    DCPAesFree(aes);
     8284#endif
    75348285}
    75358286
     
    75908341        return BAD_FUNC_ARG;
    75918342
    7592         return AES_ECB_encrypt(aes, in, out, sz);
     8343    return AES_ECB_encrypt(aes, in, out, sz);
    75938344}
    75948345
     
    75998350        return BAD_FUNC_ARG;
    76008351
    7601         return AES_ECB_decrypt(aes, in, out, sz);
     8352    return AES_ECB_decrypt(aes, in, out, sz);
    76028353}
    76038354
     
    76118362    if ((in == NULL) || (out == NULL) || (aes == NULL))
    76128363      return BAD_FUNC_ARG;
    7613     while (blocks>0) {
     8364#ifdef WOLFSSL_IMXRT_DCP
     8365    if (aes->keylen == 16)
     8366        return DCPAesEcbEncrypt(aes, out, in, sz);
     8367#endif
     8368    while (blocks > 0) {
    76148369      wc_AesEncryptDirect(aes, out, in);
    76158370      out += AES_BLOCK_SIZE;
     
    76288383    if ((in == NULL) || (out == NULL) || (aes == NULL))
    76298384      return BAD_FUNC_ARG;
    7630     while (blocks>0) {
     8385#ifdef WOLFSSL_IMXRT_DCP
     8386    if (aes->keylen == 16)
     8387        return DCPAesEcbDecrypt(aes, out, in, sz);
     8388#endif
     8389    while (blocks > 0) {
    76318390      wc_AesDecryptDirect(aes, out, in);
    76328391      out += AES_BLOCK_SIZE;
     
    79108669
    79118670        /* MSB + XOR */
     8671    #ifdef BIG_ENDIAN_ORDER
     8672        ByteReverseWords(aes->tmp, aes->tmp, AES_BLOCK_SIZE);
     8673    #endif
    79128674        out[0] = aes->tmp[0] ^ in[0];
    79138675        if (dir == AES_ENCRYPTION) {
     
    81418903                  byte* out, word32 outSz, const byte* iv)
    81428904{
    8143     Aes aes;
     8905#ifdef WOLFSSL_SMALL_STACK
     8906    Aes *aes = NULL;
     8907#else
     8908    Aes aes[1];
     8909#endif
     8910    int aes_inited = 0;
    81448911    byte* r;
    81458912    word32 i;
     
    81578924    if (inSz % KEYWRAP_BLOCK_SIZE != 0)
    81588925        return BAD_FUNC_ARG;
     8926
     8927#ifdef WOLFSSL_SMALL_STACK
     8928    if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
     8929                              DYNAMIC_TYPE_AES)) == NULL)
     8930        return MEMORY_E;
     8931#endif
    81598932
    81608933    /* user IV is optional */
     
    81698942    XMEMSET(t, 0, sizeof(t));
    81708943
    8171     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
     8944    ret = wc_AesInit(aes, NULL, INVALID_DEVID);
    81728945    if (ret != 0)
    8173         return ret;
    8174 
    8175     ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION);
     8946        goto out;
     8947    else
     8948        aes_inited = 1;
     8949
     8950    ret = wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
    81768951    if (ret != 0)
    8177         return ret;
     8952        goto out;
    81788953
    81798954    for (j = 0; j <= 5; j++) {
     
    81838958            XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
    81848959
    8185             wc_AesEncryptDirect(&aes, tmp, tmp);
     8960            wc_AesEncryptDirect(aes, tmp, tmp);
    81868961
    81878962            /* calculate new A */
     
    81998974    XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE);
    82008975
    8201     wc_AesFree(&aes);
    8202 
    8203     return inSz + KEYWRAP_BLOCK_SIZE;
     8976    ret = 0;
     8977
     8978  out:
     8979
     8980    if (aes_inited)
     8981        wc_AesFree(aes);
     8982#ifdef WOLFSSL_SMALL_STACK
     8983    if (aes)
     8984        XFREE(aes, NULL, DYNAMIC_TYPE_AES);
     8985#endif
     8986
     8987    if (ret != 0)
     8988        return ret;
     8989    else
     8990        return inSz + KEYWRAP_BLOCK_SIZE;
    82048991}
    82058992
     
    82078994                    byte* out, word32 outSz, const byte* iv)
    82088995{
    8209     Aes aes;
     8996#ifdef WOLFSSL_SMALL_STACK
     8997    Aes *aes = NULL;
     8998#else
     8999    Aes aes[1];
     9000#endif
     9001    int aes_inited = 0;
    82109002    byte* r;
    82119003    word32 i, n;
     
    82299021    if (inSz % KEYWRAP_BLOCK_SIZE != 0)
    82309022        return BAD_FUNC_ARG;
     9023
     9024#ifdef WOLFSSL_SMALL_STACK
     9025    if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
     9026                              DYNAMIC_TYPE_AES)) == NULL)
     9027        return MEMORY_E;
     9028#endif
    82319029
    82329030    /* user IV optional */
     
    82429040    XMEMSET(t, 0, sizeof(t));
    82439041
    8244     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
     9042    ret = wc_AesInit(aes, NULL, INVALID_DEVID);
    82459043    if (ret != 0)
    8246         return ret;
    8247 
    8248     ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_DECRYPTION);
     9044        goto out;
     9045    else
     9046        aes_inited = 1;
     9047
     9048    ret = wc_AesSetKey(aes, key, keySz, NULL, AES_DECRYPTION);
    82499049    if (ret != 0)
    8250         return ret;
     9050        goto out;
    82519051
    82529052    /* initialize counter to 6n */
     
    82649064            r = out + ((i - 1) * KEYWRAP_BLOCK_SIZE);
    82659065            XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
    8266             wc_AesDecryptDirect(&aes, tmp, tmp);
     9066            wc_AesDecryptDirect(aes, tmp, tmp);
    82679067
    82689068            /* save R[i] */
     
    82719071    }
    82729072
    8273     wc_AesFree(&aes);
    8274 
    82759073    /* verify IV */
    8276     if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0)
    8277         return BAD_KEYWRAP_IV_E;
    8278 
    8279     return inSz - KEYWRAP_BLOCK_SIZE;
     9074    if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0) {
     9075        ret = BAD_KEYWRAP_IV_E;
     9076        goto out;
     9077    }
     9078
     9079  out:
     9080
     9081    if (aes_inited)
     9082        wc_AesFree(aes);
     9083#ifdef WOLFSSL_SMALL_STACK
     9084    if (aes)
     9085        XFREE(aes, NULL, DYNAMIC_TYPE_AES);
     9086#endif
     9087
     9088    if (ret != 0)
     9089        return ret;
     9090    else
     9091        return inSz - KEYWRAP_BLOCK_SIZE;
    82809092}
    82819093
Note: See TracChangeset for help on using the changeset viewer.