sha512.c 53 KB

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