Skip to content

Commit 83f1b7f

Browse files
committed
Configure ECDH mode for key destination
1 parent e3ef860 commit 83f1b7f

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

examples/ECCX08ECDH/ECCX08ECDH.ino

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ void setup() {
4343
}
4444

4545
byte sharedSecret[32];
46-
if (!ECCX08.ecdh(privateKeySlot, counterPartyPubKey, sharedSecret)) {
46+
if (!ECCX08.ecdh(privateKeySlot, ECDH_MODE_OUTPUT, counterPartyPubKey, sharedSecret)) {
4747
Serial.println("The ecdh function failed!");
4848
while (1);
4949
}
5050

5151
Serial.print("Shared secret = ");
5252
printHex(sharedSecret, 32);
53+
54+
byte output[1];
55+
if (!ECCX08.ecdh(privateKeySlot, ECDH_MODE_TEMPKEY, counterPartyPubKey, output)) {
56+
Serial.println("The ecdh function failed!");
57+
while (1);
58+
}
59+
60+
Serial.print("Return code = ");
61+
printHex(output, 1);
5362
}
5463

5564
void loop() {

src/ECCX08.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,26 @@ int ECCX08Class::endSHA256(const byte data[], int length, byte result[])
326326
return 1;
327327
}
328328

329-
int ECCX08Class::ecdh(int slot, const byte pubKeyXandY[], byte sharedSecret[])
329+
int ECCX08Class::ecdh(int slot, byte mode, const byte pubKeyXandY[], byte output[])
330330
{
331331
if (!wakeup()) {
332332
return 0;
333333
}
334334

335-
if (!sendCommand(0x43, 0x0c, slot, pubKeyXandY, 64)) {
335+
if (!sendCommand(0x43, mode, slot, pubKeyXandY, 64)) {
336336
return 0;
337337
}
338338

339339
delay(55);
340340

341-
if (!receiveResponse(sharedSecret, 32)) {
342-
return 0;
341+
if (mode == ECDH_MODE_OUTPUT) {
342+
if (!receiveResponse(output, 32)) {
343+
return 0;
344+
}
345+
} else if (mode == ECDH_MODE_TEMPKEY) {
346+
if (!receiveResponse(output, 1)) {
347+
return 0;
348+
}
343349
}
344350

345351
delay(1);

src/ECCX08.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class ECCX08Class
5050
int endSHA256(byte result[]);
5151
int endSHA256(const byte data[], int length, byte result[]);
5252

53-
int ecdh(int slot, const byte pubKeyXandY[], byte sharedSecret[]);
53+
int ecdh(int slot, byte mode, const byte pubKeyXandY[], byte sharedSecret[]);
54+
#define ECDH_MODE_TEMPKEY ((uint8_t)0x08) //!< ECDH mode: write to TempKey
55+
#define ECDH_MODE_OUTPUT ((uint8_t)0x0c) //!< ECDH mode: write to buffer
5456

5557
int readSlot(int slot, byte data[], int length);
5658
int writeSlot(int slot, const byte data[], int length);

0 commit comments

Comments
 (0)