sha256.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  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 (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. {
  298. Transform_Sha256_p = Transform_Sha256_AVX2;
  299. Transform_Sha256_Len_p = Transform_Sha256_AVX2_Len;
  300. Transform_Sha256_is_vectorized = 1;
  301. }
  302. }
  303. else
  304. #endif
  305. #ifdef HAVE_INTEL_AVX1
  306. if (IS_INTEL_AVX1(intel_flags)) {
  307. #ifdef HAVE_INTEL_RORX
  308. if (IS_INTEL_BMI2(intel_flags)) {
  309. Transform_Sha256_p = Transform_Sha256_AVX1_RORX;
  310. Transform_Sha256_Len_p = Transform_Sha256_AVX1_RORX_Len;
  311. Transform_Sha256_is_vectorized = 1;
  312. }
  313. else
  314. #endif
  315. {
  316. Transform_Sha256_p = Transform_Sha256_AVX1;
  317. Transform_Sha256_Len_p = Transform_Sha256_AVX1_Len;
  318. Transform_Sha256_is_vectorized = 1;
  319. }
  320. }
  321. else
  322. #endif
  323. {
  324. Transform_Sha256_p = Transform_Sha256;
  325. Transform_Sha256_Len_p = NULL;
  326. Transform_Sha256_is_vectorized = 0;
  327. }
  328. transform_check = 1;
  329. }
  330. #if !defined(WOLFSSL_KCAPI_HASH)
  331. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  332. {
  333. int ret = 0;
  334. if (sha256 == NULL)
  335. return BAD_FUNC_ARG;
  336. sha256->heap = heap;
  337. #ifdef WOLF_CRYPTO_CB
  338. sha256->devId = devId;
  339. sha256->devCtx = NULL;
  340. #endif
  341. #ifdef WOLFSSL_SMALL_STACK_CACHE
  342. sha256->W = NULL;
  343. #endif
  344. ret = InitSha256(sha256);
  345. if (ret != 0)
  346. return ret;
  347. /* choose best Transform function under this runtime environment */
  348. Sha256_SetTransform();
  349. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  350. ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
  351. WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
  352. #else
  353. (void)devId;
  354. #endif /* WOLFSSL_ASYNC_CRYPT */
  355. return ret;
  356. }
  357. #endif /* !WOLFSSL_KCAPI_HASH */
  358. #elif defined(FREESCALE_LTC_SHA)
  359. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  360. {
  361. (void)heap;
  362. (void)devId;
  363. LTC_HASH_Init(LTC_BASE, &sha256->ctx, kLTC_Sha256, NULL, 0);
  364. return 0;
  365. }
  366. #elif defined(FREESCALE_MMCAU_SHA)
  367. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  368. #include "cau_api.h"
  369. #else
  370. #include "fsl_mmcau.h"
  371. #endif
  372. #define XTRANSFORM(S, D) Transform_Sha256((S),(D))
  373. #define XTRANSFORM_LEN(S, D, L) Transform_Sha256_Len((S),(D),(L))
  374. #ifndef WC_HASH_DATA_ALIGNMENT
  375. /* these hardware API's require 4 byte (word32) alignment */
  376. #define WC_HASH_DATA_ALIGNMENT 4
  377. #endif
  378. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  379. {
  380. int ret = 0;
  381. (void)heap;
  382. (void)devId;
  383. ret = wolfSSL_CryptHwMutexLock();
  384. if (ret != 0) {
  385. return ret;
  386. }
  387. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  388. cau_sha256_initialize_output(sha256->digest);
  389. #else
  390. MMCAU_SHA256_InitializeOutput((uint32_t*)sha256->digest);
  391. #endif
  392. wolfSSL_CryptHwMutexUnLock();
  393. sha256->buffLen = 0;
  394. sha256->loLen = 0;
  395. sha256->hiLen = 0;
  396. #ifdef WOLFSSL_SMALL_STACK_CACHE
  397. sha256->W = NULL;
  398. #endif
  399. return ret;
  400. }
  401. static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
  402. {
  403. int ret = wolfSSL_CryptHwMutexLock();
  404. if (ret == 0) {
  405. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  406. cau_sha256_hash_n((byte*)data, 1, sha256->digest);
  407. #else
  408. MMCAU_SHA256_HashN((byte*)data, 1, (uint32_t*)sha256->digest);
  409. #endif
  410. wolfSSL_CryptHwMutexUnLock();
  411. }
  412. return ret;
  413. }
  414. static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
  415. word32 len)
  416. {
  417. int ret = wolfSSL_CryptHwMutexLock();
  418. if (ret == 0) {
  419. #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
  420. if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
  421. /* data pointer is NOT aligned,
  422. * so copy and perform one block at a time */
  423. byte* local = (byte*)sha256->buffer;
  424. while (len >= WC_SHA256_BLOCK_SIZE) {
  425. XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
  426. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  427. cau_sha256_hash_n(local, 1, sha256->digest);
  428. #else
  429. MMCAU_SHA256_HashN(local, 1, (uint32_t*)sha256->digest);
  430. #endif
  431. data += WC_SHA256_BLOCK_SIZE;
  432. len -= WC_SHA256_BLOCK_SIZE;
  433. }
  434. }
  435. else
  436. #endif
  437. {
  438. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  439. cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
  440. sha256->digest);
  441. #else
  442. MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
  443. (uint32_t*)sha256->digest);
  444. #endif
  445. }
  446. wolfSSL_CryptHwMutexUnLock();
  447. }
  448. return ret;
  449. }
  450. #elif defined(WOLFSSL_PIC32MZ_HASH)
  451. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  452. #elif defined(STM32_HASH_SHA2)
  453. /* Supports CubeMX HAL or Standard Peripheral Library */
  454. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  455. {
  456. if (sha256 == NULL)
  457. return BAD_FUNC_ARG;
  458. (void)devId;
  459. (void)heap;
  460. XMEMSET(sha256, 0, sizeof(wc_Sha256));
  461. wc_Stm32_Hash_Init(&sha256->stmCtx);
  462. return 0;
  463. }
  464. int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  465. {
  466. int ret = 0;
  467. if (sha256 == NULL || (data == NULL && len > 0)) {
  468. return BAD_FUNC_ARG;
  469. }
  470. ret = wolfSSL_CryptHwMutexLock();
  471. if (ret == 0) {
  472. ret = wc_Stm32_Hash_Update(&sha256->stmCtx,
  473. HASH_AlgoSelection_SHA256, data, len, WC_SHA256_BLOCK_SIZE);
  474. wolfSSL_CryptHwMutexUnLock();
  475. }
  476. return ret;
  477. }
  478. int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
  479. {
  480. int ret = 0;
  481. if (sha256 == NULL || hash == NULL) {
  482. return BAD_FUNC_ARG;
  483. }
  484. ret = wolfSSL_CryptHwMutexLock();
  485. if (ret == 0) {
  486. ret = wc_Stm32_Hash_Final(&sha256->stmCtx,
  487. HASH_AlgoSelection_SHA256, hash, WC_SHA256_DIGEST_SIZE);
  488. wolfSSL_CryptHwMutexUnLock();
  489. }
  490. (void)wc_InitSha256(sha256); /* reset state */
  491. return ret;
  492. }
  493. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  494. !defined(WOLFSSL_QNX_CAAM)
  495. /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
  496. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  497. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  498. {
  499. if (sha256 == NULL) {
  500. return BAD_FUNC_ARG;
  501. }
  502. (void)devId;
  503. return se050_hash_init(&sha256->se050Ctx, heap);
  504. }
  505. int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  506. {
  507. return se050_hash_update(&sha256->se050Ctx, data, len);
  508. }
  509. int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
  510. {
  511. int ret = 0;
  512. ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
  513. kAlgorithm_SSS_SHA256);
  514. return ret;
  515. }
  516. int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
  517. {
  518. int ret = 0;
  519. ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
  520. kAlgorithm_SSS_SHA256);
  521. return ret;
  522. }
  523. #elif defined(WOLFSSL_AFALG_HASH)
  524. /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
  525. #elif defined(WOLFSSL_DEVCRYPTO_HASH)
  526. /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
  527. #elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)
  528. #include "hal_data.h"
  529. #ifndef WOLFSSL_SCE_SHA256_HANDLE
  530. #define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0
  531. #endif
  532. #define WC_SHA256_DIGEST_WORD_SIZE 16
  533. #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM((S), (D))
  534. static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data)
  535. {
  536. if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
  537. CRYPTO_WORD_ENDIAN_LITTLE)
  538. {
  539. ByteReverseWords((word32*)data, (word32*)data,
  540. WC_SHA256_BLOCK_SIZE);
  541. ByteReverseWords(sha256->digest, sha256->digest,
  542. WC_SHA256_DIGEST_SIZE);
  543. }
  544. if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(
  545. WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, (word32*)data,
  546. WC_SHA256_DIGEST_WORD_SIZE, sha256->digest) != SSP_SUCCESS){
  547. WOLFSSL_MSG("Unexpected hardware return value");
  548. return WC_HW_E;
  549. }
  550. if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
  551. CRYPTO_WORD_ENDIAN_LITTLE)
  552. {
  553. ByteReverseWords((word32*)data, (word32*)data,
  554. WC_SHA256_BLOCK_SIZE);
  555. ByteReverseWords(sha256->digest, sha256->digest,
  556. WC_SHA256_DIGEST_SIZE);
  557. }
  558. return 0;
  559. }
  560. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  561. {
  562. int ret = 0;
  563. if (sha256 == NULL)
  564. return BAD_FUNC_ARG;
  565. sha256->heap = heap;
  566. ret = InitSha256(sha256);
  567. if (ret != 0)
  568. return ret;
  569. (void)devId;
  570. return ret;
  571. }
  572. #elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  573. /* HW may fail since there's only one, so we still need SW */
  574. #define NEED_SOFT_SHA256
  575. /*
  576. ** An Espressif-specific InitSha256()
  577. **
  578. ** soft SHA needs initialization digest, but HW does not.
  579. */
  580. static int InitSha256(wc_Sha256* sha256)
  581. {
  582. int ret = 0; /* zero = success */
  583. if (sha256 == NULL) {
  584. return BAD_FUNC_ARG;
  585. }
  586. /* We may or may not need initial digest for HW.
  587. * Always needed for SW-only. */
  588. sha256->digest[0] = 0x6A09E667L;
  589. sha256->digest[1] = 0xBB67AE85L;
  590. sha256->digest[2] = 0x3C6EF372L;
  591. sha256->digest[3] = 0xA54FF53AL;
  592. sha256->digest[4] = 0x510E527FL;
  593. sha256->digest[5] = 0x9B05688CL;
  594. sha256->digest[6] = 0x1F83D9ABL;
  595. sha256->digest[7] = 0x5BE0CD19L;
  596. sha256->buffLen = 0;
  597. sha256->loLen = 0;
  598. sha256->hiLen = 0;
  599. #ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
  600. ret = esp_sha_init(&(sha256->ctx), WC_HASH_TYPE_SHA256);
  601. #endif
  602. return ret;
  603. }
  604. /*
  605. ** An Espressif-specific wolfCrypt InitSha256 external wrapper.
  606. **
  607. ** we'll assume this is ALWAYS for a new, uninitialized sha256
  608. */
  609. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  610. {
  611. (void)devId;
  612. if (sha256 == NULL) {
  613. return BAD_FUNC_ARG;
  614. }
  615. #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  616. #ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
  617. /* We know this is a fresh, uninitialized item, so set to INIT */
  618. if (sha256->ctx.mode != ESP32_SHA_INIT) {
  619. ESP_LOGV(TAG, "Set ctx mode from prior value: "
  620. "%d", sha256->ctx.mode);
  621. }
  622. sha256->ctx.mode = ESP32_SHA_INIT;
  623. #endif
  624. #endif
  625. return InitSha256(sha256);
  626. }
  627. #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  628. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  629. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  630. /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
  631. #elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP)) \
  632. && !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  633. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  634. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  635. /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
  636. #elif defined(WOLFSSL_IMXRT_DCP)
  637. #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
  638. /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
  639. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  640. /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
  641. #elif defined(WOLFSSL_KCAPI_HASH)
  642. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  643. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  644. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  645. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  646. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  647. #else
  648. #define NEED_SOFT_SHA256
  649. int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
  650. {
  651. int ret = 0;
  652. if (sha256 == NULL)
  653. return BAD_FUNC_ARG;
  654. ret = InitSha256(sha256);
  655. if (ret != 0)
  656. return ret;
  657. sha256->heap = heap;
  658. #ifdef WOLF_CRYPTO_CB
  659. sha256->devId = devId;
  660. sha256->devCtx = NULL;
  661. #endif
  662. #ifdef WOLFSSL_SMALL_STACK_CACHE
  663. sha256->W = NULL;
  664. #endif
  665. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  666. ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
  667. WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
  668. #else
  669. (void)devId;
  670. #endif /* WOLFSSL_ASYNC_CRYPT */
  671. #ifdef WOLFSSL_IMXRT1170_CAAM
  672. ret = wc_CAAM_HashInit(&sha256->hndl, &sha256->ctx, WC_HASH_TYPE_SHA256);
  673. #endif
  674. return ret;
  675. }
  676. #endif /* End Hardware Acceleration */
  677. #ifdef NEED_SOFT_SHA256
  678. static const FLASH_QUALIFIER ALIGN32 word32 K[64] = {
  679. 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
  680. 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
  681. 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
  682. 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
  683. 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
  684. 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
  685. 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
  686. 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
  687. 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
  688. 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
  689. 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
  690. 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
  691. 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
  692. };
  693. /* Both versions of Ch and Maj are logically the same, but with the second set
  694. the compilers can recognize them better for optimization */
  695. #ifdef WOLFSSL_SHA256_BY_SPEC
  696. /* SHA256 math based on specification */
  697. #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  698. #define Maj(x,y,z) ((((x) | (y)) & (z)) | ((x) & (y)))
  699. #else
  700. /* SHA256 math reworked for easier compiler optimization */
  701. #define Ch(x,y,z) ((((y) ^ (z)) & (x)) ^ (z))
  702. #define Maj(x,y,z) ((((x) ^ (y)) & ((y) ^ (z))) ^ (y))
  703. #endif
  704. #define R(x, n) (((x) & 0xFFFFFFFFU) >> (n))
  705. #define S(x, n) rotrFixed(x, n)
  706. #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
  707. #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
  708. #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
  709. #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
  710. #define a(i) S[(0-(i)) & 7]
  711. #define b(i) S[(1-(i)) & 7]
  712. #define c(i) S[(2-(i)) & 7]
  713. #define d(i) S[(3-(i)) & 7]
  714. #define e(i) S[(4-(i)) & 7]
  715. #define f(i) S[(5-(i)) & 7]
  716. #define g(i) S[(6-(i)) & 7]
  717. #define h(i) S[(7-(i)) & 7]
  718. #ifndef XTRANSFORM
  719. #define XTRANSFORM(S, D) Transform_Sha256((S),(D))
  720. #endif
  721. #ifndef SHA256_MANY_REGISTERS
  722. #define RND(j) \
  723. t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+(j)] + W[i+(j)]; \
  724. t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
  725. d(j) += t0; \
  726. h(j) = t0 + t1
  727. static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
  728. {
  729. word32 S[8], t0, t1;
  730. int i;
  731. #ifdef WOLFSSL_SMALL_STACK_CACHE
  732. word32* W = sha256->W;
  733. if (W == NULL) {
  734. W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
  735. DYNAMIC_TYPE_DIGEST);
  736. if (W == NULL)
  737. return MEMORY_E;
  738. sha256->W = W;
  739. }
  740. #elif defined(WOLFSSL_SMALL_STACK)
  741. word32* W;
  742. W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
  743. DYNAMIC_TYPE_TMP_BUFFER);
  744. if (W == NULL)
  745. return MEMORY_E;
  746. #else
  747. word32 W[WC_SHA256_BLOCK_SIZE];
  748. #endif
  749. /* Copy context->state[] to working vars */
  750. for (i = 0; i < 8; i++)
  751. S[i] = sha256->digest[i];
  752. for (i = 0; i < 16; i++)
  753. W[i] = *((const word32*)&data[i*(int)sizeof(word32)]);
  754. for (i = 16; i < WC_SHA256_BLOCK_SIZE; i++)
  755. W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
  756. #ifdef USE_SLOW_SHA256
  757. /* not unrolled - ~2k smaller and ~25% slower */
  758. for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
  759. int j;
  760. for (j = 0; j < 8; j++) { /* braces needed here for macros {} */
  761. RND(j);
  762. }
  763. }
  764. #else
  765. /* partially loop unrolled */
  766. for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
  767. RND(0); RND(1); RND(2); RND(3);
  768. RND(4); RND(5); RND(6); RND(7);
  769. }
  770. #endif /* USE_SLOW_SHA256 */
  771. /* Add the working vars back into digest state[] */
  772. for (i = 0; i < 8; i++) {
  773. sha256->digest[i] += S[i];
  774. }
  775. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
  776. ForceZero(W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
  777. XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  778. #endif
  779. return 0;
  780. }
  781. #else
  782. /* SHA256 version that keeps all data in registers */
  783. #define SCHED1(j) (W[j] = *((word32*)&data[j*sizeof(word32)]))
  784. #define SCHED(j) ( \
  785. W[ j & 15] += \
  786. Gamma1(W[(j-2) & 15])+ \
  787. W[(j-7) & 15] + \
  788. Gamma0(W[(j-15) & 15]) \
  789. )
  790. #define RND1(j) \
  791. t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED1(j); \
  792. t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
  793. d(j) += t0; \
  794. h(j) = t0 + t1
  795. #define RNDN(j) \
  796. t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED(j); \
  797. t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
  798. d(j) += t0; \
  799. h(j) = t0 + t1
  800. static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
  801. {
  802. word32 S[8], t0, t1;
  803. int i;
  804. word32 W[WC_SHA256_BLOCK_SIZE/sizeof(word32)];
  805. /* Copy digest to working vars */
  806. S[0] = sha256->digest[0];
  807. S[1] = sha256->digest[1];
  808. S[2] = sha256->digest[2];
  809. S[3] = sha256->digest[3];
  810. S[4] = sha256->digest[4];
  811. S[5] = sha256->digest[5];
  812. S[6] = sha256->digest[6];
  813. S[7] = sha256->digest[7];
  814. i = 0;
  815. RND1( 0); RND1( 1); RND1( 2); RND1( 3);
  816. RND1( 4); RND1( 5); RND1( 6); RND1( 7);
  817. RND1( 8); RND1( 9); RND1(10); RND1(11);
  818. RND1(12); RND1(13); RND1(14); RND1(15);
  819. /* 64 operations, partially loop unrolled */
  820. for (i = 16; i < 64; i += 16) {
  821. RNDN( 0); RNDN( 1); RNDN( 2); RNDN( 3);
  822. RNDN( 4); RNDN( 5); RNDN( 6); RNDN( 7);
  823. RNDN( 8); RNDN( 9); RNDN(10); RNDN(11);
  824. RNDN(12); RNDN(13); RNDN(14); RNDN(15);
  825. }
  826. /* Add the working vars back into digest */
  827. sha256->digest[0] += S[0];
  828. sha256->digest[1] += S[1];
  829. sha256->digest[2] += S[2];
  830. sha256->digest[3] += S[3];
  831. sha256->digest[4] += S[4];
  832. sha256->digest[5] += S[5];
  833. sha256->digest[6] += S[6];
  834. sha256->digest[7] += S[7];
  835. return 0;
  836. }
  837. #endif /* SHA256_MANY_REGISTERS */
  838. #endif
  839. /* End wc_ software implementation */
  840. #ifdef XTRANSFORM
  841. static WC_INLINE void AddLength(wc_Sha256* sha256, word32 len)
  842. {
  843. word32 tmp = sha256->loLen;
  844. if ((sha256->loLen += len) < tmp) {
  845. sha256->hiLen++; /* carry low to high */
  846. }
  847. }
  848. /* do block size increments/updates */
  849. static WC_INLINE int Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  850. {
  851. int ret = 0;
  852. word32 blocksLen;
  853. byte* local;
  854. if (sha256 == NULL || (data == NULL && len > 0)) {
  855. return BAD_FUNC_ARG;
  856. }
  857. if (data == NULL && len == 0) {
  858. /* valid, but do nothing */
  859. return 0;
  860. }
  861. /* check that internal buffLen is valid */
  862. if (sha256->buffLen >= WC_SHA256_BLOCK_SIZE) {
  863. return BUFFER_E;
  864. }
  865. /* add length for final */
  866. AddLength(sha256, len);
  867. local = (byte*)sha256->buffer;
  868. /* process any remainder from previous operation */
  869. if (sha256->buffLen > 0) {
  870. blocksLen = min(len, WC_SHA256_BLOCK_SIZE - sha256->buffLen);
  871. XMEMCPY(&local[sha256->buffLen], data, blocksLen);
  872. sha256->buffLen += blocksLen;
  873. data += blocksLen;
  874. len -= blocksLen;
  875. if (sha256->buffLen == WC_SHA256_BLOCK_SIZE) {
  876. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  877. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  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. #endif
  883. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  884. #if defined(WOLFSSL_X86_64_BUILD) && \
  885. defined(USE_INTEL_SPEEDUP) && \
  886. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  887. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  888. #endif
  889. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  890. defined(CONFIG_IDF_TARGET_ESP8684) || \
  891. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  892. defined(CONFIG_IDF_TARGET_ESP32C6) \
  893. ) && \
  894. defined(WOLFSSL_ESP32_CRYPT) && \
  895. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  896. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  897. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  898. * depending on if HW is active or not. */
  899. if (esp_sha_need_byte_reversal(&sha256->ctx))
  900. #endif
  901. {
  902. ByteReverseWords(sha256->buffer, sha256->buffer,
  903. WC_SHA256_BLOCK_SIZE);
  904. }
  905. #endif
  906. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  907. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  908. if (sha256->ctx.mode == ESP32_SHA_SW) {
  909. #if defined(WOLFSSL_DEBUG_MUTEX)
  910. {
  911. ESP_LOGI(TAG, "Sha256Update process software");
  912. }
  913. #endif
  914. #ifdef WOLFSSL_HW_METRICS
  915. {
  916. /* Track of # SW during transforms during active HW */
  917. esp_sw_sha256_count_add();
  918. }
  919. #endif /* WOLFSSL_HW_METRICS */
  920. ret = XTRANSFORM(sha256, (const byte*)local);
  921. }
  922. else {
  923. #if defined(WOLFSSL_DEBUG_MUTEX)
  924. {
  925. ESP_LOGI(TAG, "Sha256Update process hardware");
  926. }
  927. #endif
  928. esp_sha256_process(sha256, (const byte*)local);
  929. }
  930. #else
  931. /* Always SW */
  932. ret = XTRANSFORM(sha256, (const byte*)local);
  933. #endif
  934. if (ret == 0)
  935. sha256->buffLen = 0;
  936. else
  937. len = 0; /* error */
  938. }
  939. }
  940. /* process blocks */
  941. #ifdef XTRANSFORM_LEN
  942. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  943. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  944. if (Transform_Sha256_Len_p != NULL)
  945. #endif
  946. {
  947. /* get number of blocks */
  948. /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
  949. /* len (masked by 0xFFFFFFC0) returns block aligned length */
  950. blocksLen = len & ~((word32)WC_SHA256_BLOCK_SIZE-1);
  951. if (blocksLen > 0) {
  952. /* Byte reversal and alignment handled in function if required */
  953. XTRANSFORM_LEN(sha256, data, blocksLen);
  954. data += blocksLen;
  955. len -= blocksLen;
  956. }
  957. }
  958. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  959. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  960. else
  961. #endif
  962. #endif /* XTRANSFORM_LEN */
  963. #if !defined(XTRANSFORM_LEN) || \
  964. (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  965. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  966. {
  967. while (len >= WC_SHA256_BLOCK_SIZE) {
  968. word32* local32 = sha256->buffer;
  969. /* optimization to avoid memcpy if data pointer is properly aligned */
  970. /* Intel transform function requires use of sha256->buffer */
  971. /* Little Endian requires byte swap, so can't use data directly */
  972. #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) && \
  973. !(defined(WOLFSSL_X86_64_BUILD) && \
  974. defined(USE_INTEL_SPEEDUP) && \
  975. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  976. if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
  977. local32 = (word32*)data;
  978. }
  979. else
  980. #endif
  981. {
  982. XMEMCPY(local32, data, WC_SHA256_BLOCK_SIZE);
  983. }
  984. data += WC_SHA256_BLOCK_SIZE;
  985. len -= WC_SHA256_BLOCK_SIZE;
  986. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  987. !defined( NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  988. if (sha256->ctx.mode == ESP32_SHA_INIT){
  989. ESP_LOGV(TAG, "Sha256Update try hardware loop");
  990. esp_sha_try_hw_lock(&sha256->ctx);
  991. }
  992. #endif
  993. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  994. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  995. defined(CONFIG_IDF_TARGET_ESP8684) || \
  996. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  997. defined(CONFIG_IDF_TARGET_ESP32C6) \
  998. ) && \
  999. defined(WOLFSSL_ESP32_CRYPT) && \
  1000. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1001. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1002. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  1003. * depending on if HW is active or not. */
  1004. if (esp_sha_need_byte_reversal(&sha256->ctx))
  1005. #endif
  1006. #if defined(WOLFSSL_X86_64_BUILD) && \
  1007. defined(USE_INTEL_SPEEDUP) && \
  1008. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1009. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  1010. #endif
  1011. {
  1012. ByteReverseWords(local32, local32, WC_SHA256_BLOCK_SIZE);
  1013. }
  1014. #endif
  1015. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1016. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1017. if (sha256->ctx.mode == ESP32_SHA_SW) {
  1018. ESP_LOGV(TAG, "Sha256Update process software loop");
  1019. ret = XTRANSFORM(sha256, (const byte*)local32);
  1020. }
  1021. else {
  1022. ESP_LOGV(TAG, "Sha256Update process hardware");
  1023. esp_sha256_process(sha256, (const byte*)local32);
  1024. }
  1025. #else
  1026. ret = XTRANSFORM(sha256, (const byte*)local32);
  1027. #endif
  1028. if (ret != 0)
  1029. break;
  1030. }
  1031. }
  1032. #endif
  1033. /* save remainder */
  1034. if (ret == 0 && len > 0) {
  1035. XMEMCPY(local, data, len);
  1036. sha256->buffLen = len;
  1037. }
  1038. return ret;
  1039. }
  1040. #if defined(WOLFSSL_KCAPI_HASH)
  1041. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1042. #else
  1043. int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
  1044. {
  1045. if (sha256 == NULL || (data == NULL && len > 0)) {
  1046. return BAD_FUNC_ARG;
  1047. }
  1048. if (data == NULL && len == 0) {
  1049. /* valid, but do nothing */
  1050. return 0;
  1051. }
  1052. #ifdef WOLF_CRYPTO_CB
  1053. #ifndef WOLF_CRYPTO_CB_FIND
  1054. if (sha256->devId != INVALID_DEVID)
  1055. #endif
  1056. {
  1057. int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
  1058. if (ret != CRYPTOCB_UNAVAILABLE)
  1059. return ret;
  1060. /* fall-through when unavailable */
  1061. }
  1062. #endif
  1063. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1064. if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
  1065. #if defined(HAVE_INTEL_QA)
  1066. return IntelQaSymSha256(&sha256->asyncDev, NULL, data, len);
  1067. #endif
  1068. }
  1069. #endif /* WOLFSSL_ASYNC_CRYPT */
  1070. return Sha256Update(sha256, data, len);
  1071. }
  1072. #endif
  1073. static WC_INLINE int Sha256Final(wc_Sha256* sha256)
  1074. {
  1075. int ret;
  1076. byte* local;
  1077. if (sha256 == NULL) {
  1078. return BAD_FUNC_ARG;
  1079. }
  1080. /* we'll add a 0x80 byte at the end,
  1081. ** so make sure we have appropriate buffer length. */
  1082. if (sha256->buffLen > WC_SHA256_BLOCK_SIZE - 1) {
  1083. /* exit with error code if there's a bad buffer size in buffLen */
  1084. return BAD_STATE_E;
  1085. } /* buffLen check */
  1086. local = (byte*)sha256->buffer;
  1087. local[sha256->buffLen++] = 0x80; /* add 1 */
  1088. /* pad with zeros */
  1089. if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
  1090. XMEMSET(&local[sha256->buffLen], 0,
  1091. WC_SHA256_BLOCK_SIZE - sha256->buffLen);
  1092. sha256->buffLen += WC_SHA256_BLOCK_SIZE - sha256->buffLen;
  1093. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1094. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1095. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  1096. esp_sha_try_hw_lock(&sha256->ctx);
  1097. }
  1098. #endif
  1099. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  1100. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1101. defined(CONFIG_IDF_TARGET_ESP8684) || \
  1102. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1103. defined(CONFIG_IDF_TARGET_ESP32C6) \
  1104. ) && \
  1105. defined(WOLFSSL_ESP32_CRYPT) && \
  1106. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1107. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1108. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  1109. * depending on if HW is active or not. */
  1110. if (esp_sha_need_byte_reversal(&sha256->ctx))
  1111. #endif
  1112. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1113. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1114. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  1115. #endif
  1116. {
  1117. ByteReverseWords(sha256->buffer, sha256->buffer,
  1118. WC_SHA256_BLOCK_SIZE);
  1119. }
  1120. #endif
  1121. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1122. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1123. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  1124. esp_sha_try_hw_lock(&sha256->ctx);
  1125. }
  1126. if (sha256->ctx.mode == ESP32_SHA_SW) {
  1127. ret = XTRANSFORM(sha256, (const byte*)local);
  1128. }
  1129. else {
  1130. ret = esp_sha256_process(sha256, (const byte*)local);
  1131. }
  1132. #else
  1133. ret = XTRANSFORM(sha256, (const byte*)local);
  1134. #endif
  1135. if (ret != 0)
  1136. return ret;
  1137. sha256->buffLen = 0;
  1138. }
  1139. XMEMSET(&local[sha256->buffLen], 0,
  1140. WC_SHA256_PAD_SIZE - sha256->buffLen);
  1141. /* put 64 bit length in separate 32 bit parts */
  1142. sha256->hiLen = (sha256->loLen >> (8 * sizeof(sha256->loLen) - 3)) +
  1143. (sha256->hiLen << 3);
  1144. sha256->loLen = sha256->loLen << 3;
  1145. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1146. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1147. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  1148. esp_sha_try_hw_lock(&sha256->ctx);
  1149. }
  1150. #endif
  1151. /* store lengths */
  1152. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  1153. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1154. defined(CONFIG_IDF_TARGET_ESP8684) || \
  1155. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1156. defined(CONFIG_IDF_TARGET_ESP32C6) \
  1157. ) && \
  1158. defined(WOLFSSL_ESP32_CRYPT) && \
  1159. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1160. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1161. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  1162. * depending on if HW is active or not. */
  1163. if (esp_sha_need_byte_reversal(&sha256->ctx))
  1164. #endif
  1165. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1166. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1167. if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
  1168. #endif
  1169. {
  1170. ByteReverseWords(sha256->buffer, sha256->buffer,
  1171. WC_SHA256_BLOCK_SIZE);
  1172. }
  1173. #endif
  1174. /* ! 64-bit length ordering dependent on digest endian type ! */
  1175. XMEMCPY(&local[WC_SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
  1176. XMEMCPY(&local[WC_SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
  1177. sizeof(word32));
  1178. /* Only the ESP32-C3 with HW enabled may need pad size byte order reversal
  1179. * depending on HW or SW mode */
  1180. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1181. defined(CONFIG_IDF_TARGET_ESP8684) || \
  1182. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1183. defined(CONFIG_IDF_TARGET_ESP32C6) \
  1184. ) && \
  1185. defined(WOLFSSL_ESP32_CRYPT) && \
  1186. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1187. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1188. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  1189. * depending on if HW is active or not. */
  1190. if (sha256->ctx.mode == ESP32_SHA_HW) {
  1191. #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
  1192. ESP_LOGV(TAG, "Start: Reverse PAD SIZE Endianness.");
  1193. #endif
  1194. ByteReverseWords(
  1195. &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)], /* out */
  1196. &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)], /* in */
  1197. 2 * sizeof(word32) /* byte count to reverse */
  1198. );
  1199. #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
  1200. ESP_LOGV(TAG, "End: Reverse PAD SIZE Endianness.");
  1201. #endif
  1202. } /* end if (sha256->ctx.mode == ESP32_SHA_HW) */
  1203. #endif
  1204. #if defined(FREESCALE_MMCAU_SHA) || \
  1205. (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1206. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
  1207. /* Kinetis requires only these bytes reversed */
  1208. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1209. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1210. if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
  1211. #endif
  1212. {
  1213. ByteReverseWords(
  1214. &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
  1215. &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
  1216. 2 * sizeof(word32));
  1217. }
  1218. #endif
  1219. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1220. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1221. if (sha256->ctx.mode == ESP32_SHA_INIT) {
  1222. esp_sha_try_hw_lock(&sha256->ctx);
  1223. }
  1224. /* depending on architecture and ctx.mode value
  1225. * we may or may not need default digest */
  1226. if (sha256->ctx.mode == ESP32_SHA_SW) {
  1227. ret = XTRANSFORM(sha256, (const byte*)local);
  1228. }
  1229. else {
  1230. ret = esp_sha256_digest_process(sha256, 1);
  1231. }
  1232. #else
  1233. ret = XTRANSFORM(sha256, (const byte*)local);
  1234. #endif
  1235. return ret;
  1236. }
  1237. #if !defined(WOLFSSL_KCAPI_HASH)
  1238. int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
  1239. {
  1240. #ifdef LITTLE_ENDIAN_ORDER
  1241. word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
  1242. #endif
  1243. if (sha256 == NULL || hash == NULL) {
  1244. return BAD_FUNC_ARG;
  1245. }
  1246. #ifdef LITTLE_ENDIAN_ORDER
  1247. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1248. defined(CONFIG_IDF_TARGET_ESP8684) || \
  1249. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1250. defined(CONFIG_IDF_TARGET_ESP32C6) \
  1251. ) && \
  1252. defined(WOLFSSL_ESP32_CRYPT) && \
  1253. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1254. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1255. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  1256. * depending on if HW is active or not. */
  1257. if (esp_sha_need_byte_reversal(&sha256->ctx))
  1258. #endif
  1259. {
  1260. ByteReverseWords((word32*)digest,
  1261. (word32*)sha256->digest,
  1262. WC_SHA256_DIGEST_SIZE);
  1263. }
  1264. XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
  1265. #else
  1266. XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
  1267. #endif
  1268. return 0;
  1269. }
  1270. int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
  1271. {
  1272. int ret;
  1273. if (sha256 == NULL || hash == NULL) {
  1274. return BAD_FUNC_ARG;
  1275. }
  1276. #ifdef WOLF_CRYPTO_CB
  1277. #ifndef WOLF_CRYPTO_CB_FIND
  1278. if (sha256->devId != INVALID_DEVID)
  1279. #endif
  1280. {
  1281. ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
  1282. if (ret != CRYPTOCB_UNAVAILABLE)
  1283. return ret;
  1284. /* fall-through when unavailable */
  1285. }
  1286. #endif
  1287. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1288. if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
  1289. #if defined(HAVE_INTEL_QA)
  1290. return IntelQaSymSha256(&sha256->asyncDev, hash, NULL,
  1291. WC_SHA256_DIGEST_SIZE);
  1292. #endif
  1293. }
  1294. #endif /* WOLFSSL_ASYNC_CRYPT */
  1295. ret = Sha256Final(sha256);
  1296. if (ret != 0) {
  1297. return ret;
  1298. }
  1299. #if defined(LITTLE_ENDIAN_ORDER)
  1300. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1301. defined(CONFIG_IDF_TARGET_ESP8684) || \
  1302. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1303. defined(CONFIG_IDF_TARGET_ESP32C6) \
  1304. ) && \
  1305. defined(WOLFSSL_ESP32_CRYPT) && \
  1306. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1307. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1308. /* For Espressif RISC-V Targets, we *may* need to reverse bytes
  1309. * depending on if HW is active or not. */
  1310. if (esp_sha_need_byte_reversal(&sha256->ctx))
  1311. #endif
  1312. {
  1313. ByteReverseWords(sha256->digest, sha256->digest,
  1314. WC_SHA256_DIGEST_SIZE);
  1315. }
  1316. #endif
  1317. XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
  1318. return InitSha256(sha256); /* reset state */
  1319. }
  1320. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  1321. /* Apply SHA256 transformation to the data */
  1322. /* @param sha a pointer to wc_Sha256 structure */
  1323. /* @param data data to be applied SHA256 transformation */
  1324. /* @return 0 on successful, otherwise non-zero on failure */
  1325. int wc_Sha256Transform(wc_Sha256* sha, const unsigned char* data)
  1326. {
  1327. if (sha == NULL || data == NULL) {
  1328. return BAD_FUNC_ARG;
  1329. }
  1330. return (Transform_Sha256(sha, data));
  1331. }
  1332. #endif
  1333. #endif /* OPENSSL_EXTRA */
  1334. #endif /* !WOLFSSL_KCAPI_HASH */
  1335. #ifdef WOLFSSL_SHA224
  1336. #ifdef STM32_HASH_SHA2
  1337. /* Supports CubeMX HAL or Standard Peripheral Library */
  1338. int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
  1339. {
  1340. if (sha224 == NULL)
  1341. return BAD_FUNC_ARG;
  1342. (void)devId;
  1343. (void)heap;
  1344. XMEMSET(sha224, 0, sizeof(wc_Sha224));
  1345. wc_Stm32_Hash_Init(&sha224->stmCtx);
  1346. return 0;
  1347. }
  1348. int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
  1349. {
  1350. int ret = 0;
  1351. if (sha224 == NULL || (data == NULL && len > 0)) {
  1352. return BAD_FUNC_ARG;
  1353. }
  1354. ret = wolfSSL_CryptHwMutexLock();
  1355. if (ret == 0) {
  1356. ret = wc_Stm32_Hash_Update(&sha224->stmCtx,
  1357. HASH_AlgoSelection_SHA224, data, len, WC_SHA224_BLOCK_SIZE);
  1358. wolfSSL_CryptHwMutexUnLock();
  1359. }
  1360. return ret;
  1361. }
  1362. int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
  1363. {
  1364. int ret = 0;
  1365. if (sha224 == NULL || hash == NULL) {
  1366. return BAD_FUNC_ARG;
  1367. }
  1368. ret = wolfSSL_CryptHwMutexLock();
  1369. if (ret == 0) {
  1370. ret = wc_Stm32_Hash_Final(&sha224->stmCtx,
  1371. HASH_AlgoSelection_SHA224, hash, WC_SHA224_DIGEST_SIZE);
  1372. wolfSSL_CryptHwMutexUnLock();
  1373. }
  1374. (void)wc_InitSha224(sha224); /* reset state */
  1375. return ret;
  1376. }
  1377. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1378. int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
  1379. {
  1380. if (sha224 == NULL) {
  1381. return BAD_FUNC_ARG;
  1382. }
  1383. (void)devId;
  1384. return se050_hash_init(&sha224->se050Ctx, heap);
  1385. }
  1386. int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
  1387. {
  1388. return se050_hash_update(&sha224->se050Ctx, data, len);
  1389. }
  1390. int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
  1391. {
  1392. int ret = 0;
  1393. ret = se050_hash_final(&sha224->se050Ctx, hash, WC_SHA224_DIGEST_SIZE,
  1394. kAlgorithm_SSS_SHA224);
  1395. (void)wc_InitSha224(sha224);
  1396. return ret;
  1397. }
  1398. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  1399. !defined(WOLFSSL_QNX_CAAM)
  1400. /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
  1401. #elif defined(WOLFSSL_AFALG_HASH)
  1402. #error SHA224 currently not supported with AF_ALG enabled
  1403. #elif defined(WOLFSSL_DEVCRYPTO_HASH)
  1404. /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
  1405. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  1406. /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
  1407. #elif defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
  1408. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1409. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  1410. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1411. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  1412. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  1413. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  1414. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1415. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1416. #else
  1417. #define NEED_SOFT_SHA224
  1418. static int InitSha224(wc_Sha224* sha224)
  1419. {
  1420. int ret = 0;
  1421. if (sha224 == NULL) {
  1422. return BAD_FUNC_ARG;
  1423. }
  1424. sha224->digest[0] = 0xc1059ed8;
  1425. sha224->digest[1] = 0x367cd507;
  1426. sha224->digest[2] = 0x3070dd17;
  1427. sha224->digest[3] = 0xf70e5939;
  1428. sha224->digest[4] = 0xffc00b31;
  1429. sha224->digest[5] = 0x68581511;
  1430. sha224->digest[6] = 0x64f98fa7;
  1431. sha224->digest[7] = 0xbefa4fa4;
  1432. sha224->buffLen = 0;
  1433. sha224->loLen = 0;
  1434. sha224->hiLen = 0;
  1435. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
  1436. (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
  1437. /* choose best Transform function under this runtime environment */
  1438. Sha256_SetTransform();
  1439. #endif
  1440. #ifdef WOLFSSL_HASH_FLAGS
  1441. sha224->flags = 0;
  1442. #endif
  1443. #ifdef WOLFSSL_HASH_KEEP
  1444. sha224->msg = NULL;
  1445. sha224->len = 0;
  1446. sha224->used = 0;
  1447. #endif
  1448. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1449. (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  1450. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
  1451. /* not to be confused with SHAS512_224 */
  1452. ret = esp_sha_init(&(sha224->ctx), WC_HASH_TYPE_SHA224);
  1453. #endif
  1454. return ret;
  1455. }
  1456. #endif
  1457. #ifdef NEED_SOFT_SHA224
  1458. int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
  1459. {
  1460. int ret = 0;
  1461. if (sha224 == NULL)
  1462. return BAD_FUNC_ARG;
  1463. sha224->heap = heap;
  1464. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1465. sha224->W = NULL;
  1466. #endif
  1467. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  1468. #if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224)
  1469. /* We know this is a fresh, uninitialized item, so set to INIT */
  1470. if (sha224->ctx.mode != ESP32_SHA_SW) {
  1471. ESP_LOGV(TAG, "Set sha224 ctx mode init to ESP32_SHA_SW. "
  1472. "Prior value: %d", sha224->ctx.mode);
  1473. }
  1474. /* no sha224 HW support is available, set to SW */
  1475. sha224->ctx.mode = ESP32_SHA_SW;
  1476. #else
  1477. /* We know this is a fresh, uninitialized item, so set to INIT */
  1478. sha224->ctx.mode = ESP32_SHA_INIT;
  1479. #endif
  1480. #endif
  1481. ret = InitSha224(sha224);
  1482. if (ret != 0) {
  1483. return ret;
  1484. }
  1485. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1486. ret = wolfAsync_DevCtxInit(&sha224->asyncDev,
  1487. WOLFSSL_ASYNC_MARKER_SHA224, sha224->heap, devId);
  1488. #else
  1489. (void)devId;
  1490. #endif /* WOLFSSL_ASYNC_CRYPT */
  1491. #ifdef WOLFSSL_IMXRT1170_CAAM
  1492. ret = wc_CAAM_HashInit(&sha224->hndl, &sha224->ctx, WC_HASH_TYPE_SHA224);
  1493. #endif
  1494. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1495. (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  1496. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
  1497. if (sha224->ctx.mode != ESP32_SHA_INIT) {
  1498. ESP_LOGV("SHA224", "Set ctx mode from prior value: "
  1499. "%d", sha224->ctx.mode);
  1500. }
  1501. /* We know this is a fresh, uninitialized item, so set to INIT */
  1502. sha224->ctx.mode = ESP32_SHA_INIT;
  1503. #endif
  1504. return ret;
  1505. }
  1506. int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
  1507. {
  1508. int ret;
  1509. if (sha224 == NULL || (data == NULL && len > 0)) {
  1510. return BAD_FUNC_ARG;
  1511. }
  1512. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1513. if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
  1514. #if defined(HAVE_INTEL_QA)
  1515. return IntelQaSymSha224(&sha224->asyncDev, NULL, data, len);
  1516. #endif
  1517. }
  1518. #endif /* WOLFSSL_ASYNC_CRYPT */
  1519. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1520. (defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  1521. defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
  1522. sha224->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
  1523. #endif
  1524. ret = Sha256Update((wc_Sha256*)sha224, data, len);
  1525. return ret;
  1526. }
  1527. int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
  1528. {
  1529. int ret;
  1530. if (sha224 == NULL || hash == NULL) {
  1531. return BAD_FUNC_ARG;
  1532. }
  1533. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1534. if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
  1535. #if defined(HAVE_INTEL_QA)
  1536. return IntelQaSymSha224(&sha224->asyncDev, hash, NULL,
  1537. WC_SHA224_DIGEST_SIZE);
  1538. #endif
  1539. }
  1540. #endif /* WOLFSSL_ASYNC_CRYPT */
  1541. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1542. ( !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  1543. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) )
  1544. /* nothing enabled here for RISC-V C2/C3/C6 success */
  1545. #endif
  1546. ret = Sha256Final((wc_Sha256*)sha224);
  1547. if (ret != 0)
  1548. return ret;
  1549. #if defined(LITTLE_ENDIAN_ORDER)
  1550. #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1551. defined(CONFIG_IDF_TARGET_ESP8684) || \
  1552. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1553. defined(CONFIG_IDF_TARGET_ESP32C6) \
  1554. ) && \
  1555. defined(WOLFSSL_ESP32_CRYPT) && \
  1556. (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  1557. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) \
  1558. )
  1559. if (esp_sha_need_byte_reversal(&sha224->ctx))
  1560. #endif
  1561. {
  1562. ByteReverseWords(sha224->digest,
  1563. sha224->digest,
  1564. WC_SHA224_DIGEST_SIZE);
  1565. }
  1566. #endif
  1567. XMEMCPY(hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
  1568. return InitSha224(sha224); /* reset state */
  1569. }
  1570. #endif /* end of SHA224 software implementation */
  1571. int wc_InitSha224(wc_Sha224* sha224)
  1572. {
  1573. int devId = INVALID_DEVID;
  1574. #ifdef WOLF_CRYPTO_CB
  1575. devId = wc_CryptoCb_DefaultDevID();
  1576. #endif
  1577. return wc_InitSha224_ex(sha224, NULL, devId);
  1578. }
  1579. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
  1580. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1581. void wc_Sha224Free(wc_Sha224* sha224)
  1582. {
  1583. if (sha224 == NULL)
  1584. return;
  1585. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1586. if (sha224->W != NULL) {
  1587. ForceZero(sha224->W, sizeof(word32) * WC_SHA224_BLOCK_SIZE);
  1588. XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
  1589. sha224->W = NULL;
  1590. }
  1591. #endif
  1592. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1593. wolfAsync_DevCtxFree(&sha224->asyncDev, WOLFSSL_ASYNC_MARKER_SHA224);
  1594. #endif /* WOLFSSL_ASYNC_CRYPT */
  1595. #ifdef WOLFSSL_PIC32MZ_HASH
  1596. wc_Sha256Pic32Free(sha224);
  1597. #endif
  1598. #if defined(WOLFSSL_KCAPI_HASH)
  1599. KcapiHashFree(&sha224->kcapi);
  1600. #endif
  1601. #if defined(WOLFSSL_RENESAS_RX64_HASH)
  1602. if (sha224->msg != NULL) {
  1603. ForceZero(sha224->msg, sha224->len);
  1604. XFREE(sha224->msg, sha224->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1605. sha224->msg = NULL;
  1606. }
  1607. #endif
  1608. ForceZero(sha224, sizeof(*sha224));
  1609. }
  1610. #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
  1611. #endif /* WOLFSSL_SHA224 */
  1612. int wc_InitSha256(wc_Sha256* sha256)
  1613. {
  1614. int devId = INVALID_DEVID;
  1615. #ifdef WOLF_CRYPTO_CB
  1616. devId = wc_CryptoCb_DefaultDevID();
  1617. #endif
  1618. return wc_InitSha256_ex(sha256, NULL, devId);
  1619. }
  1620. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
  1621. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1622. void wc_Sha256Free(wc_Sha256* sha256)
  1623. {
  1624. if (sha256 == NULL)
  1625. return;
  1626. #if defined(WOLFSSL_ESP32) && \
  1627. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1628. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1629. esp_sha_release_unfinished_lock(&sha256->ctx);
  1630. #endif
  1631. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1632. if (sha256->W != NULL) {
  1633. ForceZero(sha256->W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
  1634. XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST);
  1635. sha256->W = NULL;
  1636. }
  1637. #endif
  1638. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1639. wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256);
  1640. #endif /* WOLFSSL_ASYNC_CRYPT */
  1641. #ifdef WOLFSSL_PIC32MZ_HASH
  1642. wc_Sha256Pic32Free(sha256);
  1643. #endif
  1644. #if defined(WOLFSSL_AFALG_HASH)
  1645. if (sha256->alFd > 0) {
  1646. close(sha256->alFd);
  1647. sha256->alFd = -1; /* avoid possible double close on socket */
  1648. }
  1649. if (sha256->rdFd > 0) {
  1650. close(sha256->rdFd);
  1651. sha256->rdFd = -1; /* avoid possible double close on socket */
  1652. }
  1653. #endif /* WOLFSSL_AFALG_HASH */
  1654. #ifdef WOLFSSL_DEVCRYPTO_HASH
  1655. wc_DevCryptoFree(&sha256->ctx);
  1656. #endif /* WOLFSSL_DEVCRYPTO */
  1657. #if (defined(WOLFSSL_AFALG_HASH) && defined(WOLFSSL_AFALG_HASH_KEEP)) || \
  1658. (defined(WOLFSSL_DEVCRYPTO_HASH) && defined(WOLFSSL_DEVCRYPTO_HASH_KEEP)) || \
  1659. ((defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  1660. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  1661. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) || \
  1662. (defined(WOLFSSL_RENESAS_SCEPROTECT) && \
  1663. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) || \
  1664. defined(WOLFSSL_RENESAS_RX64_HASH) || \
  1665. defined(WOLFSSL_HASH_KEEP)
  1666. if (sha256->msg != NULL) {
  1667. ForceZero(sha256->msg, sha256->len);
  1668. XFREE(sha256->msg, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1669. sha256->msg = NULL;
  1670. }
  1671. #endif
  1672. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  1673. se050_hash_free(&sha256->se050Ctx);
  1674. #endif
  1675. #if defined(WOLFSSL_KCAPI_HASH)
  1676. KcapiHashFree(&sha256->kcapi);
  1677. #endif
  1678. #ifdef WOLFSSL_IMXRT_DCP
  1679. DCPSha256Free(sha256);
  1680. #endif
  1681. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  1682. wc_MAXQ10XX_Sha256Free(sha256);
  1683. #endif
  1684. #ifdef HAVE_ARIA
  1685. if (sha256->hSession != NULL) {
  1686. MC_CloseSession(sha256->hSession);
  1687. sha256->hSession = NULL;
  1688. }
  1689. #endif
  1690. /* Espressif embedded hardware acceleration specific: */
  1691. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1692. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  1693. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1694. if (sha256->ctx.lockDepth > 0) {
  1695. /* probably due to unclean shutdown, error, or other problem.
  1696. *
  1697. * if you find yourself here, code needs to be cleaned up to
  1698. * properly release hardware. this init is only for handling
  1699. * the unexpected. by the time free is called, the hardware
  1700. * should have already been released (lockDepth = 0)
  1701. */
  1702. (void)InitSha256(sha256); /* unlock mutex, set mode to ESP32_SHA_INIT */
  1703. ESP_LOGV(TAG, "Alert: hardware unlock needed in wc_Sha256Free.");
  1704. }
  1705. else {
  1706. ESP_LOGV(TAG, "Hardware unlock not needed in wc_Sha256Free.");
  1707. }
  1708. #endif
  1709. ForceZero(sha256, sizeof(*sha256));
  1710. } /* wc_Sha256Free */
  1711. #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
  1712. #ifdef WOLFSSL_HASH_KEEP
  1713. /* Some hardware have issues with update, this function stores the data to be
  1714. * hashed into an array. Once ready, the Final operation is called on all of the
  1715. * data to be hashed at once.
  1716. * returns 0 on success
  1717. */
  1718. int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz)
  1719. {
  1720. return _wc_Hash_Grow(&(sha256->msg), &(sha256->used), &(sha256->len), in,
  1721. inSz, sha256->heap);
  1722. }
  1723. #ifdef WOLFSSL_SHA224
  1724. int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
  1725. {
  1726. return _wc_Hash_Grow(&(sha224->msg), &(sha224->used), &(sha224->len), in,
  1727. inSz, sha224->heap);
  1728. }
  1729. #endif /* WOLFSSL_SHA224 */
  1730. #endif /* WOLFSSL_HASH_KEEP */
  1731. #endif /* !WOLFSSL_TI_HASH */
  1732. #ifndef WOLFSSL_TI_HASH
  1733. #if !defined(WOLFSSL_RENESAS_RX64_HASH) && \
  1734. (!defined(WOLFSSL_RENESAS_RSIP) || \
  1735. defined(NO_WOLFSSL_RENESAS_FSPSM_HASH))
  1736. #ifdef WOLFSSL_SHA224
  1737. #if defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
  1738. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1739. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  1740. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1741. #else
  1742. int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash)
  1743. {
  1744. int ret;
  1745. #ifdef WOLFSSL_SMALL_STACK
  1746. wc_Sha224* tmpSha224;
  1747. #else
  1748. wc_Sha224 tmpSha224[1];
  1749. #endif
  1750. if (sha224 == NULL || hash == NULL) {
  1751. return BAD_FUNC_ARG;
  1752. }
  1753. #ifdef WOLFSSL_SMALL_STACK
  1754. tmpSha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
  1755. DYNAMIC_TYPE_TMP_BUFFER);
  1756. if (tmpSha224 == NULL) {
  1757. return MEMORY_E;
  1758. }
  1759. #endif
  1760. ret = wc_Sha224Copy(sha224, tmpSha224);
  1761. if (ret == 0) {
  1762. ret = wc_Sha224Final(tmpSha224, hash);
  1763. wc_Sha224Free(tmpSha224);
  1764. }
  1765. #ifdef WOLFSSL_SMALL_STACK
  1766. XFREE(tmpSha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1767. #endif
  1768. return ret;
  1769. }
  1770. int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
  1771. {
  1772. int ret = 0; /* assume success unless proven otherwise */
  1773. if (src == NULL || dst == NULL) {
  1774. return BAD_FUNC_ARG;
  1775. }
  1776. XMEMCPY(dst, src, sizeof(wc_Sha224));
  1777. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1778. dst->W = NULL;
  1779. #endif
  1780. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
  1781. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  1782. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  1783. #endif
  1784. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1785. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  1786. #endif
  1787. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1788. (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  1789. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
  1790. /* regardless of any other settings, there's no SHA-224 HW on ESP32 */
  1791. #ifndef CONFIG_IDF_TARGET_ESP32
  1792. ret = esp_sha224_ctx_copy(src, dst);
  1793. #endif
  1794. #endif
  1795. #ifdef WOLFSSL_HASH_FLAGS
  1796. dst->flags |= WC_HASH_FLAG_ISCOPY;
  1797. #endif
  1798. #if defined(WOLFSSL_HASH_KEEP)
  1799. if (src->msg != NULL) {
  1800. dst->msg = (byte*)XMALLOC(src->len, dst->heap,
  1801. DYNAMIC_TYPE_TMP_BUFFER);
  1802. if (dst->msg == NULL)
  1803. return MEMORY_E;
  1804. XMEMCPY(dst->msg, src->msg, src->len);
  1805. }
  1806. #endif
  1807. return ret;
  1808. }
  1809. #endif /* WOLFSSL_KCAPI_HASH && !WOLFSSL_NO_KCAPI_SHA224 */
  1810. #ifdef WOLFSSL_HASH_FLAGS
  1811. int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags)
  1812. {
  1813. if (sha224) {
  1814. sha224->flags = flags;
  1815. }
  1816. return 0;
  1817. }
  1818. int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags)
  1819. {
  1820. if (sha224 && flags) {
  1821. *flags = sha224->flags;
  1822. }
  1823. return 0;
  1824. }
  1825. #endif
  1826. #endif /* WOLFSSL_SHA224 */
  1827. #endif /* WOLFSSL_RENESAS_RX64_HASH */
  1828. #ifdef WOLFSSL_AFALG_HASH
  1829. /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
  1830. #elif defined(WOLFSSL_DEVCRYPTO_HASH)
  1831. /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
  1832. #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  1833. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  1834. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  1835. /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
  1836. #elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP))\
  1837. && !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  1838. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  1839. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  1840. /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
  1841. #elif defined(WOLFSSL_IMXRT_DCP)
  1842. /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
  1843. #elif defined(WOLFSSL_KCAPI_HASH)
  1844. /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
  1845. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  1846. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  1847. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  1848. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  1849. #else
  1850. int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
  1851. {
  1852. int ret;
  1853. #ifdef WOLFSSL_SMALL_STACK
  1854. wc_Sha256* tmpSha256;
  1855. #else
  1856. wc_Sha256 tmpSha256[1];
  1857. #endif
  1858. if (sha256 == NULL || hash == NULL) {
  1859. return BAD_FUNC_ARG;
  1860. }
  1861. #ifdef WOLFSSL_SMALL_STACK
  1862. tmpSha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
  1863. DYNAMIC_TYPE_TMP_BUFFER);
  1864. if (tmpSha256 == NULL) {
  1865. return MEMORY_E;
  1866. }
  1867. #endif
  1868. ret = wc_Sha256Copy(sha256, tmpSha256);
  1869. if (ret == 0) {
  1870. ret = wc_Sha256Final(tmpSha256, hash);
  1871. wc_Sha256Free(tmpSha256); /* TODO move outside brackets? */
  1872. }
  1873. #ifdef WOLFSSL_SMALL_STACK
  1874. XFREE(tmpSha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1875. #endif
  1876. return ret;
  1877. }
  1878. int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
  1879. {
  1880. int ret = 0;
  1881. if (src == NULL || dst == NULL) {
  1882. return BAD_FUNC_ARG;
  1883. }
  1884. XMEMCPY(dst, src, sizeof(wc_Sha256));
  1885. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  1886. wc_MAXQ10XX_Sha256Copy(src);
  1887. #endif
  1888. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1889. dst->W = NULL;
  1890. #endif
  1891. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
  1892. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  1893. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  1894. #endif
  1895. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1896. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  1897. #endif
  1898. #ifdef WOLFSSL_PIC32MZ_HASH
  1899. ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
  1900. #endif
  1901. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
  1902. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
  1903. esp_sha256_ctx_copy(src, dst);
  1904. #endif
  1905. #ifdef HAVE_ARIA
  1906. dst->hSession = NULL;
  1907. if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
  1908. return MEMORY_E;
  1909. }
  1910. #endif
  1911. #ifdef WOLFSSL_HASH_FLAGS
  1912. dst->flags |= WC_HASH_FLAG_ISCOPY;
  1913. #endif
  1914. #if defined(WOLFSSL_HASH_KEEP)
  1915. if (src->msg != NULL) {
  1916. dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1917. if (dst->msg == NULL)
  1918. return MEMORY_E;
  1919. XMEMCPY(dst->msg, src->msg, src->len);
  1920. }
  1921. #endif
  1922. return ret;
  1923. }
  1924. #endif
  1925. #ifdef WOLFSSL_HASH_FLAGS
  1926. int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags)
  1927. {
  1928. if (sha256) {
  1929. sha256->flags = flags;
  1930. }
  1931. return 0;
  1932. }
  1933. int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags)
  1934. {
  1935. if (sha256 && flags) {
  1936. *flags = sha256->flags;
  1937. }
  1938. return 0;
  1939. }
  1940. #endif
  1941. #endif /* !WOLFSSL_TI_HASH */
  1942. #endif /* NO_SHA256 */