-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathECCX08ECDH.ino
75 lines (60 loc) · 1.73 KB
/
ECCX08ECDH.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
ECCX08 ECDH
This sketch uses the ECC608 to compute the ECDH shared secret
for a ECCX08 private key and a public key.
*/
#include <ArduinoECCX08.h>
void setup() {
Serial.begin(9600);
while (!Serial);
if (!ECCX08.begin()) {
Serial.println("Failed to communicate with ECC508/ECC608!");
while (1);
}
if (!ECCX08.locked()) {
Serial.println("The ECC508/ECC608 is not locked!");
while (1);
}
int privateKeySlot = 2;
int counterpartyKeySlot = 3;
byte devicePubKey[64];
if (!ECCX08.generatePrivateKey(privateKeySlot, devicePubKey)){
Serial.println("Failed to generate private key.");
} else {
Serial.println("Device Public key:");
printHex(devicePubKey, 64);
}
byte counterPartyPubKey[64];
// Yes, this function actually returns a public key.
if (!ECCX08.generatePrivateKey(counterpartyKeySlot, counterPartyPubKey)){
Serial.println("Failed to generate public key.");
} else {
Serial.println("Counterparty Public key:");
printHex(counterPartyPubKey, 64);
}
byte sharedSecret[32];
if (!ECCX08.ecdh(privateKeySlot, ECDH_MODE_OUTPUT, counterPartyPubKey, sharedSecret)) {
Serial.println("The ecdh function failed!");
while (1);
}
Serial.print("Shared secret = ");
printHex(sharedSecret, 32);
byte output[1];
if (!ECCX08.ecdh(privateKeySlot, ECDH_MODE_TEMPKEY, counterPartyPubKey, output)) {
Serial.println("The ecdh function failed!");
while (1);
}
Serial.print("Return code = ");
printHex(output, 1);
}
void loop() {
}
void printHex(uint8_t *data, uint8_t length) {
char tmp[16];
for (int i = 0; i < length; i++) {
sprintf(tmp, "%.2X", data[i]);
Serial.print(tmp);
Serial.print(" ");
}
Serial.println();
}