sha512.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* sha512.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/sha512.h
  23. */
  24. #ifndef WOLF_CRYPT_SHA512_H
  25. #define WOLF_CRYPT_SHA512_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
  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 __cplusplus
  33. extern "C" {
  34. #endif
  35. /* avoid redefinition of structs */
  36. #if !defined(HAVE_FIPS) || \
  37. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  38. #ifdef WOLFSSL_ASYNC_CRYPT
  39. #include <wolfssl/wolfcrypt/async.h>
  40. #endif
  41. #ifdef WOLFSSL_ESP32_CRYPT
  42. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  43. #endif
  44. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  45. #include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h>
  46. #endif
  47. #if defined(WOLFSSL_PSOC6_CRYPTO)
  48. #include "cy_crypto_core_sha.h"
  49. #include "cy_device_headers.h"
  50. #include "cy_crypto_common.h"
  51. #include "cy_crypto_core.h"
  52. #endif
  53. #if defined(WOLFSSL_KCAPI_HASH)
  54. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_hash.h>
  55. #endif
  56. #if defined(WOLFSSL_IMXRT1170_CAAM)
  57. #include "fsl_device_registers.h"
  58. #include "fsl_caam.h"
  59. #endif
  60. #if defined(_MSC_VER)
  61. #define SHA512_NOINLINE __declspec(noinline)
  62. #elif defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
  63. #define SHA512_NOINLINE __attribute__((noinline))
  64. #else
  65. #define SHA512_NOINLINE
  66. #endif
  67. #ifdef WOLFSSL_SHA512
  68. #if !defined(NO_OLD_SHA_NAMES)
  69. #define SHA512 WC_SHA512
  70. #endif
  71. #if !defined(NO_OLD_WC_NAMES)
  72. #define Sha512 wc_Sha512
  73. #define SHA512_BLOCK_SIZE WC_SHA512_BLOCK_SIZE
  74. #define SHA512_DIGEST_SIZE WC_SHA512_DIGEST_SIZE
  75. #define SHA512_PAD_SIZE WC_SHA512_PAD_SIZE
  76. #endif
  77. #endif /* WOLFSSL_SHA512 */
  78. /* in bytes */
  79. enum {
  80. #ifdef WOLFSSL_SHA512
  81. WC_SHA512 = WC_HASH_TYPE_SHA512,
  82. #ifndef WOLFSSL_NOSHA512_224
  83. WC_SHA512_224 = WC_HASH_TYPE_SHA512_224,
  84. #endif
  85. #ifndef WOLFSSL_NOSHA512_256
  86. WC_SHA512_256 = WC_HASH_TYPE_SHA512_256,
  87. #endif
  88. #endif
  89. WC_SHA512_BLOCK_SIZE = 128,
  90. WC_SHA512_DIGEST_SIZE = 64,
  91. WC_SHA512_PAD_SIZE = 112,
  92. WC_SHA512_224_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
  93. WC_SHA512_224_DIGEST_SIZE = 28,
  94. WC_SHA512_224_PAD_SIZE = WC_SHA512_PAD_SIZE,
  95. WC_SHA512_256_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
  96. WC_SHA512_256_DIGEST_SIZE = 32,
  97. WC_SHA512_256_PAD_SIZE = WC_SHA512_PAD_SIZE
  98. };
  99. #if defined(WOLFSSL_IMX6_CAAM) && !defined(WOLFSSL_QNX_CAAM)
  100. #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
  101. #elif defined(WOLFSSL_RENESAS_RSIP) && \
  102. !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
  103. #include "wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h"
  104. #else
  105. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  106. #include "wolfssl/wolfcrypt/port/nxp/se050_port.h"
  107. #endif
  108. #ifdef HAVE_ARIA
  109. #include "mcapi.h"
  110. #include "mcapi_error.h"
  111. #endif
  112. /* wc_Sha512 digest */
  113. struct wc_Sha512 {
  114. #ifdef WOLFSSL_PSOC6_CRYPTO
  115. cy_stc_crypto_sha_state_t hash_state;
  116. cy_en_crypto_sha_mode_t sha_mode;
  117. cy_stc_crypto_v2_sha512_buffers_t sha_buffers;
  118. #else
  119. word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
  120. word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)];
  121. word32 buffLen; /* in bytes */
  122. word64 loLen; /* length in bytes */
  123. word64 hiLen; /* length in bytes */
  124. void* heap;
  125. #ifdef USE_INTEL_SPEEDUP
  126. const byte* data;
  127. #endif
  128. #ifdef WOLFSSL_ASYNC_CRYPT
  129. WC_ASYNC_DEV asyncDev;
  130. #endif /* WOLFSSL_ASYNC_CRYPT */
  131. #ifdef WOLFSSL_SMALL_STACK_CACHE
  132. word64* W;
  133. #endif
  134. #if defined(WOLFSSL_ESP32_CRYPT) && \
  135. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
  136. !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
  137. WC_ESP32SHA ctx;
  138. #endif
  139. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  140. wc_silabs_sha_t silabsCtx;
  141. #endif
  142. #ifdef WOLFSSL_KCAPI_HASH
  143. wolfssl_KCAPI_Hash kcapi;
  144. #endif
  145. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  146. SE050_HASH_Context se050Ctx;
  147. #endif
  148. #if defined(WOLFSSL_HASH_KEEP)
  149. byte* msg;
  150. word32 used;
  151. word32 len;
  152. #endif
  153. #ifdef WOLF_CRYPTO_CB
  154. int devId;
  155. void* devCtx; /* generic crypto callback context */
  156. #endif
  157. #ifdef WOLFSSL_HASH_FLAGS
  158. word32 flags; /* enum wc_HashFlags in hash.h */
  159. #endif
  160. #ifdef WOLFSSL_IMXRT1170_CAAM
  161. caam_hash_ctx_t ctx;
  162. caam_handle_t hndl;
  163. #endif
  164. #ifdef HAVE_ARIA
  165. MC_HSESSION hSession;
  166. #endif
  167. #endif /* WOLFSSL_PSOC6_CRYPTO */
  168. };
  169. #ifndef WC_SHA512_TYPE_DEFINED
  170. typedef struct wc_Sha512 wc_Sha512;
  171. typedef struct wc_Sha512 wc_Sha512_224;
  172. typedef struct wc_Sha512 wc_Sha512_256;
  173. #define WC_SHA512_TYPE_DEFINED
  174. #endif
  175. #endif /* WOLFSSL_IMX6_CAAM && !WOLFSSL_QNX_CAAM */
  176. #endif /* HAVE_FIPS */
  177. #ifdef WOLFSSL_SHA512
  178. #ifdef WOLFSSL_ARMASM
  179. #ifdef __aarch64__
  180. #ifndef WOLFSSL_ARMASM_CRYPTO_SHA512
  181. void Transform_Sha512_Len_neon(wc_Sha512* sha512, const byte* data,
  182. word32 len);
  183. #define Transform_Sha512_Len Transform_Sha512_Len_neon
  184. #else
  185. void Transform_Sha512_Len_crypto(wc_Sha512* sha512, const byte* data,
  186. word32 len);
  187. #define Transform_Sha512_Len Transform_Sha512_Len_crypto
  188. #endif
  189. #else
  190. extern void Transform_Sha512_Len(wc_Sha512* sha512, const byte* data,
  191. word32 len);
  192. #endif
  193. #endif
  194. WOLFSSL_API int wc_InitSha512(wc_Sha512* sha);
  195. WOLFSSL_API int wc_InitSha512_ex(wc_Sha512* sha, void* heap, int devId);
  196. WOLFSSL_API int wc_Sha512Update(wc_Sha512* sha, const byte* data, word32 len);
  197. WOLFSSL_API int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash);
  198. WOLFSSL_API int wc_Sha512Final(wc_Sha512* sha512, byte* hash);
  199. WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha);
  200. WOLFSSL_API int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash);
  201. WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst);
  202. #if defined(WOLFSSL_HASH_KEEP)
  203. WOLFSSL_API int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz);
  204. #endif
  205. #ifdef WOLFSSL_HASH_FLAGS
  206. WOLFSSL_API int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags);
  207. WOLFSSL_API int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags);
  208. #endif
  209. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  210. WOLFSSL_API int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data);
  211. #endif
  212. #if !defined(WOLFSSL_NOSHA512_224) && \
  213. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  214. WOLFSSL_API int wc_InitSha512_224(wc_Sha512* sha);
  215. WOLFSSL_API int wc_InitSha512_224_ex(wc_Sha512* sha, void* heap, int devId);
  216. WOLFSSL_API int wc_Sha512_224Update(wc_Sha512* sha, const byte* data, word32 len);
  217. WOLFSSL_API int wc_Sha512_224FinalRaw(wc_Sha512* sha512, byte* hash);
  218. WOLFSSL_API int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash);
  219. WOLFSSL_API void wc_Sha512_224Free(wc_Sha512* sha);
  220. WOLFSSL_API int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash);
  221. WOLFSSL_API int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst);
  222. #ifdef WOLFSSL_HASH_FLAGS
  223. WOLFSSL_API int wc_Sha512_224SetFlags(wc_Sha512* sha512, word32 flags);
  224. WOLFSSL_API int wc_Sha512_224GetFlags(wc_Sha512* sha512, word32* flags);
  225. #endif
  226. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  227. WOLFSSL_API int wc_Sha512_224Transform(wc_Sha512* sha,
  228. const unsigned char* data);
  229. #endif /* OPENSSL_EXTRA */
  230. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  231. #if !defined(WOLFSSL_NOSHA512_256) && \
  232. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  233. WOLFSSL_API int wc_InitSha512_256(wc_Sha512* sha);
  234. WOLFSSL_API int wc_InitSha512_256_ex(wc_Sha512* sha, void* heap, int devId);
  235. WOLFSSL_API int wc_Sha512_256Update(wc_Sha512* sha, const byte* data, word32 len);
  236. WOLFSSL_API int wc_Sha512_256FinalRaw(wc_Sha512* sha512, byte* hash);
  237. WOLFSSL_API int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash);
  238. WOLFSSL_API void wc_Sha512_256Free(wc_Sha512* sha);
  239. WOLFSSL_API int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash);
  240. WOLFSSL_API int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst);
  241. #ifdef WOLFSSL_HASH_FLAGS
  242. WOLFSSL_API int wc_Sha512_256SetFlags(wc_Sha512* sha512, word32 flags);
  243. WOLFSSL_API int wc_Sha512_256GetFlags(wc_Sha512* sha512, word32* flags);
  244. #endif
  245. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  246. WOLFSSL_API int wc_Sha512_256Transform(wc_Sha512* sha,
  247. const unsigned char* data);
  248. #endif /* OPENSSL_EXTRA */
  249. #endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */
  250. #endif /* WOLFSSL_SHA512 */
  251. #if defined(WOLFSSL_SHA384)
  252. /* avoid redefinition of structs */
  253. #if !defined(HAVE_FIPS) || \
  254. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  255. #if !defined(NO_OLD_SHA_NAMES)
  256. #define SHA384 WC_SHA384
  257. #endif
  258. #if !defined(NO_OLD_WC_NAMES)
  259. #define Sha384 wc_Sha384
  260. #define SHA384_BLOCK_SIZE WC_SHA384_BLOCK_SIZE
  261. #define SHA384_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
  262. #define SHA384_PAD_SIZE WC_SHA384_PAD_SIZE
  263. #endif
  264. /* in bytes */
  265. enum {
  266. WC_SHA384 = WC_HASH_TYPE_SHA384,
  267. WC_SHA384_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
  268. WC_SHA384_DIGEST_SIZE = 48,
  269. WC_SHA384_PAD_SIZE = WC_SHA512_PAD_SIZE
  270. };
  271. #ifndef WC_SHA384_TYPE_DEFINED
  272. typedef struct wc_Sha512 wc_Sha384;
  273. #define WC_SHA384_TYPE_DEFINED
  274. #endif
  275. #endif /* HAVE_FIPS */
  276. WOLFSSL_API int wc_InitSha384(wc_Sha384* sha);
  277. WOLFSSL_API int wc_InitSha384_ex(wc_Sha384* sha, void* heap, int devId);
  278. WOLFSSL_API int wc_Sha384Update(wc_Sha384* sha, const byte* data, word32 len);
  279. WOLFSSL_API int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash);
  280. WOLFSSL_API int wc_Sha384Final(wc_Sha384* sha384, byte* hash);
  281. WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha);
  282. WOLFSSL_API int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash);
  283. WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst);
  284. #if defined(WOLFSSL_HASH_KEEP)
  285. WOLFSSL_API int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz);
  286. #endif
  287. #ifdef WOLFSSL_HASH_FLAGS
  288. WOLFSSL_API int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags);
  289. WOLFSSL_API int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags);
  290. #endif
  291. #endif /* WOLFSSL_SHA384 */
  292. #ifdef __cplusplus
  293. } /* extern "C" */
  294. #endif
  295. #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */
  296. #endif /* WOLF_CRYPT_SHA512_H */