|
| 1 | +/* |
| 2 | + Configure and Lock your ATECCX08 SecureElement |
| 3 | +
|
| 4 | + This sketch can be used to apply default configuration and lock |
| 5 | + yout ATECCX08 Secure Element. |
| 6 | + Default configuration can be found here: |
| 7 | + https://github.com/arduino-libraries/ArduinoECCX08/blob/master/src/utility/ECCX08DefaultTLSConfig.h |
| 8 | +
|
| 9 | + SE050 do not have EEPROM configuration and do not need to be locked |
| 10 | + to work correctly. secureElement.locked() always returns true for SE050 |
| 11 | + and the sketch does nothing. |
| 12 | +
|
| 13 | + The circuit: |
| 14 | + - A board equipped with ECC508 or ECC608 or SE050 chip |
| 15 | +
|
| 16 | + This example code is in the public domain. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <Arduino_SecureElement.h> |
| 20 | + |
| 21 | +void setup() { |
| 22 | + Serial.begin(9600); |
| 23 | + while (!Serial); |
| 24 | + |
| 25 | + SecureElement secureElement; |
| 26 | + |
| 27 | + if (!secureElement.begin()) { |
| 28 | + Serial.println("No SecureElement present!"); |
| 29 | + while (1); |
| 30 | + } |
| 31 | + |
| 32 | + String serialNumber = secureElement.serialNumber(); |
| 33 | + |
| 34 | + Serial.print("SecureElement Serial Number = "); |
| 35 | + Serial.println(serialNumber); |
| 36 | + Serial.println(); |
| 37 | + |
| 38 | + if (!secureElement.locked()) { |
| 39 | + String lock = promptAndReadLine("The SecureElement on your board is not locked, would you like to PERMANENTLY configure and lock it now? (y/N)", "N"); |
| 40 | + lock.toLowerCase(); |
| 41 | + |
| 42 | + if (!lock.startsWith("y")) { |
| 43 | + Serial.println("Unfortunately you can't proceed without locking it :("); |
| 44 | + while (1); |
| 45 | + } |
| 46 | + |
| 47 | + if (!secureElement.writeConfiguration()) { |
| 48 | + Serial.println("Writing SecureElement configuration failed!"); |
| 49 | + while (1); |
| 50 | + } |
| 51 | + |
| 52 | + if (!secureElement.lock()) { |
| 53 | + Serial.println("Locking SecureElement configuration failed!"); |
| 54 | + while (1); |
| 55 | + } |
| 56 | + |
| 57 | + Serial.println("SecureElement locked successfully"); |
| 58 | + Serial.println(); |
| 59 | + } else { |
| 60 | +#if defined(SECURE_ELEMENT_IS_ECCX08) |
| 61 | + Serial.println("SecureElement already locked!"); |
| 62 | + Serial.println(); |
| 63 | +#else |
| 64 | + Serial.println("SecureElement does not need to be locked!"); |
| 65 | + Serial.println(); |
| 66 | +#endif |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +void loop() { |
| 72 | + // do nothing |
| 73 | +} |
| 74 | + |
| 75 | +String promptAndReadLine(const char* prompt, const char* defaultValue) { |
| 76 | + Serial.print(prompt); |
| 77 | + Serial.print(" ["); |
| 78 | + Serial.print(defaultValue); |
| 79 | + Serial.print("]: "); |
| 80 | + |
| 81 | + String s = readLine(); |
| 82 | + |
| 83 | + if (s.length() == 0) { |
| 84 | + s = defaultValue; |
| 85 | + } |
| 86 | + |
| 87 | + Serial.println(s); |
| 88 | + |
| 89 | + return s; |
| 90 | +} |
| 91 | + |
| 92 | +String readLine() { |
| 93 | + String line; |
| 94 | + |
| 95 | + while (1) { |
| 96 | + if (Serial.available()) { |
| 97 | + char c = Serial.read(); |
| 98 | + |
| 99 | + if (c == '\r') { |
| 100 | + // ignore |
| 101 | + continue; |
| 102 | + } else if (c == '\n') { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + line += c; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return line; |
| 111 | +} |
0 commit comments