MultiTech / CyaSSL

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-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-2014 wolfSSL Inc.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL 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  * CyaSSL 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-1301, USA
00020  */
00021 
00022 
00023 #ifndef CTAO_CRYPT_RANDOM_H
00024 #define CTAO_CRYPT_RANDOM_H
00025 
00026 #include <cyassl/ctaocrypt/types.h>
00027 
00028 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
00029     #ifdef NO_SHA256
00030         #error "Hash DRBG requires SHA-256."
00031     #endif /* NO_SHA256 */
00032 
00033     #include <cyassl/ctaocrypt/sha256.h>
00034 #else /* HAVE_HASHDRBG || NO_RC4 */
00035     #include <cyassl/ctaocrypt/arc4.h>
00036 #endif /* HAVE_HASHDRBG || NO_RC4 */
00037 
00038 #ifdef __cplusplus
00039     extern "C" {
00040 #endif
00041 
00042 
00043 #if defined(USE_WINDOWS_API)
00044     #if defined(_WIN64)
00045         typedef unsigned __int64 ProviderHandle;
00046         /* type HCRYPTPROV, avoid #include <windows.h> */
00047     #else
00048         typedef unsigned long ProviderHandle;
00049     #endif
00050 #endif
00051 
00052 
00053 /* OS specific seeder */
00054 typedef struct OS_Seed {
00055     #if defined(USE_WINDOWS_API)
00056         ProviderHandle handle;
00057     #else
00058         int fd;
00059     #endif
00060 } OS_Seed;
00061 
00062 
00063 CYASSL_LOCAL
00064 int GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
00065 
00066 #if defined(CYASSL_MDK_ARM)
00067 #undef RNG
00068 #define RNG CyaSSL_RNG   /* for avoiding name conflict in "stm32f2xx.h" */
00069 #endif
00070 
00071 
00072 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
00073 
00074 
00075 #define DRBG_SEED_LEN (440/8)
00076 
00077 
00078 struct DRBG; /* Private DRBG state */
00079 
00080 
00081 /* Hash-based Deterministic Random Bit Generator */
00082 typedef struct RNG {
00083     OS_Seed seed;
00084     struct DRBG* drbg;
00085     byte status;
00086 } RNG;
00087 
00088 
00089 #else /* HAVE_HASHDRBG || NO_RC4 */
00090 
00091 
00092 #define CYASSL_RNG_CAVIUM_MAGIC 0xBEEF0004
00093 
00094 /* secure Random Number Generator */
00095 
00096 
00097 typedef struct RNG {
00098     OS_Seed seed;
00099     Arc4    cipher;
00100 #ifdef HAVE_CAVIUM
00101     int    devId;           /* nitrox device id */
00102     word32 magic;           /* using cavium magic */
00103 #endif
00104 } RNG;
00105 
00106 
00107 #ifdef HAVE_CAVIUM
00108     CYASSL_API int  InitRngCavium(RNG*, int);
00109 #endif
00110 
00111 
00112 #endif /* HAVE_HASH_DRBG || NO_RC4 */
00113 
00114 
00115 CYASSL_API int  InitRng(RNG*);
00116 CYASSL_API int  RNG_GenerateBlock(RNG*, byte*, word32 sz);
00117 CYASSL_API int  RNG_GenerateByte(RNG*, byte*);
00118 
00119 
00120 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
00121     CYASSL_API int FreeRng(RNG*);
00122     CYASSL_API int RNG_HealthTest(int reseed,
00123                                         const byte* entropyA, word32 entropyASz,
00124                                         const byte* entropyB, word32 entropyBSz,
00125                                         byte* output, word32 outputSz);
00126 #endif /* HAVE_HASHDRBG || NO_RC4 */
00127 
00128 
00129 #ifdef HAVE_FIPS
00130     /* fips wrapper calls, user can call direct */
00131     CYASSL_API int InitRng_fips(RNG* rng);
00132     CYASSL_API int FreeRng_fips(RNG* rng);
00133     CYASSL_API int RNG_GenerateBlock_fips(RNG* rng, byte* buf, word32 bufSz);
00134     CYASSL_API int RNG_HealthTest_fips(int reseed,
00135                                         const byte* entropyA, word32 entropyASz,
00136                                         const byte* entropyB, word32 entropyBSz,
00137                                         byte* output, word32 outputSz);
00138     #ifndef FIPS_NO_WRAPPERS
00139         /* if not impl or fips.c impl wrapper force fips calls if fips build */
00140         #define InitRng              InitRng_fips
00141         #define FreeRng              FreeRng_fips
00142         #define RNG_GenerateBlock    RNG_GenerateBlock_fips
00143         #define RNG_HealthTest       RNG_HealthTest_fips
00144     #endif /* FIPS_NO_WRAPPERS */
00145 #endif /* HAVE_FIPS */
00146 
00147 
00148 #ifdef __cplusplus
00149     } /* extern "C" */
00150 #endif
00151 
00152 #endif /* CTAO_CRYPT_RANDOM_H */
00153