random.h 8.3 KB

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