From 0fe299d8a37d3583ec1859b893d4a1645435cfdc Mon Sep 17 00:00:00 2001 From: RLeclair Date: Thu, 20 Oct 2022 15:39:27 -0700 Subject: [PATCH 1/8] Adding HMAC functionality. --- src/ECCX08.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++- src/ECCX08.h | 7 ++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index 6145dd4..0a34051 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -405,7 +405,7 @@ int ECCX08Class::writeConfiguration(const byte data[]) // not writable continue; } - + if (!write(0, i / 4, &data[i], 4)) { return 0; } @@ -440,6 +440,106 @@ int ECCX08Class::lock() return 1; } + +int ECCX08Class::beginHMAC(uint16_t keySlot) +{ + uint8_t status; + + if (!wakeup()) { + return 0; + } + + if (!sendCommand(0x47, 0x04, keySlot)) { + return 0; + } + + delay(9); + + if (!receiveResponse(&status, sizeof(status))) { + return 0; + } + + delay(1); + idle(); + + if (status != 0) { + return 0; + } + + return 1; +} + +int ECCX08Class::updateHMAC(const byte data[], int length) { + uint8_t status; + + if (!wakeup()) { + return 0; + } + + int currLength = 0; + while (length) { + data += currLength; + + if (length > 64) { + currLength = 64; + } else { + currLength = length; + } + length -= currLength; + + if (!sendCommand(0x47, 0x01, length, data, length)) { + return 0; + } + + delay(9); + + if (!receiveResponse(&status, sizeof(status))) { + return 0; + } + + delay(1); + } + idle(); + + if (status != 0) { + return 0; + } + + return 1; +} + +int ECCX08Class::endHMAC(byte result[]) +{ + return endHMAC(NULL, 0, result); +} + +int ECCX08Class::endHMAC(const byte data[], int length, byte result[]) +{ + if (!wakeup()) { + return 0; + } + + if (!sendCommand(0x47, 0x02, length, data, length)) { + return 0; + } + + delay(9); + + if (!receiveResponse(result, 32)) { + return 0; + } + + delay(1); + idle(); + + return 1; +} + +int ECCX08Class::nonce(const byte data[]) +{ + return challenge(data); +} + int ECCX08Class::wakeup() { _wire->setClock(_wakeupFrequency); diff --git a/src/ECCX08.h b/src/ECCX08.h index 0439b17..54fdc3a 100644 --- a/src/ECCX08.h +++ b/src/ECCX08.h @@ -59,6 +59,13 @@ class ECCX08Class int readConfiguration(byte data[]); int lock(); + int beginHMAC(uint16_t keySlot); + int updateHMAC(const byte data[], int length); + int endHMAC(byte result[]); + int endHMAC(const byte data[], int length, byte result[]); + + int nonce(const byte data[]); + private: int wakeup(); int sleep(); From a36a841c64874d6ce7e350ced4ceb16608d9bcc8 Mon Sep 17 00:00:00 2001 From: RLeclair Date: Thu, 20 Oct 2022 16:07:19 -0700 Subject: [PATCH 2/8] Minor bug fix --- src/ECCX08.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index 0a34051..5631cd5 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -405,7 +405,7 @@ int ECCX08Class::writeConfiguration(const byte data[]) // not writable continue; } - + if (!write(0, i / 4, &data[i], 4)) { return 0; } @@ -476,6 +476,7 @@ int ECCX08Class::updateHMAC(const byte data[], int length) { return 0; } + // Processing message int currLength = 0; while (length) { data += currLength; @@ -486,8 +487,8 @@ int ECCX08Class::updateHMAC(const byte data[], int length) { currLength = length; } length -= currLength; - - if (!sendCommand(0x47, 0x01, length, data, length)) { + + if (!sendCommand(0x47, 0x01, currLength, data, currLength)) { return 0; } From bca4e138586f76f94fdb468245d3d03ad429ec13 Mon Sep 17 00:00:00 2001 From: RLeclair Date: Thu, 20 Oct 2022 16:53:05 -0700 Subject: [PATCH 3/8] Adding HMAC and nonce example --- examples/ECCX08HMAC/ECCX08HMAC.ino | 72 ++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 examples/ECCX08HMAC/ECCX08HMAC.ino diff --git a/examples/ECCX08HMAC/ECCX08HMAC.ino b/examples/ECCX08HMAC/ECCX08HMAC.ino new file mode 100644 index 0000000..631e4d2 --- /dev/null +++ b/examples/ECCX08HMAC/ECCX08HMAC.ino @@ -0,0 +1,72 @@ +/* + ECCX08 HMAC functionality example + + This sketch uses the ECC608 to generate an hmac on some data. + Stores key using nonce. + + Used the Arduino Nano RP2040. + + created 10 October 2022 + by Raul Leclair +*/ + +#include + +byte nonceKey[] = { + 0x10, 0x10, 0x10, 0x10 +}; +byte data[] = { + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 +}; +int dataLength = 56; + +void setup() { + Serial.begin(115200); + while (!Serial); + + if (!ECCX08.begin()) { + Serial.println("Failed to initialize ECC608 board."); + while (1); + } + + // Perform nonce + if (!ECCX08.nonce(nonceKey)) + { + Serial.println("Failed to do nonce."); + while (1); + } + + // Starting HMAC operation on tempkey slot + if (!ECCX08.beginHMAC(0xFFFF)) { + Serial.println("Failed to start HMAC operation."); + while (1); + } + + if (!ECCX08.updateHMAC(data, dataLength)) { + Serial.println("Failed to update HMAC operation."); + while (1); + } + + byte resultHMAC[32]; + if (!ECCX08.endHMAC(resultHMAC)) { + Serial.println("Failed to end HMAC operation"); + while (1); + } + + Serial.println("HMAC Result: "); + for (int i = 0; i Date: Thu, 20 Oct 2022 16:59:37 -0700 Subject: [PATCH 4/8] Adding 508 check --- examples/ECCX08HMAC/ECCX08HMAC.ino | 6 ++++-- src/ECCX08.cpp | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/ECCX08HMAC/ECCX08HMAC.ino b/examples/ECCX08HMAC/ECCX08HMAC.ino index 631e4d2..cb83171 100644 --- a/examples/ECCX08HMAC/ECCX08HMAC.ino +++ b/examples/ECCX08HMAC/ECCX08HMAC.ino @@ -22,9 +22,11 @@ byte data[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, - 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, }; -int dataLength = 56; +int dataLength = 72; void setup() { Serial.begin(115200); diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index 5631cd5..a07aabe 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -443,6 +443,12 @@ int ECCX08Class::lock() int ECCX08Class::beginHMAC(uint16_t keySlot) { + // HMAC implementation is only for ATECC608 + long ver = version() & 0x0F00000; + if (ver != 0x0600000) { + return 0; + } + uint8_t status; if (!wakeup()) { From 7a9b7c20646fed3e9fbf7b69574f1679f93fcb23 Mon Sep 17 00:00:00 2001 From: RLeclair Date: Thu, 20 Oct 2022 17:05:13 -0700 Subject: [PATCH 5/8] Adding newline --- examples/ECCX08HMAC/ECCX08HMAC.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/ECCX08HMAC/ECCX08HMAC.ino b/examples/ECCX08HMAC/ECCX08HMAC.ino index cb83171..5f0295b 100644 --- a/examples/ECCX08HMAC/ECCX08HMAC.ino +++ b/examples/ECCX08HMAC/ECCX08HMAC.ino @@ -15,6 +15,7 @@ byte nonceKey[] = { 0x10, 0x10, 0x10, 0x10 }; + byte data[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, @@ -26,6 +27,7 @@ byte data[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, }; + int dataLength = 72; void setup() { @@ -71,4 +73,4 @@ void setup() { } void loop() { -} \ No newline at end of file +} From 4562d7c3bb80e00d7cacfa8d1aca3d6be1b14f18 Mon Sep 17 00:00:00 2001 From: Raul Leclair Date: Fri, 18 Nov 2022 10:30:20 -0800 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Dane Walton --- examples/ECCX08HMAC/ECCX08HMAC.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/ECCX08HMAC/ECCX08HMAC.ino b/examples/ECCX08HMAC/ECCX08HMAC.ino index 5f0295b..0a09f86 100644 --- a/examples/ECCX08HMAC/ECCX08HMAC.ino +++ b/examples/ECCX08HMAC/ECCX08HMAC.ino @@ -4,7 +4,7 @@ This sketch uses the ECC608 to generate an hmac on some data. Stores key using nonce. - Used the Arduino Nano RP2040. + Tested on the Arduino Nano RP2040. created 10 October 2022 by Raul Leclair @@ -28,21 +28,21 @@ byte data[] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, }; -int dataLength = 72; +int dataLength = sizeof(data); void setup() { Serial.begin(115200); while (!Serial); if (!ECCX08.begin()) { - Serial.println("Failed to initialize ECC608 board."); + Serial.println("Failed to initialize ECCX08 board."); while (1); } // Perform nonce if (!ECCX08.nonce(nonceKey)) { - Serial.println("Failed to do nonce."); + Serial.println("Failed to perform nonce."); while (1); } @@ -64,7 +64,7 @@ void setup() { } Serial.println("HMAC Result: "); - for (int i = 0; i Date: Fri, 18 Nov 2022 10:47:31 -0800 Subject: [PATCH 7/8] Addressing comments --- examples/ECCX08HMAC/ECCX08HMAC.ino | 4 +++- src/ECCX08.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/ECCX08HMAC/ECCX08HMAC.ino b/examples/ECCX08HMAC/ECCX08HMAC.ino index 0a09f86..935334d 100644 --- a/examples/ECCX08HMAC/ECCX08HMAC.ino +++ b/examples/ECCX08HMAC/ECCX08HMAC.ino @@ -12,6 +12,8 @@ #include +#define TEMPKEY_SLOT 0xFFFF + byte nonceKey[] = { 0x10, 0x10, 0x10, 0x10 }; @@ -47,7 +49,7 @@ void setup() { } // Starting HMAC operation on tempkey slot - if (!ECCX08.beginHMAC(0xFFFF)) { + if (!ECCX08.beginHMAC(TEMPKEY_SLOT)) { Serial.println("Failed to start HMAC operation."); while (1); } diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index a07aabe..af7db0d 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -444,13 +444,14 @@ int ECCX08Class::lock() int ECCX08Class::beginHMAC(uint16_t keySlot) { // HMAC implementation is only for ATECC608 - long ver = version() & 0x0F00000; - if (ver != 0x0600000) { + uint8_t status; + long ecc608ver = 0x0600000; + long eccCurrVer = version() & 0x0F00000; + + if (eccCurVer != ecc608ver) { return 0; } - uint8_t status; - if (!wakeup()) { return 0; } From eb93d9db9d408fb1aca5faf55c8a57c01274ca70 Mon Sep 17 00:00:00 2001 From: RLeclair Date: Fri, 18 Nov 2022 11:06:41 -0800 Subject: [PATCH 8/8] Quick fix --- src/ECCX08.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index af7db0d..a858668 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -448,7 +448,7 @@ int ECCX08Class::beginHMAC(uint16_t keySlot) long ecc608ver = 0x0600000; long eccCurrVer = version() & 0x0F00000; - if (eccCurVer != ecc608ver) { + if (eccCurrVer != ecc608ver) { return 0; }