sha.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /* sha.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef DEBUG_WOLFSSL_VERBOSE
  26. #if defined(WOLFSSL_ESPIDF)
  27. #include <esp_log.h>
  28. #else
  29. #include <wolfssl/wolfcrypt/logging.h>
  30. #endif
  31. #endif
  32. #if !defined(NO_SHA)
  33. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  34. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  35. #define FIPS_NO_WRAPPERS
  36. #ifdef USE_WINDOWS_API
  37. #pragma code_seg(".fipsA$j")
  38. #pragma const_seg(".fipsB$j")
  39. #endif
  40. #endif
  41. #include <wolfssl/wolfcrypt/sha.h>
  42. #include <wolfssl/wolfcrypt/error-crypt.h>
  43. #include <wolfssl/wolfcrypt/hash.h>
  44. #ifdef WOLF_CRYPTO_CB
  45. #include <wolfssl/wolfcrypt/cryptocb.h>
  46. #endif
  47. #ifdef WOLFSSL_IMXRT1170_CAAM
  48. #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
  49. #endif
  50. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  51. #if defined(WOLFSSL_ESP32_CRYPT) && \
  52. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
  53. /* define a single keyword for simplicity & readability
  54. *
  55. * by default the HW acceleration is on for ESP32-WROOM32
  56. * but individual components can be turned off.
  57. */
  58. #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  59. #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
  60. /* Although we have hardware acceleration,
  61. ** we may need to fall back to software */
  62. #define USE_SHA_SOFTWARE_IMPL
  63. #elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
  64. /* The ESP32C3 is different; HW crypto here. Not yet implemented.
  65. ** We'll be using software for RISC-V at this time */
  66. #else
  67. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  68. #endif
  69. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  70. #if defined(WOLFSSL_ESP32_CRYPT) && \
  71. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
  72. /* define a single keyword for simplicity & readability
  73. *
  74. * by default the HW acceleration is on for ESP32-WROOM32
  75. * but individual components can be turned off.
  76. */
  77. #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  78. #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
  79. /* Although we have hardware acceleration,
  80. ** we may need to fall back to software */
  81. #define USE_SHA_SOFTWARE_IMPL
  82. static const char* TAG = "wc_sha";
  83. #elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
  84. /* The ESP32C3 is different; HW crypto here. Not yet implemented.
  85. ** We'll be using software for RISC-V at this time */
  86. static const char* TAG = "wc_sha-c3";
  87. #else
  88. #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  89. #endif
  90. #if defined(WOLFSSL_TI_HASH)
  91. /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
  92. #else
  93. #include <wolfssl/wolfcrypt/logging.h>
  94. #ifdef NO_INLINE
  95. #include <wolfssl/wolfcrypt/misc.h>
  96. #else
  97. #define WOLFSSL_MISC_INCLUDED
  98. #include <wolfcrypt/src/misc.c>
  99. #endif
  100. /* Hardware Acceleration */
  101. #if defined(WOLFSSL_PIC32MZ_HASH)
  102. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  103. #elif defined(STM32_HASH)
  104. /* Supports CubeMX HAL or Standard Peripheral Library */
  105. int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
  106. {
  107. if (sha == NULL) {
  108. return BAD_FUNC_ARG;
  109. }
  110. (void)devId;
  111. (void)heap;
  112. wc_Stm32_Hash_Init(&sha->stmCtx);
  113. return 0;
  114. }
  115. int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
  116. {
  117. int ret;
  118. if (sha == NULL || (data == NULL && len > 0)) {
  119. return BAD_FUNC_ARG;
  120. }
  121. ret = wolfSSL_CryptHwMutexLock();
  122. if (ret == 0) {
  123. ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1,
  124. data, len, WC_SHA_BLOCK_SIZE);
  125. wolfSSL_CryptHwMutexUnLock();
  126. }
  127. return ret;
  128. }
  129. int wc_ShaFinal(wc_Sha* sha, byte* hash)
  130. {
  131. int ret;
  132. if (sha == NULL || hash == NULL) {
  133. return BAD_FUNC_ARG;
  134. }
  135. ret = wolfSSL_CryptHwMutexLock();
  136. if (ret == 0) {
  137. ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1,
  138. hash, WC_SHA_DIGEST_SIZE);
  139. wolfSSL_CryptHwMutexUnLock();
  140. }
  141. (void)wc_InitSha(sha); /* reset state */
  142. return ret;
  143. }
  144. #elif defined(FREESCALE_LTC_SHA)
  145. #include "fsl_ltc.h"
  146. int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
  147. {
  148. if (sha == NULL) {
  149. return BAD_FUNC_ARG;
  150. }
  151. (void)devId;
  152. (void)heap;
  153. LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0);
  154. return 0;
  155. }
  156. int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
  157. {
  158. LTC_HASH_Update(&sha->ctx, data, len);
  159. return 0;
  160. }
  161. int wc_ShaFinal(wc_Sha* sha, byte* hash)
  162. {
  163. word32 hashlen = WC_SHA_DIGEST_SIZE;
  164. LTC_HASH_Finish(&sha->ctx, hash, &hashlen);
  165. return wc_InitSha(sha); /* reset state */
  166. }
  167. #elif defined(FREESCALE_MMCAU_SHA)
  168. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  169. #include "cau_api.h"
  170. #else
  171. #include "fsl_mmcau.h"
  172. #endif
  173. #define USE_SHA_SOFTWARE_IMPL /* Only for API's, actual transform is here */
  174. #define XTRANSFORM(S,B) Transform((S),(B))
  175. #define XTRANSFORM_LEN(S,B,L) Transform_Len((S),(B),(L))
  176. #ifndef WC_HASH_DATA_ALIGNMENT
  177. /* these hardware API's require 4 byte (word32) alignment */
  178. #define WC_HASH_DATA_ALIGNMENT 4
  179. #endif
  180. static int InitSha(wc_Sha* sha)
  181. {
  182. int ret = 0;
  183. ret = wolfSSL_CryptHwMutexLock();
  184. if (ret != 0) {
  185. return ret;
  186. }
  187. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  188. cau_sha1_initialize_output(sha->digest);
  189. #else
  190. MMCAU_SHA1_InitializeOutput((word32*)sha->digest);
  191. #endif
  192. wolfSSL_CryptHwMutexUnLock();
  193. sha->buffLen = 0;
  194. sha->loLen = 0;
  195. sha->hiLen = 0;
  196. return ret;
  197. }
  198. static int Transform(wc_Sha* sha, const byte* data)
  199. {
  200. int ret = wolfSSL_CryptHwMutexLock();
  201. if (ret == 0) {
  202. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  203. cau_sha1_hash_n((byte*)data, 1, sha->digest);
  204. #else
  205. MMCAU_SHA1_HashN((byte*)data, 1, (word32*)sha->digest);
  206. #endif
  207. wolfSSL_CryptHwMutexUnLock();
  208. }
  209. return ret;
  210. }
  211. static int Transform_Len(wc_Sha* sha, const byte* data, word32 len)
  212. {
  213. int ret = wolfSSL_CryptHwMutexLock();
  214. if (ret == 0) {
  215. #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
  216. if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
  217. /* data pointer is NOT aligned,
  218. * so copy and perform one block at a time */
  219. byte* local = (byte*)sha->buffer;
  220. while (len >= WC_SHA_BLOCK_SIZE) {
  221. XMEMCPY(local, data, WC_SHA_BLOCK_SIZE);
  222. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  223. cau_sha1_hash_n(local, 1, sha->digest);
  224. #else
  225. MMCAU_SHA1_HashN(local, 1, sha->digest);
  226. #endif
  227. data += WC_SHA_BLOCK_SIZE;
  228. len -= WC_SHA_BLOCK_SIZE;
  229. }
  230. }
  231. else
  232. #endif
  233. {
  234. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  235. cau_sha1_hash_n((byte*)data, len/WC_SHA_BLOCK_SIZE, sha->digest);
  236. #else
  237. MMCAU_SHA1_HashN((byte*)data, len/WC_SHA_BLOCK_SIZE,
  238. (word32*)sha->digest);
  239. #endif
  240. }
  241. wolfSSL_CryptHwMutexUnLock();
  242. }
  243. return ret;
  244. }
  245. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  246. !defined(WOLFSSL_QNX_CAAM)
  247. /* wolfcrypt/src/port/caam/caam_sha.c */
  248. #elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
  249. defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
  250. /* This function initializes SHA.
  251. ** This is automatically called by wc_ShaHash */
  252. static int InitSha(wc_Sha* sha)
  253. {
  254. int ret = 0;
  255. sha->digest[0] = 0x67452301L;
  256. sha->digest[1] = 0xEFCDAB89L;
  257. sha->digest[2] = 0x98BADCFEL;
  258. sha->digest[3] = 0x10325476L;
  259. sha->digest[4] = 0xC3D2E1F0L;
  260. sha->buffLen = 0;
  261. sha->loLen = 0;
  262. sha->hiLen = 0;
  263. /* HW needs to be carefully initialized, taking into account soft copy.
  264. ** If already in use; copy may revert to SW as needed. */
  265. ret = esp_sha_init(&(sha->ctx), WC_HASH_TYPE_SHA);
  266. return ret;
  267. }
  268. #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  269. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  270. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  271. /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
  272. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  273. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  274. /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
  275. #elif defined(WOLFSSL_IMXRT_DCP)
  276. #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
  277. /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
  278. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  279. /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
  280. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  281. /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
  282. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  283. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  284. int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
  285. {
  286. if (sha == NULL) {
  287. return BAD_FUNC_ARG;
  288. }
  289. (void)devId;
  290. return se050_hash_init(&sha->se050Ctx, heap);
  291. }
  292. int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
  293. {
  294. return se050_hash_update(&sha->se050Ctx, data, len);
  295. }
  296. int wc_ShaFinal(wc_Sha* sha, byte* hash)
  297. {
  298. int ret = 0;
  299. ret = se050_hash_final(&sha->se050Ctx, hash, WC_SHA_DIGEST_SIZE,
  300. kAlgorithm_SSS_SHA1);
  301. return ret;
  302. }
  303. int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
  304. {
  305. int ret = 0;
  306. ret = se050_hash_final(&sha->se050Ctx, hash, WC_SHA_DIGEST_SIZE,
  307. kAlgorithm_SSS_SHA1);
  308. return ret;
  309. }
  310. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  311. /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
  312. #else
  313. /* Software implementation */
  314. #define USE_SHA_SOFTWARE_IMPL
  315. static int InitSha(wc_Sha* sha)
  316. {
  317. int ret = 0;
  318. sha->digest[0] = 0x67452301L;
  319. sha->digest[1] = 0xEFCDAB89L;
  320. sha->digest[2] = 0x98BADCFEL;
  321. sha->digest[3] = 0x10325476L;
  322. sha->digest[4] = 0xC3D2E1F0L;
  323. sha->buffLen = 0;
  324. sha->loLen = 0;
  325. sha->hiLen = 0;
  326. #ifdef WOLFSSL_HASH_FLAGS
  327. sha->flags = 0;
  328. #endif
  329. return ret;
  330. }
  331. #endif /* End Hardware Acceleration */
  332. /* Software implementation */
  333. #ifdef USE_SHA_SOFTWARE_IMPL
  334. static WC_INLINE void AddLength(wc_Sha* sha, word32 len)
  335. {
  336. word32 tmp = sha->loLen;
  337. if ((sha->loLen += len) < tmp)
  338. sha->hiLen++; /* carry low to high */
  339. }
  340. /* Check if custom wc_Sha transform is used */
  341. #ifndef XTRANSFORM
  342. #define XTRANSFORM(S,B) Transform((S),(B))
  343. #define blk0(i) (W[i] = *((word32*)&data[(i)*sizeof(word32)]))
  344. #define blk1(i) (W[(i)&15] = \
  345. rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1))
  346. #define f1(x,y,z) ((z)^((x) &((y)^(z))))
  347. #define f2(x,y,z) ((x)^(y)^(z))
  348. #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
  349. #define f4(x,y,z) ((x)^(y)^(z))
  350. #ifdef WOLFSSL_NUCLEUS_1_2
  351. /* nucleus.h also defines R1-R4 */
  352. #undef R1
  353. #undef R2
  354. #undef R3
  355. #undef R4
  356. #endif
  357. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  358. #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
  359. rotlFixed((v),5); (w) = rotlFixed((w),30);
  360. #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
  361. rotlFixed((v),5); (w) = rotlFixed((w),30);
  362. #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
  363. rotlFixed((v),5); (w) = rotlFixed((w),30);
  364. #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
  365. rotlFixed((v),5); (w) = rotlFixed((w),30);
  366. #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
  367. rotlFixed((v),5); (w) = rotlFixed((w),30);
  368. static int Transform(wc_Sha* sha, const byte* data)
  369. {
  370. word32 W[WC_SHA_BLOCK_SIZE / sizeof(word32)];
  371. /* Copy context->state[] to working vars */
  372. word32 a = sha->digest[0];
  373. word32 b = sha->digest[1];
  374. word32 c = sha->digest[2];
  375. word32 d = sha->digest[3];
  376. word32 e = sha->digest[4];
  377. #ifdef USE_SLOW_SHA
  378. word32 t, i;
  379. for (i = 0; i < 16; i++) {
  380. R0(a, b, c, d, e, i);
  381. t = e; e = d; d = c; c = b; b = a; a = t;
  382. }
  383. for (; i < 20; i++) {
  384. R1(a, b, c, d, e, i);
  385. t = e; e = d; d = c; c = b; b = a; a = t;
  386. }
  387. for (; i < 40; i++) {
  388. R2(a, b, c, d, e, i);
  389. t = e; e = d; d = c; c = b; b = a; a = t;
  390. }
  391. for (; i < 60; i++) {
  392. R3(a, b, c, d, e, i);
  393. t = e; e = d; d = c; c = b; b = a; a = t;
  394. }
  395. for (; i < 80; i++) {
  396. R4(a, b, c, d, e, i);
  397. t = e; e = d; d = c; c = b; b = a; a = t;
  398. }
  399. #else
  400. /* nearly 1 K bigger in code size but 25% faster */
  401. /* 4 rounds of 20 operations each. Loop unrolled. */
  402. R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  403. R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  404. R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  405. R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  406. R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  407. R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  408. R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  409. R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  410. R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  411. R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  412. R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  413. R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  414. R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  415. R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  416. R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  417. R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  418. R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  419. R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  420. R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  421. R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  422. #endif
  423. /* Add the working vars back into digest state[] */
  424. sha->digest[0] += a;
  425. sha->digest[1] += b;
  426. sha->digest[2] += c;
  427. sha->digest[3] += d;
  428. sha->digest[4] += e;
  429. (void)data; /* Not used */
  430. return 0;
  431. }
  432. #endif /* XTRANSFORM when USE_SHA_SOFTWARE_IMPL is enabled */
  433. /*
  434. ** wolfCrypt InitSha256 external wrapper.
  435. **
  436. ** we'll assume this is ALWAYS for a new, uninitialized sha256
  437. */
  438. int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
  439. {
  440. int ret = 0;
  441. if (sha == NULL) {
  442. return BAD_FUNC_ARG;
  443. }
  444. sha->heap = heap;
  445. #ifdef WOLF_CRYPTO_CB
  446. sha->devId = devId;
  447. sha->devCtx = NULL;
  448. #endif
  449. #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
  450. if (sha->ctx.mode != ESP32_SHA_INIT) {
  451. /* it may be interesting to see old values during debugging */
  452. ESP_LOGV(TAG, "Set ctx mode from prior value: %d", sha->ctx.mode);
  453. }
  454. /* We know this is a fresh, uninitialized item, so set to INIT */
  455. sha->ctx.mode = ESP32_SHA_INIT;
  456. #endif
  457. ret = InitSha(sha);
  458. if (ret != 0) {
  459. return ret;
  460. }
  461. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  462. ret = wolfAsync_DevCtxInit(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA,
  463. sha->heap, devId);
  464. #else
  465. (void)devId;
  466. # endif /* WOLFSSL_ASYNC_CRYPT */
  467. #ifdef WOLFSSL_IMXRT1170_CAAM
  468. ret = wc_CAAM_HashInit(&sha->hndl, &sha->ctx, WC_HASH_TYPE_SHA);
  469. #endif
  470. return ret;
  471. } /* wc_InitSha_ex */
  472. /* do block size increments/updates */
  473. int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
  474. {
  475. int ret = 0;
  476. word32 blocksLen;
  477. byte* local;
  478. if (sha == NULL || (data == NULL && len > 0)) {
  479. return BAD_FUNC_ARG;
  480. }
  481. if (data == NULL && len == 0) {
  482. /* valid, but do nothing */
  483. return 0;
  484. }
  485. #ifdef WOLF_CRYPTO_CB
  486. if (sha->devId != INVALID_DEVID) {
  487. ret = wc_CryptoCb_ShaHash(sha, data, len, NULL);
  488. if (ret != CRYPTOCB_UNAVAILABLE)
  489. return ret;
  490. ret = 0; /* reset ret */
  491. /* fall-through when unavailable */
  492. }
  493. #endif
  494. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  495. if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
  496. #if defined(HAVE_INTEL_QA)
  497. return IntelQaSymSha(&sha->asyncDev, NULL, data, len);
  498. #endif
  499. }
  500. #endif /* WOLFSSL_ASYNC_CRYPT */
  501. /* check that internal buffLen is valid */
  502. if (sha->buffLen >= WC_SHA_BLOCK_SIZE) {
  503. return BUFFER_E;
  504. }
  505. /* add length for final */
  506. AddLength(sha, len);
  507. local = (byte*)sha->buffer;
  508. /* process any remainder from previous operation */
  509. if (sha->buffLen > 0) {
  510. blocksLen = min(len, WC_SHA_BLOCK_SIZE - sha->buffLen);
  511. XMEMCPY(&local[sha->buffLen], data, blocksLen);
  512. sha->buffLen += blocksLen;
  513. data += blocksLen;
  514. len -= blocksLen;
  515. if (sha->buffLen == WC_SHA_BLOCK_SIZE) {
  516. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  517. ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
  518. #endif
  519. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  520. if (sha->ctx.mode == ESP32_SHA_INIT) {
  521. ESP_LOGV(TAG, "wc_ShaUpdate try hardware");
  522. esp_sha_try_hw_lock(&sha->ctx);
  523. }
  524. if (sha->ctx.mode == ESP32_SHA_SW) {
  525. ESP_LOGI(TAG, "wc_ShaUpdate process software");
  526. ret = XTRANSFORM(sha, (const byte*)local);
  527. }
  528. else {
  529. ESP_LOGV(TAG, "wc_ShaUpdate process hardware");
  530. esp_sha_process(sha, (const byte*)local);
  531. }
  532. #elif defined (WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
  533. ESP_LOGI(TAG, "wc_ShaUpdate not implemented for ESP32C3");
  534. ret = XTRANSFORM(sha, (const byte*)local);
  535. #else
  536. ret = XTRANSFORM(sha, (const byte*)local);
  537. #endif
  538. if (ret != 0) {
  539. return ret;
  540. }
  541. sha->buffLen = 0; /* Nothing left to do, so set to zero. */
  542. } /* (sha->buffLen == WC_SHA_BLOCK_SIZE) */
  543. } /* (sha->buffLen > 0) Process any remainder from previous operation. */
  544. /* process blocks */
  545. #ifdef XTRANSFORM_LEN
  546. /* get number of blocks */
  547. /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
  548. /* len (masked by 0xFFFFFFC0) returns block aligned length */
  549. blocksLen = len & ~(WC_SHA_BLOCK_SIZE-1);
  550. if (blocksLen > 0) {
  551. /* Byte reversal performed in function if required. */
  552. XTRANSFORM_LEN(sha, data, blocksLen);
  553. data += blocksLen;
  554. len -= blocksLen;
  555. }
  556. #else
  557. while (len >= WC_SHA_BLOCK_SIZE) {
  558. word32* local32 = sha->buffer;
  559. /* optimization to avoid memcpy if data pointer is properly aligned */
  560. /* Little Endian requires byte swap, so can't use data directly */
  561. #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER)
  562. if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
  563. local32 = (word32*)data;
  564. }
  565. else
  566. #endif
  567. {
  568. XMEMCPY(local32, data, WC_SHA_BLOCK_SIZE);
  569. }
  570. data += WC_SHA_BLOCK_SIZE;
  571. len -= WC_SHA_BLOCK_SIZE;
  572. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  573. ByteReverseWords(local32, local32, WC_SHA_BLOCK_SIZE);
  574. #endif
  575. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  576. if (sha->ctx.mode == ESP32_SHA_INIT){
  577. esp_sha_try_hw_lock(&sha->ctx);
  578. }
  579. if (sha->ctx.mode == ESP32_SHA_SW){
  580. ret = XTRANSFORM(sha, (const byte*)local32);
  581. }
  582. else {
  583. esp_sha_process(sha, (const byte*)local32);
  584. }
  585. #else
  586. ret = XTRANSFORM(sha, (const byte*)local32);
  587. #endif
  588. }
  589. #endif /* XTRANSFORM_LEN */
  590. /* save remainder */
  591. if (len > 0) {
  592. XMEMCPY(local, data, len);
  593. sha->buffLen = len;
  594. }
  595. return ret;
  596. }
  597. int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
  598. {
  599. #ifdef LITTLE_ENDIAN_ORDER
  600. word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
  601. #endif
  602. if (sha == NULL || hash == NULL) {
  603. return BAD_FUNC_ARG;
  604. }
  605. #ifdef LITTLE_ENDIAN_ORDER
  606. ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE);
  607. XMEMCPY(hash, (byte *)&digest[0], WC_SHA_DIGEST_SIZE);
  608. #else
  609. XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
  610. #endif
  611. return 0;
  612. }
  613. /*
  614. ** Finalizes hashing of data. Result is placed into hash.
  615. ** Resets state of sha struct.
  616. */
  617. int wc_ShaFinal(wc_Sha* sha, byte* hash)
  618. {
  619. int ret;
  620. byte* local;
  621. if (sha == NULL || hash == NULL) {
  622. return BAD_FUNC_ARG;
  623. }
  624. local = (byte*)sha->buffer;
  625. #ifdef WOLF_CRYPTO_CB
  626. if (sha->devId != INVALID_DEVID) {
  627. ret = wc_CryptoCb_ShaHash(sha, NULL, 0, hash);
  628. if (ret != CRYPTOCB_UNAVAILABLE)
  629. return ret;
  630. /* fall-through when unavailable */
  631. }
  632. #endif
  633. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  634. if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
  635. #if defined(HAVE_INTEL_QA)
  636. return IntelQaSymSha(&sha->asyncDev, hash, NULL, WC_SHA_DIGEST_SIZE);
  637. #endif
  638. }
  639. #endif /* WOLFSSL_ASYNC_CRYPT */
  640. /* we'll add a 0x80 byte at the end,
  641. ** so make sure we have appropriate buffer length. */
  642. if (sha->buffLen > WC_SHA_BLOCK_SIZE - 1) {
  643. /* exit with error code if there's a bad buffer size in buffLen */
  644. return BAD_STATE_E;
  645. } /* buffLen check */
  646. local[sha->buffLen++] = 0x80; /* add 1 */
  647. /* pad with zeros */
  648. if (sha->buffLen > WC_SHA_PAD_SIZE) {
  649. XMEMSET(&local[sha->buffLen], 0, WC_SHA_BLOCK_SIZE - sha->buffLen);
  650. sha->buffLen += WC_SHA_BLOCK_SIZE - sha->buffLen;
  651. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  652. ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
  653. #endif
  654. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  655. /* For a fresh sha.ctx, try to use hardware acceleration */
  656. if (sha->ctx.mode == ESP32_SHA_INIT) {
  657. esp_sha_try_hw_lock(&sha->ctx);
  658. }
  659. /* if HW was busy, we may need to fall back to SW. */
  660. if (sha->ctx.mode == ESP32_SHA_SW) {
  661. ret = XTRANSFORM(sha, (const byte*)local);
  662. }
  663. else {
  664. ret = esp_sha_process(sha, (const byte*)local);
  665. }
  666. #elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
  667. /* The ESP32C3 is different; SW crypto here. Not yet implemented */
  668. ret = XTRANSFORM(sha, (const byte*)local);
  669. #else
  670. /*
  671. ** The #if defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) also falls
  672. ** though here to SW, as it's not yet implemented for HW.
  673. */
  674. ret = XTRANSFORM(sha, (const byte*)local);
  675. #endif
  676. if (ret != 0) {
  677. return ret;
  678. }
  679. sha->buffLen = 0;
  680. } /* (sha->buffLen > WC_SHA_PAD_SIZE) */
  681. XMEMSET(&local[sha->buffLen], 0, WC_SHA_PAD_SIZE - sha->buffLen);
  682. #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  683. ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
  684. #endif
  685. /* store lengths */
  686. /* put lengths in bits */
  687. sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + (sha->hiLen << 3);
  688. sha->loLen = sha->loLen << 3;
  689. /* ! length ordering dependent on digest endian type ! */
  690. XMEMCPY(&local[WC_SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
  691. XMEMCPY(&local[WC_SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
  692. #if defined(FREESCALE_MMCAU_SHA)
  693. /* Kinetis requires only these bytes reversed */
  694. ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
  695. &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
  696. 2 * sizeof(word32));
  697. #endif
  698. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  699. if (sha->ctx.mode == ESP32_SHA_INIT) {
  700. esp_sha_try_hw_lock(&sha->ctx);
  701. }
  702. if (sha->ctx.mode == ESP32_SHA_SW) {
  703. ret = XTRANSFORM(sha, (const byte*)local);
  704. }
  705. else {
  706. ret = esp_sha_digest_process(sha, 1);
  707. }
  708. /*
  709. ** The #if defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) also falls
  710. ** though here to SW, as it's not yet implemented for HW.
  711. */
  712. #else
  713. ret = XTRANSFORM(sha, (const byte*)local);
  714. #endif
  715. #ifdef LITTLE_ENDIAN_ORDER
  716. ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE);
  717. #endif
  718. XMEMCPY(hash, (byte *)&sha->digest[0], WC_SHA_DIGEST_SIZE);
  719. /* we'll always reset state upon exit and return the error code from above,
  720. * which may cause fall back to SW if HW is busy. we do not return result
  721. * of initSha here */
  722. (void)InitSha(sha); /* reset state */
  723. return ret;
  724. }
  725. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  726. /* Apply SHA1 transformation to the data */
  727. /* @param sha a pointer to wc_Sha structure */
  728. /* @param data data to be applied SHA1 transformation */
  729. /* @return 0 on successful, otherwise non-zero on failure */
  730. int wc_ShaTransform(wc_Sha* sha, const unsigned char* data)
  731. {
  732. /* sanity check */
  733. if (sha == NULL || data == NULL) {
  734. return BAD_FUNC_ARG;
  735. }
  736. return (Transform(sha, data));
  737. }
  738. #endif
  739. #endif /* USE_SHA_SOFTWARE_IMPL */
  740. /*
  741. ** This function initializes SHA. This is automatically called by wc_ShaHash.
  742. */
  743. int wc_InitSha(wc_Sha* sha)
  744. {
  745. return wc_InitSha_ex(sha, NULL, INVALID_DEVID);
  746. }
  747. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
  748. void wc_ShaFree(wc_Sha* sha)
  749. {
  750. if (sha == NULL)
  751. return;
  752. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  753. wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA);
  754. #endif /* WOLFSSL_ASYNC_CRYPT */
  755. #ifdef WOLFSSL_PIC32MZ_HASH
  756. wc_ShaPic32Free(sha);
  757. #endif
  758. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  759. se050_hash_free(&sha->se050Ctx);
  760. #endif
  761. #if (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  762. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  763. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) || \
  764. defined(WOLFSSL_RENESAS_RX64_HASH)
  765. if (sha->msg != NULL) {
  766. XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
  767. sha->msg = NULL;
  768. }
  769. #endif
  770. #ifdef WOLFSSL_IMXRT_DCP
  771. DCPShaFree(sha);
  772. #endif
  773. }
  774. #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
  775. #endif /* !WOLFSSL_TI_HASH */
  776. #if !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_IMXRT_DCP)
  777. #if ((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  778. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \
  779. defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \
  780. (!defined(WOLFSSL_RENESAS_RSIP) || \
  781. defined(NO_WOLFSSL_RENESAS_FSPSM_HASH))
  782. #if !defined(WOLFSSL_RENESAS_RX64_HASH)
  783. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
  784. /* wc_ShaGetHash get hash value */
  785. int wc_ShaGetHash(wc_Sha* sha, byte* hash)
  786. {
  787. int ret;
  788. #ifdef WOLFSSL_SMALL_STACK
  789. wc_Sha* tmpSha;
  790. #else
  791. wc_Sha tmpSha[1];
  792. #endif
  793. if (sha == NULL || hash == NULL) {
  794. return BAD_FUNC_ARG;
  795. }
  796. #ifdef WOLFSSL_SMALL_STACK
  797. tmpSha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL,
  798. DYNAMIC_TYPE_TMP_BUFFER);
  799. if (tmpSha == NULL) {
  800. return MEMORY_E;
  801. }
  802. #endif
  803. ret = wc_ShaCopy(sha, tmpSha);
  804. if (ret == 0) {
  805. ret = wc_ShaFinal(tmpSha, hash);
  806. }
  807. #ifdef WOLFSSL_SMALL_STACK
  808. XFREE(tmpSha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  809. #endif
  810. return ret;
  811. }
  812. int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
  813. {
  814. int ret = 0;
  815. if (src == NULL || dst == NULL)
  816. return BAD_FUNC_ARG;
  817. XMEMCPY(dst, src, sizeof(wc_Sha));
  818. #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
  819. dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
  820. dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
  821. #endif
  822. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  823. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  824. #endif
  825. #ifdef WOLFSSL_PIC32MZ_HASH
  826. ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
  827. #endif
  828. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  829. ret = se050_hash_copy(&src->se050Ctx, &dst->se050Ctx);
  830. #endif
  831. #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
  832. esp_sha_ctx_copy(src, dst);
  833. #endif
  834. #ifdef WOLFSSL_HASH_FLAGS
  835. dst->flags |= WC_HASH_FLAG_ISCOPY;
  836. #endif
  837. return ret;
  838. }
  839. #endif /* WOLFSSL_RENESAS_RX64_HASH */
  840. #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
  841. #endif /* !defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  842. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) ||
  843. defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) */
  844. #endif /* !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_IMXRT_DCP) */
  845. #ifdef WOLFSSL_HASH_FLAGS
  846. int wc_ShaSetFlags(wc_Sha* sha, word32 flags)
  847. {
  848. if (sha) {
  849. sha->flags = flags;
  850. }
  851. return 0;
  852. }
  853. int wc_ShaGetFlags(wc_Sha* sha, word32* flags)
  854. {
  855. if (sha && flags) {
  856. *flags = sha->flags;
  857. }
  858. return 0;
  859. }
  860. #endif
  861. #endif /* !NO_SHA */