Skip to content

Commit 34ff442

Browse files
committed
Get random bytes from hardware RNG
1 parent 6830d98 commit 34ff442

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

crypto/crypto_misc.c

+36-27
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* Copyright (c) 2007, Cameron Rich
3-
*
3+
*
44
* All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
5+
*
6+
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions are met:
88
*
9-
* * Redistributions of source code must retain the above copyright notice,
9+
* * Redistributions of source code must retain the above copyright notice,
1010
* this list of conditions and the following disclaimer.
11-
* * Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
1313
* and/or other materials provided with the distribution.
14-
* * Neither the name of the axTLS project nor the names of its contributors
15-
* may be used to endorse or promote products derived from this software
14+
* * Neither the name of the axTLS project nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
1616
* without specific prior written permission.
1717
*
1818
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@@ -44,6 +44,7 @@
4444

4545
#ifdef ESP8266
4646
#define CONFIG_SSL_SKELETON_MODE 1
47+
uint32_t phy_get_rand();
4748
#endif
4849

4950
#if defined(CONFIG_USE_DEV_URANDOM)
@@ -63,20 +64,20 @@ static uint8_t entropy_pool[ENTROPY_POOL_SIZE];
6364
const char * const unsupported_str = "Error: Feature not supported\n";
6465

6566
#ifndef CONFIG_SSL_SKELETON_MODE
66-
/**
67+
/**
6768
* Retrieve a file and put it into memory
6869
* @return The size of the file, or -1 on failure.
6970
*/
7071
int get_file(const char *filename, uint8_t **buf)
7172
{
7273
int total_bytes = 0;
73-
int bytes_read = 0;
74+
int bytes_read = 0;
7475
int filesize;
7576
FILE *stream = fopen(filename, "rb");
7677

7778
if (stream == NULL)
7879
{
79-
#ifdef CONFIG_SSL_FULL_MODE
80+
#ifdef CONFIG_SSL_FULL_MODE
8081
printf("file '%s' does not exist\n", filename); TTY_FLUSH();
8182
#endif
8283
return -1;
@@ -93,7 +94,7 @@ int get_file(const char *filename, uint8_t **buf)
9394
bytes_read = fread(*buf+total_bytes, 1, filesize-total_bytes, stream);
9495
total_bytes += bytes_read;
9596
} while (total_bytes < filesize && bytes_read > 0);
96-
97+
9798
fclose(stream);
9899
return filesize;
99100
}
@@ -110,25 +111,26 @@ EXP_FUNC void STDCALL RNG_initialize()
110111
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
111112
rng_fd = ax_open("/dev/urandom", O_RDONLY);
112113
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
113-
if (!CryptAcquireContext(&gCryptProv,
114+
if (!CryptAcquireContext(&gCryptProv,
114115
NULL, NULL, PROV_RSA_FULL, 0))
115116
{
116117
if (GetLastError() == NTE_BAD_KEYSET &&
117-
!CryptAcquireContext(&gCryptProv,
118-
NULL,
119-
NULL,
120-
PROV_RSA_FULL,
118+
!CryptAcquireContext(&gCryptProv,
119+
NULL,
120+
NULL,
121+
PROV_RSA_FULL,
121122
CRYPT_NEWKEYSET))
122123
{
123124
printf("CryptoLib: %x\n", unsupported_str, GetLastError());
124125
exit(1);
125126
}
126127
}
128+
#elif defined(ESP8266)
127129
#else
128130
/* start of with a stack to copy across */
129131
int i;
130132
memcpy(entropy_pool, &i, ENTROPY_POOL_SIZE);
131-
srand((unsigned int)&i);
133+
srand((unsigned int)&i);
132134
#endif
133135
}
134136

@@ -161,15 +163,22 @@ EXP_FUNC void STDCALL RNG_terminate(void)
161163
* Set a series of bytes with a random number. Individual bytes can be 0
162164
*/
163165
EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
164-
{
166+
{
165167
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
166168
/* use the Linux default */
167169
read(rng_fd, rand_data, num_rand_bytes); /* read from /dev/urandom */
168170
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
169171
/* use Microsoft Crypto Libraries */
170172
CryptGenRandom(gCryptProv, num_rand_bytes, rand_data);
173+
#elif defined(ESP8266)
174+
for (size_t cb = 0; cb < num_rand_bytes; cb += 4) {
175+
uint32_t r = phy_get_rand();
176+
size_t left = num_rand_bytes - cb;
177+
left = (left < 4) ? left : 4;
178+
memcpy(rand_data + cb, &r, left);
179+
}
171180
#else /* nothing else to use, so use a custom RNG */
172-
/* The method we use when we've got nothing better. Use RC4, time
181+
/* The method we use when we've got nothing better. Use RC4, time
173182
and a couple of random seeds to generate a random sequence */
174183
RC4_CTX rng_ctx;
175184
struct timeval tv;
@@ -179,10 +188,10 @@ EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
179188
int i;
180189

181190
/* A proper implementation would use counters etc for entropy */
182-
gettimeofday(&tv, NULL);
191+
gettimeofday(&tv, NULL);
183192
ep = (uint64_t *)entropy_pool;
184193
ep[0] ^= ENTROPY_COUNTER1;
185-
ep[1] ^= ENTROPY_COUNTER2;
194+
ep[1] ^= ENTROPY_COUNTER2;
186195

187196
/* use a digested version of the entropy pool as a key */
188197
MD5_Init(&rng_digest_ctx);
@@ -214,8 +223,9 @@ void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
214223

215224
for (i = 0; i < num_rand_bytes; i++)
216225
{
217-
while (rand_data[i] == 0) /* can't be 0 */
218-
rand_data[i] = (uint8_t)(rand());
226+
while (rand_data[i] == 0) {
227+
get_random(1, rand_data + i);
228+
}
219229
}
220230
}
221231

@@ -267,7 +277,7 @@ static void print_hex(uint8_t hex)
267277
* @param data [in] The start of data to use
268278
* @param ... [in] Any additional arguments
269279
*/
270-
EXP_FUNC void STDCALL print_blob(const char *format,
280+
EXP_FUNC void STDCALL print_blob(const char *format,
271281
const uint8_t *data, int size, ...)
272282
{
273283
int i;
@@ -348,7 +358,7 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
348358
}
349359

350360
/* check that we don't go past the output buffer */
351-
if (z > *outlen)
361+
if (z > *outlen)
352362
goto error;
353363
}
354364

@@ -368,4 +378,3 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
368378

369379
}
370380
#endif
371-

0 commit comments

Comments
 (0)