From cbcf89417c2fc668e35172abb63a2c3fb2e68f69 Mon Sep 17 00:00:00 2001 From: maihde Date: Sun, 16 Jul 2023 18:54:18 -0400 Subject: [PATCH 1/4] Update ECCX08.h with method description --- src/ECCX08.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ECCX08.h b/src/ECCX08.h index 54fdc3a..ead8468 100644 --- a/src/ECCX08.h +++ b/src/ECCX08.h @@ -66,6 +66,9 @@ class ECCX08Class int nonce(const byte data[]); + long incrementCounter(int keyId); + long readCounter(int keyId); + private: int wakeup(); int sleep(); From 0fe82c9ca32fa51020a1db64abc8f4989970419d Mon Sep 17 00:00:00 2001 From: maihde Date: Sun, 16 Jul 2023 18:57:03 -0400 Subject: [PATCH 2/4] Update ECCX08.cpp with counter functions --- src/ECCX08.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index 0164343..3b7383e 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -548,6 +548,54 @@ int ECCX08Class::nonce(const byte data[]) return challenge(data); } +long ECCX08Class::incrementCounter(int keyId) +{ + uint32_t counter; // the counter can go up to 2,097,151 + + if (!wakeup()) { + return -1; + } + + if (!sendCommand(0x24, 1, keyId)) { + return -1; + } + + delay(20); + + if (!receiveResponse(&counter, sizeof(counter))) { + return -1; + } + + delay(1); + idle(); + + return counter; +} + +long ECCX08Class::readCounter(int keyId) +{ + uint32_t counter; // the counter can go up to 2,097,151 + + if (!wakeup()) { + return -1; + } + + if (!sendCommand(0x24, 0, keyId)) { + return -1; + } + + delay(20); + + if (!receiveResponse(&counter, sizeof(counter))) { + return -1; + } + + delay(1); + idle(); + + return counter; +} + int ECCX08Class::wakeup() { _wire->setClock(_wakeupFrequency); @@ -892,4 +940,4 @@ uint16_t ECCX08Class::crc16(const byte data[], size_t length) ECCX08Class ECCX08(CRYPTO_WIRE, 0x60); #else ECCX08Class ECCX08(Wire, 0x60); -#endif \ No newline at end of file +#endif From 6ced7ecd17d1afeeb15d9ca849b1aac341c0180f Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 2 Jan 2024 14:52:47 +0100 Subject: [PATCH 3/4] ECCX08 monotonic counter feature - rename keyId to counterId - add counterId value check - split functions to keep library style --- src/ECCX08.cpp | 50 ++++++++++++++++++++++++++++++++++++++------------ src/ECCX08.h | 6 ++++-- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/ECCX08.cpp b/src/ECCX08.cpp index 3b7383e..502497b 100644 --- a/src/ECCX08.cpp +++ b/src/ECCX08.cpp @@ -548,51 +548,77 @@ int ECCX08Class::nonce(const byte data[]) return challenge(data); } -long ECCX08Class::incrementCounter(int keyId) +int ECCX08Class::incrementCounter(int counterId, long& counter) { - uint32_t counter; // the counter can go up to 2,097,151 + if (counterId < 0 || counterId > 1) { + return 0; + } if (!wakeup()) { - return -1; + return 0; } - if (!sendCommand(0x24, 1, keyId)) { - return -1; + if (!sendCommand(0x24, 1, counterId)) { + return 0; } delay(20); if (!receiveResponse(&counter, sizeof(counter))) { - return -1; + return 0; } delay(1); idle(); + return 1; +} + +long ECCX08Class::incrementCounter(int counterId) +{ + long counter; // the counter can go up to 2,097,151 + + if(!incrementCounter(counterId, counter)) { + return -1; + } + return counter; } -long ECCX08Class::readCounter(int keyId) +int ECCX08Class::readCounter(int counterId, long& counter) { - uint32_t counter; // the counter can go up to 2,097,151 + if (counterId < 0 || counterId > 1) { + return 0; + } if (!wakeup()) { - return -1; + return 0; } - if (!sendCommand(0x24, 0, keyId)) { - return -1; + if (!sendCommand(0x24, 0, counterId)) { + return 0; } delay(20); if (!receiveResponse(&counter, sizeof(counter))) { - return -1; + return 0; } delay(1); idle(); + return 1; +} + +long ECCX08Class::readCounter(int counterId) +{ + long counter; // the counter can go up to 2,097,151 + + if(!readCounter(counterId, counter)) { + return -1; + } + return counter; } diff --git a/src/ECCX08.h b/src/ECCX08.h index ead8468..c2f21e1 100644 --- a/src/ECCX08.h +++ b/src/ECCX08.h @@ -66,8 +66,10 @@ class ECCX08Class int nonce(const byte data[]); - long incrementCounter(int keyId); - long readCounter(int keyId); + int incrementCounter(int counterId, long& counter); + long incrementCounter(int counterId); + int readCounter(int counterId, long& counter); + long readCounter(int counterId); private: int wakeup(); From 6b15f79a758c3c1a53630ccf3a913c277f47a1e6 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 2 Jan 2024 15:22:07 +0100 Subject: [PATCH 4/4] ECCX08 monotonic counter example sketch --- examples/ECCX08Counter/ECCX08Counter.ino | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/ECCX08Counter/ECCX08Counter.ino diff --git a/examples/ECCX08Counter/ECCX08Counter.ino b/examples/ECCX08Counter/ECCX08Counter.ino new file mode 100644 index 0000000..319543b --- /dev/null +++ b/examples/ECCX08Counter/ECCX08Counter.ino @@ -0,0 +1,43 @@ +/* + ECCX08 Counter + + This sketch uses the ECC508 or ECC608 to increment a monotonic + counter at each startup + + Circuit: + - Any board with ECC508 or ECC608 on board + +*/ + +#include + +const int keyId = 5; +long counter = -1; + +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!ECCX08.begin()) { + Serial.println("Failed to communicate with ECC508/ECC608!"); + while (1); + } + + if (!ECCX08.incrementCounter(keyId, counter)) { + Serial.println("Failed to increment counter"); + while (1); + } +} + +void loop() { + if (!ECCX08.readCounter(keyId, counter)) { + Serial.println("Failed to read counter"); + while (1); + } + + Serial.print("Counter value = "); + Serial.println(counter); + + delay(1000); +} +