1
1
/*
2
2
* Copyright (c) 2007, Cameron Rich
3
- *
3
+ *
4
4
* 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
7
7
* modification, are permitted provided that the following conditions are met:
8
8
*
9
- * * Redistributions of source code must retain the above copyright notice,
9
+ * * Redistributions of source code must retain the above copyright notice,
10
10
* 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
13
13
* 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
16
16
* without specific prior written permission.
17
17
*
18
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44
44
45
45
#ifdef ESP8266
46
46
#define CONFIG_SSL_SKELETON_MODE 1
47
+ uint32_t phy_get_rand ();
47
48
#endif
48
49
49
50
#if defined(CONFIG_USE_DEV_URANDOM )
@@ -63,20 +64,20 @@ static uint8_t entropy_pool[ENTROPY_POOL_SIZE];
63
64
const char * const unsupported_str = "Error: Feature not supported\n" ;
64
65
65
66
#ifndef CONFIG_SSL_SKELETON_MODE
66
- /**
67
+ /**
67
68
* Retrieve a file and put it into memory
68
69
* @return The size of the file, or -1 on failure.
69
70
*/
70
71
int get_file (const char * filename , uint8_t * * buf )
71
72
{
72
73
int total_bytes = 0 ;
73
- int bytes_read = 0 ;
74
+ int bytes_read = 0 ;
74
75
int filesize ;
75
76
FILE * stream = fopen (filename , "rb" );
76
77
77
78
if (stream == NULL )
78
79
{
79
- #ifdef CONFIG_SSL_FULL_MODE
80
+ #ifdef CONFIG_SSL_FULL_MODE
80
81
printf ("file '%s' does not exist\n" , filename ); TTY_FLUSH ();
81
82
#endif
82
83
return -1 ;
@@ -93,7 +94,7 @@ int get_file(const char *filename, uint8_t **buf)
93
94
bytes_read = fread (* buf + total_bytes , 1 , filesize - total_bytes , stream );
94
95
total_bytes += bytes_read ;
95
96
} while (total_bytes < filesize && bytes_read > 0 );
96
-
97
+
97
98
fclose (stream );
98
99
return filesize ;
99
100
}
@@ -110,25 +111,26 @@ EXP_FUNC void STDCALL RNG_initialize()
110
111
#if !defined(WIN32 ) && defined(CONFIG_USE_DEV_URANDOM )
111
112
rng_fd = ax_open ("/dev/urandom" , O_RDONLY );
112
113
#elif defined(WIN32 ) && defined(CONFIG_WIN32_USE_CRYPTO_LIB )
113
- if (!CryptAcquireContext (& gCryptProv ,
114
+ if (!CryptAcquireContext (& gCryptProv ,
114
115
NULL , NULL , PROV_RSA_FULL , 0 ))
115
116
{
116
117
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 ,
121
122
CRYPT_NEWKEYSET ))
122
123
{
123
124
printf ("CryptoLib: %x\n" , unsupported_str , GetLastError ());
124
125
exit (1 );
125
126
}
126
127
}
128
+ #elif defined(ESP8266 )
127
129
#else
128
130
/* start of with a stack to copy across */
129
131
int i ;
130
132
memcpy (entropy_pool , & i , ENTROPY_POOL_SIZE );
131
- srand ((unsigned int )& i );
133
+ srand ((unsigned int )& i );
132
134
#endif
133
135
}
134
136
@@ -161,15 +163,22 @@ EXP_FUNC void STDCALL RNG_terminate(void)
161
163
* Set a series of bytes with a random number. Individual bytes can be 0
162
164
*/
163
165
EXP_FUNC void STDCALL get_random (int num_rand_bytes , uint8_t * rand_data )
164
- {
166
+ {
165
167
#if !defined(WIN32 ) && defined(CONFIG_USE_DEV_URANDOM )
166
168
/* use the Linux default */
167
169
read (rng_fd , rand_data , num_rand_bytes ); /* read from /dev/urandom */
168
170
#elif defined(WIN32 ) && defined(CONFIG_WIN32_USE_CRYPTO_LIB )
169
171
/* use Microsoft Crypto Libraries */
170
172
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
+ }
171
180
#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
173
182
and a couple of random seeds to generate a random sequence */
174
183
RC4_CTX rng_ctx ;
175
184
struct timeval tv ;
@@ -179,10 +188,10 @@ EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
179
188
int i ;
180
189
181
190
/* A proper implementation would use counters etc for entropy */
182
- gettimeofday (& tv , NULL );
191
+ gettimeofday (& tv , NULL );
183
192
ep = (uint64_t * )entropy_pool ;
184
193
ep [0 ] ^= ENTROPY_COUNTER1 ;
185
- ep [1 ] ^= ENTROPY_COUNTER2 ;
194
+ ep [1 ] ^= ENTROPY_COUNTER2 ;
186
195
187
196
/* use a digested version of the entropy pool as a key */
188
197
MD5_Init (& rng_digest_ctx );
@@ -214,8 +223,9 @@ void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
214
223
215
224
for (i = 0 ; i < num_rand_bytes ; i ++ )
216
225
{
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
+ }
219
229
}
220
230
}
221
231
@@ -267,7 +277,7 @@ static void print_hex(uint8_t hex)
267
277
* @param data [in] The start of data to use
268
278
* @param ... [in] Any additional arguments
269
279
*/
270
- EXP_FUNC void STDCALL print_blob (const char * format ,
280
+ EXP_FUNC void STDCALL print_blob (const char * format ,
271
281
const uint8_t * data , int size , ...)
272
282
{
273
283
int i ;
@@ -348,7 +358,7 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
348
358
}
349
359
350
360
/* check that we don't go past the output buffer */
351
- if (z > * outlen )
361
+ if (z > * outlen )
352
362
goto error ;
353
363
}
354
364
@@ -368,4 +378,3 @@ EXP_FUNC int STDCALL base64_decode(const char *in, int len,
368
378
369
379
}
370
380
#endif
371
-
0 commit comments