wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers random.h Source File

random.h

00001 /* random.h
00002  *
00003  * Copyright (C) 2006-2016 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 
00024 #ifndef WOLF_CRYPT_RANDOM_H
00025 #define WOLF_CRYPT_RANDOM_H
00026 
00027 #include <wolfssl/wolfcrypt/types.h>
00028 
00029 #ifdef HAVE_FIPS
00030 /* for fips @wc_fips */
00031 #include <cyassl/ctaocrypt/random.h>
00032 #endif
00033 
00034 #ifdef __cplusplus
00035     extern "C" {
00036 #endif
00037 
00038  /* Maximum generate block length */
00039 #ifndef RNG_MAX_BLOCK_LEN
00040     #define RNG_MAX_BLOCK_LEN (0x10000)
00041 #endif
00042 
00043 /* Size of the BRBG seed */
00044 #ifndef DRBG_SEED_LEN
00045     #define DRBG_SEED_LEN (440/8)
00046 #endif
00047 
00048 
00049 #if defined(CUSTOM_RAND_GENERATE) && !defined(CUSTOM_RAND_TYPE)
00050     /* To maintain compatibility the default is byte */
00051     #define CUSTOM_RAND_TYPE    byte
00052 #endif
00053 
00054 /* make sure Hash DRBG is enabled, unless WC_NO_HASHDRBG is defined
00055     or CUSTOM_RAND_GENERATE_BLOCK is defined*/
00056 #if !defined(WC_NO_HASHDRBG) || !defined(CUSTOM_RAND_GENERATE_BLOCK)
00057     #undef  HAVE_HASHDRBG
00058     #define HAVE_HASHDRBG
00059 #endif
00060 
00061 
00062 #ifndef HAVE_FIPS /* avoid redefining structs and macros */
00063 
00064 /* RNG supports the following sources (in order):
00065  * 1. CUSTOM_RAND_GENERATE_BLOCK: Defines name of function as RNG source and
00066  *     bypasses the options below.
00067  * 2. HAVE_INTEL_RDRAND: Uses the Intel RDRAND if supported by CPU.
00068  * 3. HAVE_HASHDRBG (requires SHA256 enabled): Uses SHA256 based P-RNG
00069  *     seeded via wc_GenerateSeed. This is the default source.
00070  */
00071 
00072  /* Seed source can be overriden by defining one of these:
00073       CUSTOM_RAND_GENERATE_SEED
00074       CUSTOM_RAND_GENERATE_SEED_OS
00075       CUSTOM_RAND_GENERATE */
00076 
00077 
00078 #if defined(CUSTOM_RAND_GENERATE_BLOCK)
00079     /* To use define the following:
00080      * #define CUSTOM_RAND_GENERATE_BLOCK myRngFunc
00081      * extern int myRngFunc(byte* output, word32 sz);
00082      */
00083 #elif defined(HAVE_HASHDRBG)
00084     #ifdef NO_SHA256
00085         #error "Hash DRBG requires SHA-256."
00086     #endif /* NO_SHA256 */
00087     #include <wolfssl/wolfcrypt/sha256.h>
00088 #elif defined(HAVE_WNR)
00089      /* allow whitewood as direct RNG source using wc_GenerateSeed directly */
00090 #else
00091     #error No RNG source defined!
00092 #endif
00093 
00094 #ifdef HAVE_WNR
00095     #include <wnr.h>
00096 #endif
00097 
00098 #ifdef WOLFSSL_ASYNC_CRYPT
00099     #include <wolfssl/wolfcrypt/async.h>
00100 #endif
00101 
00102 
00103 #if defined(USE_WINDOWS_API)
00104     #if defined(_WIN64)
00105         typedef unsigned __int64 ProviderHandle;
00106         /* type HCRYPTPROV, avoid #include <windows.h> */
00107     #else
00108         typedef unsigned long ProviderHandle;
00109     #endif
00110 #endif
00111 
00112 
00113 /* OS specific seeder */
00114 typedef struct OS_Seed {
00115     #if defined(USE_WINDOWS_API)
00116         ProviderHandle handle;
00117     #else
00118         int fd;
00119     #endif
00120 } OS_Seed;
00121 
00122 
00123 #ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */
00124     typedef struct WC_RNG WC_RNG;
00125     #define WC_RNG_TYPE_DEFINED
00126 #endif
00127 
00128 #ifdef HAVE_HASHDRBG
00129     /* Private DRBG state */
00130     struct DRBG;
00131 #endif
00132 
00133 /* RNG context */
00134 struct WC_RNG {
00135     OS_Seed seed;
00136     void* heap;
00137 #ifdef HAVE_HASHDRBG
00138     /* Hash-based Deterministic Random Bit Generator */
00139     struct DRBG* drbg;
00140     byte status;
00141 #endif
00142 #ifdef WOLFSSL_ASYNC_CRYPT
00143     WC_ASYNC_DEV asyncDev;
00144     int devId;
00145 #endif
00146 };
00147 
00148 #endif /* HAVE_FIPS */
00149 
00150 /* NO_OLD_RNGNAME removes RNG struct name to prevent possible type conflicts,
00151  * can't be used with CTaoCrypt FIPS */
00152 #if !defined(NO_OLD_RNGNAME) && !defined(HAVE_FIPS)
00153     #define RNG WC_RNG
00154 #endif
00155 
00156 
00157 WOLFSSL_LOCAL
00158 int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
00159 
00160 
00161 #ifdef HAVE_WNR
00162     /* Whitewood netRandom client library */
00163     WOLFSSL_API int  wc_InitNetRandom(const char*, wnr_hmac_key, int);
00164     WOLFSSL_API int  wc_FreeNetRandom(void);
00165 #endif /* HAVE_WNR */
00166 
00167 
00168 WOLFSSL_API int  wc_InitRng(WC_RNG*);
00169 WOLFSSL_API int  wc_InitRng_ex(WC_RNG* rng, void* heap, int devId);
00170 WOLFSSL_API int  wc_RNG_GenerateBlock(WC_RNG*, byte*, word32 sz);
00171 WOLFSSL_API int  wc_RNG_GenerateByte(WC_RNG*, byte*);
00172 WOLFSSL_API int  wc_FreeRng(WC_RNG*);
00173 
00174 
00175 #ifdef HAVE_HASHDRBG
00176     WOLFSSL_API int wc_RNG_HealthTest(int reseed,
00177                                         const byte* entropyA, word32 entropyASz,
00178                                         const byte* entropyB, word32 entropyBSz,
00179                                         byte* output, word32 outputSz);
00180 #endif /* HAVE_HASHDRBG */
00181 
00182 #ifdef __cplusplus
00183     } /* extern "C" */
00184 #endif
00185 
00186 #endif /* WOLF_CRYPT_RANDOM_H */
00187 
00188