sha512.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /* sha512.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. #if (defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)) && \
  26. (!defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_ARMASM_NO_NEON)) && \
  27. !defined(WOLFSSL_PSOC6_CRYPTO)
  28. /* determine if we are using Espressif SHA hardware acceleration */
  29. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  30. #if defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
  31. #include "sdkconfig.h"
  32. /* Define a single keyword for simplicity & readability.
  33. *
  34. * By default the HW acceleration is on for ESP32 Chipsets,
  35. * but individual components can be turned off. See user_settings.h
  36. */
  37. #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  38. static const char* TAG = "wc_sha_512";
  39. #else
  40. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  41. #endif
  42. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  43. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  44. #define FIPS_NO_WRAPPERS
  45. #ifdef USE_WINDOWS_API
  46. #pragma code_seg(".fipsA$k")
  47. #pragma const_seg(".fipsB$k")
  48. #endif
  49. #endif
  50. #include <wolfssl/wolfcrypt/sha512.h>
  51. #include <wolfssl/wolfcrypt/error-crypt.h>
  52. #include <wolfssl/wolfcrypt/cpuid.h>
  53. #include <wolfssl/wolfcrypt/hash.h>
  54. #ifdef WOLF_CRYPTO_CB
  55. #include <wolfssl/wolfcrypt/cryptocb.h>
  56. #endif
  57. #ifdef WOLFSSL_IMXRT1170_CAAM
  58. #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
  59. #endif
  60. /* deprecated USE_SLOW_SHA2 (replaced with USE_SLOW_SHA512) */
  61. #if defined(USE_SLOW_SHA2) && !defined(USE_SLOW_SHA512)
  62. #define USE_SLOW_SHA512
  63. #endif
  64. #include <wolfssl/wolfcrypt/logging.h>
  65. #ifdef NO_INLINE
  66. #include <wolfssl/wolfcrypt/misc.h>
  67. #else
  68. #define WOLFSSL_MISC_INCLUDED
  69. #include <wolfcrypt/src/misc.c>
  70. #endif
  71. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  72. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  73. #endif
  74. #if defined(USE_INTEL_SPEEDUP)
  75. #if defined(__GNUC__) && ((__GNUC__ < 4) || \
  76. (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
  77. #undef NO_AVX2_SUPPORT
  78. #define NO_AVX2_SUPPORT
  79. #endif
  80. #if defined(__clang__) && ((__clang_major__ < 3) || \
  81. (__clang_major__ == 3 && __clang_minor__ <= 5))
  82. #define NO_AVX2_SUPPORT
  83. #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
  84. #undef NO_AVX2_SUPPORT
  85. #endif
  86. #define HAVE_INTEL_AVX1
  87. #ifndef NO_AVX2_SUPPORT
  88. #define HAVE_INTEL_AVX2
  89. #endif
  90. #endif
  91. #if defined(HAVE_INTEL_AVX1)
  92. /* #define DEBUG_XMM */
  93. #endif
  94. #if defined(HAVE_INTEL_AVX2)
  95. #define HAVE_INTEL_RORX
  96. /* #define DEBUG_YMM */
  97. #endif
  98. #if defined(HAVE_BYTEREVERSE64) && \
  99. !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
  100. #define ByteReverseWords64(out, in, size) ByteReverseWords64_1(out, size)
  101. #define ByteReverseWords64_1(buf, size) \
  102. { unsigned int i ;\
  103. for(i=0; i< size/sizeof(word64); i++){\
  104. __asm__ volatile("bswapq %0":"+r"(buf[i])::) ;\
  105. }\
  106. }
  107. #endif
  108. #if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  109. !defined(WOLFSSL_QNX_CAAM)
  110. /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
  111. #elif defined(WOLFSSL_SILABS_SHA384)
  112. /* functions defined in wolfcrypt/src/port/silabs/silabs_hash.c */
  113. #elif defined(WOLFSSL_KCAPI_HASH)
  114. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  115. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  116. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  117. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  118. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  119. int wc_InitSha512(wc_Sha512* sha512)
  120. {
  121. if (sha512 == NULL)
  122. return BAD_FUNC_ARG;
  123. return se050_hash_init(&sha512->se050Ctx, NULL);
  124. }
  125. int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
  126. {
  127. if (sha512 == NULL) {
  128. return BAD_FUNC_ARG;
  129. }
  130. (void)devId;
  131. return se050_hash_init(&sha512->se050Ctx, heap);
  132. }
  133. int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
  134. {
  135. return se050_hash_update(&sha512->se050Ctx, data, len);
  136. }
  137. int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
  138. {
  139. int ret = 0;
  140. int devId = INVALID_DEVID;
  141. if (sha512 == NULL) {
  142. return BAD_FUNC_ARG;
  143. }
  144. #ifdef WOLF_CRYPTO_CB
  145. devId = sha512->devId;
  146. #endif
  147. ret = se050_hash_final(&sha512->se050Ctx, hash, WC_SHA512_DIGEST_SIZE,
  148. kAlgorithm_SSS_SHA512);
  149. return ret;
  150. }
  151. int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
  152. {
  153. int ret = 0;
  154. int devId = INVALID_DEVID;
  155. if (sha512 == NULL) {
  156. return BAD_FUNC_ARG;
  157. }
  158. #ifdef WOLF_CRYPTO_CB
  159. devId = sha512->devId;
  160. #endif
  161. ret = se050_hash_final(&sha512->se050Ctx, hash, WC_SHA512_DIGEST_SIZE,
  162. kAlgorithm_SSS_SHA512);
  163. return ret;
  164. }
  165. void wc_Sha512Free(wc_Sha512* sha512)
  166. {
  167. se050_hash_free(&sha512->se050Ctx);
  168. }
  169. #else
  170. #ifdef WOLFSSL_SHA512
  171. static int InitSha512(wc_Sha512* sha512)
  172. {
  173. if (sha512 == NULL)
  174. return BAD_FUNC_ARG;
  175. sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
  176. sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
  177. sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
  178. sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
  179. sha512->digest[4] = W64LIT(0x510e527fade682d1);
  180. sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
  181. sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
  182. sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
  183. sha512->buffLen = 0;
  184. sha512->loLen = 0;
  185. sha512->hiLen = 0;
  186. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  187. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  188. /* HW needs to be carefully initialized, taking into account soft copy.
  189. ** If already in use; copy may revert to SW as needed. */
  190. esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512);
  191. #endif
  192. #ifdef WOLFSSL_HASH_FLAGS
  193. sha512->flags = 0;
  194. #endif
  195. return 0;
  196. }
  197. #if !defined(WOLFSSL_NOSHA512_224) && \
  198. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  199. /**
  200. * Initialize given wc_Sha512 structure with value specific to sha512/224.
  201. * Note that sha512/224 has different initial hash value from sha512.
  202. * The initial hash value consists of eight 64bit words. They are given
  203. * in FIPS180-4.
  204. */
  205. static int InitSha512_224(wc_Sha512* sha512)
  206. {
  207. if (sha512 == NULL)
  208. return BAD_FUNC_ARG;
  209. sha512->digest[0] = W64LIT(0x8c3d37c819544da2);
  210. sha512->digest[1] = W64LIT(0x73e1996689dcd4d6);
  211. sha512->digest[2] = W64LIT(0x1dfab7ae32ff9c82);
  212. sha512->digest[3] = W64LIT(0x679dd514582f9fcf);
  213. sha512->digest[4] = W64LIT(0x0f6d2b697bd44da8);
  214. sha512->digest[5] = W64LIT(0x77e36f7304c48942);
  215. sha512->digest[6] = W64LIT(0x3f9d85a86a1d36c8);
  216. sha512->digest[7] = W64LIT(0x1112e6ad91d692a1);
  217. sha512->buffLen = 0;
  218. sha512->loLen = 0;
  219. sha512->hiLen = 0;
  220. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  221. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  222. /* HW needs to be carefully initialized, taking into account soft copy.
  223. ** If already in use; copy may revert to SW as needed.
  224. **
  225. ** Note for original ESP32, there's no HW for SHA512/224
  226. */
  227. esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512_224);
  228. #endif
  229. #ifdef WOLFSSL_HASH_FLAGS
  230. sha512->flags = 0;
  231. #endif
  232. return 0;
  233. }
  234. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  235. #if !defined(WOLFSSL_NOSHA512_256) && \
  236. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  237. /**
  238. * Initialize given wc_Sha512 structure with value specific to sha512/256.
  239. * Note that sha512/256 has different initial hash value from sha512.
  240. * The initial hash value consists of eight 64bit words. They are given
  241. * in FIPS180-4.
  242. */
  243. static int InitSha512_256(wc_Sha512* sha512)
  244. {
  245. if (sha512 == NULL)
  246. return BAD_FUNC_ARG;
  247. sha512->digest[0] = W64LIT(0x22312194fc2bf72c);
  248. sha512->digest[1] = W64LIT(0x9f555fa3c84c64c2);
  249. sha512->digest[2] = W64LIT(0x2393b86b6f53b151);
  250. sha512->digest[3] = W64LIT(0x963877195940eabd);
  251. sha512->digest[4] = W64LIT(0x96283ee2a88effe3);
  252. sha512->digest[5] = W64LIT(0xbe5e1e2553863992);
  253. sha512->digest[6] = W64LIT(0x2b0199fc2c85b8aa);
  254. sha512->digest[7] = W64LIT(0x0eb72ddc81c52ca2);
  255. sha512->buffLen = 0;
  256. sha512->loLen = 0;
  257. sha512->hiLen = 0;
  258. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  259. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  260. /* HW needs to be carefully initialized, taking into account soft copy.
  261. ** If already in use; copy may revert to SW as needed.
  262. **
  263. ** Note for original ESP32, there's no HW for SHA512/2256.
  264. */
  265. esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512_256);
  266. #endif
  267. #ifdef WOLFSSL_HASH_FLAGS
  268. sha512->flags = 0;
  269. #endif
  270. return 0;
  271. }
  272. #endif /* !WOLFSSL_NOSHA512_256 && !FIPS... */
  273. #endif /* WOLFSSL_SHA512 */
  274. /* Hardware Acceleration */
  275. #if defined(USE_INTEL_SPEEDUP) && \
  276. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  277. #ifdef WOLFSSL_SHA512
  278. /*****
  279. Intel AVX1/AVX2 Macro Control Structure
  280. #if defined(HAVE_INTEL_SPEEDUP)
  281. #define HAVE_INTEL_AVX1
  282. #define HAVE_INTEL_AVX2
  283. #endif
  284. int InitSha512(wc_Sha512* sha512) {
  285. Save/Recover XMM, YMM
  286. ...
  287. Check Intel AVX cpuid flags
  288. }
  289. #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
  290. Transform_Sha512_AVX1(); # Function prototype
  291. Transform_Sha512_AVX2(); #
  292. #endif
  293. _Transform_Sha512() { # Native Transform Function body
  294. }
  295. int Sha512Update() {
  296. Save/Recover XMM, YMM
  297. ...
  298. }
  299. int Sha512Final() {
  300. Save/Recover XMM, YMM
  301. ...
  302. }
  303. #if defined(HAVE_INTEL_AVX1)
  304. XMM Instructions/INLINE asm Definitions
  305. #endif
  306. #if defined(HAVE_INTEL_AVX2)
  307. YMM Instructions/INLINE asm Definitions
  308. #endif
  309. #if defined(HAVE_INTEL_AVX1)
  310. int Transform_Sha512_AVX1() {
  311. Stitched Message Sched/Round
  312. }
  313. #endif
  314. #if defined(HAVE_INTEL_AVX2)
  315. int Transform_Sha512_AVX2() {
  316. Stitched Message Sched/Round
  317. }
  318. #endif
  319. */
  320. /* Each platform needs to query info type 1 from cpuid to see if aesni is
  321. * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
  322. */
  323. #ifdef __cplusplus
  324. extern "C" {
  325. #endif
  326. #if defined(HAVE_INTEL_AVX1)
  327. extern int Transform_Sha512_AVX1(wc_Sha512 *sha512);
  328. extern int Transform_Sha512_AVX1_Len(wc_Sha512 *sha512, word32 len);
  329. #endif
  330. #if defined(HAVE_INTEL_AVX2)
  331. extern int Transform_Sha512_AVX2(wc_Sha512 *sha512);
  332. extern int Transform_Sha512_AVX2_Len(wc_Sha512 *sha512, word32 len);
  333. #if defined(HAVE_INTEL_RORX)
  334. extern int Transform_Sha512_AVX1_RORX(wc_Sha512 *sha512);
  335. extern int Transform_Sha512_AVX1_RORX_Len(wc_Sha512 *sha512,
  336. word32 len);
  337. extern int Transform_Sha512_AVX2_RORX(wc_Sha512 *sha512);
  338. extern int Transform_Sha512_AVX2_RORX_Len(wc_Sha512 *sha512,
  339. word32 len);
  340. #endif
  341. #endif
  342. #ifdef __cplusplus
  343. } /* extern "C" */
  344. #endif
  345. static int _Transform_Sha512(wc_Sha512 *sha512);
  346. static int (*Transform_Sha512_p)(wc_Sha512* sha512) = _Transform_Sha512;
  347. static int (*Transform_Sha512_Len_p)(wc_Sha512* sha512, word32 len) = NULL;
  348. static int transform_check = 0;
  349. static word32 intel_flags;
  350. static int Transform_Sha512_is_vectorized = 0;
  351. static WC_INLINE int Transform_Sha512(wc_Sha512 *sha512) {
  352. int ret;
  353. ret = (*Transform_Sha512_p)(sha512);
  354. return ret;
  355. }
  356. static WC_INLINE int Transform_Sha512_Len(wc_Sha512 *sha512, word32 len) {
  357. int ret;
  358. ret = (*Transform_Sha512_Len_p)(sha512, len);
  359. return ret;
  360. }
  361. static void Sha512_SetTransform(void)
  362. {
  363. if (transform_check)
  364. return;
  365. intel_flags = cpuid_get_flags();
  366. #if defined(HAVE_INTEL_AVX2)
  367. if (IS_INTEL_AVX2(intel_flags)) {
  368. #ifdef HAVE_INTEL_RORX
  369. if (IS_INTEL_BMI2(intel_flags)) {
  370. Transform_Sha512_p = Transform_Sha512_AVX2_RORX;
  371. Transform_Sha512_Len_p = Transform_Sha512_AVX2_RORX_Len;
  372. Transform_Sha512_is_vectorized = 1;
  373. }
  374. else
  375. #endif
  376. if (1) {
  377. Transform_Sha512_p = Transform_Sha512_AVX2;
  378. Transform_Sha512_Len_p = Transform_Sha512_AVX2_Len;
  379. Transform_Sha512_is_vectorized = 1;
  380. }
  381. #ifdef HAVE_INTEL_RORX
  382. else {
  383. Transform_Sha512_p = Transform_Sha512_AVX1_RORX;
  384. Transform_Sha512_Len_p = Transform_Sha512_AVX1_RORX_Len;
  385. Transform_Sha512_is_vectorized = 1;
  386. }
  387. #endif
  388. }
  389. else
  390. #endif
  391. #if defined(HAVE_INTEL_AVX1)
  392. if (IS_INTEL_AVX1(intel_flags)) {
  393. Transform_Sha512_p = Transform_Sha512_AVX1;
  394. Transform_Sha512_Len_p = Transform_Sha512_AVX1_Len;
  395. Transform_Sha512_is_vectorized = 1;
  396. }
  397. else
  398. #endif
  399. {
  400. Transform_Sha512_p = _Transform_Sha512;
  401. Transform_Sha512_is_vectorized = 1;
  402. }
  403. transform_check = 1;
  404. }
  405. #endif /* WOLFSSL_SHA512 */
  406. #else
  407. #define Transform_Sha512(sha512) _Transform_Sha512(sha512)
  408. #endif
  409. #ifdef WOLFSSL_SHA512
  410. static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
  411. int (*initfp)(wc_Sha512*))
  412. {
  413. int ret = 0;
  414. if (sha512 == NULL) {
  415. return BAD_FUNC_ARG;
  416. }
  417. sha512->heap = heap;
  418. #ifdef WOLFSSL_SMALL_STACK_CACHE
  419. sha512->W = NULL;
  420. #endif
  421. #ifdef WOLF_CRYPTO_CB
  422. sha512->devId = devId;
  423. sha512->devCtx = NULL;
  424. #endif
  425. /* call the initialization function pointed to by initfp */
  426. ret = initfp(sha512);
  427. if (ret != 0)
  428. return ret;
  429. #if defined(USE_INTEL_SPEEDUP) && \
  430. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  431. Sha512_SetTransform();
  432. #endif
  433. #ifdef WOLFSSL_HASH_KEEP
  434. sha512->msg = NULL;
  435. sha512->len = 0;
  436. sha512->used = 0;
  437. #endif
  438. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  439. ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
  440. WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
  441. #else
  442. (void)devId;
  443. #endif /* WOLFSSL_ASYNC_CRYPT */
  444. #ifdef WOLFSSL_IMXRT1170_CAAM
  445. ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
  446. #endif
  447. return ret;
  448. } /* InitSha512_Family */
  449. int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
  450. {
  451. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  452. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  453. if (sha512->ctx.mode != ESP32_SHA_INIT) {
  454. ESP_LOGV(TAG, "Set ctx mode from prior value: "
  455. "%d", sha512->ctx.mode);
  456. }
  457. /* We know this is a fresh, uninitialized item, so set to INIT */
  458. sha512->ctx.mode = ESP32_SHA_INIT;
  459. #endif
  460. return InitSha512_Family(sha512, heap, devId, InitSha512);
  461. }
  462. #if !defined(WOLFSSL_NOSHA512_224) && \
  463. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  464. int wc_InitSha512_224_ex(wc_Sha512* sha512, void* heap, int devId)
  465. {
  466. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  467. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  468. /* No SHA512/224 HW support is available, set to SW. */
  469. sha512->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
  470. #endif
  471. return InitSha512_Family(sha512, heap, devId, InitSha512_224);
  472. }
  473. #endif /* !WOLFSSL_NOSHA512_224 ... */
  474. #if !defined(WOLFSSL_NOSHA512_256) && \
  475. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  476. int wc_InitSha512_256_ex(wc_Sha512* sha512, void* heap, int devId)
  477. {
  478. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  479. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  480. /* No SHA512/256 HW support is available on ESP32, set to SW. */
  481. sha512->ctx.mode = ESP32_SHA_SW;
  482. #endif
  483. return InitSha512_Family(sha512, heap, devId, InitSha512_256);
  484. }
  485. #endif /* !WOLFSSL_NOSHA512_256 ... */
  486. #endif /* WOLFSSL_SHA512 */
  487. static const word64 K512[80] = {
  488. W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
  489. W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
  490. W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
  491. W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
  492. W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
  493. W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
  494. W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
  495. W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
  496. W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
  497. W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
  498. W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
  499. W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
  500. W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
  501. W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
  502. W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
  503. W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
  504. W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
  505. W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
  506. W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
  507. W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
  508. W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
  509. W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
  510. W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
  511. W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
  512. W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
  513. W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
  514. W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
  515. W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
  516. W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
  517. W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
  518. W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
  519. W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
  520. W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
  521. W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
  522. W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
  523. W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
  524. W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
  525. W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
  526. W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
  527. W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
  528. };
  529. #define blk0(i) (W[i] = sha512->buffer[i])
  530. #define blk2(i) (\
  531. W[ (i) & 15] += \
  532. s1(W[((i)-2) & 15])+ \
  533. W[((i)-7) & 15] + \
  534. s0(W[((i)-15) & 15]) \
  535. )
  536. #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  537. #define Maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  538. #define a(i) T[(0-(i)) & 7]
  539. #define b(i) T[(1-(i)) & 7]
  540. #define c(i) T[(2-(i)) & 7]
  541. #define d(i) T[(3-(i)) & 7]
  542. #define e(i) T[(4-(i)) & 7]
  543. #define f(i) T[(5-(i)) & 7]
  544. #define g(i) T[(6-(i)) & 7]
  545. #define h(i) T[(7-(i)) & 7]
  546. #define S0(x) (rotrFixed64(x,28) ^ rotrFixed64(x,34) ^ rotrFixed64(x,39))
  547. #define S1(x) (rotrFixed64(x,14) ^ rotrFixed64(x,18) ^ rotrFixed64(x,41))
  548. #define s0(x) (rotrFixed64(x,1) ^ rotrFixed64(x,8) ^ ((x)>>7))
  549. #define s1(x) (rotrFixed64(x,19) ^ rotrFixed64(x,61) ^ ((x)>>6))
  550. #define R(i) \
  551. h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[(i)+j] + (j ? blk2(i) : blk0(i)); \
  552. d(i) += h(i); \
  553. h(i) += S0(a(i)) + Maj(a(i),b(i),c(i))
  554. static int _Transform_Sha512(wc_Sha512* sha512)
  555. {
  556. const word64* K = K512;
  557. word32 j;
  558. word64 T[8];
  559. #ifdef WOLFSSL_SMALL_STACK_CACHE
  560. word64* W = sha512->W;
  561. if (W == NULL) {
  562. W = (word64*)XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
  563. if (W == NULL)
  564. return MEMORY_E;
  565. sha512->W = W;
  566. }
  567. #elif defined(WOLFSSL_SMALL_STACK)
  568. word64* W;
  569. W = (word64*) XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
  570. if (W == NULL)
  571. return MEMORY_E;
  572. #else
  573. word64 W[16];
  574. #endif
  575. /* Copy digest to working vars */
  576. XMEMCPY(T, sha512->digest, sizeof(T));
  577. #ifdef USE_SLOW_SHA512
  578. /* over twice as small, but 50% slower */
  579. /* 80 operations, not unrolled */
  580. for (j = 0; j < 80; j += 16) {
  581. int m;
  582. for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
  583. R(m);
  584. }
  585. }
  586. #else
  587. /* 80 operations, partially loop unrolled */
  588. for (j = 0; j < 80; j += 16) {
  589. R( 0); R( 1); R( 2); R( 3);
  590. R( 4); R( 5); R( 6); R( 7);
  591. R( 8); R( 9); R(10); R(11);
  592. R(12); R(13); R(14); R(15);
  593. }
  594. #endif /* USE_SLOW_SHA512 */
  595. /* Add the working vars back into digest */
  596. sha512->digest[0] += a(0);
  597. sha512->digest[1] += b(0);
  598. sha512->digest[2] += c(0);
  599. sha512->digest[3] += d(0);
  600. sha512->digest[4] += e(0);
  601. sha512->digest[5] += f(0);
  602. sha512->digest[6] += g(0);
  603. sha512->digest[7] += h(0);
  604. /* Wipe variables */
  605. ForceZero(W, sizeof(word64) * 16);
  606. ForceZero(T, sizeof(T));
  607. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
  608. XFREE(W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
  609. #endif
  610. return 0;
  611. }
  612. static WC_INLINE void AddLength(wc_Sha512* sha512, word32 len)
  613. {
  614. word64 tmp = sha512->loLen;
  615. if ( (sha512->loLen += len) < tmp)
  616. sha512->hiLen++; /* carry low to high */
  617. }
  618. static WC_INLINE int Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
  619. {
  620. int ret = 0;
  621. /* do block size increments */
  622. byte* local = (byte*)sha512->buffer;
  623. /* check that internal buffLen is valid */
  624. if (sha512->buffLen >= WC_SHA512_BLOCK_SIZE)
  625. return BUFFER_E;
  626. if (len == 0)
  627. return 0;
  628. AddLength(sha512, len);
  629. if (sha512->buffLen > 0) {
  630. word32 add = min(len, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
  631. if (add > 0) {
  632. XMEMCPY(&local[sha512->buffLen], data, add);
  633. sha512->buffLen += add;
  634. data += add;
  635. len -= add;
  636. }
  637. if (sha512->buffLen == WC_SHA512_BLOCK_SIZE) {
  638. #if defined(LITTLE_ENDIAN_ORDER)
  639. #if defined(USE_INTEL_SPEEDUP) && \
  640. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  641. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  642. #endif
  643. {
  644. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  645. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  646. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  647. ByteReverseWords64(sha512->buffer, sha512->buffer,
  648. WC_SHA512_BLOCK_SIZE);
  649. #endif
  650. }
  651. #endif
  652. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  653. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  654. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  655. ret = Transform_Sha512(sha512);
  656. #else
  657. if(sha512->ctx.mode == ESP32_SHA_INIT) {
  658. esp_sha_try_hw_lock(&sha512->ctx);
  659. }
  660. ret = esp_sha512_process(sha512);
  661. if(ret == 0 && sha512->ctx.mode == ESP32_SHA_SW){
  662. ByteReverseWords64(sha512->buffer, sha512->buffer,
  663. WC_SHA512_BLOCK_SIZE);
  664. ret = Transform_Sha512(sha512);
  665. }
  666. #endif
  667. if (ret == 0)
  668. sha512->buffLen = 0;
  669. else
  670. len = 0;
  671. }
  672. }
  673. #if defined(USE_INTEL_SPEEDUP) && \
  674. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  675. if (Transform_Sha512_Len_p != NULL) {
  676. word32 blocksLen = len & ~((word32)WC_SHA512_BLOCK_SIZE-1);
  677. if (blocksLen > 0) {
  678. sha512->data = data;
  679. /* Byte reversal performed in function if required. */
  680. Transform_Sha512_Len(sha512, blocksLen);
  681. data += blocksLen;
  682. len -= blocksLen;
  683. }
  684. }
  685. else
  686. #endif
  687. #if !defined(LITTLE_ENDIAN_ORDER) || (defined(USE_INTEL_SPEEDUP) && \
  688. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  689. {
  690. while (len >= WC_SHA512_BLOCK_SIZE) {
  691. XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
  692. data += WC_SHA512_BLOCK_SIZE;
  693. len -= WC_SHA512_BLOCK_SIZE;
  694. #if defined(USE_INTEL_SPEEDUP) && \
  695. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  696. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  697. {
  698. ByteReverseWords64(sha512->buffer, sha512->buffer,
  699. WC_SHA512_BLOCK_SIZE);
  700. }
  701. #endif
  702. /* Byte reversal performed in function if required. */
  703. ret = Transform_Sha512(sha512);
  704. if (ret != 0)
  705. break;
  706. }
  707. }
  708. #else
  709. {
  710. while (len >= WC_SHA512_BLOCK_SIZE) {
  711. XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
  712. data += WC_SHA512_BLOCK_SIZE;
  713. len -= WC_SHA512_BLOCK_SIZE;
  714. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  715. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  716. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  717. ByteReverseWords64(sha512->buffer, sha512->buffer,
  718. WC_SHA512_BLOCK_SIZE);
  719. #endif
  720. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  721. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  722. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  723. ret = Transform_Sha512(sha512);
  724. #else
  725. if(sha512->ctx.mode == ESP32_SHA_INIT) {
  726. esp_sha_try_hw_lock(&sha512->ctx);
  727. }
  728. if (sha512->ctx.mode == ESP32_SHA_SW) {
  729. ByteReverseWords64(sha512->buffer, sha512->buffer,
  730. WC_SHA512_BLOCK_SIZE);
  731. ret = Transform_Sha512(sha512);
  732. }
  733. else {
  734. ret = esp_sha512_process(sha512);
  735. }
  736. #endif
  737. if (ret != 0)
  738. break;
  739. } /* while (len >= WC_SHA512_BLOCK_SIZE) */
  740. }
  741. #endif
  742. if (ret == 0 && len > 0) {
  743. XMEMCPY(local, data, len);
  744. sha512->buffLen = len;
  745. }
  746. return ret;
  747. }
  748. #ifdef WOLFSSL_SHA512
  749. int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
  750. {
  751. if (sha512 == NULL || (data == NULL && len > 0)) {
  752. return BAD_FUNC_ARG;
  753. }
  754. #ifdef WOLF_CRYPTO_CB
  755. #ifndef WOLF_CRYPTO_CB_FIND
  756. if (sha512->devId != INVALID_DEVID)
  757. #endif
  758. {
  759. int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL);
  760. if (ret != CRYPTOCB_UNAVAILABLE)
  761. return ret;
  762. /* fall-through when unavailable */
  763. }
  764. #endif
  765. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  766. if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
  767. #if defined(HAVE_INTEL_QA)
  768. return IntelQaSymSha512(&sha512->asyncDev, NULL, data, len);
  769. #endif
  770. }
  771. #endif /* WOLFSSL_ASYNC_CRYPT */
  772. return Sha512Update(sha512, data, len);
  773. }
  774. #endif /* WOLFSSL_SHA512 */
  775. #endif /* WOLFSSL_IMX6_CAAM || WOLFSSL_SILABS_SHA384 */
  776. #if defined(WOLFSSL_KCAPI_HASH)
  777. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  778. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  779. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  780. /* functions defined in wolfcrypt/src/port/renesas/renesas_fspsm_sha.c */
  781. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  782. #else
  783. static WC_INLINE int Sha512Final(wc_Sha512* sha512)
  784. {
  785. int ret;
  786. byte* local;
  787. if (sha512 == NULL) {
  788. return BAD_FUNC_ARG;
  789. }
  790. local = (byte*)sha512->buffer;
  791. /* we'll add a 0x80 byte at the end,
  792. ** so make sure we have appropriate buffer length. */
  793. if (sha512->buffLen > WC_SHA512_BLOCK_SIZE - 1) {
  794. return BAD_STATE_E;
  795. } /* buffLen check */
  796. local[sha512->buffLen++] = 0x80; /* add 1 */
  797. /* pad with zeros */
  798. if (sha512->buffLen > WC_SHA512_PAD_SIZE) {
  799. XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
  800. sha512->buffLen += WC_SHA512_BLOCK_SIZE - sha512->buffLen;
  801. #if defined(LITTLE_ENDIAN_ORDER)
  802. #if defined(USE_INTEL_SPEEDUP) && \
  803. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  804. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  805. #endif
  806. {
  807. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  808. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  809. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  810. ByteReverseWords64(sha512->buffer,sha512->buffer,
  811. WC_SHA512_BLOCK_SIZE);
  812. #endif
  813. }
  814. #endif /* LITTLE_ENDIAN_ORDER */
  815. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  816. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  817. if (sha512->ctx.mode == ESP32_SHA_INIT) {
  818. esp_sha_try_hw_lock(&sha512->ctx);
  819. }
  820. if (sha512->ctx.mode == ESP32_SHA_SW) {
  821. ByteReverseWords64(sha512->buffer,sha512->buffer,
  822. WC_SHA512_BLOCK_SIZE);
  823. ret = Transform_Sha512(sha512);
  824. }
  825. else {
  826. ret = esp_sha512_process(sha512);
  827. }
  828. #else
  829. ret = Transform_Sha512(sha512);
  830. #endif
  831. if (ret != 0)
  832. return ret;
  833. sha512->buffLen = 0;
  834. } /* (sha512->buffLen > WC_SHA512_PAD_SIZE) pad with zeros */
  835. XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_PAD_SIZE - sha512->buffLen);
  836. /* put lengths in bits */
  837. sha512->hiLen = (sha512->loLen >> (8 * sizeof(sha512->loLen) - 3)) +
  838. (sha512->hiLen << 3);
  839. sha512->loLen = sha512->loLen << 3;
  840. /* store lengths */
  841. #if defined(LITTLE_ENDIAN_ORDER)
  842. #if defined(USE_INTEL_SPEEDUP) && \
  843. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  844. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  845. #endif
  846. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  847. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  848. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  849. ByteReverseWords64(sha512->buffer, sha512->buffer, WC_SHA512_PAD_SIZE);
  850. #endif
  851. #endif
  852. /* ! length ordering dependent on digest endian type ! */
  853. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  854. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  855. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  856. sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
  857. sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
  858. #endif
  859. #if defined(USE_INTEL_SPEEDUP) && \
  860. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  861. if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
  862. ByteReverseWords64(&(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
  863. &(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
  864. WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE);
  865. #endif
  866. #if !defined(WOLFSSL_ESP32_CRYPT) || \
  867. defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
  868. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  869. ret = Transform_Sha512(sha512);
  870. #else
  871. if(sha512->ctx.mode == ESP32_SHA_INIT) {
  872. /* typically for tiny block: first = last */
  873. esp_sha_try_hw_lock(&sha512->ctx);
  874. }
  875. if (sha512->ctx.mode == ESP32_SHA_SW) {
  876. ByteReverseWords64(sha512->buffer,
  877. sha512->buffer,
  878. WC_SHA512_BLOCK_SIZE);
  879. sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
  880. sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
  881. ret = Transform_Sha512(sha512);
  882. }
  883. else {
  884. ret = esp_sha512_digest_process(sha512, 1);
  885. }
  886. #endif
  887. if (ret != 0)
  888. return ret;
  889. #ifdef LITTLE_ENDIAN_ORDER
  890. ByteReverseWords64(sha512->digest, sha512->digest, WC_SHA512_DIGEST_SIZE);
  891. #endif
  892. return 0;
  893. }
  894. #endif /* WOLFSSL_KCAPI_HASH */
  895. #ifdef WOLFSSL_SHA512
  896. #if defined(WOLFSSL_KCAPI_HASH)
  897. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  898. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  899. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  900. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  901. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  902. #else
  903. static int Sha512FinalRaw(wc_Sha512* sha512, byte* hash, size_t digestSz)
  904. {
  905. #ifdef LITTLE_ENDIAN_ORDER
  906. word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
  907. #endif
  908. if (sha512 == NULL || hash == NULL) {
  909. return BAD_FUNC_ARG;
  910. }
  911. #ifdef LITTLE_ENDIAN_ORDER
  912. ByteReverseWords64((word64*)digest, (word64*)sha512->digest,
  913. WC_SHA512_DIGEST_SIZE);
  914. XMEMCPY(hash, digest, digestSz);
  915. #else
  916. XMEMCPY(hash, sha512->digest, digestSz);
  917. #endif
  918. return 0;
  919. }
  920. int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
  921. {
  922. return Sha512FinalRaw(sha512, hash, WC_SHA512_DIGEST_SIZE);
  923. }
  924. static int Sha512_Family_Final(wc_Sha512* sha512, byte* hash, size_t digestSz,
  925. int (*initfp)(wc_Sha512*))
  926. {
  927. int ret;
  928. if (sha512 == NULL || hash == NULL) {
  929. return BAD_FUNC_ARG;
  930. }
  931. #ifdef WOLF_CRYPTO_CB
  932. #ifndef WOLF_CRYPTO_CB_FIND
  933. if (sha512->devId != INVALID_DEVID)
  934. #endif
  935. {
  936. byte localHash[WC_SHA512_DIGEST_SIZE];
  937. ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, localHash);
  938. if (ret != CRYPTOCB_UNAVAILABLE) {
  939. XMEMCPY(hash, localHash, digestSz);
  940. return ret;
  941. }
  942. /* fall-through when unavailable */
  943. }
  944. #endif
  945. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  946. if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
  947. #if defined(HAVE_INTEL_QA)
  948. return IntelQaSymSha512(&sha512->asyncDev, hash, NULL, digestSz);
  949. #endif
  950. }
  951. #endif /* WOLFSSL_ASYNC_CRYPT */
  952. ret = Sha512Final(sha512);
  953. if (ret != 0)
  954. return ret;
  955. XMEMCPY(hash, sha512->digest, digestSz);
  956. /* initialize Sha512 structure for the next use */
  957. return initfp(sha512);
  958. }
  959. int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
  960. {
  961. return Sha512_Family_Final(sha512, hash, WC_SHA512_DIGEST_SIZE, InitSha512);
  962. }
  963. #endif /* WOLFSSL_KCAPI_HASH */
  964. #if !defined(WOLFSSL_SE050) || !defined(WOLFSSL_SE050_HASH)
  965. int wc_InitSha512(wc_Sha512* sha512)
  966. {
  967. int devId = INVALID_DEVID;
  968. #ifdef WOLF_CRYPTO_CB
  969. devId = wc_CryptoCb_DefaultDevID();
  970. #endif
  971. return wc_InitSha512_ex(sha512, NULL, devId);
  972. }
  973. void wc_Sha512Free(wc_Sha512* sha512)
  974. {
  975. if (sha512 == NULL)
  976. return;
  977. #if defined(WOLFSSL_ESP32) && \
  978. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  979. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  980. esp_sha_release_unfinished_lock(&sha512->ctx);
  981. #endif
  982. #ifdef WOLFSSL_SMALL_STACK_CACHE
  983. if (sha512->W != NULL) {
  984. ForceZero(sha512->W, sizeof(word64) * 16);
  985. XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
  986. sha512->W = NULL;
  987. }
  988. #endif
  989. #if defined(WOLFSSL_KCAPI_HASH)
  990. KcapiHashFree(&sha512->kcapi);
  991. #endif
  992. #if defined(WOLFSSL_HASH_KEEP)
  993. if (sha512->msg != NULL) {
  994. ForceZero(sha512->msg, sha512->len);
  995. XFREE(sha512->msg, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
  996. sha512->msg = NULL;
  997. }
  998. #endif
  999. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  1000. wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
  1001. #endif /* WOLFSSL_ASYNC_CRYPT */
  1002. ForceZero(sha512, sizeof(*sha512));
  1003. }
  1004. #if (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) \
  1005. && !defined(WOLFSSL_KCAPI_HASH)
  1006. /* Apply SHA512 transformation to the data */
  1007. /* @param sha a pointer to wc_Sha512 structure */
  1008. /* @param data data to be applied SHA512 transformation */
  1009. /* @return 0 on successful, otherwise non-zero on failure */
  1010. int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
  1011. {
  1012. int ret;
  1013. /* back up buffer */
  1014. #ifdef WOLFSSL_SMALL_STACK
  1015. word64 *buffer;
  1016. #else
  1017. word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)];
  1018. #endif
  1019. /* sanity check */
  1020. if (sha == NULL || data == NULL) {
  1021. return BAD_FUNC_ARG;
  1022. }
  1023. #ifdef WOLFSSL_SMALL_STACK
  1024. buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap,
  1025. DYNAMIC_TYPE_TMP_BUFFER);
  1026. if (buffer == NULL)
  1027. return MEMORY_E;
  1028. #endif
  1029. #if defined(USE_INTEL_SPEEDUP) && \
  1030. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1031. Sha512_SetTransform();
  1032. #endif
  1033. #if defined(LITTLE_ENDIAN_ORDER)
  1034. #if defined(USE_INTEL_SPEEDUP) && \
  1035. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1036. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  1037. #endif
  1038. {
  1039. ByteReverseWords64((word64*)data, (word64*)data,
  1040. WC_SHA512_BLOCK_SIZE);
  1041. }
  1042. #endif /* LITTLE_ENDIAN_ORDER */
  1043. XMEMCPY(buffer, sha->buffer, WC_SHA512_BLOCK_SIZE);
  1044. XMEMCPY(sha->buffer, data, WC_SHA512_BLOCK_SIZE);
  1045. ret = Transform_Sha512(sha);
  1046. XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE);
  1047. #ifdef WOLFSSL_SMALL_STACK
  1048. ForceZero(buffer, WC_SHA512_BLOCK_SIZE);
  1049. XFREE(buffer, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1050. #endif
  1051. return ret;
  1052. }
  1053. #endif /* OPENSSL_EXTRA */
  1054. #endif /* WOLFSSL_SHA512 */
  1055. #endif /* !WOLFSSL_SE050 || !WOLFSSL_SE050_HASH */
  1056. /* -------------------------------------------------------------------------- */
  1057. /* SHA384 */
  1058. /* -------------------------------------------------------------------------- */
  1059. #ifdef WOLFSSL_SHA384
  1060. #if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  1061. !defined(WOLFSSL_QNX_CAAM)
  1062. /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
  1063. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1064. int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
  1065. {
  1066. if (sha384 == NULL) {
  1067. return BAD_FUNC_ARG;
  1068. }
  1069. (void)devId;
  1070. return se050_hash_init(&sha384->se050Ctx, heap);
  1071. }
  1072. int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
  1073. {
  1074. return se050_hash_update(&sha384->se050Ctx, data, len);
  1075. }
  1076. int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
  1077. {
  1078. int ret = 0;
  1079. ret = se050_hash_final(&sha384->se050Ctx, hash, WC_SHA384_DIGEST_SIZE,
  1080. kAlgorithm_SSS_SHA384);
  1081. return ret;
  1082. }
  1083. int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash)
  1084. {
  1085. int ret = 0;
  1086. ret = se050_hash_final(&sha384->se050Ctx, hash, WC_SHA384_DIGEST_SIZE,
  1087. kAlgorithm_SSS_SHA384);
  1088. return ret;
  1089. }
  1090. #elif defined(WOLFSSL_SILABS_SHA512)
  1091. /* functions defined in wolfcrypt/src/port/silabs/silabs_hash.c */
  1092. #elif defined(WOLFSSL_KCAPI_HASH)
  1093. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1094. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1095. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1096. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1097. #else
  1098. static int InitSha384(wc_Sha384* sha384)
  1099. {
  1100. if (sha384 == NULL) {
  1101. return BAD_FUNC_ARG;
  1102. }
  1103. sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
  1104. sha384->digest[1] = W64LIT(0x629a292a367cd507);
  1105. sha384->digest[2] = W64LIT(0x9159015a3070dd17);
  1106. sha384->digest[3] = W64LIT(0x152fecd8f70e5939);
  1107. sha384->digest[4] = W64LIT(0x67332667ffc00b31);
  1108. sha384->digest[5] = W64LIT(0x8eb44a8768581511);
  1109. sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7);
  1110. sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4);
  1111. sha384->buffLen = 0;
  1112. sha384->loLen = 0;
  1113. sha384->hiLen = 0;
  1114. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1115. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
  1116. /* HW needs to be carefully initialized, taking into account soft copy.
  1117. ** If already in use; copy may revert to SW as needed. */
  1118. esp_sha_init(&(sha384->ctx), WC_HASH_TYPE_SHA384);
  1119. #endif
  1120. #ifdef WOLFSSL_HASH_FLAGS
  1121. sha384->flags = 0;
  1122. #endif
  1123. #ifdef HAVE_ARIA
  1124. sha384->hSession = NULL;
  1125. #endif
  1126. #ifdef WOLFSSL_HASH_KEEP
  1127. sha384->msg = NULL;
  1128. sha384->len = 0;
  1129. sha384->used = 0;
  1130. #endif
  1131. return 0;
  1132. }
  1133. int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
  1134. {
  1135. if (sha384 == NULL || (data == NULL && len > 0)) {
  1136. return BAD_FUNC_ARG;
  1137. }
  1138. #ifdef WOLF_CRYPTO_CB
  1139. #ifndef WOLF_CRYPTO_CB_FIND
  1140. if (sha384->devId != INVALID_DEVID)
  1141. #endif
  1142. {
  1143. int ret = wc_CryptoCb_Sha384Hash(sha384, data, len, NULL);
  1144. if (ret != CRYPTOCB_UNAVAILABLE)
  1145. return ret;
  1146. /* fall-through when unavailable */
  1147. }
  1148. #endif
  1149. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1150. if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
  1151. #if defined(HAVE_INTEL_QA)
  1152. return IntelQaSymSha384(&sha384->asyncDev, NULL, data, len);
  1153. #endif
  1154. }
  1155. #endif /* WOLFSSL_ASYNC_CRYPT */
  1156. return Sha512Update((wc_Sha512*)sha384, data, len);
  1157. }
  1158. int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash)
  1159. {
  1160. #ifdef LITTLE_ENDIAN_ORDER
  1161. word64 digest[WC_SHA384_DIGEST_SIZE / sizeof(word64)];
  1162. #endif
  1163. if (sha384 == NULL || hash == NULL) {
  1164. return BAD_FUNC_ARG;
  1165. }
  1166. #ifdef LITTLE_ENDIAN_ORDER
  1167. ByteReverseWords64((word64*)digest, (word64*)sha384->digest,
  1168. WC_SHA384_DIGEST_SIZE);
  1169. XMEMCPY(hash, digest, WC_SHA384_DIGEST_SIZE);
  1170. #else
  1171. XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
  1172. #endif
  1173. return 0;
  1174. }
  1175. int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
  1176. {
  1177. int ret;
  1178. if (sha384 == NULL || hash == NULL) {
  1179. return BAD_FUNC_ARG;
  1180. }
  1181. #ifdef WOLF_CRYPTO_CB
  1182. #ifndef WOLF_CRYPTO_CB_FIND
  1183. if (sha384->devId != INVALID_DEVID)
  1184. #endif
  1185. {
  1186. ret = wc_CryptoCb_Sha384Hash(sha384, NULL, 0, hash);
  1187. if (ret != CRYPTOCB_UNAVAILABLE)
  1188. return ret;
  1189. /* fall-through when unavailable */
  1190. }
  1191. #endif
  1192. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1193. if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
  1194. #if defined(HAVE_INTEL_QA)
  1195. return IntelQaSymSha384(&sha384->asyncDev, hash, NULL,
  1196. WC_SHA384_DIGEST_SIZE);
  1197. #endif
  1198. }
  1199. #endif /* WOLFSSL_ASYNC_CRYPT */
  1200. ret = Sha512Final((wc_Sha512*)sha384);
  1201. if (ret != 0)
  1202. return ret;
  1203. XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
  1204. return InitSha384(sha384); /* reset state */
  1205. }
  1206. int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
  1207. {
  1208. int ret;
  1209. if (sha384 == NULL) {
  1210. return BAD_FUNC_ARG;
  1211. }
  1212. sha384->heap = heap;
  1213. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1214. sha384->W = NULL;
  1215. #endif
  1216. #ifdef WOLF_CRYPTO_CB
  1217. sha384->devId = devId;
  1218. sha384->devCtx = NULL;
  1219. #endif
  1220. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1221. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
  1222. if (sha384->ctx.mode != ESP32_SHA_INIT) {
  1223. ESP_LOGV(TAG, "Set ctx mode from prior value: "
  1224. "%d", sha384->ctx.mode);
  1225. }
  1226. /* We know this is a fresh, uninitialized item, so set to INIT */
  1227. sha384->ctx.mode = ESP32_SHA_INIT;
  1228. #endif
  1229. ret = InitSha384(sha384);
  1230. if (ret != 0) {
  1231. return ret;
  1232. }
  1233. #if defined(USE_INTEL_SPEEDUP) && \
  1234. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1235. Sha512_SetTransform();
  1236. #endif
  1237. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1238. ret = wolfAsync_DevCtxInit(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384,
  1239. sha384->heap, devId);
  1240. #else
  1241. (void)devId;
  1242. #endif /* WOLFSSL_ASYNC_CRYPT */
  1243. #ifdef WOLFSSL_IMXRT1170_CAAM
  1244. ret = wc_CAAM_HashInit(&sha384->hndl, &sha384->ctx, WC_HASH_TYPE_SHA384);
  1245. #endif
  1246. return ret;
  1247. }
  1248. #endif /* WOLFSSL_IMX6_CAAM || WOLFSSL_SILABS_SHA512 || WOLFSSL_KCAPI_HASH */
  1249. int wc_InitSha384(wc_Sha384* sha384)
  1250. {
  1251. int devId = INVALID_DEVID;
  1252. #ifdef WOLF_CRYPTO_CB
  1253. devId = wc_CryptoCb_DefaultDevID();
  1254. #endif
  1255. return wc_InitSha384_ex(sha384, NULL, devId);
  1256. }
  1257. void wc_Sha384Free(wc_Sha384* sha384)
  1258. {
  1259. if (sha384 == NULL)
  1260. return;
  1261. #if defined(WOLFSSL_ESP32) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1262. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
  1263. esp_sha_release_unfinished_lock(&sha384->ctx);
  1264. #endif
  1265. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1266. if (sha384->W != NULL) {
  1267. ForceZero(sha384->W, sizeof(word64) * 16);
  1268. XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1269. sha384->W = NULL;
  1270. }
  1271. #endif
  1272. #if defined(WOLFSSL_KCAPI_HASH)
  1273. KcapiHashFree(&sha384->kcapi);
  1274. #endif
  1275. #if defined(WOLFSSL_HASH_KEEP)
  1276. if (sha384->msg != NULL) {
  1277. ForceZero(sha384->msg, sha384->len);
  1278. XFREE(sha384->msg, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1279. sha384->msg = NULL;
  1280. }
  1281. #endif
  1282. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1283. se050_hash_free(&sha384->se050Ctx);
  1284. #endif
  1285. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1286. wolfAsync_DevCtxFree(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384);
  1287. #endif /* WOLFSSL_ASYNC_CRYPT */
  1288. #ifdef HAVE_ARIA
  1289. if (sha384->hSession != NULL) {
  1290. MC_CloseSession(sha384->hSession);
  1291. sha384->hSession = NULL;
  1292. }
  1293. #endif
  1294. ForceZero(sha384, sizeof(*sha384));
  1295. }
  1296. #endif /* WOLFSSL_SHA384 */
  1297. #ifdef WOLFSSL_SHA512
  1298. #if defined(WOLFSSL_KCAPI_HASH)
  1299. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1300. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1301. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1302. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1303. #else
  1304. static int Sha512_Family_GetHash(wc_Sha512* sha512, byte* hash,
  1305. int (*finalfp)(wc_Sha512*, byte*))
  1306. {
  1307. int ret;
  1308. #ifdef WOLFSSL_SMALL_STACK
  1309. wc_Sha512* tmpSha512;
  1310. #else
  1311. wc_Sha512 tmpSha512[1];
  1312. #endif
  1313. if (sha512 == NULL || hash == NULL) {
  1314. return BAD_FUNC_ARG;
  1315. }
  1316. #ifdef WOLFSSL_SMALL_STACK
  1317. tmpSha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
  1318. DYNAMIC_TYPE_TMP_BUFFER);
  1319. if (tmpSha512 == NULL) {
  1320. return MEMORY_E;
  1321. }
  1322. #endif
  1323. /* copy this sha512 into tmpSha */
  1324. ret = wc_Sha512Copy(sha512, tmpSha512);
  1325. if (ret == 0) {
  1326. ret = finalfp(tmpSha512, hash);
  1327. wc_Sha512Free(tmpSha512);
  1328. }
  1329. #ifdef WOLFSSL_SMALL_STACK
  1330. XFREE(tmpSha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1331. #endif
  1332. return ret;
  1333. }
  1334. int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash)
  1335. {
  1336. return Sha512_Family_GetHash(sha512, hash, wc_Sha512Final);
  1337. }
  1338. int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
  1339. {
  1340. int ret = 0;
  1341. if (src == NULL || dst == NULL) {
  1342. return BAD_FUNC_ARG;
  1343. }
  1344. XMEMCPY(dst, src, sizeof(wc_Sha512));
  1345. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1346. dst->W = NULL;
  1347. #endif
  1348. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
  1349. defined(WOLFSSL_SILABS_SHA512)
  1350. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  1351. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  1352. #endif
  1353. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  1354. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  1355. #endif
  1356. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1357. #if defined(CONFIG_IDF_TARGET_ESP32)
  1358. if (ret == 0) {
  1359. ret = esp_sha512_ctx_copy(src, dst);
  1360. }
  1361. #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1362. defined(CONFIG_IDF_TARGET_ESP32C6)
  1363. ESP_LOGV(TAG, "No SHA-512 HW on the ESP32-C3");
  1364. #elif defined(CONFIG_IDF_TARGET_ESP32S2) || \
  1365. defined(CONFIG_IDF_TARGET_ESP32S3)
  1366. if (ret == 0) {
  1367. ret = esp_sha512_ctx_copy(src, dst);
  1368. }
  1369. #else
  1370. ESP_LOGW(TAG, "No SHA384 HW or not yet implemented for %s",
  1371. CONFIG_IDF_TARGET);
  1372. #endif
  1373. #endif
  1374. #ifdef WOLFSSL_HASH_FLAGS
  1375. dst->flags |= WC_HASH_FLAG_ISCOPY;
  1376. #endif
  1377. #if defined(WOLFSSL_HASH_KEEP)
  1378. if (src->msg != NULL) {
  1379. dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1380. if (dst->msg == NULL)
  1381. return MEMORY_E;
  1382. XMEMCPY(dst->msg, src->msg, src->len);
  1383. }
  1384. #endif
  1385. return ret;
  1386. }
  1387. #endif /* WOLFSSL_KCAPI_HASH */
  1388. #ifdef WOLFSSL_HASH_FLAGS
  1389. int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags)
  1390. {
  1391. if (sha512) {
  1392. sha512->flags = flags;
  1393. }
  1394. return 0;
  1395. }
  1396. int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags)
  1397. {
  1398. if (sha512 && flags) {
  1399. *flags = sha512->flags;
  1400. }
  1401. return 0;
  1402. }
  1403. #endif /* WOLFSSL_HASH_FLAGS */
  1404. #if !defined(WOLFSSL_NOSHA512_224) && \
  1405. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1406. int wc_InitSha512_224(wc_Sha512* sha)
  1407. {
  1408. return wc_InitSha512_224_ex(sha, NULL, INVALID_DEVID);
  1409. }
  1410. int wc_Sha512_224Update(wc_Sha512* sha, const byte* data, word32 len)
  1411. {
  1412. return wc_Sha512Update(sha, data, len);
  1413. }
  1414. #if defined(WOLFSSL_KCAPI_HASH)
  1415. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1416. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1417. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1418. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1419. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1420. #else
  1421. int wc_Sha512_224FinalRaw(wc_Sha512* sha, byte* hash)
  1422. {
  1423. return Sha512FinalRaw(sha, hash, WC_SHA512_224_DIGEST_SIZE);
  1424. }
  1425. int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash)
  1426. {
  1427. return Sha512_Family_Final(sha512, hash, WC_SHA512_224_DIGEST_SIZE,
  1428. InitSha512_224);
  1429. }
  1430. #endif /* else none of the above: WOLFSSL_KCAPI_HASH, WOLFSSL_SE050 */
  1431. void wc_Sha512_224Free(wc_Sha512* sha)
  1432. {
  1433. wc_Sha512Free(sha);
  1434. }
  1435. #if defined(WOLFSSL_KCAPI_HASH)
  1436. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1437. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1438. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1439. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1440. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1441. #else
  1442. int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash)
  1443. {
  1444. return Sha512_Family_GetHash(sha512, hash, wc_Sha512_224Final);
  1445. }
  1446. int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst)
  1447. {
  1448. return wc_Sha512Copy(src, dst);
  1449. }
  1450. #endif /* else none of the above: WOLFSSL_KCAPI_HASH, WOLFSSL_SE050 */
  1451. #ifdef WOLFSSL_HASH_FLAGS
  1452. int wc_Sha512_224SetFlags(wc_Sha512* sha, word32 flags)
  1453. {
  1454. return wc_Sha512SetFlags(sha, flags);
  1455. }
  1456. int wc_Sha512_224GetFlags(wc_Sha512* sha, word32* flags)
  1457. {
  1458. return wc_Sha512GetFlags(sha, flags);
  1459. }
  1460. #endif /* WOLFSSL_HASH_FLAGS */
  1461. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  1462. int wc_Sha512_224Transform(wc_Sha512* sha, const unsigned char* data)
  1463. {
  1464. return wc_Sha512Transform(sha, data);
  1465. }
  1466. #endif /* OPENSSL_EXTRA */
  1467. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  1468. #if !defined(WOLFSSL_NOSHA512_256) && \
  1469. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1470. int wc_InitSha512_256(wc_Sha512* sha)
  1471. {
  1472. return wc_InitSha512_256_ex(sha, NULL, INVALID_DEVID);
  1473. }
  1474. int wc_Sha512_256Update(wc_Sha512* sha, const byte* data, word32 len)
  1475. {
  1476. return wc_Sha512Update(sha, data, len);
  1477. }
  1478. #if defined(WOLFSSL_KCAPI_HASH)
  1479. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1480. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1481. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1482. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1483. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1484. #else
  1485. int wc_Sha512_256FinalRaw(wc_Sha512* sha, byte* hash)
  1486. {
  1487. return Sha512FinalRaw(sha, hash, WC_SHA512_256_DIGEST_SIZE);
  1488. }
  1489. int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash)
  1490. {
  1491. return Sha512_Family_Final(sha512, hash, WC_SHA512_256_DIGEST_SIZE,
  1492. InitSha512_256);
  1493. }
  1494. #endif
  1495. void wc_Sha512_256Free(wc_Sha512* sha)
  1496. {
  1497. wc_Sha512Free(sha);
  1498. }
  1499. #if defined(WOLFSSL_KCAPI_HASH)
  1500. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1501. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1502. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1503. /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1504. #else
  1505. int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash)
  1506. {
  1507. return Sha512_Family_GetHash(sha512, hash, wc_Sha512_256Final);
  1508. }
  1509. int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst)
  1510. {
  1511. return wc_Sha512Copy(src, dst);
  1512. }
  1513. #endif
  1514. #ifdef WOLFSSL_HASH_FLAGS
  1515. int wc_Sha512_256SetFlags(wc_Sha512* sha, word32 flags)
  1516. {
  1517. return wc_Sha512SetFlags(sha, flags);
  1518. }
  1519. int wc_Sha512_256GetFlags(wc_Sha512* sha, word32* flags)
  1520. {
  1521. return wc_Sha512GetFlags(sha, flags);
  1522. }
  1523. #endif /* WOLFSSL_HASH_FLAGS */
  1524. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  1525. int wc_Sha512_256Transform(wc_Sha512* sha, const unsigned char* data)
  1526. {
  1527. return wc_Sha512Transform(sha, data);
  1528. }
  1529. #endif /* OPENSSL_EXTRA */
  1530. #endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */
  1531. #endif /* WOLFSSL_SHA512 */
  1532. #ifdef WOLFSSL_SHA384
  1533. #if defined(WOLFSSL_KCAPI_HASH)
  1534. /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1535. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1536. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1537. /* functions defined in wolfcrypt/src/port/renesas/renesas_fspsm_sha.c */
  1538. #else
  1539. int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
  1540. {
  1541. int ret;
  1542. #ifdef WOLFSSL_SMALL_STACK
  1543. wc_Sha384* tmpSha384;
  1544. #else
  1545. wc_Sha384 tmpSha384[1];
  1546. #endif
  1547. if (sha384 == NULL || hash == NULL) {
  1548. return BAD_FUNC_ARG;
  1549. }
  1550. #ifdef WOLFSSL_SMALL_STACK
  1551. tmpSha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL,
  1552. DYNAMIC_TYPE_TMP_BUFFER);
  1553. if (tmpSha384 == NULL) {
  1554. return MEMORY_E;
  1555. }
  1556. #endif
  1557. /* copy this sha384 into tmpSha */
  1558. ret = wc_Sha384Copy(sha384, tmpSha384);
  1559. if (ret == 0) {
  1560. ret = wc_Sha384Final(tmpSha384, hash);
  1561. wc_Sha384Free(tmpSha384);
  1562. }
  1563. #ifdef WOLFSSL_SMALL_STACK
  1564. XFREE(tmpSha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1565. #endif
  1566. return ret;
  1567. }
  1568. int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
  1569. {
  1570. int ret = 0;
  1571. if (src == NULL || dst == NULL) {
  1572. return BAD_FUNC_ARG;
  1573. }
  1574. XMEMCPY(dst, src, sizeof(wc_Sha384));
  1575. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1576. dst->W = NULL;
  1577. #endif
  1578. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
  1579. defined(WOLFSSL_SILABS_SHA384)
  1580. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  1581. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  1582. #endif
  1583. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1584. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  1585. #endif
  1586. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1587. #if defined(CONFIG_IDF_TARGET_ESP32)
  1588. esp_sha384_ctx_copy(src, dst);
  1589. #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1590. defined(CONFIG_IDF_TARGET_ESP32C6)
  1591. ESP_LOGV(TAG, "No SHA-384 HW on the ESP32-C3");
  1592. #elif defined(CONFIG_IDF_TARGET_ESP32S2) || \
  1593. defined(CONFIG_IDF_TARGET_ESP32S3)
  1594. esp_sha384_ctx_copy(src, dst);
  1595. #else
  1596. ESP_LOGW(TAG, "No SHA384 HW or not yet implemented for %s",
  1597. CONFIG_IDF_TARGET);
  1598. #endif
  1599. #endif
  1600. #ifdef HAVE_ARIA
  1601. dst->hSession = NULL;
  1602. if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
  1603. return MEMORY_E;
  1604. }
  1605. #endif
  1606. #ifdef WOLFSSL_HASH_FLAGS
  1607. dst->flags |= WC_HASH_FLAG_ISCOPY;
  1608. #endif
  1609. #if defined(WOLFSSL_HASH_KEEP)
  1610. if (src->msg != NULL) {
  1611. dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1612. if (dst->msg == NULL)
  1613. return MEMORY_E;
  1614. XMEMCPY(dst->msg, src->msg, src->len);
  1615. }
  1616. #endif
  1617. return ret;
  1618. }
  1619. #endif /* WOLFSSL_KCAPI_HASH */
  1620. #ifdef WOLFSSL_HASH_FLAGS
  1621. int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags)
  1622. {
  1623. if (sha384) {
  1624. sha384->flags = flags;
  1625. }
  1626. return 0;
  1627. }
  1628. int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags)
  1629. {
  1630. if (sha384 && flags) {
  1631. *flags = sha384->flags;
  1632. }
  1633. return 0;
  1634. }
  1635. #endif
  1636. #endif /* WOLFSSL_SHA384 */
  1637. #ifdef WOLFSSL_HASH_KEEP
  1638. /* Some hardware have issues with update, this function stores the data to be
  1639. * hashed into an array. Once ready, the Final operation is called on all of the
  1640. * data to be hashed at once.
  1641. * returns 0 on success
  1642. */
  1643. int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz)
  1644. {
  1645. return _wc_Hash_Grow(&(sha512->msg), &(sha512->used), &(sha512->len), in,
  1646. inSz, sha512->heap);
  1647. }
  1648. #ifdef WOLFSSL_SHA384
  1649. int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz)
  1650. {
  1651. return _wc_Hash_Grow(&(sha384->msg), &(sha384->used), &(sha384->len), in,
  1652. inSz, sha384->heap);
  1653. }
  1654. #endif /* WOLFSSL_SHA384 */
  1655. #endif /* WOLFSSL_HASH_KEEP */
  1656. #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */