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/evp.c

    r457 r464  
    2020 */
    2121
     22
     23#ifdef HAVE_CONFIG_H
     24    #include <config.h>
     25#endif
     26
     27#include <wolfssl/wolfcrypt/settings.h>
     28
    2229#if !defined(WOLFSSL_EVP_INCLUDED)
    2330    #ifndef WOLFSSL_IGNORE_FILE_WARN
     
    2734#else
    2835
    29 #ifdef HAVE_CONFIG_H
    30     #include <config.h>
    31 #endif
    32 
    33 #include <wolfssl/wolfcrypt/settings.h>
     36#if defined(OPENSSL_EXTRA)
     37
     38#if !defined(HAVE_PKCS7) && \
     39      ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
     40       (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST))
     41    #include <wolfssl/wolfcrypt/aes.h>
     42#endif
     43
    3444
    3545#include <wolfssl/openssl/ecdsa.h>
    3646#include <wolfssl/openssl/evp.h>
    37 
    38 #if defined(OPENSSL_EXTRA)
    3947
    4048#ifndef NO_AES
    4149    #ifdef HAVE_AES_CBC
    4250    #ifdef WOLFSSL_AES_128
    43         static char *EVP_AES_128_CBC = NULL;
     51        static const char EVP_AES_128_CBC[] = "AES-128-CBC";
    4452    #endif
    4553    #ifdef WOLFSSL_AES_192
    46         static char *EVP_AES_192_CBC = NULL;
     54        static const char EVP_AES_192_CBC[] = "AES-192-CBC";
    4755    #endif
    4856    #ifdef WOLFSSL_AES_256
    49         static char *EVP_AES_256_CBC = NULL;
     57        static const char EVP_AES_256_CBC[] = "AES-256-CBC";
    5058    #endif
    5159    #endif /* HAVE_AES_CBC */
     
    5361    #ifdef WOLFSSL_AES_OFB
    5462    #ifdef WOLFSSL_AES_128
    55         static char *EVP_AES_128_OFB = NULL;
     63        static const char EVP_AES_128_OFB[] = "AES-128-OFB";
    5664    #endif
    5765    #ifdef WOLFSSL_AES_192
    58         static char *EVP_AES_192_OFB = NULL;
     66        static const char EVP_AES_192_OFB[] = "AES-192-OFB";
    5967    #endif
    6068    #ifdef WOLFSSL_AES_256
    61         static char *EVP_AES_256_OFB = NULL;
     69        static const char EVP_AES_256_OFB[] = "AES-256-OFB";
    6270    #endif
    6371    #endif /* WOLFSSL_AES_OFB */
     
    6573    #ifdef WOLFSSL_AES_XTS
    6674    #ifdef WOLFSSL_AES_128
    67         static char *EVP_AES_128_XTS = NULL;
     75        static const char EVP_AES_128_XTS[] = "AES-128-XTS";
    6876    #endif
    6977    #ifdef WOLFSSL_AES_256
    70         static char *EVP_AES_256_XTS = NULL;
     78        static const char EVP_AES_256_XTS[] = "AES-256-XTS";
    7179    #endif
    7280    #endif /* WOLFSSL_AES_XTS */
     
    7482    #ifdef WOLFSSL_AES_CFB
    7583    #ifdef WOLFSSL_AES_128
    76         static char *EVP_AES_128_CFB1 = NULL;
     84        static const char EVP_AES_128_CFB1[] = "AES-128-CFB1";
    7785    #endif
    7886    #ifdef WOLFSSL_AES_192
    79         static char *EVP_AES_192_CFB1 = NULL;
     87        static const char EVP_AES_192_CFB1[] = "AES-192-CFB1";
    8088    #endif
    8189    #ifdef WOLFSSL_AES_256
    82         static char *EVP_AES_256_CFB1 = NULL;
     90        static const char EVP_AES_256_CFB1[] = "AES-256-CFB1";
    8391    #endif
    8492
    8593    #ifdef WOLFSSL_AES_128
    86         static char *EVP_AES_128_CFB8 = NULL;
     94        static const char EVP_AES_128_CFB8[] = "AES-128-CFB8";
    8795    #endif
    8896    #ifdef WOLFSSL_AES_192
    89         static char *EVP_AES_192_CFB8 = NULL;
     97        static const char EVP_AES_192_CFB8[] = "AES-192-CFB8";
    9098    #endif
    9199    #ifdef WOLFSSL_AES_256
    92         static char *EVP_AES_256_CFB8 = NULL;
     100        static const char EVP_AES_256_CFB8[] = "AES-256-CFB8";
    93101    #endif
    94102
    95103    #ifdef WOLFSSL_AES_128
    96         static char *EVP_AES_128_CFB128 = NULL;
     104        static const char EVP_AES_128_CFB128[] = "AES-128-CFB128";
    97105    #endif
    98106    #ifdef WOLFSSL_AES_192
    99         static char *EVP_AES_192_CFB128 = NULL;
     107        static const char EVP_AES_192_CFB128[] = "AES-192-CFB128";
    100108    #endif
    101109    #ifdef WOLFSSL_AES_256
    102         static char *EVP_AES_256_CFB128 = NULL;
     110        static const char EVP_AES_256_CFB128[] = "AES-256-CFB128";
    103111    #endif
    104112    #endif /* WOLFSSL_AES_CFB */
     
    106114    #ifdef HAVE_AESGCM
    107115        #ifdef WOLFSSL_AES_128
    108             static char *EVP_AES_128_GCM = NULL;
     116            static const char EVP_AES_128_GCM[] = "AES-128-GCM";
    109117        #endif
    110118        #ifdef WOLFSSL_AES_192
    111             static char *EVP_AES_192_GCM = NULL;
     119            static const char EVP_AES_192_GCM[] = "AES-192-GCM";
    112120        #endif
    113121        #ifdef WOLFSSL_AES_256
    114             static char *EVP_AES_256_GCM = NULL;
     122            static const char EVP_AES_256_GCM[] = "AES-256-GCM";
    115123        #endif
    116124    #endif /* HAVE_AESGCM */
     125
     126    #ifdef WOLFSSL_AES_COUNTER
    117127    #ifdef WOLFSSL_AES_128
    118         static char *EVP_AES_128_CTR = NULL;
     128        static const char EVP_AES_128_CTR[] = "AES-128-CTR";
    119129    #endif
    120130    #ifdef WOLFSSL_AES_192
    121         static char *EVP_AES_192_CTR = NULL;
     131        static const char EVP_AES_192_CTR[] = "AES-192-CTR";
    122132    #endif
    123133    #ifdef WOLFSSL_AES_256
    124         static char *EVP_AES_256_CTR = NULL;
    125     #endif
    126 
     134        static const char EVP_AES_256_CTR[] = "AES-256-CTR";
     135    #endif
     136    #endif
     137
     138    #ifdef HAVE_AES_ECB
    127139    #ifdef WOLFSSL_AES_128
    128         static char *EVP_AES_128_ECB = NULL;
     140        static const char EVP_AES_128_ECB[] = "AES-128-ECB";
    129141    #endif
    130142    #ifdef WOLFSSL_AES_192
    131         static char *EVP_AES_192_ECB = NULL;
     143        static const char EVP_AES_192_ECB[] = "AES-192-ECB";
    132144    #endif
    133145    #ifdef WOLFSSL_AES_256
    134         static char *EVP_AES_256_ECB = NULL;
     146        static const char EVP_AES_256_ECB[] = "AES-256-ECB";
     147    #endif
    135148    #endif
    136149        #define      EVP_AES_SIZE 11
     
    141154
    142155#ifndef NO_DES3
    143     static char *EVP_DES_CBC = NULL;
    144     static char *EVP_DES_ECB = NULL;
    145 
    146     static char *EVP_DES_EDE3_CBC = NULL;
    147     static char *EVP_DES_EDE3_ECB = NULL;
     156    static const char EVP_DES_CBC[] = "DES-CBC";
     157    static const char EVP_DES_ECB[] = "DES-ECB";
     158
     159    static const char EVP_DES_EDE3_CBC[] = "DES-EDE3-CBC";
     160    static const char EVP_DES_EDE3_ECB[] = "DES-EDE3-ECB";
    148161
    149162    #define EVP_DES_SIZE 7
     
    152165
    153166#ifdef HAVE_IDEA
    154     static char *EVP_IDEA_CBC;
     167    static const char EVP_IDEA_CBC[] = "IDEA-CBC";
    155168    #define EVP_IDEA_SIZE 8
    156169#endif
     170
     171#ifndef NO_RC4
     172    static const char EVP_ARC4[] = "ARC4";
     173    #define EVP_ARC4_SIZE 4
     174#endif
     175
     176static const char EVP_NULL[] = "NULL";
     177#define EVP_NULL_SIZE 4
     178
    157179
    158180static unsigned int cipherType(const WOLFSSL_EVP_CIPHER *cipher);
     
    200222  #endif
    201223  #if defined(WOLFSSL_AES_XTS)
    202       case AES_128_XTS_TYPE: return 16;
    203       case AES_256_XTS_TYPE: return 32;
     224      /* Two keys for XTS. */
     225      case AES_128_XTS_TYPE: return 16 * 2;
     226      case AES_256_XTS_TYPE: return 32 * 2;
    204227  #endif
    205228  #if defined(HAVE_AESGCM)
     
    418441            break;
    419442    #endif
    420     #if defined(HAVE_AESGCM)
    421         case AES_128_GCM_TYPE:
    422         case AES_192_GCM_TYPE:
    423         case AES_256_GCM_TYPE:
    424             if (ctx->enc) {
    425                 if (out){
    426                     /* encrypt confidential data*/
    427                     ret = wc_AesGcmEncrypt(&ctx->cipher.aes, out, in, inl,
    428                               ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
    429                               NULL, 0);
    430                 }
    431                 else {
    432                     /* authenticated, non-confidential data */
    433                     ret = wc_AesGcmEncrypt(&ctx->cipher.aes, NULL, NULL, 0,
    434                               ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
    435                               in, inl);
    436                     /* Reset partial authTag error for AAD*/
    437                     if (ret == AES_GCM_AUTH_E)
    438                         ret = 0;
    439                 }
    440             }
    441             else {
    442                 if (out){
    443                     /* decrypt confidential data*/
    444                     ret = wc_AesGcmDecrypt(&ctx->cipher.aes, out, in, inl,
    445                               ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
    446                               NULL, 0);
    447                 }
    448                 else {
    449                     /* authenticated, non-confidential data*/
    450                     ret = wc_AesGcmDecrypt(&ctx->cipher.aes, NULL, NULL, 0,
    451                               ctx->iv, ctx->ivSz,
    452                               ctx->authTag, ctx->authTagSz,
    453                               in, inl);
    454                     /* Reset partial authTag error for AAD*/
    455                     if (ret == AES_GCM_AUTH_E)
    456                         ret = 0;
    457                 }
    458             }
    459             break;
    460     #endif
    461443    #if defined(WOLFSSL_AES_COUNTER)
    462444        case AES_128_CTR_TYPE:
     
    572554
    573555#if defined(HAVE_AESGCM)
     556static int wolfSSL_EVP_CipherUpdate_GCM_AAD(WOLFSSL_EVP_CIPHER_CTX *ctx,
     557        const unsigned char *in, int inl) {
     558    if (in && inl > 0) {
     559        byte* tmp = (byte*)XREALLOC(ctx->gcmAuthIn,
     560                ctx->gcmAuthInSz + inl, NULL, DYNAMIC_TYPE_OPENSSL);
     561        if (tmp) {
     562            ctx->gcmAuthIn = tmp;
     563            XMEMCPY(ctx->gcmAuthIn + ctx->gcmAuthInSz, in, inl);
     564            ctx->gcmAuthInSz += inl;
     565        }
     566        else {
     567            WOLFSSL_MSG("realloc error");
     568            return MEMORY_E;
     569        }
     570    }
     571    return 0;
     572}
     573
    574574static int wolfSSL_EVP_CipherUpdate_GCM(WOLFSSL_EVP_CIPHER_CTX *ctx,
    575575                                   unsigned char *out, int *outl,
    576576                                   const unsigned char *in, int inl)
    577577{
    578     /* process blocks */
    579     if (evpCipherBlock(ctx, out, in, inl) == 0)
     578    int ret = 0;
     579
     580    *outl = inl;
     581    if (out) {
     582        /* Buffer input for one-shot API */
     583        if (inl > 0) {
     584            byte* tmp;
     585            tmp = (byte*)XREALLOC(ctx->gcmBuffer,
     586                    ctx->gcmBufferLen + inl, NULL,
     587                    DYNAMIC_TYPE_OPENSSL);
     588            if (tmp) {
     589                XMEMCPY(tmp + ctx->gcmBufferLen, in, inl);
     590                ctx->gcmBufferLen += inl;
     591                ctx->gcmBuffer = tmp;
     592                *outl = 0;
     593            }
     594            else {
     595                ret = MEMORY_E;
     596            }
     597        }
     598    }
     599    else {
     600        ret = wolfSSL_EVP_CipherUpdate_GCM_AAD(ctx, in, inl);
     601    }
     602
     603    if (ret != 0) {
     604        *outl = 0;
    580605        return WOLFSSL_FAILURE;
    581     *outl = inl;
     606    }
     607
    582608    return WOLFSSL_SUCCESS;
    583609}
     
    599625
    600626    *outl = 0;
    601     if (inl == 0) {
    602         return WOLFSSL_SUCCESS;
    603     }
    604627
    605628#if !defined(NO_AES) && defined(HAVE_AESGCM)
     
    620643    }
    621644
    622 
     645    /* if(inl == 0)wolfSSL_EVP_CipherUpdate_GCM to get tag */
     646    if (inl == 0) {
     647        return WOLFSSL_SUCCESS;
     648    }
    623649    if (ctx->bufUsed > 0) { /* concatenate them if there is anything */
    624650        fill = fillBuff(ctx, in, inl);
     
    740766
    741767    WOLFSSL_ENTER("wolfSSL_EVP_CipherFinal");
    742 
     768    switch (ctx->cipherType) {
    743769#if !defined(NO_AES) && defined(HAVE_AESGCM)
    744         switch (ctx->cipherType) {
    745             case AES_128_GCM_TYPE:
    746             case AES_192_GCM_TYPE:
    747             case AES_256_GCM_TYPE:
     770        case AES_128_GCM_TYPE:
     771        case AES_192_GCM_TYPE:
     772        case AES_256_GCM_TYPE:
     773            if ((ctx->gcmBuffer && ctx->gcmBufferLen > 0)
     774             || (ctx->gcmBufferLen == 0)) {
     775                if (ctx->enc)
     776                    ret = wc_AesGcmEncrypt(&ctx->cipher.aes, out,
     777                            ctx->gcmBuffer, ctx->gcmBufferLen,
     778                            ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
     779                            ctx->gcmAuthIn, ctx->gcmAuthInSz);
     780                else
     781                    ret = wc_AesGcmDecrypt(&ctx->cipher.aes, out,
     782                            ctx->gcmBuffer, ctx->gcmBufferLen,
     783                            ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
     784                            ctx->gcmAuthIn, ctx->gcmAuthInSz);
     785
     786                if (ret == 0) {
     787                    ret = WOLFSSL_SUCCESS;
     788                    *outl = ctx->gcmBufferLen;
     789                }
     790                else {
     791                    ret = WOLFSSL_FAILURE;
     792                    *outl = 0;
     793                }
     794
     795                XFREE(ctx->gcmBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
     796                ctx->gcmBuffer = NULL;
     797                ctx->gcmBufferLen = 0;
     798            }
     799            else {
    748800                *outl = 0;
    749                 /* Clear IV, since IV reuse is not recommended for AES GCM. */
    750                 XMEMSET(ctx->iv, 0, AES_BLOCK_SIZE);
    751                 return WOLFSSL_SUCCESS;
    752             default:
    753                 /* fall-through */
    754                 break;
    755         }
     801            }
     802            /* Clear IV, since IV reuse is not recommended for AES GCM. */
     803            XMEMSET(ctx->iv, 0, AES_BLOCK_SIZE);
     804            break;
    756805#endif /* !NO_AES && HAVE_AESGCM */
    757 
    758     if (!out)
    759         return WOLFSSL_FAILURE;
    760 
    761     if (ctx->flags & WOLFSSL_EVP_CIPH_NO_PADDING) {
    762         if (ctx->bufUsed != 0) return WOLFSSL_FAILURE;
    763         *outl = 0;
    764     }
    765     else if (ctx->enc) {
    766         if (ctx->block_size == 1) {
    767             *outl = 0;
    768         }
    769         else if ((ctx->bufUsed >= 0) && (ctx->block_size != 1)) {
    770             padBlock(ctx);
    771             PRINT_BUF(ctx->buf, ctx->block_size);
    772             if (evpCipherBlock(ctx, out, ctx->buf, ctx->block_size) == 0) {
    773                 WOLFSSL_MSG("Final Cipher Block failed");
    774                 ret = WOLFSSL_FAILURE;
     806        default:
     807            if (!out)
     808                return WOLFSSL_FAILURE;
     809
     810            if (ctx->flags & WOLFSSL_EVP_CIPH_NO_PADDING) {
     811                if (ctx->bufUsed != 0) return WOLFSSL_FAILURE;
     812                *outl = 0;
     813            }
     814            else if (ctx->enc) {
     815                if (ctx->block_size == 1) {
     816                    *outl = 0;
     817                }
     818                else if ((ctx->bufUsed >= 0) && (ctx->block_size != 1)) {
     819                    padBlock(ctx);
     820                    PRINT_BUF(ctx->buf, ctx->block_size);
     821                    if (evpCipherBlock(ctx, out, ctx->buf, ctx->block_size) == 0) {
     822                        WOLFSSL_MSG("Final Cipher Block failed");
     823                        ret = WOLFSSL_FAILURE;
     824                    }
     825                    else {
     826                        PRINT_BUF(out, ctx->block_size);
     827                        *outl = ctx->block_size;
     828                    }
     829                }
    775830            }
    776831            else {
    777                 PRINT_BUF(out, ctx->block_size);
    778                 *outl = ctx->block_size;
    779             }
    780         }
    781     }
    782     else {
    783         if (ctx->block_size == 1) {
    784             *outl = 0;
    785         }
    786         else if ((ctx->bufUsed % ctx->block_size) != 0) {
    787             *outl = 0;
    788             /* not enough padding for decrypt */
    789             WOLFSSL_MSG("Final Cipher Block not enough padding");
    790             ret = WOLFSSL_FAILURE;
    791         }
    792         else if (ctx->lastUsed) {
    793             PRINT_BUF(ctx->lastBlock, ctx->block_size);
    794             if ((fl = checkPad(ctx, ctx->lastBlock)) >= 0) {
    795                 XMEMCPY(out, ctx->lastBlock, fl);
    796                 *outl = fl;
    797                 if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
    798                     /* return error in cases where the block length is incorrect */
    799                     WOLFSSL_MSG("Final Cipher Block bad length");
     832                if (ctx->block_size == 1) {
     833                    *outl = 0;
     834                }
     835                else if ((ctx->bufUsed % ctx->block_size) != 0) {
     836                    *outl = 0;
     837                    /* not enough padding for decrypt */
     838                    WOLFSSL_MSG("Final Cipher Block not enough padding");
    800839                    ret = WOLFSSL_FAILURE;
    801840                }
    802             }
    803             else {
    804                 ret = WOLFSSL_FAILURE;
    805             }
    806         }
    807         else if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
    808             /* return error in cases where the block length is incorrect */
    809             ret = WOLFSSL_FAILURE;
    810         }
    811     }
     841                else if (ctx->lastUsed) {
     842                    PRINT_BUF(ctx->lastBlock, ctx->block_size);
     843                    if ((fl = checkPad(ctx, ctx->lastBlock)) >= 0) {
     844                        XMEMCPY(out, ctx->lastBlock, fl);
     845                        *outl = fl;
     846                        if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
     847                            /* return error in cases where the block length is
     848                             * incorrect */
     849                            WOLFSSL_MSG("Final Cipher Block bad length");
     850                            ret = WOLFSSL_FAILURE;
     851                        }
     852                    }
     853                    else {
     854                        ret = WOLFSSL_FAILURE;
     855                    }
     856                }
     857                else if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
     858                    /* return error in cases where the block length is
     859                     * incorrect */
     860                    ret = WOLFSSL_FAILURE;
     861                }
     862            }
     863            break;
     864    }
     865
    812866    if (ret == WOLFSSL_SUCCESS) {
    813867        /* reset cipher state after final */
    814         wolfSSL_EVP_CipherInit(ctx, NULL, NULL, NULL, -1);
     868        ret = wolfSSL_EVP_CipherInit(ctx, NULL, NULL, NULL, -1);
    815869    }
    816870    return ret;
     
    867921#endif
    868922
    869 
    870923int wolfSSL_EVP_CIPHER_CTX_block_size(const WOLFSSL_EVP_CIPHER_CTX *ctx)
    871924{
     
    931984    if (cipher == NULL) return 0; /* dummy for #ifdef */
    932985#ifndef NO_DES3
    933     else if (EVP_DES_CBC && XSTRNCMP(cipher, EVP_DES_CBC, EVP_DES_SIZE) == 0)
     986    else if (XSTRNCMP(cipher, EVP_DES_CBC, EVP_DES_SIZE) == 0)
    934987        return DES_CBC_TYPE;
    935     else if (EVP_DES_EDE3_CBC && XSTRNCMP(cipher, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)
     988    else if (XSTRNCMP(cipher, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)
    936989        return DES_EDE3_CBC_TYPE;
    937990#if !defined(NO_DES3)
    938     else if (EVP_DES_ECB && XSTRNCMP(cipher, EVP_DES_ECB, EVP_DES_SIZE) == 0)
     991    else if (XSTRNCMP(cipher, EVP_DES_ECB, EVP_DES_SIZE) == 0)
    939992        return DES_ECB_TYPE;
    940     else if (EVP_DES_EDE3_ECB && XSTRNCMP(cipher, EVP_DES_EDE3_ECB, EVP_DES_EDE3_SIZE) == 0)
     993    else if (XSTRNCMP(cipher, EVP_DES_EDE3_ECB, EVP_DES_EDE3_SIZE) == 0)
    941994        return DES_EDE3_ECB_TYPE;
    942995#endif /* NO_DES3 && HAVE_AES_ECB */
     
    945998#if defined(HAVE_AES_CBC)
    946999    #ifdef WOLFSSL_AES_128
    947     else if (EVP_AES_128_CBC && XSTRNCMP(cipher, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)
     1000    else if (XSTRNCMP(cipher, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)
    9481001        return AES_128_CBC_TYPE;
    9491002    #endif
    9501003    #ifdef WOLFSSL_AES_192
    951     else if (EVP_AES_192_CBC && XSTRNCMP(cipher, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)
     1004    else if (XSTRNCMP(cipher, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)
    9521005        return AES_192_CBC_TYPE;
    9531006    #endif
    9541007    #ifdef WOLFSSL_AES_256
    955     else if (EVP_AES_256_CBC && XSTRNCMP(cipher, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)
     1008    else if (XSTRNCMP(cipher, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)
    9561009        return AES_256_CBC_TYPE;
    9571010    #endif
     
    9591012#if defined(HAVE_AESGCM)
    9601013    #ifdef WOLFSSL_AES_128
    961     else if (EVP_AES_128_GCM && XSTRNCMP(cipher, EVP_AES_128_GCM, EVP_AES_SIZE) == 0)
     1014    else if (XSTRNCMP(cipher, EVP_AES_128_GCM, EVP_AES_SIZE) == 0)
    9621015        return AES_128_GCM_TYPE;
    9631016    #endif
    9641017    #ifdef WOLFSSL_AES_192
    965     else if (EVP_AES_192_GCM && XSTRNCMP(cipher, EVP_AES_192_GCM, EVP_AES_SIZE) == 0)
     1018    else if (XSTRNCMP(cipher, EVP_AES_192_GCM, EVP_AES_SIZE) == 0)
    9661019        return AES_192_GCM_TYPE;
    9671020    #endif
    9681021    #ifdef WOLFSSL_AES_256
    969     else if (EVP_AES_256_GCM && XSTRNCMP(cipher, EVP_AES_256_GCM, EVP_AES_SIZE) == 0)
     1022    else if (XSTRNCMP(cipher, EVP_AES_256_GCM, EVP_AES_SIZE) == 0)
    9701023        return AES_256_GCM_TYPE;
    9711024    #endif
     
    9731026#if defined(WOLFSSL_AES_COUNTER)
    9741027    #ifdef WOLFSSL_AES_128
    975     else if (EVP_AES_128_CTR && XSTRNCMP(cipher, EVP_AES_128_CTR, EVP_AES_SIZE) == 0)
     1028    else if (XSTRNCMP(cipher, EVP_AES_128_CTR, EVP_AES_SIZE) == 0)
    9761029        return AES_128_CTR_TYPE;
    9771030    #endif
    9781031    #ifdef WOLFSSL_AES_192
    979     else if (EVP_AES_192_CTR && XSTRNCMP(cipher, EVP_AES_192_CTR, EVP_AES_SIZE) == 0)
     1032    else if (XSTRNCMP(cipher, EVP_AES_192_CTR, EVP_AES_SIZE) == 0)
    9801033        return AES_192_CTR_TYPE;
    9811034    #endif
    9821035    #ifdef WOLFSSL_AES_256
    983     else if (EVP_AES_256_CTR && XSTRNCMP(cipher, EVP_AES_256_CTR, EVP_AES_SIZE) == 0)
     1036    else if (XSTRNCMP(cipher, EVP_AES_256_CTR, EVP_AES_SIZE) == 0)
    9841037        return AES_256_CTR_TYPE;
    9851038    #endif
     
    9871040#if defined(HAVE_AES_ECB)
    9881041    #ifdef WOLFSSL_AES_128
    989     else if (EVP_AES_128_ECB && XSTRNCMP(cipher, EVP_AES_128_ECB, EVP_AES_SIZE) == 0)
     1042    else if (XSTRNCMP(cipher, EVP_AES_128_ECB, EVP_AES_SIZE) == 0)
    9901043        return AES_128_ECB_TYPE;
    9911044    #endif
    9921045    #ifdef WOLFSSL_AES_192
    993     else if (EVP_AES_192_ECB && XSTRNCMP(cipher, EVP_AES_192_ECB, EVP_AES_SIZE) == 0)
     1046    else if (XSTRNCMP(cipher, EVP_AES_192_ECB, EVP_AES_SIZE) == 0)
    9941047        return AES_192_ECB_TYPE;
    9951048    #endif
    9961049    #ifdef WOLFSSL_AES_256
    997     else if (EVP_AES_256_ECB && XSTRNCMP(cipher, EVP_AES_256_ECB, EVP_AES_SIZE) == 0)
     1050    else if (XSTRNCMP(cipher, EVP_AES_256_ECB, EVP_AES_SIZE) == 0)
    9981051        return AES_256_ECB_TYPE;
    9991052    #endif
     
    10011054#if defined(WOLFSSL_AES_XTS)
    10021055    #ifdef WOLFSSL_AES_128
    1003     else if (EVP_AES_128_XTS && XSTRNCMP(cipher, EVP_AES_128_XTS, EVP_AES_SIZE) == 0)
     1056    else if (XSTRNCMP(cipher, EVP_AES_128_XTS, EVP_AES_SIZE) == 0)
    10041057        return AES_128_XTS_TYPE;
    10051058    #endif
    10061059    #ifdef WOLFSSL_AES_256
    1007     else if (EVP_AES_256_XTS && XSTRNCMP(cipher, EVP_AES_256_XTS, EVP_AES_SIZE) == 0)
     1060    else if (XSTRNCMP(cipher, EVP_AES_256_XTS, EVP_AES_SIZE) == 0)
    10081061        return AES_256_XTS_TYPE;
    10091062    #endif
     
    10111064#if defined(WOLFSSL_AES_CFB)
    10121065    #ifdef WOLFSSL_AES_128
    1013     else if (EVP_AES_128_CFB1 && XSTRNCMP(cipher, EVP_AES_128_CFB1, EVP_AESCFB_SIZE) == 0)
     1066    else if (XSTRNCMP(cipher, EVP_AES_128_CFB1, EVP_AESCFB_SIZE) == 0)
    10141067        return AES_128_CFB1_TYPE;
    10151068    #endif
    10161069    #ifdef WOLFSSL_AES_192
    1017     else if (EVP_AES_192_CFB1 && XSTRNCMP(cipher, EVP_AES_192_CFB1, EVP_AESCFB_SIZE) == 0)
     1070    else if (XSTRNCMP(cipher, EVP_AES_192_CFB1, EVP_AESCFB_SIZE) == 0)
    10181071        return AES_192_CFB1_TYPE;
    10191072    #endif
    10201073    #ifdef WOLFSSL_AES_256
    1021     else if (EVP_AES_256_CFB1 && XSTRNCMP(cipher, EVP_AES_256_CFB1, EVP_AESCFB_SIZE) == 0)
     1074    else if (XSTRNCMP(cipher, EVP_AES_256_CFB1, EVP_AESCFB_SIZE) == 0)
    10221075        return AES_256_CFB1_TYPE;
    10231076    #endif
    10241077    #ifdef WOLFSSL_AES_128
    1025     else if (EVP_AES_128_CFB8 && XSTRNCMP(cipher, EVP_AES_128_CFB8, EVP_AESCFB_SIZE) == 0)
     1078    else if (XSTRNCMP(cipher, EVP_AES_128_CFB8, EVP_AESCFB_SIZE) == 0)
    10261079        return AES_128_CFB8_TYPE;
    10271080    #endif
    10281081    #ifdef WOLFSSL_AES_192
    1029     else if (EVP_AES_192_CFB8 && XSTRNCMP(cipher, EVP_AES_192_CFB8, EVP_AESCFB_SIZE) == 0)
     1082    else if (XSTRNCMP(cipher, EVP_AES_192_CFB8, EVP_AESCFB_SIZE) == 0)
    10301083        return AES_192_CFB8_TYPE;
    10311084    #endif
    10321085    #ifdef WOLFSSL_AES_256
    1033     else if (EVP_AES_256_CFB8 && XSTRNCMP(cipher, EVP_AES_256_CFB8, EVP_AESCFB_SIZE) == 0)
     1086    else if (XSTRNCMP(cipher, EVP_AES_256_CFB8, EVP_AESCFB_SIZE) == 0)
    10341087        return AES_256_CFB8_TYPE;
    10351088    #endif
    10361089    #ifdef WOLFSSL_AES_128
    1037     else if (EVP_AES_128_CFB128 && XSTRNCMP(cipher, EVP_AES_128_CFB128, EVP_AESCFB_SIZE) == 0)
     1090    else if (XSTRNCMP(cipher, EVP_AES_128_CFB128, EVP_AESCFB_SIZE) == 0)
    10381091        return AES_128_CFB128_TYPE;
    10391092    #endif
    10401093    #ifdef WOLFSSL_AES_192
    1041     else if (EVP_AES_192_CFB128 && XSTRNCMP(cipher, EVP_AES_192_CFB128, EVP_AESCFB_SIZE) == 0)
     1094    else if (XSTRNCMP(cipher, EVP_AES_192_CFB128, EVP_AESCFB_SIZE) == 0)
    10421095        return AES_192_CFB128_TYPE;
    10431096    #endif
    10441097    #ifdef WOLFSSL_AES_256
    1045     else if (EVP_AES_256_CFB128 && XSTRNCMP(cipher, EVP_AES_256_CFB128, EVP_AESCFB_SIZE) == 0)
     1098    else if (XSTRNCMP(cipher, EVP_AES_256_CFB128, EVP_AESCFB_SIZE) == 0)
    10461099        return AES_256_CFB128_TYPE;
    10471100    #endif
    10481101#endif /*HAVE_AES_CBC */
     1102#if defined(WOLFSSL_AES_OFB)
     1103    #ifdef WOLFSSL_AES_128
     1104    else if (XSTRNCMP(cipher, EVP_AES_128_OFB, EVP_AES_SIZE) == 0)
     1105      return AES_128_OFB_TYPE;
     1106    #endif
     1107    #ifdef WOLFSSL_AES_192
     1108    else if (XSTRNCMP(cipher, EVP_AES_192_OFB, EVP_AES_SIZE) == 0)
     1109      return AES_192_OFB_TYPE;
     1110    #endif
     1111    #ifdef WOLFSSL_AES_256
     1112    else if (XSTRNCMP(cipher, EVP_AES_256_OFB, EVP_AES_SIZE) == 0)
     1113      return AES_256_OFB_TYPE;
     1114    #endif
     1115#endif
    10491116#endif /* !NO_AES */
     1117
     1118#ifndef NO_RC4
     1119    else if (XSTRNCMP(cipher, EVP_ARC4, EVP_ARC4_SIZE) == 0)
     1120      return ARC4_TYPE;
     1121#endif
    10501122      else return 0;
    10511123}
     
    10661138      case AES_192_GCM_TYPE:
    10671139      case AES_256_GCM_TYPE:
    1068           return AES_BLOCK_SIZE;
     1140          return 1;
    10691141  #endif
    10701142  #if defined(WOLFSSL_AES_COUNTER)
     
    10721144      case AES_192_CTR_TYPE:
    10731145      case AES_256_CTR_TYPE:
    1074           return AES_BLOCK_SIZE;
     1146          return 1;
    10751147  #endif
    10761148  #if defined(HAVE_AES_ECB)
     
    10801152          return AES_BLOCK_SIZE;
    10811153  #endif
     1154  #if defined(WOLFSSL_AES_CFB)
     1155      case AES_128_CFB1_TYPE:
     1156      case AES_192_CFB1_TYPE:
     1157      case AES_256_CFB1_TYPE:
     1158      case AES_128_CFB8_TYPE:
     1159      case AES_192_CFB8_TYPE:
     1160      case AES_256_CFB8_TYPE:
     1161      case AES_128_CFB128_TYPE:
     1162      case AES_192_CFB128_TYPE:
     1163      case AES_256_CFB128_TYPE:
     1164          return 1;
     1165  #endif
     1166  #if defined(WOLFSSL_AES_OFB)
     1167      case AES_128_OFB_TYPE:
     1168      case AES_192_OFB_TYPE:
     1169      case AES_256_OFB_TYPE:
     1170          return 1;
     1171  #endif
     1172  #if defined(WOLFSSL_AES_XTS)
     1173      case AES_128_XTS_TYPE:
     1174      case AES_256_XTS_TYPE:
     1175          return 1;
     1176  #endif
    10821177#endif /* NO_AES */
    1083   #ifndef NO_DES3
     1178
     1179#ifndef NO_RC4
     1180      case ARC4_TYPE:
     1181          return 1;
     1182#endif
     1183
     1184#ifndef NO_DES3
    10841185      case DES_CBC_TYPE: return 8;
    10851186      case DES_EDE3_CBC_TYPE: return 8;
    10861187      case DES_ECB_TYPE: return 8;
    10871188      case DES_EDE3_ECB_TYPE: return 8;
    1088   #endif
     1189#endif
    10891190      default:
    10901191          return 0;
     
    11061207        case AES_192_GCM_TYPE:
    11071208        case AES_256_GCM_TYPE:
    1108             return WOLFSSL_EVP_CIPH_GCM_MODE;
     1209            return WOLFSSL_EVP_CIPH_GCM_MODE &
     1210                    WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
    11091211    #endif
    11101212    #if defined(WOLFSSL_AES_COUNTER)
     
    11141216            return WOLFSSL_EVP_CIPH_CTR_MODE;
    11151217    #endif
     1218    #if defined(WOLFSSL_AES_CFB)
     1219        case AES_128_CFB1_TYPE:
     1220        case AES_192_CFB1_TYPE:
     1221        case AES_256_CFB1_TYPE:
     1222        case AES_128_CFB8_TYPE:
     1223        case AES_192_CFB8_TYPE:
     1224        case AES_256_CFB8_TYPE:
     1225        case AES_128_CFB128_TYPE:
     1226        case AES_192_CFB128_TYPE:
     1227        case AES_256_CFB128_TYPE:
     1228            return WOLFSSL_EVP_CIPH_CFB_MODE;
     1229    #endif
     1230    #if defined(WOLFSSL_AES_OFB)
     1231        case AES_128_OFB_TYPE:
     1232        case AES_192_OFB_TYPE:
     1233        case AES_256_OFB_TYPE:
     1234            return WOLFSSL_EVP_CIPH_OFB_MODE;
     1235    #endif
     1236    #if defined(WOLFSSL_AES_XTS)
     1237        case AES_128_XTS_TYPE:
     1238        case AES_256_XTS_TYPE:
     1239            return WOLFSSL_EVP_CIPH_XTS_MODE;
     1240    #endif
    11161241        case AES_128_ECB_TYPE:
    11171242        case AES_192_ECB_TYPE:
    11181243        case AES_256_ECB_TYPE:
    11191244            return WOLFSSL_EVP_CIPH_ECB_MODE;
    1120 #endif /* NO_ASE */
     1245#endif /* NO_AES */
    11211246    #ifndef NO_DES3
    11221247        case DES_CBC_TYPE:
     
    12091334{
    12101335    WOLFSSL_EVP_PKEY_CTX* ctx;
    1211     int type = NID_undef;
    12121336
    12131337    if (pkey == NULL) return 0;
     
    12231347    ctx->padding = RSA_PKCS1_PADDING;
    12241348#endif
    1225     type = wolfSSL_EVP_PKEY_type(pkey->type);
    1226 
    1227     if (type != NID_undef) {
    1228         if (wc_LockMutex(&pkey->refMutex) != 0) {
    1229             WOLFSSL_MSG("Couldn't lock pkey mutex");
    1230         }
    1231         pkey->references++;
    1232 
    1233         wc_UnLockMutex(&pkey->refMutex);
     1349    if (wolfSSL_EVP_PKEY_up_ref(pkey) != WOLFSSL_SUCCESS) {
     1350        WOLFSSL_MSG("Couldn't increase key reference count");
    12341351    }
    12351352    return ctx;
     
    12641381        pkey->type = id;
    12651382        ctx = wolfSSL_EVP_PKEY_CTX_new(pkey, e);
    1266         if (ctx == NULL) {
    1267             wolfSSL_EVP_PKEY_free(pkey);
    1268         }
     1383        /* wolfSSL_EVP_PKEY_CTX_new calls wolfSSL_EVP_PKEY_up_ref so we need
     1384         * to always call wolfSSL_EVP_PKEY_free (either to free it if an
     1385         * error occured in the previous function or to decrease the reference
     1386         * count so that pkey is actually free'd when wolfSSL_EVP_PKEY_CTX_free
     1387         * is called) */
     1388        wolfSSL_EVP_PKEY_free(pkey);
    12691389    }
    12701390    return ctx;
     
    13111431}
    13121432
     1433#ifndef NO_WOLFSSL_STUB
     1434int wolfSSL_EVP_PKEY_CTX_ctrl_str(WOLFSSL_EVP_PKEY_CTX *ctx,
     1435                          const char *name, const char *value)
     1436{
     1437    WOLFSSL_STUB("wolfSSL_EVP_PKEY_CTX_ctrl_str");
     1438    (void)ctx;
     1439    (void)name;
     1440    (void)value;
     1441    return WOLFSSL_FAILURE;
     1442}
     1443#endif /* NO_WOLFSSL_STUB */
     1444
    13131445#if !defined(NO_DH) && defined(HAVE_ECC)
     1446#if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION!=2))
    13141447int wolfSSL_EVP_PKEY_derive(WOLFSSL_EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
    13151448{
     
    13361469                return WOLFSSL_FAILURE;
    13371470            }
     1471            /* computed DH agreement can be less than DH size if leading zeros */
    13381472            if (wolfSSL_DH_compute_key(key, ctx->peerKey->dh->pub_key,
    1339                                        ctx->pkey->dh) != len) {
     1473                                       ctx->pkey->dh) <= 0) {
    13401474                return WOLFSSL_FAILURE;
    13411475            }
     
    13681502        if (key) {
    13691503            word32 len32 = (word32)len;
     1504#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
     1505    && (!defined(HAVE_FIPS) || \
     1506         (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
     1507
     1508            WC_RNG rng;
     1509            if (wc_InitRng(&rng) != MP_OKAY) {
     1510                WOLFSSL_MSG("Init RNG failed");
     1511                return WOLFSSL_FAILURE;
     1512            }
     1513            ((ecc_key*)ctx->pkey->ecc->internal)->rng = &rng;
     1514#endif
    13701515            if (*keylen < len32) {
    13711516                WOLFSSL_MSG("buffer too short");
     1517#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
     1518    && (!defined(HAVE_FIPS) || \
     1519         (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
     1520                ((ecc_key*)ctx->pkey->ecc->internal)->rng = NULL;
     1521                wc_FreeRng(&rng);
     1522#endif
    13721523                return WOLFSSL_FAILURE;
    13731524            }
     
    13761527                                         key, &len32) != MP_OKAY) {
    13771528                WOLFSSL_MSG("wc_ecc_shared_secret failed");
     1529#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
     1530    && (!defined(HAVE_FIPS) || \
     1531         (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
     1532                ((ecc_key*)ctx->pkey->ecc->internal)->rng = NULL;
     1533                wc_FreeRng(&rng);
     1534#endif
    13781535                return WOLFSSL_FAILURE;
    13791536            }
     1537#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
     1538    && (!defined(HAVE_FIPS) || \
     1539         (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
     1540            ((ecc_key*)ctx->pkey->ecc->internal)->rng = NULL;
     1541            wc_FreeRng(&rng);
     1542#endif
    13801543            len = (int)len32;
    13811544        }
     
    13891552    return WOLFSSL_SUCCESS;
    13901553}
    1391 #endif
     1554#endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
     1555#endif /* !NO_DH || HAVE_ECC */
    13921556
    13931557/* Uses the WOLFSSL_EVP_PKEY_CTX to decrypt a buffer.
     
    17201884}
    17211885
     1886
     1887int wolfSSL_EVP_PKEY_copy_parameters(WOLFSSL_EVP_PKEY *to,
     1888        const WOLFSSL_EVP_PKEY *from)
     1889{
     1890    WOLFSSL_ENTER("wolfSSL_EVP_PKEY_copy_parameters");
     1891
     1892    if (!to || !from) {
     1893        WOLFSSL_MSG("Bad parameter");
     1894        return WOLFSSL_FAILURE;
     1895    }
     1896
     1897    if (to->type == EVP_PKEY_NONE) {
     1898        to->type = from->type;
     1899    }
     1900    else if (to->type != from->type) {
     1901        WOLFSSL_MSG("Different key types");
     1902        return WOLFSSL_FAILURE;
     1903    }
     1904
     1905    switch(from->type) {
     1906#ifdef HAVE_ECC
     1907    case EVP_PKEY_EC:
     1908        if (from->ecc) {
     1909            if (!to->ecc && !(to->ecc = wolfSSL_EC_KEY_new())) {
     1910                WOLFSSL_MSG("wolfSSL_EC_KEY_new error");
     1911                return WOLFSSL_FAILURE;
     1912            }
     1913            to->ownEcc = 1;
     1914            to->ecc->group->curve_idx = from->ecc->group->curve_idx;
     1915            to->ecc->group->curve_nid = from->ecc->group->curve_nid;
     1916            to->ecc->group->curve_oid = from->ecc->group->curve_oid;
     1917        }
     1918        else {
     1919            WOLFSSL_MSG("Missing ECC struct");
     1920            return WOLFSSL_FAILURE;
     1921        }
     1922        break;
     1923#endif
     1924#ifndef NO_DSA
     1925    case EVP_PKEY_DSA:
     1926        if (from->dsa) {
     1927            WOLFSSL_BIGNUM* cpy;
     1928            if (!to->dsa && !(to->dsa = wolfSSL_DSA_new())) {
     1929                WOLFSSL_MSG("wolfSSL_DSA_new error");
     1930                return WOLFSSL_FAILURE;
     1931            }
     1932            if (!(cpy = wolfSSL_BN_dup(from->dsa->p))) {
     1933                WOLFSSL_MSG("wolfSSL_BN_dup error");
     1934                return WOLFSSL_FAILURE;
     1935            }
     1936            to->dsa->p = cpy;
     1937            if (!(cpy = wolfSSL_BN_dup(from->dsa->q))) {
     1938                WOLFSSL_MSG("wolfSSL_BN_dup error");
     1939                return WOLFSSL_FAILURE;
     1940            }
     1941            to->dsa->q = cpy;
     1942            if (!(cpy = wolfSSL_BN_dup(from->dsa->g))) {
     1943                WOLFSSL_MSG("wolfSSL_BN_dup error");
     1944                return WOLFSSL_FAILURE;
     1945            }
     1946            to->dsa->g = cpy;
     1947        }
     1948        else {
     1949            WOLFSSL_MSG("Missing DSA struct");
     1950            return WOLFSSL_FAILURE;
     1951        }
     1952        break;
     1953#endif
     1954#ifndef NO_RSA
     1955    case EVP_PKEY_RSA:
     1956#endif
     1957#ifndef NO_DH
     1958    case EVP_PKEY_DH:
     1959#endif
     1960    default:
     1961        WOLFSSL_MSG("Copy parameters not available for this key type");
     1962        return WOLFSSL_FAILURE;
     1963    }
     1964#if defined(HAVE_ECC) || !defined(NO_DSA)
     1965    return WOLFSSL_SUCCESS;
     1966#endif
     1967}
     1968
    17221969#ifndef NO_WOLFSSL_STUB
    17231970WOLFSSL_API int wolfSSL_EVP_PKEY_missing_parameters(WOLFSSL_EVP_PKEY *pkey)
     
    17602007#endif /* HAVE_ECC */
    17612008    default:
    1762         break;
     2009        return ret;
    17632010    } /* switch (a->type) */
    17642011
     
    18242071
    18252072static const struct s_ent {
    1826     const int macType;
     2073    const enum wc_HashType macType;
    18272074    const int nid;
    18282075    const char *name;
     
    18372084
    18382085#ifndef NO_SHA
    1839     {WC_HASH_TYPE_SHA, NID_sha1, "SHA"},
     2086    {WC_HASH_TYPE_SHA, NID_sha1, "SHA1"},
     2087    {WC_HASH_TYPE_SHA, NID_sha1, "SHA"}, /* Leave for backwards compatibility */
    18402088#endif /* NO_SHA */
    18412089
     
    18592107    {WC_HASH_TYPE_SHA3_256, NID_sha3_256, "SHA3_256"},
    18602108#endif
     2109#ifndef WOLFSSL_NOSHA3_384
    18612110    {WC_HASH_TYPE_SHA3_384, NID_sha3_384, "SHA3_384"},
     2111#endif
    18622112#ifndef WOLFSSL_NOSHA3_512
    18632113    {WC_HASH_TYPE_SHA3_512, NID_sha3_512, "SHA3_512"},
    18642114#endif
    1865     {0, 0, NULL}
     2115    {WC_HASH_TYPE_NONE, 0, NULL}
    18662116};
    18672117
    1868 static int wolfSSL_EVP_md2macType(const WOLFSSL_EVP_MD *md)
     2118static enum wc_HashType wolfSSL_EVP_md2macType(const WOLFSSL_EVP_MD *md)
    18692119{
    18702120    const struct s_ent *ent ;
     
    20472297}
    20482298
    2049 
    20502299/* Initialize an EVP_DigestSign/Verify operation.
    20512300 * Initialize a digest for RSA and ECC keys, or HMAC for HMAC key.
     
    20572306                                      WOLFSSL_EVP_PKEY *pkey)
    20582307{
     2308    if (!type) {
     2309        int default_digest;
     2310        if (wolfSSL_EVP_PKEY_get_default_digest_nid(pkey, &default_digest)
     2311                != WOLFSSL_SUCCESS) {
     2312            WOLFSSL_MSG("Could not get default digest");
     2313            return WOLFSSL_FAILURE;
     2314        }
     2315        type = wolfSSL_EVP_get_digestbynid(default_digest);
     2316        if (!type) {
     2317            return BAD_FUNC_ARG;
     2318        }
     2319    }
     2320
    20592321    if (pkey->type == EVP_PKEY_HMAC) {
    20602322        int                  hashType;
    20612323        const unsigned char* key;
    2062         size_t               keySz;
    20632324
    20642325        if (XSTRNCMP(type, "SHA256", 6) == 0) {
     
    20802341        }
    20812342    #endif
     2343#ifdef WOLFSSL_SHA3
     2344    #ifndef WOLFSSL_NOSHA3_224
     2345        else if (XSTRNCMP(type, "SHA3_224", 8) == 0) {
     2346            hashType = WC_SHA3_224;
     2347        }
     2348    #endif
     2349    #ifndef WOLFSSL_NOSHA3_256
     2350        else if (XSTRNCMP(type, "SHA3_256", 8) == 0) {
     2351            hashType = WC_SHA3_256;
     2352        }
     2353    #endif
     2354        else if (XSTRNCMP(type, "SHA3_384", 8) == 0) {
     2355            hashType = WC_SHA3_384;
     2356        }
     2357    #ifndef WOLFSSL_NOSHA3_512
     2358        else if (XSTRNCMP(type, "SHA3_512", 8) == 0) {
     2359            hashType = WC_SHA3_512;
     2360        }
     2361    #endif
     2362#endif
    20822363    #ifndef NO_MD5
    20832364        else if (XSTRNCMP(type, "MD5", 3) == 0) {
     
    20882369        /* has to be last since would pick or 224, 256, 384, or 512 too */
    20892370        else if (XSTRNCMP(type, "SHA", 3) == 0) {
    2090              hashType = WC_SHA;
     2371            hashType = WC_SHA;
    20912372        }
    20922373    #endif /* NO_SHA */
     
    20942375             return BAD_FUNC_ARG;
    20952376
    2096         key = wolfSSL_EVP_PKEY_get0_hmac(pkey, &keySz);
    2097 
    2098         if (wc_HmacInit(&ctx->hash.hmac, NULL, INVALID_DEVID) != 0)
     2377        {
     2378            size_t keySz = 0;
     2379
     2380            key = wolfSSL_EVP_PKEY_get0_hmac(pkey, &keySz);
     2381
     2382            if (wc_HmacInit(&ctx->hash.hmac, NULL, INVALID_DEVID) != 0)
     2383                return WOLFSSL_FAILURE;
     2384
     2385            if (wc_HmacSetKey(&ctx->hash.hmac, hashType, key, (word32)keySz) != 0)
     2386                return WOLFSSL_FAILURE;
     2387        }
     2388
     2389        ctx->isHMAC = 1;
     2390    }
     2391    else if (wolfSSL_EVP_DigestInit(ctx, type) != 1)
    20992392            return WOLFSSL_FAILURE;
    21002393
    2101         if (wc_HmacSetKey(&ctx->hash.hmac, hashType, key, (word32)keySz) != 0)
     2394    if (ctx->pctx == NULL) {
     2395        ctx->pctx = wolfSSL_EVP_PKEY_CTX_new(pkey, e);
     2396        if (ctx->pctx == NULL)
    21022397            return WOLFSSL_FAILURE;
    2103 
    2104         ctx->macType = NID_hmac;
    2105     }
    2106     else {
    2107         int ret;
    2108 
    2109         if (ctx->pctx == NULL) {
    2110             ctx->pctx = wolfSSL_EVP_PKEY_CTX_new(pkey, e);
    2111             if (ctx->pctx == NULL)
    2112                 return WOLFSSL_FAILURE;
    2113         }
    2114 
    2115         ret = wolfSSL_EVP_DigestInit(ctx, type);
    2116         if (ret == WOLFSSL_SUCCESS && pctx != NULL)
    2117             *pctx = ctx->pctx;
    2118         return ret;
    2119     }
    2120 
     2398    }
     2399    if (pctx != NULL)
     2400        *pctx = ctx->pctx;
    21212401    return WOLFSSL_SUCCESS;
    21222402}
     
    21282408                                        const void *d, unsigned int cnt)
    21292409{
    2130     if (ctx->pctx == NULL) {
    2131         if (ctx->macType != NID_hmac)
    2132             return WOLFSSL_FAILURE;
    2133 
     2410    if (ctx->isHMAC) {
    21342411        if (wc_HmacUpdate(&ctx->hash.hmac, (const byte *)d, cnt) != 0)
    21352412            return WOLFSSL_FAILURE;
     
    21502427    int  ret;
    21512428
    2152     if (ctx->pctx == NULL) {
     2429    if (ctx->isHMAC) {
    21532430        Hmac hmacCopy;
    2154 
    2155         if (ctx->macType != NID_hmac)
    2156             return WOLFSSL_FAILURE;
    21572431
    21582432        if (wolfSSL_HmacCopy(&hmacCopy, &ctx->hash.hmac) != WOLFSSL_SUCCESS)
     
    22212495    #endif /* HAVE_BLAKE2 */
    22222496
     2497    #ifdef WOLFSSL_SHA3
     2498        #ifndef WOLFSSL_NOSHA3_224
     2499        case WC_SHA3_224:
     2500            hashLen = WC_SHA3_224_DIGEST_SIZE;
     2501            break;
     2502        #endif
     2503        #ifndef WOLFSSL_NOSHA3_256
     2504        case WC_SHA3_256:
     2505            hashLen = WC_SHA3_256_DIGEST_SIZE;
     2506            break;
     2507        #endif
     2508        #ifndef WOLFSSL_NOSHA3_384
     2509        case WC_SHA3_384:
     2510            hashLen = WC_SHA3_384_DIGEST_SIZE;
     2511            break;
     2512        #endif
     2513        #ifndef WOLFSSL_NOSHA3_512
     2514        case WC_SHA3_512:
     2515            hashLen = WC_SHA3_512_DIGEST_SIZE;
     2516            break;
     2517        #endif
     2518    #endif
     2519
    22232520        default:
    22242521            hashLen = 0;
     
    22362533    WOLFSSL_ENTER("EVP_DigestSignInit");
    22372534
    2238     if (ctx == NULL || type == NULL || pkey == NULL)
     2535    if (ctx == NULL || pkey == NULL)
    22392536        return BAD_FUNC_ARG;
    22402537
     
    22672564
    22682565    /* Return the maximum size of the signaure when sig is NULL. */
    2269     if (ctx->pctx == NULL) {
    2270         if (ctx->macType != NID_hmac)
    2271             return WOLFSSL_FAILURE;
    2272 
     2566    if (ctx->isHMAC) {
    22732567        hashLen = wolfssl_mac_len(ctx->hash.hmac.macType);
    22742568
     
    23002594        return WOLFSSL_FAILURE;
    23012595
    2302     if (ctx->pctx == NULL) {
     2596    if (ctx->isHMAC) {
    23032597        /* Copy the HMAC result as signature. */
    23042598        if ((unsigned int)(*siglen) > hashLen)
     
    23182612            if (nid < 0)
    23192613                break;
    2320             ret = wolfSSL_RSA_sign(nid, digest, hashLen, sig, &sigSz,
    2321                                    ctx->pctx->pkey->rsa);
     2614            ret = wolfSSL_RSA_sign_generic_padding(nid, digest, hashLen,
     2615                    sig, &sigSz, ctx->pctx->pkey->rsa, 1, ctx->pctx->padding);
    23222616            if (ret >= 0)
    23232617                *siglen = sigSz;
     
    23852679        return WOLFSSL_FAILURE;
    23862680
    2387     if (ctx->pctx == NULL) {
    2388         if (ctx->macType != NID_hmac)
    2389             return WOLFSSL_FAILURE;
     2681    if (ctx->isHMAC) {
    23902682
    23912683        hashLen = wolfssl_mac_len(ctx->hash.hmac.macType);
     
    23992691        return WOLFSSL_FAILURE;
    24002692
    2401     if (ctx->pctx == NULL) {
     2693    if (ctx->isHMAC) {
    24022694        /* Check HMAC result matches the signature. */
    24032695        if (XMEMCMP(sig, digest, siglen) == 0)
     
    24132705            if (nid < 0)
    24142706                return WOLFSSL_FAILURE;
    2415             return wolfSSL_RSA_verify(nid, digest, hashLen, sig,
     2707            return wolfSSL_RSA_verify_ex(nid, digest, hashLen, sig,
    24162708                                      (unsigned int)siglen,
    2417                                       ctx->pctx->pkey->rsa);
     2709                                      ctx->pctx->pkey->rsa, ctx->pctx->padding);
    24182710        }
    24192711    #endif /* NO_RSA */
     
    25742866
    25752867#ifndef NO_AES
     2868    #ifdef HAVE_AES_CBC
    25762869    #ifdef WOLFSSL_AES_128
    2577     {AES_128_CBC_TYPE, "AES-128-CBC", NID_aes_128_cbc},
     2870    {AES_128_CBC_TYPE, EVP_AES_128_CBC, NID_aes_128_cbc},
    25782871    #endif
    25792872    #ifdef WOLFSSL_AES_192
    2580     {AES_192_CBC_TYPE, "AES-192-CBC", NID_aes_192_cbc},
     2873    {AES_192_CBC_TYPE, EVP_AES_192_CBC, NID_aes_192_cbc},
    25812874    #endif
    25822875    #ifdef WOLFSSL_AES_256
    2583     {AES_256_CBC_TYPE, "AES-256-CBC", NID_aes_256_cbc},
    2584     #endif
    2585 
     2876    {AES_256_CBC_TYPE, EVP_AES_256_CBC, NID_aes_256_cbc},
     2877    #endif
     2878    #endif
     2879
     2880    #ifdef WOLFSSL_AES_CFB
    25862881    #ifdef WOLFSSL_AES_128
    2587     {AES_128_CFB1_TYPE, "AES-128-CFB1", NID_aes_128_cfb1},
     2882    {AES_128_CFB1_TYPE, EVP_AES_128_CFB1, NID_aes_128_cfb1},
    25882883    #endif
    25892884    #ifdef WOLFSSL_AES_192
    2590     {AES_192_CFB1_TYPE, "AES-192-CFB1", NID_aes_192_cfb1},
     2885    {AES_192_CFB1_TYPE, EVP_AES_192_CFB1, NID_aes_192_cfb1},
    25912886    #endif
    25922887    #ifdef WOLFSSL_AES_256
    2593     {AES_256_CFB1_TYPE, "AES-256-CFB1", NID_aes_256_cfb1},
     2888    {AES_256_CFB1_TYPE, EVP_AES_256_CFB1, NID_aes_256_cfb1},
    25942889    #endif
    25952890
    25962891    #ifdef WOLFSSL_AES_128
    2597     {AES_128_CFB8_TYPE, "AES-128-CFB8", NID_aes_128_cfb8},
     2892    {AES_128_CFB8_TYPE, EVP_AES_128_CFB8, NID_aes_128_cfb8},
    25982893    #endif
    25992894    #ifdef WOLFSSL_AES_192
    2600     {AES_192_CFB8_TYPE, "AES-192-CFB8", NID_aes_192_cfb8},
     2895    {AES_192_CFB8_TYPE, EVP_AES_192_CFB8, NID_aes_192_cfb8},
    26012896    #endif
    26022897    #ifdef WOLFSSL_AES_256
    2603     {AES_256_CFB8_TYPE, "AES-256-CFB8", NID_aes_256_cfb8},
     2898    {AES_256_CFB8_TYPE, EVP_AES_256_CFB8, NID_aes_256_cfb8},
    26042899    #endif
    26052900
    26062901    #ifdef WOLFSSL_AES_128
    2607     {AES_128_CFB128_TYPE, "AES-128-CFB128", NID_aes_128_cfb128},
     2902    {AES_128_CFB128_TYPE, EVP_AES_128_CFB128, NID_aes_128_cfb128},
    26082903    #endif
    26092904    #ifdef WOLFSSL_AES_192
    2610     {AES_192_CFB128_TYPE, "AES-192-CFB128", NID_aes_192_cfb128},
     2905    {AES_192_CFB128_TYPE, EVP_AES_192_CFB128, NID_aes_192_cfb128},
    26112906    #endif
    26122907    #ifdef WOLFSSL_AES_256
    2613     {AES_256_CFB128_TYPE, "AES-256-CFB128", NID_aes_256_cfb128},
    2614     #endif
    2615 
     2908    {AES_256_CFB128_TYPE, EVP_AES_256_CFB128, NID_aes_256_cfb128},
     2909    #endif
     2910    #endif
     2911
     2912    #ifdef HAVE_AES_OFB
    26162913    #ifdef WOLFSSL_AES_128
    2617     {AES_128_OFB_TYPE, "AES-128-OFB", NID_aes_128_ofb},
     2914    {AES_128_OFB_TYPE, EVP_AES_128_OFB, NID_aes_128_ofb},
    26182915    #endif
    26192916    #ifdef WOLFSSL_AES_192
    2620     {AES_192_OFB_TYPE, "AES-192-OFB", NID_aes_192_ofb},
     2917    {AES_192_OFB_TYPE, EVP_AES_192_OFB, NID_aes_192_ofb},
    26212918    #endif
    26222919    #ifdef WOLFSSL_AES_256
    2623     {AES_256_OFB_TYPE, "AES-256-OFB", NID_aes_256_ofb},
    2624     #endif
    2625 
     2920    {AES_256_OFB_TYPE, EVP_AES_256_OFB, NID_aes_256_ofb},
     2921    #endif
     2922    #endif
     2923
     2924    #ifdef HAVE_AES_XTS
    26262925    #ifdef WOLFSSL_AES_128
    2627     {AES_128_XTS_TYPE, "AES-128-XTS", NID_aes_128_xts},
     2926    {AES_128_XTS_TYPE, EVP_AES_128_XTS, NID_aes_128_xts},
    26282927    #endif
    26292928    #ifdef WOLFSSL_AES_256
    2630     {AES_256_XTS_TYPE, "AES-256-XTS", NID_aes_256_xts},
    2631     #endif
    2632 
     2929    {AES_256_XTS_TYPE, EVP_AES_256_XTS, NID_aes_256_xts},
     2930    #endif
     2931    #endif
     2932
     2933    #ifdef HAVE_AESGCM
    26332934    #ifdef WOLFSSL_AES_128
    2634     {AES_128_GCM_TYPE, "AES-128-GCM", NID_aes_128_gcm},
     2935    {AES_128_GCM_TYPE, EVP_AES_128_GCM, NID_aes_128_gcm},
    26352936    #endif
    26362937    #ifdef WOLFSSL_AES_192
    2637     {AES_192_GCM_TYPE, "AES-192-GCM", NID_aes_192_gcm},
     2938    {AES_192_GCM_TYPE, EVP_AES_192_GCM, NID_aes_192_gcm},
    26382939    #endif
    26392940    #ifdef WOLFSSL_AES_256
    2640     {AES_256_GCM_TYPE, "AES-256-GCM", NID_aes_256_gcm},
    2641     #endif
     2941    {AES_256_GCM_TYPE, EVP_AES_256_GCM, NID_aes_256_gcm},
     2942    #endif
     2943    #endif
     2944
     2945    #ifdef WOLFSSL_AES_COUNTER
    26422946    #ifdef WOLFSSL_AES_128
    2643         {AES_128_CTR_TYPE, "AES-128-CTR", NID_aes_128_ctr},
     2947        {AES_128_CTR_TYPE, EVP_AES_128_CTR, NID_aes_128_ctr},
    26442948    #endif
    26452949    #ifdef WOLFSSL_AES_192
    2646         {AES_192_CTR_TYPE, "AES-192-CTR", NID_aes_192_ctr},
     2950        {AES_192_CTR_TYPE, EVP_AES_192_CTR, NID_aes_192_ctr},
    26472951    #endif
    26482952    #ifdef WOLFSSL_AES_256
    2649         {AES_256_CTR_TYPE, "AES-256-CTR", NID_aes_256_ctr},
    2650     #endif
    2651 
     2953        {AES_256_CTR_TYPE, EVP_AES_256_CTR, NID_aes_256_ctr},
     2954    #endif
     2955    #endif
     2956
     2957    #ifdef HAVE_AES_ECB
    26522958    #ifdef WOLFSSL_AES_128
    2653         {AES_128_ECB_TYPE, "AES-128-ECB", NID_aes_128_ecb},
     2959        {AES_128_ECB_TYPE, EVP_AES_128_ECB, NID_aes_128_ecb},
    26542960    #endif
    26552961    #ifdef WOLFSSL_AES_192
    2656         {AES_192_ECB_TYPE, "AES-192-ECB", NID_aes_192_ecb},
     2962        {AES_192_ECB_TYPE, EVP_AES_192_ECB, NID_aes_192_ecb},
    26572963    #endif
    26582964    #ifdef WOLFSSL_AES_256
    2659         {AES_256_ECB_TYPE, "AES-256-ECB", NID_aes_256_ecb},
    2660     #endif
    2661 
     2965        {AES_256_ECB_TYPE, EVP_AES_256_ECB, NID_aes_256_ecb},
     2966    #endif
     2967    #endif
    26622968#endif
    26632969
    26642970#ifndef NO_DES3
    2665     {DES_CBC_TYPE, "DES-CBC", NID_des_cbc},
    2666     {DES_ECB_TYPE, "DES-ECB", NID_des_ecb},
    2667 
    2668     {DES_EDE3_CBC_TYPE, "DES-EDE3-CBC", NID_des_ede3_cbc},
    2669     {DES_EDE3_ECB_TYPE, "DES-EDE3-ECB", NID_des_ede3_ecb},
     2971    {DES_CBC_TYPE, EVP_DES_CBC, NID_des_cbc},
     2972    {DES_ECB_TYPE, EVP_DES_ECB, NID_des_ecb},
     2973
     2974    {DES_EDE3_CBC_TYPE, EVP_DES_EDE3_CBC, NID_des_ede3_cbc},
     2975    {DES_EDE3_ECB_TYPE, EVP_DES_EDE3_ECB, NID_des_ede3_ecb},
    26702976#endif
    26712977
    26722978#ifndef NO_RC4
    2673     {ARC4_TYPE, "ARC4", NID_undef},
     2979    {ARC4_TYPE, EVP_ARC4, NID_undef},
    26742980#endif
    26752981
    26762982#ifdef HAVE_IDEA
    2677     {IDEA_CBC_TYPE, "IDEA-CBC", NID_idea_cbc},
     2983    {IDEA_CBC_TYPE, EVP_IDEA_CBC, NID_idea_cbc},
    26782984#endif
    26792985    { 0, NULL, 0}
     
    27193025{
    27203026
    2721     static const struct alias {
     3027    const struct alias {
    27223028        const char *name;
    27233029        const char *alias;
     
    27253031    {
    27263032#ifndef NO_DES3
    2727         {"DES-CBC", "DES"},
    2728         {"DES-CBC", "des"},
    2729         {"DES-ECB", "DES-ECB"},
    2730         {"DES-ECB", "des-ecb"},
    2731         {"DES-EDE3-CBC", "DES3"},
    2732         {"DES-EDE3-CBC", "des3"},
    2733         {"DES-EDE3-ECB", "DES-EDE3"},
    2734         {"DES-EDE3-ECB", "des-ede3"},
    2735         {"DES-EDE3-ECB", "des-ede3-ecb"},
     3033        {EVP_DES_CBC, "DES"},
     3034        {EVP_DES_CBC, "des"},
     3035        {EVP_DES_ECB, "DES-ECB"},
     3036        {EVP_DES_ECB, "des-ecb"},
     3037        {EVP_DES_EDE3_CBC, "DES3"},
     3038        {EVP_DES_EDE3_CBC, "des3"},
     3039        {EVP_DES_EDE3_ECB, "DES-EDE3"},
     3040        {EVP_DES_EDE3_ECB, "des-ede3"},
     3041        {EVP_DES_EDE3_ECB, "des-ede3-ecb"},
    27363042#endif
    27373043#ifdef HAVE_IDEA
    2738         {"IDEA-CBC", "IDEA"},
    2739         {"IDEA-CBC", "idea"},
     3044        {EVP_IDEA_CBC, "IDEA"},
     3045        {EVP_IDEA_CBC, "idea"},
    27403046#endif
    27413047#ifndef NO_AES
    27423048    #ifdef HAVE_AES_CBC
    27433049        #ifdef WOLFSSL_AES_128
    2744         {"AES-128-CBC", "AES128-CBC"},
    2745         {"AES-128-CBC", "aes128-cbc"},
     3050            {EVP_AES_128_CBC, "AES128-CBC"},
     3051            {EVP_AES_128_CBC, "aes128-cbc"},
    27463052        #endif
    27473053        #ifdef WOLFSSL_AES_192
    2748         {"AES-192-CBC", "AES192-CBC"},
    2749         {"AES-192-CBC", "aes192-cbc"},
     3054            {EVP_AES_192_CBC, "AES192-CBC"},
     3055            {EVP_AES_192_CBC, "aes192-cbc"},
    27503056        #endif
    27513057        #ifdef WOLFSSL_AES_256
    2752         {"AES-256-CBC", "AES256-CBC"},
    2753         {"AES-256-CBC", "aes256-cbc"},
     3058            {EVP_AES_256_CBC, "AES256-CBC"},
     3059            {EVP_AES_256_CBC, "aes256-cbc"},
    27543060        #endif
    27553061    #endif
    2756     #ifdef WOLFSSL_AES_128
    2757         {"AES-128-ECB", "AES128-ECB"},
    2758         {"AES-128-ECB", "aes128-ecb"},
    2759     #endif
    2760     #ifdef WOLFSSL_AES_192
    2761         {"AES-192-ECB", "AES192-ECB"},
    2762         {"AES-192-ECB", "aes192-ecb"},
    2763     #endif
    2764     #ifdef WOLFSSL_AES_256
    2765         {"AES-256-ECB", "AES256-ECB"},
     3062    #ifdef HAVE_AES_ECB
     3063        #ifdef WOLFSSL_AES_128
     3064            {EVP_AES_128_ECB, "AES128-ECB"},
     3065            {EVP_AES_128_ECB, "aes128-ecb"},
     3066        #endif
     3067        #ifdef WOLFSSL_AES_192
     3068            {EVP_AES_192_ECB, "AES192-ECB"},
     3069            {EVP_AES_192_ECB, "aes192-ecb"},
     3070        #endif
     3071        #ifdef WOLFSSL_AES_256
     3072            {EVP_AES_256_ECB, "AES256-ECB"},
     3073        #endif
    27663074    #endif
    27673075    #ifdef HAVE_AESGCM
    27683076        #ifdef WOLFSSL_AES_128
    2769         {"AES-128-GCM", "aes-128-gcm"},
    2770         {"AES-128-GCM", "id-aes128-GCM"},
     3077            {EVP_AES_128_GCM, "aes-128-gcm"},
     3078            {EVP_AES_128_GCM, "id-aes128-GCM"},
    27713079        #endif
    27723080        #ifdef WOLFSSL_AES_192
    2773         {"AES-192-GCM", "aes-192-gcm"},
    2774         {"AES-192-GCM", "id-aes192-GCM"},
     3081            {EVP_AES_192_GCM, "aes-192-gcm"},
     3082            {EVP_AES_192_GCM, "id-aes192-GCM"},
    27753083        #endif
    27763084        #ifdef WOLFSSL_AES_256
    2777         {"AES-256-GCM", "aes-256-gcm"},
    2778         {"AES-256-GCM", "id-aes256-GCM"},
     3085            {EVP_AES_256_GCM, "aes-256-gcm"},
     3086            {EVP_AES_256_GCM, "id-aes256-GCM"},
    27793087        #endif
    27803088    #endif
    27813089#endif
    27823090#ifndef NO_RC4
    2783         {"ARC4", "RC4"},
     3091        {EVP_ARC4, "RC4"},
    27843092#endif
    27853093        { NULL, NULL}
     
    29063214void wolfSSL_EVP_init(void)
    29073215{
    2908 #ifndef NO_AES
    2909     #ifdef HAVE_AES_CBC
    2910         #ifdef WOLFSSL_AES_128
    2911         EVP_AES_128_CBC = (char *)EVP_get_cipherbyname("AES-128-CBC");
    2912         #endif
    2913         #ifdef WOLFSSL_AES_192
    2914         EVP_AES_192_CBC = (char *)EVP_get_cipherbyname("AES-192-CBC");
    2915         #endif
    2916         #ifdef WOLFSSL_AES_256
    2917         EVP_AES_256_CBC = (char *)EVP_get_cipherbyname("AES-256-CBC");
    2918         #endif
    2919     #endif /* HAVE_AES_CBC */
    2920 
    2921     #ifdef WOLFSSL_AES_CFB
    2922         #ifdef WOLFSSL_AES_128
    2923         EVP_AES_128_CFB1 = (char *)EVP_get_cipherbyname("AES-128-CFB1");
    2924         #endif
    2925 
    2926         #ifdef WOLFSSL_AES_192
    2927         EVP_AES_192_CFB1 = (char *)EVP_get_cipherbyname("AES-192-CFB1");
    2928         #endif
    2929 
    2930         #ifdef WOLFSSL_AES_256
    2931         EVP_AES_256_CFB1 = (char *)EVP_get_cipherbyname("AES-256-CFB1");
    2932         #endif
    2933 
    2934         #ifdef WOLFSSL_AES_128
    2935         EVP_AES_128_CFB8 = (char *)EVP_get_cipherbyname("AES-128-CFB8");
    2936         #endif
    2937 
    2938         #ifdef WOLFSSL_AES_192
    2939         EVP_AES_192_CFB8 = (char *)EVP_get_cipherbyname("AES-192-CFB8");
    2940         #endif
    2941 
    2942         #ifdef WOLFSSL_AES_256
    2943         EVP_AES_256_CFB8 = (char *)EVP_get_cipherbyname("AES-256-CFB8");
    2944         #endif
    2945 
    2946         #ifdef WOLFSSL_AES_128
    2947         EVP_AES_128_CFB128 = (char *)EVP_get_cipherbyname("AES-128-CFB128");
    2948         #endif
    2949 
    2950         #ifdef WOLFSSL_AES_192
    2951         EVP_AES_192_CFB128 = (char *)EVP_get_cipherbyname("AES-192-CFB128");
    2952         #endif
    2953 
    2954         #ifdef WOLFSSL_AES_256
    2955         EVP_AES_256_CFB128 = (char *)EVP_get_cipherbyname("AES-256-CFB128");
    2956         #endif
    2957     #endif /* WOLFSSL_AES_CFB */
    2958 
    2959     #ifdef WOLFSSL_AES_OFB
    2960         #ifdef WOLFSSL_AES_128
    2961         EVP_AES_128_OFB = (char *)EVP_get_cipherbyname("AES-128-OFB");
    2962         #endif
    2963 
    2964         #ifdef WOLFSSL_AES_192
    2965         EVP_AES_192_OFB = (char *)EVP_get_cipherbyname("AES-192-OFB");
    2966         #endif
    2967 
    2968         #ifdef WOLFSSL_AES_256
    2969         EVP_AES_256_OFB = (char *)EVP_get_cipherbyname("AES-256-OFB");
    2970         #endif
    2971     #endif /* WOLFSSL_AES_OFB */
    2972 
    2973     #ifdef WOLFSSL_AES_XTS
    2974         #ifdef WOLFSSL_AES_128
    2975         EVP_AES_128_XTS = (char *)EVP_get_cipherbyname("AES-128-XTS");
    2976         #endif
    2977 
    2978         #ifdef WOLFSSL_AES_256
    2979         EVP_AES_256_XTS = (char *)EVP_get_cipherbyname("AES-256-XTS");
    2980         #endif
    2981     #endif /* WOLFSSL_AES_XTS */
    2982 
    2983     #ifdef HAVE_AESGCM
    2984         #ifdef WOLFSSL_AES_128
    2985         EVP_AES_128_GCM = (char *)EVP_get_cipherbyname("AES-128-GCM");
    2986         #endif
    2987         #ifdef WOLFSSL_AES_192
    2988         EVP_AES_192_GCM = (char *)EVP_get_cipherbyname("AES-192-GCM");
    2989         #endif
    2990         #ifdef WOLFSSL_AES_256
    2991         EVP_AES_256_GCM = (char *)EVP_get_cipherbyname("AES-256-GCM");
    2992         #endif
    2993     #endif /* HAVE_AESGCM*/
    2994         #ifdef WOLFSSL_AES_128
    2995         EVP_AES_128_CTR = (char *)EVP_get_cipherbyname("AES-128-CTR");
    2996         #endif
    2997         #ifdef WOLFSSL_AES_192
    2998         EVP_AES_192_CTR = (char *)EVP_get_cipherbyname("AES-192-CTR");
    2999         #endif
    3000         #ifdef WOLFSSL_AES_256
    3001         EVP_AES_256_CTR = (char *)EVP_get_cipherbyname("AES-256-CTR");
    3002         #endif
    3003 
    3004         #ifdef WOLFSSL_AES_128
    3005         EVP_AES_128_ECB = (char *)EVP_get_cipherbyname("AES-128-ECB");
    3006         #endif
    3007         #ifdef WOLFSSL_AES_192
    3008         EVP_AES_192_ECB = (char *)EVP_get_cipherbyname("AES-192-ECB");
    3009         #endif
    3010         #ifdef WOLFSSL_AES_256
    3011         EVP_AES_256_ECB = (char *)EVP_get_cipherbyname("AES-256-ECB");
    3012         #endif
    3013 #endif /* ifndef NO_AES*/
    3014 
    3015 #ifndef NO_DES3
    3016     EVP_DES_CBC = (char *)EVP_get_cipherbyname("DES-CBC");
    3017     EVP_DES_ECB = (char *)EVP_get_cipherbyname("DES-ECB");
    3018 
    3019     EVP_DES_EDE3_CBC = (char *)EVP_get_cipherbyname("DES-EDE3-CBC");
    3020     EVP_DES_EDE3_ECB = (char *)EVP_get_cipherbyname("DES-EDE3-ECB");
    3021 #endif
    3022 
    3023 #ifdef HAVE_IDEA
    3024     EVP_IDEA_CBC = (char *)EVP_get_cipherbyname("IDEA-CBC");
    3025 #endif
     3216    /* Does nothing. */
    30263217}
    30273218
    30283219#if !defined(NO_PWDBASED)
    3029 int wolfSSL_EVP_get_hashinfo(const WOLFSSL_EVP_MD* evp,
    3030     int* pHash, int* pHashSz)
    3031 {
    3032     enum wc_HashType hash = WC_HASH_TYPE_NONE;
    3033     int hashSz;
    3034 
    3035     if (XSTRLEN(evp) < 3) {
    3036         /* do not try comparing strings if size is too small */
    3037         return WOLFSSL_FAILURE;
    3038     }
    3039 
    3040     if (XSTRNCMP("SHA", evp, 3) == 0) {
    3041         if (XSTRLEN(evp) > 3) {
    3042         #ifndef NO_SHA256
    3043             if (XSTRNCMP("SHA256", evp, 6) == 0) {
    3044                 hash = WC_HASH_TYPE_SHA256;
    3045             }
    3046             else
    3047         #endif
    3048         #ifdef WOLFSSL_SHA384
    3049             if (XSTRNCMP("SHA384", evp, 6) == 0) {
    3050                 hash = WC_HASH_TYPE_SHA384;
    3051             }
    3052             else
    3053         #endif
    3054         #ifdef WOLFSSL_SHA512
    3055             if (XSTRNCMP("SHA512", evp, 6) == 0) {
    3056                 hash = WC_HASH_TYPE_SHA512;
    3057             }
    3058             else
    3059         #endif
    3060             {
    3061                 WOLFSSL_MSG("Unknown SHA hash");
    3062             }
    3063         }
    3064         else {
    3065             hash = WC_HASH_TYPE_SHA;
    3066         }
    3067     }
    3068 #ifdef WOLFSSL_MD2
    3069     else if (XSTRNCMP("MD2", evp, 3) == 0) {
    3070         hash = WC_HASH_TYPE_MD2;
    3071     }
    3072 #endif
    3073 #ifndef NO_MD4
    3074     else if (XSTRNCMP("MD4", evp, 3) == 0) {
    3075         hash = WC_HASH_TYPE_MD4;
    3076     }
    3077 #endif
    3078 #ifndef NO_MD5
    3079     else if (XSTRNCMP("MD5", evp, 3) == 0) {
    3080         hash = WC_HASH_TYPE_MD5;
    3081     }
    3082 #endif
    3083 
    3084     if (pHash)
    3085         *pHash = hash;
    3086 
    3087     hashSz = wc_HashGetDigestSize(hash);
    3088     if (pHashSz)
    3089         *pHashSz = hashSz;
    3090 
    3091     if (hashSz < 0) {
    3092         return WOLFSSL_FAILURE;
    3093     }
    3094 
    3095     return WOLFSSL_SUCCESS;
    3096 }
    3097 
    30983220/* this function makes the assumption that out buffer is big enough for digest*/
    30993221int wolfSSL_EVP_Digest(const unsigned char* in, int inSz, unsigned char* out,
     
    31363258        {"MD4", "ssl3-md4"},
    31373259        {"MD5", "ssl3-md5"},
    3138         {"SHA", "ssl3-sha1"},
    3139         {"SHA", "SHA1"},
     3260        {"SHA1", "ssl3-sha1"},
     3261        {"SHA1", "SHA"},
    31403262        { NULL, NULL}
    31413263    };
     3264    char nameUpper[15]; /* 15 bytes should be enough for any name */
     3265    size_t i;
    31423266
    31433267    const struct alias  *al;
    31443268    const struct s_ent *ent;
    31453269
    3146 
     3270    for (i = 0; i < sizeof(nameUpper) && name[i] != '\0'; i++) {
     3271        nameUpper[i] = (char)XTOUPPER(name[i]);
     3272    }
     3273    if (i < sizeof(nameUpper))
     3274        nameUpper[i] = '\0';
     3275    else
     3276        return NULL;
     3277
     3278    name = nameUpper;
    31473279    for (al = alias_tbl; al->name != NULL; al++)
    31483280        if(XSTRNCMP(name, al->alias, XSTRLEN(al->alias)+1) == 0) {
     
    32053337    {
    32063338        WOLFSSL_ENTER("EVP_sha1");
    3207         return EVP_get_digestbyname("SHA");
     3339        return EVP_get_digestbyname("SHA1");
    32083340    }
    32093341#endif /* NO_SHA */
     
    32953427        if (ctx) {
    32963428            WOLFSSL_ENTER("EVP_MD_CTX_free");
    3297                 wolfSSL_EVP_MD_CTX_cleanup(ctx);
    3298                 XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
    3299             }
     3429            wolfSSL_EVP_MD_CTX_cleanup(ctx);
     3430            XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
     3431        }
    33003432    }
    33013433
    33023434    /* returns the NID of message digest used by the ctx */
    3303     int wolfSSL_EVP_MD_CTX_type(const WOLFSSL_EVP_MD_CTX *ctx) {
     3435    int wolfSSL_EVP_MD_CTX_type(const WOLFSSL_EVP_MD_CTX *ctx)
     3436    {
    33043437        const struct s_ent *ent;
    33053438
     
    33073440
    33083441        if (ctx) {
     3442            if (ctx->isHMAC) {
     3443                return NID_hmac;
     3444            }
     3445
    33093446            for(ent = md_tbl; ent->name != NULL; ent++) {
    33103447                if (ctx->macType == ent->macType) {
     
    33393476            const WOLFSSL_EVP_MD_CTX* src)
    33403477    {
    3341         if (src->macType == NID_hmac) {
    3342             wolfSSL_HmacCopy(&des->hash.hmac, (Hmac*)&src->hash.hmac);
     3478        int ret;
     3479        if (src->isHMAC) {
     3480            ret = wolfSSL_HmacCopy(&des->hash.hmac, (Hmac*)&src->hash.hmac);
    33433481        }
    33443482        else {
    33453483            switch (src->macType) {
     3484                case WC_HASH_TYPE_MD5:
    33463485            #ifndef NO_MD5
    3347                 case WC_HASH_TYPE_MD5:
    3348                     wc_Md5Copy((wc_Md5*)&src->hash.digest,
     3486                    ret = wc_Md5Copy((wc_Md5*)&src->hash.digest,
    33493487                            (wc_Md5*)&des->hash.digest);
     3488            #else
     3489                    ret = NOT_COMPILED_IN;
     3490            #endif /* !NO_MD5 */
    33503491                    break;
    3351             #endif /* !NO_MD5 */
    3352 
     3492                case WC_HASH_TYPE_SHA:
    33533493            #ifndef NO_SHA
    3354                 case WC_HASH_TYPE_SHA:
    3355                     wc_ShaCopy((wc_Sha*)&src->hash.digest,
     3494                    ret = wc_ShaCopy((wc_Sha*)&src->hash.digest,
    33563495                            (wc_Sha*)&des->hash.digest);
     3496            #else
     3497                    ret = NOT_COMPILED_IN;
     3498            #endif /* !NO_SHA */
    33573499                    break;
    3358             #endif /* !NO_SHA */
    3359 
     3500                case WC_HASH_TYPE_SHA224:
    33603501            #ifdef WOLFSSL_SHA224
    3361                 case WC_HASH_TYPE_SHA224:
    3362                     wc_Sha224Copy((wc_Sha224*)&src->hash.digest,
     3502                    ret = wc_Sha224Copy((wc_Sha224*)&src->hash.digest,
    33633503                            (wc_Sha224*)&des->hash.digest);
     3504            #else
     3505                    ret = NOT_COMPILED_IN;
     3506            #endif /* WOLFSSL_SHA224 */
    33643507                    break;
    3365             #endif /* WOLFSSL_SHA224 */
    3366 
     3508                case WC_HASH_TYPE_SHA256:
    33673509            #ifndef NO_SHA256
    3368                 case WC_HASH_TYPE_SHA256:
    3369                     wc_Sha256Copy((wc_Sha256*)&src->hash.digest,
     3510                    ret = wc_Sha256Copy((wc_Sha256*)&src->hash.digest,
    33703511                            (wc_Sha256*)&des->hash.digest);
     3512            #else
     3513                    ret = NOT_COMPILED_IN;
     3514            #endif /* !NO_SHA256 */
    33713515                    break;
    3372             #endif /* !NO_SHA256 */
    3373 
     3516                case WC_HASH_TYPE_SHA384:
    33743517            #ifdef WOLFSSL_SHA384
    3375                 case WC_HASH_TYPE_SHA384:
    3376                     wc_Sha384Copy((wc_Sha384*)&src->hash.digest,
     3518                    ret = wc_Sha384Copy((wc_Sha384*)&src->hash.digest,
    33773519                            (wc_Sha384*)&des->hash.digest);
     3520            #else
     3521                    ret = NOT_COMPILED_IN;
     3522            #endif /* WOLFSSL_SHA384 */
    33783523                    break;
    3379             #endif /* WOLFSSL_SHA384 */
     3524                case WC_HASH_TYPE_SHA512:
    33803525            #ifdef WOLFSSL_SHA512
    3381                 case WC_HASH_TYPE_SHA512:
    3382                     wc_Sha512Copy((wc_Sha512*)&src->hash.digest,
     3526                    ret = wc_Sha512Copy((wc_Sha512*)&src->hash.digest,
    33833527                        (wc_Sha512*)&des->hash.digest);
     3528            #else
     3529                    ret = NOT_COMPILED_IN;
     3530            #endif /* WOLFSSL_SHA512 */
    33843531                    break;
    3385             #endif /* WOLFSSL_SHA512 */
    3386         #ifdef WOLFSSL_SHA3
    3387             #ifndef WOLFSSL_NOSHA3_224
    33883532                case WC_HASH_TYPE_SHA3_224:
    3389                     wc_Sha3_224_Copy((wc_Sha3*)&src->hash.digest,
     3533            #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
     3534                    ret = wc_Sha3_224_Copy((wc_Sha3*)&src->hash.digest,
    33903535                            (wc_Sha3*)&des->hash.digest);
     3536            #else
     3537                    ret = NOT_COMPILED_IN;
     3538            #endif
    33913539                    break;
     3540                case WC_HASH_TYPE_SHA3_256:
     3541            #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
     3542                    ret = wc_Sha3_256_Copy((wc_Sha3*)&src->hash.digest,
     3543                            (wc_Sha3*)&des->hash.digest);
     3544            #else
     3545                    ret = NOT_COMPILED_IN;
    33923546            #endif
    3393 
    3394             #ifndef WOLFSSL_NOSHA3_256
    3395                 case WC_HASH_TYPE_SHA3_256:
    3396                     wc_Sha3_256_Copy((wc_Sha3*)&src->hash.digest,
     3547                    break;
     3548                case WC_HASH_TYPE_SHA3_384:
     3549            #if defined(WOLFSSL_SHA3)
     3550                    ret = wc_Sha3_384_Copy((wc_Sha3*)&src->hash.digest,
    33973551                            (wc_Sha3*)&des->hash.digest);
     3552            #else
     3553                    ret = NOT_COMPILED_IN;
     3554            #endif
    33983555                    break;
     3556                case WC_HASH_TYPE_SHA3_512:
     3557            #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
     3558                    ret = wc_Sha3_512_Copy((wc_Sha3*)&src->hash.digest,
     3559                        (wc_Sha3*)&des->hash.digest);
     3560            #else
     3561                    ret = NOT_COMPILED_IN;
    33993562            #endif
    3400 
    3401                 case WC_HASH_TYPE_SHA3_384:
    3402                     wc_Sha3_384_Copy((wc_Sha3*)&src->hash.digest,
    3403                             (wc_Sha3*)&des->hash.digest);
    34043563                    break;
    3405 
    3406             #ifndef WOLFSSL_NOSHA3_512
    3407                 case WC_HASH_TYPE_SHA3_512:
    3408                     wc_Sha3_512_Copy((wc_Sha3*)&src->hash.digest,
    3409                         (wc_Sha3*)&des->hash.digest);
     3564                case WC_HASH_TYPE_NONE:
     3565                case WC_HASH_TYPE_MD2:
     3566                case WC_HASH_TYPE_MD4:
     3567                case WC_HASH_TYPE_MD5_SHA:
     3568                case WC_HASH_TYPE_BLAKE2B:
     3569                case WC_HASH_TYPE_BLAKE2S:
     3570                default:
     3571                    ret = BAD_FUNC_ARG;
    34103572                    break;
    3411             #endif
    3412         #endif
    3413                 default:
    3414                     return WOLFSSL_FAILURE;
    3415             }
    3416         }
    3417         return WOLFSSL_SUCCESS;
     3573            }
     3574        }
     3575        return ret == 0 ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
    34183576    }
    34193577
     
    34463604            return NULL;
    34473605        WOLFSSL_ENTER("EVP_MD_CTX_md");
     3606        if (ctx->isHMAC) {
     3607            return "HMAC";
     3608        }
    34483609        for(ent = md_tbl; ent->name != NULL; ent++) {
    34493610            if(ctx->macType == ent->macType) {
     
    34613622    {
    34623623        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cbc");
    3463         if (EVP_AES_128_CBC == NULL)
    3464             wolfSSL_EVP_init();
    34653624        return EVP_AES_128_CBC;
    34663625    }
     
    34723631    {
    34733632        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cbc");
    3474         if (EVP_AES_192_CBC == NULL)
    3475             wolfSSL_EVP_init();
    34763633        return EVP_AES_192_CBC;
    34773634    }
     
    34833640    {
    34843641        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cbc");
    3485         if (EVP_AES_256_CBC == NULL)
    3486             wolfSSL_EVP_init();
    34873642        return EVP_AES_256_CBC;
    34883643    }
     
    34963651    {
    34973652        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cfb1");
    3498         if (EVP_AES_128_CFB1 == NULL)
    3499             wolfSSL_EVP_init();
    35003653        return EVP_AES_128_CFB1;
    35013654    }
     
    35063659    {
    35073660        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cfb1");
    3508         if (EVP_AES_192_CFB1 == NULL)
    3509             wolfSSL_EVP_init();
    35103661        return EVP_AES_192_CFB1;
    35113662    }
     
    35163667    {
    35173668        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cfb1");
    3518         if (EVP_AES_256_CFB1 == NULL)
    3519             wolfSSL_EVP_init();
    35203669        return EVP_AES_256_CFB1;
    35213670    }
     
    35263675    {
    35273676        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cfb8");
    3528         if (EVP_AES_128_CFB8 == NULL)
    3529             wolfSSL_EVP_init();
    35303677        return EVP_AES_128_CFB8;
    35313678    }
     
    35363683    {
    35373684        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cfb8");
    3538         if (EVP_AES_192_CFB8 == NULL)
    3539             wolfSSL_EVP_init();
    35403685        return EVP_AES_192_CFB8;
    35413686    }
     
    35463691    {
    35473692        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cfb8");
    3548         if (EVP_AES_256_CFB8 == NULL)
    3549             wolfSSL_EVP_init();
    35503693        return EVP_AES_256_CFB8;
    35513694    }
     
    35573700    {
    35583701        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cfb128");
    3559         if (EVP_AES_128_CFB128 == NULL)
    3560             wolfSSL_EVP_init();
    35613702        return EVP_AES_128_CFB128;
    35623703    }
     
    35673708    {
    35683709        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cfb128");
    3569         if (EVP_AES_192_CFB128 == NULL)
    3570             wolfSSL_EVP_init();
    35713710        return EVP_AES_192_CFB128;
    35723711    }
     
    35773716    {
    35783717        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cfb128");
    3579         if (EVP_AES_256_CFB128 == NULL)
    3580             wolfSSL_EVP_init();
    35813718        return EVP_AES_256_CFB128;
    35823719    }
     
    35893726    {
    35903727        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ofb");
    3591         if (EVP_AES_128_OFB == NULL)
    3592             wolfSSL_EVP_init();
    35933728        return EVP_AES_128_OFB;
    35943729    }
     
    35993734    {
    36003735        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ofb");
    3601         if (EVP_AES_192_OFB == NULL)
    3602             wolfSSL_EVP_init();
    36033736        return EVP_AES_192_OFB;
    36043737    }
     
    36093742    {
    36103743        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ofb");
    3611         if (EVP_AES_256_OFB == NULL)
    3612             wolfSSL_EVP_init();
    36133744        return EVP_AES_256_OFB;
    36143745    }
     
    36213752    {
    36223753        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_xts");
    3623         if (EVP_AES_128_XTS == NULL)
    3624             wolfSSL_EVP_init();
    36253754        return EVP_AES_128_XTS;
    36263755    }
     
    36313760    {
    36323761        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_xts");
    3633         if (EVP_AES_256_XTS == NULL)
    3634             wolfSSL_EVP_init();
    36353762        return EVP_AES_256_XTS;
    36363763    }
     
    36433770    {
    36443771        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_gcm");
    3645         if (EVP_AES_128_GCM == NULL)
    3646             wolfSSL_EVP_init();
    36473772        return EVP_AES_128_GCM;
    36483773    }
     
    36533778    {
    36543779        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_gcm");
    3655         if (EVP_AES_192_GCM == NULL)
    3656             wolfSSL_EVP_init();
    36573780        return EVP_AES_192_GCM;
    36583781    }
     
    36633786    {
    36643787        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_gcm");
    3665         if (EVP_AES_256_GCM == NULL)
    3666             wolfSSL_EVP_init();
    36673788        return EVP_AES_256_GCM;
    36683789    }
     
    36703791    #endif /* HAVE_AESGCM */
    36713792
     3793    #ifdef WOLFSSL_AES_COUNTER
    36723794    #ifdef WOLFSSL_AES_128
    36733795    const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void)
    36743796    {
    36753797        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ctr");
    3676         if (EVP_AES_128_CTR == NULL)
    3677             wolfSSL_EVP_init();
    36783798        return EVP_AES_128_CTR;
    36793799    }
     
    36853805    {
    36863806        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ctr");
    3687         if (EVP_AES_192_CTR == NULL)
    3688             wolfSSL_EVP_init();
    36893807        return EVP_AES_192_CTR;
    36903808    }
     
    36963814    {
    36973815        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ctr");
    3698         if (EVP_AES_256_CTR == NULL)
    3699             wolfSSL_EVP_init();
    37003816        return EVP_AES_256_CTR;
    37013817    }
    37023818    #endif /* WOLFSSL_AES_256 */
    3703 
     3819    #endif /* WOLFSSL_AES_COUNTER */
     3820
     3821    #ifdef HAVE_AES_ECB
    37043822    #ifdef WOLFSSL_AES_128
    37053823    const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ecb(void)
    37063824    {
    37073825        WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ecb");
    3708         if (EVP_AES_128_ECB == NULL)
    3709             wolfSSL_EVP_init();
    37103826        return EVP_AES_128_ECB;
    37113827    }
     
    37173833    {
    37183834        WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ecb");
    3719         if (EVP_AES_192_ECB == NULL)
    3720             wolfSSL_EVP_init();
    37213835        return EVP_AES_192_ECB;
    37223836    }
     
    37283842    {
    37293843        WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ecb");
    3730         if (EVP_AES_256_ECB == NULL)
    3731             wolfSSL_EVP_init();
    37323844        return EVP_AES_256_ECB;
    37333845    }
    37343846    #endif /* WOLFSSL_AES_256 */
     3847    #endif /* HAVE_AES_ECB */
    37353848    #endif /* NO_AES */
    37363849
     
    37393852    {
    37403853        WOLFSSL_ENTER("wolfSSL_EVP_des_cbc");
    3741         if (EVP_DES_CBC == NULL)
    3742             wolfSSL_EVP_init();
    37433854        return EVP_DES_CBC;
    37443855    }
     
    37473858    {
    37483859        WOLFSSL_ENTER("wolfSSL_EVP_des_ecb");
    3749         if (EVP_DES_ECB == NULL)
    3750             wolfSSL_EVP_init();
    37513860        return EVP_DES_ECB;
    37523861    }
     
    37553864    {
    37563865        WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_cbc");
    3757         if (EVP_DES_EDE3_CBC == NULL)
    3758             wolfSSL_EVP_init();
    37593866        return EVP_DES_EDE3_CBC;
    37603867    }
     
    37633870    {
    37643871        WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_ecb");
    3765         if (EVP_DES_EDE3_ECB == NULL)
    3766             wolfSSL_EVP_init();
    37673872        return EVP_DES_EDE3_ECB;
    37683873    }
     
    37733878    const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void)
    37743879    {
    3775         static const char* type = "ARC4";
    37763880        WOLFSSL_ENTER("wolfSSL_EVP_rc4");
    3777         return type;
     3881        return EVP_ARC4;
    37783882    }
    37793883#endif
     
    37833887    {
    37843888        WOLFSSL_ENTER("wolfSSL_EVP_idea_cbc");
    3785         if (EVP_IDEA_CBC == NULL)
    3786             wolfSSL_EVP_init();
    37873889        return EVP_IDEA_CBC;
    37883890    }
     
    37903892    const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void)
    37913893    {
    3792         static const char* type = "NULL";
    37933894        WOLFSSL_ENTER("wolfSSL_EVP_enc_null");
    3794         return type;
     3895        return EVP_NULL;
    37953896    }
    37963897
    37973898    int wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx)
    37983899    {
     3900        int ret = WOLFSSL_SUCCESS;
    37993901        WOLFSSL_ENTER("EVP_MD_CTX_cleanup");
    38003902        if (ctx->pctx != NULL)
    38013903            wolfSSL_EVP_PKEY_CTX_free(ctx->pctx);
    38023904
    3803         if (ctx->macType == NID_hmac) {
     3905        if (ctx->isHMAC) {
    38043906            wc_HmacFree(&ctx->hash.hmac);
    38053907        }
    38063908        else {
    38073909            switch (ctx->macType) {
     3910                case WC_HASH_TYPE_MD5:
    38083911            #ifndef NO_MD5
    3809                 case WC_HASH_TYPE_MD5:
    38103912                    wc_Md5Free((wc_Md5*)&ctx->hash.digest);
     3913            #endif /* !NO_MD5 */
    38113914                    break;
    3812             #endif /* !NO_MD5 */
    3813 
     3915                case WC_HASH_TYPE_SHA:
    38143916            #ifndef NO_SHA
    3815                 case WC_HASH_TYPE_SHA:
    38163917                    wc_ShaFree((wc_Sha*)&ctx->hash.digest);
     3918            #endif /* !NO_SHA */
    38173919                    break;
    3818             #endif /* !NO_SHA */
    3819 
     3920                case WC_HASH_TYPE_SHA224:
    38203921            #ifdef WOLFSSL_SHA224
    3821                 case WC_HASH_TYPE_SHA224:
    38223922                    wc_Sha224Free((wc_Sha224*)&ctx->hash.digest);
     3923            #endif /* WOLFSSL_SHA224 */
    38233924                    break;
    3824             #endif /* WOLFSSL_SHA224 */
    3825 
     3925                case WC_HASH_TYPE_SHA256:
    38263926            #ifndef NO_SHA256
    3827                 case WC_HASH_TYPE_SHA256:
    38283927                    wc_Sha256Free((wc_Sha256*)&ctx->hash.digest);
     3928            #endif /* !NO_SHA256 */
    38293929                    break;
    3830             #endif /* !NO_SHA256 */
    3831 
     3930                case WC_HASH_TYPE_SHA384:
    38323931            #ifdef WOLFSSL_SHA384
    3833                 case WC_HASH_TYPE_SHA384:
    38343932                    wc_Sha384Free((wc_Sha384*)&ctx->hash.digest);
     3933            #endif /* WOLFSSL_SHA384 */
    38353934                    break;
    3836             #endif /* WOLFSSL_SHA384 */
     3935                case WC_HASH_TYPE_SHA512:
    38373936            #ifdef WOLFSSL_SHA512
    3838                 case WC_HASH_TYPE_SHA512:
    38393937                    wc_Sha512Free((wc_Sha512*)&ctx->hash.digest);
     3938            #endif /* WOLFSSL_SHA512 */
    38403939                    break;
    3841             #endif /* WOLFSSL_SHA512 */
    3842         #ifdef WOLFSSL_SHA3
    3843             #ifndef WOLFSSL_NOSHA3_224
    38443940                case WC_HASH_TYPE_SHA3_224:
     3941            #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
    38453942                    wc_Sha3_224_Free((wc_Sha3*)&ctx->hash.digest);
     3943            #endif
    38463944                    break;
     3945                case WC_HASH_TYPE_SHA3_256:
     3946            #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
     3947                    wc_Sha3_256_Free((wc_Sha3*)&ctx->hash.digest);
    38473948            #endif
    3848 
    3849             #ifndef WOLFSSL_NOSHA3_256
    3850                 case WC_HASH_TYPE_SHA3_256:
    3851                     wc_Sha3_256_Free((wc_Sha3*)&ctx->hash.digest);
    38523949                    break;
     3950                case WC_HASH_TYPE_SHA3_384:
     3951            #if defined(WOLFSSL_SHA3)
     3952                    wc_Sha3_384_Free((wc_Sha3*)&ctx->hash.digest);
    38533953            #endif
    3854 
    3855                 case WC_HASH_TYPE_SHA3_384:
    3856                     wc_Sha3_384_Free((wc_Sha3*)&ctx->hash.digest);
    38573954                    break;
    3858 
    3859             #ifndef WOLFSSL_NOSHA3_512
    38603955                case WC_HASH_TYPE_SHA3_512:
     3956            #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
    38613957                    wc_Sha3_512_Free((wc_Sha3*)&ctx->hash.digest);
     3958            #endif
    38623959                    break;
    3863             #endif
    3864         #endif
     3960                case WC_HASH_TYPE_NONE:
     3961                case WC_HASH_TYPE_MD2:
     3962                case WC_HASH_TYPE_MD4:
     3963                case WC_HASH_TYPE_MD5_SHA:
     3964                case WC_HASH_TYPE_BLAKE2B:
     3965                case WC_HASH_TYPE_BLAKE2S:
    38653966                default:
    3866                     return WOLFSSL_FAILURE;
     3967                    ret = WOLFSSL_FAILURE;
     3968                    break;
    38673969            }
    38683970        }
    38693971        ForceZero(ctx, sizeof(*ctx));
    38703972        ctx->macType = WC_HASH_TYPE_NONE;
    3871         return 1;
     3973        return ret;
    38723974    }
    38733975
     
    40114113            ctx->cipherType = WOLFSSL_EVP_CIPH_TYPE_INIT;  /* not yet initialized  */
    40124114            ctx->keyLen     = 0;
     4115#ifdef HAVE_AESGCM
     4116            if (ctx->gcmBuffer) {
     4117                XFREE(ctx->gcmBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
     4118                ctx->gcmBuffer = NULL;
     4119            }
     4120            ctx->gcmBufferLen = 0;
     4121            if (ctx->gcmAuthIn) {
     4122                XFREE(ctx->gcmAuthIn, NULL, DYNAMIC_TYPE_OPENSSL);
     4123                ctx->gcmAuthIn = NULL;
     4124            }
     4125            ctx->gcmAuthInSz = 0;
     4126#endif
    40134127        }
    40144128
     
    40814195#endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
    40824196
     4197
    40834198#ifndef NO_AES
     4199#if defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_192) || \
     4200    defined(WOLFSSL_AES_256)
     4201    #define AES_SIZE_ANY
     4202#endif
     4203
     4204#if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || \
     4205    defined(HAVE_AES_ECB) || defined(WOLFSSL_AES_CFB) || \
     4206    defined(WOLFSSSL_AES_OFB)
     4207    #define AES_SET_KEY
     4208#endif
     4209
     4210#if defined(AES_SIZE_ANY) && defined(AES_SET_KEY)
    40844211    static int   AesSetKey_ex(Aes* aes, const byte* key, word32 len,
    40854212                              const byte* iv, int dir, int direct)
     
    41044231        return ret;
    41054232    }
    4106 #endif
     4233#endif /* AES_ANY_SIZE && AES_SET_KEY */
     4234#endif /* NO_AES */
    41074235
    41084236    /* return WOLFSSL_SUCCESS on ok, 0 on failure to match API compatibility */
     
    41394267            iv = ctx->iv;
    41404268        }
     4269#endif
     4270#ifdef HAVE_AESGCM
     4271        if (ctx->gcmAuthIn) {
     4272            XFREE(ctx->gcmAuthIn, NULL, DYNAMIC_TYPE_OPENSSL);
     4273            ctx->gcmAuthIn = NULL;
     4274        }
     4275        ctx->gcmAuthInSz = 0;
    41414276#endif
    41424277
     
    42234358        #endif /* WOLFSSL_AES_256 */
    42244359    #endif /* HAVE_AES_CBC */
    4225 #if !defined(_WIN32) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
     4360#if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
     4361    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
    42264362    #ifdef HAVE_AESGCM
    42274363        #ifdef WOLFSSL_AES_128
     
    42314367            ctx->cipherType = AES_128_GCM_TYPE;
    42324368            ctx->flags     &= ~WOLFSSL_EVP_CIPH_MODE;
    4233             ctx->flags     |= WOLFSSL_EVP_CIPH_GCM_MODE;
     4369            ctx->flags     |= WOLFSSL_EVP_CIPH_GCM_MODE |
     4370                    WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
    42344371            ctx->keyLen     = 16;
    42354372            ctx->block_size = AES_BLOCK_SIZE;
     
    42374374            ctx->ivSz       = GCM_NONCE_MID_SZ;
    42384375
    4239             XMEMSET(ctx->authTag, 0, ctx->authTagSz);
    42404376            if (key && wc_AesGcmSetKey(&ctx->cipher.aes, key, ctx->keyLen)) {
    42414377                WOLFSSL_MSG("wc_AesGcmSetKey() failed");
     
    42564392            ctx->cipherType = AES_192_GCM_TYPE;
    42574393            ctx->flags     &= ~WOLFSSL_EVP_CIPH_MODE;
    4258             ctx->flags     |= WOLFSSL_EVP_CIPH_GCM_MODE;
     4394            ctx->flags     |= WOLFSSL_EVP_CIPH_GCM_MODE |
     4395                    WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
    42594396            ctx->keyLen     = 24;
    42604397            ctx->block_size = AES_BLOCK_SIZE;
     
    42624399            ctx->ivSz       = GCM_NONCE_MID_SZ;
    42634400
    4264             XMEMSET(ctx->authTag, 0, ctx->authTagSz);
    42654401            if (key && wc_AesGcmSetKey(&ctx->cipher.aes, key, ctx->keyLen)) {
    42664402                WOLFSSL_MSG("wc_AesGcmSetKey() failed");
     
    42814417            ctx->cipherType = AES_256_GCM_TYPE;
    42824418            ctx->flags     &= ~WOLFSSL_EVP_CIPH_MODE;
    4283             ctx->flags     |= WOLFSSL_EVP_CIPH_GCM_MODE;
     4419            ctx->flags     |= WOLFSSL_EVP_CIPH_GCM_MODE |
     4420                    WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
    42844421            ctx->keyLen     = 32;
    42854422            ctx->block_size = AES_BLOCK_SIZE;
     
    42874424            ctx->ivSz       = GCM_NONCE_MID_SZ;
    42884425
    4289             XMEMSET(ctx->authTag, 0, ctx->authTagSz);
    42904426            if (key && wc_AesGcmSetKey(&ctx->cipher.aes, key, ctx->keyLen)) {
    42914427                WOLFSSL_MSG("wc_AesGcmSetKey() failed");
     
    43014437        #endif /* WOLFSSL_AES_256 */
    43024438    #endif /* HAVE_AESGCM */
    4303 #endif /* !defined(_WIN32) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) */
     4439#endif /*!HAVE_FIPS && !HAVE_SELFTEST ||(HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2)*/
    43044440#ifdef WOLFSSL_AES_COUNTER
    43054441        #ifdef WOLFSSL_AES_128
     
    43884524        #endif /* WOLFSSL_AES_256 */
    43894525#endif /* WOLFSSL_AES_COUNTER */
     4526    #ifdef HAVE_AES_ECB
    43904527        #ifdef WOLFSSL_AES_128
    43914528        if (ctx->cipherType == AES_128_ECB_TYPE ||
     
    44454582        }
    44464583        #endif /* WOLFSSL_AES_256 */
     4584    #endif /* HAVE_AES_ECB */
    44474585    #ifdef WOLFSSL_AES_CFB
    44484586        #ifdef WOLFSSL_AES_128
     
    46744812        }
    46754813        #endif /* WOLFSSL_AES_256 */
    4676     #endif /* HAVE_AES_CFB */
     4814    #endif /* WOLFSSL_AES_CFB */
    46774815    #ifdef WOLFSSL_AES_OFB
    46784816        #ifdef WOLFSSL_AES_128
     
    49045042#endif /* NO_DES3 */
    49055043#ifndef NO_RC4
    4906         if (ctx->cipherType == ARC4_TYPE || (type &&
    4907                                      XSTRNCMP(type, "ARC4", 4) == 0)) {
     5044        if (ctx->cipherType == ARC4_TYPE ||
     5045                (type && XSTRNCMP(type, EVP_ARC4, 4) == 0)) {
    49085046            WOLFSSL_MSG("ARC4");
    49095047            ctx->cipherType = ARC4_TYPE;
     
    49415079        }
    49425080#endif /* HAVE_IDEA */
    4943         if (ctx->cipherType == NULL_CIPHER_TYPE || (type &&
    4944                                      XSTRNCMP(type, "NULL", 4) == 0)) {
     5081        if (ctx->cipherType == NULL_CIPHER_TYPE ||
     5082                (type && XSTRNCMP(type, EVP_NULL, 4) == 0)) {
    49455083            WOLFSSL_MSG("NULL cipher");
    49465084            ctx->cipherType = NULL_CIPHER_TYPE;
     
    50175155#endif
    50185156
    5019     /* WOLFSSL_SUCCESS on ok */
     5157    /* Return length on ok */
    50205158    int wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
    50215159                          word32 len)
     
    50305168             ctx->cipherType != AES_256_GCM_TYPE)) {
    50315169            WOLFSSL_MSG("Bad function argument");
    5032             return 0;  /* failure */
     5170            return WOLFSSL_FATAL_ERROR;
    50335171        }
    50345172
    50355173        if (ctx->cipherType == 0xff) {
    50365174            WOLFSSL_MSG("no init");
    5037             return 0;  /* failure */
     5175            return WOLFSSL_FATAL_ERROR;
    50385176        }
    50395177
     
    50505188                else
    50515189                    ret = wc_AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
     5190                if (ret == 0)
     5191                    ret = (len / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    50525192                break;
    50535193#endif /* HAVE_AES_CBC */
     
    50635203                else
    50645204                    ret = wc_AesCfb1Decrypt(&ctx->cipher.aes, dst, src, len);
     5205                if (ret == 0)
     5206                    ret = len;
    50655207                break;
    50665208            case AES_128_CFB8_TYPE:
     
    50725214                else
    50735215                    ret = wc_AesCfb8Decrypt(&ctx->cipher.aes, dst, src, len);
     5216                if (ret == 0)
     5217                    ret = len;
    50745218                break;
    50755219#endif /* !HAVE_SELFTEST && !HAVE_FIPS */
     
    50825226                else
    50835227                    ret = wc_AesCfbDecrypt(&ctx->cipher.aes, dst, src, len);
     5228                if (ret == 0)
     5229                    ret = len;
    50845230                break;
    50855231#endif /* WOLFSSL_AES_CFB */
     
    50935239                else
    50945240                    ret = wc_AesOfbDecrypt(&ctx->cipher.aes, dst, src, len);
     5241                if (ret == 0)
     5242                    ret = len;
    50955243                break;
    50965244#endif /* WOLFSSL_AES_OFB */
     
    51055253                    ret = wc_AesXtsDecrypt(&ctx->cipher.xts, dst, src, len,
    51065254                            ctx->iv, ctx->ivSz);
     5255                if (ret == 0)
     5256                    ret = len;
    51075257                break;
    51085258#endif /* WOLFSSL_AES_XTS */
     
    51135263            case AES_256_GCM_TYPE :
    51145264                WOLFSSL_MSG("AES GCM");
    5115                 if (ctx->enc) {
    5116                     if (dst){
    5117                         /* encrypt confidential data*/
    5118                         ret = wc_AesGcmEncrypt(&ctx->cipher.aes, dst, src, len,
    5119                                   ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
    5120                                   NULL, 0);
    5121                     }
    5122                     else {
    5123                         /* authenticated, non-confidential data */
    5124                         ret = wc_AesGcmEncrypt(&ctx->cipher.aes, NULL, NULL, 0,
    5125                                   ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
    5126                                   src, len);
    5127                         /* Reset partial authTag error for AAD*/
    5128                         if (ret == AES_GCM_AUTH_E)
    5129                             ret = 0;
    5130                     }
     5265                if (!dst) {
     5266                    ret = wolfSSL_EVP_CipherUpdate_GCM_AAD(ctx, src, len);
    51315267                }
    51325268                else {
    5133                     if (dst){
    5134                         /* decrypt confidential data*/
    5135                         ret = wc_AesGcmDecrypt(&ctx->cipher.aes, dst, src, len,
    5136                                   ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
    5137                                   NULL, 0);
    5138                     }
    5139                     else {
    5140                         /* authenticated, non-confidential data*/
    5141                         ret = wc_AesGcmDecrypt(&ctx->cipher.aes, NULL, NULL, 0,
    5142                                   ctx->iv, ctx->ivSz,
    5143                                   ctx->authTag, ctx->authTagSz,
    5144                                   src, len);
    5145                         /* Reset partial authTag error for AAD*/
    5146                         if (ret == AES_GCM_AUTH_E)
    5147                             ret = 0;
    5148                     }
     5269                    if (ctx->enc)
     5270                        ret = wc_AesGcmEncrypt(&ctx->cipher.aes, dst, src,
     5271                                len, ctx->iv, ctx->ivSz, ctx->authTag,
     5272                                ctx->authTagSz, ctx->gcmAuthIn, ctx->gcmAuthInSz);
     5273                    else
     5274                        ret = wc_AesGcmDecrypt(&ctx->cipher.aes, dst, src,
     5275                                len, ctx->iv, ctx->ivSz, ctx->authTag,
     5276                                ctx->authTagSz, ctx->gcmAuthIn, ctx->gcmAuthInSz);
    51495277                }
     5278                if (ret == 0)
     5279                    ret = len;
    51505280                break;
    51515281#endif /* HAVE_AESGCM */
     
    51595289                else
    51605290                    ret = wc_AesEcbDecrypt(&ctx->cipher.aes, dst, src, len);
     5291                if (ret == 0)
     5292                    ret = (len / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    51615293                break;
    51625294#endif
     
    51655297            case AES_192_CTR_TYPE :
    51665298            case AES_256_CTR_TYPE :
    5167                     WOLFSSL_MSG("AES CTR");
    5168                     ret = wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
     5299                WOLFSSL_MSG("AES CTR");
     5300                ret = wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
     5301                if (ret == 0)
     5302                    ret = len;
    51695303                break;
    51705304#endif /* WOLFSSL_AES_COUNTER */
     
    51785312                else
    51795313                    wc_Des_CbcDecrypt(&ctx->cipher.des, dst, src, len);
     5314                if (ret == 0)
     5315                    ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
    51805316                break;
    51815317            case DES_EDE3_CBC_TYPE :
     
    51855321                else
    51865322                    ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len);
     5323                if (ret == 0)
     5324                    ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
    51875325                break;
    51885326#ifdef WOLFSSL_DES_ECB
     
    51905328                WOLFSSL_MSG("DES ECB");
    51915329                ret = wc_Des_EcbEncrypt(&ctx->cipher.des, dst, src, len);
     5330                if (ret == 0)
     5331                    ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
    51925332                break;
    51935333            case DES_EDE3_ECB_TYPE :
    51945334                WOLFSSL_MSG("DES3 ECB");
    51955335                ret = wc_Des3_EcbEncrypt(&ctx->cipher.des3, dst, src, len);
     5336                if (ret == 0)
     5337                    ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
    51965338                break;
    51975339#endif
     
    52025344                WOLFSSL_MSG("ARC4");
    52035345                wc_Arc4Process(&ctx->cipher.arc4, dst, src, len);
     5346                if (ret == 0)
     5347                    ret = len;
    52045348                break;
    52055349#endif
     
    52125356                else
    52135357                    wc_IdeaCbcDecrypt(&ctx->cipher.idea, dst, src, len);
     5358                if (ret == 0)
     5359                    ret = (len / IDEA_BLOCK_SIZE) * IDEA_BLOCK_SIZE;
    52145360                break;
    52155361#endif
     
    52175363                WOLFSSL_MSG("NULL CIPHER");
    52185364                XMEMCPY(dst, src, len);
     5365                ret = len;
    52195366                break;
    52205367
    52215368            default: {
    52225369                WOLFSSL_MSG("bad type");
    5223                 return 0;  /* failure */
    5224             }
    5225         }
    5226 
    5227         if (ret != 0) {
     5370                return WOLFSSL_FATAL_ERROR;
     5371            }
     5372        }
     5373
     5374        if (ret < 0) {
    52285375            WOLFSSL_MSG("wolfSSL_EVP_Cipher failure");
    5229             return 0;  /* failure */
     5376            return WOLFSSL_FATAL_ERROR;
    52305377        }
    52315378
    52325379        if (wolfSSL_StoreExternalIV(ctx) != WOLFSSL_SUCCESS) {
    5233             return WOLFSSL_FAILURE;
     5380            return WOLFSSL_FATAL_ERROR;
    52345381        }
    52355382
    52365383        WOLFSSL_MSG("wolfSSL_EVP_Cipher success");
    5237         return WOLFSSL_SUCCESS;  /* success */
     5384        return ret;
    52385385    }
    52395386
     
    52465393        WOLFSSL_ENTER("EVP_DigestInit");
    52475394
    5248         if (ctx == NULL || md == NULL) {
     5395        if (ctx == NULL) {
    52495396            return BAD_FUNC_ARG;
    52505397        }
     
    52605407        /* Set to 0 if no match */
    52615408        ctx->macType = wolfSSL_EVP_md2macType(md);
    5262         if (XSTRNCMP(md, "SHA256", 6) == 0) {
     5409        if (md == NULL) {
     5410             XMEMSET(&ctx->hash.digest, 0, sizeof(WOLFSSL_Hasher));
     5411        }
     5412        else if (XSTRNCMP(md, "SHA256", 6) == 0) {
    52635413             ret = wolfSSL_SHA256_Init(&(ctx->hash.digest.sha256));
    52645414        }
     
    53265476                                size_t sz)
    53275477    {
    5328         int macType;
     5478        int ret = WOLFSSL_FAILURE;
     5479        enum wc_HashType macType;
    53295480
    53305481        WOLFSSL_ENTER("EVP_DigestUpdate");
     
    53325483        macType = wolfSSL_EVP_md2macType(EVP_MD_CTX_md(ctx));
    53335484        switch (macType) {
    5334 #ifndef NO_MD4
    53355485            case WC_HASH_TYPE_MD4:
     5486        #ifndef NO_MD4
    53365487                wolfSSL_MD4_Update((MD4_CTX*)&ctx->hash, data,
    53375488                                  (unsigned long)sz);
     5489                ret = WOLFSSL_SUCCESS;
     5490        #endif
    53385491                break;
    5339 #endif
    5340 #ifndef NO_MD5
    53415492            case WC_HASH_TYPE_MD5:
    5342                 wolfSSL_MD5_Update((MD5_CTX*)&ctx->hash, data,
     5493        #ifndef NO_MD5
     5494                ret = wolfSSL_MD5_Update((MD5_CTX*)&ctx->hash, data,
    53435495                                  (unsigned long)sz);
     5496        #endif
    53445497                break;
    5345 #endif
    5346 #ifndef NO_SHA
    53475498            case WC_HASH_TYPE_SHA:
    5348                 wolfSSL_SHA_Update((SHA_CTX*)&ctx->hash, data,
     5499        #ifndef NO_SHA
     5500                ret = wolfSSL_SHA_Update((SHA_CTX*)&ctx->hash, data,
    53495501                                  (unsigned long)sz);
     5502        #endif
    53505503                break;
    5351 #endif
    5352 #ifdef WOLFSSL_SHA224
    53535504            case WC_HASH_TYPE_SHA224:
    5354                 wolfSSL_SHA224_Update((SHA224_CTX*)&ctx->hash, data,
     5505        #ifdef WOLFSSL_SHA224
     5506                ret = wolfSSL_SHA224_Update((SHA224_CTX*)&ctx->hash, data,
    53555507                                     (unsigned long)sz);
     5508        #endif
    53565509                break;
    5357 #endif
    5358 #ifndef NO_SHA256
    53595510            case WC_HASH_TYPE_SHA256:
    5360                 wolfSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
     5511        #ifndef NO_SHA256
     5512                ret = wolfSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
    53615513                                     (unsigned long)sz);
     5514        #endif /* !NO_SHA256 */
    53625515                break;
    5363 #endif /* !NO_SHA256 */
    5364 #ifdef WOLFSSL_SHA384
    53655516            case WC_HASH_TYPE_SHA384:
    5366                 wolfSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
     5517        #ifdef WOLFSSL_SHA384
     5518                ret = wolfSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
    53675519                                     (unsigned long)sz);
     5520        #endif
    53685521                break;
    5369 #endif
    5370 #ifdef WOLFSSL_SHA512
    53715522            case WC_HASH_TYPE_SHA512:
    5372                 wolfSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
     5523        #ifdef WOLFSSL_SHA512
     5524                ret = wolfSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
    53735525                                     (unsigned long)sz);
     5526        #endif /* WOLFSSL_SHA512 */
    53745527                break;
    5375 #endif /* WOLFSSL_SHA512 */
    5376     #ifdef WOLFSSL_SHA3
    5377         #ifndef WOLFSSL_NOSHA3_224
    53785528            case WC_HASH_TYPE_SHA3_224:
    5379                 wolfSSL_SHA3_224_Update((SHA3_224_CTX*)&ctx->hash, data,
     5529        #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
     5530                ret = wolfSSL_SHA3_224_Update((SHA3_224_CTX*)&ctx->hash, data,
    53805531                                     (unsigned long)sz);
     5532        #endif
    53815533                break;
     5534            case WC_HASH_TYPE_SHA3_256:
     5535        #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
     5536                ret = wolfSSL_SHA3_256_Update((SHA3_256_CTX*)&ctx->hash, data,
     5537                                     (unsigned long)sz);
    53825538        #endif
    5383         #ifndef WOLFSSL_NOSHA3_256
    5384             case WC_HASH_TYPE_SHA3_256:
    5385                 wolfSSL_SHA3_256_Update((SHA3_256_CTX*)&ctx->hash, data,
     5539                break;
     5540            case WC_HASH_TYPE_SHA3_384:
     5541        #if defined(WOLFSSL_SHA3)
     5542                ret = wolfSSL_SHA3_384_Update((SHA3_384_CTX*)&ctx->hash, data,
    53865543                                     (unsigned long)sz);
     5544        #endif
    53875545                break;
     5546            case WC_HASH_TYPE_SHA3_512:
     5547        #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
     5548                ret = wolfSSL_SHA3_512_Update((SHA3_512_CTX*)&ctx->hash, data,
     5549                                     (unsigned long)sz);
    53885550        #endif
    5389             case WC_HASH_TYPE_SHA3_384:
    5390                 wolfSSL_SHA3_384_Update((SHA3_384_CTX*)&ctx->hash, data,
    5391                                      (unsigned long)sz);
    53925551                break;
    5393         #ifndef WOLFSSL_NOSHA3_512
    5394             case WC_HASH_TYPE_SHA3_512:
    5395                 wolfSSL_SHA3_512_Update((SHA3_512_CTX*)&ctx->hash, data,
    5396                                      (unsigned long)sz);
    5397                 break;
    5398         #endif
    5399     #endif
     5552            case WC_HASH_TYPE_NONE:
     5553            case WC_HASH_TYPE_MD2:
     5554            case WC_HASH_TYPE_MD5_SHA:
     5555            case WC_HASH_TYPE_BLAKE2B:
     5556            case WC_HASH_TYPE_BLAKE2S:
    54005557            default:
    54015558                return WOLFSSL_FAILURE;
    54025559        }
    54035560
    5404         return WOLFSSL_SUCCESS;
     5561        return ret;
    54055562    }
    54065563
     
    54095566                               unsigned int* s)
    54105567    {
    5411         int macType;
     5568        int ret = WOLFSSL_FAILURE;
     5569        enum wc_HashType macType;
    54125570
    54135571        WOLFSSL_ENTER("EVP_DigestFinal");
    54145572        macType = wolfSSL_EVP_md2macType(EVP_MD_CTX_md(ctx));
    54155573        switch (macType) {
    5416 #ifndef NO_MD4
    54175574            case WC_HASH_TYPE_MD4:
     5575        #ifndef NO_MD4
    54185576                wolfSSL_MD4_Final(md, (MD4_CTX*)&ctx->hash);
    54195577                if (s) *s = MD4_DIGEST_SIZE;
     5578                ret = WOLFSSL_SUCCESS;
     5579        #endif
    54205580                break;
    5421 #endif
    5422 #ifndef NO_MD5
    54235581            case WC_HASH_TYPE_MD5:
    5424                 wolfSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
     5582        #ifndef NO_MD5
     5583                ret = wolfSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
    54255584                if (s) *s = WC_MD5_DIGEST_SIZE;
     5585        #endif
    54265586                break;
    5427 #endif
    5428 #ifndef NO_SHA
    54295587            case WC_HASH_TYPE_SHA:
    5430                 wolfSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
     5588        #ifndef NO_SHA
     5589                ret = wolfSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
    54315590                if (s) *s = WC_SHA_DIGEST_SIZE;
     5591        #endif
    54325592                break;
    5433 #endif
    5434 #ifdef WOLFSSL_SHA224
    54355593            case WC_HASH_TYPE_SHA224:
    5436                 wolfSSL_SHA224_Final(md, (SHA224_CTX*)&ctx->hash);
     5594        #ifdef WOLFSSL_SHA224
     5595                ret = wolfSSL_SHA224_Final(md, (SHA224_CTX*)&ctx->hash);
    54375596                if (s) *s = WC_SHA224_DIGEST_SIZE;
     5597        #endif
    54385598                break;
    5439 #endif
    5440 #ifndef NO_SHA256
    54415599            case WC_HASH_TYPE_SHA256:
    5442                 wolfSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
     5600        #ifndef NO_SHA256
     5601                ret = wolfSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
    54435602                if (s) *s = WC_SHA256_DIGEST_SIZE;
     5603        #endif /* !NO_SHA256 */
    54445604                break;
    5445 #endif /* !NO_SHA256 */
    5446 #ifdef WOLFSSL_SHA384
    54475605            case WC_HASH_TYPE_SHA384:
    5448                 wolfSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
     5606        #ifdef WOLFSSL_SHA384
     5607                ret = wolfSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
    54495608                if (s) *s = WC_SHA384_DIGEST_SIZE;
     5609        #endif
    54505610                break;
    5451 #endif
    5452 #ifdef WOLFSSL_SHA512
    54535611            case WC_HASH_TYPE_SHA512:
    5454                 wolfSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
     5612        #ifdef WOLFSSL_SHA512
     5613                ret = wolfSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
    54555614                if (s) *s = WC_SHA512_DIGEST_SIZE;
     5615        #endif /* WOLFSSL_SHA512 */
    54565616                break;
    5457 #endif /* WOLFSSL_SHA512 */
    5458     #ifdef WOLFSSL_SHA3
    5459         #ifndef WOLFSSL_NOSHA3_224
    54605617            case WC_HASH_TYPE_SHA3_224:
    5461                 wolfSSL_SHA3_224_Final(md, (SHA3_224_CTX*)&ctx->hash);
     5618        #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
     5619                ret = wolfSSL_SHA3_224_Final(md, (SHA3_224_CTX*)&ctx->hash);
    54625620                if (s) *s = WC_SHA3_224_DIGEST_SIZE;
     5621        #endif
    54635622                break;
     5623            case WC_HASH_TYPE_SHA3_256:
     5624        #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
     5625                ret = wolfSSL_SHA3_256_Final(md, (SHA3_256_CTX*)&ctx->hash);
     5626                if (s) *s = WC_SHA3_256_DIGEST_SIZE;
    54645627        #endif
    5465         #ifndef WOLFSSL_NOSHA3_256
    5466             case WC_HASH_TYPE_SHA3_256:
    5467                 wolfSSL_SHA3_256_Final(md, (SHA3_256_CTX*)&ctx->hash);
    5468                 if (s) *s = WC_SHA3_256_DIGEST_SIZE;
    54695628                break;
     5629            case WC_HASH_TYPE_SHA3_384:
     5630        #if defined(WOLFSSL_SHA3)
     5631                ret = wolfSSL_SHA3_384_Final(md, (SHA3_384_CTX*)&ctx->hash);
     5632                if (s) *s = WC_SHA3_384_DIGEST_SIZE;
    54705633        #endif
    5471             case WC_HASH_TYPE_SHA3_384:
    5472                 wolfSSL_SHA3_384_Final(md, (SHA3_384_CTX*)&ctx->hash);
    5473                 if (s) *s = WC_SHA3_384_DIGEST_SIZE;
    54745634                break;
    5475         #ifndef WOLFSSL_NOSHA3_512
    54765635            case WC_HASH_TYPE_SHA3_512:
    5477                 wolfSSL_SHA3_512_Final(md, (SHA3_512_CTX*)&ctx->hash);
     5636        #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
     5637                ret = wolfSSL_SHA3_512_Final(md, (SHA3_512_CTX*)&ctx->hash);
    54785638                if (s) *s = WC_SHA3_512_DIGEST_SIZE;
     5639        #endif
    54795640                break;
    5480         #endif
    5481     #endif
     5641            case WC_HASH_TYPE_NONE:
     5642            case WC_HASH_TYPE_MD2:
     5643            case WC_HASH_TYPE_MD5_SHA:
     5644            case WC_HASH_TYPE_BLAKE2B:
     5645            case WC_HASH_TYPE_BLAKE2S:
    54825646            default:
    54835647                return WOLFSSL_FAILURE;
    54845648        }
    54855649
    5486         return WOLFSSL_SUCCESS;
     5650        return ret;
    54875651    }
    54885652
     
    55125676        case NID_sha1:
    55135677            return wolfSSL_EVP_sha1();
     5678#endif
     5679#ifndef NO_SHA256
     5680        case NID_sha256:
     5681            return wolfSSL_EVP_sha256();
    55145682#endif
    55155683        default:
     
    55815749        return WOLFSSL_FAILURE;
    55825750
     5751    if (wolfSSL_RSA_up_ref(key) != WOLFSSL_SUCCESS) {
     5752        WOLFSSL_MSG("wolfSSL_RSA_up_ref failed");
     5753        return WOLFSSL_FAILURE;
     5754    }
    55835755    if (pkey->rsa != NULL && pkey->ownRsa == 1) {
    55845756        wolfSSL_RSA_free(pkey->rsa);
    55855757    }
    55865758    pkey->rsa    = key;
    5587     pkey->ownRsa = 0; /* pkey does not own RSA */
     5759    pkey->ownRsa = 1; /* pkey does not own RSA but needs to call free on it */
    55885760    pkey->type   = EVP_PKEY_RSA;
    55895761    if (key->inSet == 0) {
     
    57185890
    57195891    return WOLFSSL_SUCCESS;
     5892}
     5893
     5894WOLFSSL_DSA* wolfSSL_EVP_PKEY_get0_DSA(struct WOLFSSL_EVP_PKEY *pkey)
     5895{
     5896    if (!pkey) {
     5897        return NULL;
     5898    }
     5899    return pkey->dsa;
    57205900}
    57215901
     
    58145994#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
    58155995#if !defined(NO_DH) && !defined(NO_FILESYSTEM)
     5996#if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
    58165997/* with set1 functions the pkey struct does not own the DH structure
    58175998 * Build the following DH Key format from the passed in WOLFSSL_DH
     
    58926073    return WOLFSSL_SUCCESS;
    58936074}
     6075#endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
    58946076
    58956077WOLFSSL_DH* wolfSSL_EVP_PKEY_get0_DH(WOLFSSL_EVP_PKEY* key)
     
    59016083}
    59026084
     6085#if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
    59036086WOLFSSL_DH* wolfSSL_EVP_PKEY_get1_DH(WOLFSSL_EVP_PKEY* key)
    59046087{
     
    59346117    return local;
    59356118}
     6119#endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
    59366120#endif /* NO_DH && NO_FILESYSTEM */
    59376121
     
    59596143            break;
    59606144    #endif
    5961     #ifdef NO_DH
     6145    #ifndef NO_DH
    59626146         case EVP_PKEY_DH:
    59636147            ret = wolfSSL_EVP_PKEY_assign_DH(pkey, (WOLFSSL_DH*)key);
     
    59756159#if defined(HAVE_ECC)
    59766160/* try and populate public pkey_sz and pkey.ptr */
    5977 static void ECC_populate_EVP_PKEY(EVP_PKEY* pkey, ecc_key* ecc)
    5978 {
    5979     int ret;
     6161static int ECC_populate_EVP_PKEY(EVP_PKEY* pkey, ecc_key* ecc)
     6162{
     6163    word32 derSz = 0;
    59806164    if (!pkey || !ecc)
    5981         return;
    5982     if ((ret = wc_EccPublicKeyDerSize(ecc, 1)) > 0) {
    5983         int derSz = ret;
    5984         char* derBuf = (char*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     6165        return WOLFSSL_FAILURE;
     6166    if (wc_EccKeyToPKCS8(ecc, NULL, &derSz) == LENGTH_ONLY_E) {
     6167        byte* derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_OPENSSL);
    59856168        if (derBuf) {
    5986             ret = wc_EccPublicKeyToDer(ecc, (byte*)derBuf, derSz, 1);
    5987             if (ret >= 0) {
     6169            if (wc_EccKeyToPKCS8(ecc, derBuf, &derSz) >= 0) {
    59886170                if (pkey->pkey.ptr) {
    59896171                    XFREE(pkey->pkey.ptr, NULL, DYNAMIC_TYPE_OPENSSL);
    59906172                }
    5991                 pkey->pkey_sz = ret;
    5992                 pkey->pkey.ptr = derBuf;
    5993             }
    5994             else { /* failure - okay to ignore */
    5995                 XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
     6173                pkey->pkey_sz = (int)derSz;
     6174                pkey->pkey.ptr = (char*)derBuf;
     6175                return WOLFSSL_SUCCESS;
     6176            }
     6177            else {
     6178                XFREE(derBuf, NULL, DYNAMIC_TYPE_OPENSSL);
    59966179                derBuf = NULL;
    59976180            }
    59986181        }
    59996182    }
     6183    return WOLFSSL_FAILURE;
    60006184}
    60016185
     
    60296213    pkey->ownEcc = 0; /* pkey does not own EC key */
    60306214    pkey->type   = EVP_PKEY_EC;
    6031     ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal);
    6032     return WOLFSSL_SUCCESS;
     6215    return ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal);
    60336216#else
    60346217    (void)pkey;
     
    60666249
    60676250    /* try and populate public pkey_sz and pkey.ptr */
    6068     ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal);
    6069 
    6070     return WOLFSSL_SUCCESS;
     6251    return ECC_populate_EVP_PKEY(pkey, (ecc_key*)key->internal);
    60716252}
    60726253#endif /* HAVE_ECC */
     
    61806361            return AES_BLOCK_SIZE;
    61816362#endif
     6363#if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
     6364    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
    61826365#ifdef HAVE_AESGCM
    61836366        case AES_128_GCM_TYPE :
     
    61876370            return GCM_NONCE_MID_SZ;
    61886371#endif
     6372#endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
    61896373#ifdef WOLFSSL_AES_COUNTER
    61906374        case AES_128_CTR_TYPE :
     
    62656449#ifdef HAVE_AES_CBC
    62666450    #ifdef WOLFSSL_AES_128
    6267     if (EVP_AES_128_CBC && XSTRNCMP(name, EVP_AES_128_CBC, XSTRLEN(EVP_AES_128_CBC)) == 0)
     6451    if (XSTRNCMP(name, EVP_AES_128_CBC, XSTRLEN(EVP_AES_128_CBC)) == 0)
    62686452        return AES_BLOCK_SIZE;
    62696453    #endif
    62706454    #ifdef WOLFSSL_AES_192
    6271     if (EVP_AES_192_CBC && XSTRNCMP(name, EVP_AES_192_CBC, XSTRLEN(EVP_AES_192_CBC)) == 0)
     6455    if (XSTRNCMP(name, EVP_AES_192_CBC, XSTRLEN(EVP_AES_192_CBC)) == 0)
    62726456        return AES_BLOCK_SIZE;
    62736457    #endif
    62746458    #ifdef WOLFSSL_AES_256
    6275     if (EVP_AES_256_CBC && XSTRNCMP(name, EVP_AES_256_CBC, XSTRLEN(EVP_AES_256_CBC)) == 0)
     6459    if (XSTRNCMP(name, EVP_AES_256_CBC, XSTRLEN(EVP_AES_256_CBC)) == 0)
    62766460        return AES_BLOCK_SIZE;
    62776461    #endif
    62786462#endif /* HAVE_AES_CBC */
     6463#if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
     6464    (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
    62796465#ifdef HAVE_AESGCM
    62806466    #ifdef WOLFSSL_AES_128
    6281     if (EVP_AES_128_GCM && XSTRNCMP(name, EVP_AES_128_GCM, XSTRLEN(EVP_AES_128_GCM)) == 0)
     6467    if (XSTRNCMP(name, EVP_AES_128_GCM, XSTRLEN(EVP_AES_128_GCM)) == 0)
    62826468        return GCM_NONCE_MID_SZ;
    62836469    #endif
    62846470    #ifdef WOLFSSL_AES_192
    6285     if (EVP_AES_192_GCM && XSTRNCMP(name, EVP_AES_192_GCM, XSTRLEN(EVP_AES_192_GCM)) == 0)
     6471    if (XSTRNCMP(name, EVP_AES_192_GCM, XSTRLEN(EVP_AES_192_GCM)) == 0)
    62866472        return GCM_NONCE_MID_SZ;
    62876473    #endif
    62886474    #ifdef WOLFSSL_AES_256
    6289     if (EVP_AES_256_GCM && XSTRNCMP(name, EVP_AES_256_GCM, XSTRLEN(EVP_AES_256_GCM)) == 0)
     6475    if (XSTRNCMP(name, EVP_AES_256_GCM, XSTRLEN(EVP_AES_256_GCM)) == 0)
    62906476        return GCM_NONCE_MID_SZ;
    62916477    #endif
    62926478#endif /* HAVE_AESGCM */
     6479#endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
    62936480#ifdef WOLFSSL_AES_COUNTER
    62946481    #ifdef WOLFSSL_AES_128
    6295     if (EVP_AES_128_CTR && XSTRNCMP(name, EVP_AES_128_CTR, XSTRLEN(EVP_AES_128_CTR)) == 0)
     6482    if (XSTRNCMP(name, EVP_AES_128_CTR, XSTRLEN(EVP_AES_128_CTR)) == 0)
    62966483        return AES_BLOCK_SIZE;
    62976484    #endif
    62986485    #ifdef WOLFSSL_AES_192
    6299     if (EVP_AES_192_CTR && XSTRNCMP(name, EVP_AES_192_CTR, XSTRLEN(EVP_AES_192_CTR)) == 0)
     6486    if (XSTRNCMP(name, EVP_AES_192_CTR, XSTRLEN(EVP_AES_192_CTR)) == 0)
    63006487        return AES_BLOCK_SIZE;
    63016488    #endif
    63026489    #ifdef WOLFSSL_AES_256
    6303     if (EVP_AES_256_CTR && XSTRNCMP(name, EVP_AES_256_CTR, XSTRLEN(EVP_AES_256_CTR)) == 0)
     6490    if (XSTRNCMP(name, EVP_AES_256_CTR, XSTRLEN(EVP_AES_256_CTR)) == 0)
    63046491        return AES_BLOCK_SIZE;
    63056492    #endif
     
    63076494#ifdef WOLFSSL_AES_XTS
    63086495    #ifdef WOLFSSL_AES_128
    6309     if (EVP_AES_128_XTS && XSTRNCMP(name, EVP_AES_128_XTS, XSTRLEN(EVP_AES_128_XTS)) == 0)
     6496    if (XSTRNCMP(name, EVP_AES_128_XTS, XSTRLEN(EVP_AES_128_XTS)) == 0)
    63106497        return AES_BLOCK_SIZE;
    63116498    #endif /* WOLFSSL_AES_128 */
    63126499
    63136500    #ifdef WOLFSSL_AES_256
    6314     if (EVP_AES_256_XTS && XSTRNCMP(name, EVP_AES_256_XTS, XSTRLEN(EVP_AES_256_XTS)) == 0)
     6501    if (XSTRNCMP(name, EVP_AES_256_XTS, XSTRLEN(EVP_AES_256_XTS)) == 0)
    63156502        return AES_BLOCK_SIZE;
    63166503    #endif /* WOLFSSL_AES_256 */
     
    63206507
    63216508#ifndef NO_DES3
    6322     if ((EVP_DES_CBC && XSTRNCMP(name, EVP_DES_CBC, XSTRLEN(EVP_DES_CBC)) == 0) ||
    6323            (EVP_DES_EDE3_CBC && XSTRNCMP(name, EVP_DES_EDE3_CBC, XSTRLEN(EVP_DES_EDE3_CBC)) == 0)) {
     6509    if ((XSTRNCMP(name, EVP_DES_CBC, XSTRLEN(EVP_DES_CBC)) == 0) ||
     6510           (XSTRNCMP(name, EVP_DES_EDE3_CBC, XSTRLEN(EVP_DES_EDE3_CBC)) == 0)) {
    63246511        return DES_BLOCK_SIZE;
    63256512    }
     
    63276514
    63286515#ifdef HAVE_IDEA
    6329     if (EVP_IDEA_CBC && XSTRNCMP(name, EVP_IDEA_CBC, XSTRLEN(EVP_IDEA_CBC)) == 0)
     6516    if (XSTRNCMP(name, EVP_IDEA_CBC, XSTRLEN(EVP_IDEA_CBC)) == 0)
    63306517        return IDEA_BLOCK_SIZE;
    63316518#endif
     
    63826569
    63836570
    6384 int wolfSSL_EVP_PKEY_id(const EVP_PKEY *pkey)
     6571int wolfSSL_EVP_PKEY_id(const WOLFSSL_EVP_PKEY *pkey)
    63856572{
    63866573    if (pkey != NULL)
     
    63906577
    63916578
    6392 int wolfSSL_EVP_PKEY_base_id(const EVP_PKEY *pkey)
     6579int wolfSSL_EVP_PKEY_base_id(const WOLFSSL_EVP_PKEY *pkey)
    63936580{
    63946581    if (pkey == NULL)
     
    63976584}
    63986585
     6586int wolfSSL_EVP_PKEY_get_default_digest_nid(WOLFSSL_EVP_PKEY *pkey, int *pnid)
     6587{
     6588    WOLFSSL_ENTER("wolfSSL_EVP_PKEY_get_default_digest_nid");
     6589
     6590    if (!pkey || !pnid) {
     6591        WOLFSSL_MSG("Bad parameter");
     6592        return WOLFSSL_FAILURE;
     6593    }
     6594
     6595    switch (pkey->type) {
     6596    case EVP_PKEY_HMAC:
     6597#ifndef NO_DSA
     6598    case EVP_PKEY_DSA:
     6599#endif
     6600#ifndef NO_RSA
     6601    case EVP_PKEY_RSA:
     6602#endif
     6603#ifdef HAVE_ECC
     6604    case EVP_PKEY_EC:
     6605#endif
     6606        *pnid = NID_sha256;
     6607        return WOLFSSL_SUCCESS;
     6608    default:
     6609        return WOLFSSL_FAILURE;
     6610    }
     6611}
    63996612
    64006613/* increments ref count of WOLFSSL_EVP_PKEY. Return 1 on success, 0 on error */
     
    64086621        wc_UnLockMutex(&pkey->refMutex);
    64096622
    6410         return 1;
    6411     }
    6412 
    6413     return 0;
     6623        return WOLFSSL_SUCCESS;
     6624    }
     6625
     6626    return WOLFSSL_FAILURE;
    64146627}
    64156628
     
    64796692#endif /* OPENSSL_EXTRA */
    64806693
    6481 #if defined(OPENSSL_EXTRA_X509_SMALL)
     6694#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
    64826695/* Subset of OPENSSL_EXTRA for PKEY operations PKEY free is needed by the
    64836696 * subset of X509 API */
     
    65036716        ret = wc_InitRng(&pkey->rng);
    65046717#endif
     6718        pkey->references = 1;
     6719        wc_InitMutex(&pkey->refMutex); /* init of mutex needs to come before
     6720                                        * wolfSSL_EVP_PKEY_free */
    65056721        if (ret != 0){
    65066722            wolfSSL_EVP_PKEY_free(pkey);
     
    65086724            return NULL;
    65096725        }
    6510         pkey->references = 1;
    6511         wc_InitMutex(&pkey->refMutex);
    65126726    }
    65136727    else {
     
    65916805}
    65926806
    6593 #endif /* OPENSSL_EXTRA_X509_SMALL */
     6807#if !defined(NO_PWDBASED)
     6808int wolfSSL_EVP_get_hashinfo(const WOLFSSL_EVP_MD* evp,
     6809    int* pHash, int* pHashSz)
     6810{
     6811    enum wc_HashType hash = WC_HASH_TYPE_NONE;
     6812    int hashSz;
     6813
     6814    if (XSTRLEN(evp) < 3) {
     6815        /* do not try comparing strings if size is too small */
     6816        return WOLFSSL_FAILURE;
     6817    }
     6818
     6819    if (XSTRNCMP("SHA", evp, 3) == 0) {
     6820        if (XSTRLEN(evp) > 3) {
     6821        #ifndef NO_SHA256
     6822            if (XSTRNCMP("SHA256", evp, 6) == 0) {
     6823                hash = WC_HASH_TYPE_SHA256;
     6824            }
     6825            else
     6826        #endif
     6827        #ifdef WOLFSSL_SHA384
     6828            if (XSTRNCMP("SHA384", evp, 6) == 0) {
     6829                hash = WC_HASH_TYPE_SHA384;
     6830            }
     6831            else
     6832        #endif
     6833        #ifdef WOLFSSL_SHA512
     6834            if (XSTRNCMP("SHA512", evp, 6) == 0) {
     6835                hash = WC_HASH_TYPE_SHA512;
     6836            }
     6837            else
     6838        #endif
     6839            if (XSTRNCMP("SHA1", evp, 4) == 0) {
     6840                hash = WC_HASH_TYPE_SHA;
     6841            }
     6842            else {
     6843                WOLFSSL_MSG("Unknown SHA hash");
     6844            }
     6845        }
     6846        else {
     6847            hash = WC_HASH_TYPE_SHA;
     6848        }
     6849    }
     6850#ifdef WOLFSSL_MD2
     6851    else if (XSTRNCMP("MD2", evp, 3) == 0) {
     6852        hash = WC_HASH_TYPE_MD2;
     6853    }
     6854#endif
     6855#ifndef NO_MD4
     6856    else if (XSTRNCMP("MD4", evp, 3) == 0) {
     6857        hash = WC_HASH_TYPE_MD4;
     6858    }
     6859#endif
     6860#ifndef NO_MD5
     6861    else if (XSTRNCMP("MD5", evp, 3) == 0) {
     6862        hash = WC_HASH_TYPE_MD5;
     6863    }
     6864#endif
     6865
     6866    if (pHash)
     6867        *pHash = hash;
     6868
     6869    hashSz = wc_HashGetDigestSize(hash);
     6870    if (pHashSz)
     6871        *pHashSz = hashSz;
     6872
     6873    if (hashSz < 0) {
     6874        return WOLFSSL_FAILURE;
     6875    }
     6876
     6877    return WOLFSSL_SUCCESS;
     6878}
     6879#endif /* !defined(NO_PWDBASED) */
     6880
     6881#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
    65946882
    65956883#endif /* WOLFSSL_EVP_INCLUDED */
Note: See TracChangeset for help on using the changeset viewer.