sha.c 27 KB

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