3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL 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.
12 * CyaSSL 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.
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-1301, USA
23 #ifndef CTAO_CRYPT_RANDOM_H
24 #define CTAO_CRYPT_RANDOM_H
26 #include <cyassl/ctaocrypt/types.h>
28 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
30 #error "Hash DRBG requires SHA-256."
31 #endif /* NO_SHA256 */
33 #include <cyassl/ctaocrypt/sha256.h>
34 #else /* HAVE_HASHDRBG || NO_RC4 */
35 #include <cyassl/ctaocrypt/arc4.h>
36 #endif /* HAVE_HASHDRBG || NO_RC4 */
43 #if defined(USE_WINDOWS_API)
45 typedef unsigned __int64 ProviderHandle;
46 /* type HCRYPTPROV, avoid #include <windows.h> */
48 typedef unsigned long ProviderHandle;
53 /* OS specific seeder */
54 typedef struct OS_Seed {
55 #if defined(USE_WINDOWS_API)
56 ProviderHandle handle;
64 int GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
66 #if defined(CYASSL_MDK_ARM)
68 #define RNG CyaSSL_RNG /* for avoiding name conflict in "stm32f2xx.h" */
72 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
75 #define DRBG_SEED_LEN (440/8)
78 /* Hash-based Deterministic Random Bit Generator */
83 byte digest[SHA256_DIGEST_SIZE];
84 byte V[DRBG_SEED_LEN];
85 byte C[DRBG_SEED_LEN];
91 #else /* HAVE_HASHDRBG || NO_RC4 */
94 #define CYASSL_RNG_CAVIUM_MAGIC 0xBEEF0004
96 /* secure Random Number Generator */
103 int devId; /* nitrox device id */
104 word32 magic; /* using cavium magic */
110 CYASSL_API int InitRngCavium(RNG*, int);
114 #endif /* HAVE_HASH_DRBG || NO_RC4 */
117 CYASSL_API int InitRng(RNG*);
118 CYASSL_API int RNG_GenerateBlock(RNG*, byte*, word32 sz);
119 CYASSL_API int RNG_GenerateByte(RNG*, byte*);
122 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
123 CYASSL_API int FreeRng(RNG*);
124 CYASSL_API int RNG_HealthTest(int reseed,
125 const byte* entropyA, word32 entropyASz,
126 const byte* entropyB, word32 entropyBSz,
127 const byte* output, word32 outputSz);
128 #endif /* HAVE_HASHDRBG || NO_RC4 */
132 /* fips wrapper calls, user can call direct */
133 CYASSL_API int InitRng_fips(RNG* rng);
134 CYASSL_API int FreeRng_fips(RNG* rng);
135 CYASSL_API int RNG_GenerateBlock_fips(RNG* rng, byte* buf, word32 bufSz);
136 CYASSL_API int RNG_HealthTest_fips(int reseed,
137 const byte* entropyA, word32 entropyASz,
138 const byte* entropyB, word32 entropyBSz,
139 const byte* output, word32 outputSz);
140 #ifndef FIPS_NO_WRAPPERS
141 /* if not impl or fips.c impl wrapper force fips calls if fips build */
142 #define InitRng InitRng_fips
143 #define FreeRng FreeRng_fips
144 #define RNG_GenerateBlock RNG_GenerateBlock_fips
145 #define RNG_HealthTest RNG_HealthTest_fips
146 #endif /* FIPS_NO_WRAPPERS */
147 #endif /* HAVE_FIPS */
154 #endif /* CTAO_CRYPT_RANDOM_H */