1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Get random bytes from hardware RNG

This commit is contained in:
Ivan Grokhotkov 2015-12-02 23:49:49 +03:00
parent 6830d98c7f
commit 34ff4421d2

View File

@ -44,6 +44,7 @@
#ifdef ESP8266 #ifdef ESP8266
#define CONFIG_SSL_SKELETON_MODE 1 #define CONFIG_SSL_SKELETON_MODE 1
uint32_t phy_get_rand();
#endif #endif
#if defined(CONFIG_USE_DEV_URANDOM) #if defined(CONFIG_USE_DEV_URANDOM)
@ -124,6 +125,7 @@ EXP_FUNC void STDCALL RNG_initialize()
exit(1); exit(1);
} }
} }
#elif defined(ESP8266)
#else #else
/* start of with a stack to copy across */ /* start of with a stack to copy across */
int i; int i;
@ -168,6 +170,13 @@ EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB) #elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
/* use Microsoft Crypto Libraries */ /* use Microsoft Crypto Libraries */
CryptGenRandom(gCryptProv, num_rand_bytes, rand_data); CryptGenRandom(gCryptProv, num_rand_bytes, rand_data);
#elif defined(ESP8266)
for (size_t cb = 0; cb < num_rand_bytes; cb += 4) {
uint32_t r = phy_get_rand();
size_t left = num_rand_bytes - cb;
left = (left < 4) ? left : 4;
memcpy(rand_data + cb, &r, left);
}
#else /* nothing else to use, so use a custom RNG */ #else /* nothing else to use, so use a custom RNG */
/* The method we use when we've got nothing better. Use RC4, time /* The method we use when we've got nothing better. Use RC4, time
and a couple of random seeds to generate a random sequence */ and a couple of random seeds to generate a random sequence */
@ -214,8 +223,9 @@ void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
for (i = 0; i < num_rand_bytes; i++) for (i = 0; i < num_rand_bytes; i++)
{ {
while (rand_data[i] == 0) /* can't be 0 */ while (rand_data[i] == 0) {
rand_data[i] = (uint8_t)(rand()); get_random(1, rand_data + i);
}
} }
} }
@ -368,4 +378,3 @@ error:
} }
#endif #endif