sha.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* sha.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/sha.h
  23. */
  24. #ifndef WOLF_CRYPT_SHA_H
  25. #define WOLF_CRYPT_SHA_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifndef NO_SHA
  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_Sha Sha
  35. #define WC_SHA SHA
  36. #define WC_SHA_BLOCK_SIZE SHA_BLOCK_SIZE
  37. #define WC_SHA_DIGEST_SIZE SHA_DIGEST_SIZE
  38. #define WC_SHA_PAD_SIZE SHA_PAD_SIZE
  39. /* for fips @wc_fips */
  40. #include <cyassl/ctaocrypt/sha.h>
  41. #endif
  42. #ifdef FREESCALE_LTC_SHA
  43. #include "fsl_ltc.h"
  44. #endif
  45. #if defined(WOLFSSL_IMXRT1170_CAAM)
  46. #include "fsl_device_registers.h"
  47. #include "fsl_caam.h"
  48. #endif
  49. #ifdef WOLFSSL_IMXRT_DCP
  50. #include "fsl_dcp.h"
  51. #endif
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /* avoid redefinition of structs */
  56. #if !defined(HAVE_FIPS) || \
  57. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  58. #ifdef WOLFSSL_MICROCHIP_PIC32MZ
  59. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  60. #endif
  61. #ifdef STM32_HASH
  62. #include <wolfssl/wolfcrypt/port/st/stm32.h>
  63. #endif
  64. #ifdef WOLFSSL_ASYNC_CRYPT
  65. #include <wolfssl/wolfcrypt/async.h>
  66. #endif
  67. #ifdef WOLFSSL_ESP32WROOM32_CRYPT
  68. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  69. #endif
  70. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  71. #include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h>
  72. #endif
  73. #if !defined(NO_OLD_SHA_NAMES)
  74. #define SHA WC_SHA
  75. #endif
  76. #ifndef NO_OLD_WC_NAMES
  77. #define Sha wc_Sha
  78. #define SHA_BLOCK_SIZE WC_SHA_BLOCK_SIZE
  79. #define SHA_DIGEST_SIZE WC_SHA_DIGEST_SIZE
  80. #define SHA_PAD_SIZE WC_SHA_PAD_SIZE
  81. #endif
  82. /* in bytes */
  83. enum {
  84. WC_SHA = WC_HASH_TYPE_SHA,
  85. WC_SHA_BLOCK_SIZE = 64,
  86. WC_SHA_DIGEST_SIZE = 20,
  87. WC_SHA_PAD_SIZE = 56
  88. };
  89. #if defined(WOLFSSL_TI_HASH)
  90. #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
  91. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM)
  92. #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
  93. #elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
  94. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
  95. #include "wolfssl/wolfcrypt/port/Renesas/renesas_tsip_types.h"
  96. #elif defined(WOLFSSL_RENESAS_RX64_HASH)
  97. #include "wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h"
  98. #else
  99. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  100. #include "wolfssl/wolfcrypt/port/nxp/se050_port.h"
  101. #endif
  102. #if defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  103. #include <psa/crypto.h>
  104. #undef WOLFSSL_NO_HASH_RAW
  105. #define WOLFSSL_NO_HASH_RAW
  106. #endif
  107. /* Sha digest */
  108. struct wc_Sha {
  109. #ifdef FREESCALE_LTC_SHA
  110. ltc_hash_ctx_t ctx;
  111. #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  112. SE050_HASH_Context se050Ctx;
  113. #elif defined(STM32_HASH)
  114. STM32_HASH_Context stmCtx;
  115. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  116. wc_silabs_sha_t silabsCtx;
  117. #elif defined(WOLFSSL_IMXRT_DCP)
  118. dcp_handle_t handle;
  119. dcp_hash_ctx_t ctx;
  120. #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
  121. psa_hash_operation_t psa_ctx;
  122. #else
  123. word32 buffLen; /* in bytes */
  124. word32 loLen; /* length in bytes */
  125. word32 hiLen; /* length in bytes */
  126. word32 buffer[WC_SHA_BLOCK_SIZE / sizeof(word32)];
  127. #ifdef WOLFSSL_PIC32MZ_HASH
  128. word32 digest[PIC32_DIGEST_SIZE / sizeof(word32)];
  129. #else
  130. word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
  131. #endif
  132. void* heap;
  133. #ifdef WOLFSSL_PIC32MZ_HASH
  134. hashUpdCache cache; /* cache for updates */
  135. #endif
  136. #ifdef WOLFSSL_ASYNC_CRYPT
  137. WC_ASYNC_DEV asyncDev;
  138. #endif /* WOLFSSL_ASYNC_CRYPT */
  139. #ifdef WOLF_CRYPTO_CB
  140. int devId;
  141. void* devCtx; /* generic crypto callback context */
  142. #endif
  143. #ifdef WOLFSSL_IMXRT1170_CAAM
  144. caam_hash_ctx_t ctx;
  145. caam_handle_t hndl;
  146. #endif
  147. #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
  148. byte* msg;
  149. word32 used;
  150. word32 len;
  151. #endif
  152. #endif
  153. #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
  154. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
  155. WC_ESP32SHA ctx;
  156. #endif
  157. #ifdef WOLFSSL_HASH_FLAGS
  158. word32 flags; /* enum wc_HashFlags in hash.h */
  159. #endif
  160. };
  161. #ifndef WC_SHA_TYPE_DEFINED
  162. typedef struct wc_Sha wc_Sha;
  163. #define WC_SHA_TYPE_DEFINED
  164. #endif
  165. #endif /* WOLFSSL_TI_HASH */
  166. #endif /* HAVE_FIPS */
  167. WOLFSSL_API int wc_InitSha(wc_Sha* sha);
  168. WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId);
  169. WOLFSSL_API int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len);
  170. WOLFSSL_API int wc_ShaFinalRaw(wc_Sha* sha, byte* hash);
  171. WOLFSSL_API int wc_ShaFinal(wc_Sha* sha, byte* hash);
  172. WOLFSSL_API void wc_ShaFree(wc_Sha* sha);
  173. WOLFSSL_API int wc_ShaGetHash(wc_Sha* sha, byte* hash);
  174. WOLFSSL_API int wc_ShaCopy(wc_Sha* src, wc_Sha* dst);
  175. #if defined(OPENSSL_EXTRA)
  176. WOLFSSL_API int wc_ShaTransform(wc_Sha* sha, const unsigned char* data);
  177. #endif
  178. #ifdef WOLFSSL_PIC32MZ_HASH
  179. WOLFSSL_API void wc_ShaSizeSet(wc_Sha* sha, word32 len);
  180. #endif
  181. #ifdef WOLFSSL_HASH_FLAGS
  182. WOLFSSL_API int wc_ShaSetFlags(wc_Sha* sha, word32 flags);
  183. WOLFSSL_API int wc_ShaGetFlags(wc_Sha* sha, word32* flags);
  184. #endif
  185. #ifdef __cplusplus
  186. } /* extern "C" */
  187. #endif
  188. #endif /* NO_SHA */
  189. #endif /* WOLF_CRYPT_SHA_H */