cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
CW_SecureChannel.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 CW_SECURECHANNEL_H
24#define CW_SECURECHANNEL_H
25
26/******************************************************************
27 * 1. Public constants
28 ******************************************************************/
29
30#define CW_PAIRING_DATA "Cryptnox Basic CommonPairingData"
31#define CW_PAIRING_DATA_BYTES (sizeof(CW_PAIRING_DATA) - 1U)
32
33/******************************************************************
34 * 2. Included files
35 ******************************************************************/
36
37#include "platform_compat.h"
38#include "CW_NfcTransport.h"
39#include "CW_Logger.h"
40#include "CW_CryptoProvider.h"
41#include "CW_Platform.h"
42#include "CW_Defs.h" /* for CW_SecureSession, CW_Curve, and constants */
43
44/******************************************************************
45 * 2. Class declaration
46 ******************************************************************/
47
65public:
76
79
84 bool begin();
85
91
95 void resetReader();
96
102
107 bool selectApdu();
108
120 bool getCardCertificate(uint8_t* cardCertificate, uint8_t& cardCertificateLength);
121
130 bool extractCardEphemeralKey(const uint8_t* cardCertificate,
131 uint8_t* cardEphemeralPubKey,
132 uint8_t* fullEphemeralPubKey65 = NULL);
133
151 bool openSecureChannel(uint8_t* salt,
152 uint8_t* clientPublicKey,
153 uint8_t* clientPrivateKey,
154 CW_Curve sessionCurve);
155
184 const uint8_t* salt,
185 uint8_t* clientPublicKey,
186 const uint8_t* clientPrivateKey,
187 CW_Curve sessionCurve,
188 const uint8_t* cardEphemeralPubKey);
189
197 bool getManufacturerCertificate(uint8_t* cert, uint16_t& certLen);
198
210
238 uint8_t verifyCertificateChain(const uint8_t* cardCert, uint8_t cardCertLen);
239
269 bool aesCbcEncrypt(CW_SecureSession& session,
270 const uint8_t apdu[], uint16_t apduLength,
271 const uint8_t data[], uint16_t dataLength,
272 uint8_t* decryptedOutput = NULL,
273 uint16_t* decryptedOutputLength = NULL);
274
294 bool aesCbcDecrypt(const CW_SecureSession& session,
295 uint8_t* response, size_t responseLen,
296 uint8_t* macValue,
297 uint8_t* decryptedOutput = NULL,
298 uint16_t* decryptedOutputLength = NULL);
299
309 bool checkStatusWord(const uint8_t* response, uint16_t responseLength,
310 uint8_t sw1Expected, uint8_t sw2Expected);
311
312private:
317
320
323
324 static bool parseDerSigToRaw(const uint8_t* der, uint8_t derLen,
325 uint8_t* raw64);
326
327 bool verifyEcdsaSha256(const uint8_t* pubKey64,
328 const uint8_t* message, uint16_t msgLen,
329 const uint8_t* derSig, uint8_t derSigLen);
330
331#ifdef CW_FUZZ_BUILD
332 friend struct DerFuzzTarget;
333#endif
334};
335
336#endif // CW_SECURECHANNEL_H
ArduinoPlatform platform
Abstract cryptographic primitives interface.
Shared constants, error codes, and session state for the SDK.
#define CW_CERT_NONCE_SIZE
Definition CW_Defs.h:124
Abstract logging interface.
Abstract NFC transport interface.
Abstract platform interface for timing primitives.
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
CW_SecureChannel(CW_NfcTransport &driver, CW_Logger &logger, CW_CryptoProvider &crypto, CW_Platform &platform)
Construct a CW_SecureChannel.
CW_SecureChannel & operator=(const CW_SecureChannel &)=delete
static bool parseDerSigToRaw(const uint8_t *der, uint8_t derLen, uint8_t *raw64)
CW_Logger & _logger
Logging interface.
bool openSecureChannel(uint8_t *salt, uint8_t *clientPublicKey, uint8_t *clientPrivateKey, CW_Curve sessionCurve)
Send OPEN SECURE CHANNEL and retrieve the session salt.
CW_NfcTransport & _driver
NFC transport for APDU exchange.
bool mutuallyAuthenticate(CW_SecureSession &session, const uint8_t *salt, uint8_t *clientPublicKey, const uint8_t *clientPrivateKey, CW_Curve sessionCurve, const uint8_t *cardEphemeralPubKey)
Perform ECDH derivation and MUTUALLY AUTHENTICATE with the card.
bool aesCbcEncrypt(CW_SecureSession &session, const uint8_t apdu[], uint16_t apduLength, const uint8_t data[], uint16_t dataLength, uint8_t *decryptedOutput=NULL, uint16_t *decryptedOutputLength=NULL)
AES-CBC encrypt + MAC, send APDU, and decrypt response.
bool inListPassiveTarget()
Detect a passive NFC target (ISO-DEP card).
bool aesCbcDecrypt(const CW_SecureSession &session, uint8_t *response, size_t responseLen, uint8_t *macValue, uint8_t *decryptedOutput=NULL, uint16_t *decryptedOutputLength=NULL)
Verify MAC and decrypt an encrypted APDU response.
bool begin()
Initialize the NFC transport module.
bool verifyEcdsaSha256(const uint8_t *pubKey64, const uint8_t *message, uint16_t msgLen, const uint8_t *derSig, uint8_t derSigLen)
bool checkStatusWord(const uint8_t *response, uint16_t responseLength, uint8_t sw1Expected, uint8_t sw2Expected)
Verify the SW1/SW2 status word at the end of an APDU response.
bool preFetchManufacturerCert()
Fetch and cache the manufacturer certificate before getCardCertificate().
bool getCardCertificate(uint8_t *cardCertificate, uint8_t &cardCertificateLength)
Retrieve the card's ephemeral public key via GET CARD CERTIFICATE.
CW_SecureChannel(const CW_SecureChannel &)=delete
uint16_t _cachedMfCertLen
Non-zero when s_mfCertBuf holds a valid pre-fetched manufacturer certificate.
bool selectApdu()
Send the SELECT APDU to activate the Cryptnox application.
CW_Platform & _platform
Platform abstraction (sleep_ms).
uint8_t verifyCertificateChain(const uint8_t *cardCert, uint8_t cardCertLen)
Verify the full card certificate chain against the trusted CA.
void resetReader()
Reset the NFC reader hardware.
uint8_t _lastNonce[CW_CERT_NONCE_SIZE]
Nonce sent in the last getCardCertificate() call; checked in verifyCertificateChain().
bool extractCardEphemeralKey(const uint8_t *cardCertificate, uint8_t *cardEphemeralPubKey, uint8_t *fullEphemeralPubKey65=NULL)
Extract the card's ephemeral EC P-256 public key from a certificate.
CW_CryptoProvider & _crypto
Crypto operations (AES, SHA, ECDH, RNG).
bool getManufacturerCertificate(uint8_t *cert, uint16_t &certLen)
Retrieve the manufacturer certificate stored in card flash.
bool printFirmwareVersion()
Print the NFC reader firmware version to the logger.
CW_Curve
Portable curve identifier used throughout the SDK.
Definition CW_Defs.h:151
Arduino compatibility shims for non-Arduino (plain C++) builds.
Holds cryptographic session state for reentrant secure channel operations.
Definition CW_Defs.h:168