1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Port the crypto library to ESP32

This commit is contained in:
Rhys Weatherley 2018-04-02 15:02:42 +10:00
parent b1ac67efb6
commit e2420adfc5
10 changed files with 76 additions and 21 deletions

View File

@ -41,11 +41,28 @@
// ESP8266 does not have EEPROM but it does have SPI flash memory. // ESP8266 does not have EEPROM but it does have SPI flash memory.
// It also has a TRNG register for generating "true" random numbers. // It also has a TRNG register for generating "true" random numbers.
// For now we use the TRNG but don't save the seed in flash memory. // For now we use the TRNG but don't save the seed in flash memory.
#define RNG_ESP8266 1 #define RNG_WORD_TRNG 1
#define RNG_ESP8266_GET_TRNG() (ESP8266_DREG(0x20E44)) #define RNG_WORD_TRNG_GET() (ESP8266_DREG(0x20E44))
#elif defined(ESP32)
// ESP32 has a word-based TRNG and an API for Non-Volatile Storage (NVS).
#define RNG_WORD_TRNG 1
#define RNG_WORD_TRNG_GET() (esp_random())
#define RNG_ESP_NVS 1
#include <nvs.h>
#endif #endif
#include <string.h> #include <string.h>
// Throw an error if there is no built-in hardware random number source.
// If this happens, then you need to do one of two things:
// 1. Edit RNG.cpp to add your platform's hardware TRNG.
// 2. Provide a proper noise source like TransistorNoiseSource
// in your sketch and then comment out the #error line below.
#if !defined(RNG_DUE_TRNG) && \
!defined(RNG_WATCHDOG) && \
!defined(RNG_WORD_TRNG)
#error "no hardware random number source detected for this platform"
#endif
/** /**
* \class RNGClass RNG.h <RNG.h> * \class RNGClass RNG.h <RNG.h>
* \brief Pseudo random number generator suitable for cryptography. * \brief Pseudo random number generator suitable for cryptography.
@ -400,8 +417,25 @@ void RNGClass::begin(const char *tag)
REG_TRNG_CR = TRNG_CR_KEY(0x524E47) | TRNG_CR_ENABLE; REG_TRNG_CR = TRNG_CR_KEY(0x524E47) | TRNG_CR_ENABLE;
REG_TRNG_IDR = TRNG_IDR_DATRDY; // Disable interrupts - we will poll. REG_TRNG_IDR = TRNG_IDR_DATRDY; // Disable interrupts - we will poll.
mixTRNG(); mixTRNG();
#elif defined(RNG_ESP8266) #endif
// Mix in some output from the ESP8266's TRNG to initialize the state. #if defined(RNG_ESP_NVS)
// Do we have a seed saved in ESP non-volatile storage (NVS)?
nvs_handle handle = 0;
if (nvs_open("rng", NVS_READONLY, &handle) == 0) {
size_t len = 0;
if (nvs_get_blob(handle, "seed", NULL, &len) == 0 && len == SEED_SIZE) {
uint32_t seed[12];
if (nvs_get_blob(handle, "seed", seed, &len) == 0) {
for (int posn = 0; posn < 12; ++posn)
block[posn + 4] ^= seed[posn];
}
clean(seed);
}
nvs_close(handle);
}
#endif
#if defined(RNG_WORD_TRNG)
// Mix in some output from a word-based TRNG to initialize the state.
mixTRNG(); mixTRNG();
#endif #endif
@ -422,13 +456,17 @@ void RNGClass::begin(const char *tag)
// Stir in the unique identifier for the CPU so that different // Stir in the unique identifier for the CPU so that different
// devices will give different outputs even without seeding. // devices will give different outputs even without seeding.
stirUniqueIdentifier(); stirUniqueIdentifier();
#elif defined(RNG_ESP8266) #elif defined(ESP8266)
// ESP8266's have a 32-bit CPU chip ID and 32-bit flash chip ID // ESP8266's have a 32-bit CPU chip ID and 32-bit flash chip ID
// that we can use as a device unique identifier. // that we can use as a device unique identifier.
uint32_t ids[2]; uint32_t ids[2];
ids[0] = ESP.getChipId(); ids[0] = ESP.getChipId();
ids[1] = ESP.getFlashChipId(); ids[1] = ESP.getFlashChipId();
stir((const uint8_t *)ids, sizeof(ids)); stir((const uint8_t *)ids, sizeof(ids));
#elif defined(ESP32)
// ESP32's have a MAC address that can be used as a device identifier.
uint64_t mac = ESP.getEfuseMac();
stir((const uint8_t *)&mac, sizeof(mac));
#else #else
// AVR devices don't have anything like a serial number so it is // AVR devices don't have anything like a serial number so it is
// difficult to make every device unique. Use the compilation // difficult to make every device unique. Use the compilation
@ -743,6 +781,15 @@ void RNGClass::save()
for (posn = 13; posn < (RNG_FLASH_PAGE_SIZE / 4); ++posn) for (posn = 13; posn < (RNG_FLASH_PAGE_SIZE / 4); ++posn)
((uint32_t *)(RNG_SEED_ADDR))[posn + 13] = 0xFFFFFFFF; ((uint32_t *)(RNG_SEED_ADDR))[posn + 13] = 0xFFFFFFFF;
eraseAndWriteSeed(); eraseAndWriteSeed();
#elif defined(RNG_ESP_NVS)
// Save the seed into ESP non-volatile storage (NVS).
nvs_handle handle = 0;
if (nvs_open("rng", NVS_READWRITE, &handle) == 0) {
nvs_erase_all(handle);
nvs_set_blob(handle, "seed", stream, SEED_SIZE);
nvs_commit(handle);
nvs_close(handle);
}
#endif #endif
rekey(); rekey();
timer = millis(); timer = millis();
@ -792,9 +839,9 @@ void RNGClass::loop()
} }
trngPending = 1; trngPending = 1;
} }
#elif defined(RNG_ESP8266) #elif defined(RNG_WORD_TRNG)
// Read a word from the ESP8266's TRNG and XOR it into the state. // Read a word from the TRNG and XOR it into the state.
block[4 + trngPosn] ^= RNG_ESP8266_GET_TRNG(); block[4 + trngPosn] ^= RNG_WORD_TRNG_GET();
if (++trngPosn >= 12) if (++trngPosn >= 12)
trngPosn = 0; trngPosn = 0;
if (credits < RNG_MAX_CREDITS) { if (credits < RNG_MAX_CREDITS) {
@ -876,7 +923,15 @@ void RNGClass::destroy()
for (unsigned posn = 0; posn < (RNG_FLASH_PAGE_SIZE / 4); ++posn) for (unsigned posn = 0; posn < (RNG_FLASH_PAGE_SIZE / 4); ++posn)
((uint32_t *)(RNG_SEED_ADDR))[posn] = 0xFFFFFFFF; ((uint32_t *)(RNG_SEED_ADDR))[posn] = 0xFFFFFFFF;
eraseAndWriteSeed(); eraseAndWriteSeed();
#elif defined(RNG_ESP_NVS)
nvs_handle handle = 0;
if (nvs_open("rng", NVS_READWRITE, &handle) == 0) {
nvs_erase_all(handle);
nvs_commit(handle);
nvs_close(handle);
}
#endif #endif
initialized = 0;
} }
/** /**
@ -921,10 +976,10 @@ void RNGClass::mixTRNG()
break; break;
block[posn + 4] ^= REG_TRNG_ODATA; block[posn + 4] ^= REG_TRNG_ODATA;
} }
#elif defined(RNG_ESP8266) #elif defined(RNG_WORD_TRNG)
// Read 12 words from the ESP8266's TRNG and XOR them into the state. // Read 12 words from the TRNG and XOR them into the state.
for (uint8_t index = 4; index < 16; ++index) for (uint8_t index = 4; index < 16; ++index)
block[index] ^= RNG_ESP8266_GET_TRNG(); block[index] ^= RNG_WORD_TRNG_GET();
#elif defined(RNG_WATCHDOG) #elif defined(RNG_WATCHDOG)
// Read the pending 32 bit buffer from the WDT interrupt and mix it in. // Read the pending 32 bit buffer from the WDT interrupt and mix it in.
cli(); cli();

View File

@ -27,7 +27,7 @@ This example runs tests on the BLAKE2b implementation to verify correct behaviou
#include <Crypto.h> #include <Crypto.h>
#include <BLAKE2b.h> #include <BLAKE2b.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -27,7 +27,7 @@ This example runs tests on the BLAKE2s implementation to verify correct behaviou
#include <Crypto.h> #include <Crypto.h>
#include <BLAKE2s.h> #include <BLAKE2s.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -27,7 +27,7 @@ This example runs tests on the ChaCha implementation to verify correct behaviour
#include <Crypto.h> #include <Crypto.h>
#include <ChaCha.h> #include <ChaCha.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -28,7 +28,7 @@ correct behaviour.
#include <Crypto.h> #include <Crypto.h>
#include <ChaChaPoly.h> #include <ChaChaPoly.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -30,7 +30,7 @@ This example runs tests on the EAX implementation to verify correct behaviour.
#include <Speck.h> #include <Speck.h>
#include <SpeckTiny.h> #include <SpeckTiny.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
@ -540,7 +540,7 @@ void setup()
Serial.println(); Serial.println();
delete eax; delete eax;
eax256 = new EAX<AES256>(); eax256 = new EAX<AES256>();
perfCipher(eax, &testVectorEAX1, "AES-256"); perfCipher(eax256, &testVectorEAX1, "AES-256");
Serial.println(); Serial.println();
delete eax256; delete eax256;
eaxSpeck = new EAX<Speck>(); eaxSpeck = new EAX<Speck>();

View File

@ -30,7 +30,7 @@ This example runs tests on the GCM implementation to verify correct behaviour.
#include <SpeckTiny.h> #include <SpeckTiny.h>
#include <GCM.h> #include <GCM.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -34,7 +34,7 @@ AVR platforms with 32K or less of flash memory.
#include <RNG.h> #include <RNG.h>
#include <RNG.h> #include <RNG.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -31,7 +31,7 @@ This example runs tests on the XTS implementation to verify correct behaviour.
#include <SpeckTiny.h> #include <SpeckTiny.h>
#include <XTS.h> #include <XTS.h>
#include <string.h> #include <string.h>
#if defined(ESP8266) #if defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#else #else
#include <avr/pgmspace.h> #include <avr/pgmspace.h>

View File

@ -31,7 +31,7 @@
((uint64_t)pgm_read_dword(_temp)) | \ ((uint64_t)pgm_read_dword(_temp)) | \
(((uint64_t)pgm_read_dword(_temp + 1)) << 32); \ (((uint64_t)pgm_read_dword(_temp + 1)) << 32); \
})) }))
#elif defined(ESP8266) #elif defined(ESP8266) || defined(ESP32)
#include <pgmspace.h> #include <pgmspace.h>
#define pgm_read_qword(x) \ #define pgm_read_qword(x) \
(__extension__ ({ \ (__extension__ ({ \