Ignore:
Timestamp:
Feb 7, 2019, 8:36:33 AM (5 years ago)
Author:
coas-nagasima
Message:

wolfsslを3.15.7にバージョンアップ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfcrypt/src/hash.c

    r352 r372  
    4747    SHA512h = 416
    4848};
    49 #endif
     49#endif /* !NO_ASN */
     50
     51#ifdef HAVE_SELFTEST
     52enum {
     53    /* CAVP selftest includes these in hmac.h instead of sha3.h,
     54       copied here for that build */
     55    WC_SHA3_224_BLOCK_SIZE = 144,
     56    WC_SHA3_256_BLOCK_SIZE = 136,
     57    WC_SHA3_384_BLOCK_SIZE = 104,
     58    WC_SHA3_512_BLOCK_SIZE = 72,
     59};
     60#endif
     61
     62
     63/* function converts int hash type to enum */
     64enum wc_HashType wc_HashTypeConvert(int hashType)
     65{
     66    /* Default to hash type none as error */
     67    enum wc_HashType eHashType = WC_HASH_TYPE_NONE;
     68#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
     69    /* original FIPSv1  and CAVP selftest require a mapping for unique hash
     70       type to wc_HashType */
     71    switch (hashType) {
     72    #ifndef NO_MD5
     73        case WC_MD5:
     74            eHashType = WC_HASH_TYPE_MD5;
     75            break;
     76    #endif /* !NO_MD5 */
     77    #ifndef NO_SHA
     78        case WC_SHA:
     79            eHashType = WC_HASH_TYPE_SHA;
     80            break;
     81    #endif /* !NO_SHA */
     82
     83    #ifdef WOLFSSL_SHA224
     84        case WC_SHA224:
     85            eHashType = WC_HASH_TYPE_SHA224;
     86            break;
     87    #endif /* WOLFSSL_SHA224 */
     88
     89    #ifndef NO_SHA256
     90        case WC_SHA256:
     91            eHashType = WC_HASH_TYPE_SHA256;
     92            break;
     93    #endif /* !NO_SHA256 */
     94
     95    #ifdef WOLFSSL_SHA384
     96        case WC_SHA384:
     97            eHashType = WC_HASH_TYPE_SHA384;
     98            break;
     99    #endif /* WOLFSSL_SHA384 */
     100    #ifdef WOLFSSL_SHA512
     101        case WC_SHA512:
     102            eHashType = WC_HASH_TYPE_SHA512;
     103            break;
     104    #endif /* WOLFSSL_SHA512 */
     105        default:
     106            eHashType = WC_HASH_TYPE_NONE;
     107            break;
     108    }
     109#else
     110    /* current master uses same unique types as wc_HashType */
     111    if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) {
     112        eHashType = (enum wc_HashType)hashType;
     113    }
     114#endif
     115    return eHashType;
     116}
     117
    50118
    51119int wc_HashGetOID(enum wc_HashType hash_type)
     
    71139            break;
    72140        case WC_HASH_TYPE_SHA224:
    73         #if defined(WOLFSSL_SHA224)
     141        #ifdef WOLFSSL_SHA224
    74142            oid = SHA224h;
    75143        #endif
     
    81149            break;
    82150        case WC_HASH_TYPE_SHA384:
    83         #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)
     151        #ifdef WOLFSSL_SHA384
    84152            oid = SHA384h;
    85153        #endif
     
    93161        /* Not Supported */
    94162        case WC_HASH_TYPE_MD4:
     163        case WC_HASH_TYPE_SHA3_224:
     164        case WC_HASH_TYPE_SHA3_256:
     165        case WC_HASH_TYPE_SHA3_384:
     166        case WC_HASH_TYPE_SHA3_512:
     167        case WC_HASH_TYPE_BLAKE2B:
    95168        case WC_HASH_TYPE_NONE:
    96169        default:
     
    100173    return oid;
    101174}
    102 #endif
     175
     176enum wc_HashType wc_OidGetHash(int oid)
     177{
     178    enum wc_HashType hash_type = WC_HASH_TYPE_NONE;
     179    switch (oid)
     180    {
     181    #ifdef WOLFSSL_MD2
     182        case MD2h:
     183            hash_type = WC_HASH_TYPE_MD2;
     184            break;
     185    #endif
     186        case MD5h:
     187        #ifndef NO_MD5
     188            hash_type = WC_HASH_TYPE_MD5;
     189#endif
     190            break;
     191        case SHAh:
     192        #ifndef NO_SHA
     193            hash_type = WC_HASH_TYPE_SHA;
     194        #endif
     195            break;
     196        case SHA224h:
     197        #ifdef WOLFSSL_SHA224
     198            hash_type = WC_HASH_TYPE_SHA224;
     199        #endif
     200            break;
     201        case SHA256h:
     202        #ifndef NO_SHA256
     203            hash_type = WC_HASH_TYPE_SHA256;
     204        #endif
     205            break;
     206        case SHA384h:
     207        #ifdef WOLFSSL_SHA384
     208            hash_type = WC_HASH_TYPE_SHA384;
     209        #endif
     210            break;
     211        case SHA512h:
     212        #ifdef WOLFSSL_SHA512
     213            hash_type = WC_HASH_TYPE_SHA512;
     214        #endif
     215            break;
     216        default:
     217            break;
     218    }
     219    return hash_type;
     220}
     221#endif /* !NO_ASN || !NO_DH || HAVE_ECC */
     222
     223
    103224
    104225/* Get Hash digest size */
     
    108229    switch(hash_type)
    109230    {
     231        case WC_HASH_TYPE_MD2:
     232        #ifdef WOLFSSL_MD2
     233            dig_size = MD2_DIGEST_SIZE;
     234        #endif
     235            break;
     236        case WC_HASH_TYPE_MD4:
     237        #ifndef NO_MD4
     238            dig_size = MD4_DIGEST_SIZE;
     239        #endif
     240            break;
    110241        case WC_HASH_TYPE_MD5:
    111242#ifndef NO_MD5
     
    129260            break;
    130261        case WC_HASH_TYPE_SHA384:
    131 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)
     262        #ifdef WOLFSSL_SHA384
    132263            dig_size = WC_SHA384_DIGEST_SIZE;
    133264#endif
     
    138269#endif
    139270            break;
    140         case WC_HASH_TYPE_MD5_SHA:
     271        case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
    141272#if !defined(NO_MD5) && !defined(NO_SHA)
    142             dig_size = WC_MD5_DIGEST_SIZE + WC_SHA_DIGEST_SIZE;
     273            dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE;
     274        #endif
     275            break;
     276
     277        case WC_HASH_TYPE_SHA3_224:
     278        #ifdef WOLFSSL_SHA3
     279            dig_size = WC_SHA3_224_DIGEST_SIZE;
     280        #endif
     281            break;
     282        case WC_HASH_TYPE_SHA3_256:
     283        #ifdef WOLFSSL_SHA3
     284            dig_size = WC_SHA3_256_DIGEST_SIZE;
     285        #endif
     286            break;
     287        case WC_HASH_TYPE_SHA3_384:
     288        #ifdef WOLFSSL_SHA3
     289            dig_size = WC_SHA3_384_DIGEST_SIZE;
     290        #endif
     291            break;
     292        case WC_HASH_TYPE_SHA3_512:
     293        #ifdef WOLFSSL_SHA3
     294            dig_size = WC_SHA3_512_DIGEST_SIZE;
    143295#endif
    144296            break;
    145297
    146298        /* Not Supported */
    147         case WC_HASH_TYPE_MD2:
    148         case WC_HASH_TYPE_MD4:
     299        case WC_HASH_TYPE_BLAKE2B:
    149300        case WC_HASH_TYPE_NONE:
    150301        default:
     
    153304    }
    154305    return dig_size;
     306}
     307
     308
     309/* Get Hash block size */
     310int wc_HashGetBlockSize(enum wc_HashType hash_type)
     311{
     312    int block_size = HASH_TYPE_E; /* Default to hash type error */
     313    switch (hash_type)
     314    {
     315        case WC_HASH_TYPE_MD2:
     316        #ifdef WOLFSSL_MD2
     317            block_size = MD2_BLOCK_SIZE;
     318        #endif
     319            break;
     320        case WC_HASH_TYPE_MD4:
     321        #ifndef NO_MD4
     322            block_size = MD4_BLOCK_SIZE;
     323        #endif
     324            break;
     325        case WC_HASH_TYPE_MD5:
     326        #ifndef NO_MD5
     327            block_size = WC_MD5_BLOCK_SIZE;
     328        #endif
     329            break;
     330        case WC_HASH_TYPE_SHA:
     331        #ifndef NO_SHA
     332            block_size = WC_SHA_BLOCK_SIZE;
     333        #endif
     334            break;
     335        case WC_HASH_TYPE_SHA224:
     336        #ifdef WOLFSSL_SHA224
     337            block_size = WC_SHA224_BLOCK_SIZE;
     338        #endif
     339            break;
     340        case WC_HASH_TYPE_SHA256:
     341        #ifndef NO_SHA256
     342            block_size = WC_SHA256_BLOCK_SIZE;
     343        #endif
     344            break;
     345        case WC_HASH_TYPE_SHA384:
     346        #ifdef WOLFSSL_SHA384
     347            block_size = WC_SHA384_BLOCK_SIZE;
     348        #endif
     349            break;
     350        case WC_HASH_TYPE_SHA512:
     351        #ifdef WOLFSSL_SHA512
     352            block_size = WC_SHA512_BLOCK_SIZE;
     353        #endif
     354            break;
     355        case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
     356        #if !defined(NO_MD5) && !defined(NO_SHA)
     357            block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE;
     358        #endif
     359            break;
     360
     361        case WC_HASH_TYPE_SHA3_224:
     362        #ifdef WOLFSSL_SHA3
     363            block_size = WC_SHA3_224_BLOCK_SIZE;
     364        #endif
     365            break;
     366        case WC_HASH_TYPE_SHA3_256:
     367        #ifdef WOLFSSL_SHA3
     368            block_size = WC_SHA3_256_BLOCK_SIZE;
     369        #endif
     370            break;
     371        case WC_HASH_TYPE_SHA3_384:
     372        #ifdef WOLFSSL_SHA3
     373            block_size = WC_SHA3_384_BLOCK_SIZE;
     374        #endif
     375            break;
     376        case WC_HASH_TYPE_SHA3_512:
     377        #ifdef WOLFSSL_SHA3
     378            block_size = WC_SHA3_512_BLOCK_SIZE;
     379        #endif
     380            break;
     381
     382        /* Not Supported */
     383        case WC_HASH_TYPE_BLAKE2B:
     384        case WC_HASH_TYPE_NONE:
     385        default:
     386            block_size = BAD_FUNC_ARG;
     387            break;
     388    }
     389    return block_size;
    155390}
    156391
     
    197432            break;
    198433        case WC_HASH_TYPE_SHA384:
    199 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)
     434#ifdef WOLFSSL_SHA384
    200435            ret = wc_Sha384Hash(data, data_len, hash);
    201436#endif
     
    218453        case WC_HASH_TYPE_MD2:
    219454        case WC_HASH_TYPE_MD4:
     455        case WC_HASH_TYPE_SHA3_224:
     456        case WC_HASH_TYPE_SHA3_256:
     457        case WC_HASH_TYPE_SHA3_384:
     458        case WC_HASH_TYPE_SHA3_512:
     459        case WC_HASH_TYPE_BLAKE2B:
    220460        case WC_HASH_TYPE_NONE:
    221461        default:
     
    236476        case WC_HASH_TYPE_MD5:
    237477#ifndef NO_MD5
    238             wc_InitMd5(&hash->md5);
    239             ret = 0;
     478            ret = wc_InitMd5(&hash->md5);
    240479#endif
    241480            break;
     
    270509        case WC_HASH_TYPE_MD2:
    271510        case WC_HASH_TYPE_MD4:
     511        case WC_HASH_TYPE_SHA3_224:
     512        case WC_HASH_TYPE_SHA3_256:
     513        case WC_HASH_TYPE_SHA3_384:
     514        case WC_HASH_TYPE_SHA3_512:
     515        case WC_HASH_TYPE_BLAKE2B:
    272516        case WC_HASH_TYPE_NONE:
    273517        default:
     
    289533        case WC_HASH_TYPE_MD5:
    290534#ifndef NO_MD5
    291             wc_Md5Update(&hash->md5, data, dataSz);
    292             ret = 0;
     535            ret = wc_Md5Update(&hash->md5, data, dataSz);
    293536#endif
    294537            break;
     
    296539#ifndef NO_SHA
    297540            ret = wc_ShaUpdate(&hash->sha, data, dataSz);
    298             if (ret != 0)
    299                 return ret;
    300541#endif
    301542            break;
     
    325566        case WC_HASH_TYPE_MD2:
    326567        case WC_HASH_TYPE_MD4:
     568        case WC_HASH_TYPE_SHA3_224:
     569        case WC_HASH_TYPE_SHA3_256:
     570        case WC_HASH_TYPE_SHA3_384:
     571        case WC_HASH_TYPE_SHA3_512:
     572        case WC_HASH_TYPE_BLAKE2B:
    327573        case WC_HASH_TYPE_NONE:
    328574        default:
     
    343589        case WC_HASH_TYPE_MD5:
    344590#ifndef NO_MD5
    345             wc_Md5Final(&hash->md5, out);
    346             ret = 0;
     591            ret = wc_Md5Final(&hash->md5, out);
    347592#endif
    348593            break;
     
    377622        case WC_HASH_TYPE_MD2:
    378623        case WC_HASH_TYPE_MD4:
     624        case WC_HASH_TYPE_SHA3_224:
     625        case WC_HASH_TYPE_SHA3_256:
     626        case WC_HASH_TYPE_SHA3_384:
     627        case WC_HASH_TYPE_SHA3_512:
     628        case WC_HASH_TYPE_BLAKE2B:
    379629        case WC_HASH_TYPE_NONE:
    380630        default:
     
    385635}
    386636
     637int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
     638{
     639    int ret = HASH_TYPE_E; /* Default to hash type error */
     640
     641    if (hash == NULL)
     642        return BAD_FUNC_ARG;
     643
     644    switch (type) {
     645        case WC_HASH_TYPE_MD5:
     646#ifndef NO_MD5
     647            wc_Md5Free(&hash->md5);
     648            ret = 0;
     649#endif
     650            break;
     651        case WC_HASH_TYPE_SHA:
     652#ifndef NO_SHA
     653            wc_ShaFree(&hash->sha);
     654            ret = 0;
     655#endif
     656            break;
     657        case WC_HASH_TYPE_SHA224:
     658#ifdef WOLFSSL_SHA224
     659            wc_Sha224Free(&hash->sha224);
     660            ret = 0;
     661#endif
     662            break;
     663        case WC_HASH_TYPE_SHA256:
     664#ifndef NO_SHA256
     665            wc_Sha256Free(&hash->sha256);
     666            ret = 0;
     667#endif
     668            break;
     669        case WC_HASH_TYPE_SHA384:
     670#ifdef WOLFSSL_SHA384
     671            wc_Sha384Free(&hash->sha384);
     672            ret = 0;
     673#endif
     674            break;
     675        case WC_HASH_TYPE_SHA512:
     676#ifdef WOLFSSL_SHA512
     677            wc_Sha512Free(&hash->sha512);
     678            ret = 0;
     679#endif
     680            break;
     681
     682        /* not supported */
     683        case WC_HASH_TYPE_MD5_SHA:
     684        case WC_HASH_TYPE_MD2:
     685        case WC_HASH_TYPE_MD4:
     686        case WC_HASH_TYPE_SHA3_224:
     687        case WC_HASH_TYPE_SHA3_256:
     688        case WC_HASH_TYPE_SHA3_384:
     689        case WC_HASH_TYPE_SHA3_512:
     690        case WC_HASH_TYPE_BLAKE2B:
     691        case WC_HASH_TYPE_NONE:
     692        default:
     693            ret = BAD_FUNC_ARG;
     694    };
     695
     696    return ret;
     697}
     698
    387699
    388700#if !defined(WOLFSSL_TI_HASH)
     
    404716    #endif
    405717
    406         ret = wc_InitMd5(md5);
    407         if (ret == 0) {
    408             ret = wc_Md5Update(md5, data, len);
    409             if (ret == 0) {
    410                 ret = wc_Md5Final(md5, hash);
     718        if ((ret = wc_InitMd5(md5)) != 0) {
     719            WOLFSSL_MSG("InitMd5 failed");
     720        }
     721        else {
     722            if ((ret = wc_Md5Update(md5, data, len)) != 0) {
     723                WOLFSSL_MSG("Md5Update failed");
    411724            }
     725            else if ((ret = wc_Md5Final(md5, hash)) != 0) {
     726                WOLFSSL_MSG("Md5Final failed");
     727            }
     728            wc_Md5Free(md5);
    412729        }
    413730
     
    437754
    438755        if ((ret = wc_InitSha(sha)) != 0) {
    439             WOLFSSL_MSG("wc_InitSha failed");
     756            WOLFSSL_MSG("InitSha failed");
    440757        }
    441758        else {
    442             wc_ShaUpdate(sha, data, len);
    443             wc_ShaFinal(sha, hash);
     759            if ((ret = wc_ShaUpdate(sha, data, len)) != 0) {
     760                WOLFSSL_MSG("ShaUpdate failed");
     761            }
     762            else if ((ret = wc_ShaFinal(sha, hash)) != 0) {
     763                WOLFSSL_MSG("ShaFinal failed");
     764            }
     765            wc_ShaFree(sha);
    444766        }
    445767
     
    472794        WOLFSSL_MSG("InitSha224 failed");
    473795    }
    474     else if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
     796        else {
     797            if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
    475798        WOLFSSL_MSG("Sha224Update failed");
    476799    }
     
    478801        WOLFSSL_MSG("Sha224Final failed");
    479802    }
     803            wc_Sha224Free(sha224);
     804        }
    480805
    481806#ifdef WOLFSSL_SMALL_STACK
     
    507832            WOLFSSL_MSG("InitSha256 failed");
    508833        }
    509         else if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
     834        else {
     835            if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
    510836            WOLFSSL_MSG("Sha256Update failed");
    511837        }
     
    513839            WOLFSSL_MSG("Sha256Final failed");
    514840        }
     841            wc_Sha256Free(sha256);
     842        }
     843
    515844
    516845    #ifdef WOLFSSL_SMALL_STACK
     
    545874            WOLFSSL_MSG("InitSha512 failed");
    546875        }
    547         else if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
     876        else {
     877            if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
    548878            WOLFSSL_MSG("Sha512Update failed");
    549879        }
     
    551881            WOLFSSL_MSG("Sha512Final failed");
    552882        }
     883            wc_Sha512Free(sha512);
     884        }
    553885
    554886    #ifdef WOLFSSL_SMALL_STACK
     
    558890        return ret;
    559891    }
     892#endif /* WOLFSSL_SHA512 */
    560893
    561894    #if defined(WOLFSSL_SHA384)
     
    579912                WOLFSSL_MSG("InitSha384 failed");
    580913            }
    581             else if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
     914        else {
     915            if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
    582916                WOLFSSL_MSG("Sha384Update failed");
    583917            }
     
    585919                WOLFSSL_MSG("Sha384Final failed");
    586920            }
     921            wc_Sha384Free(sha384);
     922        }
    587923
    588924        #ifdef WOLFSSL_SMALL_STACK
     
    593929        }
    594930    #endif /* WOLFSSL_SHA384 */
    595 #endif /* WOLFSSL_SHA512 */
Note: See TracChangeset for help on using the changeset viewer.