sha512.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. WC_ESP32SHA ctx;
  137. #endif
  138. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  139. wc_silabs_sha_t silabsCtx;
  140. #endif
  141. #ifdef WOLFSSL_KCAPI_HASH
  142. wolfssl_KCAPI_Hash kcapi;
  143. #endif
  144. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
  145. SE050_HASH_Context se050Ctx;
  146. #endif
  147. #if defined(WOLFSSL_HASH_KEEP)
  148. byte* msg;
  149. word32 used;
  150. word32 len;
  151. #endif
  152. #ifdef WOLF_CRYPTO_CB
  153. int devId;
  154. void* devCtx; /* generic crypto callback context */
  155. #endif
  156. #ifdef WOLFSSL_HASH_FLAGS
  157. word32 flags; /* enum wc_HashFlags in hash.h */
  158. #endif
  159. #ifdef WOLFSSL_IMXRT1170_CAAM
  160. caam_hash_ctx_t ctx;
  161. caam_handle_t hndl;
  162. #endif
  163. #ifdef HAVE_ARIA
  164. MC_HSESSION hSession;
  165. #endif
  166. #endif /* WOLFSSL_PSOC6_CRYPTO */
  167. };
  168. #ifndef WC_SHA512_TYPE_DEFINED
  169. typedef struct wc_Sha512 wc_Sha512;
  170. typedef struct wc_Sha512 wc_Sha512_224;
  171. typedef struct wc_Sha512 wc_Sha512_256;
  172. #define WC_SHA512_TYPE_DEFINED
  173. #endif
  174. #endif /* WOLFSSL_IMX6_CAAM && !WOLFSSL_QNX_CAAM */
  175. #endif /* HAVE_FIPS */
  176. #ifdef WOLFSSL_SHA512
  177. #ifdef WOLFSSL_ARMASM
  178. #ifdef __aarch64__
  179. #ifndef WOLFSSL_ARMASM_CRYPTO_SHA512
  180. void Transform_Sha512_Len_neon(wc_Sha512* sha512, const byte* data,
  181. word32 len);
  182. #define Transform_Sha512_Len Transform_Sha512_Len_neon
  183. #else
  184. void Transform_Sha512_Len_crypto(wc_Sha512* sha512, const byte* data,
  185. word32 len);
  186. #define Transform_Sha512_Len Transform_Sha512_Len_crypto
  187. #endif
  188. #else
  189. extern void Transform_Sha512_Len(wc_Sha512* sha512, const byte* data,
  190. word32 len);
  191. #endif
  192. #endif
  193. WOLFSSL_API int wc_InitSha512(wc_Sha512* sha);
  194. WOLFSSL_API int wc_InitSha512_ex(wc_Sha512* sha, void* heap, int devId);
  195. WOLFSSL_API int wc_Sha512Update(wc_Sha512* sha, const byte* data, word32 len);
  196. WOLFSSL_API int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash);
  197. WOLFSSL_API int wc_Sha512Final(wc_Sha512* sha512, byte* hash);
  198. WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha);
  199. WOLFSSL_API int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash);
  200. WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst);
  201. #if defined(WOLFSSL_HASH_KEEP)
  202. WOLFSSL_API int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz);
  203. #endif
  204. #ifdef WOLFSSL_HASH_FLAGS
  205. WOLFSSL_API int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags);
  206. WOLFSSL_API int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags);
  207. #endif
  208. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  209. WOLFSSL_API int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data);
  210. #endif
  211. #if !defined(WOLFSSL_NOSHA512_224) && \
  212. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  213. WOLFSSL_API int wc_InitSha512_224(wc_Sha512* sha);
  214. WOLFSSL_API int wc_InitSha512_224_ex(wc_Sha512* sha, void* heap, int devId);
  215. WOLFSSL_API int wc_Sha512_224Update(wc_Sha512* sha, const byte* data, word32 len);
  216. WOLFSSL_API int wc_Sha512_224FinalRaw(wc_Sha512* sha512, byte* hash);
  217. WOLFSSL_API int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash);
  218. WOLFSSL_API void wc_Sha512_224Free(wc_Sha512* sha);
  219. WOLFSSL_API int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash);
  220. WOLFSSL_API int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst);
  221. #ifdef WOLFSSL_HASH_FLAGS
  222. WOLFSSL_API int wc_Sha512_224SetFlags(wc_Sha512* sha512, word32 flags);
  223. WOLFSSL_API int wc_Sha512_224GetFlags(wc_Sha512* sha512, word32* flags);
  224. #endif
  225. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  226. WOLFSSL_API int wc_Sha512_224Transform(wc_Sha512* sha,
  227. const unsigned char* data);
  228. #endif /* OPENSSL_EXTRA */
  229. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  230. #if !defined(WOLFSSL_NOSHA512_256) && \
  231. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  232. WOLFSSL_API int wc_InitSha512_256(wc_Sha512* sha);
  233. WOLFSSL_API int wc_InitSha512_256_ex(wc_Sha512* sha, void* heap, int devId);
  234. WOLFSSL_API int wc_Sha512_256Update(wc_Sha512* sha, const byte* data, word32 len);
  235. WOLFSSL_API int wc_Sha512_256FinalRaw(wc_Sha512* sha512, byte* hash);
  236. WOLFSSL_API int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash);
  237. WOLFSSL_API void wc_Sha512_256Free(wc_Sha512* sha);
  238. WOLFSSL_API int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash);
  239. WOLFSSL_API int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst);
  240. #ifdef WOLFSSL_HASH_FLAGS
  241. WOLFSSL_API int wc_Sha512_256SetFlags(wc_Sha512* sha512, word32 flags);
  242. WOLFSSL_API int wc_Sha512_256GetFlags(wc_Sha512* sha512, word32* flags);
  243. #endif
  244. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  245. WOLFSSL_API int wc_Sha512_256Transform(wc_Sha512* sha,
  246. const unsigned char* data);
  247. #endif /* OPENSSL_EXTRA */
  248. #endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */
  249. #endif /* WOLFSSL_SHA512 */
  250. #if defined(WOLFSSL_SHA384)
  251. /* avoid redefinition of structs */
  252. #if !defined(HAVE_FIPS) || \
  253. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  254. #if !defined(NO_OLD_SHA_NAMES)
  255. #define SHA384 WC_SHA384
  256. #endif
  257. #if !defined(NO_OLD_WC_NAMES)
  258. #define Sha384 wc_Sha384
  259. #define SHA384_BLOCK_SIZE WC_SHA384_BLOCK_SIZE
  260. #define SHA384_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
  261. #define SHA384_PAD_SIZE WC_SHA384_PAD_SIZE
  262. #endif
  263. /* in bytes */
  264. enum {
  265. WC_SHA384 = WC_HASH_TYPE_SHA384,
  266. WC_SHA384_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
  267. WC_SHA384_DIGEST_SIZE = 48,
  268. WC_SHA384_PAD_SIZE = WC_SHA512_PAD_SIZE
  269. };
  270. #ifndef WC_SHA384_TYPE_DEFINED
  271. typedef struct wc_Sha512 wc_Sha384;
  272. #define WC_SHA384_TYPE_DEFINED
  273. #endif
  274. #endif /* HAVE_FIPS */
  275. WOLFSSL_API int wc_InitSha384(wc_Sha384* sha);
  276. WOLFSSL_API int wc_InitSha384_ex(wc_Sha384* sha, void* heap, int devId);
  277. WOLFSSL_API int wc_Sha384Update(wc_Sha384* sha, const byte* data, word32 len);
  278. WOLFSSL_API int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash);
  279. WOLFSSL_API int wc_Sha384Final(wc_Sha384* sha384, byte* hash);
  280. WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha);
  281. WOLFSSL_API int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash);
  282. WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst);
  283. #if defined(WOLFSSL_HASH_KEEP)
  284. WOLFSSL_API int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz);
  285. #endif
  286. #ifdef WOLFSSL_HASH_FLAGS
  287. WOLFSSL_API int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags);
  288. WOLFSSL_API int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags);
  289. #endif
  290. #endif /* WOLFSSL_SHA384 */
  291. #ifdef __cplusplus
  292. } /* extern "C" */
  293. #endif
  294. #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */
  295. #endif /* WOLF_CRYPT_SHA512_H */