sha.c 30 KB

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