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