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