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