random.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* random.h
  2. *
  3. * Copyright (C) 2006-2017 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. #ifndef WOLF_CRYPT_RANDOM_H
  22. #define WOLF_CRYPT_RANDOM_H
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #ifdef HAVE_FIPS
  25. /* for fips @wc_fips */
  26. #include <cyassl/ctaocrypt/random.h>
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Maximum generate block length */
  32. #ifndef RNG_MAX_BLOCK_LEN
  33. #ifdef HAVE_INTEL_QA
  34. #define RNG_MAX_BLOCK_LEN (0xFFFF)
  35. #else
  36. #define RNG_MAX_BLOCK_LEN (0x10000)
  37. #endif
  38. #endif
  39. /* Size of the BRBG seed */
  40. #ifndef DRBG_SEED_LEN
  41. #define DRBG_SEED_LEN (440/8)
  42. #endif
  43. #if !defined(CUSTOM_RAND_TYPE)
  44. /* To maintain compatibility the default is byte */
  45. #define CUSTOM_RAND_TYPE byte
  46. #endif
  47. /* make sure Hash DRBG is enabled, unless WC_NO_HASHDRBG is defined
  48. or CUSTOM_RAND_GENERATE_BLOCK is defined*/
  49. #if !defined(WC_NO_HASHDRBG) || !defined(CUSTOM_RAND_GENERATE_BLOCK)
  50. #undef HAVE_HASHDRBG
  51. #define HAVE_HASHDRBG
  52. #ifndef WC_RESEED_INTERVAL
  53. #define WC_RESEED_INTERVAL (1000000)
  54. #endif
  55. #endif
  56. #ifndef HAVE_FIPS /* avoid redefining structs and macros */
  57. /* RNG supports the following sources (in order):
  58. * 1. CUSTOM_RAND_GENERATE_BLOCK: Defines name of function as RNG source and
  59. * bypasses the options below.
  60. * 2. HAVE_INTEL_RDRAND: Uses the Intel RDRAND if supported by CPU.
  61. * 3. HAVE_HASHDRBG (requires SHA256 enabled): Uses SHA256 based P-RNG
  62. * seeded via wc_GenerateSeed. This is the default source.
  63. */
  64. /* Seed source can be overriden by defining one of these:
  65. CUSTOM_RAND_GENERATE_SEED
  66. CUSTOM_RAND_GENERATE_SEED_OS
  67. CUSTOM_RAND_GENERATE */
  68. #if defined(CUSTOM_RAND_GENERATE_BLOCK)
  69. /* To use define the following:
  70. * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc
  71. * extern int myRngFunc(byte* output, word32 sz);
  72. */
  73. #elif defined(HAVE_HASHDRBG)
  74. #ifdef NO_SHA256
  75. #error "Hash DRBG requires SHA-256."
  76. #endif /* NO_SHA256 */
  77. #include <wolfssl/wolfcrypt/sha256.h>
  78. #elif defined(HAVE_WNR)
  79. /* allow whitewood as direct RNG source using wc_GenerateSeed directly */
  80. #else
  81. #error No RNG source defined!
  82. #endif
  83. #ifdef HAVE_WNR
  84. #include <wnr.h>
  85. #endif
  86. #ifdef WOLFSSL_ASYNC_CRYPT
  87. #include <wolfssl/wolfcrypt/async.h>
  88. #endif
  89. #if defined(USE_WINDOWS_API)
  90. #if defined(_WIN64)
  91. typedef unsigned __int64 ProviderHandle;
  92. /* type HCRYPTPROV, avoid #include <windows.h> */
  93. #else
  94. typedef unsigned long ProviderHandle;
  95. #endif
  96. #endif
  97. /* OS specific seeder */
  98. typedef struct OS_Seed {
  99. #if defined(USE_WINDOWS_API)
  100. ProviderHandle handle;
  101. #else
  102. int fd;
  103. #endif
  104. } OS_Seed;
  105. #ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */
  106. typedef struct WC_RNG WC_RNG;
  107. #define WC_RNG_TYPE_DEFINED
  108. #endif
  109. /* RNG context */
  110. struct WC_RNG {
  111. OS_Seed seed;
  112. void* heap;
  113. #ifdef HAVE_HASHDRBG
  114. /* Hash-based Deterministic Random Bit Generator */
  115. struct DRBG* drbg;
  116. byte status;
  117. #endif
  118. #ifdef WOLFSSL_ASYNC_CRYPT
  119. WC_ASYNC_DEV asyncDev;
  120. int devId;
  121. #endif
  122. };
  123. #endif /* HAVE_FIPS */
  124. /* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts,
  125. * can't be used with CTaoCrypt FIPS */
  126. #if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS)
  127. #define RNG WC_RNG
  128. #endif
  129. WOLFSSL_LOCAL
  130. int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
  131. #ifdef HAVE_WNR
  132. /* Whitewood netRandom client library */
  133. WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int);
  134. WOLFSSL_API int wc_FreeNetRandom(void);
  135. #endif /* HAVE_WNR */
  136. WOLFSSL_API int wc_InitRng(WC_RNG*);
  137. WOLFSSL_API int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId);
  138. WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz);
  139. WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG*, byte*);
  140. WOLFSSL_API int wc_FreeRng(WC_RNG*);
  141. #ifdef HAVE_HASHDRBG
  142. WOLFSSL_LOCAL int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* entropy,
  143. word32 entropySz);
  144. WOLFSSL_API int wc_RNG_HealthTest(int reseed,
  145. const byte* entropyA, word32 entropyASz,
  146. const byte* entropyB, word32 entropyBSz,
  147. byte* output, word32 outputSz);
  148. #endif /* HAVE_HASHDRBG */
  149. #ifdef __cplusplus
  150. } /* extern "C" */
  151. #endif
  152. #endif /* WOLF_CRYPT_RANDOM_H */