source: azure_iot_hub_f767zi/trunk/wolfssl-4.4.0/wolfssl/wolfcrypt/random.h@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 7.4 KB
Line 
1/* random.h
2 *
3 * Copyright (C) 2006-2020 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 (0xFFFFl)
52 #else
53 #define RNG_MAX_BLOCK_LEN (0x10000l)
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 overridden 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 */
135typedef struct OS_Seed {
136 #if defined(USE_WINDOWS_API)
137 ProviderHandle handle;
138 #else
139 int fd;
140 #endif
141 #if defined(WOLF_CRYPTO_CB)
142 int devId;
143 #endif
144} OS_Seed;
145
146
147#ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */
148 typedef struct WC_RNG WC_RNG;
149 #define WC_RNG_TYPE_DEFINED
150#endif
151
152/* RNG context */
153struct WC_RNG {
154 OS_Seed seed;
155 void* heap;
156#ifdef HAVE_HASHDRBG
157 /* Hash-based Deterministic Random Bit Generator */
158 struct DRBG* drbg;
159#if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
160 #define DRBG_STRUCT_SZ ((sizeof(word32)*3) + (DRBG_SEED_LEN*2))
161 #ifdef WOLFSSL_SMALL_STACK_CACHE
162 #define DRBG_STRUCT_SZ_SHA256 (sizeof(wc_Sha256))
163 #else
164 #define DRBG_STRUCT_SZ_SHA256 0
165 #endif
166 #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
167 #define DRBG_STRUCT_SZ_ASYNC (sizeof(void*) + sizeof(int))
168 #else
169 #define DRBG_STRUCT_SZ_ASYNC 0
170 #endif
171 byte drbg_data[DRBG_STRUCT_SZ + DRBG_STRUCT_SZ_SHA256 + DRBG_STRUCT_SZ_ASYNC];
172#endif
173 byte status;
174#endif
175#ifdef WOLFSSL_ASYNC_CRYPT
176 WC_ASYNC_DEV asyncDev;
177#endif
178#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
179 int devId;
180#endif
181};
182
183#endif /* NO FIPS or have FIPS v2*/
184
185/* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts,
186 * can't be used with CTaoCrypt FIPS */
187#if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS)
188 #define RNG WC_RNG
189#endif
190
191
192WOLFSSL_LOCAL
193int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
194
195
196#ifdef HAVE_WNR
197 /* Whitewood netRandom client library */
198 WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int);
199 WOLFSSL_API int wc_FreeNetRandom(void);
200#endif /* HAVE_WNR */
201
202
203WOLFSSL_ABI WOLFSSL_API WC_RNG* wc_rng_new(byte*, word32, void*);
204WOLFSSL_ABI WOLFSSL_API void wc_rng_free(WC_RNG*);
205
206
207#ifndef WC_NO_RNG
208WOLFSSL_API int wc_InitRng(WC_RNG*);
209WOLFSSL_API int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId);
210WOLFSSL_API int wc_InitRngNonce(WC_RNG* rng, byte* nonce, word32 nonceSz);
211WOLFSSL_API int wc_InitRngNonce_ex(WC_RNG* rng, byte* nonce, word32 nonceSz,
212 void* heap, int devId);
213WOLFSSL_ABI WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz);
214WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG*, byte*);
215WOLFSSL_API int wc_FreeRng(WC_RNG*);
216#else
217#include <wolfssl/wolfcrypt/error-crypt.h>
218#define wc_InitRng(rng) NOT_COMPILED_IN
219#define wc_InitRng_ex(rng, h, d) NOT_COMPILED_IN
220#define wc_InitRngNonce(rng, n, s) NOT_COMPILED_IN
221#define wc_InitRngNonce_ex(rng, n, s, h, d) NOT_COMPILED_IN
222#define wc_RNG_GenerateBlock(rng, b, s) NOT_COMPILED_IN
223#define wc_RNG_GenerateByte(rng, b) NOT_COMPILED_IN
224#define wc_FreeRng(rng) (void)NOT_COMPILED_IN
225#endif
226
227
228
229#ifdef HAVE_HASHDRBG
230 WOLFSSL_LOCAL int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* entropy,
231 word32 entropySz);
232 WOLFSSL_API int wc_RNG_TestSeed(const byte* seed, word32 seedSz);
233 WOLFSSL_API int wc_RNG_HealthTest(int reseed,
234 const byte* entropyA, word32 entropyASz,
235 const byte* entropyB, word32 entropyBSz,
236 byte* output, word32 outputSz);
237 WOLFSSL_API int wc_RNG_HealthTest_ex(int reseed,
238 const byte* nonce, word32 nonceSz,
239 const byte* entropyA, word32 entropyASz,
240 const byte* entropyB, word32 entropyBSz,
241 byte* output, word32 outputSz,
242 void* heap, int devId);
243#endif /* HAVE_HASHDRBG */
244
245#ifdef __cplusplus
246 } /* extern "C" */
247#endif
248
249#endif /* WOLF_CRYPT_RANDOM_H */
250
Note: See TracBrowser for help on using the repository browser.