Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ECDH key exchange #35

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Configure ECDH mode for key destination
  • Loading branch information
jezcrypt committed Aug 26, 2021
commit 83f1b7f00f0e2b0ccdd61793e81fd842387c12d6
11 changes: 10 additions & 1 deletion examples/ECCX08ECDH/ECCX08ECDH.ino
Original file line number Diff line number Diff line change
@@ -43,13 +43,22 @@ void setup() {
}

byte sharedSecret[32];
if (!ECCX08.ecdh(privateKeySlot, counterPartyPubKey, sharedSecret)) {
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() {
14 changes: 10 additions & 4 deletions src/ECCX08.cpp
Original file line number Diff line number Diff line change
@@ -326,20 +326,26 @@ int ECCX08Class::endSHA256(const byte data[], int length, byte result[])
return 1;
}

int ECCX08Class::ecdh(int slot, const byte pubKeyXandY[], byte sharedSecret[])
int ECCX08Class::ecdh(int slot, byte mode, const byte pubKeyXandY[], byte output[])
{
if (!wakeup()) {
return 0;
}

if (!sendCommand(0x43, 0x0c, slot, pubKeyXandY, 64)) {
if (!sendCommand(0x43, mode, slot, pubKeyXandY, 64)) {
return 0;
}

delay(55);

if (!receiveResponse(sharedSecret, 32)) {
return 0;
if (mode == ECDH_MODE_OUTPUT) {
if (!receiveResponse(output, 32)) {
return 0;
}
} else if (mode == ECDH_MODE_TEMPKEY) {
if (!receiveResponse(output, 1)) {
return 0;
}
}

delay(1);
4 changes: 3 additions & 1 deletion src/ECCX08.h
Original file line number Diff line number Diff line change
@@ -50,7 +50,9 @@ class ECCX08Class
int endSHA256(byte result[]);
int endSHA256(const byte data[], int length, byte result[]);

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

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