Skip to content

Commit 66ebf75

Browse files
committed
Add signing example
1 parent 8fb63ce commit 66ebf75

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
ECCX08 Signing
3+
4+
This sketch uses the ECC508 or ECC608 to sign some data
5+
using the private key of a key slot, the signature
6+
is printed to the Serial monitor. Then the signature is
7+
verified using the public key of the slot.
8+
9+
NOTE: the input data must be 64 bytes in length!
10+
11+
Circuit:
12+
- MKR board with ECC508 or ECC608 on board
13+
14+
created 5 November 2019
15+
by Sandeep Mistry
16+
*/
17+
18+
#include <ArduinoECCX08.h>
19+
20+
const byte input[64] = {
21+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
22+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
23+
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
24+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
25+
};
26+
27+
const int slot = 0;
28+
29+
void setup() {
30+
Serial.begin(9600);
31+
while (!Serial);
32+
33+
Serial.println("ECCX08 Signing");
34+
Serial.println();
35+
36+
if (!ECCX08.begin()) {
37+
Serial.println("Failed to communicate with ECC508/ECC608!");
38+
while (1);
39+
}
40+
41+
if (!ECCX08.locked()) {
42+
Serial.println("The ECC508/ECC608 is not locked!");
43+
while (1);
44+
}
45+
46+
// print the input
47+
Serial.print("Input is: ");
48+
printBufferHex(input, sizeof(input));
49+
50+
// retrieve the public key
51+
byte publicKey[64];
52+
ECCX08.generatePublicKey(slot, publicKey);
53+
54+
// print the public key
55+
Serial.print("Public key of slot ");
56+
Serial.print(slot);
57+
Serial.print(" is: ");
58+
printBufferHex(publicKey, sizeof(publicKey));
59+
60+
// calculate the signature, input MUST be 64-byte array
61+
byte signature[64];
62+
ECCX08.ecSign(slot, input, signature);
63+
64+
// print the signature
65+
Serial.print("Signature using slot ");
66+
Serial.print(slot);
67+
Serial.print(" is: ");
68+
printBufferHex(signature, sizeof(signature));
69+
70+
Serial.println();
71+
72+
// To make the signature verifcation fail, uncomment the next line:
73+
// signature[0] = 0x00;
74+
75+
// validate the signature
76+
if (ECCX08.ecdsaVerify(input, signature, publicKey)) {
77+
Serial.println("Verified signature successfully :D");
78+
} else {
79+
Serial.println("oh no! failed to verify signature :(");
80+
}
81+
}
82+
83+
void loop() {
84+
// do nothing
85+
}
86+
87+
void printBufferHex(const byte input[], int inputLength) {
88+
for (int i = 0; i < inputLength; i++) {
89+
Serial.print(input[i] >> 4, HEX);
90+
Serial.print(input[i] & 0x0f, HEX);
91+
}
92+
Serial.println();
93+
}

0 commit comments

Comments
 (0)