sha256.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* sha256.h
  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. /*!
  22. \file wolfssl/wolfcrypt/sha256.h
  23. */
  24. #ifndef WOLF_CRYPT_SHA256_H
  25. #define WOLF_CRYPT_SHA256_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifndef NO_SHA256
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  30. #include <wolfssl/wolfcrypt/fips.h>
  31. #endif /* HAVE_FIPS_VERSION >= 2 */
  32. #ifdef FREESCALE_LTC_SHA
  33. #include "fsl_ltc.h"
  34. #endif
  35. #if defined(WOLFSSL_IMXRT1170_CAAM)
  36. #include "fsl_device_registers.h"
  37. #include "fsl_caam.h"
  38. #endif
  39. #ifdef WOLFSSL_IMXRT_DCP
  40. #include "fsl_dcp.h"
  41. #endif
  42. #if defined(WOLFSSL_PSOC6_CRYPTO)
  43. #include "cy_crypto_core_sha.h"
  44. #include "cy_device_headers.h"
  45. #include "cy_crypto_common.h"
  46. #include "cy_crypto_core.h"
  47. #endif
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /* avoid redefinition of structs */
  52. #if !defined(HAVE_FIPS) || \
  53. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  54. #ifdef WOLFSSL_MICROCHIP_PIC32MZ
  55. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  56. #endif
  57. #ifdef STM32_HASH
  58. #include <wolfssl/wolfcrypt/port/st/stm32.h>
  59. #endif
  60. #ifdef WOLFSSL_ASYNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HASH)
  64. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  65. #endif
  66. #if defined(WOLFSSL_ESP32_CRYPT)
  67. #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
  68. #endif
  69. #if defined(WOLFSSL_CRYPTOCELL)
  70. #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
  71. #endif
  72. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  73. #include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h>
  74. #endif
  75. #if defined(WOLFSSL_KCAPI_HASH)
  76. #include "wolfssl/wolfcrypt/port/kcapi/kcapi_hash.h"
  77. #endif
  78. #if defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  79. #include <psa/crypto.h>
  80. #undef WOLFSSL_NO_HASH_RAW
  81. #define WOLFSSL_NO_HASH_RAW
  82. #endif
  83. #if defined(_MSC_VER)
  84. #define SHA256_NOINLINE __declspec(noinline)
  85. #elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
  86. #define SHA256_NOINLINE __attribute__((noinline))
  87. #else
  88. #define SHA256_NOINLINE
  89. #endif
  90. #if !defined(NO_OLD_SHA_NAMES)
  91. #define SHA256 WC_SHA256
  92. #endif
  93. #ifndef NO_OLD_WC_NAMES
  94. #define Sha256 wc_Sha256
  95. #define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
  96. #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  97. #define SHA256_PAD_SIZE WC_SHA256_PAD_SIZE
  98. #endif
  99. /* in bytes */
  100. enum {
  101. WC_SHA256 = WC_HASH_TYPE_SHA256,
  102. WC_SHA256_BLOCK_SIZE = 64,
  103. WC_SHA256_DIGEST_SIZE = 32,
  104. WC_SHA256_PAD_SIZE = 56
  105. };
  106. #ifdef WOLFSSL_TI_HASH
  107. #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
  108. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM)
  109. #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
  110. #elif defined(WOLFSSL_AFALG_HASH)
  111. #include "wolfssl/wolfcrypt/port/af_alg/afalg_hash.h"
  112. #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  113. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  114. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  115. #include "wolfssl/wolfcrypt/port/Renesas/renesas_tsip_types.h"
  116. #elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  117. defined(WOLFSSL_RENESAS_RSIP)) && \
  118. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  119. #include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h"
  120. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  121. #include "wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h"
  122. #else
  123. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  124. #include "wolfssl/wolfcrypt/port/nxp/se050_port.h"
  125. #endif
  126. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  127. #include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h>
  128. #endif
  129. #ifdef HAVE_ARIA
  130. #include "mcapi.h"
  131. #include "mcapi_error.h"
  132. #endif
  133. /* wc_Sha256 digest */
  134. struct wc_Sha256 {
  135. #ifdef FREESCALE_LTC_SHA
  136. ltc_hash_ctx_t ctx;
  137. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  138. SE050_HASH_Context se050Ctx;
  139. #elif defined(STM32_HASH_SHA2)
  140. STM32_HASH_Context stmCtx;
  141. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  142. wc_silabs_sha_t silabsCtx;
  143. #elif defined(WOLFSSL_IMXRT_DCP)
  144. dcp_handle_t handle;
  145. dcp_hash_ctx_t ctx;
  146. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  147. cy_stc_crypto_sha_state_t hash_state;
  148. cy_en_crypto_sha_mode_t sha_mode;
  149. cy_stc_crypto_v2_sha256_buffers_t sha_buffers;
  150. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  151. psa_hash_operation_t psa_ctx;
  152. #else
  153. /* alignment on digest and buffer speeds up ARMv8 crypto operations */
  154. ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
  155. ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
  156. word32 buffLen; /* in bytes */
  157. word32 loLen; /* length in bytes */
  158. word32 hiLen; /* length in bytes */
  159. void* heap;
  160. #endif
  161. #ifdef WOLFSSL_PIC32MZ_HASH
  162. hashUpdCache cache; /* cache for updates */
  163. #endif
  164. #ifdef WOLFSSL_ASYNC_CRYPT
  165. WC_ASYNC_DEV asyncDev;
  166. #endif /* WOLFSSL_ASYNC_CRYPT */
  167. #ifdef WOLFSSL_SMALL_STACK_CACHE
  168. word32* W;
  169. #endif /* !FREESCALE_LTC_SHA && !STM32_HASH_SHA2 */
  170. #ifdef WOLFSSL_DEVCRYPTO_HASH
  171. WC_CRYPTODEV ctx;
  172. #endif
  173. #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
  174. byte* msg;
  175. word32 used;
  176. word32 len;
  177. #endif
  178. #if defined(WOLFSSL_ESP32_CRYPT) && \
  179. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  180. (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
  181. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
  182. WC_ESP32SHA ctx;
  183. #endif
  184. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  185. maxq_sha256_t maxq_ctx;
  186. #endif
  187. #ifdef WOLFSSL_CRYPTOCELL
  188. CRYS_HASHUserContext_t ctx;
  189. #endif
  190. #ifdef WOLFSSL_KCAPI_HASH
  191. wolfssl_KCAPI_Hash kcapi;
  192. #endif
  193. #ifdef WOLF_CRYPTO_CB
  194. int devId;
  195. void* devCtx; /* generic crypto callback context */
  196. #endif
  197. #ifdef WOLFSSL_IMXRT1170_CAAM
  198. caam_hash_ctx_t ctx;
  199. caam_handle_t hndl;
  200. #endif
  201. #ifdef HAVE_ARIA
  202. MC_HSESSION hSession;
  203. #endif
  204. #ifdef WOLFSSL_HASH_FLAGS
  205. word32 flags; /* enum wc_HashFlags in hash.h */
  206. #endif
  207. };
  208. #ifndef WC_SHA256_TYPE_DEFINED
  209. typedef struct wc_Sha256 wc_Sha256;
  210. #define WC_SHA256_TYPE_DEFINED
  211. #endif
  212. #endif
  213. #endif /* HAVE_FIPS */
  214. WOLFSSL_API int wc_InitSha256(wc_Sha256* sha);
  215. WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha, void* heap, int devId);
  216. WOLFSSL_API int wc_Sha256Update(wc_Sha256* sha, const byte* data, word32 len);
  217. WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash);
  218. WOLFSSL_API int wc_Sha256Final(wc_Sha256* sha256, byte* hash);
  219. WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256);
  220. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  221. WOLFSSL_API int wc_Sha256Transform(wc_Sha256* sha, const unsigned char* data);
  222. #endif
  223. #if defined(WOLFSSL_HASH_KEEP)
  224. WOLFSSL_API int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz);
  225. #endif
  226. WOLFSSL_API int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash);
  227. WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
  228. #ifdef WOLFSSL_PIC32MZ_HASH
  229. WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256* sha256, word32 len);
  230. #endif
  231. #ifdef WOLFSSL_HASH_FLAGS
  232. WOLFSSL_API int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags);
  233. WOLFSSL_API int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags);
  234. #endif
  235. #ifdef WOLFSSL_SHA224
  236. /* avoid redefinition of structs */
  237. #if !defined(HAVE_FIPS) || \
  238. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  239. #if !defined(NO_OLD_SHA_NAMES)
  240. #define SHA224 WC_SHA224
  241. #endif
  242. #ifndef NO_OLD_WC_NAMES
  243. #define Sha224 wc_Sha224
  244. #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
  245. #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
  246. #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
  247. #endif
  248. /* in bytes */
  249. enum {
  250. WC_SHA224 = WC_HASH_TYPE_SHA224,
  251. WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE,
  252. WC_SHA224_DIGEST_SIZE = 28,
  253. WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE
  254. };
  255. #ifndef WC_SHA224_TYPE_DEFINED
  256. typedef struct wc_Sha256 wc_Sha224;
  257. #define WC_SHA224_TYPE_DEFINED
  258. #endif
  259. #endif /* HAVE_FIPS */
  260. WOLFSSL_API int wc_InitSha224(wc_Sha224* sha224);
  261. WOLFSSL_API int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId);
  262. WOLFSSL_API int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len);
  263. WOLFSSL_API int wc_Sha224Final(wc_Sha224* sha224, byte* hash);
  264. WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224);
  265. #if defined(WOLFSSL_HASH_KEEP)
  266. WOLFSSL_API int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz);
  267. #endif
  268. WOLFSSL_API int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash);
  269. WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
  270. #ifdef WOLFSSL_HASH_FLAGS
  271. WOLFSSL_API int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags);
  272. WOLFSSL_API int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags);
  273. #endif
  274. #endif /* WOLFSSL_SHA224 */
  275. #ifdef __cplusplus
  276. } /* extern "C" */
  277. #endif
  278. #endif /* NO_SHA256 */
  279. #endif /* WOLF_CRYPT_SHA256_H */