2
0

sha512.h 10 KB

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