sha512.c 55 KB

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