sha256.c 61 KB

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