sha256.h 8.3 KB

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