sha256.c 64 KB

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