|
| 1 | +/* |
| 2 | + ECCX08 HMAC functionality example |
| 3 | +
|
| 4 | + This sketch uses the ECC608 to generate an hmac on some data. |
| 5 | + Stores key using nonce. |
| 6 | +
|
| 7 | + Tested on the Arduino Nano RP2040. |
| 8 | +
|
| 9 | + created 10 October 2022 |
| 10 | + by Raul Leclair |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <ArduinoECCX08.h> |
| 14 | + |
| 15 | +#define TEMPKEY_SLOT 0xFFFF |
| 16 | + |
| 17 | +byte nonceKey[] = { |
| 18 | + 0x10, 0x10, 0x10, 0x10 |
| 19 | +}; |
| 20 | + |
| 21 | +byte data[] = { |
| 22 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 23 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 24 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 25 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 26 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 27 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 28 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 29 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 30 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 31 | +}; |
| 32 | + |
| 33 | +int dataLength = sizeof(data); |
| 34 | + |
| 35 | +void setup() { |
| 36 | + Serial.begin(115200); |
| 37 | + while (!Serial); |
| 38 | + |
| 39 | + if (!ECCX08.begin()) { |
| 40 | + Serial.println("Failed to initialize ECCX08 board."); |
| 41 | + while (1); |
| 42 | + } |
| 43 | + |
| 44 | + // Perform nonce |
| 45 | + if (!ECCX08.nonce(nonceKey)) |
| 46 | + { |
| 47 | + Serial.println("Failed to perform nonce."); |
| 48 | + while (1); |
| 49 | + } |
| 50 | + |
| 51 | + // Starting HMAC operation on tempkey slot |
| 52 | + if (!ECCX08.beginHMAC(TEMPKEY_SLOT)) { |
| 53 | + Serial.println("Failed to start HMAC operation."); |
| 54 | + while (1); |
| 55 | + } |
| 56 | + |
| 57 | + if (!ECCX08.updateHMAC(data, dataLength)) { |
| 58 | + Serial.println("Failed to update HMAC operation."); |
| 59 | + while (1); |
| 60 | + } |
| 61 | + |
| 62 | + byte resultHMAC[32]; |
| 63 | + if (!ECCX08.endHMAC(resultHMAC)) { |
| 64 | + Serial.println("Failed to end HMAC operation"); |
| 65 | + while (1); |
| 66 | + } |
| 67 | + |
| 68 | + Serial.println("HMAC Result: "); |
| 69 | + for (int i = 0; i < sizeof(resultHMAC); i++) { |
| 70 | + char hexChar[2]; |
| 71 | + sprintf(hexChar, "%02X", resultHMAC[i]); |
| 72 | + Serial.print(hexChar); |
| 73 | + Serial.print(" "); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void loop() { |
| 78 | +} |
0 commit comments