cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
Sign.ino
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
40#include <CryptnoxWallet.h>
41#include <SPI.h>
42
44#define PN532_SS_PIN (10U)
45
50#define DEMO_PIN "000000000"
51
54
57
60
63
66
73void setup() {
74 serialAdapter.begin(115200);
75 delay(1000); /* Arduino R4: wait for Serial */
76 SPI.begin();
77
78 if (!wallet.begin()) {
79 serialAdapter.println(F("PN532 init failed"));
80 while (1);
81 }
82}
83
100void loop() {
101 CW_SecureSession session;
102 if (!wallet.connect(session)) {
103 serialAdapter.println(F("Card not detected"));
104 wallet.disconnect(session);
105 delay(1000);
106 return;
107 }
108
109 uint8_t hash[CW_HASH_SIZE];
110 memset(hash, 0x01, sizeof(hash)); /* replace with SHA-256 of your tx */
111
112 CW_SignRequest req(session,
116 req.hash = hash;
117 req.hashLength = sizeof(hash);
118 CW_Utils::safe_memcpy(req.pin, sizeof(req.pin),
119 reinterpret_cast<const uint8_t*>(DEMO_PIN), strlen(DEMO_PIN));
120
121 CW_SignResult sig = wallet.sign(req);
122 if (sig.errorCode == CW_OK) {
123 serialAdapter.println(F("Signature OK"));
124
125 serialAdapter.print(F(" r = "));
126 for (uint8_t i = 0U; i < 32U; i++) {
127 uint8_t b = sig.signature[CW_SIG_R_OFFSET + i];
128 if (b < 0x10U) { serialAdapter.print(F("0")); }
129 serialAdapter.print(b, HEX);
130 }
131 serialAdapter.println();
132
133 serialAdapter.print(F(" s = "));
134 for (uint8_t i = 0U; i < 32U; i++) {
135 uint8_t b = sig.signature[CW_SIG_S_OFFSET + i];
136 if (b < 0x10U) { serialAdapter.print(F("0")); }
137 serialAdapter.print(b, HEX);
138 }
139 serialAdapter.println();
140
141 serialAdapter.print(F(" errorCode = 0x"));
142 serialAdapter.println(sig.errorCode, HEX);
143 } else if (sig.errorCode == CW_SIGN_PIN_INCORRECT) {
144 /* CRITICAL: do NOT loop — each wrong PIN attempt burns a retry. */
145 serialAdapter.println(F("Wrong PIN — halting to protect retry counter"));
146 CW_Utils::secure_wipe(hash, sizeof(hash));
147 CW_Utils::secure_wipe(sig.signature, sizeof(sig.signature));
148 wallet.disconnect(session);
149 while (1);
150 } else {
151 serialAdapter.print(F("Sign failed: 0x"));
152 serialAdapter.println(sig.errorCode, HEX);
153 }
154
155 CW_Utils::secure_wipe(hash, sizeof(hash));
156 CW_Utils::secure_wipe(sig.signature, sizeof(sig.signature));
157 wallet.disconnect(session);
158 delay(1000);
159}
void setup()
Arduino setup function.
CryptnoxWallet wallet(nfc, serialAdapter, cryptoProvider, platform)
PN532Adapter nfc(serialAdapter, PN532_SS, &SPI)
ArduinoLoggerAdapter serialAdapter
ArduinoPlatform platform
ArduinoCryptoProvider cryptoProvider
void loop()
Arduino main loop.
#define CW_SIG_R_OFFSET
Definition CW_Defs.h:116
#define CW_HASH_SIZE
Definition CW_Defs.h:108
#define CW_OK
Definition CW_Defs.h:80
#define CW_SIGN_WITH_PIN
Definition CW_Defs.h:92
#define CW_SIGN_CURR_K1
Definition CW_Defs.h:85
#define CW_SIGN_SIG_ECDSA_LOW_S
Definition CW_Defs.h:96
#define CW_SIG_S_OFFSET
Definition CW_Defs.h:117
#define CW_SIGN_PIN_INCORRECT
Definition CW_Defs.h:103
#define PN532_SS_PIN
SPI slave-select (CS) pin connected to the PN532 module.
Definition Connect.ino:31
#define DEMO_PIN
Demo PIN used by this example. Must match the PIN that the card was initialised with (4–9 ASCII digit...
Definition Sign.ino:50
CW_CryptoProvider implementation for the Arduino UNO R4 (RA4M1).
CW_Logger implementation wrapping Arduino's HardwareSerial.
CW_Platform implementation using Arduino's blocking delay().
static bool safe_memcpy(uint8_t *dst, size_t dstSize, const uint8_t *src, size_t count)
Safe memcpy — validates pointers, sizes, and checks for overlap.
Definition CW_Utils.cpp:50
static void secure_wipe(uint8_t *buf, size_t len)
Securely zero a buffer, guaranteed not to be optimised away.
Definition CW_Utils.cpp:37
High-level interface for interacting with a Cryptnox Hardware Wallet over NFC.
CW_NfcTransport implementation over the Adafruit_PN532 driver.
#define F(string_literal)
#define HEX
Holds cryptographic session state for reentrant secure channel operations.
Definition CW_Defs.h:168
Request parameters for CryptnoxWallet::sign.
const uint8_t * hash
uint8_t pin[CW_MAX_PIN_LENGTH]
Result of CryptnoxWallet::sign.
uint8_t signature[CW_RAW_SIGNATURE_SIZE]