]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/cyassl/ctaocrypt/random.h
728c22209d181c39a1f7996368a9518feb4fc230
[freertos] / FreeRTOS-Plus / Source / WolfSSL / cyassl / ctaocrypt / random.h
1 /* random.h
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
16  *
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
20  */
21
22
23 #ifndef CTAO_CRYPT_RANDOM_H
24 #define CTAO_CRYPT_RANDOM_H
25
26 #include <cyassl/ctaocrypt/types.h>
27
28 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
29     #ifdef NO_SHA256
30         #error "Hash DRBG requires SHA-256."
31     #endif /* NO_SHA256 */
32
33     #include <cyassl/ctaocrypt/sha256.h>
34 #else /* HAVE_HASHDRBG || NO_RC4 */
35     #include <cyassl/ctaocrypt/arc4.h>
36 #endif /* HAVE_HASHDRBG || NO_RC4 */
37
38 #ifdef __cplusplus
39     extern "C" {
40 #endif
41
42
43 #if defined(USE_WINDOWS_API)
44     #if defined(_WIN64)
45         typedef unsigned __int64 ProviderHandle;
46         /* type HCRYPTPROV, avoid #include <windows.h> */
47     #else
48         typedef unsigned long ProviderHandle;
49     #endif
50 #endif
51
52
53 /* OS specific seeder */
54 typedef struct OS_Seed {
55     #if defined(USE_WINDOWS_API)
56         ProviderHandle handle;
57     #else
58         int fd;
59     #endif
60 } OS_Seed;
61
62
63 CYASSL_LOCAL
64 int GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
65
66 #if defined(CYASSL_MDK_ARM)
67 #undef RNG
68 #define RNG CyaSSL_RNG   /* for avoiding name conflict in "stm32f2xx.h" */
69 #endif
70
71
72 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
73
74
75 #define DRBG_SEED_LEN (440/8)
76
77
78 /* Hash-based Deterministic Random Bit Generator */
79 typedef struct RNG {
80     OS_Seed seed;
81
82     Sha256 sha;
83     byte digest[SHA256_DIGEST_SIZE];
84     byte V[DRBG_SEED_LEN];
85     byte C[DRBG_SEED_LEN];
86     word32 reseedCtr;
87     byte status;
88 } RNG;
89
90
91 #else /* HAVE_HASHDRBG || NO_RC4 */
92
93
94 #define CYASSL_RNG_CAVIUM_MAGIC 0xBEEF0004
95
96 /* secure Random Number Generator */
97
98
99 typedef struct RNG {
100     OS_Seed seed;
101     Arc4    cipher;
102 #ifdef HAVE_CAVIUM
103     int    devId;           /* nitrox device id */
104     word32 magic;           /* using cavium magic */
105 #endif
106 } RNG;
107
108
109 #ifdef HAVE_CAVIUM
110     CYASSL_API int  InitRngCavium(RNG*, int);
111 #endif
112
113
114 #endif /* HAVE_HASH_DRBG || NO_RC4 */
115
116
117 CYASSL_API int  InitRng(RNG*);
118 CYASSL_API int  RNG_GenerateBlock(RNG*, byte*, word32 sz);
119 CYASSL_API int  RNG_GenerateByte(RNG*, byte*);
120
121
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 */
129
130
131 #ifdef HAVE_FIPS
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 */
148
149
150 #ifdef __cplusplus
151     } /* extern "C" */
152 #endif
153
154 #endif /* CTAO_CRYPT_RANDOM_H */
155