Skip to content

Commit ce200ed

Browse files
authored
- Move TypeConversion from namespace esp8266 to namespace experimental. (#7252)
- Add using namespace experimental::crypto; to HelloCrypto.ino. - Add mention about new random function in libraries.rst. - Update keywords. Co-authored-by: Anders <andlo151@student.liu.se>
1 parent 3c9a75f commit ce200ed

File tree

7 files changed

+19
-17
lines changed

7 files changed

+19
-17
lines changed

cores/esp8266/Crypto.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include <assert.h>
3131

32-
namespace TypeCast = esp8266::TypeConversion;
32+
namespace TypeCast = experimental::TypeConversion;
3333

3434
namespace
3535
{

cores/esp8266/Esp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ uint8_t *EspClass::random(uint8_t *resultArray, const size_t outputSizeBytes) co
542542
* It should be noted that the ESP8266 has no Bluetooth functionality, so turning the WiFi off is likely to cause RANDOM_REG32 to use pseudo-random numbers.
543543
*
544544
* It is possible that yield() must be called on the ESP8266 to properly feed the hardware random number generator new bits, since there is only one processor core available.
545-
* However, no feeding requirements are mentioned in the ESP32 documentation, and using yield() could possibly cause extended delays during nonce generation.
545+
* However, no feeding requirements are mentioned in the ESP32 documentation, and using yield() could possibly cause extended delays during number generation.
546546
* Thus only delayMicroseconds() is used below.
547547
*/
548548

cores/esp8266/TypeConversion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <assert.h>
2727
#include "TypeConversion.h"
2828

29-
namespace esp8266
29+
namespace experimental
3030
{
3131
namespace TypeConversion
3232
{

cores/esp8266/TypeConversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include <Arduino.h>
3030

31-
namespace esp8266
31+
namespace experimental
3232
{
3333
namespace TypeConversion
3434
{

doc/libraries.rst

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Some ESP-specific APIs related to deep sleep, RTC and flash memories are availab
113113

114114
``ESP.getCycleCount()`` returns the cpu instruction cycle count since start as an unsigned 32-bit. This is useful for accurate timing of very short actions like bit banging.
115115

116+
``ESP.random()`` should be used to generate true random numbers on the ESP. Returns an unsigned 32-bit integer with the random number. An alternate version is also available that fills an array of arbitrary length. Note that it seems as though the WiFi needs to be enabled to generate entropy for the random numbers, otherwise pseudo-random numbers are used.
117+
116118
``ESP.checkFlashCRC()`` calculates the CRC of the program memory (not including any filesystems) and compares it to the one embedded in the image. If this call returns ``false`` then the flash has been corrupted. At that point, you may want to consider trying to send a MQTT message, to start a re-download of the application, blink a LED in an `SOS` pattern, etc. However, since the flash is known corrupted at this point there is no guarantee the app will be able to perform any of these operations, so in safety critical deployments an immediate shutdown to a fail-safe mode may be indicated.
117119

118120
``ESP.getVcc()`` may be used to measure supply voltage. ESP needs to reconfigure the ADC at startup in order for this feature to be available. Add the following line to the top of your sketch to use ``getVcc``:

libraries/esp8266/examples/HelloCrypto/HelloCrypto.ino

+13-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include <TypeConversion.h>
99
#include <Crypto.h>
1010

11-
namespace TypeCast = esp8266::TypeConversion;
12-
using namespace experimental;
11+
namespace TypeCast = experimental::TypeConversion;
1312

1413
/**
1514
NOTE: Although we could define the strings below as normal String variables,
@@ -38,34 +37,36 @@ void setup() {
3837
void loop() {
3938
// This serves only to demonstrate the library use. See the header file for a full list of functions.
4039

40+
using namespace experimental::crypto;
41+
4142
String exampleData = F("Hello Crypto World!");
4243
Serial.println(String(F("This is our example data: ")) + exampleData);
4344

44-
uint8_t resultArray[crypto::SHA256::NATURAL_LENGTH] { 0 };
45-
uint8_t derivedKey[crypto::ENCRYPTION_KEY_LENGTH] { 0 };
45+
uint8_t resultArray[SHA256::NATURAL_LENGTH] { 0 };
46+
uint8_t derivedKey[ENCRYPTION_KEY_LENGTH] { 0 };
4647

4748
static uint32_t encryptionCounter = 0;
4849

4950

5051
// Generate the salt to use for HKDF
5152
uint8_t hkdfSalt[16] { 0 };
52-
crypto::getNonceGenerator()(hkdfSalt, sizeof hkdfSalt);
53+
getNonceGenerator()(hkdfSalt, sizeof hkdfSalt);
5354

5455
// Generate the key to use for HMAC and encryption
55-
crypto::HKDF hkdfInstance(FPSTR(masterKey), (sizeof masterKey) - 1, hkdfSalt, sizeof hkdfSalt); // (sizeof masterKey) - 1 removes the terminating null value of the c-string
56+
HKDF hkdfInstance(FPSTR(masterKey), (sizeof masterKey) - 1, hkdfSalt, sizeof hkdfSalt); // (sizeof masterKey) - 1 removes the terminating null value of the c-string
5657
hkdfInstance.produce(derivedKey, sizeof derivedKey);
5758

5859
// Hash
59-
crypto::SHA256::hash(exampleData.c_str(), exampleData.length(), resultArray);
60+
SHA256::hash(exampleData.c_str(), exampleData.length(), resultArray);
6061
Serial.println(String(F("\nThis is the SHA256 hash of our example data, in HEX format:\n")) + TypeCast::uint8ArrayToHexString(resultArray, sizeof resultArray));
61-
Serial.println(String(F("This is the SHA256 hash of our example data, in HEX format, using String output:\n")) + crypto::SHA256::hash(exampleData));
62+
Serial.println(String(F("This is the SHA256 hash of our example data, in HEX format, using String output:\n")) + SHA256::hash(exampleData));
6263

6364

6465
// HMAC
6566
// Note that HMAC output length is limited
66-
crypto::SHA256::hmac(exampleData.c_str(), exampleData.length(), derivedKey, sizeof derivedKey, resultArray, sizeof resultArray);
67+
SHA256::hmac(exampleData.c_str(), exampleData.length(), derivedKey, sizeof derivedKey, resultArray, sizeof resultArray);
6768
Serial.println(String(F("\nThis is the SHA256 HMAC of our example data, in HEX format:\n")) + TypeCast::uint8ArrayToHexString(resultArray, sizeof resultArray));
68-
Serial.println(String(F("This is the SHA256 HMAC of our example data, in HEX format, using String output:\n")) + crypto::SHA256::hmac(exampleData, derivedKey, sizeof derivedKey, crypto::SHA256::NATURAL_LENGTH));
69+
Serial.println(String(F("This is the SHA256 HMAC of our example data, in HEX format, using String output:\n")) + SHA256::hmac(exampleData, derivedKey, sizeof derivedKey, SHA256::NATURAL_LENGTH));
6970

7071

7172
// Authenticated Encryption with Associated Data (AEAD)
@@ -76,10 +77,10 @@ void loop() {
7677
Serial.println(String(F("\nThis is the data to encrypt: ")) + dataToEncrypt);
7778

7879
// Note that the key must be ENCRYPTION_KEY_LENGTH long.
79-
crypto::ChaCha20Poly1305::encrypt(dataToEncrypt.begin(), dataToEncrypt.length(), derivedKey, &encryptionCounter, sizeof encryptionCounter, resultingNonce, resultingTag);
80+
ChaCha20Poly1305::encrypt(dataToEncrypt.begin(), dataToEncrypt.length(), derivedKey, &encryptionCounter, sizeof encryptionCounter, resultingNonce, resultingTag);
8081
Serial.println(String(F("Encrypted data: ")) + dataToEncrypt);
8182

82-
bool decryptionSucceeded = crypto::ChaCha20Poly1305::decrypt(dataToEncrypt.begin(), dataToEncrypt.length(), derivedKey, &encryptionCounter, sizeof encryptionCounter, resultingNonce, resultingTag);
83+
bool decryptionSucceeded = ChaCha20Poly1305::decrypt(dataToEncrypt.begin(), dataToEncrypt.length(), derivedKey, &encryptionCounter, sizeof encryptionCounter, resultingNonce, resultingTag);
8384
encryptionCounter++;
8485

8586
if (decryptionSucceeded) {

libraries/esp8266/keywords.txt

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
ESP KEYWORD1
1414

15-
crypto KEYWORD1
1615
nonceGeneratorType KEYWORD1
1716
MD5 KEYWORD1
1817
SHA1 KEYWORD1

0 commit comments

Comments
 (0)