Skip to content

Commit b2c6f7c

Browse files
Better SpritzBestPracticePasswordESP8266 example
1 parent 51bf614 commit b2c6f7c

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

examples/SpritzBestPracticePasswordESP8266/SpritzBestPracticePasswordESP8266.ino

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,79 +22,65 @@
2222
#include <SpritzCipher.h>
2323
#include <ESP8266WiFi.h>
2424

25-
/* The RNG seed */
26-
volatile uint32_t hw_rng;
27-
uint8_t entropy_buf[256]; /* You can also use malloc() & free(). */
25+
2826

2927
/* Table of alphabetic (uppercase and lowercase) and numeric characters */
30-
const uint8_t alphanumeric_table[62] =
28+
const char alphanumeric_table[62] =
3129
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
3230
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
3331
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
3432
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
3533
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
3634
};
3735

36+
#define PASSWORD_LEN 28 /* ~160-bit of entropy password; ceil(160/log2(26*2+10)) */
37+
/* One spritz_ctx (One mem buffer) to save some memory.
38+
* For both, hash function and the random numbers generator.
39+
*/
40+
spritz_ctx the_ctx;
3841

3942
void setup() {
43+
volatile uint32_t hw_rng; /* The RNG seed */
44+
uint8_t buf[32];
45+
4046
/* Initialize serial and wait for port to open */
41-
Serial.begin(9600);
47+
Serial.begin(115200);
4248
while (!Serial) {
4349
; /* Wait for serial port to connect. Needed for Leonardo only */
4450
}
45-
}
46-
4751

48-
void loop() {
49-
uint8_t password_len = 28; /* ~160-bit of entropy password; ceil(160/log2(26*2+10)) */
50-
uint8_t buf[32];
51-
spritz_ctx the_ctx;
52-
/* One spritz_ctx (One mem buffer) to save some memory.
53-
* For both, hash function and the random numbers generator.
52+
/* We did NOT use spritz_hash() cause it will have its own spritz_ctx,
53+
* And we like to save memory
5454
*/
55-
56-
for (size_t i = 0; i < (sizeof(entropy_buf)/4); i += 4) {
55+
/* To make a 256-bit hash of the entropy */
56+
spritz_hash_setup(&the_ctx); /* Initialize */
57+
58+
/* Insert 64*4=256 bytes of entropy in hash function */
59+
for (uint16_t i = 0; i < 64; i++) {
5760
delay(1);
5861
hw_rng = *(volatile uint32_t *)0x3FF20E44;
59-
entropy_buf[i] = ((uint8_t *) &hw_rng)[0];
60-
entropy_buf[i+2] = ((uint8_t *) &hw_rng)[1];
61-
entropy_buf[i+3] = ((uint8_t *) &hw_rng)[2];
62-
entropy_buf[i+4] = ((uint8_t *) &hw_rng)[3];
62+
spritz_hash_update(&the_ctx, (const uint8_t *)&hw_rng, (uint16_t)(sizeof(hw_rng))); /* Add 4 bytes of entropy */
6363
}
64-
6564

66-
/* We did NOT use spritz_hash() cause it will have its own spritz_ctx,
67-
* And we like to save memory
68-
*/
69-
/* Make a 256-bit hash of the entropy in "buf" using one function */
70-
spritz_hash_setup(&the_ctx); /* Initialize */
71-
spritz_hash_update(&the_ctx, entropy_buf, (uint16_t)(sizeof(entropy_buf))); /* Add data */
7265
spritz_hash_final(&the_ctx, buf, (uint8_t)(sizeof(buf))); /* Output the final hash */
73-
74-
spritz_state_memzero(&the_ctx); /* wipe "the_ctx" data by replacing it with zeros (0x00) */
75-
spritz_memzero(entropy_buf, (uint16_t)(sizeof(entropy_buf))); /* wipe "entropy" data by replacing it with zeros (0x00) */
76-
spritz_memzero((uint8_t *)hw_rng, (uint16_t)(sizeof(hw_rng)));
66+
spritz_memzero((uint8_t *)&hw_rng, (uint16_t)(sizeof(hw_rng)));
7767

7868
/* Initialize/Seed the RNG with the hash of entropy */
79-
spritz_setup(&the_ctx, buf, (uint8_t)(sizeof(buf)));
80-
69+
spritz_setup(&the_ctx, buf, (uint8_t)(sizeof(buf))); /* wipe "the_ctx" data by replacing it with zeros (0x00) */
8170
spritz_memzero(buf, (uint16_t)(sizeof(buf))); /* wipe "buf" data by replacing it with zeros (0x00) */
8271

72+
Serial.println(F("\n\n[Make strong Alphanumeric passwords]\n\n"));
73+
}
8374

84-
Serial.println("\n[Make strong Alphanumeric passwords]\n");
85-
86-
while (1)
87-
{
88-
/* Fill the buffer with random uniformly distributed alphanumeric characters */
89-
for (uint8_t i = 0; i < password_len; i++) {
90-
/* Like: buf[i] = charactersTable[random() % number_Of_elements_In_charactersTable] */
91-
buf[i] = alphanumeric_table[spritz_random32_uniform(&the_ctx, (uint32_t)(sizeof(alphanumeric_table)))];
92-
}
93-
94-
/* Print the password */
95-
Serial.write(buf, password_len);
96-
Serial.println();
9775

98-
delay(1000); /* Wait 1s */
76+
void loop() {
77+
/* Print random uniformly distributed alphanumeric characters.
78+
* Better than: rand_char = charactersTable[random() % number_Of_elements_In_charactersTable]
79+
*/
80+
for (uint8_t i = 0; i < PASSWORD_LEN; i++) {
81+
Serial.print((char)alphanumeric_table[spritz_random32_uniform(&the_ctx, (uint32_t)(sizeof(alphanumeric_table)))]);
9982
}
83+
Serial.println();
84+
85+
delay(1000); /* Wait 1s */
10086
}

0 commit comments

Comments
 (0)