123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902 |
- /* hash.c
- *
- * Copyright (C) 2006-2023 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <wolfssl/wolfcrypt/settings.h>
- #include <wolfssl/wolfcrypt/logging.h>
- #include <wolfssl/wolfcrypt/error-crypt.h>
- #ifndef NO_ASN
- #include <wolfssl/wolfcrypt/asn.h>
- #endif
- #include <wolfssl/wolfcrypt/hash.h>
- #include <wolfssl/wolfcrypt/hmac.h>
- #include <wolfssl/wolfcrypt/cryptocb.h>
- #ifdef NO_INLINE
- #include <wolfssl/wolfcrypt/misc.h>
- #else
- #define WOLFSSL_MISC_INCLUDED
- #include <wolfcrypt/src/misc.c>
- #endif
- #ifdef NO_ASN
- enum Hash_Sum {
- MD2h = 646,
- MD5h = 649,
- SHAh = 88,
- SHA224h = 417,
- SHA256h = 414,
- SHA384h = 415,
- SHA512h = 416,
- SHA512_224h = 418,
- SHA512_256h = 419,
- SHA3_224h = 420,
- SHA3_256h = 421,
- SHA3_384h = 422,
- SHA3_512h = 423,
- SHAKE128h = 424,
- SHAKE256h = 425,
- SM3h = 640 /* 0x2A,0x81,0x1C,0xCF,0x55,0x01,0x83,0x11 */
- };
- #endif /* !NO_ASN */
- #if !defined(NO_PWDBASED) || !defined(NO_ASN)
- /* function converts int hash type to enum */
- enum wc_HashType wc_HashTypeConvert(int hashType)
- {
- /* Default to hash type none as error */
- enum wc_HashType eHashType = WC_HASH_TYPE_NONE;
- #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
- /* original FIPSv1 and CAVP selftest require a mapping for unique hash
- type to wc_HashType */
- switch (hashType) {
- #ifndef NO_MD5
- case WC_MD5:
- eHashType = WC_HASH_TYPE_MD5;
- break;
- #endif /* !NO_MD5 */
- #ifndef NO_SHA
- case WC_SHA:
- eHashType = WC_HASH_TYPE_SHA;
- break;
- #endif /* !NO_SHA */
- #ifdef WOLFSSL_SHA224
- case WC_SHA224:
- eHashType = WC_HASH_TYPE_SHA224;
- break;
- #endif /* WOLFSSL_SHA224 */
- #ifndef NO_SHA256
- case WC_SHA256:
- eHashType = WC_HASH_TYPE_SHA256;
- break;
- #endif /* !NO_SHA256 */
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- eHashType = WC_HASH_TYPE_SHA384;
- break;
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- eHashType = WC_HASH_TYPE_SHA512;
- break;
- #endif /* WOLFSSL_SHA512 */
- #ifdef WOLFSSL_SHA3
- case WC_SHA3_224:
- eHashType = WC_HASH_TYPE_SHA3_224;
- break;
- case WC_SHA3_256:
- eHashType = WC_HASH_TYPE_SHA3_256;
- break;
- case WC_SHA3_384:
- eHashType = WC_HASH_TYPE_SHA3_384;
- break;
- case WC_SHA3_512:
- eHashType = WC_HASH_TYPE_SHA3_512;
- break;
- #endif /* WOLFSSL_SHA3 */
- #ifdef WOLFSSL_SM3
- case WC_SM3:
- eHashType = WC_HASH_TYPE_SM3;
- break;
- #endif
- default:
- eHashType = WC_HASH_TYPE_NONE;
- break;
- }
- #else
- /* current master uses same unique types as wc_HashType */
- if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) {
- eHashType = (enum wc_HashType)hashType;
- }
- #endif
- return eHashType;
- }
- #endif /* !NO_PWDBASED || !NO_ASN */
- #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
- int wc_HashGetOID(enum wc_HashType hash_type)
- {
- int oid = HASH_TYPE_E; /* Default to hash type error */
- switch(hash_type)
- {
- case WC_HASH_TYPE_MD2:
- #ifdef WOLFSSL_MD2
- oid = MD2h;
- #endif
- break;
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- oid = MD5h;
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- oid = SHAh;
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- oid = SHA224h;
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- oid = SHA256h;
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- oid = SHA384h;
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- oid = SHA512h;
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- oid = SHA512_224h;
- #endif
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- oid = SHA512_256h;
- #endif
- break;
- #endif
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- oid = SHA3_224h;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- oid = SHA3_256h;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- oid = SHA3_384h;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- oid = SHA3_512h;
- #endif
- break;
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- oid = SHAKE128h;
- break;
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- oid = SHAKE256h;
- break;
- #endif
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- oid = SM3h;
- break;
- #endif
- /* Not Supported */
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- case WC_HASH_TYPE_NONE:
- default:
- oid = BAD_FUNC_ARG;
- break;
- }
- return oid;
- }
- enum wc_HashType wc_OidGetHash(int oid)
- {
- enum wc_HashType hash_type = WC_HASH_TYPE_NONE;
- switch (oid)
- {
- #ifdef WOLFSSL_MD2
- case MD2h:
- hash_type = WC_HASH_TYPE_MD2;
- break;
- #endif
- case MD5h:
- #ifndef NO_MD5
- hash_type = WC_HASH_TYPE_MD5;
- #endif
- break;
- case SHAh:
- #ifndef NO_SHA
- hash_type = WC_HASH_TYPE_SHA;
- #endif
- break;
- case SHA224h:
- #ifdef WOLFSSL_SHA224
- hash_type = WC_HASH_TYPE_SHA224;
- #endif
- break;
- case SHA256h:
- #ifndef NO_SHA256
- hash_type = WC_HASH_TYPE_SHA256;
- #endif
- break;
- case SHA384h:
- #ifdef WOLFSSL_SHA384
- hash_type = WC_HASH_TYPE_SHA384;
- #endif
- break;
- case SHA512h:
- #ifdef WOLFSSL_SHA512
- hash_type = WC_HASH_TYPE_SHA512;
- #endif
- break;
- #ifdef WOLFSSL_SHA3
- case SHA3_224h:
- hash_type = WC_HASH_TYPE_SHA3_224;
- break;
- case SHA3_256h:
- hash_type = WC_HASH_TYPE_SHA3_256;
- break;
- case SHA3_384h:
- hash_type = WC_HASH_TYPE_SHA3_384;
- break;
- case SHA3_512h:
- hash_type = WC_HASH_TYPE_SHA3_512;
- break;
- #endif /* WOLFSSL_SHA3 */
- #ifdef WOLFSSL_SM3
- case SM3h:
- hash_type = WC_HASH_TYPE_SM3;
- break;
- #endif
- default:
- break;
- }
- return hash_type;
- }
- #endif /* !NO_ASN || !NO_DH || HAVE_ECC */
- #ifndef NO_HASH_WRAPPER
- /* Get Hash digest size */
- int wc_HashGetDigestSize(enum wc_HashType hash_type)
- {
- int dig_size = HASH_TYPE_E; /* Default to hash type error */
- switch(hash_type)
- {
- case WC_HASH_TYPE_MD2:
- #ifdef WOLFSSL_MD2
- dig_size = MD2_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_MD4:
- #ifndef NO_MD4
- dig_size = MD4_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- dig_size = WC_MD5_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- dig_size = WC_SHA_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- dig_size = WC_SHA224_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- dig_size = WC_SHA256_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- dig_size = WC_SHA384_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- dig_size = WC_SHA512_DIGEST_SIZE;
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- dig_size = WC_SHA512_224_DIGEST_SIZE;
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- dig_size = WC_SHA512_256_DIGEST_SIZE;
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
- #if !defined(NO_MD5) && !defined(NO_SHA)
- dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- dig_size = WC_SHA3_224_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- dig_size = WC_SHA3_256_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- dig_size = WC_SHA3_384_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- dig_size = WC_SHA3_512_DIGEST_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
- dig_size = BLAKE2S_OUTBYTES;
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- dig_size = WC_SM3_DIGEST_SIZE;
- break;
- #endif
- /* Not Supported */
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- dig_size = BAD_FUNC_ARG;
- break;
- }
- return dig_size;
- }
- /* Get Hash block size */
- int wc_HashGetBlockSize(enum wc_HashType hash_type)
- {
- int block_size = HASH_TYPE_E; /* Default to hash type error */
- switch (hash_type)
- {
- case WC_HASH_TYPE_MD2:
- #ifdef WOLFSSL_MD2
- block_size = MD2_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_MD4:
- #ifndef NO_MD4
- block_size = MD4_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- block_size = WC_MD5_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- block_size = WC_SHA_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- block_size = WC_SHA224_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- block_size = WC_SHA256_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- block_size = WC_SHA384_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- block_size = WC_SHA512_BLOCK_SIZE;
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- block_size = WC_SHA512_224_BLOCK_SIZE;
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- block_size = WC_SHA512_256_BLOCK_SIZE;
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
- #if !defined(NO_MD5) && !defined(NO_SHA)
- block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- block_size = WC_SHA3_224_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- block_size = WC_SHA3_256_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- block_size = WC_SHA3_384_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- block_size = WC_SHA3_512_BLOCK_SIZE;
- #endif
- break;
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
- block_size = BLAKE2S_BLOCKBYTES;
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- block_size = WC_SM3_BLOCK_SIZE;
- break;
- #endif
- /* Not Supported */
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- block_size = BAD_FUNC_ARG;
- break;
- }
- return block_size;
- }
- /* Generic Hashing Wrapper */
- int wc_Hash(enum wc_HashType hash_type, const byte* data,
- word32 data_len, byte* hash, word32 hash_len)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- int dig_size;
- /* Validate hash buffer size */
- dig_size = wc_HashGetDigestSize(hash_type);
- if (dig_size < 0) {
- return dig_size;
- }
- if (hash_len < (word32)dig_size) {
- return BUFFER_E;
- }
- /* Suppress possible unused arg if all hashing is disabled */
- (void)data;
- (void)data_len;
- (void)hash;
- (void)hash_len;
- switch(hash_type)
- {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- ret = wc_Md5Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- ret = wc_ShaHash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- ret = wc_Sha224Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- ret = wc_Sha256Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512Hash(data, data_len, hash);
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- ret = wc_Sha512_224Hash(data, data_len, hash);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- ret = wc_Sha512_256Hash(data, data_len, hash);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_MD5_SHA:
- #if !defined(NO_MD5) && !defined(NO_SHA)
- ret = wc_Md5Hash(data, data_len, hash);
- if (ret == 0) {
- ret = wc_ShaHash(data, data_len, &hash[WC_MD5_DIGEST_SIZE]);
- }
- #endif
- break;
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- ret = wc_Sha3_224Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- ret = wc_Sha3_256Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- ret = wc_Sha3_384Hash(data, data_len, hash);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- ret = wc_Sha3_512Hash(data, data_len, hash);
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- ret = wc_Sm3Hash(data, data_len, hash);
- break;
- #endif
- /* Not Supported */
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- ret = BAD_FUNC_ARG;
- break;
- }
- return ret;
- }
- int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
- int devId)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- if (hash == NULL)
- return BAD_FUNC_ARG;
- switch (type) {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- ret = wc_InitMd5_ex(&hash->md5, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- ret = wc_InitSha_ex(&hash->sha, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- ret = wc_InitSha224_ex(&hash->sha224, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- ret = wc_InitSha256_ex(&hash->sha256, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- ret = wc_InitSha384_ex(&hash->sha384, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- ret = wc_InitSha512_ex(&hash->sha512, heap, devId);
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- ret = wc_InitSha512_224_ex(&hash->sha512, heap, devId);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- ret = wc_InitSha512_256_ex(&hash->sha512, heap, devId);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- ret = wc_InitSha3_224(&hash->sha3, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- ret = wc_InitSha3_256(&hash->sha3, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- ret = wc_InitSha3_384(&hash->sha3, heap, devId);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- ret = wc_InitSha3_512(&hash->sha3, heap, devId);
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- ret = wc_InitSm3(&hash->sm3, heap, devId);
- break;
- #endif
- /* not supported */
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- ret = BAD_FUNC_ARG;
- };
- (void)heap;
- (void)devId;
- return ret;
- }
- int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type)
- {
- return wc_HashInit_ex(hash, type, NULL, INVALID_DEVID);
- }
- int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data,
- word32 dataSz)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- if (hash == NULL || (data == NULL && dataSz > 0))
- return BAD_FUNC_ARG;
- switch (type) {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- ret = wc_Md5Update(&hash->md5, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- ret = wc_ShaUpdate(&hash->sha, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- ret = wc_Sha224Update(&hash->sha224, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- ret = wc_Sha256Update(&hash->sha256, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384Update(&hash->sha384, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512Update(&hash->sha512, data, dataSz);
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- ret = wc_Sha512_224Update(&hash->sha512, data, dataSz);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- ret = wc_Sha512_256Update(&hash->sha512, data, dataSz);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- ret = wc_Sha3_224_Update(&hash->sha3, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- ret = wc_Sha3_256_Update(&hash->sha3, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- ret = wc_Sha3_384_Update(&hash->sha3, data, dataSz);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- ret = wc_Sha3_512_Update(&hash->sha3, data, dataSz);
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- ret = wc_Sm3Update(&hash->sm3, data, dataSz);
- break;
- #endif
- /* not supported */
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- ret = BAD_FUNC_ARG;
- };
- return ret;
- }
- int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- if (hash == NULL || out == NULL)
- return BAD_FUNC_ARG;
- switch (type) {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- ret = wc_Md5Final(&hash->md5, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- ret = wc_ShaFinal(&hash->sha, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- ret = wc_Sha224Final(&hash->sha224, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- ret = wc_Sha256Final(&hash->sha256, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384Final(&hash->sha384, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512Final(&hash->sha512, out);
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- ret = wc_Sha512_224Final(&hash->sha512, out);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- ret = wc_Sha512_256Final(&hash->sha512, out);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- ret = wc_Sha3_224_Final(&hash->sha3, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- ret = wc_Sha3_256_Final(&hash->sha3, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- ret = wc_Sha3_384_Final(&hash->sha3, out);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- ret = wc_Sha3_512_Final(&hash->sha3, out);
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- ret = wc_Sm3Final(&hash->sm3, out);
- break;
- #endif
- /* not supported */
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- ret = BAD_FUNC_ARG;
- };
- return ret;
- }
- int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- if (hash == NULL)
- return BAD_FUNC_ARG;
- switch (type) {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- wc_Md5Free(&hash->md5);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- wc_ShaFree(&hash->sha);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- wc_Sha224Free(&hash->sha224);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- wc_Sha256Free(&hash->sha256);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- wc_Sha384Free(&hash->sha384);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifdef WOLFSSL_SHA512
- wc_Sha512Free(&hash->sha512);
- ret = 0;
- #endif
- break;
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512_224Free(&hash->sha512);
- ret = 0;
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512_256Free(&hash->sha512);
- ret = 0;
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- break;
- #endif
- case WC_HASH_TYPE_SHA3_224:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- wc_Sha3_224_Free(&hash->sha3);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_256:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wc_Sha3_256_Free(&hash->sha3);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_384:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- wc_Sha3_384_Free(&hash->sha3);
- ret = 0;
- #endif
- break;
- case WC_HASH_TYPE_SHA3_512:
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- wc_Sha3_512_Free(&hash->sha3);
- ret = 0;
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- wc_Sm3Free(&hash->sm3);
- ret = 0;
- break;
- #endif
- /* not supported */
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- ret = BAD_FUNC_ARG;
- };
- return ret;
- }
- #ifdef WOLFSSL_HASH_FLAGS
- int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, word32 flags)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- if (hash == NULL)
- return BAD_FUNC_ARG;
- switch (type) {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- ret = wc_Md5SetFlags(&hash->md5, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- ret = wc_ShaSetFlags(&hash->sha, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- ret = wc_Sha224SetFlags(&hash->sha224, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- ret = wc_Sha256SetFlags(&hash->sha256, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384SetFlags(&hash->sha384, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #endif
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512SetFlags(&hash->sha512, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_224:
- case WC_HASH_TYPE_SHA3_256:
- case WC_HASH_TYPE_SHA3_384:
- case WC_HASH_TYPE_SHA3_512:
- #ifdef WOLFSSL_SHA3
- ret = wc_Sha3_SetFlags(&hash->sha3, flags);
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- ret = wc_Sm3SetFlags(&hash->sm3, flags);
- break;
- #endif
- /* not supported */
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- case WC_HASH_TYPE_NONE:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- default:
- ret = BAD_FUNC_ARG;
- };
- return ret;
- }
- int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags)
- {
- int ret = HASH_TYPE_E; /* Default to hash type error */
- if (hash == NULL)
- return BAD_FUNC_ARG;
- switch (type) {
- case WC_HASH_TYPE_MD5:
- #ifndef NO_MD5
- ret = wc_Md5GetFlags(&hash->md5, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA:
- #ifndef NO_SHA
- ret = wc_ShaGetFlags(&hash->sha, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA224:
- #ifdef WOLFSSL_SHA224
- ret = wc_Sha224GetFlags(&hash->sha224, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA256:
- #ifndef NO_SHA256
- ret = wc_Sha256GetFlags(&hash->sha256, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA384:
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384GetFlags(&hash->sha384, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA512:
- #ifndef WOLFSSL_NOSHA512_224
- case WC_HASH_TYPE_SHA512_224:
- #endif
- #ifndef WOLFSSL_NOSHA512_256
- case WC_HASH_TYPE_SHA512_256:
- #endif
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512GetFlags(&hash->sha512, flags);
- #endif
- break;
- case WC_HASH_TYPE_SHA3_224:
- case WC_HASH_TYPE_SHA3_256:
- case WC_HASH_TYPE_SHA3_384:
- case WC_HASH_TYPE_SHA3_512:
- #ifdef WOLFSSL_SHA3
- ret = wc_Sha3_GetFlags(&hash->sha3, flags);
- #endif
- break;
- #ifdef WOLFSSL_SM3
- case WC_HASH_TYPE_SM3:
- ret = wc_Sm3GetFlags(&hash->sm3, flags);
- break;
- #endif
- /* not supported */
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
- case WC_HASH_TYPE_SHAKE128:
- #endif
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
- case WC_HASH_TYPE_SHAKE256:
- #endif
- case WC_HASH_TYPE_NONE:
- default:
- ret = BAD_FUNC_ARG;
- };
- return ret;
- }
- #endif /* WOLFSSL_HASH_FLAGS */
- #if !defined(WOLFSSL_TI_HASH)
- #if !defined(NO_MD5)
- int wc_Md5Hash(const byte* data, word32 len, byte* hash)
- {
- int ret;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Md5* md5;
- #else
- wc_Md5 md5[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (md5 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitMd5(md5)) != 0) {
- WOLFSSL_MSG("InitMd5 failed");
- }
- else {
- if ((ret = wc_Md5Update(md5, data, len)) != 0) {
- WOLFSSL_MSG("Md5Update failed");
- }
- else if ((ret = wc_Md5Final(md5, hash)) != 0) {
- WOLFSSL_MSG("Md5Final failed");
- }
- wc_Md5Free(md5);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !NO_MD5 */
- #if !defined(NO_SHA)
- int wc_ShaHash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha* sha;
- #else
- wc_Sha sha[1];
- #endif
- int devId = INVALID_DEVID;
- #ifdef WOLFSSL_SMALL_STACK
- sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sha == NULL)
- return MEMORY_E;
- #endif
- #ifdef WOLF_CRYPTO_CB
- /* only use devId if its not an empty hash */
- if (data != NULL && len > 0)
- devId = wc_CryptoCb_GetDevIdAtIndex(0);
- #endif
- if ((ret = wc_InitSha_ex(sha, NULL, devId)) != 0) {
- WOLFSSL_MSG("InitSha failed");
- }
- else {
- if ((ret = wc_ShaUpdate(sha, data, len)) != 0) {
- WOLFSSL_MSG("ShaUpdate failed");
- }
- else if ((ret = wc_ShaFinal(sha, hash)) != 0) {
- WOLFSSL_MSG("ShaFinal failed");
- }
- wc_ShaFree(sha);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !NO_SHA */
- #if defined(WOLFSSL_SHA224)
- int wc_Sha224Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha224* sha224;
- #else
- wc_Sha224 sha224[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha224 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha224(sha224)) != 0) {
- WOLFSSL_MSG("InitSha224 failed");
- }
- else {
- if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
- WOLFSSL_MSG("Sha224Update failed");
- }
- else if ((ret = wc_Sha224Final(sha224, hash)) != 0) {
- WOLFSSL_MSG("Sha224Final failed");
- }
- wc_Sha224Free(sha224);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* WOLFSSL_SHA224 */
- #if !defined(NO_SHA256)
- int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha256* sha256;
- #else
- wc_Sha256 sha256[1];
- #endif
- int devId = INVALID_DEVID;
- #ifdef WOLFSSL_SMALL_STACK
- sha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha256 == NULL)
- return MEMORY_E;
- #endif
- #ifdef WOLF_CRYPTO_CB
- /* only use devId if its not an empty hash */
- if (data != NULL && len > 0)
- devId = wc_CryptoCb_GetDevIdAtIndex(0);
- #endif
- if ((ret = wc_InitSha256_ex(sha256, NULL, devId)) != 0) {
- WOLFSSL_MSG("InitSha256 failed");
- }
- else {
- if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
- WOLFSSL_MSG("Sha256Update failed");
- }
- else if ((ret = wc_Sha256Final(sha256, hash)) != 0) {
- WOLFSSL_MSG("Sha256Final failed");
- }
- wc_Sha256Free(sha256);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !NO_SHA256 */
- #endif /* !defined(WOLFSSL_TI_HASH) */
- #if defined(WOLFSSL_SHA512)
- int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha512* sha512;
- #else
- wc_Sha512 sha512[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha512 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha512(sha512)) != 0) {
- WOLFSSL_MSG("InitSha512 failed");
- }
- else {
- if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
- WOLFSSL_MSG("Sha512Update failed");
- }
- else if ((ret = wc_Sha512Final(sha512, hash)) != 0) {
- WOLFSSL_MSG("Sha512Final failed");
- }
- wc_Sha512Free(sha512);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #ifndef WOLFSSL_NOSHA512_224
- int wc_Sha512_224Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha512* sha512;
- #else
- wc_Sha512 sha512[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha512 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha512_224(sha512)) != 0) {
- WOLFSSL_MSG("wc_InitSha512_224 failed");
- }
- else {
- if ((ret = wc_Sha512_224Update(sha512, data, len)) != 0) {
- WOLFSSL_MSG("wc_Sha512_224_Update failed");
- }
- else if ((ret = wc_Sha512_224Final(sha512, hash)) != 0) {
- WOLFSSL_MSG("wc_Sha512_224_Final failed");
- }
- wc_Sha512_224Free(sha512);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #ifndef WOLFSSL_NOSHA512_256
- int wc_Sha512_256Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha512* sha512;
- #else
- wc_Sha512 sha512[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha512 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha512_256(sha512)) != 0) {
- WOLFSSL_MSG("wc_InitSha512_256 failed");
- }
- else {
- if ((ret = wc_Sha512_256Update(sha512, data, len)) != 0) {
- WOLFSSL_MSG("wc_Sha512_256_Update failed");
- }
- else if ((ret = wc_Sha512_256Final(sha512, hash)) != 0) {
- WOLFSSL_MSG("wc_Sha512_256_Final failed");
- }
- wc_Sha512_256Free(sha512);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- #endif /* WOLFSSL_SHA512 */
- #if defined(WOLFSSL_SHA384)
- int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha384* sha384;
- #else
- wc_Sha384 sha384[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha384 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha384(sha384)) != 0) {
- WOLFSSL_MSG("InitSha384 failed");
- }
- else {
- if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
- WOLFSSL_MSG("Sha384Update failed");
- }
- else if ((ret = wc_Sha384Final(sha384, hash)) != 0) {
- WOLFSSL_MSG("Sha384Final failed");
- }
- wc_Sha384Free(sha384);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* WOLFSSL_SHA384 */
- #if defined(WOLFSSL_SHA3)
- #if !defined(WOLFSSL_NOSHA3_224)
- int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha3* sha3;
- #else
- wc_Sha3 sha3[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha3 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha3_224(sha3, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitSha3_224 failed");
- }
- else {
- if ((ret = wc_Sha3_224_Update(sha3, data, len)) != 0) {
- WOLFSSL_MSG("Sha3_224_Update failed");
- }
- else if ((ret = wc_Sha3_224_Final(sha3, hash)) != 0) {
- WOLFSSL_MSG("Sha3_224_Final failed");
- }
- wc_Sha3_224_Free(sha3);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA3_224 */
- #if !defined(WOLFSSL_NOSHA3_256)
- int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha3* sha3;
- #else
- wc_Sha3 sha3[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha3 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha3_256(sha3, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitSha3_256 failed");
- }
- else {
- if ((ret = wc_Sha3_256_Update(sha3, data, len)) != 0) {
- WOLFSSL_MSG("Sha3_256_Update failed");
- }
- else if ((ret = wc_Sha3_256_Final(sha3, hash)) != 0) {
- WOLFSSL_MSG("Sha3_256_Final failed");
- }
- wc_Sha3_256_Free(sha3);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA3_256 */
- #if !defined(WOLFSSL_NOSHA3_384)
- int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha3* sha3;
- #else
- wc_Sha3 sha3[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha3 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha3_384(sha3, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitSha3_384 failed");
- }
- else {
- if ((ret = wc_Sha3_384_Update(sha3, data, len)) != 0) {
- WOLFSSL_MSG("Sha3_384_Update failed");
- }
- else if ((ret = wc_Sha3_384_Final(sha3, hash)) != 0) {
- WOLFSSL_MSG("Sha3_384_Final failed");
- }
- wc_Sha3_384_Free(sha3);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA3_384 */
- #if !defined(WOLFSSL_NOSHA3_512)
- int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sha3* sha3;
- #else
- wc_Sha3 sha3[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (sha3 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSha3_512(sha3, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitSha3_512 failed");
- }
- else {
- if ((ret = wc_Sha3_512_Update(sha3, data, len)) != 0) {
- WOLFSSL_MSG("Sha3_512_Update failed");
- }
- else if ((ret = wc_Sha3_512_Final(sha3, hash)) != 0) {
- WOLFSSL_MSG("Sha3_512_Final failed");
- }
- wc_Sha3_512_Free(sha3);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA3_512 */
- #ifdef WOLFSSL_SHAKE128
- int wc_Shake128Hash(const byte* data, word32 len, byte* hash,
- word32 hashLen)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Shake* shake;
- #else
- wc_Shake shake[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (shake == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitShake128(shake, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitShake128 failed");
- }
- else {
- if ((ret = wc_Shake128_Update(shake, data, len)) != 0) {
- WOLFSSL_MSG("Shake128_Update failed");
- }
- else if ((ret = wc_Shake128_Final(shake, hash, hashLen)) != 0) {
- WOLFSSL_MSG("Shake128_Final failed");
- }
- wc_Shake128_Free(shake);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* WOLFSSL_SHAKE_128 */
- #ifdef WOLFSSL_SHAKE256
- int wc_Shake256Hash(const byte* data, word32 len, byte* hash,
- word32 hashLen)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Shake* shake;
- #else
- wc_Shake shake[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (shake == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitShake256(shake, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitShake256 failed");
- }
- else {
- if ((ret = wc_Shake256_Update(shake, data, len)) != 0) {
- WOLFSSL_MSG("Shake256_Update failed");
- }
- else if ((ret = wc_Shake256_Final(shake, hash, hashLen)) != 0) {
- WOLFSSL_MSG("Shake256_Final failed");
- }
- wc_Shake256_Free(shake);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* WOLFSSL_SHAKE_256 */
- #endif /* WOLFSSL_SHA3 */
- #ifdef WOLFSSL_SM3
- int wc_Sm3Hash(const byte* data, word32 len, byte* hash)
- {
- int ret = 0;
- #ifdef WOLFSSL_SMALL_STACK
- wc_Sm3* sm3;
- #else
- wc_Sm3 sm3[1];
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- sm3 = (wc_Sm3*)XMALLOC(sizeof(wc_Sm3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sm3 == NULL)
- return MEMORY_E;
- #endif
- if ((ret = wc_InitSm3(sm3, NULL, INVALID_DEVID)) != 0) {
- WOLFSSL_MSG("InitSm3 failed");
- }
- else {
- if ((ret = wc_Sm3Update(sm3, data, len)) != 0) {
- WOLFSSL_MSG("Sm3Update failed");
- }
- else if ((ret = wc_Sm3Final(sm3, hash)) != 0) {
- WOLFSSL_MSG("Sm3Final failed");
- }
- wc_Sm3Free(sm3);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(sm3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- #endif /* !WOLFSSL_NOSHA3_224 */
- #endif /* !NO_HASH_WRAPPER */
- #ifdef WOLFSSL_HASH_KEEP
- int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in,
- int inSz, void* heap)
- {
- if (*len < *used + inSz) {
- if (*msg == NULL) {
- *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- else {
- byte* pt = (byte*)XREALLOC(*msg, *used + inSz, heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (pt == NULL) {
- return MEMORY_E;
- }
- *msg = pt;
- }
- if (*msg == NULL) {
- return MEMORY_E;
- }
- *len = *used + inSz;
- }
- XMEMCPY(*msg + *used, in, inSz);
- *used += inSz;
- return 0;
- }
- #endif /* WOLFSSL_HASH_KEEP */
|