hash.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /* hash.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/logging.h>
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #ifndef NO_ASN
  28. #include <wolfssl/wolfcrypt/asn.h>
  29. #endif
  30. #include <wolfssl/wolfcrypt/hash.h>
  31. #include <wolfssl/wolfcrypt/hmac.h>
  32. #include <wolfssl/wolfcrypt/cryptocb.h>
  33. #ifdef NO_INLINE
  34. #include <wolfssl/wolfcrypt/misc.h>
  35. #else
  36. #define WOLFSSL_MISC_INCLUDED
  37. #include <wolfcrypt/src/misc.c>
  38. #endif
  39. #ifdef NO_ASN
  40. enum Hash_Sum {
  41. MD2h = 646,
  42. MD5h = 649,
  43. SHAh = 88,
  44. SHA224h = 417,
  45. SHA256h = 414,
  46. SHA384h = 415,
  47. SHA512h = 416,
  48. SHA512_224h = 418,
  49. SHA512_256h = 419,
  50. SHA3_224h = 420,
  51. SHA3_256h = 421,
  52. SHA3_384h = 422,
  53. SHA3_512h = 423,
  54. SHAKE128h = 424,
  55. SHAKE256h = 425,
  56. SM3h = 640 /* 0x2A,0x81,0x1C,0xCF,0x55,0x01,0x83,0x11 */
  57. };
  58. #endif /* !NO_ASN */
  59. #if !defined(NO_PWDBASED) || !defined(NO_ASN)
  60. /* function converts int hash type to enum */
  61. enum wc_HashType wc_HashTypeConvert(int hashType)
  62. {
  63. /* Default to hash type none as error */
  64. enum wc_HashType eHashType = WC_HASH_TYPE_NONE;
  65. #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
  66. /* original FIPSv1 and CAVP selftest require a mapping for unique hash
  67. type to wc_HashType */
  68. switch (hashType) {
  69. #ifndef NO_MD5
  70. case WC_MD5:
  71. eHashType = WC_HASH_TYPE_MD5;
  72. break;
  73. #endif /* !NO_MD5 */
  74. #ifndef NO_SHA
  75. case WC_SHA:
  76. eHashType = WC_HASH_TYPE_SHA;
  77. break;
  78. #endif /* !NO_SHA */
  79. #ifdef WOLFSSL_SHA224
  80. case WC_SHA224:
  81. eHashType = WC_HASH_TYPE_SHA224;
  82. break;
  83. #endif /* WOLFSSL_SHA224 */
  84. #ifndef NO_SHA256
  85. case WC_SHA256:
  86. eHashType = WC_HASH_TYPE_SHA256;
  87. break;
  88. #endif /* !NO_SHA256 */
  89. #ifdef WOLFSSL_SHA384
  90. case WC_SHA384:
  91. eHashType = WC_HASH_TYPE_SHA384;
  92. break;
  93. #endif /* WOLFSSL_SHA384 */
  94. #ifdef WOLFSSL_SHA512
  95. case WC_SHA512:
  96. eHashType = WC_HASH_TYPE_SHA512;
  97. break;
  98. #endif /* WOLFSSL_SHA512 */
  99. #ifdef WOLFSSL_SHA3
  100. case WC_SHA3_224:
  101. eHashType = WC_HASH_TYPE_SHA3_224;
  102. break;
  103. case WC_SHA3_256:
  104. eHashType = WC_HASH_TYPE_SHA3_256;
  105. break;
  106. case WC_SHA3_384:
  107. eHashType = WC_HASH_TYPE_SHA3_384;
  108. break;
  109. case WC_SHA3_512:
  110. eHashType = WC_HASH_TYPE_SHA3_512;
  111. break;
  112. #endif /* WOLFSSL_SHA3 */
  113. #ifdef WOLFSSL_SM3
  114. case WC_SM3:
  115. eHashType = WC_HASH_TYPE_SM3;
  116. break;
  117. #endif
  118. default:
  119. eHashType = WC_HASH_TYPE_NONE;
  120. break;
  121. }
  122. #else
  123. /* current master uses same unique types as wc_HashType */
  124. if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) {
  125. eHashType = (enum wc_HashType)hashType;
  126. }
  127. #endif
  128. return eHashType;
  129. }
  130. #endif /* !NO_PWDBASED || !NO_ASN */
  131. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  132. int wc_HashGetOID(enum wc_HashType hash_type)
  133. {
  134. int oid = HASH_TYPE_E; /* Default to hash type error */
  135. switch(hash_type)
  136. {
  137. case WC_HASH_TYPE_MD2:
  138. #ifdef WOLFSSL_MD2
  139. oid = MD2h;
  140. #endif
  141. break;
  142. case WC_HASH_TYPE_MD5_SHA:
  143. case WC_HASH_TYPE_MD5:
  144. #ifndef NO_MD5
  145. oid = MD5h;
  146. #endif
  147. break;
  148. case WC_HASH_TYPE_SHA:
  149. #ifndef NO_SHA
  150. oid = SHAh;
  151. #endif
  152. break;
  153. case WC_HASH_TYPE_SHA224:
  154. #ifdef WOLFSSL_SHA224
  155. oid = SHA224h;
  156. #endif
  157. break;
  158. case WC_HASH_TYPE_SHA256:
  159. #ifndef NO_SHA256
  160. oid = SHA256h;
  161. #endif
  162. break;
  163. case WC_HASH_TYPE_SHA384:
  164. #ifdef WOLFSSL_SHA384
  165. oid = SHA384h;
  166. #endif
  167. break;
  168. case WC_HASH_TYPE_SHA512:
  169. #ifdef WOLFSSL_SHA512
  170. oid = SHA512h;
  171. #endif
  172. break;
  173. #ifndef WOLFSSL_NOSHA512_224
  174. case WC_HASH_TYPE_SHA512_224:
  175. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  176. oid = SHA512_224h;
  177. #endif
  178. break;
  179. #endif
  180. #ifndef WOLFSSL_NOSHA512_256
  181. case WC_HASH_TYPE_SHA512_256:
  182. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  183. oid = SHA512_256h;
  184. #endif
  185. break;
  186. #endif
  187. case WC_HASH_TYPE_SHA3_224:
  188. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  189. oid = SHA3_224h;
  190. #endif
  191. break;
  192. case WC_HASH_TYPE_SHA3_256:
  193. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  194. oid = SHA3_256h;
  195. #endif
  196. break;
  197. case WC_HASH_TYPE_SHA3_384:
  198. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  199. oid = SHA3_384h;
  200. #endif
  201. break;
  202. case WC_HASH_TYPE_SHA3_512:
  203. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  204. oid = SHA3_512h;
  205. #endif
  206. break;
  207. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  208. case WC_HASH_TYPE_SHAKE128:
  209. oid = SHAKE128h;
  210. break;
  211. #endif
  212. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  213. case WC_HASH_TYPE_SHAKE256:
  214. oid = SHAKE256h;
  215. break;
  216. #endif
  217. #ifdef WOLFSSL_SM3
  218. case WC_HASH_TYPE_SM3:
  219. oid = SM3h;
  220. break;
  221. #endif
  222. /* Not Supported */
  223. case WC_HASH_TYPE_MD4:
  224. case WC_HASH_TYPE_BLAKE2B:
  225. case WC_HASH_TYPE_BLAKE2S:
  226. case WC_HASH_TYPE_NONE:
  227. default:
  228. oid = BAD_FUNC_ARG;
  229. break;
  230. }
  231. return oid;
  232. }
  233. enum wc_HashType wc_OidGetHash(int oid)
  234. {
  235. enum wc_HashType hash_type = WC_HASH_TYPE_NONE;
  236. switch (oid)
  237. {
  238. #ifdef WOLFSSL_MD2
  239. case MD2h:
  240. hash_type = WC_HASH_TYPE_MD2;
  241. break;
  242. #endif
  243. case MD5h:
  244. #ifndef NO_MD5
  245. hash_type = WC_HASH_TYPE_MD5;
  246. #endif
  247. break;
  248. case SHAh:
  249. #ifndef NO_SHA
  250. hash_type = WC_HASH_TYPE_SHA;
  251. #endif
  252. break;
  253. case SHA224h:
  254. #ifdef WOLFSSL_SHA224
  255. hash_type = WC_HASH_TYPE_SHA224;
  256. #endif
  257. break;
  258. case SHA256h:
  259. #ifndef NO_SHA256
  260. hash_type = WC_HASH_TYPE_SHA256;
  261. #endif
  262. break;
  263. case SHA384h:
  264. #ifdef WOLFSSL_SHA384
  265. hash_type = WC_HASH_TYPE_SHA384;
  266. #endif
  267. break;
  268. case SHA512h:
  269. #ifdef WOLFSSL_SHA512
  270. hash_type = WC_HASH_TYPE_SHA512;
  271. #endif
  272. break;
  273. #ifdef WOLFSSL_SHA3
  274. case SHA3_224h:
  275. hash_type = WC_HASH_TYPE_SHA3_224;
  276. break;
  277. case SHA3_256h:
  278. hash_type = WC_HASH_TYPE_SHA3_256;
  279. break;
  280. case SHA3_384h:
  281. hash_type = WC_HASH_TYPE_SHA3_384;
  282. break;
  283. case SHA3_512h:
  284. hash_type = WC_HASH_TYPE_SHA3_512;
  285. break;
  286. #endif /* WOLFSSL_SHA3 */
  287. #ifdef WOLFSSL_SM3
  288. case SM3h:
  289. hash_type = WC_HASH_TYPE_SM3;
  290. break;
  291. #endif
  292. default:
  293. break;
  294. }
  295. return hash_type;
  296. }
  297. #endif /* !NO_ASN || !NO_DH || HAVE_ECC */
  298. #ifndef NO_HASH_WRAPPER
  299. /* Get Hash digest size */
  300. int wc_HashGetDigestSize(enum wc_HashType hash_type)
  301. {
  302. int dig_size = HASH_TYPE_E; /* Default to hash type error */
  303. switch(hash_type)
  304. {
  305. case WC_HASH_TYPE_MD2:
  306. #ifdef WOLFSSL_MD2
  307. dig_size = MD2_DIGEST_SIZE;
  308. #endif
  309. break;
  310. case WC_HASH_TYPE_MD4:
  311. #ifndef NO_MD4
  312. dig_size = MD4_DIGEST_SIZE;
  313. #endif
  314. break;
  315. case WC_HASH_TYPE_MD5:
  316. #ifndef NO_MD5
  317. dig_size = WC_MD5_DIGEST_SIZE;
  318. #endif
  319. break;
  320. case WC_HASH_TYPE_SHA:
  321. #ifndef NO_SHA
  322. dig_size = WC_SHA_DIGEST_SIZE;
  323. #endif
  324. break;
  325. case WC_HASH_TYPE_SHA224:
  326. #ifdef WOLFSSL_SHA224
  327. dig_size = WC_SHA224_DIGEST_SIZE;
  328. #endif
  329. break;
  330. case WC_HASH_TYPE_SHA256:
  331. #ifndef NO_SHA256
  332. dig_size = WC_SHA256_DIGEST_SIZE;
  333. #endif
  334. break;
  335. case WC_HASH_TYPE_SHA384:
  336. #ifdef WOLFSSL_SHA384
  337. dig_size = WC_SHA384_DIGEST_SIZE;
  338. #endif
  339. break;
  340. case WC_HASH_TYPE_SHA512:
  341. #ifdef WOLFSSL_SHA512
  342. dig_size = WC_SHA512_DIGEST_SIZE;
  343. #endif
  344. break;
  345. #ifndef WOLFSSL_NOSHA512_224
  346. case WC_HASH_TYPE_SHA512_224:
  347. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  348. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  349. dig_size = WC_SHA512_224_DIGEST_SIZE;
  350. #endif
  351. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  352. break;
  353. #endif
  354. #ifndef WOLFSSL_NOSHA512_256
  355. case WC_HASH_TYPE_SHA512_256:
  356. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  357. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  358. dig_size = WC_SHA512_256_DIGEST_SIZE;
  359. #endif
  360. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  361. break;
  362. #endif
  363. case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
  364. #if !defined(NO_MD5) && !defined(NO_SHA)
  365. dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE;
  366. #endif
  367. break;
  368. case WC_HASH_TYPE_SHA3_224:
  369. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  370. dig_size = WC_SHA3_224_DIGEST_SIZE;
  371. #endif
  372. break;
  373. case WC_HASH_TYPE_SHA3_256:
  374. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  375. dig_size = WC_SHA3_256_DIGEST_SIZE;
  376. #endif
  377. break;
  378. case WC_HASH_TYPE_SHA3_384:
  379. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  380. dig_size = WC_SHA3_384_DIGEST_SIZE;
  381. #endif
  382. break;
  383. case WC_HASH_TYPE_SHA3_512:
  384. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  385. dig_size = WC_SHA3_512_DIGEST_SIZE;
  386. #endif
  387. break;
  388. case WC_HASH_TYPE_BLAKE2B:
  389. case WC_HASH_TYPE_BLAKE2S:
  390. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  391. dig_size = BLAKE2S_OUTBYTES;
  392. #endif
  393. break;
  394. #ifdef WOLFSSL_SM3
  395. case WC_HASH_TYPE_SM3:
  396. dig_size = WC_SM3_DIGEST_SIZE;
  397. break;
  398. #endif
  399. /* Not Supported */
  400. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  401. case WC_HASH_TYPE_SHAKE128:
  402. #endif
  403. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  404. case WC_HASH_TYPE_SHAKE256:
  405. #endif
  406. case WC_HASH_TYPE_NONE:
  407. default:
  408. dig_size = BAD_FUNC_ARG;
  409. break;
  410. }
  411. return dig_size;
  412. }
  413. /* Get Hash block size */
  414. int wc_HashGetBlockSize(enum wc_HashType hash_type)
  415. {
  416. int block_size = HASH_TYPE_E; /* Default to hash type error */
  417. switch (hash_type)
  418. {
  419. case WC_HASH_TYPE_MD2:
  420. #ifdef WOLFSSL_MD2
  421. block_size = MD2_BLOCK_SIZE;
  422. #endif
  423. break;
  424. case WC_HASH_TYPE_MD4:
  425. #ifndef NO_MD4
  426. block_size = MD4_BLOCK_SIZE;
  427. #endif
  428. break;
  429. case WC_HASH_TYPE_MD5:
  430. #ifndef NO_MD5
  431. block_size = WC_MD5_BLOCK_SIZE;
  432. #endif
  433. break;
  434. case WC_HASH_TYPE_SHA:
  435. #ifndef NO_SHA
  436. block_size = WC_SHA_BLOCK_SIZE;
  437. #endif
  438. break;
  439. case WC_HASH_TYPE_SHA224:
  440. #ifdef WOLFSSL_SHA224
  441. block_size = WC_SHA224_BLOCK_SIZE;
  442. #endif
  443. break;
  444. case WC_HASH_TYPE_SHA256:
  445. #ifndef NO_SHA256
  446. block_size = WC_SHA256_BLOCK_SIZE;
  447. #endif
  448. break;
  449. case WC_HASH_TYPE_SHA384:
  450. #ifdef WOLFSSL_SHA384
  451. block_size = WC_SHA384_BLOCK_SIZE;
  452. #endif
  453. break;
  454. case WC_HASH_TYPE_SHA512:
  455. #ifdef WOLFSSL_SHA512
  456. block_size = WC_SHA512_BLOCK_SIZE;
  457. #endif
  458. break;
  459. #ifndef WOLFSSL_NOSHA512_224
  460. case WC_HASH_TYPE_SHA512_224:
  461. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  462. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  463. block_size = WC_SHA512_224_BLOCK_SIZE;
  464. #endif
  465. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  466. break;
  467. #endif
  468. #ifndef WOLFSSL_NOSHA512_256
  469. case WC_HASH_TYPE_SHA512_256:
  470. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  471. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  472. block_size = WC_SHA512_256_BLOCK_SIZE;
  473. #endif
  474. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  475. break;
  476. #endif
  477. case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
  478. #if !defined(NO_MD5) && !defined(NO_SHA)
  479. block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE;
  480. #endif
  481. break;
  482. case WC_HASH_TYPE_SHA3_224:
  483. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  484. block_size = WC_SHA3_224_BLOCK_SIZE;
  485. #endif
  486. break;
  487. case WC_HASH_TYPE_SHA3_256:
  488. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  489. block_size = WC_SHA3_256_BLOCK_SIZE;
  490. #endif
  491. break;
  492. case WC_HASH_TYPE_SHA3_384:
  493. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  494. block_size = WC_SHA3_384_BLOCK_SIZE;
  495. #endif
  496. break;
  497. case WC_HASH_TYPE_SHA3_512:
  498. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  499. block_size = WC_SHA3_512_BLOCK_SIZE;
  500. #endif
  501. break;
  502. case WC_HASH_TYPE_BLAKE2B:
  503. case WC_HASH_TYPE_BLAKE2S:
  504. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  505. block_size = BLAKE2S_BLOCKBYTES;
  506. #endif
  507. break;
  508. #ifdef WOLFSSL_SM3
  509. case WC_HASH_TYPE_SM3:
  510. block_size = WC_SM3_BLOCK_SIZE;
  511. break;
  512. #endif
  513. /* Not Supported */
  514. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  515. case WC_HASH_TYPE_SHAKE128:
  516. #endif
  517. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  518. case WC_HASH_TYPE_SHAKE256:
  519. #endif
  520. case WC_HASH_TYPE_NONE:
  521. default:
  522. block_size = BAD_FUNC_ARG;
  523. break;
  524. }
  525. return block_size;
  526. }
  527. /* Generic Hashing Wrapper */
  528. int wc_Hash(enum wc_HashType hash_type, const byte* data,
  529. word32 data_len, byte* hash, word32 hash_len)
  530. {
  531. int ret = HASH_TYPE_E; /* Default to hash type error */
  532. int dig_size;
  533. /* Validate hash buffer size */
  534. dig_size = wc_HashGetDigestSize(hash_type);
  535. if (dig_size < 0) {
  536. return dig_size;
  537. }
  538. if (hash_len < (word32)dig_size) {
  539. return BUFFER_E;
  540. }
  541. /* Suppress possible unused arg if all hashing is disabled */
  542. (void)data;
  543. (void)data_len;
  544. (void)hash;
  545. (void)hash_len;
  546. switch(hash_type)
  547. {
  548. case WC_HASH_TYPE_MD5:
  549. #ifndef NO_MD5
  550. ret = wc_Md5Hash(data, data_len, hash);
  551. #endif
  552. break;
  553. case WC_HASH_TYPE_SHA:
  554. #ifndef NO_SHA
  555. ret = wc_ShaHash(data, data_len, hash);
  556. #endif
  557. break;
  558. case WC_HASH_TYPE_SHA224:
  559. #ifdef WOLFSSL_SHA224
  560. ret = wc_Sha224Hash(data, data_len, hash);
  561. #endif
  562. break;
  563. case WC_HASH_TYPE_SHA256:
  564. #ifndef NO_SHA256
  565. ret = wc_Sha256Hash(data, data_len, hash);
  566. #endif
  567. break;
  568. case WC_HASH_TYPE_SHA384:
  569. #ifdef WOLFSSL_SHA384
  570. ret = wc_Sha384Hash(data, data_len, hash);
  571. #endif
  572. break;
  573. case WC_HASH_TYPE_SHA512:
  574. #ifdef WOLFSSL_SHA512
  575. ret = wc_Sha512Hash(data, data_len, hash);
  576. #endif
  577. break;
  578. #ifndef WOLFSSL_NOSHA512_224
  579. case WC_HASH_TYPE_SHA512_224:
  580. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  581. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  582. ret = wc_Sha512_224Hash(data, data_len, hash);
  583. #endif
  584. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  585. break;
  586. #endif
  587. #ifndef WOLFSSL_NOSHA512_256
  588. case WC_HASH_TYPE_SHA512_256:
  589. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  590. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  591. ret = wc_Sha512_256Hash(data, data_len, hash);
  592. #endif
  593. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  594. break;
  595. #endif
  596. case WC_HASH_TYPE_MD5_SHA:
  597. #if !defined(NO_MD5) && !defined(NO_SHA)
  598. ret = wc_Md5Hash(data, data_len, hash);
  599. if (ret == 0) {
  600. ret = wc_ShaHash(data, data_len, &hash[WC_MD5_DIGEST_SIZE]);
  601. }
  602. #endif
  603. break;
  604. case WC_HASH_TYPE_SHA3_224:
  605. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  606. ret = wc_Sha3_224Hash(data, data_len, hash);
  607. #endif
  608. break;
  609. case WC_HASH_TYPE_SHA3_256:
  610. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  611. ret = wc_Sha3_256Hash(data, data_len, hash);
  612. #endif
  613. break;
  614. case WC_HASH_TYPE_SHA3_384:
  615. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  616. ret = wc_Sha3_384Hash(data, data_len, hash);
  617. #endif
  618. break;
  619. case WC_HASH_TYPE_SHA3_512:
  620. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  621. ret = wc_Sha3_512Hash(data, data_len, hash);
  622. #endif
  623. break;
  624. #ifdef WOLFSSL_SM3
  625. case WC_HASH_TYPE_SM3:
  626. ret = wc_Sm3Hash(data, data_len, hash);
  627. break;
  628. #endif
  629. /* Not Supported */
  630. case WC_HASH_TYPE_MD2:
  631. case WC_HASH_TYPE_MD4:
  632. case WC_HASH_TYPE_BLAKE2B:
  633. case WC_HASH_TYPE_BLAKE2S:
  634. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  635. case WC_HASH_TYPE_SHAKE128:
  636. #endif
  637. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  638. case WC_HASH_TYPE_SHAKE256:
  639. #endif
  640. case WC_HASH_TYPE_NONE:
  641. default:
  642. ret = BAD_FUNC_ARG;
  643. break;
  644. }
  645. return ret;
  646. }
  647. int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
  648. int devId)
  649. {
  650. int ret = HASH_TYPE_E; /* Default to hash type error */
  651. if (hash == NULL)
  652. return BAD_FUNC_ARG;
  653. switch (type) {
  654. case WC_HASH_TYPE_MD5:
  655. #ifndef NO_MD5
  656. ret = wc_InitMd5_ex(&hash->md5, heap, devId);
  657. #endif
  658. break;
  659. case WC_HASH_TYPE_SHA:
  660. #ifndef NO_SHA
  661. ret = wc_InitSha_ex(&hash->sha, heap, devId);
  662. #endif
  663. break;
  664. case WC_HASH_TYPE_SHA224:
  665. #ifdef WOLFSSL_SHA224
  666. ret = wc_InitSha224_ex(&hash->sha224, heap, devId);
  667. #endif
  668. break;
  669. case WC_HASH_TYPE_SHA256:
  670. #ifndef NO_SHA256
  671. ret = wc_InitSha256_ex(&hash->sha256, heap, devId);
  672. #endif
  673. break;
  674. case WC_HASH_TYPE_SHA384:
  675. #ifdef WOLFSSL_SHA384
  676. ret = wc_InitSha384_ex(&hash->sha384, heap, devId);
  677. #endif
  678. break;
  679. case WC_HASH_TYPE_SHA512:
  680. #ifdef WOLFSSL_SHA512
  681. ret = wc_InitSha512_ex(&hash->sha512, heap, devId);
  682. #endif
  683. break;
  684. #ifndef WOLFSSL_NOSHA512_224
  685. case WC_HASH_TYPE_SHA512_224:
  686. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  687. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  688. ret = wc_InitSha512_224_ex(&hash->sha512, heap, devId);
  689. #endif
  690. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  691. break;
  692. #endif
  693. #ifndef WOLFSSL_NOSHA512_256
  694. case WC_HASH_TYPE_SHA512_256:
  695. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  696. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  697. ret = wc_InitSha512_256_ex(&hash->sha512, heap, devId);
  698. #endif
  699. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  700. break;
  701. #endif
  702. case WC_HASH_TYPE_SHA3_224:
  703. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  704. ret = wc_InitSha3_224(&hash->sha3, heap, devId);
  705. #endif
  706. break;
  707. case WC_HASH_TYPE_SHA3_256:
  708. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  709. ret = wc_InitSha3_256(&hash->sha3, heap, devId);
  710. #endif
  711. break;
  712. case WC_HASH_TYPE_SHA3_384:
  713. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  714. ret = wc_InitSha3_384(&hash->sha3, heap, devId);
  715. #endif
  716. break;
  717. case WC_HASH_TYPE_SHA3_512:
  718. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  719. ret = wc_InitSha3_512(&hash->sha3, heap, devId);
  720. #endif
  721. break;
  722. #ifdef WOLFSSL_SM3
  723. case WC_HASH_TYPE_SM3:
  724. ret = wc_InitSm3(&hash->sm3, heap, devId);
  725. break;
  726. #endif
  727. /* not supported */
  728. case WC_HASH_TYPE_MD5_SHA:
  729. case WC_HASH_TYPE_MD2:
  730. case WC_HASH_TYPE_MD4:
  731. case WC_HASH_TYPE_BLAKE2B:
  732. case WC_HASH_TYPE_BLAKE2S:
  733. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  734. case WC_HASH_TYPE_SHAKE128:
  735. #endif
  736. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  737. case WC_HASH_TYPE_SHAKE256:
  738. #endif
  739. case WC_HASH_TYPE_NONE:
  740. default:
  741. ret = BAD_FUNC_ARG;
  742. };
  743. (void)heap;
  744. (void)devId;
  745. return ret;
  746. }
  747. int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type)
  748. {
  749. return wc_HashInit_ex(hash, type, NULL, INVALID_DEVID);
  750. }
  751. int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data,
  752. word32 dataSz)
  753. {
  754. int ret = HASH_TYPE_E; /* Default to hash type error */
  755. if (hash == NULL || (data == NULL && dataSz > 0))
  756. return BAD_FUNC_ARG;
  757. switch (type) {
  758. case WC_HASH_TYPE_MD5:
  759. #ifndef NO_MD5
  760. ret = wc_Md5Update(&hash->md5, data, dataSz);
  761. #endif
  762. break;
  763. case WC_HASH_TYPE_SHA:
  764. #ifndef NO_SHA
  765. ret = wc_ShaUpdate(&hash->sha, data, dataSz);
  766. #endif
  767. break;
  768. case WC_HASH_TYPE_SHA224:
  769. #ifdef WOLFSSL_SHA224
  770. ret = wc_Sha224Update(&hash->sha224, data, dataSz);
  771. #endif
  772. break;
  773. case WC_HASH_TYPE_SHA256:
  774. #ifndef NO_SHA256
  775. ret = wc_Sha256Update(&hash->sha256, data, dataSz);
  776. #endif
  777. break;
  778. case WC_HASH_TYPE_SHA384:
  779. #ifdef WOLFSSL_SHA384
  780. ret = wc_Sha384Update(&hash->sha384, data, dataSz);
  781. #endif
  782. break;
  783. case WC_HASH_TYPE_SHA512:
  784. #ifdef WOLFSSL_SHA512
  785. ret = wc_Sha512Update(&hash->sha512, data, dataSz);
  786. #endif
  787. break;
  788. #ifndef WOLFSSL_NOSHA512_224
  789. case WC_HASH_TYPE_SHA512_224:
  790. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  791. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  792. ret = wc_Sha512_224Update(&hash->sha512, data, dataSz);
  793. #endif
  794. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  795. break;
  796. #endif
  797. #ifndef WOLFSSL_NOSHA512_256
  798. case WC_HASH_TYPE_SHA512_256:
  799. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  800. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  801. ret = wc_Sha512_256Update(&hash->sha512, data, dataSz);
  802. #endif
  803. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  804. break;
  805. #endif
  806. case WC_HASH_TYPE_SHA3_224:
  807. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  808. ret = wc_Sha3_224_Update(&hash->sha3, data, dataSz);
  809. #endif
  810. break;
  811. case WC_HASH_TYPE_SHA3_256:
  812. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  813. ret = wc_Sha3_256_Update(&hash->sha3, data, dataSz);
  814. #endif
  815. break;
  816. case WC_HASH_TYPE_SHA3_384:
  817. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  818. ret = wc_Sha3_384_Update(&hash->sha3, data, dataSz);
  819. #endif
  820. break;
  821. case WC_HASH_TYPE_SHA3_512:
  822. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  823. ret = wc_Sha3_512_Update(&hash->sha3, data, dataSz);
  824. #endif
  825. break;
  826. #ifdef WOLFSSL_SM3
  827. case WC_HASH_TYPE_SM3:
  828. ret = wc_Sm3Update(&hash->sm3, data, dataSz);
  829. break;
  830. #endif
  831. /* not supported */
  832. case WC_HASH_TYPE_MD5_SHA:
  833. case WC_HASH_TYPE_MD2:
  834. case WC_HASH_TYPE_MD4:
  835. case WC_HASH_TYPE_BLAKE2B:
  836. case WC_HASH_TYPE_BLAKE2S:
  837. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  838. case WC_HASH_TYPE_SHAKE128:
  839. #endif
  840. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  841. case WC_HASH_TYPE_SHAKE256:
  842. #endif
  843. case WC_HASH_TYPE_NONE:
  844. default:
  845. ret = BAD_FUNC_ARG;
  846. };
  847. return ret;
  848. }
  849. int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
  850. {
  851. int ret = HASH_TYPE_E; /* Default to hash type error */
  852. if (hash == NULL || out == NULL)
  853. return BAD_FUNC_ARG;
  854. switch (type) {
  855. case WC_HASH_TYPE_MD5:
  856. #ifndef NO_MD5
  857. ret = wc_Md5Final(&hash->md5, out);
  858. #endif
  859. break;
  860. case WC_HASH_TYPE_SHA:
  861. #ifndef NO_SHA
  862. ret = wc_ShaFinal(&hash->sha, out);
  863. #endif
  864. break;
  865. case WC_HASH_TYPE_SHA224:
  866. #ifdef WOLFSSL_SHA224
  867. ret = wc_Sha224Final(&hash->sha224, out);
  868. #endif
  869. break;
  870. case WC_HASH_TYPE_SHA256:
  871. #ifndef NO_SHA256
  872. ret = wc_Sha256Final(&hash->sha256, out);
  873. #endif
  874. break;
  875. case WC_HASH_TYPE_SHA384:
  876. #ifdef WOLFSSL_SHA384
  877. ret = wc_Sha384Final(&hash->sha384, out);
  878. #endif
  879. break;
  880. case WC_HASH_TYPE_SHA512:
  881. #ifdef WOLFSSL_SHA512
  882. ret = wc_Sha512Final(&hash->sha512, out);
  883. #endif
  884. break;
  885. #ifndef WOLFSSL_NOSHA512_224
  886. case WC_HASH_TYPE_SHA512_224:
  887. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  888. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  889. ret = wc_Sha512_224Final(&hash->sha512, out);
  890. #endif
  891. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  892. break;
  893. #endif
  894. #ifndef WOLFSSL_NOSHA512_256
  895. case WC_HASH_TYPE_SHA512_256:
  896. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  897. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  898. ret = wc_Sha512_256Final(&hash->sha512, out);
  899. #endif
  900. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  901. break;
  902. #endif
  903. case WC_HASH_TYPE_SHA3_224:
  904. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  905. ret = wc_Sha3_224_Final(&hash->sha3, out);
  906. #endif
  907. break;
  908. case WC_HASH_TYPE_SHA3_256:
  909. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  910. ret = wc_Sha3_256_Final(&hash->sha3, out);
  911. #endif
  912. break;
  913. case WC_HASH_TYPE_SHA3_384:
  914. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  915. ret = wc_Sha3_384_Final(&hash->sha3, out);
  916. #endif
  917. break;
  918. case WC_HASH_TYPE_SHA3_512:
  919. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  920. ret = wc_Sha3_512_Final(&hash->sha3, out);
  921. #endif
  922. break;
  923. #ifdef WOLFSSL_SM3
  924. case WC_HASH_TYPE_SM3:
  925. ret = wc_Sm3Final(&hash->sm3, out);
  926. break;
  927. #endif
  928. /* not supported */
  929. case WC_HASH_TYPE_MD5_SHA:
  930. case WC_HASH_TYPE_MD2:
  931. case WC_HASH_TYPE_MD4:
  932. case WC_HASH_TYPE_BLAKE2B:
  933. case WC_HASH_TYPE_BLAKE2S:
  934. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  935. case WC_HASH_TYPE_SHAKE128:
  936. #endif
  937. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  938. case WC_HASH_TYPE_SHAKE256:
  939. #endif
  940. case WC_HASH_TYPE_NONE:
  941. default:
  942. ret = BAD_FUNC_ARG;
  943. };
  944. return ret;
  945. }
  946. int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
  947. {
  948. int ret = HASH_TYPE_E; /* Default to hash type error */
  949. if (hash == NULL)
  950. return BAD_FUNC_ARG;
  951. switch (type) {
  952. case WC_HASH_TYPE_MD5:
  953. #ifndef NO_MD5
  954. wc_Md5Free(&hash->md5);
  955. ret = 0;
  956. #endif
  957. break;
  958. case WC_HASH_TYPE_SHA:
  959. #ifndef NO_SHA
  960. wc_ShaFree(&hash->sha);
  961. ret = 0;
  962. #endif
  963. break;
  964. case WC_HASH_TYPE_SHA224:
  965. #ifdef WOLFSSL_SHA224
  966. wc_Sha224Free(&hash->sha224);
  967. ret = 0;
  968. #endif
  969. break;
  970. case WC_HASH_TYPE_SHA256:
  971. #ifndef NO_SHA256
  972. wc_Sha256Free(&hash->sha256);
  973. ret = 0;
  974. #endif
  975. break;
  976. case WC_HASH_TYPE_SHA384:
  977. #ifdef WOLFSSL_SHA384
  978. wc_Sha384Free(&hash->sha384);
  979. ret = 0;
  980. #endif
  981. break;
  982. case WC_HASH_TYPE_SHA512:
  983. #ifdef WOLFSSL_SHA512
  984. wc_Sha512Free(&hash->sha512);
  985. ret = 0;
  986. #endif
  987. break;
  988. #ifndef WOLFSSL_NOSHA512_224
  989. case WC_HASH_TYPE_SHA512_224:
  990. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  991. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  992. wc_Sha512_224Free(&hash->sha512);
  993. ret = 0;
  994. #endif
  995. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  996. break;
  997. #endif
  998. #ifndef WOLFSSL_NOSHA512_256
  999. case WC_HASH_TYPE_SHA512_256:
  1000. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  1001. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  1002. wc_Sha512_256Free(&hash->sha512);
  1003. ret = 0;
  1004. #endif
  1005. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  1006. break;
  1007. #endif
  1008. case WC_HASH_TYPE_SHA3_224:
  1009. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  1010. wc_Sha3_224_Free(&hash->sha3);
  1011. ret = 0;
  1012. #endif
  1013. break;
  1014. case WC_HASH_TYPE_SHA3_256:
  1015. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1016. wc_Sha3_256_Free(&hash->sha3);
  1017. ret = 0;
  1018. #endif
  1019. break;
  1020. case WC_HASH_TYPE_SHA3_384:
  1021. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  1022. wc_Sha3_384_Free(&hash->sha3);
  1023. ret = 0;
  1024. #endif
  1025. break;
  1026. case WC_HASH_TYPE_SHA3_512:
  1027. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  1028. wc_Sha3_512_Free(&hash->sha3);
  1029. ret = 0;
  1030. #endif
  1031. break;
  1032. #ifdef WOLFSSL_SM3
  1033. case WC_HASH_TYPE_SM3:
  1034. wc_Sm3Free(&hash->sm3);
  1035. ret = 0;
  1036. break;
  1037. #endif
  1038. /* not supported */
  1039. case WC_HASH_TYPE_MD5_SHA:
  1040. case WC_HASH_TYPE_MD2:
  1041. case WC_HASH_TYPE_MD4:
  1042. case WC_HASH_TYPE_BLAKE2B:
  1043. case WC_HASH_TYPE_BLAKE2S:
  1044. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  1045. case WC_HASH_TYPE_SHAKE128:
  1046. #endif
  1047. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  1048. case WC_HASH_TYPE_SHAKE256:
  1049. #endif
  1050. case WC_HASH_TYPE_NONE:
  1051. default:
  1052. ret = BAD_FUNC_ARG;
  1053. };
  1054. return ret;
  1055. }
  1056. #ifdef WOLFSSL_HASH_FLAGS
  1057. int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, word32 flags)
  1058. {
  1059. int ret = HASH_TYPE_E; /* Default to hash type error */
  1060. if (hash == NULL)
  1061. return BAD_FUNC_ARG;
  1062. switch (type) {
  1063. case WC_HASH_TYPE_MD5:
  1064. #ifndef NO_MD5
  1065. ret = wc_Md5SetFlags(&hash->md5, flags);
  1066. #endif
  1067. break;
  1068. case WC_HASH_TYPE_SHA:
  1069. #ifndef NO_SHA
  1070. ret = wc_ShaSetFlags(&hash->sha, flags);
  1071. #endif
  1072. break;
  1073. case WC_HASH_TYPE_SHA224:
  1074. #ifdef WOLFSSL_SHA224
  1075. ret = wc_Sha224SetFlags(&hash->sha224, flags);
  1076. #endif
  1077. break;
  1078. case WC_HASH_TYPE_SHA256:
  1079. #ifndef NO_SHA256
  1080. ret = wc_Sha256SetFlags(&hash->sha256, flags);
  1081. #endif
  1082. break;
  1083. case WC_HASH_TYPE_SHA384:
  1084. #ifdef WOLFSSL_SHA384
  1085. ret = wc_Sha384SetFlags(&hash->sha384, flags);
  1086. #endif
  1087. break;
  1088. case WC_HASH_TYPE_SHA512:
  1089. #ifndef WOLFSSL_NOSHA512_224
  1090. case WC_HASH_TYPE_SHA512_224:
  1091. #endif
  1092. #ifndef WOLFSSL_NOSHA512_256
  1093. case WC_HASH_TYPE_SHA512_256:
  1094. #endif
  1095. #ifdef WOLFSSL_SHA512
  1096. ret = wc_Sha512SetFlags(&hash->sha512, flags);
  1097. #endif
  1098. break;
  1099. case WC_HASH_TYPE_SHA3_224:
  1100. case WC_HASH_TYPE_SHA3_256:
  1101. case WC_HASH_TYPE_SHA3_384:
  1102. case WC_HASH_TYPE_SHA3_512:
  1103. #ifdef WOLFSSL_SHA3
  1104. ret = wc_Sha3_SetFlags(&hash->sha3, flags);
  1105. #endif
  1106. break;
  1107. #ifdef WOLFSSL_SM3
  1108. case WC_HASH_TYPE_SM3:
  1109. ret = wc_Sm3SetFlags(&hash->sm3, flags);
  1110. break;
  1111. #endif
  1112. /* not supported */
  1113. case WC_HASH_TYPE_MD5_SHA:
  1114. case WC_HASH_TYPE_MD2:
  1115. case WC_HASH_TYPE_MD4:
  1116. case WC_HASH_TYPE_BLAKE2B:
  1117. case WC_HASH_TYPE_BLAKE2S:
  1118. case WC_HASH_TYPE_NONE:
  1119. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  1120. case WC_HASH_TYPE_SHAKE128:
  1121. #endif
  1122. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  1123. case WC_HASH_TYPE_SHAKE256:
  1124. #endif
  1125. default:
  1126. ret = BAD_FUNC_ARG;
  1127. };
  1128. return ret;
  1129. }
  1130. int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags)
  1131. {
  1132. int ret = HASH_TYPE_E; /* Default to hash type error */
  1133. if (hash == NULL)
  1134. return BAD_FUNC_ARG;
  1135. switch (type) {
  1136. case WC_HASH_TYPE_MD5:
  1137. #ifndef NO_MD5
  1138. ret = wc_Md5GetFlags(&hash->md5, flags);
  1139. #endif
  1140. break;
  1141. case WC_HASH_TYPE_SHA:
  1142. #ifndef NO_SHA
  1143. ret = wc_ShaGetFlags(&hash->sha, flags);
  1144. #endif
  1145. break;
  1146. case WC_HASH_TYPE_SHA224:
  1147. #ifdef WOLFSSL_SHA224
  1148. ret = wc_Sha224GetFlags(&hash->sha224, flags);
  1149. #endif
  1150. break;
  1151. case WC_HASH_TYPE_SHA256:
  1152. #ifndef NO_SHA256
  1153. ret = wc_Sha256GetFlags(&hash->sha256, flags);
  1154. #endif
  1155. break;
  1156. case WC_HASH_TYPE_SHA384:
  1157. #ifdef WOLFSSL_SHA384
  1158. ret = wc_Sha384GetFlags(&hash->sha384, flags);
  1159. #endif
  1160. break;
  1161. case WC_HASH_TYPE_SHA512:
  1162. #ifndef WOLFSSL_NOSHA512_224
  1163. case WC_HASH_TYPE_SHA512_224:
  1164. #endif
  1165. #ifndef WOLFSSL_NOSHA512_256
  1166. case WC_HASH_TYPE_SHA512_256:
  1167. #endif
  1168. #ifdef WOLFSSL_SHA512
  1169. ret = wc_Sha512GetFlags(&hash->sha512, flags);
  1170. #endif
  1171. break;
  1172. case WC_HASH_TYPE_SHA3_224:
  1173. case WC_HASH_TYPE_SHA3_256:
  1174. case WC_HASH_TYPE_SHA3_384:
  1175. case WC_HASH_TYPE_SHA3_512:
  1176. #ifdef WOLFSSL_SHA3
  1177. ret = wc_Sha3_GetFlags(&hash->sha3, flags);
  1178. #endif
  1179. break;
  1180. #ifdef WOLFSSL_SM3
  1181. case WC_HASH_TYPE_SM3:
  1182. ret = wc_Sm3GetFlags(&hash->sm3, flags);
  1183. break;
  1184. #endif
  1185. /* not supported */
  1186. case WC_HASH_TYPE_MD5_SHA:
  1187. case WC_HASH_TYPE_MD2:
  1188. case WC_HASH_TYPE_MD4:
  1189. case WC_HASH_TYPE_BLAKE2B:
  1190. case WC_HASH_TYPE_BLAKE2S:
  1191. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
  1192. case WC_HASH_TYPE_SHAKE128:
  1193. #endif
  1194. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
  1195. case WC_HASH_TYPE_SHAKE256:
  1196. #endif
  1197. case WC_HASH_TYPE_NONE:
  1198. default:
  1199. ret = BAD_FUNC_ARG;
  1200. };
  1201. return ret;
  1202. }
  1203. #endif /* WOLFSSL_HASH_FLAGS */
  1204. #if !defined(WOLFSSL_TI_HASH)
  1205. #if !defined(NO_MD5)
  1206. int wc_Md5Hash(const byte* data, word32 len, byte* hash)
  1207. {
  1208. int ret;
  1209. #ifdef WOLFSSL_SMALL_STACK
  1210. wc_Md5* md5;
  1211. #else
  1212. wc_Md5 md5[1];
  1213. #endif
  1214. #ifdef WOLFSSL_SMALL_STACK
  1215. md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1216. if (md5 == NULL)
  1217. return MEMORY_E;
  1218. #endif
  1219. if ((ret = wc_InitMd5(md5)) != 0) {
  1220. WOLFSSL_MSG("InitMd5 failed");
  1221. }
  1222. else {
  1223. if ((ret = wc_Md5Update(md5, data, len)) != 0) {
  1224. WOLFSSL_MSG("Md5Update failed");
  1225. }
  1226. else if ((ret = wc_Md5Final(md5, hash)) != 0) {
  1227. WOLFSSL_MSG("Md5Final failed");
  1228. }
  1229. wc_Md5Free(md5);
  1230. }
  1231. #ifdef WOLFSSL_SMALL_STACK
  1232. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1233. #endif
  1234. return ret;
  1235. }
  1236. #endif /* !NO_MD5 */
  1237. #if !defined(NO_SHA)
  1238. int wc_ShaHash(const byte* data, word32 len, byte* hash)
  1239. {
  1240. int ret = 0;
  1241. #ifdef WOLFSSL_SMALL_STACK
  1242. wc_Sha* sha;
  1243. #else
  1244. wc_Sha sha[1];
  1245. #endif
  1246. int devId = INVALID_DEVID;
  1247. #ifdef WOLFSSL_SMALL_STACK
  1248. sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1249. if (sha == NULL)
  1250. return MEMORY_E;
  1251. #endif
  1252. #ifdef WOLF_CRYPTO_CB
  1253. /* only use devId if its not an empty hash */
  1254. if (data != NULL && len > 0)
  1255. devId = wc_CryptoCb_GetDevIdAtIndex(0);
  1256. #endif
  1257. if ((ret = wc_InitSha_ex(sha, NULL, devId)) != 0) {
  1258. WOLFSSL_MSG("InitSha failed");
  1259. }
  1260. else {
  1261. if ((ret = wc_ShaUpdate(sha, data, len)) != 0) {
  1262. WOLFSSL_MSG("ShaUpdate failed");
  1263. }
  1264. else if ((ret = wc_ShaFinal(sha, hash)) != 0) {
  1265. WOLFSSL_MSG("ShaFinal failed");
  1266. }
  1267. wc_ShaFree(sha);
  1268. }
  1269. #ifdef WOLFSSL_SMALL_STACK
  1270. XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1271. #endif
  1272. return ret;
  1273. }
  1274. #endif /* !NO_SHA */
  1275. #if defined(WOLFSSL_SHA224)
  1276. int wc_Sha224Hash(const byte* data, word32 len, byte* hash)
  1277. {
  1278. int ret = 0;
  1279. #ifdef WOLFSSL_SMALL_STACK
  1280. wc_Sha224* sha224;
  1281. #else
  1282. wc_Sha224 sha224[1];
  1283. #endif
  1284. #ifdef WOLFSSL_SMALL_STACK
  1285. sha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
  1286. DYNAMIC_TYPE_TMP_BUFFER);
  1287. if (sha224 == NULL)
  1288. return MEMORY_E;
  1289. #endif
  1290. if ((ret = wc_InitSha224(sha224)) != 0) {
  1291. WOLFSSL_MSG("InitSha224 failed");
  1292. }
  1293. else {
  1294. if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
  1295. WOLFSSL_MSG("Sha224Update failed");
  1296. }
  1297. else if ((ret = wc_Sha224Final(sha224, hash)) != 0) {
  1298. WOLFSSL_MSG("Sha224Final failed");
  1299. }
  1300. wc_Sha224Free(sha224);
  1301. }
  1302. #ifdef WOLFSSL_SMALL_STACK
  1303. XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1304. #endif
  1305. return ret;
  1306. }
  1307. #endif /* WOLFSSL_SHA224 */
  1308. #if !defined(NO_SHA256)
  1309. int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
  1310. {
  1311. int ret = 0;
  1312. #ifdef WOLFSSL_SMALL_STACK
  1313. wc_Sha256* sha256;
  1314. #else
  1315. wc_Sha256 sha256[1];
  1316. #endif
  1317. int devId = INVALID_DEVID;
  1318. #ifdef WOLFSSL_SMALL_STACK
  1319. sha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
  1320. DYNAMIC_TYPE_TMP_BUFFER);
  1321. if (sha256 == NULL)
  1322. return MEMORY_E;
  1323. #endif
  1324. #ifdef WOLF_CRYPTO_CB
  1325. /* only use devId if its not an empty hash */
  1326. if (data != NULL && len > 0)
  1327. devId = wc_CryptoCb_GetDevIdAtIndex(0);
  1328. #endif
  1329. if ((ret = wc_InitSha256_ex(sha256, NULL, devId)) != 0) {
  1330. WOLFSSL_MSG("InitSha256 failed");
  1331. }
  1332. else {
  1333. if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
  1334. WOLFSSL_MSG("Sha256Update failed");
  1335. }
  1336. else if ((ret = wc_Sha256Final(sha256, hash)) != 0) {
  1337. WOLFSSL_MSG("Sha256Final failed");
  1338. }
  1339. wc_Sha256Free(sha256);
  1340. }
  1341. #ifdef WOLFSSL_SMALL_STACK
  1342. XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1343. #endif
  1344. return ret;
  1345. }
  1346. #endif /* !NO_SHA256 */
  1347. #endif /* !defined(WOLFSSL_TI_HASH) */
  1348. #if defined(WOLFSSL_SHA512)
  1349. int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
  1350. {
  1351. int ret = 0;
  1352. #ifdef WOLFSSL_SMALL_STACK
  1353. wc_Sha512* sha512;
  1354. #else
  1355. wc_Sha512 sha512[1];
  1356. #endif
  1357. #ifdef WOLFSSL_SMALL_STACK
  1358. sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
  1359. DYNAMIC_TYPE_TMP_BUFFER);
  1360. if (sha512 == NULL)
  1361. return MEMORY_E;
  1362. #endif
  1363. if ((ret = wc_InitSha512(sha512)) != 0) {
  1364. WOLFSSL_MSG("InitSha512 failed");
  1365. }
  1366. else {
  1367. if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
  1368. WOLFSSL_MSG("Sha512Update failed");
  1369. }
  1370. else if ((ret = wc_Sha512Final(sha512, hash)) != 0) {
  1371. WOLFSSL_MSG("Sha512Final failed");
  1372. }
  1373. wc_Sha512Free(sha512);
  1374. }
  1375. #ifdef WOLFSSL_SMALL_STACK
  1376. XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1377. #endif
  1378. return ret;
  1379. }
  1380. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  1381. #ifndef WOLFSSL_NOSHA512_224
  1382. int wc_Sha512_224Hash(const byte* data, word32 len, byte* hash)
  1383. {
  1384. int ret = 0;
  1385. #ifdef WOLFSSL_SMALL_STACK
  1386. wc_Sha512* sha512;
  1387. #else
  1388. wc_Sha512 sha512[1];
  1389. #endif
  1390. #ifdef WOLFSSL_SMALL_STACK
  1391. sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
  1392. DYNAMIC_TYPE_TMP_BUFFER);
  1393. if (sha512 == NULL)
  1394. return MEMORY_E;
  1395. #endif
  1396. if ((ret = wc_InitSha512_224(sha512)) != 0) {
  1397. WOLFSSL_MSG("wc_InitSha512_224 failed");
  1398. }
  1399. else {
  1400. if ((ret = wc_Sha512_224Update(sha512, data, len)) != 0) {
  1401. WOLFSSL_MSG("wc_Sha512_224_Update failed");
  1402. }
  1403. else if ((ret = wc_Sha512_224Final(sha512, hash)) != 0) {
  1404. WOLFSSL_MSG("wc_Sha512_224_Final failed");
  1405. }
  1406. wc_Sha512_224Free(sha512);
  1407. }
  1408. #ifdef WOLFSSL_SMALL_STACK
  1409. XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1410. #endif
  1411. return ret;
  1412. }
  1413. #endif /* !WOLFSSL_NOSHA512_224 */
  1414. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  1415. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  1416. #ifndef WOLFSSL_NOSHA512_256
  1417. int wc_Sha512_256Hash(const byte* data, word32 len, byte* hash)
  1418. {
  1419. int ret = 0;
  1420. #ifdef WOLFSSL_SMALL_STACK
  1421. wc_Sha512* sha512;
  1422. #else
  1423. wc_Sha512 sha512[1];
  1424. #endif
  1425. #ifdef WOLFSSL_SMALL_STACK
  1426. sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
  1427. DYNAMIC_TYPE_TMP_BUFFER);
  1428. if (sha512 == NULL)
  1429. return MEMORY_E;
  1430. #endif
  1431. if ((ret = wc_InitSha512_256(sha512)) != 0) {
  1432. WOLFSSL_MSG("wc_InitSha512_256 failed");
  1433. }
  1434. else {
  1435. if ((ret = wc_Sha512_256Update(sha512, data, len)) != 0) {
  1436. WOLFSSL_MSG("wc_Sha512_256_Update failed");
  1437. }
  1438. else if ((ret = wc_Sha512_256Final(sha512, hash)) != 0) {
  1439. WOLFSSL_MSG("wc_Sha512_256_Final failed");
  1440. }
  1441. wc_Sha512_256Free(sha512);
  1442. }
  1443. #ifdef WOLFSSL_SMALL_STACK
  1444. XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1445. #endif
  1446. return ret;
  1447. }
  1448. #endif /* !WOLFSSL_NOSHA512_256 */
  1449. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  1450. #endif /* WOLFSSL_SHA512 */
  1451. #if defined(WOLFSSL_SHA384)
  1452. int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
  1453. {
  1454. int ret = 0;
  1455. #ifdef WOLFSSL_SMALL_STACK
  1456. wc_Sha384* sha384;
  1457. #else
  1458. wc_Sha384 sha384[1];
  1459. #endif
  1460. #ifdef WOLFSSL_SMALL_STACK
  1461. sha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL,
  1462. DYNAMIC_TYPE_TMP_BUFFER);
  1463. if (sha384 == NULL)
  1464. return MEMORY_E;
  1465. #endif
  1466. if ((ret = wc_InitSha384(sha384)) != 0) {
  1467. WOLFSSL_MSG("InitSha384 failed");
  1468. }
  1469. else {
  1470. if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
  1471. WOLFSSL_MSG("Sha384Update failed");
  1472. }
  1473. else if ((ret = wc_Sha384Final(sha384, hash)) != 0) {
  1474. WOLFSSL_MSG("Sha384Final failed");
  1475. }
  1476. wc_Sha384Free(sha384);
  1477. }
  1478. #ifdef WOLFSSL_SMALL_STACK
  1479. XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1480. #endif
  1481. return ret;
  1482. }
  1483. #endif /* WOLFSSL_SHA384 */
  1484. #if defined(WOLFSSL_SHA3)
  1485. #if !defined(WOLFSSL_NOSHA3_224)
  1486. int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash)
  1487. {
  1488. int ret = 0;
  1489. #ifdef WOLFSSL_SMALL_STACK
  1490. wc_Sha3* sha3;
  1491. #else
  1492. wc_Sha3 sha3[1];
  1493. #endif
  1494. #ifdef WOLFSSL_SMALL_STACK
  1495. sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
  1496. DYNAMIC_TYPE_TMP_BUFFER);
  1497. if (sha3 == NULL)
  1498. return MEMORY_E;
  1499. #endif
  1500. if ((ret = wc_InitSha3_224(sha3, NULL, INVALID_DEVID)) != 0) {
  1501. WOLFSSL_MSG("InitSha3_224 failed");
  1502. }
  1503. else {
  1504. if ((ret = wc_Sha3_224_Update(sha3, data, len)) != 0) {
  1505. WOLFSSL_MSG("Sha3_224_Update failed");
  1506. }
  1507. else if ((ret = wc_Sha3_224_Final(sha3, hash)) != 0) {
  1508. WOLFSSL_MSG("Sha3_224_Final failed");
  1509. }
  1510. wc_Sha3_224_Free(sha3);
  1511. }
  1512. #ifdef WOLFSSL_SMALL_STACK
  1513. XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1514. #endif
  1515. return ret;
  1516. }
  1517. #endif /* !WOLFSSL_NOSHA3_224 */
  1518. #if !defined(WOLFSSL_NOSHA3_256)
  1519. int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash)
  1520. {
  1521. int ret = 0;
  1522. #ifdef WOLFSSL_SMALL_STACK
  1523. wc_Sha3* sha3;
  1524. #else
  1525. wc_Sha3 sha3[1];
  1526. #endif
  1527. #ifdef WOLFSSL_SMALL_STACK
  1528. sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
  1529. DYNAMIC_TYPE_TMP_BUFFER);
  1530. if (sha3 == NULL)
  1531. return MEMORY_E;
  1532. #endif
  1533. if ((ret = wc_InitSha3_256(sha3, NULL, INVALID_DEVID)) != 0) {
  1534. WOLFSSL_MSG("InitSha3_256 failed");
  1535. }
  1536. else {
  1537. if ((ret = wc_Sha3_256_Update(sha3, data, len)) != 0) {
  1538. WOLFSSL_MSG("Sha3_256_Update failed");
  1539. }
  1540. else if ((ret = wc_Sha3_256_Final(sha3, hash)) != 0) {
  1541. WOLFSSL_MSG("Sha3_256_Final failed");
  1542. }
  1543. wc_Sha3_256_Free(sha3);
  1544. }
  1545. #ifdef WOLFSSL_SMALL_STACK
  1546. XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1547. #endif
  1548. return ret;
  1549. }
  1550. #endif /* !WOLFSSL_NOSHA3_256 */
  1551. #if !defined(WOLFSSL_NOSHA3_384)
  1552. int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash)
  1553. {
  1554. int ret = 0;
  1555. #ifdef WOLFSSL_SMALL_STACK
  1556. wc_Sha3* sha3;
  1557. #else
  1558. wc_Sha3 sha3[1];
  1559. #endif
  1560. #ifdef WOLFSSL_SMALL_STACK
  1561. sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
  1562. DYNAMIC_TYPE_TMP_BUFFER);
  1563. if (sha3 == NULL)
  1564. return MEMORY_E;
  1565. #endif
  1566. if ((ret = wc_InitSha3_384(sha3, NULL, INVALID_DEVID)) != 0) {
  1567. WOLFSSL_MSG("InitSha3_384 failed");
  1568. }
  1569. else {
  1570. if ((ret = wc_Sha3_384_Update(sha3, data, len)) != 0) {
  1571. WOLFSSL_MSG("Sha3_384_Update failed");
  1572. }
  1573. else if ((ret = wc_Sha3_384_Final(sha3, hash)) != 0) {
  1574. WOLFSSL_MSG("Sha3_384_Final failed");
  1575. }
  1576. wc_Sha3_384_Free(sha3);
  1577. }
  1578. #ifdef WOLFSSL_SMALL_STACK
  1579. XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1580. #endif
  1581. return ret;
  1582. }
  1583. #endif /* !WOLFSSL_NOSHA3_384 */
  1584. #if !defined(WOLFSSL_NOSHA3_512)
  1585. int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash)
  1586. {
  1587. int ret = 0;
  1588. #ifdef WOLFSSL_SMALL_STACK
  1589. wc_Sha3* sha3;
  1590. #else
  1591. wc_Sha3 sha3[1];
  1592. #endif
  1593. #ifdef WOLFSSL_SMALL_STACK
  1594. sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
  1595. DYNAMIC_TYPE_TMP_BUFFER);
  1596. if (sha3 == NULL)
  1597. return MEMORY_E;
  1598. #endif
  1599. if ((ret = wc_InitSha3_512(sha3, NULL, INVALID_DEVID)) != 0) {
  1600. WOLFSSL_MSG("InitSha3_512 failed");
  1601. }
  1602. else {
  1603. if ((ret = wc_Sha3_512_Update(sha3, data, len)) != 0) {
  1604. WOLFSSL_MSG("Sha3_512_Update failed");
  1605. }
  1606. else if ((ret = wc_Sha3_512_Final(sha3, hash)) != 0) {
  1607. WOLFSSL_MSG("Sha3_512_Final failed");
  1608. }
  1609. wc_Sha3_512_Free(sha3);
  1610. }
  1611. #ifdef WOLFSSL_SMALL_STACK
  1612. XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1613. #endif
  1614. return ret;
  1615. }
  1616. #endif /* !WOLFSSL_NOSHA3_512 */
  1617. #ifdef WOLFSSL_SHAKE128
  1618. int wc_Shake128Hash(const byte* data, word32 len, byte* hash,
  1619. word32 hashLen)
  1620. {
  1621. int ret = 0;
  1622. #ifdef WOLFSSL_SMALL_STACK
  1623. wc_Shake* shake;
  1624. #else
  1625. wc_Shake shake[1];
  1626. #endif
  1627. #ifdef WOLFSSL_SMALL_STACK
  1628. shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL,
  1629. DYNAMIC_TYPE_TMP_BUFFER);
  1630. if (shake == NULL)
  1631. return MEMORY_E;
  1632. #endif
  1633. if ((ret = wc_InitShake128(shake, NULL, INVALID_DEVID)) != 0) {
  1634. WOLFSSL_MSG("InitShake128 failed");
  1635. }
  1636. else {
  1637. if ((ret = wc_Shake128_Update(shake, data, len)) != 0) {
  1638. WOLFSSL_MSG("Shake128_Update failed");
  1639. }
  1640. else if ((ret = wc_Shake128_Final(shake, hash, hashLen)) != 0) {
  1641. WOLFSSL_MSG("Shake128_Final failed");
  1642. }
  1643. wc_Shake128_Free(shake);
  1644. }
  1645. #ifdef WOLFSSL_SMALL_STACK
  1646. XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1647. #endif
  1648. return ret;
  1649. }
  1650. #endif /* WOLFSSL_SHAKE_128 */
  1651. #ifdef WOLFSSL_SHAKE256
  1652. int wc_Shake256Hash(const byte* data, word32 len, byte* hash,
  1653. word32 hashLen)
  1654. {
  1655. int ret = 0;
  1656. #ifdef WOLFSSL_SMALL_STACK
  1657. wc_Shake* shake;
  1658. #else
  1659. wc_Shake shake[1];
  1660. #endif
  1661. #ifdef WOLFSSL_SMALL_STACK
  1662. shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL,
  1663. DYNAMIC_TYPE_TMP_BUFFER);
  1664. if (shake == NULL)
  1665. return MEMORY_E;
  1666. #endif
  1667. if ((ret = wc_InitShake256(shake, NULL, INVALID_DEVID)) != 0) {
  1668. WOLFSSL_MSG("InitShake256 failed");
  1669. }
  1670. else {
  1671. if ((ret = wc_Shake256_Update(shake, data, len)) != 0) {
  1672. WOLFSSL_MSG("Shake256_Update failed");
  1673. }
  1674. else if ((ret = wc_Shake256_Final(shake, hash, hashLen)) != 0) {
  1675. WOLFSSL_MSG("Shake256_Final failed");
  1676. }
  1677. wc_Shake256_Free(shake);
  1678. }
  1679. #ifdef WOLFSSL_SMALL_STACK
  1680. XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1681. #endif
  1682. return ret;
  1683. }
  1684. #endif /* WOLFSSL_SHAKE_256 */
  1685. #endif /* WOLFSSL_SHA3 */
  1686. #ifdef WOLFSSL_SM3
  1687. int wc_Sm3Hash(const byte* data, word32 len, byte* hash)
  1688. {
  1689. int ret = 0;
  1690. #ifdef WOLFSSL_SMALL_STACK
  1691. wc_Sm3* sm3;
  1692. #else
  1693. wc_Sm3 sm3[1];
  1694. #endif
  1695. #ifdef WOLFSSL_SMALL_STACK
  1696. sm3 = (wc_Sm3*)XMALLOC(sizeof(wc_Sm3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1697. if (sm3 == NULL)
  1698. return MEMORY_E;
  1699. #endif
  1700. if ((ret = wc_InitSm3(sm3, NULL, INVALID_DEVID)) != 0) {
  1701. WOLFSSL_MSG("InitSm3 failed");
  1702. }
  1703. else {
  1704. if ((ret = wc_Sm3Update(sm3, data, len)) != 0) {
  1705. WOLFSSL_MSG("Sm3Update failed");
  1706. }
  1707. else if ((ret = wc_Sm3Final(sm3, hash)) != 0) {
  1708. WOLFSSL_MSG("Sm3Final failed");
  1709. }
  1710. wc_Sm3Free(sm3);
  1711. }
  1712. #ifdef WOLFSSL_SMALL_STACK
  1713. XFREE(sm3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1714. #endif
  1715. return ret;
  1716. }
  1717. #endif /* !WOLFSSL_NOSHA3_224 */
  1718. #endif /* !NO_HASH_WRAPPER */
  1719. #ifdef WOLFSSL_HASH_KEEP
  1720. int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in,
  1721. int inSz, void* heap)
  1722. {
  1723. if (*len < *used + inSz) {
  1724. if (*msg == NULL) {
  1725. *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1726. }
  1727. else {
  1728. byte* pt = (byte*)XREALLOC(*msg, *used + inSz, heap,
  1729. DYNAMIC_TYPE_TMP_BUFFER);
  1730. if (pt == NULL) {
  1731. return MEMORY_E;
  1732. }
  1733. *msg = pt;
  1734. }
  1735. if (*msg == NULL) {
  1736. return MEMORY_E;
  1737. }
  1738. *len = *used + inSz;
  1739. }
  1740. XMEMCPY(*msg + *used, in, inSz);
  1741. *used += inSz;
  1742. return 0;
  1743. }
  1744. #endif /* WOLFSSL_HASH_KEEP */