This is a port of cyaSSL 2.7.0.
Dependents: CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet
ctaocrypt/src/random.c@0:714293de3836, 2013-09-05 (annotated)
- Committer:
- ashleymills
- Date:
- Thu Sep 05 10:33:04 2013 +0000
- Revision:
- 0:714293de3836
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:714293de3836 | 1 | /* random.c |
ashleymills | 0:714293de3836 | 2 | * |
ashleymills | 0:714293de3836 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
ashleymills | 0:714293de3836 | 4 | * |
ashleymills | 0:714293de3836 | 5 | * This file is part of CyaSSL. |
ashleymills | 0:714293de3836 | 6 | * |
ashleymills | 0:714293de3836 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
ashleymills | 0:714293de3836 | 8 | * it under the terms of the GNU General Public License as published by |
ashleymills | 0:714293de3836 | 9 | * the Free Software Foundation; either version 2 of the License, or |
ashleymills | 0:714293de3836 | 10 | * (at your option) any later version. |
ashleymills | 0:714293de3836 | 11 | * |
ashleymills | 0:714293de3836 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
ashleymills | 0:714293de3836 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ashleymills | 0:714293de3836 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ashleymills | 0:714293de3836 | 15 | * GNU General Public License for more details. |
ashleymills | 0:714293de3836 | 16 | * |
ashleymills | 0:714293de3836 | 17 | * You should have received a copy of the GNU General Public License |
ashleymills | 0:714293de3836 | 18 | * along with this program; if not, write to the Free Software |
ashleymills | 0:714293de3836 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
ashleymills | 0:714293de3836 | 20 | */ |
ashleymills | 0:714293de3836 | 21 | |
ashleymills | 0:714293de3836 | 22 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:714293de3836 | 23 | #include <config.h> |
ashleymills | 0:714293de3836 | 24 | #endif |
ashleymills | 0:714293de3836 | 25 | |
ashleymills | 0:714293de3836 | 26 | /* on HPUX 11 you may need to install /dev/random see |
ashleymills | 0:714293de3836 | 27 | http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I |
ashleymills | 0:714293de3836 | 28 | |
ashleymills | 0:714293de3836 | 29 | */ |
ashleymills | 0:714293de3836 | 30 | |
ashleymills | 0:714293de3836 | 31 | #include <cyassl/ctaocrypt/random.h> |
ashleymills | 0:714293de3836 | 32 | #include <cyassl/ctaocrypt/ctaoerror2.h> |
ashleymills | 0:714293de3836 | 33 | |
ashleymills | 0:714293de3836 | 34 | #ifdef NO_RC4 |
ashleymills | 0:714293de3836 | 35 | #include <cyassl/ctaocrypt/sha256.h> |
ashleymills | 0:714293de3836 | 36 | |
ashleymills | 0:714293de3836 | 37 | #ifdef NO_INLINE |
ashleymills | 0:714293de3836 | 38 | #include <cyassl/ctaocrypt/misc.h> |
ashleymills | 0:714293de3836 | 39 | #else |
ashleymills | 0:714293de3836 | 40 | #define MISC_DUMM_FUNC misc_dummy_random |
ashleymills | 0:714293de3836 | 41 | #include <ctaocrypt/src/misc.c> |
ashleymills | 0:714293de3836 | 42 | #endif |
ashleymills | 0:714293de3836 | 43 | #endif |
ashleymills | 0:714293de3836 | 44 | |
ashleymills | 0:714293de3836 | 45 | #if defined(USE_WINDOWS_API) |
ashleymills | 0:714293de3836 | 46 | #ifndef _WIN32_WINNT |
ashleymills | 0:714293de3836 | 47 | #define _WIN32_WINNT 0x0400 |
ashleymills | 0:714293de3836 | 48 | #endif |
ashleymills | 0:714293de3836 | 49 | #include <windows.h> |
ashleymills | 0:714293de3836 | 50 | #include <wincrypt.h> |
ashleymills | 0:714293de3836 | 51 | #else |
ashleymills | 0:714293de3836 | 52 | #if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM) |
ashleymills | 0:714293de3836 | 53 | #include <fcntl.h> |
ashleymills | 0:714293de3836 | 54 | #ifndef EBSNET |
ashleymills | 0:714293de3836 | 55 | #include <unistd.h> |
ashleymills | 0:714293de3836 | 56 | #endif |
ashleymills | 0:714293de3836 | 57 | #else |
ashleymills | 0:714293de3836 | 58 | /* include headers that may be needed to get good seed */ |
ashleymills | 0:714293de3836 | 59 | #endif |
ashleymills | 0:714293de3836 | 60 | #endif /* USE_WINDOWS_API */ |
ashleymills | 0:714293de3836 | 61 | |
ashleymills | 0:714293de3836 | 62 | |
ashleymills | 0:714293de3836 | 63 | #ifdef NO_RC4 |
ashleymills | 0:714293de3836 | 64 | |
ashleymills | 0:714293de3836 | 65 | /* Start NIST DRBG code */ |
ashleymills | 0:714293de3836 | 66 | |
ashleymills | 0:714293de3836 | 67 | #define OUTPUT_BLOCK_LEN (256/8) |
ashleymills | 0:714293de3836 | 68 | #define MAX_REQUEST_LEN (0x1000) |
ashleymills | 0:714293de3836 | 69 | #define MAX_STRING_LEN (0x100000000) |
ashleymills | 0:714293de3836 | 70 | #define RESEED_MAX (0x100000000000LL) |
ashleymills | 0:714293de3836 | 71 | #define ENTROPY_SZ 256 |
ashleymills | 0:714293de3836 | 72 | |
ashleymills | 0:714293de3836 | 73 | #define DBRG_SUCCESS 0 |
ashleymills | 0:714293de3836 | 74 | #define DBRG_ERROR 1 |
ashleymills | 0:714293de3836 | 75 | #define DBRG_NEED_RESEED 2 |
ashleymills | 0:714293de3836 | 76 | |
ashleymills | 0:714293de3836 | 77 | |
ashleymills | 0:714293de3836 | 78 | enum { |
ashleymills | 0:714293de3836 | 79 | dbrgInitC = 0, |
ashleymills | 0:714293de3836 | 80 | dbrgReseed = 1, |
ashleymills | 0:714293de3836 | 81 | dbrgGenerateW = 2, |
ashleymills | 0:714293de3836 | 82 | dbrgGenerateH = 3, |
ashleymills | 0:714293de3836 | 83 | dbrgInitV |
ashleymills | 0:714293de3836 | 84 | }; |
ashleymills | 0:714293de3836 | 85 | |
ashleymills | 0:714293de3836 | 86 | |
ashleymills | 0:714293de3836 | 87 | static int Hash_df(RNG* rng, byte* out, word32 outSz, byte type, byte* inA, word32 inASz, |
ashleymills | 0:714293de3836 | 88 | byte* inB, word32 inBSz, byte* inC, word32 inCSz) |
ashleymills | 0:714293de3836 | 89 | { |
ashleymills | 0:714293de3836 | 90 | byte ctr; |
ashleymills | 0:714293de3836 | 91 | int i; |
ashleymills | 0:714293de3836 | 92 | int len; |
ashleymills | 0:714293de3836 | 93 | word32 bits = (outSz * 8); /* reverse byte order */ |
ashleymills | 0:714293de3836 | 94 | |
ashleymills | 0:714293de3836 | 95 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 96 | bits = ByteReverseWord32(bits); |
ashleymills | 0:714293de3836 | 97 | #endif |
ashleymills | 0:714293de3836 | 98 | len = (outSz / SHA256_DIGEST_SIZE) |
ashleymills | 0:714293de3836 | 99 | + ((outSz % SHA256_DIGEST_SIZE) ? 1 : 0); |
ashleymills | 0:714293de3836 | 100 | |
ashleymills | 0:714293de3836 | 101 | for (i = 0, ctr = 1; i < len; i++, ctr++) |
ashleymills | 0:714293de3836 | 102 | { |
ashleymills | 0:714293de3836 | 103 | InitSha256(&rng->sha); |
ashleymills | 0:714293de3836 | 104 | Sha256Update(&rng->sha, &ctr, sizeof(ctr)); |
ashleymills | 0:714293de3836 | 105 | Sha256Update(&rng->sha, (byte*)&bits, sizeof(bits)); |
ashleymills | 0:714293de3836 | 106 | /* churning V is the only string that doesn't have |
ashleymills | 0:714293de3836 | 107 | * the type added */ |
ashleymills | 0:714293de3836 | 108 | if (type != dbrgInitV) |
ashleymills | 0:714293de3836 | 109 | Sha256Update(&rng->sha, &type, sizeof(type)); |
ashleymills | 0:714293de3836 | 110 | Sha256Update(&rng->sha, inA, inASz); |
ashleymills | 0:714293de3836 | 111 | if (inB != NULL && inBSz > 0) |
ashleymills | 0:714293de3836 | 112 | Sha256Update(&rng->sha, inB, inBSz); |
ashleymills | 0:714293de3836 | 113 | if (inC != NULL && inCSz > 0) |
ashleymills | 0:714293de3836 | 114 | Sha256Update(&rng->sha, inC, inCSz); |
ashleymills | 0:714293de3836 | 115 | Sha256Final(&rng->sha, rng->digest); |
ashleymills | 0:714293de3836 | 116 | |
ashleymills | 0:714293de3836 | 117 | if (outSz > SHA256_DIGEST_SIZE) { |
ashleymills | 0:714293de3836 | 118 | XMEMCPY(out, rng->digest, SHA256_DIGEST_SIZE); |
ashleymills | 0:714293de3836 | 119 | outSz -= SHA256_DIGEST_SIZE; |
ashleymills | 0:714293de3836 | 120 | out += SHA256_DIGEST_SIZE; |
ashleymills | 0:714293de3836 | 121 | } |
ashleymills | 0:714293de3836 | 122 | else { |
ashleymills | 0:714293de3836 | 123 | XMEMCPY(out, rng->digest, outSz); |
ashleymills | 0:714293de3836 | 124 | } |
ashleymills | 0:714293de3836 | 125 | } |
ashleymills | 0:714293de3836 | 126 | |
ashleymills | 0:714293de3836 | 127 | return DBRG_SUCCESS; |
ashleymills | 0:714293de3836 | 128 | } |
ashleymills | 0:714293de3836 | 129 | |
ashleymills | 0:714293de3836 | 130 | |
ashleymills | 0:714293de3836 | 131 | static int Hash_DBRG_Reseed(RNG* rng, byte* entropy, word32 entropySz) |
ashleymills | 0:714293de3836 | 132 | { |
ashleymills | 0:714293de3836 | 133 | byte seed[DBRG_SEED_LEN]; |
ashleymills | 0:714293de3836 | 134 | |
ashleymills | 0:714293de3836 | 135 | Hash_df(rng, seed, sizeof(seed), dbrgInitV, rng->V, sizeof(rng->V), |
ashleymills | 0:714293de3836 | 136 | entropy, entropySz, NULL, 0); |
ashleymills | 0:714293de3836 | 137 | XMEMCPY(rng->V, seed, sizeof(rng->V)); |
ashleymills | 0:714293de3836 | 138 | XMEMSET(seed, 0, sizeof(seed)); |
ashleymills | 0:714293de3836 | 139 | |
ashleymills | 0:714293de3836 | 140 | Hash_df(rng, rng->C, sizeof(rng->C), dbrgInitC, rng->V, sizeof(rng->V), |
ashleymills | 0:714293de3836 | 141 | NULL, 0, NULL, 0); |
ashleymills | 0:714293de3836 | 142 | rng->reseed_ctr = 1; |
ashleymills | 0:714293de3836 | 143 | return 0; |
ashleymills | 0:714293de3836 | 144 | } |
ashleymills | 0:714293de3836 | 145 | |
ashleymills | 0:714293de3836 | 146 | static INLINE void array_add_one(byte* data, word32 dataSz) |
ashleymills | 0:714293de3836 | 147 | { |
ashleymills | 0:714293de3836 | 148 | int i; |
ashleymills | 0:714293de3836 | 149 | |
ashleymills | 0:714293de3836 | 150 | for (i = dataSz - 1; i >= 0; i--) |
ashleymills | 0:714293de3836 | 151 | { |
ashleymills | 0:714293de3836 | 152 | data[i]++; |
ashleymills | 0:714293de3836 | 153 | if (data[i] != 0) break; |
ashleymills | 0:714293de3836 | 154 | } |
ashleymills | 0:714293de3836 | 155 | } |
ashleymills | 0:714293de3836 | 156 | |
ashleymills | 0:714293de3836 | 157 | static void Hash_gen(RNG* rng, byte* out, word32 outSz, byte* V) |
ashleymills | 0:714293de3836 | 158 | { |
ashleymills | 0:714293de3836 | 159 | byte data[DBRG_SEED_LEN]; |
ashleymills | 0:714293de3836 | 160 | int i; |
ashleymills | 0:714293de3836 | 161 | int len = (outSz / SHA256_DIGEST_SIZE) |
ashleymills | 0:714293de3836 | 162 | + ((outSz % SHA256_DIGEST_SIZE) ? 1 : 0); |
ashleymills | 0:714293de3836 | 163 | |
ashleymills | 0:714293de3836 | 164 | XMEMCPY(data, V, sizeof(data)); |
ashleymills | 0:714293de3836 | 165 | for (i = 0; i < len; i++) { |
ashleymills | 0:714293de3836 | 166 | InitSha256(&rng->sha); |
ashleymills | 0:714293de3836 | 167 | Sha256Update(&rng->sha, data, sizeof(data)); |
ashleymills | 0:714293de3836 | 168 | Sha256Final(&rng->sha, rng->digest); |
ashleymills | 0:714293de3836 | 169 | if (outSz > SHA256_DIGEST_SIZE) { |
ashleymills | 0:714293de3836 | 170 | XMEMCPY(out, rng->digest, SHA256_DIGEST_SIZE); |
ashleymills | 0:714293de3836 | 171 | outSz -= SHA256_DIGEST_SIZE; |
ashleymills | 0:714293de3836 | 172 | out += SHA256_DIGEST_SIZE; |
ashleymills | 0:714293de3836 | 173 | array_add_one(data, DBRG_SEED_LEN); |
ashleymills | 0:714293de3836 | 174 | } |
ashleymills | 0:714293de3836 | 175 | else { |
ashleymills | 0:714293de3836 | 176 | XMEMCPY(out, rng->digest, outSz); |
ashleymills | 0:714293de3836 | 177 | } |
ashleymills | 0:714293de3836 | 178 | } |
ashleymills | 0:714293de3836 | 179 | XMEMSET(data, 0, sizeof(data)); |
ashleymills | 0:714293de3836 | 180 | } |
ashleymills | 0:714293de3836 | 181 | |
ashleymills | 0:714293de3836 | 182 | |
ashleymills | 0:714293de3836 | 183 | static INLINE void array_add(byte* d, word32 dLen, byte* s, word32 sLen) |
ashleymills | 0:714293de3836 | 184 | { |
ashleymills | 0:714293de3836 | 185 | word16 carry = 0; |
ashleymills | 0:714293de3836 | 186 | |
ashleymills | 0:714293de3836 | 187 | if (dLen > 0 && sLen > 0 && dLen >= sLen) { |
ashleymills | 0:714293de3836 | 188 | int sIdx, dIdx; |
ashleymills | 0:714293de3836 | 189 | |
ashleymills | 0:714293de3836 | 190 | for (sIdx = sLen - 1, dIdx = dLen - 1; sIdx >= 0; dIdx--, sIdx--) |
ashleymills | 0:714293de3836 | 191 | { |
ashleymills | 0:714293de3836 | 192 | carry += d[dIdx] + s[sIdx]; |
ashleymills | 0:714293de3836 | 193 | d[dIdx] = carry; |
ashleymills | 0:714293de3836 | 194 | carry >>= 8; |
ashleymills | 0:714293de3836 | 195 | } |
ashleymills | 0:714293de3836 | 196 | if (dIdx > 0) |
ashleymills | 0:714293de3836 | 197 | d[dIdx] += carry; |
ashleymills | 0:714293de3836 | 198 | } |
ashleymills | 0:714293de3836 | 199 | } |
ashleymills | 0:714293de3836 | 200 | |
ashleymills | 0:714293de3836 | 201 | |
ashleymills | 0:714293de3836 | 202 | static int Hash_DBRG_Generate(RNG* rng, byte* out, word32 outSz) |
ashleymills | 0:714293de3836 | 203 | { |
ashleymills | 0:714293de3836 | 204 | int ret; |
ashleymills | 0:714293de3836 | 205 | |
ashleymills | 0:714293de3836 | 206 | if (rng->reseed_ctr != RESEED_MAX) { |
ashleymills | 0:714293de3836 | 207 | byte type = dbrgGenerateH; |
ashleymills | 0:714293de3836 | 208 | |
ashleymills | 0:714293de3836 | 209 | Hash_gen(rng, out, outSz, rng->V); |
ashleymills | 0:714293de3836 | 210 | InitSha256(&rng->sha); |
ashleymills | 0:714293de3836 | 211 | Sha256Update(&rng->sha, &type, sizeof(type)); |
ashleymills | 0:714293de3836 | 212 | Sha256Update(&rng->sha, rng->V, sizeof(rng->V)); |
ashleymills | 0:714293de3836 | 213 | Sha256Final(&rng->sha, rng->digest); |
ashleymills | 0:714293de3836 | 214 | array_add(rng->V, sizeof(rng->V), rng->digest, sizeof(rng->digest)); |
ashleymills | 0:714293de3836 | 215 | array_add(rng->V, sizeof(rng->V), rng->C, sizeof(rng->C)); |
ashleymills | 0:714293de3836 | 216 | array_add(rng->V, sizeof(rng->V), |
ashleymills | 0:714293de3836 | 217 | (byte*)&rng->reseed_ctr, sizeof(rng->reseed_ctr)); |
ashleymills | 0:714293de3836 | 218 | rng->reseed_ctr++; |
ashleymills | 0:714293de3836 | 219 | ret = DBRG_SUCCESS; |
ashleymills | 0:714293de3836 | 220 | } |
ashleymills | 0:714293de3836 | 221 | else { |
ashleymills | 0:714293de3836 | 222 | ret = DBRG_NEED_RESEED; |
ashleymills | 0:714293de3836 | 223 | } |
ashleymills | 0:714293de3836 | 224 | return ret; |
ashleymills | 0:714293de3836 | 225 | } |
ashleymills | 0:714293de3836 | 226 | |
ashleymills | 0:714293de3836 | 227 | |
ashleymills | 0:714293de3836 | 228 | static void Hash_DBRG_Instantiate(RNG* rng, byte* seed, word32 seedSz) |
ashleymills | 0:714293de3836 | 229 | { |
ashleymills | 0:714293de3836 | 230 | XMEMSET(rng, 0, sizeof(*rng)); |
ashleymills | 0:714293de3836 | 231 | Hash_df(rng, rng->V, sizeof(rng->V), dbrgInitV, seed, seedSz, NULL, 0, NULL, 0); |
ashleymills | 0:714293de3836 | 232 | Hash_df(rng, rng->C, sizeof(rng->C), dbrgInitC, rng->V, sizeof(rng->V), |
ashleymills | 0:714293de3836 | 233 | NULL, 0, NULL, 0); |
ashleymills | 0:714293de3836 | 234 | rng->reseed_ctr = 1; |
ashleymills | 0:714293de3836 | 235 | } |
ashleymills | 0:714293de3836 | 236 | |
ashleymills | 0:714293de3836 | 237 | |
ashleymills | 0:714293de3836 | 238 | static int Hash_DBRG_Uninstantiate(RNG* rng) |
ashleymills | 0:714293de3836 | 239 | { |
ashleymills | 0:714293de3836 | 240 | int result = DBRG_ERROR; |
ashleymills | 0:714293de3836 | 241 | |
ashleymills | 0:714293de3836 | 242 | if (rng != NULL) { |
ashleymills | 0:714293de3836 | 243 | XMEMSET(rng, 0, sizeof(*rng)); |
ashleymills | 0:714293de3836 | 244 | result = DBRG_SUCCESS; |
ashleymills | 0:714293de3836 | 245 | } |
ashleymills | 0:714293de3836 | 246 | |
ashleymills | 0:714293de3836 | 247 | return result; |
ashleymills | 0:714293de3836 | 248 | } |
ashleymills | 0:714293de3836 | 249 | |
ashleymills | 0:714293de3836 | 250 | /* End NIST DRBG Code */ |
ashleymills | 0:714293de3836 | 251 | |
ashleymills | 0:714293de3836 | 252 | |
ashleymills | 0:714293de3836 | 253 | |
ashleymills | 0:714293de3836 | 254 | /* Get seed and key cipher */ |
ashleymills | 0:714293de3836 | 255 | int InitRng(RNG* rng) |
ashleymills | 0:714293de3836 | 256 | { |
ashleymills | 0:714293de3836 | 257 | byte entropy[ENTROPY_SZ]; |
ashleymills | 0:714293de3836 | 258 | int ret = DBRG_ERROR; |
ashleymills | 0:714293de3836 | 259 | |
ashleymills | 0:714293de3836 | 260 | if (GenerateSeed(&rng->seed, entropy, sizeof(entropy)) == 0) { |
ashleymills | 0:714293de3836 | 261 | Hash_DBRG_Instantiate(rng, entropy, sizeof(entropy)); |
ashleymills | 0:714293de3836 | 262 | ret = DBRG_SUCCESS; |
ashleymills | 0:714293de3836 | 263 | } |
ashleymills | 0:714293de3836 | 264 | XMEMSET(entropy, 0, sizeof(entropy)); |
ashleymills | 0:714293de3836 | 265 | return ret; |
ashleymills | 0:714293de3836 | 266 | } |
ashleymills | 0:714293de3836 | 267 | |
ashleymills | 0:714293de3836 | 268 | |
ashleymills | 0:714293de3836 | 269 | /* place a generated block in output */ |
ashleymills | 0:714293de3836 | 270 | void RNG_GenerateBlock(RNG* rng, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 271 | { |
ashleymills | 0:714293de3836 | 272 | int ret; |
ashleymills | 0:714293de3836 | 273 | |
ashleymills | 0:714293de3836 | 274 | XMEMSET(output, 0, sz); |
ashleymills | 0:714293de3836 | 275 | ret = Hash_DBRG_Generate(rng, output, sz); |
ashleymills | 0:714293de3836 | 276 | if (ret == DBRG_NEED_RESEED) { |
ashleymills | 0:714293de3836 | 277 | byte entropy[ENTROPY_SZ]; |
ashleymills | 0:714293de3836 | 278 | ret = GenerateSeed(&rng->seed, entropy, sizeof(entropy)); |
ashleymills | 0:714293de3836 | 279 | if (ret == 0) { |
ashleymills | 0:714293de3836 | 280 | Hash_DBRG_Reseed(rng, entropy, sizeof(entropy)); |
ashleymills | 0:714293de3836 | 281 | ret = Hash_DBRG_Generate(rng, output, sz); |
ashleymills | 0:714293de3836 | 282 | } |
ashleymills | 0:714293de3836 | 283 | else |
ashleymills | 0:714293de3836 | 284 | ret = DBRG_ERROR; |
ashleymills | 0:714293de3836 | 285 | XMEMSET(entropy, 0, sizeof(entropy)); |
ashleymills | 0:714293de3836 | 286 | } |
ashleymills | 0:714293de3836 | 287 | } |
ashleymills | 0:714293de3836 | 288 | |
ashleymills | 0:714293de3836 | 289 | |
ashleymills | 0:714293de3836 | 290 | byte RNG_GenerateByte(RNG* rng) |
ashleymills | 0:714293de3836 | 291 | { |
ashleymills | 0:714293de3836 | 292 | byte b; |
ashleymills | 0:714293de3836 | 293 | RNG_GenerateBlock(rng, &b, 1); |
ashleymills | 0:714293de3836 | 294 | |
ashleymills | 0:714293de3836 | 295 | return b; |
ashleymills | 0:714293de3836 | 296 | } |
ashleymills | 0:714293de3836 | 297 | |
ashleymills | 0:714293de3836 | 298 | |
ashleymills | 0:714293de3836 | 299 | void FreeRng(RNG* rng) |
ashleymills | 0:714293de3836 | 300 | { |
ashleymills | 0:714293de3836 | 301 | Hash_DBRG_Uninstantiate(rng); |
ashleymills | 0:714293de3836 | 302 | } |
ashleymills | 0:714293de3836 | 303 | |
ashleymills | 0:714293de3836 | 304 | #else /* NO_RC4 */ |
ashleymills | 0:714293de3836 | 305 | |
ashleymills | 0:714293de3836 | 306 | /* Get seed and key cipher */ |
ashleymills | 0:714293de3836 | 307 | int InitRng(RNG* rng) |
ashleymills | 0:714293de3836 | 308 | { |
ashleymills | 0:714293de3836 | 309 | byte key[32]; |
ashleymills | 0:714293de3836 | 310 | byte junk[256]; |
ashleymills | 0:714293de3836 | 311 | int ret; |
ashleymills | 0:714293de3836 | 312 | |
ashleymills | 0:714293de3836 | 313 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 314 | if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC) |
ashleymills | 0:714293de3836 | 315 | return 0; |
ashleymills | 0:714293de3836 | 316 | #endif |
ashleymills | 0:714293de3836 | 317 | ret = GenerateSeed(&rng->seed, key, sizeof(key)); |
ashleymills | 0:714293de3836 | 318 | |
ashleymills | 0:714293de3836 | 319 | if (ret == 0) { |
ashleymills | 0:714293de3836 | 320 | Arc4SetKey(&rng->cipher, key, sizeof(key)); |
ashleymills | 0:714293de3836 | 321 | RNG_GenerateBlock(rng, junk, sizeof(junk)); /* rid initial state */ |
ashleymills | 0:714293de3836 | 322 | } |
ashleymills | 0:714293de3836 | 323 | |
ashleymills | 0:714293de3836 | 324 | return ret; |
ashleymills | 0:714293de3836 | 325 | } |
ashleymills | 0:714293de3836 | 326 | |
ashleymills | 0:714293de3836 | 327 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 328 | static void CaviumRNG_GenerateBlock(RNG* rng, byte* output, word32 sz); |
ashleymills | 0:714293de3836 | 329 | #endif |
ashleymills | 0:714293de3836 | 330 | |
ashleymills | 0:714293de3836 | 331 | /* place a generated block in output */ |
ashleymills | 0:714293de3836 | 332 | void RNG_GenerateBlock(RNG* rng, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 333 | { |
ashleymills | 0:714293de3836 | 334 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 335 | if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC) |
ashleymills | 0:714293de3836 | 336 | return CaviumRNG_GenerateBlock(rng, output, sz); |
ashleymills | 0:714293de3836 | 337 | #endif |
ashleymills | 0:714293de3836 | 338 | XMEMSET(output, 0, sz); |
ashleymills | 0:714293de3836 | 339 | Arc4Process(&rng->cipher, output, output, sz); |
ashleymills | 0:714293de3836 | 340 | } |
ashleymills | 0:714293de3836 | 341 | |
ashleymills | 0:714293de3836 | 342 | |
ashleymills | 0:714293de3836 | 343 | byte RNG_GenerateByte(RNG* rng) |
ashleymills | 0:714293de3836 | 344 | { |
ashleymills | 0:714293de3836 | 345 | byte b; |
ashleymills | 0:714293de3836 | 346 | RNG_GenerateBlock(rng, &b, 1); |
ashleymills | 0:714293de3836 | 347 | |
ashleymills | 0:714293de3836 | 348 | return b; |
ashleymills | 0:714293de3836 | 349 | } |
ashleymills | 0:714293de3836 | 350 | |
ashleymills | 0:714293de3836 | 351 | |
ashleymills | 0:714293de3836 | 352 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 353 | |
ashleymills | 0:714293de3836 | 354 | #include <cyassl/ctaocrypt/logging.h> |
ashleymills | 0:714293de3836 | 355 | #include "cavium_common.h" |
ashleymills | 0:714293de3836 | 356 | |
ashleymills | 0:714293de3836 | 357 | /* Initiliaze RNG for use with Nitrox device */ |
ashleymills | 0:714293de3836 | 358 | int InitRngCavium(RNG* rng, int devId) |
ashleymills | 0:714293de3836 | 359 | { |
ashleymills | 0:714293de3836 | 360 | if (rng == NULL) |
ashleymills | 0:714293de3836 | 361 | return -1; |
ashleymills | 0:714293de3836 | 362 | |
ashleymills | 0:714293de3836 | 363 | rng->devId = devId; |
ashleymills | 0:714293de3836 | 364 | rng->magic = CYASSL_RNG_CAVIUM_MAGIC; |
ashleymills | 0:714293de3836 | 365 | |
ashleymills | 0:714293de3836 | 366 | return 0; |
ashleymills | 0:714293de3836 | 367 | } |
ashleymills | 0:714293de3836 | 368 | |
ashleymills | 0:714293de3836 | 369 | |
ashleymills | 0:714293de3836 | 370 | static void CaviumRNG_GenerateBlock(RNG* rng, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 371 | { |
ashleymills | 0:714293de3836 | 372 | word offset = 0; |
ashleymills | 0:714293de3836 | 373 | word32 requestId; |
ashleymills | 0:714293de3836 | 374 | |
ashleymills | 0:714293de3836 | 375 | while (sz > CYASSL_MAX_16BIT) { |
ashleymills | 0:714293de3836 | 376 | word16 slen = (word16)CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 377 | if (CspRandom(CAVIUM_BLOCKING, slen, output + offset, &requestId, |
ashleymills | 0:714293de3836 | 378 | rng->devId) != 0) { |
ashleymills | 0:714293de3836 | 379 | CYASSL_MSG("Cavium RNG failed"); |
ashleymills | 0:714293de3836 | 380 | } |
ashleymills | 0:714293de3836 | 381 | sz -= CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 382 | offset += CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 383 | } |
ashleymills | 0:714293de3836 | 384 | if (sz) { |
ashleymills | 0:714293de3836 | 385 | word16 slen = (word16)sz; |
ashleymills | 0:714293de3836 | 386 | if (CspRandom(CAVIUM_BLOCKING, slen, output + offset, &requestId, |
ashleymills | 0:714293de3836 | 387 | rng->devId) != 0) { |
ashleymills | 0:714293de3836 | 388 | CYASSL_MSG("Cavium RNG failed"); |
ashleymills | 0:714293de3836 | 389 | } |
ashleymills | 0:714293de3836 | 390 | } |
ashleymills | 0:714293de3836 | 391 | } |
ashleymills | 0:714293de3836 | 392 | |
ashleymills | 0:714293de3836 | 393 | #endif /* HAVE_CAVIUM */ |
ashleymills | 0:714293de3836 | 394 | |
ashleymills | 0:714293de3836 | 395 | #endif /* NO_RC4 */ |
ashleymills | 0:714293de3836 | 396 | |
ashleymills | 0:714293de3836 | 397 | |
ashleymills | 0:714293de3836 | 398 | #if defined(USE_WINDOWS_API) |
ashleymills | 0:714293de3836 | 399 | |
ashleymills | 0:714293de3836 | 400 | |
ashleymills | 0:714293de3836 | 401 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 402 | { |
ashleymills | 0:714293de3836 | 403 | if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL, |
ashleymills | 0:714293de3836 | 404 | CRYPT_VERIFYCONTEXT)) |
ashleymills | 0:714293de3836 | 405 | return WINCRYPT_E; |
ashleymills | 0:714293de3836 | 406 | |
ashleymills | 0:714293de3836 | 407 | if (!CryptGenRandom(os->handle, sz, output)) |
ashleymills | 0:714293de3836 | 408 | return CRYPTGEN_E; |
ashleymills | 0:714293de3836 | 409 | |
ashleymills | 0:714293de3836 | 410 | CryptReleaseContext(os->handle, 0); |
ashleymills | 0:714293de3836 | 411 | |
ashleymills | 0:714293de3836 | 412 | return 0; |
ashleymills | 0:714293de3836 | 413 | } |
ashleymills | 0:714293de3836 | 414 | |
ashleymills | 0:714293de3836 | 415 | |
ashleymills | 0:714293de3836 | 416 | #elif defined(HAVE_RTP_SYS) || defined(EBSNET) |
ashleymills | 0:714293de3836 | 417 | |
ashleymills | 0:714293de3836 | 418 | #include "rtprand.h" /* rtp_rand () */ |
ashleymills | 0:714293de3836 | 419 | #include "rtptime.h" /* rtp_get_system_msec() */ |
ashleymills | 0:714293de3836 | 420 | |
ashleymills | 0:714293de3836 | 421 | |
ashleymills | 0:714293de3836 | 422 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 423 | { |
ashleymills | 0:714293de3836 | 424 | int i; |
ashleymills | 0:714293de3836 | 425 | rtp_srand(rtp_get_system_msec()); |
ashleymills | 0:714293de3836 | 426 | |
ashleymills | 0:714293de3836 | 427 | for (i = 0; i < sz; i++ ) { |
ashleymills | 0:714293de3836 | 428 | output[i] = rtp_rand() % 256; |
ashleymills | 0:714293de3836 | 429 | if ( (i % 8) == 7) |
ashleymills | 0:714293de3836 | 430 | rtp_srand(rtp_get_system_msec()); |
ashleymills | 0:714293de3836 | 431 | } |
ashleymills | 0:714293de3836 | 432 | |
ashleymills | 0:714293de3836 | 433 | return 0; |
ashleymills | 0:714293de3836 | 434 | } |
ashleymills | 0:714293de3836 | 435 | |
ashleymills | 0:714293de3836 | 436 | |
ashleymills | 0:714293de3836 | 437 | #elif defined(MICRIUM) |
ashleymills | 0:714293de3836 | 438 | |
ashleymills | 0:714293de3836 | 439 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 440 | { |
ashleymills | 0:714293de3836 | 441 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
ashleymills | 0:714293de3836 | 442 | NetSecure_InitSeed(output, sz); |
ashleymills | 0:714293de3836 | 443 | #endif |
ashleymills | 0:714293de3836 | 444 | return 0; |
ashleymills | 0:714293de3836 | 445 | } |
ashleymills | 0:714293de3836 | 446 | |
ashleymills | 0:714293de3836 | 447 | #elif defined(MBED) |
ashleymills | 0:714293de3836 | 448 | |
ashleymills | 0:714293de3836 | 449 | /* write a real one !!!, just for testing board */ |
ashleymills | 0:714293de3836 | 450 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 451 | { |
ashleymills | 0:714293de3836 | 452 | int i; |
ashleymills | 0:714293de3836 | 453 | for (i = 0; i < sz; i++ ) |
ashleymills | 0:714293de3836 | 454 | output[i] = i; |
ashleymills | 0:714293de3836 | 455 | |
ashleymills | 0:714293de3836 | 456 | return 0; |
ashleymills | 0:714293de3836 | 457 | } |
ashleymills | 0:714293de3836 | 458 | |
ashleymills | 0:714293de3836 | 459 | #elif defined(MICROCHIP_PIC32) |
ashleymills | 0:714293de3836 | 460 | |
ashleymills | 0:714293de3836 | 461 | #include <peripheral/timer.h> |
ashleymills | 0:714293de3836 | 462 | |
ashleymills | 0:714293de3836 | 463 | /* uses the core timer, in nanoseconds to seed srand */ |
ashleymills | 0:714293de3836 | 464 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 465 | { |
ashleymills | 0:714293de3836 | 466 | int i; |
ashleymills | 0:714293de3836 | 467 | srand(ReadCoreTimer() * 25); |
ashleymills | 0:714293de3836 | 468 | |
ashleymills | 0:714293de3836 | 469 | for (i = 0; i < sz; i++ ) { |
ashleymills | 0:714293de3836 | 470 | output[i] = rand() % 256; |
ashleymills | 0:714293de3836 | 471 | if ( (i % 8) == 7) |
ashleymills | 0:714293de3836 | 472 | srand(ReadCoreTimer() * 25); |
ashleymills | 0:714293de3836 | 473 | } |
ashleymills | 0:714293de3836 | 474 | |
ashleymills | 0:714293de3836 | 475 | return 0; |
ashleymills | 0:714293de3836 | 476 | } |
ashleymills | 0:714293de3836 | 477 | |
ashleymills | 0:714293de3836 | 478 | #elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK) |
ashleymills | 0:714293de3836 | 479 | |
ashleymills | 0:714293de3836 | 480 | #warning "write a real random seed!!!!, just for testing now" |
ashleymills | 0:714293de3836 | 481 | |
ashleymills | 0:714293de3836 | 482 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 483 | { |
ashleymills | 0:714293de3836 | 484 | word32 i; |
ashleymills | 0:714293de3836 | 485 | for (i = 0; i < sz; i++ ) |
ashleymills | 0:714293de3836 | 486 | output[i] = i; |
ashleymills | 0:714293de3836 | 487 | |
ashleymills | 0:714293de3836 | 488 | (void)os; |
ashleymills | 0:714293de3836 | 489 | |
ashleymills | 0:714293de3836 | 490 | return 0; |
ashleymills | 0:714293de3836 | 491 | } |
ashleymills | 0:714293de3836 | 492 | |
ashleymills | 0:714293de3836 | 493 | #elif defined(FREESCALE_MQX) |
ashleymills | 0:714293de3836 | 494 | |
ashleymills | 0:714293de3836 | 495 | #ifdef FREESCALE_K70_RNGA |
ashleymills | 0:714293de3836 | 496 | /* |
ashleymills | 0:714293de3836 | 497 | * Generates a RNG seed using the Random Number Generator Accelerator |
ashleymills | 0:714293de3836 | 498 | * on the Kinetis K70. Documentation located in Chapter 37 of |
ashleymills | 0:714293de3836 | 499 | * K70 Sub-Family Reference Manual (see Note 3 in the README for link). |
ashleymills | 0:714293de3836 | 500 | */ |
ashleymills | 0:714293de3836 | 501 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 502 | { |
ashleymills | 0:714293de3836 | 503 | int i; |
ashleymills | 0:714293de3836 | 504 | |
ashleymills | 0:714293de3836 | 505 | /* turn on RNGA module */ |
ashleymills | 0:714293de3836 | 506 | SIM_SCGC3 |= SIM_SCGC3_RNGA_MASK; |
ashleymills | 0:714293de3836 | 507 | |
ashleymills | 0:714293de3836 | 508 | /* set SLP bit to 0 - "RNGA is not in sleep mode" */ |
ashleymills | 0:714293de3836 | 509 | RNG_CR &= ~RNG_CR_SLP_MASK; |
ashleymills | 0:714293de3836 | 510 | |
ashleymills | 0:714293de3836 | 511 | /* set HA bit to 1 - "security violations masked" */ |
ashleymills | 0:714293de3836 | 512 | RNG_CR |= RNG_CR_HA_MASK; |
ashleymills | 0:714293de3836 | 513 | |
ashleymills | 0:714293de3836 | 514 | /* set GO bit to 1 - "output register loaded with data" */ |
ashleymills | 0:714293de3836 | 515 | RNG_CR |= RNG_CR_GO_MASK; |
ashleymills | 0:714293de3836 | 516 | |
ashleymills | 0:714293de3836 | 517 | for (i = 0; i < sz; i++) { |
ashleymills | 0:714293de3836 | 518 | |
ashleymills | 0:714293de3836 | 519 | /* wait for RNG FIFO to be full */ |
ashleymills | 0:714293de3836 | 520 | while((RNG_SR & RNG_SR_OREG_LVL(0xF)) == 0) {} |
ashleymills | 0:714293de3836 | 521 | |
ashleymills | 0:714293de3836 | 522 | /* get value */ |
ashleymills | 0:714293de3836 | 523 | output[i] = RNG_OR; |
ashleymills | 0:714293de3836 | 524 | } |
ashleymills | 0:714293de3836 | 525 | |
ashleymills | 0:714293de3836 | 526 | return 0; |
ashleymills | 0:714293de3836 | 527 | } |
ashleymills | 0:714293de3836 | 528 | #else |
ashleymills | 0:714293de3836 | 529 | #warning "write a real random seed!!!!, just for testing now" |
ashleymills | 0:714293de3836 | 530 | |
ashleymills | 0:714293de3836 | 531 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 532 | { |
ashleymills | 0:714293de3836 | 533 | int i; |
ashleymills | 0:714293de3836 | 534 | for (i = 0; i < sz; i++ ) |
ashleymills | 0:714293de3836 | 535 | output[i] = i; |
ashleymills | 0:714293de3836 | 536 | |
ashleymills | 0:714293de3836 | 537 | return 0; |
ashleymills | 0:714293de3836 | 538 | } |
ashleymills | 0:714293de3836 | 539 | #endif /* FREESCALE_K70_RNGA */ |
ashleymills | 0:714293de3836 | 540 | |
ashleymills | 0:714293de3836 | 541 | #elif defined(STM32F2_RNG) |
ashleymills | 0:714293de3836 | 542 | #undef RNG |
ashleymills | 0:714293de3836 | 543 | #include "stm32f2xx_rng.h" |
ashleymills | 0:714293de3836 | 544 | #include "stm32f2xx_rcc.h" |
ashleymills | 0:714293de3836 | 545 | /* |
ashleymills | 0:714293de3836 | 546 | * Generate a RNG seed using the hardware random number generator |
ashleymills | 0:714293de3836 | 547 | * on the STM32F2. Documentation located in STM32F2xx Standard Peripheral |
ashleymills | 0:714293de3836 | 548 | * Library document (See note in README). |
ashleymills | 0:714293de3836 | 549 | */ |
ashleymills | 0:714293de3836 | 550 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 551 | { |
ashleymills | 0:714293de3836 | 552 | int i; |
ashleymills | 0:714293de3836 | 553 | |
ashleymills | 0:714293de3836 | 554 | /* enable RNG clock source */ |
ashleymills | 0:714293de3836 | 555 | RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE); |
ashleymills | 0:714293de3836 | 556 | |
ashleymills | 0:714293de3836 | 557 | /* enable RNG peripheral */ |
ashleymills | 0:714293de3836 | 558 | RNG_Cmd(ENABLE); |
ashleymills | 0:714293de3836 | 559 | |
ashleymills | 0:714293de3836 | 560 | for (i = 0; i < sz; i++) { |
ashleymills | 0:714293de3836 | 561 | /* wait until RNG number is ready */ |
ashleymills | 0:714293de3836 | 562 | while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET) { } |
ashleymills | 0:714293de3836 | 563 | |
ashleymills | 0:714293de3836 | 564 | /* get value */ |
ashleymills | 0:714293de3836 | 565 | output[i] = RNG_GetRandomNumber(); |
ashleymills | 0:714293de3836 | 566 | } |
ashleymills | 0:714293de3836 | 567 | |
ashleymills | 0:714293de3836 | 568 | return 0; |
ashleymills | 0:714293de3836 | 569 | } |
ashleymills | 0:714293de3836 | 570 | #elif defined(CYASSL_LPC43xx) || defined(CYASSL_STM32F2xx) |
ashleymills | 0:714293de3836 | 571 | |
ashleymills | 0:714293de3836 | 572 | #warning "write a real random seed!!!!, just for testing now" |
ashleymills | 0:714293de3836 | 573 | |
ashleymills | 0:714293de3836 | 574 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 575 | { |
ashleymills | 0:714293de3836 | 576 | int i; |
ashleymills | 0:714293de3836 | 577 | |
ashleymills | 0:714293de3836 | 578 | for (i = 0; i < sz; i++ ) |
ashleymills | 0:714293de3836 | 579 | output[i] = i; |
ashleymills | 0:714293de3836 | 580 | |
ashleymills | 0:714293de3836 | 581 | return 0; |
ashleymills | 0:714293de3836 | 582 | } |
ashleymills | 0:714293de3836 | 583 | |
ashleymills | 0:714293de3836 | 584 | #elif defined(NO_DEV_RANDOM) |
ashleymills | 0:714293de3836 | 585 | |
ashleymills | 0:714293de3836 | 586 | #error "you need to write an os specific GenerateSeed() here" |
ashleymills | 0:714293de3836 | 587 | |
ashleymills | 0:714293de3836 | 588 | /* |
ashleymills | 0:714293de3836 | 589 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 590 | { |
ashleymills | 0:714293de3836 | 591 | return 0; |
ashleymills | 0:714293de3836 | 592 | } |
ashleymills | 0:714293de3836 | 593 | */ |
ashleymills | 0:714293de3836 | 594 | |
ashleymills | 0:714293de3836 | 595 | |
ashleymills | 0:714293de3836 | 596 | #else /* !USE_WINDOWS_API && !HAVE_RPT_SYS && !MICRIUM && !NO_DEV_RANDOM */ |
ashleymills | 0:714293de3836 | 597 | |
ashleymills | 0:714293de3836 | 598 | |
ashleymills | 0:714293de3836 | 599 | /* may block */ |
ashleymills | 0:714293de3836 | 600 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
ashleymills | 0:714293de3836 | 601 | { |
ashleymills | 0:714293de3836 | 602 | int ret = 0; |
ashleymills | 0:714293de3836 | 603 | |
ashleymills | 0:714293de3836 | 604 | os->fd = open("/dev/urandom",O_RDONLY); |
ashleymills | 0:714293de3836 | 605 | if (os->fd == -1) { |
ashleymills | 0:714293de3836 | 606 | /* may still have /dev/random */ |
ashleymills | 0:714293de3836 | 607 | os->fd = open("/dev/random",O_RDONLY); |
ashleymills | 0:714293de3836 | 608 | if (os->fd == -1) |
ashleymills | 0:714293de3836 | 609 | return OPEN_RAN_E; |
ashleymills | 0:714293de3836 | 610 | } |
ashleymills | 0:714293de3836 | 611 | |
ashleymills | 0:714293de3836 | 612 | while (sz) { |
ashleymills | 0:714293de3836 | 613 | int len = (int)read(os->fd, output, sz); |
ashleymills | 0:714293de3836 | 614 | if (len == -1) { |
ashleymills | 0:714293de3836 | 615 | ret = READ_RAN_E; |
ashleymills | 0:714293de3836 | 616 | break; |
ashleymills | 0:714293de3836 | 617 | } |
ashleymills | 0:714293de3836 | 618 | |
ashleymills | 0:714293de3836 | 619 | sz -= len; |
ashleymills | 0:714293de3836 | 620 | output += len; |
ashleymills | 0:714293de3836 | 621 | |
ashleymills | 0:714293de3836 | 622 | if (sz) { |
ashleymills | 0:714293de3836 | 623 | #ifdef BLOCKING |
ashleymills | 0:714293de3836 | 624 | sleep(0); /* context switch */ |
ashleymills | 0:714293de3836 | 625 | #else |
ashleymills | 0:714293de3836 | 626 | ret = RAN_BLOCK_E; |
ashleymills | 0:714293de3836 | 627 | break; |
ashleymills | 0:714293de3836 | 628 | #endif |
ashleymills | 0:714293de3836 | 629 | } |
ashleymills | 0:714293de3836 | 630 | } |
ashleymills | 0:714293de3836 | 631 | close(os->fd); |
ashleymills | 0:714293de3836 | 632 | |
ashleymills | 0:714293de3836 | 633 | return ret; |
ashleymills | 0:714293de3836 | 634 | } |
ashleymills | 0:714293de3836 | 635 | |
ashleymills | 0:714293de3836 | 636 | #endif /* USE_WINDOWS_API */ |
ashleymills | 0:714293de3836 | 637 |