Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
random.h
00001 /* random.h 00002 * 00003 * Copyright (C) 2006-2017 wolfSSL Inc. 00004 * 00005 * This file is part of wolfSSL. 00006 * 00007 * wolfSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * wolfSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 00020 */ 00021 00022 /*! 00023 \file wolfssl/wolfcrypt/random.h 00024 */ 00025 00026 00027 00028 #ifndef WOLF_CRYPT_RANDOM_H 00029 #define WOLF_CRYPT_RANDOM_H 00030 00031 #include <wolfcrypt/types.h> 00032 00033 #if defined(HAVE_FIPS) && \ 00034 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 00035 #include <wolfcrypt/fips.h> 00036 #endif /* HAVE_FIPS_VERSION >= 2 */ 00037 00038 /* included for fips @wc_fips */ 00039 #if defined(HAVE_FIPS) && \ 00040 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) 00041 #include <cyassl/ctaocrypt/random.h> 00042 #endif 00043 00044 #ifdef __cplusplus 00045 extern "C" { 00046 #endif 00047 00048 /* Maximum generate block length */ 00049 #ifndef RNG_MAX_BLOCK_LEN 00050 #ifdef HAVE_INTEL_QA 00051 #define RNG_MAX_BLOCK_LEN (0xFFFF) 00052 #else 00053 #define RNG_MAX_BLOCK_LEN (0x10000) 00054 #endif 00055 #endif 00056 00057 /* Size of the BRBG seed */ 00058 #ifndef DRBG_SEED_LEN 00059 #define DRBG_SEED_LEN (440/8) 00060 #endif 00061 00062 00063 #if !defined(CUSTOM_RAND_TYPE) 00064 /* To maintain compatibility the default is byte */ 00065 #define CUSTOM_RAND_TYPE byte 00066 #endif 00067 00068 /* make sure Hash DRBG is enabled, unless WC_NO_HASHDRBG is defined 00069 or CUSTOM_RAND_GENERATE_BLOCK is defined*/ 00070 #if !defined(WC_NO_HASHDRBG) || !defined(CUSTOM_RAND_GENERATE_BLOCK) 00071 #undef HAVE_HASHDRBG 00072 #define HAVE_HASHDRBG 00073 #ifndef WC_RESEED_INTERVAL 00074 #define WC_RESEED_INTERVAL (1000000) 00075 #endif 00076 #endif 00077 00078 00079 /* avoid redefinition of structs */ 00080 #if !defined(HAVE_FIPS) || \ 00081 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 00082 00083 /* RNG supports the following sources (in order): 00084 * 1. CUSTOM_RAND_GENERATE_BLOCK: Defines name of function as RNG source and 00085 * bypasses the options below. 00086 * 2. HAVE_INTEL_RDRAND: Uses the Intel RDRAND if supported by CPU. 00087 * 3. HAVE_HASHDRBG (requires SHA256 enabled): Uses SHA256 based P-RNG 00088 * seeded via wc_GenerateSeed. This is the default source. 00089 */ 00090 00091 /* Seed source can be overriden by defining one of these: 00092 CUSTOM_RAND_GENERATE_SEED 00093 CUSTOM_RAND_GENERATE_SEED_OS 00094 CUSTOM_RAND_GENERATE */ 00095 00096 00097 #if defined(CUSTOM_RAND_GENERATE_BLOCK) 00098 /* To use define the following: 00099 * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc 00100 * extern int myRngFunc(byte* output, word32 sz); 00101 */ 00102 #elif defined(HAVE_HASHDRBG) 00103 #ifdef NO_SHA256 00104 #error "Hash DRBG requires SHA-256." 00105 #endif /* NO_SHA256 */ 00106 #include <wolfcrypt/sha256.h> 00107 #elif defined(HAVE_WNR) 00108 /* allow whitewood as direct RNG source using wc_GenerateSeed directly */ 00109 #else 00110 #error No RNG source defined! 00111 #endif 00112 00113 #ifdef HAVE_WNR 00114 #include <wnr.h> 00115 #endif 00116 00117 #ifdef WOLFSSL_ASYNC_CRYPT 00118 #include <wolfcrypt/async.h> 00119 #endif 00120 00121 00122 #if defined(USE_WINDOWS_API) 00123 #if defined(_WIN64) 00124 typedef unsigned __int64 ProviderHandle; 00125 /* type HCRYPTPROV, avoid #include <windows.h> */ 00126 #else 00127 typedef unsigned long ProviderHandle; 00128 #endif 00129 #endif 00130 00131 00132 /* OS specific seeder */ 00133 typedef struct OS_Seed { 00134 #if defined(USE_WINDOWS_API) 00135 ProviderHandle handle; 00136 #else 00137 int fd; 00138 #endif 00139 } OS_Seed; 00140 00141 00142 #ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */ 00143 typedef struct WC_RNG WC_RNG; 00144 #define WC_RNG_TYPE_DEFINED 00145 #endif 00146 00147 /* RNG context */ 00148 struct WC_RNG { 00149 OS_Seed seed; 00150 void* heap; 00151 #ifdef HAVE_HASHDRBG 00152 /* Hash-based Deterministic Random Bit Generator */ 00153 struct DRBG* drbg; 00154 byte status; 00155 #endif 00156 #ifdef WOLFSSL_ASYNC_CRYPT 00157 WC_ASYNC_DEV asyncDev; 00158 int devId; 00159 #endif 00160 }; 00161 00162 #endif /* NO FIPS or have FIPS v2*/ 00163 00164 /* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts, 00165 * can't be used with CTaoCrypt FIPS */ 00166 #if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS) 00167 #define RNG WC_RNG 00168 #endif 00169 00170 00171 WOLFSSL_LOCAL 00172 int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz); 00173 00174 00175 #ifdef HAVE_WNR 00176 /* Whitewood netRandom client library */ 00177 WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int); 00178 WOLFSSL_API int wc_FreeNetRandom(void); 00179 #endif /* HAVE_WNR */ 00180 00181 00182 WOLFSSL_API int wc_InitRng(WC_RNG*); 00183 WOLFSSL_API int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId); 00184 WOLFSSL_API int wc_InitRngNonce(WC_RNG* rng, byte* nonce, word32 nonceSz); 00185 WOLFSSL_API int wc_InitRngNonce_ex(WC_RNG* rng, byte* nonce, word32 nonceSz, 00186 void* heap, int devId); 00187 WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz); 00188 WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG*, byte*); 00189 WOLFSSL_API int wc_FreeRng(WC_RNG*); 00190 00191 00192 #ifdef HAVE_HASHDRBG 00193 WOLFSSL_LOCAL int wc_RNG_DRBG_Reseed(WC_RNG* rng, const byte* entropy, 00194 word32 entropySz); 00195 WOLFSSL_API int wc_RNG_HealthTest(int reseed, 00196 const byte* entropyA, word32 entropyASz, 00197 const byte* entropyB, word32 entropyBSz, 00198 byte* output, word32 outputSz); 00199 WOLFSSL_API int wc_RNG_HealthTest_ex(int reseed, 00200 const byte* nonce, word32 nonceSz, 00201 const byte* entropyA, word32 entropyASz, 00202 const byte* entropyB, word32 entropyBSz, 00203 byte* output, word32 outputSz, 00204 void* heap, int devId); 00205 #endif /* HAVE_HASHDRBG */ 00206 00207 #ifdef __cplusplus 00208 } /* extern "C" */ 00209 #endif 00210 00211 #endif /* WOLF_CRYPT_RANDOM_H */ 00212 00213
Generated on Tue Jul 12 2022 16:58:06 by
1.7.2