wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfssl/wolfcrypt/random.h@13:80fb167dafdf, 2017-05-30 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue May 30 06:16:19 2017 +0000
- Revision:
- 13:80fb167dafdf
- Parent:
- 11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 11:cee25a834751 | 1 | /* random.h |
wolfSSL | 11:cee25a834751 | 2 | * |
wolfSSL | 11:cee25a834751 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 11:cee25a834751 | 4 | * |
wolfSSL | 11:cee25a834751 | 5 | * This file is part of wolfSSL. |
wolfSSL | 11:cee25a834751 | 6 | * |
wolfSSL | 11:cee25a834751 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 11:cee25a834751 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 11:cee25a834751 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 11:cee25a834751 | 10 | * (at your option) any later version. |
wolfSSL | 11:cee25a834751 | 11 | * |
wolfSSL | 11:cee25a834751 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 11:cee25a834751 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 11:cee25a834751 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 11:cee25a834751 | 15 | * GNU General Public License for more details. |
wolfSSL | 11:cee25a834751 | 16 | * |
wolfSSL | 11:cee25a834751 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 11:cee25a834751 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 11:cee25a834751 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 11:cee25a834751 | 20 | */ |
wolfSSL | 11:cee25a834751 | 21 | |
wolfSSL | 11:cee25a834751 | 22 | |
wolfSSL | 11:cee25a834751 | 23 | |
wolfSSL | 11:cee25a834751 | 24 | #ifndef WOLF_CRYPT_RANDOM_H |
wolfSSL | 11:cee25a834751 | 25 | #define WOLF_CRYPT_RANDOM_H |
wolfSSL | 11:cee25a834751 | 26 | |
wolfSSL | 11:cee25a834751 | 27 | #include <wolfssl/wolfcrypt/types.h> |
wolfSSL | 11:cee25a834751 | 28 | |
wolfSSL | 11:cee25a834751 | 29 | #ifdef HAVE_FIPS |
wolfSSL | 11:cee25a834751 | 30 | /* for fips @wc_fips */ |
wolfSSL | 11:cee25a834751 | 31 | #include <cyassl/ctaocrypt/random.h> |
wolfSSL | 11:cee25a834751 | 32 | #endif |
wolfSSL | 11:cee25a834751 | 33 | |
wolfSSL | 11:cee25a834751 | 34 | #ifdef __cplusplus |
wolfSSL | 11:cee25a834751 | 35 | extern "C" { |
wolfSSL | 11:cee25a834751 | 36 | #endif |
wolfSSL | 11:cee25a834751 | 37 | |
wolfSSL | 11:cee25a834751 | 38 | /* Maximum generate block length */ |
wolfSSL | 11:cee25a834751 | 39 | #ifndef RNG_MAX_BLOCK_LEN |
wolfSSL | 11:cee25a834751 | 40 | #define RNG_MAX_BLOCK_LEN (0x10000) |
wolfSSL | 11:cee25a834751 | 41 | #endif |
wolfSSL | 11:cee25a834751 | 42 | |
wolfSSL | 11:cee25a834751 | 43 | /* Size of the BRBG seed */ |
wolfSSL | 11:cee25a834751 | 44 | #ifndef DRBG_SEED_LEN |
wolfSSL | 11:cee25a834751 | 45 | #define DRBG_SEED_LEN (440/8) |
wolfSSL | 11:cee25a834751 | 46 | #endif |
wolfSSL | 11:cee25a834751 | 47 | |
wolfSSL | 11:cee25a834751 | 48 | |
wolfSSL | 11:cee25a834751 | 49 | #if defined(CUSTOM_RAND_GENERATE) && !defined(CUSTOM_RAND_TYPE) |
wolfSSL | 11:cee25a834751 | 50 | /* To maintain compatibility the default is byte */ |
wolfSSL | 11:cee25a834751 | 51 | #define CUSTOM_RAND_TYPE byte |
wolfSSL | 11:cee25a834751 | 52 | #endif |
wolfSSL | 11:cee25a834751 | 53 | |
wolfSSL | 11:cee25a834751 | 54 | /* make sure Hash DRBG is enabled, unless WC_NO_HASHDRBG is defined |
wolfSSL | 11:cee25a834751 | 55 | or CUSTOM_RAND_GENERATE_BLOCK is defined*/ |
wolfSSL | 11:cee25a834751 | 56 | #if !defined(WC_NO_HASHDRBG) || !defined(CUSTOM_RAND_GENERATE_BLOCK) |
wolfSSL | 11:cee25a834751 | 57 | #undef HAVE_HASHDRBG |
wolfSSL | 11:cee25a834751 | 58 | #define HAVE_HASHDRBG |
wolfSSL | 11:cee25a834751 | 59 | #endif |
wolfSSL | 11:cee25a834751 | 60 | |
wolfSSL | 11:cee25a834751 | 61 | |
wolfSSL | 11:cee25a834751 | 62 | #ifndef HAVE_FIPS /* avoid redefining structs and macros */ |
wolfSSL | 11:cee25a834751 | 63 | |
wolfSSL | 11:cee25a834751 | 64 | /* RNG supports the following sources (in order): |
wolfSSL | 11:cee25a834751 | 65 | * 1. CUSTOM_RAND_GENERATE_BLOCK: Defines name of function as RNG source and |
wolfSSL | 11:cee25a834751 | 66 | * bypasses the options below. |
wolfSSL | 11:cee25a834751 | 67 | * 2. HAVE_INTEL_RDRAND: Uses the Intel RDRAND if supported by CPU. |
wolfSSL | 11:cee25a834751 | 68 | * 3. HAVE_HASHDRBG (requires SHA256 enabled): Uses SHA256 based P-RNG |
wolfSSL | 11:cee25a834751 | 69 | * seeded via wc_GenerateSeed. This is the default source. |
wolfSSL | 11:cee25a834751 | 70 | */ |
wolfSSL | 11:cee25a834751 | 71 | |
wolfSSL | 11:cee25a834751 | 72 | /* Seed source can be overriden by defining one of these: |
wolfSSL | 11:cee25a834751 | 73 | CUSTOM_RAND_GENERATE_SEED |
wolfSSL | 11:cee25a834751 | 74 | CUSTOM_RAND_GENERATE_SEED_OS |
wolfSSL | 11:cee25a834751 | 75 | CUSTOM_RAND_GENERATE */ |
wolfSSL | 11:cee25a834751 | 76 | |
wolfSSL | 11:cee25a834751 | 77 | |
wolfSSL | 11:cee25a834751 | 78 | #if defined(CUSTOM_RAND_GENERATE_BLOCK) |
wolfSSL | 11:cee25a834751 | 79 | /* To use define the following: |
wolfSSL | 11:cee25a834751 | 80 | * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc |
wolfSSL | 11:cee25a834751 | 81 | * extern int myRngFunc(byte* output, word32 sz); |
wolfSSL | 11:cee25a834751 | 82 | */ |
wolfSSL | 11:cee25a834751 | 83 | #elif defined(HAVE_HASHDRBG) |
wolfSSL | 11:cee25a834751 | 84 | #ifdef NO_SHA256 |
wolfSSL | 11:cee25a834751 | 85 | #error "Hash DRBG requires SHA-256." |
wolfSSL | 11:cee25a834751 | 86 | #endif /* NO_SHA256 */ |
wolfSSL | 11:cee25a834751 | 87 | #include <wolfssl/wolfcrypt/sha256.h> |
wolfSSL | 11:cee25a834751 | 88 | #elif defined(HAVE_WNR) |
wolfSSL | 11:cee25a834751 | 89 | /* allow whitewood as direct RNG source using wc_GenerateSeed directly */ |
wolfSSL | 11:cee25a834751 | 90 | #else |
wolfSSL | 11:cee25a834751 | 91 | #error No RNG source defined! |
wolfSSL | 11:cee25a834751 | 92 | #endif |
wolfSSL | 11:cee25a834751 | 93 | |
wolfSSL | 11:cee25a834751 | 94 | #ifdef HAVE_WNR |
wolfSSL | 11:cee25a834751 | 95 | #include <wnr.h> |
wolfSSL | 11:cee25a834751 | 96 | #endif |
wolfSSL | 11:cee25a834751 | 97 | |
wolfSSL | 11:cee25a834751 | 98 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 11:cee25a834751 | 99 | #include <wolfssl/wolfcrypt/async.h> |
wolfSSL | 11:cee25a834751 | 100 | #endif |
wolfSSL | 11:cee25a834751 | 101 | |
wolfSSL | 11:cee25a834751 | 102 | |
wolfSSL | 11:cee25a834751 | 103 | #if defined(USE_WINDOWS_API) |
wolfSSL | 11:cee25a834751 | 104 | #if defined(_WIN64) |
wolfSSL | 11:cee25a834751 | 105 | typedef unsigned __int64 ProviderHandle; |
wolfSSL | 11:cee25a834751 | 106 | /* type HCRYPTPROV, avoid #include <windows.h> */ |
wolfSSL | 11:cee25a834751 | 107 | #else |
wolfSSL | 11:cee25a834751 | 108 | typedef unsigned long ProviderHandle; |
wolfSSL | 11:cee25a834751 | 109 | #endif |
wolfSSL | 11:cee25a834751 | 110 | #endif |
wolfSSL | 11:cee25a834751 | 111 | |
wolfSSL | 11:cee25a834751 | 112 | |
wolfSSL | 11:cee25a834751 | 113 | /* OS specific seeder */ |
wolfSSL | 11:cee25a834751 | 114 | typedef struct OS_Seed { |
wolfSSL | 11:cee25a834751 | 115 | #if defined(USE_WINDOWS_API) |
wolfSSL | 11:cee25a834751 | 116 | ProviderHandle handle; |
wolfSSL | 11:cee25a834751 | 117 | #else |
wolfSSL | 11:cee25a834751 | 118 | int fd; |
wolfSSL | 11:cee25a834751 | 119 | #endif |
wolfSSL | 11:cee25a834751 | 120 | } OS_Seed; |
wolfSSL | 11:cee25a834751 | 121 | |
wolfSSL | 11:cee25a834751 | 122 | |
wolfSSL | 11:cee25a834751 | 123 | #ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */ |
wolfSSL | 11:cee25a834751 | 124 | typedef struct WC_RNG WC_RNG; |
wolfSSL | 11:cee25a834751 | 125 | #define WC_RNG_TYPE_DEFINED |
wolfSSL | 11:cee25a834751 | 126 | #endif |
wolfSSL | 11:cee25a834751 | 127 | |
wolfSSL | 11:cee25a834751 | 128 | #ifdef HAVE_HASHDRBG |
wolfSSL | 11:cee25a834751 | 129 | /* Private DRBG state */ |
wolfSSL | 11:cee25a834751 | 130 | struct DRBG; |
wolfSSL | 11:cee25a834751 | 131 | #endif |
wolfSSL | 11:cee25a834751 | 132 | |
wolfSSL | 11:cee25a834751 | 133 | /* RNG context */ |
wolfSSL | 11:cee25a834751 | 134 | struct WC_RNG { |
wolfSSL | 11:cee25a834751 | 135 | OS_Seed seed; |
wolfSSL | 11:cee25a834751 | 136 | void* heap; |
wolfSSL | 11:cee25a834751 | 137 | #ifdef HAVE_HASHDRBG |
wolfSSL | 11:cee25a834751 | 138 | /* Hash-based Deterministic Random Bit Generator */ |
wolfSSL | 11:cee25a834751 | 139 | struct DRBG* drbg; |
wolfSSL | 11:cee25a834751 | 140 | byte status; |
wolfSSL | 11:cee25a834751 | 141 | #endif |
wolfSSL | 11:cee25a834751 | 142 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 11:cee25a834751 | 143 | WC_ASYNC_DEV asyncDev; |
wolfSSL | 11:cee25a834751 | 144 | int devId; |
wolfSSL | 11:cee25a834751 | 145 | #endif |
wolfSSL | 11:cee25a834751 | 146 | }; |
wolfSSL | 11:cee25a834751 | 147 | |
wolfSSL | 11:cee25a834751 | 148 | #endif /* HAVE_FIPS */ |
wolfSSL | 11:cee25a834751 | 149 | |
wolfSSL | 11:cee25a834751 | 150 | /* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts, |
wolfSSL | 11:cee25a834751 | 151 | * can't be used with CTaoCrypt FIPS */ |
wolfSSL | 11:cee25a834751 | 152 | #if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS) |
wolfSSL | 11:cee25a834751 | 153 | #define RNG WC_RNG |
wolfSSL | 11:cee25a834751 | 154 | #endif |
wolfSSL | 11:cee25a834751 | 155 | |
wolfSSL | 11:cee25a834751 | 156 | |
wolfSSL | 11:cee25a834751 | 157 | WOLFSSL_LOCAL |
wolfSSL | 11:cee25a834751 | 158 | int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz); |
wolfSSL | 11:cee25a834751 | 159 | |
wolfSSL | 11:cee25a834751 | 160 | |
wolfSSL | 11:cee25a834751 | 161 | #ifdef HAVE_WNR |
wolfSSL | 11:cee25a834751 | 162 | /* Whitewood netRandom client library */ |
wolfSSL | 11:cee25a834751 | 163 | WOLFSSL_API int wc_InitNetRandom(const char*, wnr_hmac_key, int); |
wolfSSL | 11:cee25a834751 | 164 | WOLFSSL_API int wc_FreeNetRandom(void); |
wolfSSL | 11:cee25a834751 | 165 | #endif /* HAVE_WNR */ |
wolfSSL | 11:cee25a834751 | 166 | |
wolfSSL | 11:cee25a834751 | 167 | |
wolfSSL | 11:cee25a834751 | 168 | WOLFSSL_API int wc_InitRng(WC_RNG*); |
wolfSSL | 11:cee25a834751 | 169 | WOLFSSL_API int wc_InitRng_ex(WC_RNG* rng, void* heap, int devId); |
wolfSSL | 11:cee25a834751 | 170 | WOLFSSL_API int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz); |
wolfSSL | 11:cee25a834751 | 171 | WOLFSSL_API int wc_RNG_GenerateByte(WC_RNG*, byte*); |
wolfSSL | 11:cee25a834751 | 172 | WOLFSSL_API int wc_FreeRng(WC_RNG*); |
wolfSSL | 11:cee25a834751 | 173 | |
wolfSSL | 11:cee25a834751 | 174 | |
wolfSSL | 11:cee25a834751 | 175 | #ifdef HAVE_HASHDRBG |
wolfSSL | 11:cee25a834751 | 176 | WOLFSSL_API int wc_RNG_HealthTest(int reseed, |
wolfSSL | 11:cee25a834751 | 177 | const byte* entropyA, word32 entropyASz, |
wolfSSL | 11:cee25a834751 | 178 | const byte* entropyB, word32 entropyBSz, |
wolfSSL | 11:cee25a834751 | 179 | byte* output, word32 outputSz); |
wolfSSL | 11:cee25a834751 | 180 | #endif /* HAVE_HASHDRBG */ |
wolfSSL | 11:cee25a834751 | 181 | |
wolfSSL | 11:cee25a834751 | 182 | #ifdef __cplusplus |
wolfSSL | 11:cee25a834751 | 183 | } /* extern "C" */ |
wolfSSL | 11:cee25a834751 | 184 | #endif |
wolfSSL | 11:cee25a834751 | 185 | |
wolfSSL | 11:cee25a834751 | 186 | #endif /* WOLF_CRYPT_RANDOM_H */ |
wolfSSL | 11:cee25a834751 | 187 | |
wolfSSL | 11:cee25a834751 | 188 |