cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
CryptnoxWallet.h
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
22
23#ifndef CRYPTNOXWALLET_H
24#define CRYPTNOXWALLET_H
25
26/******************************************************************
27 * 1. Included files
28 ******************************************************************/
29
30#include "platform_compat.h"
31#include "CW_Defs.h"
32#include "CW_Logger.h"
33#include "CW_Platform.h"
34#include "CW_SecureChannel.h"
35#include "CW_Utils.h"
36
37/******************************************************************
38 * 2. Typedefs / structs (sign API)
39 ******************************************************************/
40
42#define CW_CARD_NAME_MAX_LEN (20U)
43
45#define CW_CARD_EMAIL_MAX_LEN (60U)
46
58
60 name[0] = '\0';
61 email[0] = '\0';
62 }
63};
64
76 uint8_t keyType;
77 uint8_t signatureType;
80 const uint8_t* hash;
81 uint8_t hashLength;
82 const uint8_t* derivePath;
84
93 uint8_t kType = CW_SIGN_CURR_K1,
94 uint8_t sigType = CW_SIGN_SIG_ECDSA_LOW_S,
95 bool pinless = CW_SIGN_WITH_PIN)
96 : session(sess), keyType(kType), signatureType(sigType),
97 pinLessMode(pinless), hash(NULL), hashLength(0U),
98 derivePath(NULL), derivePathLength(0U) {
99 memset(pin, 0U, sizeof(pin));
100 }
101
106};
107
118 uint8_t errorCode;
119
122 memset(signature, 0U, sizeof(signature));
123 }
124};
125
126/******************************************************************
127 * 3. CryptnoxWallet class
128 ******************************************************************/
129
161public:
172
175
180 bool begin();
181
200 bool connect(CW_SecureSession& session);
201
218
232 void disconnect(CW_SecureSession& session);
233
245 bool getCardInfo(CW_SecureSession& session, CW_CardInfo* info = NULL);
246
267 bool verifyPin(CW_SecureSession& session, const uint8_t* pin, uint8_t pinLength);
268
299
309 bool writeUserData(CW_SecureSession& session, uint8_t slot,
310 const uint8_t* data, uint16_t dataLength);
311
323 static bool parseDerSignature(const uint8_t* der, uint8_t derLength,
324 uint8_t* r, uint8_t& rLength,
325 uint8_t* s, uint8_t& sLength);
326
327private:
331
332 bool isSecureChannelOpen(const CW_SecureSession& session) const;
334
335 /* Sign helper methods */
336 bool validateSignRequest(const CW_SignRequest& request, CW_SignResult& result);
337 void buildSignPayload(const CW_SignRequest& request, uint8_t* data, uint16_t& dataLength);
338 bool sendSignApdu(CW_SignRequest& request, const uint8_t* data, uint16_t dataLength,
339 uint8_t* derResponse, uint16_t& derLength, CW_SignResult& result);
340 bool extractRawSignature(const uint8_t* derResponse, uint16_t derLength, CW_SignResult& result);
341 void debugPrintSignature(const uint8_t* signature);
342};
343
344#endif // CRYPTNOXWALLET_H
ArduinoPlatform platform
Shared constants, error codes, and session state for the SDK.
#define CW_NOK
Definition CW_Defs.h:81
#define CW_RAW_SIGNATURE_SIZE
Definition CW_Defs.h:107
#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_MAX_PIN_LENGTH
Definition CW_Defs.h:111
Abstract logging interface.
Abstract platform interface for timing primitives.
Cryptnox secure channel protocol over NFC.
Platform-independent security and memory utilities.
Abstract interface for cryptographic operations used by CW_SecureChannel.
Abstract interface for serial/debug output.
Definition CW_Logger.h:48
Abstract interface for NFC transport operations.
Abstract interface for platform-specific operations used by the SDK.
Definition CW_Platform.h:39
Implements the Cryptnox secure channel protocol over NFC.
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
static bool parseDerSignature(const uint8_t *der, uint8_t derLength, uint8_t *r, uint8_t &rLength, uint8_t *s, uint8_t &sLength)
Parse a DER-encoded ECDSA signature to extract raw r and s values.
CW_Platform & _platform
Platform abstraction (sleep_ms).
bool isSecureChannelOpen(const CW_SecureSession &session) const
void debugPrintSignature(const uint8_t *signature)
CW_SecureChannel _secure
Owned secure channel.
CryptnoxWallet(CW_NfcTransport &driver, CW_Logger &logger, CW_CryptoProvider &crypto, CW_Platform &platform)
Construct a CryptnoxWallet.
CryptnoxWallet(const CryptnoxWallet &)=delete
bool writeUserData(CW_SecureSession &session, uint8_t slot, const uint8_t *data, uint16_t dataLength)
Write data to a user memory slot, paginating in CW_USER_DATA_PAGE_SIZE chunks.
CW_Logger & _logger
Logging interface.
CW_SignResult sign(CW_SignRequest &request)
Sign a 32-byte digest using a card-resident key.
CryptnoxWallet & operator=(const CryptnoxWallet &)=delete
void disconnect(CW_SecureSession &session)
Disconnect and securely clear the session.
bool extractRawSignature(const uint8_t *derResponse, uint16_t derLength, CW_SignResult &result)
bool getCardInfo(CW_SecureSession &session, CW_CardInfo *info=NULL)
Send a secured GET CARD INFO APDU (0x80FA0000) and optionally decode the owner name/email from the re...
bool establishSecureChannel(CW_SecureSession &session)
Establish a secure channel (SELECT → certificate → ECDH → mutual auth).
bool connect(CW_SecureSession &session)
Connect to the Cryptnox card and establish a secure channel.
bool printPN532FirmwareVersion()
bool validateSignRequest(const CW_SignRequest &request, CW_SignResult &result)
bool begin()
Initialize the NFC module via the underlying transport driver.
bool sendSignApdu(CW_SignRequest &request, const uint8_t *data, uint16_t dataLength, uint8_t *derResponse, uint16_t &derLength, CW_SignResult &result)
bool verifyPin(CW_SecureSession &session, const uint8_t *pin, uint8_t pinLength)
Verify the PIN code on the card.
void buildSignPayload(const CW_SignRequest &request, uint8_t *data, uint16_t &dataLength)
#define CW_CARD_EMAIL_MAX_LEN
Max email length stored on a Cryptnox card (per card spec).
#define CW_CARD_NAME_MAX_LEN
Max name length stored on a Cryptnox card (per card spec).
Arduino compatibility shims for non-Arduino (plain C++) builds.
Subset of the Cryptnox card info returned by APDU 0x80FA0000.
char name[CW_CARD_NAME_MAX_LEN+1U]
char email[CW_CARD_EMAIL_MAX_LEN+1U]
Holds cryptographic session state for reentrant secure channel operations.
Definition CW_Defs.h:168
Request parameters for CryptnoxWallet::sign.
uint8_t signatureType
CW_SignRequest(CW_SecureSession &sess, uint8_t kType=CW_SIGN_CURR_K1, uint8_t sigType=CW_SIGN_SIG_ECDSA_LOW_S, bool pinless=CW_SIGN_WITH_PIN)
Construct a sign request with sensible defaults.
CW_SecureSession & session
uint8_t derivePathLength
const uint8_t * hash
~CW_SignRequest()
Securely wipes the PIN buffer.
const uint8_t * derivePath
uint8_t pin[CW_MAX_PIN_LENGTH]
Result of CryptnoxWallet::sign.
CW_SignResult()
Construct a default-failure result.
uint8_t signature[CW_RAW_SIGNATURE_SIZE]