sha256.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. /* sha256.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. /* For more info on the algorithm, see https://tools.ietf.org/html/rfc6234
  22. *
  23. * For more information on NIST FIPS PUB 180-4, see
  24. * https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
  25. */
  26. /*
  27. DESCRIPTION
  28. This library provides the interface to SHA-256 secure hash algorithms.
  29. SHA-256 performs processing on message blocks to produce a final hash digest
  30. output. It can be used to hash a message, M, having a length of L bits,
  31. where 0 <= L < 2^64.
  32. Note that in some cases, hardware acceleration may be enabled, depending
  33. on the specific device platform.
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. #include <config.h>
  37. #endif
  38. #include <wolfssl/wolfcrypt/settings.h>
  39. #include <wolfssl/wolfcrypt/types.h>
  40. /*
  41. * SHA256 Build Options:
  42. * USE_SLOW_SHA256: Reduces code size by not partially unrolling
  43. (~2KB smaller and ~25% slower) (default OFF)
  44. * WOLFSSL_SHA256_BY_SPEC: Uses the Ch/Maj based on SHA256 specification
  45. (default ON)
  46. * WOLFSSL_SHA256_ALT_CH_MAJ: Alternate Ch/Maj that is easier for compilers to
  47. optimize and recognize as SHA256 (default OFF)
  48. * SHA256_MANY_REGISTERS: A SHA256 version that keeps all data in registers
  49. and partial unrolled (default OFF)
  50. */
  51. /* Default SHA256 to use Ch/Maj based on specification */
  52. #if !defined(WOLFSSL_SHA256_BY_SPEC) && !defined(WOLFSSL_SHA256_ALT_CH_MAJ)
  53. #define WOLFSSL_SHA256_BY_SPEC
  54. #endif
  55. #if !defined(NO_SHA256) && (!defined(WOLFSSL_ARMASM) && \
  56. !defined(WOLFSSL_ARMASM_NO_NEON))
  57. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  58. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  59. #define FIPS_NO_WRAPPERS
  60. #ifdef USE_WINDOWS_API
  61. #pragma code_seg(".fipsA$d")
  62. #pragma const_seg(".fipsB$d")
  63. #endif
  64. #endif
  65. #include <wolfssl/wolfcrypt/sha256.h>
  66. #include <wolfssl/wolfcrypt/error-crypt.h>
  67. #include <wolfssl/wolfcrypt/cpuid.h>
  68. #include <wolfssl/wolfcrypt/hash.h>
  69. #ifdef WOLF_CRYPTO_CB
  70. #include <wolfssl/wolfcrypt/cryptocb.h>
  71. #endif
  72. #ifdef WOLFSSL_IMXRT1170_CAAM
  73. #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
  74. #endif
  75. /* determine if we are using Espressif SHA hardware acceleration */
  76. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  77. #if defined(WOLFSSL_ESP32_CRYPT) && \
  78. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
  79. /* define a single keyword for simplicity & readability
  80. *
  81. * by default the HW acceleration is on for ESP32-WROOM32
  82. * but individual components can be turned off.
  83. */
  84. #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  85. #else
  86. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  87. #endif
  88. #ifdef WOLFSSL_ESPIDF
  89. /* Define the ESP_LOGx(TAG, "" value for output messages here.
  90. **
  91. ** Beware of possible conflict in test.c (that one now named TEST_TAG)
  92. */
  93. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  94. static const char* TAG = "wc_sha256";
  95. #endif
  96. #endif
  97. #if defined(WOLFSSL_TI_HASH)
  98. /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
  99. #elif defined(WOLFSSL_CRYPTOCELL)
  100. /* wc_port.c includes wolfcrypt/src/port/arm/cryptoCellHash.c */
  101. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  102. #else
  103. #include <wolfssl/wolfcrypt/logging.h>
  104. #ifdef NO_INLINE
  105. #include <wolfssl/wolfcrypt/misc.h>
  106. #else
  107. #define WOLFSSL_MISC_INCLUDED
  108. #include <wolfcrypt/src/misc.c>
  109. #endif
  110. #ifdef WOLFSSL_DEVCRYPTO_HASH
  111. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  112. #endif
  113. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  114. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  115. #endif
  116. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  117. #if defined(__GNUC__) && ((__GNUC__ < 4) || \
  118. (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
  119. #undef NO_AVX2_SUPPORT
  120. #define NO_AVX2_SUPPORT
  121. #endif
  122. #if defined(__clang__) && ((__clang_major__ < 3) || \
  123. (__clang_major__ == 3 && __clang_minor__ <= 5))
  124. #define NO_AVX2_SUPPORT
  125. #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
  126. #undef NO_AVX2_SUPPORT
  127. #endif
  128. #define HAVE_INTEL_AVX1
  129. #ifndef NO_AVX2_SUPPORT
  130. #define HAVE_INTEL_AVX2
  131. #endif
  132. #else
  133. #undef HAVE_INTEL_AVX1
  134. #undef HAVE_INTEL_AVX2
  135. #endif /* WOLFSSL_X86_64_BUILD && USE_INTEL_SPEEDUP */
  136. #if defined(HAVE_INTEL_AVX2)
  137. #define HAVE_INTEL_RORX
  138. #endif
  139. #if !defined(WOLFSSL_PIC32MZ_HASH) && !defined(STM32_HASH_SHA2) && \
  140. (!defined(WOLFSSL_IMX6_CAAM) || defined(NO_IMX6_CAAM_HASH) || \
  141. defined(WOLFSSL_QNX_CAAM)) && \
  142. !defined(WOLFSSL_AFALG_HASH) && !defined(WOLFSSL_DEVCRYPTO_HASH) && \
  143. (!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_HASH)) && \
  144. ((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  145. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \
  146. defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \
  147. !defined(WOLFSSL_PSOC6_CRYPTO) && !defined(WOLFSSL_IMXRT_DCP) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  148. !defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_SE050_HASH) && \
  149. ((!defined(WOLFSSL_RENESAS_SCEPROTECT) && \
  150. !defined(WOLFSSL_RENESAS_RSIP)) \
  151. || defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) && \
  152. (!defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)) && \
  153. !defined(WOLFSSL_RENESAS_RX64_HASH)
  154. static int InitSha256(wc_Sha256* sha256)
  155. {
  156. int ret = 0;
  157. if (sha256 == NULL)
  158. return BAD_FUNC_ARG;
  159. XMEMSET(sha256->digest, 0, sizeof(sha256->digest));
  160. sha256->digest[0] = 0x6A09E667L;
  161. sha256->digest[1] = 0xBB67AE85L;
  162. sha256->digest[2] = 0x3C6EF372L;
  163. sha256->digest[3] = 0xA54FF53AL;
  164. sha256->digest[4] = 0x510E527FL;
  165. sha256->digest[5] = 0x9B05688CL;
  166. sha256->digest[6] = 0x1F83D9ABL;
  167. sha256->digest[7] = 0x5BE0CD19L;
  168. sha256->buffLen = 0;
  169. sha256->loLen = 0;
  170. sha256->hiLen = 0;
  171. #ifdef WOLFSSL_HASH_FLAGS
  172. sha256->flags = 0;
  173. #endif
  174. #ifdef WOLFSSL_HASH_KEEP
  175. sha256->msg = NULL;
  176. sha256->len = 0;
  177. sha256->used = 0;
  178. #endif
  179. #ifdef WOLF_CRYPTO_CB
  180. sha256->devId = wc_CryptoCb_DefaultDevID();
  181. #endif
  182. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  183. XMEMSET(&sha256->maxq_ctx, 0, sizeof(sha256->maxq_ctx));
  184. #endif
  185. #ifdef HAVE_ARIA
  186. sha256->hSession = NULL;
  187. #endif
  188. return ret;
  189. }
  190. #endif
  191. /* Hardware Acceleration */
  192. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  193. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  194. /* in case intel instructions aren't available, plus we need the K[] global */
  195. #define NEED_SOFT_SHA256
  196. /*****
  197. Intel AVX1/AVX2 Macro Control Structure
  198. #define HAVE_INTEL_AVX1
  199. #define HAVE_INTEL_AVX2
  200. #define HAVE_INTEL_RORX
  201. int InitSha256(wc_Sha256* sha256) {
  202. Save/Recover XMM, YMM
  203. ...
  204. }
  205. #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
  206. Transform_Sha256(); Function prototype
  207. #else
  208. Transform_Sha256() { }
  209. int Sha256Final() {
  210. Save/Recover XMM, YMM
  211. ...
  212. }
  213. #endif
  214. #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
  215. #if defined(HAVE_INTEL_RORX
  216. #define RND with rorx instruction
  217. #else
  218. #define RND
  219. #endif
  220. #endif
  221. #if defined(HAVE_INTEL_AVX1)
  222. #define XMM Instructions/inline asm
  223. int Transform_Sha256() {
  224. Stitched Message Sched/Round
  225. }
  226. #elif defined(HAVE_INTEL_AVX2)
  227. #define YMM Instructions/inline asm
  228. int Transform_Sha256() {
  229. More granular Stitched Message Sched/Round
  230. }
  231. #endif
  232. */
  233. /* Each platform needs to query info type 1 from cpuid to see if aesni is
  234. * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
  235. */
  236. /* #if defined(HAVE_INTEL_AVX1/2) at the tail of sha256 */
  237. static int Transform_Sha256(wc_Sha256* sha256, const byte* data);
  238. #ifdef __cplusplus
  239. extern "C" {
  240. #endif
  241. #if defined(HAVE_INTEL_AVX1)
  242. extern int Transform_Sha256_AVX1(wc_Sha256 *sha256, const byte* data);
  243. extern int Transform_Sha256_AVX1_Len(wc_Sha256* sha256,
  244. const byte* data, word32 len);
  245. #endif
  246. #if defined(HAVE_INTEL_AVX2)
  247. extern int Transform_Sha256_AVX2(wc_Sha256 *sha256, const byte* data);
  248. extern int Transform_Sha256_AVX2_Len(wc_Sha256* sha256,
  249. const byte* data, word32 len);
  250. #ifdef HAVE_INTEL_RORX
  251. extern int Transform_Sha256_AVX1_RORX(wc_Sha256 *sha256, const byte* data);
  252. extern int Transform_Sha256_AVX1_RORX_Len(wc_Sha256* sha256,
  253. const byte* data, word32 len);
  254. extern int Transform_Sha256_AVX2_RORX(wc_Sha256 *sha256, const byte* data);
  255. extern int Transform_Sha256_AVX2_RORX_Len(wc_Sha256* sha256,
  256. const byte* data, word32 len);
  257. #endif /* HAVE_INTEL_RORX */
  258. #endif /* HAVE_INTEL_AVX2 */
  259. #ifdef __cplusplus
  260. } /* extern "C" */
  261. #endif
  262. static int (*Transform_Sha256_p)(wc_Sha256* sha256, const byte* data);
  263. /* = _Transform_Sha256 */
  264. static int (*Transform_Sha256_Len_p)(wc_Sha256* sha256, const byte* data,
  265. word32 len);
  266. /* = NULL */
  267. static int transform_check = 0;
  268. static word32 intel_flags;
  269. static int Transform_Sha256_is_vectorized = 0;
  270. static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
  271. int ret;
  272. ret = (*Transform_Sha256_p)(S, D);
  273. return ret;
  274. }
  275. #define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
  276. static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
  277. int ret;
  278. ret = (*Transform_Sha256_Len_p)(S, D, L);
  279. return ret;
  280. }
  281. #define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
  282. static void Sha256_SetTransform(void)
  283. {
  284. if (transform_check)
  285. return;
  286. intel_flags = cpuid_get_flags();
  287. #ifdef HAVE_INTEL_AVX2
  288. if (1 && IS_INTEL_AVX2(intel_flags)) {
  289. #ifdef HAVE_INTEL_RORX
  290. if (IS_INTEL_BMI2(intel_flags)) {
  291. Transform_Sha256_p = Transform_Sha256_AVX2_RORX;
  292. Transform_Sha256_Len_p = Transform_Sha256_AVX2_RORX_Len;
  293. Transform_Sha256_is_vectorized = 1;
  294. }
  295. else
  296. #endif
  297. if (1)
  298. {
  299. Transform_Sha256_p = Transform_Sha256_AVX2;
  300. Transform_Sha256_Len_p = Transform_Sha256_AVX2_Len;
  301. Transform_Sha256_is_vectorized = 1;
  302. }
  303. #ifdef HAVE_INTEL_RORX
  304. else {
  305. Transform_Sha256_p = Transform_Sha256_AVX1_RORX;
  306. Transform_Sha256_Len_p = Transform_Sha256_AVX1_RORX_Len;
  307. Transform_Sha256_is_vectorized = 1;
  308. }
  309. #endif
  310. }
  311. else
  312. #endif
  313. #ifdef HAVE_INTEL_AVX1
  314. if (IS_INTEL_AVX1(intel_flags)) {
  315. Transform_Sha256_p = Transform_Sha256_AVX1;
  316. Transform_Sha256_Len_p = Transform_Sha256_AVX1_Len;
  317. Transform_Sha256_is_vectorized = 1;
  318. }
  319. else
  320. #endif
  321. {
  322. Transform_Sha256_p = Transform_Sha256;
  323. Transform_Sha256_Len_p = NULL;
  324. Transform_Sha256_is_vectorized = 0;
  325. }
  326. transform_check = 1;
  327. }
  328. #if !defined(WOLFSSL_KCAPI_HASH)
  329. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  330. {
  331. int ret = 0;
  332. if (sha256 == NULL)
  333. return BAD_FUNC_ARG;
  334. sha256->heap = heap;
  335. #ifdef WOLF_CRYPTO_CB
  336. sha256->devId = devId;
  337. sha256->devCtx = NULL;
  338. #endif
  339. #ifdef WOLFSSL_SMALL_STACK_CACHE
  340. sha256->W = NULL;
  341. #endif
  342. ret = InitSha256(sha256);
  343. if (ret != 0)
  344. return ret;
  345. /* choose best Transform function under this runtime environment */
  346. Sha256_SetTransform();
  347. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  348. ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
  349. WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
  350. #else
  351. (void)devId;
  352. #endif /* WOLFSSL_ASYNC_CRYPT */
  353. return ret;
  354. }
  355. #endif /* !WOLFSSL_KCAPI_HASH */
  356. #elif defined(FREESCALE_LTC_SHA)
  357. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  358. {
  359. (void)heap;
  360. (void)devId;
  361. LTC_HASH_Init(LTC_BASE, &sha256->ctx, kLTC_Sha256, NULL, 0);
  362. return 0;
  363. }
  364. #elif defined(FREESCALE_MMCAU_SHA)
  365. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  366. #include "cau_api.h"
  367. #else
  368. #include "fsl_mmcau.h"
  369. #endif
  370. #define XTRANSFORM(S, D) Transform_Sha256((S),(D))
  371. #define XTRANSFORM_LEN(S, D, L) Transform_Sha256_Len((S),(D),(L))
  372. #ifndef WC_HASH_DATA_ALIGNMENT
  373. /* these hardware API's require 4 byte (word32) alignment */
  374. #define WC_HASH_DATA_ALIGNMENT 4
  375. #endif
  376. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  377. {
  378. int ret = 0;
  379. (void)heap;
  380. (void)devId;
  381. ret = wolfSSL_CryptHwMutexLock();
  382. if (ret != 0) {
  383. return ret;
  384. }
  385. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  386. cau_sha256_initialize_output(sha256->digest);
  387. #else
  388. MMCAU_SHA256_InitializeOutput((uint32_t*)sha256->digest);
  389. #endif
  390. wolfSSL_CryptHwMutexUnLock();
  391. sha256->buffLen = 0;
  392. sha256->loLen = 0;
  393. sha256->hiLen = 0;
  394. #ifdef WOLFSSL_SMALL_STACK_CACHE
  395. sha256->W = NULL;
  396. #endif
  397. return ret;
  398. }
  399. static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
  400. {
  401. int ret = wolfSSL_CryptHwMutexLock();
  402. if (ret == 0) {
  403. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  404. cau_sha256_hash_n((byte*)data, 1, sha256->digest);
  405. #else
  406. MMCAU_SHA256_HashN((byte*)data, 1, (uint32_t*)sha256->digest);
  407. #endif
  408. wolfSSL_CryptHwMutexUnLock();
  409. }
  410. return ret;
  411. }
  412. static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
  413. word32 len)
  414. {
  415. int ret = wolfSSL_CryptHwMutexLock();
  416. if (ret == 0) {
  417. #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
  418. if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
  419. /* data pointer is NOT aligned,
  420. * so copy and perform one block at a time */
  421. byte* local = (byte*)sha256->buffer;
  422. while (len >= WC_SHA256_BLOCK_SIZE) {
  423. XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
  424. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  425. cau_sha256_hash_n(local, 1, sha256->digest);
  426. #else
  427. MMCAU_SHA256_HashN(local, 1, (uint32_t*)sha256->digest);
  428. #endif
  429. data += WC_SHA256_BLOCK_SIZE;
  430. len -= WC_SHA256_BLOCK_SIZE;
  431. }
  432. }
  433. else
  434. #endif
  435. {
  436. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  437. cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
  438. sha256->digest);
  439. #else
  440. MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
  441. (uint32_t*)sha256->digest);
  442. #endif
  443. }
  444. wolfSSL_CryptHwMutexUnLock();
  445. }
  446. return ret;
  447. }
  448. #elif defined(WOLFSSL_PIC32MZ_HASH)
  449. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  450. #elif defined(STM32_HASH_SHA2)
  451. /* Supports CubeMX HAL or Standard Peripheral Library */
  452. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  453. {
  454. if (sha256 == NULL)
  455. return BAD_FUNC_ARG;
  456. (void)devId;
  457. (void)heap;
  458. XMEMSET(sha256, 0, sizeof(wc_Sha256));
  459. wc_Stm32_Hash_Init(&sha256->stmCtx);
  460. return 0;
  461. }
  462. int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  463. {
  464. int ret = 0;
  465. if (sha256 == NULL || (data == NULL && len > 0)) {
  466. return BAD_FUNC_ARG;
  467. }
  468. ret = wolfSSL_CryptHwMutexLock();
  469. if (ret == 0) {
  470. ret = wc_Stm32_Hash_Update(&sha256->stmCtx,
  471. HASH_AlgoSelection_SHA256, data, len, WC_SHA256_BLOCK_SIZE);
  472. wolfSSL_CryptHwMutexUnLock();
  473. }
  474. return ret;
  475. }
  476. int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
  477. {
  478. int ret = 0;
  479. if (sha256 == NULL || hash == NULL) {
  480. return BAD_FUNC_ARG;
  481. }
  482. ret = wolfSSL_CryptHwMutexLock();
  483. if (ret == 0) {
  484. ret = wc_Stm32_Hash_Final(&sha256->stmCtx,
  485. HASH_AlgoSelection_SHA256, hash, WC_SHA256_DIGEST_SIZE);
  486. wolfSSL_CryptHwMutexUnLock();
  487. }
  488. (void)wc_InitSha256(sha256); /* reset state */
  489. return ret;
  490. }
  491. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  492. !defined(WOLFSSL_QNX_CAAM)
  493. /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
  494. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  495. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  496. {
  497. if (sha256 == NULL) {
  498. return BAD_FUNC_ARG;
  499. }
  500. (void)devId;
  501. return se050_hash_init(&sha256->se050Ctx, heap);
  502. }
  503. int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  504. {
  505. return se050_hash_update(&sha256->se050Ctx, data, len);
  506. }
  507. int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
  508. {
  509. int ret = 0;
  510. ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
  511. kAlgorithm_SSS_SHA256);
  512. return ret;
  513. }
  514. int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
  515. {
  516. int ret = 0;
  517. ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
  518. kAlgorithm_SSS_SHA256);
  519. return ret;
  520. }
  521. #elif defined(WOLFSSL_AFALG_HASH)
  522. /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
  523. #elif defined(WOLFSSL_DEVCRYPTO_HASH)
  524. /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
  525. #elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)
  526. #include "hal_data.h"
  527. #ifndef WOLFSSL_SCE_SHA256_HANDLE
  528. #define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0
  529. #endif
  530. #define WC_SHA256_DIGEST_WORD_SIZE 16
  531. #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D))
  532. static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data)
  533. {
  534. if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
  535. CRYPTO_WORD_ENDIAN_LITTLE)
  536. {
  537. ByteReverseWords((word32*)data, (word32*)data,
  538. WC_SHA256_BLOCK_SIZE);
  539. ByteReverseWords(sha256->digest, sha256->digest,
  540. WC_SHA256_DIGEST_SIZE);
  541. }
  542. if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(
  543. WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, (word32*)data,
  544. WC_SHA256_DIGEST_WORD_SIZE, sha256->digest) != SSP_SUCCESS){
  545. WOLFSSL_MSG("Unexpected hardware return value");
  546. return WC_HW_E;
  547. }
  548. if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
  549. CRYPTO_WORD_ENDIAN_LITTLE)
  550. {
  551. ByteReverseWords((word32*)data, (word32*)data,
  552. WC_SHA256_BLOCK_SIZE);
  553. ByteReverseWords(sha256->digest, sha256->digest,
  554. WC_SHA256_DIGEST_SIZE);
  555. }
  556. return 0;
  557. }
  558. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  559. {
  560. int ret = 0;
  561. if (sha256 == NULL)
  562. return BAD_FUNC_ARG;
  563. sha256->heap = heap;
  564. ret = InitSha256(sha256);
  565. if (ret != 0)
  566. return ret;
  567. (void)devId;
  568. return ret;
  569. }
  570. #elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  571. /* HW may fail since there's only one, so we still need SW */
  572. #define NEED_SOFT_SHA256
  573. /*
  574. ** soft SHA needs initialization digest, but HW does not.
  575. */
  576. static int InitSha256(wc_Sha256* sha256)
  577. {
  578. int ret = 0; /* zero = success */
  579. if (sha256 == NULL) {
  580. return BAD_FUNC_ARG;
  581. }
  582. sha256->digest[0] = 0x6A09E667L;
  583. sha256->digest[1] = 0xBB67AE85L;
  584. sha256->digest[2] = 0x3C6EF372L;
  585. sha256->digest[3] = 0xA54FF53AL;
  586. sha256->digest[4] = 0x510E527FL;
  587. sha256->digest[5] = 0x9B05688CL;
  588. sha256->digest[6] = 0x1F83D9ABL;
  589. sha256->digest[7] = 0x5BE0CD19L;
  590. sha256->buffLen = 0;
  591. sha256->loLen = 0;
  592. sha256->hiLen = 0;
  593. ret = esp_sha_init(&(sha256->ctx), WC_HASH_TYPE_SHA256);
  594. return ret;
  595. }
  596. /*
  597. ** wolfCrypt InitSha256 external wrapper.
  598. **
  599. ** we'll assume this is ALWAYS for a new, uninitialized sha256
  600. */
  601. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  602. {
  603. (void)devId;
  604. if (sha256 == NULL) {
  605. return BAD_FUNC_ARG;
  606. }
  607. #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  608. /* We know this is a fresh, uninitialized item, so set to INIT */
  609. if (sha256->ctx.mode != ESP32_SHA_INIT) {
  610. ESP_LOGV(TAG, "Set ctx mode from prior value: "
  611. "%d", sha256->ctx.mode);
  612. }
  613. sha256->ctx.mode = ESP32_SHA_INIT;
  614. #endif
  615. return InitSha256(sha256);
  616. }
  617. #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  618. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  619. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  620. /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
  621. #elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP)) \
  622. && !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  623. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  624. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  625. /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
  626. #elif defined(WOLFSSL_IMXRT_DCP)
  627. #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
  628. /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
  629. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  630. /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
  631. #elif defined(WOLFSSL_KCAPI_HASH)
  632. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  633. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  634. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  635. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  636. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  637. #else
  638. #define NEED_SOFT_SHA256
  639. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  640. {
  641. int ret = 0;
  642. if (sha256 == NULL)
  643. return BAD_FUNC_ARG;
  644. ret = InitSha256(sha256);
  645. if (ret != 0)
  646. return ret;
  647. sha256->heap = heap;
  648. #ifdef WOLF_CRYPTO_CB
  649. sha256->devId = devId;
  650. sha256->devCtx = NULL;
  651. #endif
  652. #ifdef WOLFSSL_SMALL_STACK_CACHE
  653. sha256->W = NULL;
  654. #endif
  655. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  656. ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
  657. WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
  658. #else
  659. (void)devId;
  660. #endif /* WOLFSSL_ASYNC_CRYPT */
  661. #ifdef WOLFSSL_IMXRT1170_CAAM
  662. ret = wc_CAAM_HashInit(&sha256->hndl, &sha256->ctx, WC_HASH_TYPE_SHA256);
  663. #endif
  664. return ret;
  665. }
  666. #endif /* End Hardware Acceleration */
  667. #ifdef NEED_SOFT_SHA256
  668. static const FLASH_QUALIFIER ALIGN32 word32 K[64] = {
  669. 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
  670. 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
  671. 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
  672. 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
  673. 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
  674. 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
  675. 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
  676. 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
  677. 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
  678. 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
  679. 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
  680. 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
  681. 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
  682. };
  683. /* Both versions of Ch and Maj are logically the same, but with the second set
  684. the compilers can recognize them better for optimization */
  685. #ifdef WOLFSSL_SHA256_BY_SPEC
  686. /* SHA256 math based on specification */
  687. #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  688. #define Maj(x,y,z) ((((x) | (y)) & (z)) | ((x) & (y)))
  689. #else
  690. /* SHA256 math reworked for easier compiler optimization */
  691. #define Ch(x,y,z) ((((y) ^ (z)) & (x)) ^ (z))
  692. #define Maj(x,y,z) ((((x) ^ (y)) & ((y) ^ (z))) ^ (y))
  693. #endif
  694. #define R(x, n) (((x) & 0xFFFFFFFFU) >> (n))
  695. #define S(x, n) rotrFixed(x, n)
  696. #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
  697. #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
  698. #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
  699. #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
  700. #define a(i) S[(0-(i)) & 7]
  701. #define b(i) S[(1-(i)) & 7]
  702. #define c(i) S[(2-(i)) & 7]
  703. #define d(i) S[(3-(i)) & 7]
  704. #define e(i) S[(4-(i)) & 7]
  705. #define f(i) S[(5-(i)) & 7]
  706. #define g(i) S[(6-(i)) & 7]
  707. #define h(i) S[(7-(i)) & 7]
  708. #ifndef XTRANSFORM
  709. #define XTRANSFORM(S, D) Transform_Sha256((S),(D))
  710. #endif
  711. #ifndef SHA256_MANY_REGISTERS
  712. #define RND(j) \
  713. t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+(j)] + W[i+(j)]; \
  714. t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
  715. d(j) += t0; \
  716. h(j) = t0 + t1
  717. static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
  718. {
  719. word32 S[8], t0, t1;
  720. int i;
  721. #ifdef WOLFSSL_SMALL_STACK_CACHE
  722. word32* W = sha256->W;
  723. if (W == NULL) {
  724. W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
  725. DYNAMIC_TYPE_DIGEST);
  726. if (W == NULL)
  727. return MEMORY_E;
  728. sha256->W = W;
  729. }
  730. #elif defined(WOLFSSL_SMALL_STACK)
  731. word32* W;
  732. W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
  733. DYNAMIC_TYPE_TMP_BUFFER);
  734. if (W == NULL)
  735. return MEMORY_E;
  736. #else
  737. word32 W[WC_SHA256_BLOCK_SIZE];
  738. #endif
  739. /* Copy context->state[] to working vars */
  740. for (i = 0; i < 8; i++)
  741. S[i] = sha256->digest[i];
  742. for (i = 0; i < 16; i++)
  743. W[i] = *((const word32*)&data[i*(int)sizeof(word32)]);
  744. for (i = 16; i < WC_SHA256_BLOCK_SIZE; i++)
  745. W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
  746. #ifdef USE_SLOW_SHA256
  747. /* not unrolled - ~2k smaller and ~25% slower */
  748. for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
  749. int j;
  750. for (j = 0; j < 8; j++) { /* braces needed here for macros {} */
  751. RND(j);
  752. }
  753. }
  754. #else
  755. /* partially loop unrolled */
  756. for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
  757. RND(0); RND(1); RND(2); RND(3);
  758. RND(4); RND(5); RND(6); RND(7);
  759. }
  760. #endif /* USE_SLOW_SHA256 */
  761. /* Add the working vars back into digest state[] */
  762. for (i = 0; i < 8; i++) {
  763. sha256->digest[i] += S[i];
  764. }
  765. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
  766. ForceZero(W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
  767. XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  768. #endif
  769. return 0;
  770. }
  771. #else
  772. /* SHA256 version that keeps all data in registers */
  773. #define SCHED1(j) (W[j] = *((word32*)&data[j*sizeof(word32)]))
  774. #define SCHED(j) ( \
  775. W[ j & 15] += \
  776. Gamma1(W[(j-2) & 15])+ \
  777. W[(j-7) & 15] + \
  778. Gamma0(W[(j-15) & 15]) \
  779. )
  780. #define RND1(j) \
  781. t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED1(j); \
  782. t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
  783. d(j) += t0; \
  784. h(j) = t0 + t1
  785. #define RNDN(j) \
  786. t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED(j); \
  787. t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
  788. d(j) += t0; \
  789. h(j) = t0 + t1
  790. static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
  791. {
  792. word32 S[8], t0, t1;
  793. int i;
  794. word32 W[WC_SHA256_BLOCK_SIZE/sizeof(word32)];
  795. /* Copy digest to working vars */
  796. S[0] = sha256->digest[0];
  797. S[1] = sha256->digest[1];
  798. S[2] = sha256->digest[2];
  799. S[3] = sha256->digest[3];
  800. S[4] = sha256->digest[4];
  801. S[5] = sha256->digest[5];
  802. S[6] = sha256->digest[6];
  803. S[7] = sha256->digest[7];
  804. i = 0;
  805. RND1( 0); RND1( 1); RND1( 2); RND1( 3);
  806. RND1( 4); RND1( 5); RND1( 6); RND1( 7);
  807. RND1( 8); RND1( 9); RND1(10); RND1(11);
  808. RND1(12); RND1(13); RND1(14); RND1(15);
  809. /* 64 operations, partially loop unrolled */
  810. for (i = 16; i < 64; i += 16) {
  811. RNDN( 0); RNDN( 1); RNDN( 2); RNDN( 3);
  812. RNDN( 4); RNDN( 5); RNDN( 6); RNDN( 7);
  813. RNDN( 8); RNDN( 9); RNDN(10); RNDN(11);
  814. RNDN(12); RNDN(13); RNDN(14); RNDN(15);
  815. }
  816. /* Add the working vars back into digest */
  817. sha256->digest[0] += S[0];
  818. sha256->digest[1] += S[1];
  819. sha256->digest[2] += S[2];
  820. sha256->digest[3] += S[3];
  821. sha256->digest[4] += S[4];
  822. sha256->digest[5] += S[5];
  823. sha256->digest[6] += S[6];
  824. sha256->digest[7] += S[7];
  825. return 0;
  826. }
  827. #endif /* SHA256_MANY_REGISTERS */
  828. #endif
  829. /* End wc_ software implementation */
  830. #ifdef XTRANSFORM
  831. static WC_INLINE void AddLength(wc_Sha256* sha256, word32 len)
  832. {
  833. word32 tmp = sha256->loLen;
  834. if ((sha256->loLen += len) < tmp) {
  835. sha256->hiLen++; /* carry low to high */
  836. }
  837. }
  838. /* do block size increments/updates */
  839. static WC_INLINE int Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  840. {
  841. int ret = 0;
  842. word32 blocksLen;
  843. byte* local;
  844. if (sha256 == NULL || (data == NULL && len > 0)) {
  845. return BAD_FUNC_ARG;
  846. }
  847. if (data == NULL && len == 0) {
  848. /* valid, but do nothing */
  849. return 0;
  850. }
  851. /* check that internal buffLen is valid */
  852. if (sha256->buffLen >= WC_SHA256_BLOCK_SIZE) {
  853. return BUFFER_E;
  854. }
  855. /* add length for final */
  856. AddLength(sha256, len);
  857. local = (byte*)sha256->buffer;
  858. /* process any remainder from previous operation */
  859. if (sha256->buffLen > 0) {
  860. blocksLen = min(len, WC_SHA256_BLOCK_SIZE - sha256->buffLen);
  861. XMEMCPY(&local[sha256->buffLen], data, blocksLen);
  862. sha256->buffLen += blocksLen;
  863. data += blocksLen;
  864. len -= blocksLen;
  865. if (sha256->buffLen == WC_SHA256_BLOCK_SIZE) {
  866. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  867. #if defined(WOLFSSL_X86_64_BUILD) && \
  868. defined(USE_INTEL_SPEEDUP) && \
  869. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  870. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  871. #endif
  872. {
  873. ByteReverseWords(sha256->buffer, sha256->buffer,
  874. WC_SHA256_BLOCK_SIZE);
  875. }
  876. #endif
  877. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  878. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  879. ESP_LOGV(TAG, "Sha256Update try hardware");
  880. esp_sha_try_hw_lock(&sha256->ctx);
  881. }
  882. if (sha256->ctx.mode == ESP32_SHA_SW) {
  883. ESP_LOGV(TAG, "Sha256Update process software");
  884. ret = XTRANSFORM(sha256, (const byte*)local);
  885. }
  886. else {
  887. ESP_LOGV(TAG, "Sha256Update process hardware");
  888. esp_sha256_process(sha256, (const byte*)local);
  889. }
  890. #else
  891. ret = XTRANSFORM(sha256, (const byte*)local);
  892. #endif
  893. if (ret == 0)
  894. sha256->buffLen = 0;
  895. else
  896. len = 0; /* error */
  897. }
  898. }
  899. /* process blocks */
  900. #ifdef XTRANSFORM_LEN
  901. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  902. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  903. if (Transform_Sha256_Len_p != NULL)
  904. #endif
  905. {
  906. /* get number of blocks */
  907. /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
  908. /* len (masked by 0xFFFFFFC0) returns block aligned length */
  909. blocksLen = len & ~((word32)WC_SHA256_BLOCK_SIZE-1);
  910. if (blocksLen > 0) {
  911. /* Byte reversal and alignment handled in function if required */
  912. XTRANSFORM_LEN(sha256, data, blocksLen);
  913. data += blocksLen;
  914. len -= blocksLen;
  915. }
  916. }
  917. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  918. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  919. else
  920. #endif
  921. #endif /* XTRANSFORM_LEN */
  922. #if !defined(XTRANSFORM_LEN) || \
  923. (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  924. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  925. {
  926. while (len >= WC_SHA256_BLOCK_SIZE) {
  927. word32* local32 = sha256->buffer;
  928. /* optimization to avoid memcpy if data pointer is properly aligned */
  929. /* Intel transform function requires use of sha256->buffer */
  930. /* Little Endian requires byte swap, so can't use data directly */
  931. #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) && \
  932. !(defined(WOLFSSL_X86_64_BUILD) && \
  933. defined(USE_INTEL_SPEEDUP) && \
  934. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  935. if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
  936. local32 = (word32*)data;
  937. }
  938. else
  939. #endif
  940. {
  941. XMEMCPY(local32, data, WC_SHA256_BLOCK_SIZE);
  942. }
  943. data += WC_SHA256_BLOCK_SIZE;
  944. len -= WC_SHA256_BLOCK_SIZE;
  945. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  946. #if defined(WOLFSSL_X86_64_BUILD) && \
  947. defined(USE_INTEL_SPEEDUP) && \
  948. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  949. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  950. #endif
  951. {
  952. ByteReverseWords(local32, local32, WC_SHA256_BLOCK_SIZE);
  953. }
  954. #endif
  955. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  956. if (sha256->ctx.mode == ESP32_SHA_INIT){
  957. ESP_LOGV(TAG, "Sha256Update try hardware loop");
  958. esp_sha_try_hw_lock(&sha256->ctx);
  959. }
  960. if (sha256->ctx.mode == ESP32_SHA_SW) {
  961. ESP_LOGV(TAG, "Sha256Update process software loop");
  962. ret = XTRANSFORM(sha256, (const byte*)local32);
  963. }
  964. else {
  965. ESP_LOGV(TAG, "Sha256Update process hardware");
  966. esp_sha256_process(sha256, (const byte*)local32);
  967. }
  968. #else
  969. ret = XTRANSFORM(sha256, (const byte*)local32);
  970. #endif
  971. if (ret != 0)
  972. break;
  973. }
  974. }
  975. #endif
  976. /* save remainder */
  977. if (ret == 0 && len > 0) {
  978. XMEMCPY(local, data, len);
  979. sha256->buffLen = len;
  980. }
  981. return ret;
  982. }
  983. #if defined(WOLFSSL_KCAPI_HASH)
  984. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  985. #else
  986. int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  987. {
  988. if (sha256 == NULL || (data == NULL && len > 0)) {
  989. return BAD_FUNC_ARG;
  990. }
  991. if (data == NULL && len == 0) {
  992. /* valid, but do nothing */
  993. return 0;
  994. }
  995. #ifdef WOLF_CRYPTO_CB
  996. #ifndef WOLF_CRYPTO_CB_FIND
  997. if (sha256->devId != INVALID_DEVID)
  998. #endif
  999. {
  1000. int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
  1001. if (ret != CRYPTOCB_UNAVAILABLE)
  1002. return ret;
  1003. /* fall-through when unavailable */
  1004. }
  1005. #endif
  1006. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1007. if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
  1008. #if defined(HAVE_INTEL_QA)
  1009. return IntelQaSymSha256(&sha256->asyncDev, NULL, data, len);
  1010. #endif
  1011. }
  1012. #endif /* WOLFSSL_ASYNC_CRYPT */
  1013. return Sha256Update(sha256, data, len);
  1014. }
  1015. #endif
  1016. static WC_INLINE int Sha256Final(wc_Sha256* sha256)
  1017. {
  1018. int ret;
  1019. byte* local;
  1020. if (sha256 == NULL) {
  1021. return BAD_FUNC_ARG;
  1022. }
  1023. /* we'll add a 0x80 byte at the end,
  1024. ** so make sure we have appropriate buffer length. */
  1025. if (sha256->buffLen > WC_SHA256_BLOCK_SIZE - 1) {
  1026. /* exit with error code if there's a bad buffer size in buffLen */
  1027. return BAD_STATE_E;
  1028. } /* buffLen check */
  1029. local = (byte*)sha256->buffer;
  1030. local[sha256->buffLen++] = 0x80; /* add 1 */
  1031. /* pad with zeros */
  1032. if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
  1033. XMEMSET(&local[sha256->buffLen], 0,
  1034. WC_SHA256_BLOCK_SIZE - sha256->buffLen);
  1035. sha256->buffLen += WC_SHA256_BLOCK_SIZE - sha256->buffLen;
  1036. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  1037. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1038. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1039. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  1040. #endif
  1041. {
  1042. ByteReverseWords(sha256->buffer, sha256->buffer,
  1043. WC_SHA256_BLOCK_SIZE);
  1044. }
  1045. #endif
  1046. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1047. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  1048. esp_sha_try_hw_lock(&sha256->ctx);
  1049. }
  1050. if (sha256->ctx.mode == ESP32_SHA_SW) {
  1051. ret = XTRANSFORM(sha256, (const byte*)local);
  1052. }
  1053. else {
  1054. ret = esp_sha256_process(sha256, (const byte*)local);
  1055. }
  1056. #else
  1057. ret = XTRANSFORM(sha256, (const byte*)local);
  1058. #endif
  1059. if (ret != 0)
  1060. return ret;
  1061. sha256->buffLen = 0;
  1062. }
  1063. XMEMSET(&local[sha256->buffLen], 0,
  1064. WC_SHA256_PAD_SIZE - sha256->buffLen);
  1065. /* put lengths in bits */
  1066. sha256->hiLen = (sha256->loLen >> (8 * sizeof(sha256->loLen) - 3)) +
  1067. (sha256->hiLen << 3);
  1068. sha256->loLen = sha256->loLen << 3;
  1069. /* store lengths */
  1070. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  1071. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1072. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1073. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  1074. #endif
  1075. {
  1076. ByteReverseWords(sha256->buffer, sha256->buffer,
  1077. WC_SHA256_BLOCK_SIZE);
  1078. }
  1079. #endif
  1080. /* ! length ordering dependent on digest endian type ! */
  1081. XMEMCPY(&local[WC_SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
  1082. XMEMCPY(&local[WC_SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
  1083. sizeof(word32));
  1084. #if defined(FREESCALE_MMCAU_SHA) || \
  1085. (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1086. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  1087. /* Kinetis requires only these bytes reversed */
  1088. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1089. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1090. if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
  1091. #endif
  1092. {
  1093. ByteReverseWords(
  1094. &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
  1095. &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
  1096. 2 * sizeof(word32));
  1097. }
  1098. #endif
  1099. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1100. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  1101. esp_sha_try_hw_lock(&sha256->ctx);
  1102. }
  1103. if (sha256->ctx.mode == ESP32_SHA_SW) {
  1104. ret = XTRANSFORM(sha256, (const byte*)local);
  1105. }
  1106. else {
  1107. ret = esp_sha256_digest_process(sha256, 1);
  1108. }
  1109. #else
  1110. ret = XTRANSFORM(sha256, (const byte*)local);
  1111. #endif
  1112. return ret;
  1113. }
  1114. #if !defined(WOLFSSL_KCAPI_HASH)
  1115. int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
  1116. {
  1117. #ifdef LITTLE_ENDIAN_ORDER
  1118. word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
  1119. #endif
  1120. if (sha256 == NULL || hash == NULL) {
  1121. return BAD_FUNC_ARG;
  1122. }
  1123. #ifdef LITTLE_ENDIAN_ORDER
  1124. ByteReverseWords((word32*)digest, (word32*)sha256->digest,
  1125. WC_SHA256_DIGEST_SIZE);
  1126. XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
  1127. #else
  1128. XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
  1129. #endif
  1130. return 0;
  1131. }
  1132. int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
  1133. {
  1134. int ret;
  1135. if (sha256 == NULL || hash == NULL) {
  1136. return BAD_FUNC_ARG;
  1137. }
  1138. #ifdef WOLF_CRYPTO_CB
  1139. #ifndef WOLF_CRYPTO_CB_FIND
  1140. if (sha256->devId != INVALID_DEVID)
  1141. #endif
  1142. {
  1143. ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
  1144. if (ret != CRYPTOCB_UNAVAILABLE)
  1145. return ret;
  1146. /* fall-through when unavailable */
  1147. }
  1148. #endif
  1149. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1150. if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
  1151. #if defined(HAVE_INTEL_QA)
  1152. return IntelQaSymSha256(&sha256->asyncDev, hash, NULL,
  1153. WC_SHA256_DIGEST_SIZE);
  1154. #endif
  1155. }
  1156. #endif /* WOLFSSL_ASYNC_CRYPT */
  1157. ret = Sha256Final(sha256);
  1158. if (ret != 0) {
  1159. return ret;
  1160. }
  1161. #if defined(LITTLE_ENDIAN_ORDER)
  1162. ByteReverseWords(sha256->digest, sha256->digest, WC_SHA256_DIGEST_SIZE);
  1163. #endif
  1164. XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
  1165. return InitSha256(sha256); /* reset state */
  1166. }
  1167. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  1168. /* Apply SHA256 transformation to the data */
  1169. /* @param sha a pointer to wc_Sha256 structure */
  1170. /* @param data data to be applied SHA256 transformation */
  1171. /* @return 0 on successful, otherwise non-zero on failure */
  1172. int wc_Sha256Transform(wc_Sha256* sha, const unsigned char* data)
  1173. {
  1174. if (sha == NULL || data == NULL) {
  1175. return BAD_FUNC_ARG;
  1176. }
  1177. return (Transform_Sha256(sha, data));
  1178. }
  1179. #endif
  1180. #endif /* OPENSSL_EXTRA */
  1181. #endif /* !WOLFSSL_KCAPI_HASH */
  1182. #ifdef WOLFSSL_SHA224
  1183. #ifdef STM32_HASH_SHA2
  1184. /* Supports CubeMX HAL or Standard Peripheral Library */
  1185. int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
  1186. {
  1187. if (sha224 == NULL)
  1188. return BAD_FUNC_ARG;
  1189. (void)devId;
  1190. (void)heap;
  1191. XMEMSET(sha224, 0, sizeof(wc_Sha224));
  1192. wc_Stm32_Hash_Init(&sha224->stmCtx);
  1193. return 0;
  1194. }
  1195. int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
  1196. {
  1197. int ret = 0;
  1198. if (sha224 == NULL || (data == NULL && len > 0)) {
  1199. return BAD_FUNC_ARG;
  1200. }
  1201. ret = wolfSSL_CryptHwMutexLock();
  1202. if (ret == 0) {
  1203. ret = wc_Stm32_Hash_Update(&sha224->stmCtx,
  1204. HASH_AlgoSelection_SHA224, data, len, WC_SHA224_BLOCK_SIZE);
  1205. wolfSSL_CryptHwMutexUnLock();
  1206. }
  1207. return ret;
  1208. }
  1209. int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
  1210. {
  1211. int ret = 0;
  1212. if (sha224 == NULL || hash == NULL) {
  1213. return BAD_FUNC_ARG;
  1214. }
  1215. ret = wolfSSL_CryptHwMutexLock();
  1216. if (ret == 0) {
  1217. ret = wc_Stm32_Hash_Final(&sha224->stmCtx,
  1218. HASH_AlgoSelection_SHA224, hash, WC_SHA224_DIGEST_SIZE);
  1219. wolfSSL_CryptHwMutexUnLock();
  1220. }
  1221. (void)wc_InitSha224(sha224); /* reset state */
  1222. return ret;
  1223. }
  1224. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1225. int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
  1226. {
  1227. if (sha224 == NULL) {
  1228. return BAD_FUNC_ARG;
  1229. }
  1230. (void)devId;
  1231. return se050_hash_init(&sha224->se050Ctx, heap);
  1232. }
  1233. int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
  1234. {
  1235. return se050_hash_update(&sha224->se050Ctx, data, len);
  1236. }
  1237. int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
  1238. {
  1239. int ret = 0;
  1240. ret = se050_hash_final(&sha224->se050Ctx, hash, WC_SHA224_DIGEST_SIZE,
  1241. kAlgorithm_SSS_SHA224);
  1242. (void)wc_InitSha224(sha224);
  1243. return ret;
  1244. }
  1245. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  1246. !defined(WOLFSSL_QNX_CAAM)
  1247. /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
  1248. #elif defined(WOLFSSL_AFALG_HASH)
  1249. #error SHA224 currently not supported with AF_ALG enabled
  1250. #elif defined(WOLFSSL_DEVCRYPTO_HASH)
  1251. /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
  1252. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  1253. /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
  1254. #elif defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
  1255. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1256. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  1257. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1258. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  1259. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  1260. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1261. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1262. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1263. #else
  1264. #define NEED_SOFT_SHA224
  1265. static int InitSha224(wc_Sha224* sha224)
  1266. {
  1267. int ret = 0;
  1268. if (sha224 == NULL) {
  1269. return BAD_FUNC_ARG;
  1270. }
  1271. sha224->digest[0] = 0xc1059ed8;
  1272. sha224->digest[1] = 0x367cd507;
  1273. sha224->digest[2] = 0x3070dd17;
  1274. sha224->digest[3] = 0xf70e5939;
  1275. sha224->digest[4] = 0xffc00b31;
  1276. sha224->digest[5] = 0x68581511;
  1277. sha224->digest[6] = 0x64f98fa7;
  1278. sha224->digest[7] = 0xbefa4fa4;
  1279. sha224->buffLen = 0;
  1280. sha224->loLen = 0;
  1281. sha224->hiLen = 0;
  1282. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1283. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1284. /* choose best Transform function under this runtime environment */
  1285. Sha256_SetTransform();
  1286. #endif
  1287. #ifdef WOLFSSL_HASH_FLAGS
  1288. sha224->flags = 0;
  1289. #endif
  1290. #ifdef WOLFSSL_HASH_KEEP
  1291. sha224->msg = NULL;
  1292. sha224->len = 0;
  1293. sha224->used = 0;
  1294. #endif
  1295. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1296. /* not to be confused with HAS512_224 */
  1297. sha224->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
  1298. #endif
  1299. return ret;
  1300. }
  1301. #endif
  1302. #ifdef NEED_SOFT_SHA224
  1303. int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
  1304. {
  1305. int ret = 0;
  1306. if (sha224 == NULL)
  1307. return BAD_FUNC_ARG;
  1308. sha224->heap = heap;
  1309. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1310. sha224->W = NULL;
  1311. #endif
  1312. #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  1313. /* We know this is a fresh, uninitialized item, so set to INIT */
  1314. if (sha224->ctx.mode != ESP32_SHA_SW) {
  1315. ESP_LOGV(TAG, "Set sha224 ctx mode init to ESP32_SHA_SW. "
  1316. "Prior value: %d", sha224->ctx.mode);
  1317. }
  1318. /* no sha224 HW support is available, set to SW */
  1319. sha224->ctx.mode = ESP32_SHA_SW;
  1320. #endif
  1321. ret = InitSha224(sha224);
  1322. if (ret != 0) {
  1323. return ret;
  1324. }
  1325. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1326. ret = wolfAsync_DevCtxInit(&sha224->asyncDev,
  1327. WOLFSSL_ASYNC_MARKER_SHA224, sha224->heap, devId);
  1328. #else
  1329. (void)devId;
  1330. #endif /* WOLFSSL_ASYNC_CRYPT */
  1331. #ifdef WOLFSSL_IMXRT1170_CAAM
  1332. ret = wc_CAAM_HashInit(&sha224->hndl, &sha224->ctx, WC_HASH_TYPE_SHA224);
  1333. #endif
  1334. #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  1335. if (sha224->ctx.mode != ESP32_SHA_INIT) {
  1336. ESP_LOGV("SHA224", "Set ctx mode from prior value: "
  1337. "%d", sha224->ctx.mode);
  1338. }
  1339. /* We know this is a fresh, uninitialized item, so set to INIT */
  1340. sha224->ctx.mode = ESP32_SHA_INIT;
  1341. #endif
  1342. return ret;
  1343. }
  1344. int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
  1345. {
  1346. int ret;
  1347. if (sha224 == NULL || (data == NULL && len > 0)) {
  1348. return BAD_FUNC_ARG;
  1349. }
  1350. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1351. if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
  1352. #if defined(HAVE_INTEL_QA)
  1353. return IntelQaSymSha224(&sha224->asyncDev, NULL, data, len);
  1354. #endif
  1355. }
  1356. #endif /* WOLFSSL_ASYNC_CRYPT */
  1357. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1358. sha224->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
  1359. #endif
  1360. ret = Sha256Update((wc_Sha256*)sha224, data, len);
  1361. return ret;
  1362. }
  1363. int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
  1364. {
  1365. int ret;
  1366. if (sha224 == NULL || hash == NULL) {
  1367. return BAD_FUNC_ARG;
  1368. }
  1369. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1370. if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
  1371. #if defined(HAVE_INTEL_QA)
  1372. return IntelQaSymSha224(&sha224->asyncDev, hash, NULL,
  1373. WC_SHA224_DIGEST_SIZE);
  1374. #endif
  1375. }
  1376. #endif /* WOLFSSL_ASYNC_CRYPT */
  1377. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1378. sha224->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
  1379. #endif
  1380. ret = Sha256Final((wc_Sha256*)sha224);
  1381. if (ret != 0)
  1382. return ret;
  1383. #if defined(LITTLE_ENDIAN_ORDER)
  1384. ByteReverseWords(sha224->digest, sha224->digest, WC_SHA224_DIGEST_SIZE);
  1385. #endif
  1386. XMEMCPY(hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
  1387. return InitSha224(sha224); /* reset state */
  1388. }
  1389. #endif /* end of SHA224 software implementation */
  1390. int wc_InitSha224(wc_Sha224* sha224)
  1391. {
  1392. int devId = INVALID_DEVID;
  1393. #ifdef WOLF_CRYPTO_CB
  1394. devId = wc_CryptoCb_DefaultDevID();
  1395. #endif
  1396. return wc_InitSha224_ex(sha224, NULL, devId);
  1397. }
  1398. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
  1399. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1400. void wc_Sha224Free(wc_Sha224* sha224)
  1401. {
  1402. if (sha224 == NULL)
  1403. return;
  1404. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1405. if (sha224->W != NULL) {
  1406. ForceZero(sha224->W, sizeof(word32) * WC_SHA224_BLOCK_SIZE);
  1407. XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
  1408. sha224->W = NULL;
  1409. }
  1410. #endif
  1411. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1412. wolfAsync_DevCtxFree(&sha224->asyncDev, WOLFSSL_ASYNC_MARKER_SHA224);
  1413. #endif /* WOLFSSL_ASYNC_CRYPT */
  1414. #ifdef WOLFSSL_PIC32MZ_HASH
  1415. wc_Sha256Pic32Free(sha224);
  1416. #endif
  1417. #if defined(WOLFSSL_KCAPI_HASH)
  1418. KcapiHashFree(&sha224->kcapi);
  1419. #endif
  1420. #if defined(WOLFSSL_RENESAS_RX64_HASH)
  1421. if (sha224->msg != NULL) {
  1422. ForceZero(sha224->msg, sha224->len);
  1423. XFREE(sha224->msg, sha224->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1424. sha224->msg = NULL;
  1425. }
  1426. #endif
  1427. ForceZero(sha224, sizeof(*sha224));
  1428. }
  1429. #endif /* WOLFSSL_SHA224 */
  1430. #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
  1431. int wc_InitSha256(wc_Sha256* sha256)
  1432. {
  1433. int devId = INVALID_DEVID;
  1434. #ifdef WOLF_CRYPTO_CB
  1435. devId = wc_CryptoCb_DefaultDevID();
  1436. #endif
  1437. return wc_InitSha256_ex(sha256, NULL, devId);
  1438. }
  1439. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
  1440. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1441. void wc_Sha256Free(wc_Sha256* sha256)
  1442. {
  1443. if (sha256 == NULL)
  1444. return;
  1445. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1446. if (sha256->W != NULL) {
  1447. ForceZero(sha256->W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
  1448. XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST);
  1449. sha256->W = NULL;
  1450. }
  1451. #endif
  1452. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1453. wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256);
  1454. #endif /* WOLFSSL_ASYNC_CRYPT */
  1455. #ifdef WOLFSSL_PIC32MZ_HASH
  1456. wc_Sha256Pic32Free(sha256);
  1457. #endif
  1458. #if defined(WOLFSSL_AFALG_HASH)
  1459. if (sha256->alFd > 0) {
  1460. close(sha256->alFd);
  1461. sha256->alFd = -1; /* avoid possible double close on socket */
  1462. }
  1463. if (sha256->rdFd > 0) {
  1464. close(sha256->rdFd);
  1465. sha256->rdFd = -1; /* avoid possible double close on socket */
  1466. }
  1467. #endif /* WOLFSSL_AFALG_HASH */
  1468. #ifdef WOLFSSL_DEVCRYPTO_HASH
  1469. wc_DevCryptoFree(&sha256->ctx);
  1470. #endif /* WOLFSSL_DEVCRYPTO */
  1471. #if (defined(WOLFSSL_AFALG_HASH) && defined(WOLFSSL_AFALG_HASH_KEEP)) || \
  1472. (defined(WOLFSSL_DEVCRYPTO_HASH) && defined(WOLFSSL_DEVCRYPTO_HASH_KEEP)) || \
  1473. ((defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  1474. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  1475. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) || \
  1476. (defined(WOLFSSL_RENESAS_SCEPROTECT) && \
  1477. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) || \
  1478. defined(WOLFSSL_RENESAS_RX64_HASH) || \
  1479. defined(WOLFSSL_HASH_KEEP)
  1480. if (sha256->msg != NULL) {
  1481. ForceZero(sha256->msg, sha256->len);
  1482. XFREE(sha256->msg, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1483. sha256->msg = NULL;
  1484. }
  1485. #endif
  1486. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1487. se050_hash_free(&sha256->se050Ctx);
  1488. #endif
  1489. #if defined(WOLFSSL_KCAPI_HASH)
  1490. KcapiHashFree(&sha256->kcapi);
  1491. #endif
  1492. #ifdef WOLFSSL_IMXRT_DCP
  1493. DCPSha256Free(sha256);
  1494. #endif
  1495. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  1496. wc_MAXQ10XX_Sha256Free(sha256);
  1497. #endif
  1498. #ifdef HAVE_ARIA
  1499. if (sha256->hSession != NULL) {
  1500. MC_CloseSession(sha256->hSession);
  1501. sha256->hSession = NULL;
  1502. }
  1503. #endif
  1504. /* Espressif embedded hardware acceleration specific: */
  1505. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1506. if (sha256->ctx.lockDepth > 0) {
  1507. /* probably due to unclean shutdown, error, or other problem.
  1508. *
  1509. * if you find yourself here, code needs to be cleaned up to
  1510. * properly release hardware. this init is only for handling
  1511. * the unexpected. by the time free is called, the hardware
  1512. * should have already been released (lockDepth = 0)
  1513. */
  1514. (void)InitSha256(sha256); /* unlock mutex, set mode to ESP32_SHA_INIT */
  1515. ESP_LOGV(TAG, "Alert: hardware unlock needed in wc_Sha256Free.");
  1516. }
  1517. else {
  1518. ESP_LOGV(TAG, "Hardware unlock not needed in wc_Sha256Free.");
  1519. }
  1520. #endif
  1521. ForceZero(sha256, sizeof(*sha256));
  1522. }
  1523. #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
  1524. #ifdef WOLFSSL_HASH_KEEP
  1525. /* Some hardware have issues with update, this function stores the data to be
  1526. * hashed into an array. Once ready, the Final operation is called on all of the
  1527. * data to be hashed at once.
  1528. * returns 0 on success
  1529. */
  1530. int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz)
  1531. {
  1532. return _wc_Hash_Grow(&(sha256->msg), &(sha256->used), &(sha256->len), in,
  1533. inSz, sha256->heap);
  1534. }
  1535. #ifdef WOLFSSL_SHA224
  1536. int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
  1537. {
  1538. return _wc_Hash_Grow(&(sha224->msg), &(sha224->used), &(sha224->len), in,
  1539. inSz, sha224->heap);
  1540. }
  1541. #endif /* WOLFSSL_SHA224 */
  1542. #endif /* WOLFSSL_HASH_KEEP */
  1543. #endif /* !WOLFSSL_TI_HASH */
  1544. #ifndef WOLFSSL_TI_HASH
  1545. #if !defined(WOLFSSL_RENESAS_RX64_HASH) && \
  1546. (!defined(WOLFSSL_RENESAS_RSIP) || \
  1547. defined(NO_WOLFSSL_RENESAS_FSPSM_HASH))
  1548. #ifdef WOLFSSL_SHA224
  1549. #if defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
  1550. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1551. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  1552. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1553. #else
  1554. int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash)
  1555. {
  1556. int ret;
  1557. #ifdef WOLFSSL_SMALL_STACK
  1558. wc_Sha224* tmpSha224;
  1559. #else
  1560. wc_Sha224 tmpSha224[1];
  1561. #endif
  1562. if (sha224 == NULL || hash == NULL) {
  1563. return BAD_FUNC_ARG;
  1564. }
  1565. #ifdef WOLFSSL_SMALL_STACK
  1566. tmpSha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
  1567. DYNAMIC_TYPE_TMP_BUFFER);
  1568. if (tmpSha224 == NULL) {
  1569. return MEMORY_E;
  1570. }
  1571. #endif
  1572. ret = wc_Sha224Copy(sha224, tmpSha224);
  1573. if (ret == 0) {
  1574. ret = wc_Sha224Final(tmpSha224, hash);
  1575. wc_Sha224Free(tmpSha224);
  1576. }
  1577. #ifdef WOLFSSL_SMALL_STACK
  1578. XFREE(tmpSha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1579. #endif
  1580. return ret;
  1581. }
  1582. int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
  1583. {
  1584. int ret = 0; /* assume success unless proven otherwise */
  1585. if (src == NULL || dst == NULL) {
  1586. return BAD_FUNC_ARG;
  1587. }
  1588. XMEMCPY(dst, src, sizeof(wc_Sha224));
  1589. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1590. dst->W = NULL;
  1591. #endif
  1592. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
  1593. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  1594. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  1595. #endif
  1596. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1597. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  1598. #endif
  1599. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1600. ret = esp_sha224_ctx_copy(src, dst);
  1601. #endif
  1602. #ifdef WOLFSSL_HASH_FLAGS
  1603. dst->flags |= WC_HASH_FLAG_ISCOPY;
  1604. #endif
  1605. #if defined(WOLFSSL_HASH_KEEP)
  1606. if (src->msg != NULL) {
  1607. dst->msg = (byte*)XMALLOC(src->len, dst->heap,
  1608. DYNAMIC_TYPE_TMP_BUFFER);
  1609. if (dst->msg == NULL)
  1610. return MEMORY_E;
  1611. XMEMCPY(dst->msg, src->msg, src->len);
  1612. }
  1613. #endif
  1614. return ret;
  1615. }
  1616. #endif /* WOLFSSL_KCAPI_HASH && !WOLFSSL_NO_KCAPI_SHA224 */
  1617. #ifdef WOLFSSL_HASH_FLAGS
  1618. int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags)
  1619. {
  1620. if (sha224) {
  1621. sha224->flags = flags;
  1622. }
  1623. return 0;
  1624. }
  1625. int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags)
  1626. {
  1627. if (sha224 && flags) {
  1628. *flags = sha224->flags;
  1629. }
  1630. return 0;
  1631. }
  1632. #endif
  1633. #endif /* WOLFSSL_SHA224 */
  1634. #endif /* WOLFSSL_RENESAS_RX64_HASH */
  1635. #ifdef WOLFSSL_AFALG_HASH
  1636. /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
  1637. #elif defined(WOLFSSL_DEVCRYPTO_HASH)
  1638. /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
  1639. #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  1640. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  1641. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  1642. /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
  1643. #elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP))\
  1644. && !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1645. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1646. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  1647. /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
  1648. #elif defined(WOLFSSL_IMXRT_DCP)
  1649. /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
  1650. #elif defined(WOLFSSL_KCAPI_HASH)
  1651. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1652. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  1653. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1654. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  1655. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  1656. #else
  1657. int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
  1658. {
  1659. int ret;
  1660. #ifdef WOLFSSL_SMALL_STACK
  1661. wc_Sha256* tmpSha256;
  1662. #else
  1663. wc_Sha256 tmpSha256[1];
  1664. #endif
  1665. if (sha256 == NULL || hash == NULL) {
  1666. return BAD_FUNC_ARG;
  1667. }
  1668. #ifdef WOLFSSL_SMALL_STACK
  1669. tmpSha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
  1670. DYNAMIC_TYPE_TMP_BUFFER);
  1671. if (tmpSha256 == NULL) {
  1672. return MEMORY_E;
  1673. }
  1674. #endif
  1675. ret = wc_Sha256Copy(sha256, tmpSha256);
  1676. if (ret == 0) {
  1677. ret = wc_Sha256Final(tmpSha256, hash);
  1678. wc_Sha256Free(tmpSha256); /* TODO move outside brackets? */
  1679. }
  1680. #ifdef WOLFSSL_SMALL_STACK
  1681. XFREE(tmpSha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1682. #endif
  1683. return ret;
  1684. }
  1685. int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
  1686. {
  1687. int ret = 0;
  1688. if (src == NULL || dst == NULL) {
  1689. return BAD_FUNC_ARG;
  1690. }
  1691. XMEMCPY(dst, src, sizeof(wc_Sha256));
  1692. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  1693. wc_MAXQ10XX_Sha256Copy(src);
  1694. #endif
  1695. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1696. dst->W = NULL;
  1697. #endif
  1698. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
  1699. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  1700. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  1701. #endif
  1702. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1703. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  1704. #endif
  1705. #ifdef WOLFSSL_PIC32MZ_HASH
  1706. ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
  1707. #endif
  1708. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1709. esp_sha256_ctx_copy(src, dst);
  1710. #endif
  1711. #ifdef HAVE_ARIA
  1712. dst->hSession = NULL;
  1713. if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
  1714. return MEMORY_E;
  1715. }
  1716. #endif
  1717. #ifdef WOLFSSL_HASH_FLAGS
  1718. dst->flags |= WC_HASH_FLAG_ISCOPY;
  1719. #endif
  1720. #if defined(WOLFSSL_HASH_KEEP)
  1721. if (src->msg != NULL) {
  1722. dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1723. if (dst->msg == NULL)
  1724. return MEMORY_E;
  1725. XMEMCPY(dst->msg, src->msg, src->len);
  1726. }
  1727. #endif
  1728. return ret;
  1729. }
  1730. #endif
  1731. #ifdef WOLFSSL_HASH_FLAGS
  1732. int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags)
  1733. {
  1734. if (sha256) {
  1735. sha256->flags = flags;
  1736. }
  1737. return 0;
  1738. }
  1739. int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags)
  1740. {
  1741. if (sha256 && flags) {
  1742. *flags = sha256->flags;
  1743. }
  1744. return 0;
  1745. }
  1746. #endif
  1747. #endif /* !WOLFSSL_TI_HASH */
  1748. #endif /* NO_SHA256 */