Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:1239e9b70ca2 1 /* random.c
wolfSSL 0:1239e9b70ca2 2 *
wolfSSL 0:1239e9b70ca2 3 * Copyright (C) 2006-2014 wolfSSL Inc.
wolfSSL 0:1239e9b70ca2 4 *
wolfSSL 0:1239e9b70ca2 5 * This file is part of CyaSSL.
wolfSSL 0:1239e9b70ca2 6 *
wolfSSL 0:1239e9b70ca2 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:1239e9b70ca2 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:1239e9b70ca2 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:1239e9b70ca2 10 * (at your option) any later version.
wolfSSL 0:1239e9b70ca2 11 *
wolfSSL 0:1239e9b70ca2 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:1239e9b70ca2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:1239e9b70ca2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:1239e9b70ca2 15 * GNU General Public License for more details.
wolfSSL 0:1239e9b70ca2 16 *
wolfSSL 0:1239e9b70ca2 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:1239e9b70ca2 18 * along with this program; if not, write to the Free Software
wolfSSL 0:1239e9b70ca2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:1239e9b70ca2 20 */
wolfSSL 0:1239e9b70ca2 21
wolfSSL 0:1239e9b70ca2 22 #ifdef HAVE_CONFIG_H
wolfSSL 0:1239e9b70ca2 23 #include <config.h>
wolfSSL 0:1239e9b70ca2 24 #endif
wolfSSL 0:1239e9b70ca2 25
wolfSSL 0:1239e9b70ca2 26 #include <cyassl/ctaocrypt/settings.h>
wolfSSL 0:1239e9b70ca2 27
wolfSSL 0:1239e9b70ca2 28 /* on HPUX 11 you may need to install /dev/random see
wolfSSL 0:1239e9b70ca2 29 http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I
wolfSSL 0:1239e9b70ca2 30
wolfSSL 0:1239e9b70ca2 31 */
wolfSSL 0:1239e9b70ca2 32
wolfSSL 0:1239e9b70ca2 33 #include <cyassl/ctaocrypt/random.h>
wolfSSL 0:1239e9b70ca2 34 #include <cyassl/ctaocrypt/error-crypt.h>
wolfSSL 0:1239e9b70ca2 35
wolfSSL 0:1239e9b70ca2 36 #ifdef NO_RC4
wolfSSL 0:1239e9b70ca2 37 #include <cyassl/ctaocrypt/sha256.h>
wolfSSL 0:1239e9b70ca2 38
wolfSSL 0:1239e9b70ca2 39 #ifdef NO_INLINE
wolfSSL 0:1239e9b70ca2 40 #include <cyassl/ctaocrypt/misc.h>
wolfSSL 0:1239e9b70ca2 41 #else
wolfSSL 0:1239e9b70ca2 42 #define MISC_DUMM_FUNC misc_dummy_random
wolfSSL 0:1239e9b70ca2 43 #include <ctaocrypt/src/misc.c>
wolfSSL 0:1239e9b70ca2 44 #endif
wolfSSL 0:1239e9b70ca2 45 #endif
wolfSSL 0:1239e9b70ca2 46
wolfSSL 0:1239e9b70ca2 47 #if defined(USE_WINDOWS_API)
wolfSSL 0:1239e9b70ca2 48 #ifndef _WIN32_WINNT
wolfSSL 0:1239e9b70ca2 49 #define _WIN32_WINNT 0x0400
wolfSSL 0:1239e9b70ca2 50 #endif
wolfSSL 0:1239e9b70ca2 51 #include <windows.h>
wolfSSL 0:1239e9b70ca2 52 #include <wincrypt.h>
wolfSSL 0:1239e9b70ca2 53 #else
wolfSSL 0:1239e9b70ca2 54 #if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM) \
wolfSSL 0:1239e9b70ca2 55 && !defined(CYASSL_IAR_ARM)
wolfSSL 0:1239e9b70ca2 56 #include <fcntl.h>
wolfSSL 0:1239e9b70ca2 57 #ifndef EBSNET
wolfSSL 0:1239e9b70ca2 58 #include <unistd.h>
wolfSSL 0:1239e9b70ca2 59 #endif
wolfSSL 0:1239e9b70ca2 60 #else
wolfSSL 0:1239e9b70ca2 61 /* include headers that may be needed to get good seed */
wolfSSL 0:1239e9b70ca2 62 #endif
wolfSSL 0:1239e9b70ca2 63 #endif /* USE_WINDOWS_API */
wolfSSL 0:1239e9b70ca2 64
wolfSSL 0:1239e9b70ca2 65
wolfSSL 0:1239e9b70ca2 66 #ifdef NO_RC4
wolfSSL 0:1239e9b70ca2 67
wolfSSL 0:1239e9b70ca2 68 /* Start NIST DRBG code */
wolfSSL 0:1239e9b70ca2 69
wolfSSL 0:1239e9b70ca2 70 #define OUTPUT_BLOCK_LEN (256/8)
wolfSSL 0:1239e9b70ca2 71 #define MAX_REQUEST_LEN (0x1000)
wolfSSL 0:1239e9b70ca2 72 #define MAX_STRING_LEN (0x100000000)
wolfSSL 0:1239e9b70ca2 73 #define RESEED_MAX (0x100000000000LL)
wolfSSL 0:1239e9b70ca2 74 #define ENTROPY_SZ 256
wolfSSL 0:1239e9b70ca2 75
wolfSSL 0:1239e9b70ca2 76 #define DBRG_SUCCESS 0
wolfSSL 0:1239e9b70ca2 77 #define DBRG_ERROR 1
wolfSSL 0:1239e9b70ca2 78 #define DBRG_NEED_RESEED 2
wolfSSL 0:1239e9b70ca2 79
wolfSSL 0:1239e9b70ca2 80
wolfSSL 0:1239e9b70ca2 81 enum {
wolfSSL 0:1239e9b70ca2 82 dbrgInitC = 0,
wolfSSL 0:1239e9b70ca2 83 dbrgReseed = 1,
wolfSSL 0:1239e9b70ca2 84 dbrgGenerateW = 2,
wolfSSL 0:1239e9b70ca2 85 dbrgGenerateH = 3,
wolfSSL 0:1239e9b70ca2 86 dbrgInitV
wolfSSL 0:1239e9b70ca2 87 };
wolfSSL 0:1239e9b70ca2 88
wolfSSL 0:1239e9b70ca2 89
wolfSSL 0:1239e9b70ca2 90 static int Hash_df(RNG* rng, byte* out, word32 outSz, byte type, byte* inA, word32 inASz,
wolfSSL 0:1239e9b70ca2 91 byte* inB, word32 inBSz, byte* inC, word32 inCSz)
wolfSSL 0:1239e9b70ca2 92 {
wolfSSL 0:1239e9b70ca2 93 byte ctr;
wolfSSL 0:1239e9b70ca2 94 int i;
wolfSSL 0:1239e9b70ca2 95 int len;
wolfSSL 0:1239e9b70ca2 96 word32 bits = (outSz * 8); /* reverse byte order */
wolfSSL 0:1239e9b70ca2 97
wolfSSL 0:1239e9b70ca2 98 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 99 bits = ByteReverseWord32(bits);
wolfSSL 0:1239e9b70ca2 100 #endif
wolfSSL 0:1239e9b70ca2 101 len = (outSz / SHA256_DIGEST_SIZE)
wolfSSL 0:1239e9b70ca2 102 + ((outSz % SHA256_DIGEST_SIZE) ? 1 : 0);
wolfSSL 0:1239e9b70ca2 103
wolfSSL 0:1239e9b70ca2 104 for (i = 0, ctr = 1; i < len; i++, ctr++)
wolfSSL 0:1239e9b70ca2 105 {
wolfSSL 0:1239e9b70ca2 106 if (InitSha256(&rng->sha) != 0)
wolfSSL 0:1239e9b70ca2 107 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 108
wolfSSL 0:1239e9b70ca2 109 if (Sha256Update(&rng->sha, &ctr, sizeof(ctr)) != 0)
wolfSSL 0:1239e9b70ca2 110 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 111
wolfSSL 0:1239e9b70ca2 112 if (Sha256Update(&rng->sha, (byte*)&bits, sizeof(bits)) != 0)
wolfSSL 0:1239e9b70ca2 113 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 114
wolfSSL 0:1239e9b70ca2 115 /* churning V is the only string that doesn't have
wolfSSL 0:1239e9b70ca2 116 * the type added */
wolfSSL 0:1239e9b70ca2 117 if (type != dbrgInitV)
wolfSSL 0:1239e9b70ca2 118 if (Sha256Update(&rng->sha, &type, sizeof(type)) != 0)
wolfSSL 0:1239e9b70ca2 119 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 120
wolfSSL 0:1239e9b70ca2 121 if (Sha256Update(&rng->sha, inA, inASz) != 0)
wolfSSL 0:1239e9b70ca2 122 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 123
wolfSSL 0:1239e9b70ca2 124 if (inB != NULL && inBSz > 0)
wolfSSL 0:1239e9b70ca2 125 if (Sha256Update(&rng->sha, inB, inBSz) != 0)
wolfSSL 0:1239e9b70ca2 126 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 127
wolfSSL 0:1239e9b70ca2 128 if (inC != NULL && inCSz > 0)
wolfSSL 0:1239e9b70ca2 129 if (Sha256Update(&rng->sha, inC, inCSz) != 0)
wolfSSL 0:1239e9b70ca2 130 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 131
wolfSSL 0:1239e9b70ca2 132 if (Sha256Final(&rng->sha, rng->digest) != 0)
wolfSSL 0:1239e9b70ca2 133 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 134
wolfSSL 0:1239e9b70ca2 135 if (outSz > SHA256_DIGEST_SIZE) {
wolfSSL 0:1239e9b70ca2 136 XMEMCPY(out, rng->digest, SHA256_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 137 outSz -= SHA256_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 138 out += SHA256_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 139 }
wolfSSL 0:1239e9b70ca2 140 else {
wolfSSL 0:1239e9b70ca2 141 XMEMCPY(out, rng->digest, outSz);
wolfSSL 0:1239e9b70ca2 142 }
wolfSSL 0:1239e9b70ca2 143 }
wolfSSL 0:1239e9b70ca2 144
wolfSSL 0:1239e9b70ca2 145 return DBRG_SUCCESS;
wolfSSL 0:1239e9b70ca2 146 }
wolfSSL 0:1239e9b70ca2 147
wolfSSL 0:1239e9b70ca2 148
wolfSSL 0:1239e9b70ca2 149 static int Hash_DBRG_Reseed(RNG* rng, byte* entropy, word32 entropySz)
wolfSSL 0:1239e9b70ca2 150 {
wolfSSL 0:1239e9b70ca2 151 int ret;
wolfSSL 0:1239e9b70ca2 152 byte seed[DBRG_SEED_LEN];
wolfSSL 0:1239e9b70ca2 153
wolfSSL 0:1239e9b70ca2 154 ret = Hash_df(rng, seed, sizeof(seed), dbrgInitV, rng->V, sizeof(rng->V),
wolfSSL 0:1239e9b70ca2 155 entropy, entropySz, NULL, 0);
wolfSSL 0:1239e9b70ca2 156 if (ret != 0)
wolfSSL 0:1239e9b70ca2 157 return ret;
wolfSSL 0:1239e9b70ca2 158
wolfSSL 0:1239e9b70ca2 159 XMEMCPY(rng->V, seed, sizeof(rng->V));
wolfSSL 0:1239e9b70ca2 160 XMEMSET(seed, 0, sizeof(seed));
wolfSSL 0:1239e9b70ca2 161
wolfSSL 0:1239e9b70ca2 162 ret = Hash_df(rng, rng->C, sizeof(rng->C), dbrgInitC, rng->V,
wolfSSL 0:1239e9b70ca2 163 sizeof(rng->V), NULL, 0, NULL, 0);
wolfSSL 0:1239e9b70ca2 164 if (ret != 0)
wolfSSL 0:1239e9b70ca2 165 return ret;
wolfSSL 0:1239e9b70ca2 166
wolfSSL 0:1239e9b70ca2 167 rng->reseed_ctr = 1;
wolfSSL 0:1239e9b70ca2 168 return 0;
wolfSSL 0:1239e9b70ca2 169 }
wolfSSL 0:1239e9b70ca2 170
wolfSSL 0:1239e9b70ca2 171 static INLINE void array_add_one(byte* data, word32 dataSz)
wolfSSL 0:1239e9b70ca2 172 {
wolfSSL 0:1239e9b70ca2 173 int i;
wolfSSL 0:1239e9b70ca2 174
wolfSSL 0:1239e9b70ca2 175 for (i = dataSz - 1; i >= 0; i--)
wolfSSL 0:1239e9b70ca2 176 {
wolfSSL 0:1239e9b70ca2 177 data[i]++;
wolfSSL 0:1239e9b70ca2 178 if (data[i] != 0) break;
wolfSSL 0:1239e9b70ca2 179 }
wolfSSL 0:1239e9b70ca2 180 }
wolfSSL 0:1239e9b70ca2 181
wolfSSL 0:1239e9b70ca2 182 static int Hash_gen(RNG* rng, byte* out, word32 outSz, byte* V)
wolfSSL 0:1239e9b70ca2 183 {
wolfSSL 0:1239e9b70ca2 184 byte data[DBRG_SEED_LEN];
wolfSSL 0:1239e9b70ca2 185 int i, ret;
wolfSSL 0:1239e9b70ca2 186 int len = (outSz / SHA256_DIGEST_SIZE)
wolfSSL 0:1239e9b70ca2 187 + ((outSz % SHA256_DIGEST_SIZE) ? 1 : 0);
wolfSSL 0:1239e9b70ca2 188
wolfSSL 0:1239e9b70ca2 189 XMEMCPY(data, V, sizeof(data));
wolfSSL 0:1239e9b70ca2 190 for (i = 0; i < len; i++) {
wolfSSL 0:1239e9b70ca2 191 ret = InitSha256(&rng->sha);
wolfSSL 0:1239e9b70ca2 192 if (ret != 0)
wolfSSL 0:1239e9b70ca2 193 return ret;
wolfSSL 0:1239e9b70ca2 194
wolfSSL 0:1239e9b70ca2 195 ret = Sha256Update(&rng->sha, data, sizeof(data));
wolfSSL 0:1239e9b70ca2 196 if (ret != 0)
wolfSSL 0:1239e9b70ca2 197 return ret;
wolfSSL 0:1239e9b70ca2 198
wolfSSL 0:1239e9b70ca2 199 ret = Sha256Final(&rng->sha, rng->digest);
wolfSSL 0:1239e9b70ca2 200 if (ret != 0)
wolfSSL 0:1239e9b70ca2 201 return ret;
wolfSSL 0:1239e9b70ca2 202
wolfSSL 0:1239e9b70ca2 203 if (outSz > SHA256_DIGEST_SIZE) {
wolfSSL 0:1239e9b70ca2 204 XMEMCPY(out, rng->digest, SHA256_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 205 outSz -= SHA256_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 206 out += SHA256_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 207 array_add_one(data, DBRG_SEED_LEN);
wolfSSL 0:1239e9b70ca2 208 }
wolfSSL 0:1239e9b70ca2 209 else {
wolfSSL 0:1239e9b70ca2 210 XMEMCPY(out, rng->digest, outSz);
wolfSSL 0:1239e9b70ca2 211 }
wolfSSL 0:1239e9b70ca2 212 }
wolfSSL 0:1239e9b70ca2 213 XMEMSET(data, 0, sizeof(data));
wolfSSL 0:1239e9b70ca2 214
wolfSSL 0:1239e9b70ca2 215 return 0;
wolfSSL 0:1239e9b70ca2 216 }
wolfSSL 0:1239e9b70ca2 217
wolfSSL 0:1239e9b70ca2 218
wolfSSL 0:1239e9b70ca2 219 static INLINE void array_add(byte* d, word32 dLen, byte* s, word32 sLen)
wolfSSL 0:1239e9b70ca2 220 {
wolfSSL 0:1239e9b70ca2 221 word16 carry = 0;
wolfSSL 0:1239e9b70ca2 222
wolfSSL 0:1239e9b70ca2 223 if (dLen > 0 && sLen > 0 && dLen >= sLen) {
wolfSSL 0:1239e9b70ca2 224 int sIdx, dIdx;
wolfSSL 0:1239e9b70ca2 225
wolfSSL 0:1239e9b70ca2 226 for (sIdx = sLen - 1, dIdx = dLen - 1; sIdx >= 0; dIdx--, sIdx--)
wolfSSL 0:1239e9b70ca2 227 {
wolfSSL 0:1239e9b70ca2 228 carry += d[dIdx] + s[sIdx];
wolfSSL 0:1239e9b70ca2 229 d[dIdx] = carry;
wolfSSL 0:1239e9b70ca2 230 carry >>= 8;
wolfSSL 0:1239e9b70ca2 231 }
wolfSSL 0:1239e9b70ca2 232 if (dIdx > 0)
wolfSSL 0:1239e9b70ca2 233 d[dIdx] += carry;
wolfSSL 0:1239e9b70ca2 234 }
wolfSSL 0:1239e9b70ca2 235 }
wolfSSL 0:1239e9b70ca2 236
wolfSSL 0:1239e9b70ca2 237
wolfSSL 0:1239e9b70ca2 238 static int Hash_DBRG_Generate(RNG* rng, byte* out, word32 outSz)
wolfSSL 0:1239e9b70ca2 239 {
wolfSSL 0:1239e9b70ca2 240 int ret;
wolfSSL 0:1239e9b70ca2 241
wolfSSL 0:1239e9b70ca2 242 if (rng->reseed_ctr != RESEED_MAX) {
wolfSSL 0:1239e9b70ca2 243 byte type = dbrgGenerateH;
wolfSSL 0:1239e9b70ca2 244
wolfSSL 0:1239e9b70ca2 245 if (Hash_gen(rng, out, outSz, rng->V) != 0)
wolfSSL 0:1239e9b70ca2 246 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 247 if (InitSha256(&rng->sha) != 0)
wolfSSL 0:1239e9b70ca2 248 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 249 if (Sha256Update(&rng->sha, &type, sizeof(type)) != 0)
wolfSSL 0:1239e9b70ca2 250 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 251 if (Sha256Update(&rng->sha, rng->V, sizeof(rng->V)) != 0)
wolfSSL 0:1239e9b70ca2 252 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 253 if (Sha256Final(&rng->sha, rng->digest) != 0)
wolfSSL 0:1239e9b70ca2 254 return DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 255
wolfSSL 0:1239e9b70ca2 256 array_add(rng->V, sizeof(rng->V), rng->digest, sizeof(rng->digest));
wolfSSL 0:1239e9b70ca2 257 array_add(rng->V, sizeof(rng->V), rng->C, sizeof(rng->C));
wolfSSL 0:1239e9b70ca2 258 array_add(rng->V, sizeof(rng->V),
wolfSSL 0:1239e9b70ca2 259 (byte*)&rng->reseed_ctr, sizeof(rng->reseed_ctr));
wolfSSL 0:1239e9b70ca2 260 rng->reseed_ctr++;
wolfSSL 0:1239e9b70ca2 261 ret = DBRG_SUCCESS;
wolfSSL 0:1239e9b70ca2 262 }
wolfSSL 0:1239e9b70ca2 263 else {
wolfSSL 0:1239e9b70ca2 264 ret = DBRG_NEED_RESEED;
wolfSSL 0:1239e9b70ca2 265 }
wolfSSL 0:1239e9b70ca2 266 return ret;
wolfSSL 0:1239e9b70ca2 267 }
wolfSSL 0:1239e9b70ca2 268
wolfSSL 0:1239e9b70ca2 269
wolfSSL 0:1239e9b70ca2 270 static int Hash_DBRG_Instantiate(RNG* rng, byte* seed, word32 seedSz)
wolfSSL 0:1239e9b70ca2 271 {
wolfSSL 0:1239e9b70ca2 272 int ret;
wolfSSL 0:1239e9b70ca2 273
wolfSSL 0:1239e9b70ca2 274 XMEMSET(rng, 0, sizeof(*rng));
wolfSSL 0:1239e9b70ca2 275 ret = Hash_df(rng, rng->V, sizeof(rng->V), dbrgInitV, seed, seedSz, NULL, 0,
wolfSSL 0:1239e9b70ca2 276 NULL, 0);
wolfSSL 0:1239e9b70ca2 277 if (ret != 0)
wolfSSL 0:1239e9b70ca2 278 return ret;
wolfSSL 0:1239e9b70ca2 279
wolfSSL 0:1239e9b70ca2 280 ret = Hash_df(rng, rng->C, sizeof(rng->C), dbrgInitC, rng->V,
wolfSSL 0:1239e9b70ca2 281 sizeof(rng->V), NULL, 0, NULL, 0);
wolfSSL 0:1239e9b70ca2 282 if (ret != 0)
wolfSSL 0:1239e9b70ca2 283 return ret;
wolfSSL 0:1239e9b70ca2 284
wolfSSL 0:1239e9b70ca2 285 rng->reseed_ctr = 1;
wolfSSL 0:1239e9b70ca2 286
wolfSSL 0:1239e9b70ca2 287 return 0;
wolfSSL 0:1239e9b70ca2 288 }
wolfSSL 0:1239e9b70ca2 289
wolfSSL 0:1239e9b70ca2 290
wolfSSL 0:1239e9b70ca2 291 static int Hash_DBRG_Uninstantiate(RNG* rng)
wolfSSL 0:1239e9b70ca2 292 {
wolfSSL 0:1239e9b70ca2 293 int result = DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 294
wolfSSL 0:1239e9b70ca2 295 if (rng != NULL) {
wolfSSL 0:1239e9b70ca2 296 XMEMSET(rng, 0, sizeof(*rng));
wolfSSL 0:1239e9b70ca2 297 result = DBRG_SUCCESS;
wolfSSL 0:1239e9b70ca2 298 }
wolfSSL 0:1239e9b70ca2 299
wolfSSL 0:1239e9b70ca2 300 return result;
wolfSSL 0:1239e9b70ca2 301 }
wolfSSL 0:1239e9b70ca2 302
wolfSSL 0:1239e9b70ca2 303 /* End NIST DRBG Code */
wolfSSL 0:1239e9b70ca2 304
wolfSSL 0:1239e9b70ca2 305
wolfSSL 0:1239e9b70ca2 306
wolfSSL 0:1239e9b70ca2 307 /* Get seed and key cipher */
wolfSSL 0:1239e9b70ca2 308 int InitRng(RNG* rng)
wolfSSL 0:1239e9b70ca2 309 {
wolfSSL 0:1239e9b70ca2 310 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 311 byte* entropy;
wolfSSL 0:1239e9b70ca2 312 #else
wolfSSL 0:1239e9b70ca2 313 byte entropy[ENTROPY_SZ];
wolfSSL 0:1239e9b70ca2 314 #endif
wolfSSL 0:1239e9b70ca2 315 int ret = DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 316
wolfSSL 0:1239e9b70ca2 317 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 318 entropy = (byte*)XMALLOC(ENTROPY_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 319 if (entropy == NULL)
wolfSSL 0:1239e9b70ca2 320 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 321 #endif
wolfSSL 0:1239e9b70ca2 322
wolfSSL 0:1239e9b70ca2 323 if (GenerateSeed(&rng->seed, entropy, ENTROPY_SZ) == 0)
wolfSSL 0:1239e9b70ca2 324 ret = Hash_DBRG_Instantiate(rng, entropy, ENTROPY_SZ);
wolfSSL 0:1239e9b70ca2 325
wolfSSL 0:1239e9b70ca2 326 XMEMSET(entropy, 0, ENTROPY_SZ);
wolfSSL 0:1239e9b70ca2 327
wolfSSL 0:1239e9b70ca2 328 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 329 XFREE(entropy, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 330 #endif
wolfSSL 0:1239e9b70ca2 331
wolfSSL 0:1239e9b70ca2 332 return ret;
wolfSSL 0:1239e9b70ca2 333 }
wolfSSL 0:1239e9b70ca2 334
wolfSSL 0:1239e9b70ca2 335
wolfSSL 0:1239e9b70ca2 336 /* place a generated block in output */
wolfSSL 0:1239e9b70ca2 337 int RNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 338 {
wolfSSL 0:1239e9b70ca2 339 int ret;
wolfSSL 0:1239e9b70ca2 340
wolfSSL 0:1239e9b70ca2 341 XMEMSET(output, 0, sz);
wolfSSL 0:1239e9b70ca2 342 ret = Hash_DBRG_Generate(rng, output, sz);
wolfSSL 0:1239e9b70ca2 343
wolfSSL 0:1239e9b70ca2 344 if (ret == DBRG_NEED_RESEED) {
wolfSSL 0:1239e9b70ca2 345 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 346 byte* entropy;
wolfSSL 0:1239e9b70ca2 347 #else
wolfSSL 0:1239e9b70ca2 348 byte entropy[ENTROPY_SZ];
wolfSSL 0:1239e9b70ca2 349 #endif
wolfSSL 0:1239e9b70ca2 350
wolfSSL 0:1239e9b70ca2 351 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 352 entropy = (byte*)XMALLOC(ENTROPY_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 353 if (entropy == NULL)
wolfSSL 0:1239e9b70ca2 354 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 355 #endif
wolfSSL 0:1239e9b70ca2 356
wolfSSL 0:1239e9b70ca2 357 ret = GenerateSeed(&rng->seed, entropy, ENTROPY_SZ);
wolfSSL 0:1239e9b70ca2 358 if (ret == 0) {
wolfSSL 0:1239e9b70ca2 359 ret = Hash_DBRG_Reseed(rng, entropy, ENTROPY_SZ);
wolfSSL 0:1239e9b70ca2 360
wolfSSL 0:1239e9b70ca2 361 if (ret == 0)
wolfSSL 0:1239e9b70ca2 362 ret = Hash_DBRG_Generate(rng, output, sz);
wolfSSL 0:1239e9b70ca2 363 }
wolfSSL 0:1239e9b70ca2 364 else
wolfSSL 0:1239e9b70ca2 365 ret = DBRG_ERROR;
wolfSSL 0:1239e9b70ca2 366
wolfSSL 0:1239e9b70ca2 367 XMEMSET(entropy, 0, ENTROPY_SZ);
wolfSSL 0:1239e9b70ca2 368
wolfSSL 0:1239e9b70ca2 369 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 370 XFREE(entropy, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 371 #endif
wolfSSL 0:1239e9b70ca2 372 }
wolfSSL 0:1239e9b70ca2 373
wolfSSL 0:1239e9b70ca2 374 return ret;
wolfSSL 0:1239e9b70ca2 375 }
wolfSSL 0:1239e9b70ca2 376
wolfSSL 0:1239e9b70ca2 377
wolfSSL 0:1239e9b70ca2 378 int RNG_GenerateByte(RNG* rng, byte* b)
wolfSSL 0:1239e9b70ca2 379 {
wolfSSL 0:1239e9b70ca2 380 return RNG_GenerateBlock(rng, b, 1);
wolfSSL 0:1239e9b70ca2 381 }
wolfSSL 0:1239e9b70ca2 382
wolfSSL 0:1239e9b70ca2 383
wolfSSL 0:1239e9b70ca2 384 void FreeRng(RNG* rng)
wolfSSL 0:1239e9b70ca2 385 {
wolfSSL 0:1239e9b70ca2 386 Hash_DBRG_Uninstantiate(rng);
wolfSSL 0:1239e9b70ca2 387 }
wolfSSL 0:1239e9b70ca2 388
wolfSSL 0:1239e9b70ca2 389 #else /* NO_RC4 */
wolfSSL 0:1239e9b70ca2 390
wolfSSL 0:1239e9b70ca2 391 /* Get seed and key cipher */
wolfSSL 0:1239e9b70ca2 392 int InitRng(RNG* rng)
wolfSSL 0:1239e9b70ca2 393 {
wolfSSL 0:1239e9b70ca2 394 int ret;
wolfSSL 0:1239e9b70ca2 395 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 396 byte* key;
wolfSSL 0:1239e9b70ca2 397 byte* junk;
wolfSSL 0:1239e9b70ca2 398 #else
wolfSSL 0:1239e9b70ca2 399 byte key[32];
wolfSSL 0:1239e9b70ca2 400 byte junk[256];
wolfSSL 0:1239e9b70ca2 401 #endif
wolfSSL 0:1239e9b70ca2 402
wolfSSL 0:1239e9b70ca2 403 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 404 if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 405 return 0;
wolfSSL 0:1239e9b70ca2 406 #endif
wolfSSL 0:1239e9b70ca2 407
wolfSSL 0:1239e9b70ca2 408 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 409 key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 410 if (key == NULL)
wolfSSL 0:1239e9b70ca2 411 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 412
wolfSSL 0:1239e9b70ca2 413 junk = (byte*)XMALLOC(256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 414 if (junk == NULL) {
wolfSSL 0:1239e9b70ca2 415 XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 416 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 417 }
wolfSSL 0:1239e9b70ca2 418 #endif
wolfSSL 0:1239e9b70ca2 419
wolfSSL 0:1239e9b70ca2 420 ret = GenerateSeed(&rng->seed, key, 32);
wolfSSL 0:1239e9b70ca2 421
wolfSSL 0:1239e9b70ca2 422 if (ret == 0) {
wolfSSL 0:1239e9b70ca2 423 Arc4SetKey(&rng->cipher, key, sizeof(key));
wolfSSL 0:1239e9b70ca2 424
wolfSSL 0:1239e9b70ca2 425 ret = RNG_GenerateBlock(rng, junk, 256); /*rid initial state*/
wolfSSL 0:1239e9b70ca2 426 }
wolfSSL 0:1239e9b70ca2 427
wolfSSL 0:1239e9b70ca2 428 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 429 XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 430 XFREE(junk, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 431 #endif
wolfSSL 0:1239e9b70ca2 432
wolfSSL 0:1239e9b70ca2 433 return ret;
wolfSSL 0:1239e9b70ca2 434 }
wolfSSL 0:1239e9b70ca2 435
wolfSSL 0:1239e9b70ca2 436 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 437 static void CaviumRNG_GenerateBlock(RNG* rng, byte* output, word32 sz);
wolfSSL 0:1239e9b70ca2 438 #endif
wolfSSL 0:1239e9b70ca2 439
wolfSSL 0:1239e9b70ca2 440 /* place a generated block in output */
wolfSSL 0:1239e9b70ca2 441 int RNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 442 {
wolfSSL 0:1239e9b70ca2 443 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 444 if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 445 return CaviumRNG_GenerateBlock(rng, output, sz);
wolfSSL 0:1239e9b70ca2 446 #endif
wolfSSL 0:1239e9b70ca2 447 XMEMSET(output, 0, sz);
wolfSSL 0:1239e9b70ca2 448 Arc4Process(&rng->cipher, output, output, sz);
wolfSSL 0:1239e9b70ca2 449
wolfSSL 0:1239e9b70ca2 450 return 0;
wolfSSL 0:1239e9b70ca2 451 }
wolfSSL 0:1239e9b70ca2 452
wolfSSL 0:1239e9b70ca2 453
wolfSSL 0:1239e9b70ca2 454 int RNG_GenerateByte(RNG* rng, byte* b)
wolfSSL 0:1239e9b70ca2 455 {
wolfSSL 0:1239e9b70ca2 456 return RNG_GenerateBlock(rng, b, 1);
wolfSSL 0:1239e9b70ca2 457 }
wolfSSL 0:1239e9b70ca2 458
wolfSSL 0:1239e9b70ca2 459
wolfSSL 0:1239e9b70ca2 460 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 461
wolfSSL 0:1239e9b70ca2 462 #include <cyassl/ctaocrypt/logging.h>
wolfSSL 0:1239e9b70ca2 463 #include "cavium_common.h"
wolfSSL 0:1239e9b70ca2 464
wolfSSL 0:1239e9b70ca2 465 /* Initiliaze RNG for use with Nitrox device */
wolfSSL 0:1239e9b70ca2 466 int InitRngCavium(RNG* rng, int devId)
wolfSSL 0:1239e9b70ca2 467 {
wolfSSL 0:1239e9b70ca2 468 if (rng == NULL)
wolfSSL 0:1239e9b70ca2 469 return -1;
wolfSSL 0:1239e9b70ca2 470
wolfSSL 0:1239e9b70ca2 471 rng->devId = devId;
wolfSSL 0:1239e9b70ca2 472 rng->magic = CYASSL_RNG_CAVIUM_MAGIC;
wolfSSL 0:1239e9b70ca2 473
wolfSSL 0:1239e9b70ca2 474 return 0;
wolfSSL 0:1239e9b70ca2 475 }
wolfSSL 0:1239e9b70ca2 476
wolfSSL 0:1239e9b70ca2 477
wolfSSL 0:1239e9b70ca2 478 static void CaviumRNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 479 {
wolfSSL 0:1239e9b70ca2 480 word offset = 0;
wolfSSL 0:1239e9b70ca2 481 word32 requestId;
wolfSSL 0:1239e9b70ca2 482
wolfSSL 0:1239e9b70ca2 483 while (sz > CYASSL_MAX_16BIT) {
wolfSSL 0:1239e9b70ca2 484 word16 slen = (word16)CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 485 if (CspRandom(CAVIUM_BLOCKING, slen, output + offset, &requestId,
wolfSSL 0:1239e9b70ca2 486 rng->devId) != 0) {
wolfSSL 0:1239e9b70ca2 487 CYASSL_MSG("Cavium RNG failed");
wolfSSL 0:1239e9b70ca2 488 }
wolfSSL 0:1239e9b70ca2 489 sz -= CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 490 offset += CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 491 }
wolfSSL 0:1239e9b70ca2 492 if (sz) {
wolfSSL 0:1239e9b70ca2 493 word16 slen = (word16)sz;
wolfSSL 0:1239e9b70ca2 494 if (CspRandom(CAVIUM_BLOCKING, slen, output + offset, &requestId,
wolfSSL 0:1239e9b70ca2 495 rng->devId) != 0) {
wolfSSL 0:1239e9b70ca2 496 CYASSL_MSG("Cavium RNG failed");
wolfSSL 0:1239e9b70ca2 497 }
wolfSSL 0:1239e9b70ca2 498 }
wolfSSL 0:1239e9b70ca2 499 }
wolfSSL 0:1239e9b70ca2 500
wolfSSL 0:1239e9b70ca2 501 #endif /* HAVE_CAVIUM */
wolfSSL 0:1239e9b70ca2 502
wolfSSL 0:1239e9b70ca2 503 #endif /* NO_RC4 */
wolfSSL 0:1239e9b70ca2 504
wolfSSL 0:1239e9b70ca2 505
wolfSSL 0:1239e9b70ca2 506 #if defined(USE_WINDOWS_API)
wolfSSL 0:1239e9b70ca2 507
wolfSSL 0:1239e9b70ca2 508
wolfSSL 0:1239e9b70ca2 509 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 510 {
wolfSSL 0:1239e9b70ca2 511 if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
wolfSSL 0:1239e9b70ca2 512 CRYPT_VERIFYCONTEXT))
wolfSSL 0:1239e9b70ca2 513 return WINCRYPT_E;
wolfSSL 0:1239e9b70ca2 514
wolfSSL 0:1239e9b70ca2 515 if (!CryptGenRandom(os->handle, sz, output))
wolfSSL 0:1239e9b70ca2 516 return CRYPTGEN_E;
wolfSSL 0:1239e9b70ca2 517
wolfSSL 0:1239e9b70ca2 518 CryptReleaseContext(os->handle, 0);
wolfSSL 0:1239e9b70ca2 519
wolfSSL 0:1239e9b70ca2 520 return 0;
wolfSSL 0:1239e9b70ca2 521 }
wolfSSL 0:1239e9b70ca2 522
wolfSSL 0:1239e9b70ca2 523
wolfSSL 0:1239e9b70ca2 524 #elif defined(HAVE_RTP_SYS) || defined(EBSNET)
wolfSSL 0:1239e9b70ca2 525
wolfSSL 0:1239e9b70ca2 526 #include "rtprand.h" /* rtp_rand () */
wolfSSL 0:1239e9b70ca2 527 #include "rtptime.h" /* rtp_get_system_msec() */
wolfSSL 0:1239e9b70ca2 528
wolfSSL 0:1239e9b70ca2 529
wolfSSL 0:1239e9b70ca2 530 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 531 {
wolfSSL 0:1239e9b70ca2 532 int i;
wolfSSL 0:1239e9b70ca2 533 rtp_srand(rtp_get_system_msec());
wolfSSL 0:1239e9b70ca2 534
wolfSSL 0:1239e9b70ca2 535 for (i = 0; i < sz; i++ ) {
wolfSSL 0:1239e9b70ca2 536 output[i] = rtp_rand() % 256;
wolfSSL 0:1239e9b70ca2 537 if ( (i % 8) == 7)
wolfSSL 0:1239e9b70ca2 538 rtp_srand(rtp_get_system_msec());
wolfSSL 0:1239e9b70ca2 539 }
wolfSSL 0:1239e9b70ca2 540
wolfSSL 0:1239e9b70ca2 541 return 0;
wolfSSL 0:1239e9b70ca2 542 }
wolfSSL 0:1239e9b70ca2 543
wolfSSL 0:1239e9b70ca2 544
wolfSSL 0:1239e9b70ca2 545 #elif defined(MICRIUM)
wolfSSL 0:1239e9b70ca2 546
wolfSSL 0:1239e9b70ca2 547 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 548 {
wolfSSL 0:1239e9b70ca2 549 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
wolfSSL 0:1239e9b70ca2 550 NetSecure_InitSeed(output, sz);
wolfSSL 0:1239e9b70ca2 551 #endif
wolfSSL 0:1239e9b70ca2 552 return 0;
wolfSSL 0:1239e9b70ca2 553 }
wolfSSL 0:1239e9b70ca2 554
wolfSSL 0:1239e9b70ca2 555 #elif defined(MBED)
wolfSSL 0:1239e9b70ca2 556
wolfSSL 0:1239e9b70ca2 557 /* write a real one !!!, just for testing board */
wolfSSL 0:1239e9b70ca2 558 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 559 {
wolfSSL 0:1239e9b70ca2 560 int i;
wolfSSL 0:1239e9b70ca2 561 for (i = 0; i < sz; i++ )
wolfSSL 0:1239e9b70ca2 562 output[i] = i;
wolfSSL 0:1239e9b70ca2 563
wolfSSL 0:1239e9b70ca2 564 return 0;
wolfSSL 0:1239e9b70ca2 565 }
wolfSSL 0:1239e9b70ca2 566
wolfSSL 0:1239e9b70ca2 567 #elif defined(MICROCHIP_PIC32)
wolfSSL 0:1239e9b70ca2 568
wolfSSL 0:1239e9b70ca2 569 #ifdef MICROCHIP_MPLAB_HARMONY
wolfSSL 0:1239e9b70ca2 570 #define PIC32_SEED_COUNT _CP0_GET_COUNT
wolfSSL 0:1239e9b70ca2 571 #else
wolfSSL 0:1239e9b70ca2 572 #if !defined(CYASSL_MICROCHIP_PIC32MZ)
wolfSSL 0:1239e9b70ca2 573 #include <peripheral/timer.h>
wolfSSL 0:1239e9b70ca2 574 #endif
wolfSSL 0:1239e9b70ca2 575 #define PIC32_SEED_COUNT ReadCoreTimer
wolfSSL 0:1239e9b70ca2 576 #endif
wolfSSL 0:1239e9b70ca2 577 #ifdef CYASSL_MIC32MZ_RNG
wolfSSL 0:1239e9b70ca2 578 #include "xc.h"
wolfSSL 0:1239e9b70ca2 579 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 580 {
wolfSSL 0:1239e9b70ca2 581 int i ;
wolfSSL 0:1239e9b70ca2 582 byte rnd[8] ;
wolfSSL 0:1239e9b70ca2 583 word32 *rnd32 = (word32 *)rnd ;
wolfSSL 0:1239e9b70ca2 584 word32 size = sz ;
wolfSSL 0:1239e9b70ca2 585 byte* op = output ;
wolfSSL 0:1239e9b70ca2 586
wolfSSL 0:1239e9b70ca2 587 /* This part has to be replaced with better random seed */
wolfSSL 0:1239e9b70ca2 588 RNGNUMGEN1 = ReadCoreTimer();
wolfSSL 0:1239e9b70ca2 589 RNGPOLY1 = ReadCoreTimer();
wolfSSL 0:1239e9b70ca2 590 RNGPOLY2 = ReadCoreTimer();
wolfSSL 0:1239e9b70ca2 591 RNGNUMGEN2 = ReadCoreTimer();
wolfSSL 0:1239e9b70ca2 592 #ifdef DEBUG_CYASSL
wolfSSL 0:1239e9b70ca2 593 printf("GenerateSeed::Seed=%08x, %08x\n", RNGNUMGEN1, RNGNUMGEN2) ;
wolfSSL 0:1239e9b70ca2 594 #endif
wolfSSL 0:1239e9b70ca2 595 RNGCONbits.PLEN = 0x40;
wolfSSL 0:1239e9b70ca2 596 RNGCONbits.PRNGEN = 1;
wolfSSL 0:1239e9b70ca2 597 for(i=0; i<5; i++) { /* wait for RNGNUMGEN ready */
wolfSSL 0:1239e9b70ca2 598 volatile int x ;
wolfSSL 0:1239e9b70ca2 599 x = RNGNUMGEN1 ;
wolfSSL 0:1239e9b70ca2 600 x = RNGNUMGEN2 ;
wolfSSL 0:1239e9b70ca2 601 }
wolfSSL 0:1239e9b70ca2 602 do {
wolfSSL 0:1239e9b70ca2 603 rnd32[0] = RNGNUMGEN1;
wolfSSL 0:1239e9b70ca2 604 rnd32[1] = RNGNUMGEN2;
wolfSSL 0:1239e9b70ca2 605
wolfSSL 0:1239e9b70ca2 606 for(i=0; i<8; i++, op++) {
wolfSSL 0:1239e9b70ca2 607 *op = rnd[i] ;
wolfSSL 0:1239e9b70ca2 608 size -- ;
wolfSSL 0:1239e9b70ca2 609 if(size==0)break ;
wolfSSL 0:1239e9b70ca2 610 }
wolfSSL 0:1239e9b70ca2 611 } while(size) ;
wolfSSL 0:1239e9b70ca2 612 return 0;
wolfSSL 0:1239e9b70ca2 613 }
wolfSSL 0:1239e9b70ca2 614 #else /* CYASSL_MIC32MZ_RNG */
wolfSSL 0:1239e9b70ca2 615 /* uses the core timer, in nanoseconds to seed srand */
wolfSSL 0:1239e9b70ca2 616 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 617 {
wolfSSL 0:1239e9b70ca2 618 int i;
wolfSSL 0:1239e9b70ca2 619 srand(PIC32_SEED_COUNT() * 25);
wolfSSL 0:1239e9b70ca2 620
wolfSSL 0:1239e9b70ca2 621 for (i = 0; i < sz; i++ ) {
wolfSSL 0:1239e9b70ca2 622 output[i] = rand() % 256;
wolfSSL 0:1239e9b70ca2 623 if ( (i % 8) == 7)
wolfSSL 0:1239e9b70ca2 624 srand(PIC32_SEED_COUNT() * 25);
wolfSSL 0:1239e9b70ca2 625 }
wolfSSL 0:1239e9b70ca2 626 return 0;
wolfSSL 0:1239e9b70ca2 627 }
wolfSSL 0:1239e9b70ca2 628 #endif /* CYASSL_MIC32MZ_RNG */
wolfSSL 0:1239e9b70ca2 629
wolfSSL 0:1239e9b70ca2 630 #elif defined(FREESCALE_MQX)
wolfSSL 0:1239e9b70ca2 631
wolfSSL 0:1239e9b70ca2 632 #ifdef FREESCALE_K70_RNGA
wolfSSL 0:1239e9b70ca2 633 /*
wolfSSL 0:1239e9b70ca2 634 * Generates a RNG seed using the Random Number Generator Accelerator
wolfSSL 0:1239e9b70ca2 635 * on the Kinetis K70. Documentation located in Chapter 37 of
wolfSSL 0:1239e9b70ca2 636 * K70 Sub-Family Reference Manual (see Note 3 in the README for link).
wolfSSL 0:1239e9b70ca2 637 */
wolfSSL 0:1239e9b70ca2 638 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 639 {
wolfSSL 0:1239e9b70ca2 640 int i;
wolfSSL 0:1239e9b70ca2 641
wolfSSL 0:1239e9b70ca2 642 /* turn on RNGA module */
wolfSSL 0:1239e9b70ca2 643 SIM_SCGC3 |= SIM_SCGC3_RNGA_MASK;
wolfSSL 0:1239e9b70ca2 644
wolfSSL 0:1239e9b70ca2 645 /* set SLP bit to 0 - "RNGA is not in sleep mode" */
wolfSSL 0:1239e9b70ca2 646 RNG_CR &= ~RNG_CR_SLP_MASK;
wolfSSL 0:1239e9b70ca2 647
wolfSSL 0:1239e9b70ca2 648 /* set HA bit to 1 - "security violations masked" */
wolfSSL 0:1239e9b70ca2 649 RNG_CR |= RNG_CR_HA_MASK;
wolfSSL 0:1239e9b70ca2 650
wolfSSL 0:1239e9b70ca2 651 /* set GO bit to 1 - "output register loaded with data" */
wolfSSL 0:1239e9b70ca2 652 RNG_CR |= RNG_CR_GO_MASK;
wolfSSL 0:1239e9b70ca2 653
wolfSSL 0:1239e9b70ca2 654 for (i = 0; i < sz; i++) {
wolfSSL 0:1239e9b70ca2 655
wolfSSL 0:1239e9b70ca2 656 /* wait for RNG FIFO to be full */
wolfSSL 0:1239e9b70ca2 657 while((RNG_SR & RNG_SR_OREG_LVL(0xF)) == 0) {}
wolfSSL 0:1239e9b70ca2 658
wolfSSL 0:1239e9b70ca2 659 /* get value */
wolfSSL 0:1239e9b70ca2 660 output[i] = RNG_OR;
wolfSSL 0:1239e9b70ca2 661 }
wolfSSL 0:1239e9b70ca2 662
wolfSSL 0:1239e9b70ca2 663 return 0;
wolfSSL 0:1239e9b70ca2 664 }
wolfSSL 0:1239e9b70ca2 665
wolfSSL 0:1239e9b70ca2 666 #elif defined(FREESCALE_K53_RNGB)
wolfSSL 0:1239e9b70ca2 667 /*
wolfSSL 0:1239e9b70ca2 668 * Generates a RNG seed using the Random Number Generator (RNGB)
wolfSSL 0:1239e9b70ca2 669 * on the Kinetis K53. Documentation located in Chapter 33 of
wolfSSL 0:1239e9b70ca2 670 * K53 Sub-Family Reference Manual (see note in the README for link).
wolfSSL 0:1239e9b70ca2 671 */
wolfSSL 0:1239e9b70ca2 672 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 673 {
wolfSSL 0:1239e9b70ca2 674 int i;
wolfSSL 0:1239e9b70ca2 675
wolfSSL 0:1239e9b70ca2 676 /* turn on RNGB module */
wolfSSL 0:1239e9b70ca2 677 SIM_SCGC3 |= SIM_SCGC3_RNGB_MASK;
wolfSSL 0:1239e9b70ca2 678
wolfSSL 0:1239e9b70ca2 679 /* reset RNGB */
wolfSSL 0:1239e9b70ca2 680 RNG_CMD |= RNG_CMD_SR_MASK;
wolfSSL 0:1239e9b70ca2 681
wolfSSL 0:1239e9b70ca2 682 /* FIFO generate interrupt, return all zeros on underflow,
wolfSSL 0:1239e9b70ca2 683 * set auto reseed */
wolfSSL 0:1239e9b70ca2 684 RNG_CR |= (RNG_CR_FUFMOD_MASK | RNG_CR_AR_MASK);
wolfSSL 0:1239e9b70ca2 685
wolfSSL 0:1239e9b70ca2 686 /* gen seed, clear interrupts, clear errors */
wolfSSL 0:1239e9b70ca2 687 RNG_CMD |= (RNG_CMD_GS_MASK | RNG_CMD_CI_MASK | RNG_CMD_CE_MASK);
wolfSSL 0:1239e9b70ca2 688
wolfSSL 0:1239e9b70ca2 689 /* wait for seeding to complete */
wolfSSL 0:1239e9b70ca2 690 while ((RNG_SR & RNG_SR_SDN_MASK) == 0) {}
wolfSSL 0:1239e9b70ca2 691
wolfSSL 0:1239e9b70ca2 692 for (i = 0; i < sz; i++) {
wolfSSL 0:1239e9b70ca2 693
wolfSSL 0:1239e9b70ca2 694 /* wait for a word to be available from FIFO */
wolfSSL 0:1239e9b70ca2 695 while((RNG_SR & RNG_SR_FIFO_LVL_MASK) == 0) {}
wolfSSL 0:1239e9b70ca2 696
wolfSSL 0:1239e9b70ca2 697 /* get value */
wolfSSL 0:1239e9b70ca2 698 output[i] = RNG_OUT;
wolfSSL 0:1239e9b70ca2 699 }
wolfSSL 0:1239e9b70ca2 700
wolfSSL 0:1239e9b70ca2 701 return 0;
wolfSSL 0:1239e9b70ca2 702 }
wolfSSL 0:1239e9b70ca2 703
wolfSSL 0:1239e9b70ca2 704 #else
wolfSSL 0:1239e9b70ca2 705 #warning "write a real random seed!!!!, just for testing now"
wolfSSL 0:1239e9b70ca2 706
wolfSSL 0:1239e9b70ca2 707 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 708 {
wolfSSL 0:1239e9b70ca2 709 int i;
wolfSSL 0:1239e9b70ca2 710 for (i = 0; i < sz; i++ )
wolfSSL 0:1239e9b70ca2 711 output[i] = i;
wolfSSL 0:1239e9b70ca2 712
wolfSSL 0:1239e9b70ca2 713 return 0;
wolfSSL 0:1239e9b70ca2 714 }
wolfSSL 0:1239e9b70ca2 715 #endif /* FREESCALE_K70_RNGA */
wolfSSL 0:1239e9b70ca2 716
wolfSSL 0:1239e9b70ca2 717 #elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK) \
wolfSSL 0:1239e9b70ca2 718 || defined(CYASSL_IAR_ARM)
wolfSSL 0:1239e9b70ca2 719
wolfSSL 0:1239e9b70ca2 720 #warning "write a real random seed!!!!, just for testing now"
wolfSSL 0:1239e9b70ca2 721
wolfSSL 0:1239e9b70ca2 722 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 723 {
wolfSSL 0:1239e9b70ca2 724 word32 i;
wolfSSL 0:1239e9b70ca2 725 for (i = 0; i < sz; i++ )
wolfSSL 0:1239e9b70ca2 726 output[i] = i;
wolfSSL 0:1239e9b70ca2 727
wolfSSL 0:1239e9b70ca2 728 (void)os;
wolfSSL 0:1239e9b70ca2 729
wolfSSL 0:1239e9b70ca2 730 return 0;
wolfSSL 0:1239e9b70ca2 731 }
wolfSSL 0:1239e9b70ca2 732
wolfSSL 0:1239e9b70ca2 733 #elif defined(STM32F2_RNG)
wolfSSL 0:1239e9b70ca2 734 #undef RNG
wolfSSL 0:1239e9b70ca2 735 #include "stm32f2xx_rng.h"
wolfSSL 0:1239e9b70ca2 736 #include "stm32f2xx_rcc.h"
wolfSSL 0:1239e9b70ca2 737 /*
wolfSSL 0:1239e9b70ca2 738 * Generate a RNG seed using the hardware random number generator
wolfSSL 0:1239e9b70ca2 739 * on the STM32F2. Documentation located in STM32F2xx Standard Peripheral
wolfSSL 0:1239e9b70ca2 740 * Library document (See note in README).
wolfSSL 0:1239e9b70ca2 741 */
wolfSSL 0:1239e9b70ca2 742 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 743 {
wolfSSL 0:1239e9b70ca2 744 int i;
wolfSSL 0:1239e9b70ca2 745
wolfSSL 0:1239e9b70ca2 746 /* enable RNG clock source */
wolfSSL 0:1239e9b70ca2 747 RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
wolfSSL 0:1239e9b70ca2 748
wolfSSL 0:1239e9b70ca2 749 /* enable RNG peripheral */
wolfSSL 0:1239e9b70ca2 750 RNG_Cmd(ENABLE);
wolfSSL 0:1239e9b70ca2 751
wolfSSL 0:1239e9b70ca2 752 for (i = 0; i < sz; i++) {
wolfSSL 0:1239e9b70ca2 753 /* wait until RNG number is ready */
wolfSSL 0:1239e9b70ca2 754 while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET) { }
wolfSSL 0:1239e9b70ca2 755
wolfSSL 0:1239e9b70ca2 756 /* get value */
wolfSSL 0:1239e9b70ca2 757 output[i] = RNG_GetRandomNumber();
wolfSSL 0:1239e9b70ca2 758 }
wolfSSL 0:1239e9b70ca2 759
wolfSSL 0:1239e9b70ca2 760 return 0;
wolfSSL 0:1239e9b70ca2 761 }
wolfSSL 0:1239e9b70ca2 762 #elif defined(CYASSL_LPC43xx) || defined(CYASSL_STM32F2xx)
wolfSSL 0:1239e9b70ca2 763
wolfSSL 0:1239e9b70ca2 764 #warning "write a real random seed!!!!, just for testing now"
wolfSSL 0:1239e9b70ca2 765
wolfSSL 0:1239e9b70ca2 766 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 767 {
wolfSSL 0:1239e9b70ca2 768 int i;
wolfSSL 0:1239e9b70ca2 769
wolfSSL 0:1239e9b70ca2 770 for (i = 0; i < sz; i++ )
wolfSSL 0:1239e9b70ca2 771 output[i] = i;
wolfSSL 0:1239e9b70ca2 772
wolfSSL 0:1239e9b70ca2 773 return 0;
wolfSSL 0:1239e9b70ca2 774 }
wolfSSL 0:1239e9b70ca2 775
wolfSSL 0:1239e9b70ca2 776 #elif defined(CUSTOM_RAND_GENERATE)
wolfSSL 0:1239e9b70ca2 777
wolfSSL 0:1239e9b70ca2 778 /* Implement your own random generation function
wolfSSL 0:1239e9b70ca2 779 * word32 rand_gen(void);
wolfSSL 0:1239e9b70ca2 780 * #define CUSTOM_RAND_GENERATE rand_gen */
wolfSSL 0:1239e9b70ca2 781
wolfSSL 0:1239e9b70ca2 782 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 783 {
wolfSSL 0:1239e9b70ca2 784 int i;
wolfSSL 0:1239e9b70ca2 785
wolfSSL 0:1239e9b70ca2 786 for (i = 0; i < sz; i++ )
wolfSSL 0:1239e9b70ca2 787 output[i] = CUSTOM_RAND_GENERATE();
wolfSSL 0:1239e9b70ca2 788
wolfSSL 0:1239e9b70ca2 789 return 0;
wolfSSL 0:1239e9b70ca2 790 }
wolfSSL 0:1239e9b70ca2 791
wolfSSL 0:1239e9b70ca2 792 #elif defined(NO_DEV_RANDOM)
wolfSSL 0:1239e9b70ca2 793
wolfSSL 0:1239e9b70ca2 794 #error "you need to write an os specific GenerateSeed() here"
wolfSSL 0:1239e9b70ca2 795
wolfSSL 0:1239e9b70ca2 796 /*
wolfSSL 0:1239e9b70ca2 797 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 798 {
wolfSSL 0:1239e9b70ca2 799 return 0;
wolfSSL 0:1239e9b70ca2 800 }
wolfSSL 0:1239e9b70ca2 801 */
wolfSSL 0:1239e9b70ca2 802
wolfSSL 0:1239e9b70ca2 803
wolfSSL 0:1239e9b70ca2 804 #else /* !USE_WINDOWS_API && !HAVE_RPT_SYS && !MICRIUM && !NO_DEV_RANDOM */
wolfSSL 0:1239e9b70ca2 805
wolfSSL 0:1239e9b70ca2 806
wolfSSL 0:1239e9b70ca2 807 /* may block */
wolfSSL 0:1239e9b70ca2 808 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
wolfSSL 0:1239e9b70ca2 809 {
wolfSSL 0:1239e9b70ca2 810 int ret = 0;
wolfSSL 0:1239e9b70ca2 811
wolfSSL 0:1239e9b70ca2 812 os->fd = open("/dev/urandom",O_RDONLY);
wolfSSL 0:1239e9b70ca2 813 if (os->fd == -1) {
wolfSSL 0:1239e9b70ca2 814 /* may still have /dev/random */
wolfSSL 0:1239e9b70ca2 815 os->fd = open("/dev/random",O_RDONLY);
wolfSSL 0:1239e9b70ca2 816 if (os->fd == -1)
wolfSSL 0:1239e9b70ca2 817 return OPEN_RAN_E;
wolfSSL 0:1239e9b70ca2 818 }
wolfSSL 0:1239e9b70ca2 819
wolfSSL 0:1239e9b70ca2 820 while (sz) {
wolfSSL 0:1239e9b70ca2 821 int len = (int)read(os->fd, output, sz);
wolfSSL 0:1239e9b70ca2 822 if (len == -1) {
wolfSSL 0:1239e9b70ca2 823 ret = READ_RAN_E;
wolfSSL 0:1239e9b70ca2 824 break;
wolfSSL 0:1239e9b70ca2 825 }
wolfSSL 0:1239e9b70ca2 826
wolfSSL 0:1239e9b70ca2 827 sz -= len;
wolfSSL 0:1239e9b70ca2 828 output += len;
wolfSSL 0:1239e9b70ca2 829
wolfSSL 0:1239e9b70ca2 830 if (sz) {
wolfSSL 0:1239e9b70ca2 831 #ifdef BLOCKING
wolfSSL 0:1239e9b70ca2 832 sleep(0); /* context switch */
wolfSSL 0:1239e9b70ca2 833 #else
wolfSSL 0:1239e9b70ca2 834 ret = RAN_BLOCK_E;
wolfSSL 0:1239e9b70ca2 835 break;
wolfSSL 0:1239e9b70ca2 836 #endif
wolfSSL 0:1239e9b70ca2 837 }
wolfSSL 0:1239e9b70ca2 838 }
wolfSSL 0:1239e9b70ca2 839 close(os->fd);
wolfSSL 0:1239e9b70ca2 840
wolfSSL 0:1239e9b70ca2 841 return ret;
wolfSSL 0:1239e9b70ca2 842 }
wolfSSL 0:1239e9b70ca2 843
wolfSSL 0:1239e9b70ca2 844 #endif /* USE_WINDOWS_API */
wolfSSL 0:1239e9b70ca2 845
wolfSSL 0:1239e9b70ca2 846