2
0

sha256.c 60 KB

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