Skip to content

Commit 0a93443

Browse files
authored
Merge pull request #10 from pennam/config
Add example for ATECCX08 configuration and locking
2 parents e5e0ccc + 85baef4 commit 0a93443

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

Diff for: examples/CertificateSigningRequest/CertificateSigningRequest.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This sketch can be used to generate a CSR for a private key
55
generated in an ECC508/ECC608 or SE050 crypto chip slot.
66
7-
If the ECC508/ECC608 is not configured and locked it prompts
7+
If the SecureElement is not configured and locked it prompts
88
the user to configure and lock the chip with a default TLS
99
configuration.
1010
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

Diff for: examples/RandomNumber/RandomNumber.ino

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/*
2-
secureElement Random Number
2+
SecureElement Random Number
33
44
This sketch uses the ECC508/ECC608 or SE050 to generate a random number
55
every second and print it to the Serial Monitor
66
7+
If the SecureElement is not configured and locked the ConfigurationLocking
8+
example should be used before running this sketch to setup the chip with a
9+
default TLS configuration.
10+
711
Circuit:
812
- A board equipped with ECC508 or ECC608 or SE050 chip
913
@@ -19,12 +23,12 @@ void setup() {
1923
while (!Serial);
2024

2125
if (!secureElement.begin()) {
22-
Serial.println("Failed to communicate with ECC508/ECC608!");
26+
Serial.println("Failed to communicate with SecureElement!");
2327
while (1);
2428
}
2529

2630
if (!secureElement.locked()) {
27-
Serial.println("The ECC508/ECC608 is not locked!");
31+
Serial.println("The SecureElement is not locked!");
2832
while (1);
2933
}
3034
}
@@ -35,4 +39,3 @@ void loop() {
3539

3640
delay(1000);
3741
}
38-

Diff for: examples/SelfSignedCertificate/SelfSignedCertificate.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This sketch can be used to generate a self signed certificate
55
for a private key generated in an ECC508/ECC608 or SE050 crypto chip slot.
66
7-
If the crypto chip is not configured and locked it prompts
7+
If the SecureElement is not configured and locked it prompts
88
the user to configure and lock the chip with a default TLS
99
configuration.
1010
@@ -145,4 +145,4 @@ String readLine() {
145145
}
146146

147147
return line;
148-
}
148+
}

0 commit comments

Comments
 (0)