|
22 | 22 | #include <SpritzCipher.h> |
23 | 23 | #include <ESP8266WiFi.h> |
24 | 24 |
|
25 | | -/* The RNG seed */ |
26 | | -volatile uint32_t hw_rng; |
27 | | -uint8_t entropy_buf[256]; /* You can also use malloc() & free(). */ |
| 25 | + |
28 | 26 |
|
29 | 27 | /* Table of alphabetic (uppercase and lowercase) and numeric characters */ |
30 | | -const uint8_t alphanumeric_table[62] = |
| 28 | +const char alphanumeric_table[62] = |
31 | 29 | { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
32 | 30 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
33 | 31 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
34 | 32 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
35 | 33 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' |
36 | 34 | }; |
37 | 35 |
|
| 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; |
38 | 41 |
|
39 | 42 | void setup() { |
| 43 | + volatile uint32_t hw_rng; /* The RNG seed */ |
| 44 | + uint8_t buf[32]; |
| 45 | + |
40 | 46 | /* Initialize serial and wait for port to open */ |
41 | | - Serial.begin(9600); |
| 47 | + Serial.begin(115200); |
42 | 48 | while (!Serial) { |
43 | 49 | ; /* Wait for serial port to connect. Needed for Leonardo only */ |
44 | 50 | } |
45 | | -} |
46 | | - |
47 | 51 |
|
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 |
54 | 54 | */ |
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++) { |
57 | 60 | delay(1); |
58 | 61 | 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 */ |
63 | 63 | } |
64 | | - |
65 | 64 |
|
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 */ |
72 | 65 | 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))); |
77 | 67 |
|
78 | 68 | /* 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) */ |
81 | 70 | spritz_memzero(buf, (uint16_t)(sizeof(buf))); /* wipe "buf" data by replacing it with zeros (0x00) */ |
82 | 71 |
|
| 72 | + Serial.println(F("\n\n[Make strong Alphanumeric passwords]\n\n")); |
| 73 | +} |
83 | 74 |
|
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(); |
97 | 75 |
|
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)))]); |
99 | 82 | } |
| 83 | + Serial.println(); |
| 84 | + |
| 85 | + delay(1000); /* Wait 1s */ |
100 | 86 | } |
0 commit comments