sha512.c 54 KB

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