sha256.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  118. #include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h"
  119. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  120. #include "wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h"
  121. #else
  122. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  123. #include "wolfssl/wolfcrypt/port/nxp/se050_port.h"
  124. #endif
  125. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  126. #include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h>
  127. #endif
  128. #ifdef HAVE_ARIA
  129. #include "mcapi.h"
  130. #include "mcapi_error.h"
  131. #endif
  132. /* wc_Sha256 digest */
  133. struct wc_Sha256 {
  134. #ifdef FREESCALE_LTC_SHA
  135. ltc_hash_ctx_t ctx;
  136. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  137. SE050_HASH_Context se050Ctx;
  138. #elif defined(STM32_HASH_SHA2)
  139. STM32_HASH_Context stmCtx;
  140. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  141. wc_silabs_sha_t silabsCtx;
  142. #elif defined(WOLFSSL_IMXRT_DCP)
  143. dcp_handle_t handle;
  144. dcp_hash_ctx_t ctx;
  145. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  146. cy_stc_crypto_sha_state_t hash_state;
  147. cy_en_crypto_sha_mode_t sha_mode;
  148. cy_stc_crypto_v2_sha256_buffers_t sha_buffers;
  149. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  150. psa_hash_operation_t psa_ctx;
  151. #else
  152. /* alignment on digest and buffer speeds up ARMv8 crypto operations */
  153. ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
  154. ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
  155. word32 buffLen; /* in bytes */
  156. word32 loLen; /* length in bytes */
  157. word32 hiLen; /* length in bytes */
  158. void* heap;
  159. #endif
  160. #ifdef WOLFSSL_PIC32MZ_HASH
  161. hashUpdCache cache; /* cache for updates */
  162. #endif
  163. #ifdef WOLFSSL_ASYNC_CRYPT
  164. WC_ASYNC_DEV asyncDev;
  165. #endif /* WOLFSSL_ASYNC_CRYPT */
  166. #ifdef WOLFSSL_SMALL_STACK_CACHE
  167. word32* W;
  168. #endif /* !FREESCALE_LTC_SHA && !STM32_HASH_SHA2 */
  169. #ifdef WOLFSSL_DEVCRYPTO_HASH
  170. WC_CRYPTODEV ctx;
  171. #endif
  172. #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
  173. byte* msg;
  174. word32 used;
  175. word32 len;
  176. #endif
  177. #if defined(WOLFSSL_ESP32_CRYPT) && \
  178. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
  179. WC_ESP32SHA ctx;
  180. #endif
  181. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  182. maxq_sha256_t maxq_ctx;
  183. #endif
  184. #ifdef WOLFSSL_CRYPTOCELL
  185. CRYS_HASHUserContext_t ctx;
  186. #endif
  187. #ifdef WOLFSSL_KCAPI_HASH
  188. wolfssl_KCAPI_Hash kcapi;
  189. #endif
  190. #ifdef WOLF_CRYPTO_CB
  191. int devId;
  192. void* devCtx; /* generic crypto callback context */
  193. #endif
  194. #ifdef WOLFSSL_IMXRT1170_CAAM
  195. caam_hash_ctx_t ctx;
  196. caam_handle_t hndl;
  197. #endif
  198. #ifdef HAVE_ARIA
  199. MC_HSESSION hSession;
  200. #endif
  201. #ifdef WOLFSSL_HASH_FLAGS
  202. word32 flags; /* enum wc_HashFlags in hash.h */
  203. #endif
  204. };
  205. #ifndef WC_SHA256_TYPE_DEFINED
  206. typedef struct wc_Sha256 wc_Sha256;
  207. #define WC_SHA256_TYPE_DEFINED
  208. #endif
  209. #endif
  210. #endif /* HAVE_FIPS */
  211. WOLFSSL_API int wc_InitSha256(wc_Sha256* sha);
  212. WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha, void* heap, int devId);
  213. WOLFSSL_API int wc_Sha256Update(wc_Sha256* sha, const byte* data, word32 len);
  214. WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash);
  215. WOLFSSL_API int wc_Sha256Final(wc_Sha256* sha256, byte* hash);
  216. WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256);
  217. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  218. WOLFSSL_API int wc_Sha256Transform(wc_Sha256* sha, const unsigned char* data);
  219. #endif
  220. #if defined(WOLFSSL_HASH_KEEP)
  221. WOLFSSL_API int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz);
  222. #endif
  223. WOLFSSL_API int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash);
  224. WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
  225. #ifdef WOLFSSL_PIC32MZ_HASH
  226. WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256* sha256, word32 len);
  227. #endif
  228. #ifdef WOLFSSL_HASH_FLAGS
  229. WOLFSSL_API int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags);
  230. WOLFSSL_API int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags);
  231. #endif
  232. #ifdef WOLFSSL_SHA224
  233. /* avoid redefinition of structs */
  234. #if !defined(HAVE_FIPS) || \
  235. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  236. #if !defined(NO_OLD_SHA_NAMES)
  237. #define SHA224 WC_SHA224
  238. #endif
  239. #ifndef NO_OLD_WC_NAMES
  240. #define Sha224 wc_Sha224
  241. #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
  242. #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
  243. #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
  244. #endif
  245. /* in bytes */
  246. enum {
  247. WC_SHA224 = WC_HASH_TYPE_SHA224,
  248. WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE,
  249. WC_SHA224_DIGEST_SIZE = 28,
  250. WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE
  251. };
  252. #ifndef WC_SHA224_TYPE_DEFINED
  253. typedef struct wc_Sha256 wc_Sha224;
  254. #define WC_SHA224_TYPE_DEFINED
  255. #endif
  256. #endif /* HAVE_FIPS */
  257. WOLFSSL_API int wc_InitSha224(wc_Sha224* sha224);
  258. WOLFSSL_API int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId);
  259. WOLFSSL_API int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len);
  260. WOLFSSL_API int wc_Sha224Final(wc_Sha224* sha224, byte* hash);
  261. WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224);
  262. #if defined(WOLFSSL_HASH_KEEP)
  263. WOLFSSL_API int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz);
  264. #endif
  265. WOLFSSL_API int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash);
  266. WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
  267. #ifdef WOLFSSL_HASH_FLAGS
  268. WOLFSSL_API int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags);
  269. WOLFSSL_API int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags);
  270. #endif
  271. #endif /* WOLFSSL_SHA224 */
  272. #ifdef __cplusplus
  273. } /* extern "C" */
  274. #endif
  275. #endif /* NO_SHA256 */
  276. #endif /* WOLF_CRYPT_SHA256_H */