random.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* random.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/random.h
  23. */
  24. #ifndef WOLF_CRYPT_RANDOM_H
  25. #define WOLF_CRYPT_RANDOM_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #if defined(HAVE_FIPS) && \
  28. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  29. #include <wolfssl/wolfcrypt/fips.h>
  30. #endif /* HAVE_FIPS_VERSION >= 2 */
  31. /* included for fips @wc_fips */
  32. #if defined(HAVE_FIPS) && \
  33. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  34. #include <cyassl/ctaocrypt/random.h>
  35. #endif
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /* Maximum generate block length */
  40. #ifndef RNG_MAX_BLOCK_LEN
  41. #ifdef HAVE_INTEL_QA
  42. #define RNG_MAX_BLOCK_LEN (0xFFFFl)
  43. #else
  44. #define RNG_MAX_BLOCK_LEN (0x10000l)
  45. #endif
  46. #endif
  47. /* Size of the BRBG seed */
  48. #ifndef DRBG_SEED_LEN
  49. #define DRBG_SEED_LEN (440/8)
  50. #endif
  51. #if !defined(CUSTOM_RAND_TYPE)
  52. /* To maintain compatibility the default is byte */
  53. #define CUSTOM_RAND_TYPE byte
  54. #endif
  55. /* make sure Hash DRBG is enabled, unless WC_NO_HASHDRBG is defined
  56. or CUSTOM_RAND_GENERATE_BLOCK is defined */
  57. #if !defined(WC_NO_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  58. #undef HAVE_HASHDRBG
  59. #define HAVE_HASHDRBG
  60. #ifndef WC_RESEED_INTERVAL
  61. #define WC_RESEED_INTERVAL (1000000)
  62. #endif
  63. #endif
  64. /* avoid redefinition of structs */
  65. #if !defined(HAVE_FIPS) || \
  66. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  67. /* RNG supports the following sources (in order):
  68. * 1. CUSTOM_RAND_GENERATE_BLOCK: Defines name of function as RNG source and
  69. * bypasses the options below.
  70. * 2. HAVE_INTEL_RDRAND: Uses the Intel RDRAND if supported by CPU.
  71. * 3. HAVE_HASHDRBG (requires SHA256 enabled): Uses SHA256 based P-RNG
  72. * seeded via wc_GenerateSeed. This is the default source.
  73. */
  74. /* Seed source can be overridden by defining one of these:
  75. CUSTOM_RAND_GENERATE_SEED
  76. CUSTOM_RAND_GENERATE_SEED_OS
  77. CUSTOM_RAND_GENERATE */
  78. #if defined(CUSTOM_RAND_GENERATE_BLOCK)
  79. /* To use define the following:
  80. * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc
  81. * extern int myRngFunc(byte* output, word32 sz);
  82. */
  83. #if defined(CUSTOM_RAND_GENERATE_BLOCK) && defined(WOLFSSL_KCAPI)
  84. #undef CUSTOM_RAND_GENERATE_BLOCK
  85. #define CUSTOM_RAND_GENERATE_BLOCK wc_hwrng_generate_block
  86. WOLFSSL_LOCAL int wc_hwrng_generate_block(byte *output, word32 sz);
  87. #endif
  88. #elif defined(HAVE_HASHDRBG)
  89. #ifdef NO_SHA256
  90. #error "Hash DRBG requires SHA-256."
  91. #endif /* NO_SHA256 */
  92. #include <wolfssl/wolfcrypt/sha256.h>
  93. #elif defined(HAVE_WNR)
  94. /* allow whitewood as direct RNG source using wc_GenerateSeed directly */
  95. #elif defined(HAVE_INTEL_RDRAND)
  96. /* Intel RDRAND or RDSEED */
  97. #elif !defined(WC_NO_RNG)
  98. #error No RNG source defined!
  99. #endif
  100. #ifdef HAVE_WNR
  101. #include <wnr.h>
  102. #endif
  103. #ifdef WOLFSSL_ASYNC_CRYPT
  104. #include <wolfssl/wolfcrypt/async.h>
  105. #endif
  106. #if defined(USE_WINDOWS_API)
  107. #if defined(_WIN64)
  108. typedef unsigned __int64 ProviderHandle;
  109. /* type HCRYPTPROV, avoid #include <windows.h> */
  110. #else
  111. typedef unsigned long ProviderHandle;
  112. #endif
  113. #endif
  114. #ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */
  115. typedef struct OS_Seed OS_Seed;
  116. typedef struct WC_RNG WC_RNG;
  117. #ifdef WC_RNG_SEED_CB
  118. typedef int (*wc_RngSeed_Cb)(OS_Seed* os, byte* seed, word32 sz);
  119. #endif
  120. #define WC_RNG_TYPE_DEFINED
  121. #endif
  122. /* OS specific seeder */
  123. struct OS_Seed {
  124. #if defined(USE_WINDOWS_API)
  125. ProviderHandle handle;
  126. #else
  127. int fd;
  128. #endif
  129. #if defined(WOLF_CRYPTO_CB)
  130. int devId;
  131. #endif
  132. };
  133. #ifdef HAVE_HASHDRBG
  134. struct DRBG_internal {
  135. word32 reseedCtr;
  136. word32 lastBlock;
  137. byte V[DRBG_SEED_LEN];
  138. byte C[DRBG_SEED_LEN];
  139. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  140. void* heap;
  141. int devId;
  142. #endif
  143. byte matchCount;
  144. #ifdef WOLFSSL_SMALL_STACK_CACHE
  145. wc_Sha256 sha256;
  146. #endif
  147. };
  148. #endif
  149. /* RNG context */
  150. struct WC_RNG {
  151. struct OS_Seed seed;
  152. void* heap;
  153. #ifdef HAVE_HASHDRBG
  154. /* Hash-based Deterministic Random Bit Generator */
  155. struct DRBG* drbg;
  156. #if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
  157. struct DRBG_internal drbg_data;
  158. #endif
  159. byte status;
  160. #endif
  161. #ifdef WOLFSSL_ASYNC_CRYPT
  162. WC_ASYNC_DEV asyncDev;
  163. #endif
  164. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  165. int devId;
  166. #endif
  167. };
  168. #endif /* NO FIPS or have FIPS v2*/
  169. /* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts,
  170. * can't be used with CTaoCrypt FIPS */
  171. #if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS)
  172. #define RNG WC_RNG
  173. #endif
  174. WOLFSSL_API int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
  175. #ifdef HAVE_WNR
  176. /* Whitewood netRandom client library */
  177. WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int);
  178. WOLFSSL_API int wc_FreeNetRandom(void);
  179. #endif /* HAVE_WNR */
  180. WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte* nonce, word32 nonceSz, void* heap);
  181. WOLFSSL_ABI WOLFSSL_API void wc_rng_free(WC_RNG* rng);
  182. #ifndef WC_NO_RNG
  183. WOLFSSL_ABI WOLFSSL_API int wc_InitRng(WC_RNG* rng);
  184. WOLFSSL_API int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId);
  185. WOLFSSL_API int wc_InitRngNonce(WC_RNG* rng, byte* nonce, word32 nonceSz);
  186. WOLFSSL_API int wc_InitRngNonce_ex(WC_RNG* rng, byte* nonce, word32 nonceSz,
  187. void* heap, int devId);
  188. WOLFSSL_ABI WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG* rng, byte* b, word32 sz);
  189. WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG* rng, byte* b);
  190. WOLFSSL_API int wc_FreeRng(WC_RNG* rng);
  191. #else
  192. #include <wolfssl/wolfcrypt/error-crypt.h>
  193. #define wc_InitRng(rng) NOT_COMPILED_IN
  194. #define wc_InitRng_ex(rng, h, d) NOT_COMPILED_IN
  195. #define wc_InitRngNonce(rng, n, s) NOT_COMPILED_IN
  196. #define wc_InitRngNonce_ex(rng, n, s, h, d) NOT_COMPILED_IN
  197. #if defined(__ghs__) || defined(WC_NO_RNG_SIMPLE)
  198. /* some older compilers do not like macro function in expression */
  199. #define wc_RNG_GenerateBlock(rng, b, s) NOT_COMPILED_IN
  200. #else
  201. #define wc_RNG_GenerateBlock(rng, b, s) ({(void)rng; (void)b; (void)s; NOT_COMPILED_IN;})
  202. #endif
  203. #define wc_RNG_GenerateByte(rng, b) NOT_COMPILED_IN
  204. #define wc_FreeRng(rng) (void)NOT_COMPILED_IN
  205. #endif
  206. #ifdef WC_RNG_SEED_CB
  207. WOLFSSL_API int wc_SetSeed_Cb(wc_RngSeed_Cb cb);
  208. #endif
  209. #ifdef HAVE_HASHDRBG
  210. WOLFSSL_LOCAL int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* entropy,
  211. word32 entropySz);
  212. WOLFSSL_API int wc_RNG_TestSeed(const byte* seed, word32 seedSz);
  213. WOLFSSL_API int wc_RNG_HealthTest(int reseed,
  214. const byte* entropyA, word32 entropyASz,
  215. const byte* entropyB, word32 entropyBSz,
  216. byte* output, word32 outputSz);
  217. WOLFSSL_API int wc_RNG_HealthTest_ex(int reseed,
  218. const byte* nonce, word32 nonceSz,
  219. const byte* entropyA, word32 entropyASz,
  220. const byte* entropyB, word32 entropyBSz,
  221. byte* output, word32 outputSz,
  222. void* heap, int devId);
  223. #endif /* HAVE_HASHDRBG */
  224. #ifdef __cplusplus
  225. } /* extern "C" */
  226. #endif
  227. #endif /* WOLF_CRYPT_RANDOM_H */