cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
ESP32CryptoProvider Class Reference

CW_CryptoProvider backed by mbedTLS and the ESP32 hardware TRNG. More...

#include <esp32_crypto_provider.h>

Inheritance diagram for ESP32CryptoProvider:
[legend]
Collaboration diagram for ESP32CryptoProvider:
[legend]

Public Member Functions

bool sha256 (const uint8_t *data, size_t len, uint8_t *out) override
 Compute SHA-256 over a contiguous buffer.
bool sha512 (const uint8_t *data, size_t len, uint8_t *out) override
 Compute SHA-512 over a contiguous buffer.
uint16_t aesCbcEncrypt (const uint8_t *in, uint16_t len, uint8_t *out, const uint8_t *key, uint8_t keyLen, uint8_t *iv, bool bitPadding) override
 Encrypt a buffer with AES-CBC.
uint16_t aesCbcDecrypt (uint8_t *in, uint16_t len, uint8_t *out, const uint8_t *key, uint8_t keyLen, uint8_t *iv, bool bitPadding) override
 Decrypt a buffer with AES-CBC.
bool ecdh (const uint8_t *pubKey, const uint8_t *privKey, uint8_t *secret, CW_Curve curve) override
 Compute an ECDH shared secret.
bool makeKey (uint8_t *pubKey, uint8_t *privKey, CW_Curve curve) override
 Generate an ephemeral EC key pair.
bool random (uint8_t *dest, unsigned size) override
 Fill a buffer with cryptographically random bytes.
bool ecdsaVerify (const uint8_t *pubKey64, const uint8_t *hash, size_t hashLen, const uint8_t *sig, CW_Curve curve) override
 Verify an ECDSA signature.
 ~ESP32CryptoProvider () override
 Default destructor.

Detailed Description

CW_CryptoProvider backed by mbedTLS and the ESP32 hardware TRNG.

All operations are stateless; a single instance may be shared across multiple CryptnoxWallet objects (though concurrent use from different RTOS tasks is not thread-safe without external locking).

Warning
See the random method for TRNG entropy requirements (SEC-001).
Example
CW_CryptoProvider &provider = crypto;
uint8_t pub[64], priv[32];
provider.makeKey(pub, priv, CW_CURVE_SECP256R1); // generate ephemeral key pair
uint8_t digest[32];
provider.sha256(message, messageLen, digest); // hash a message
CW_CryptoProvider backed by mbedTLS and the ESP32 hardware TRNG.
See also
CW_CryptoProvider
CryptnoxWallet
Examples
BasicUsage/main/main.cpp, Connect/main/main.cpp, Sign/main/main.cpp, UsdcSigning/main/main.cpp, and VerifyPin/main/main.cpp.

Definition at line 62 of file esp32_crypto_provider.h.

Constructor & Destructor Documentation

◆ ~ESP32CryptoProvider()

ESP32CryptoProvider::~ESP32CryptoProvider ( )
inlineoverride

Default destructor.

Definition at line 213 of file esp32_crypto_provider.h.

Member Function Documentation

◆ aesCbcDecrypt()

uint16_t ESP32CryptoProvider::aesCbcDecrypt ( uint8_t * in,
uint16_t len,
uint8_t * out,
const uint8_t * key,
uint8_t keyLen,
uint8_t * iv,
bool bitPadding )
override

Decrypt a buffer with AES-CBC.

AES-CBC decrypt with optional ISO/IEC 9797-1 Method 2 bit-padding removal.

Uses mbedTLS AES-CBC and optionally strips ISO/IEC 7816-4 bit-padding after decryption. The IV is updated in-place.

Parameters
[in,out]inCiphertext input buffer; may be modified in-place (must not be NULL).
[in]lenLength of in in bytes (must be a multiple of 16).
[out]outPlaintext output buffer (must not be NULL).
[in]keyAES key bytes (must not be NULL).
[in]keyLenKey length in bytes (16 / 24 / 32).
[in,out]iv16-byte IV; updated in-place after the call.
[in]bitPaddingWhen true, strips ISO/IEC 7816-4 bit padding from the decrypted output.
Returns
Number of plaintext bytes written to out, or 0 on failure.

Definition at line 147 of file esp32_crypto_provider.cpp.

References AES_BLOCK_SIZE_BYTES, AES_KEY_BITS_PER_BYTE, BIT_PADDING_MARKER, MBEDTLS_OK, and PADDING_ZERO_FILL.

◆ aesCbcEncrypt()

uint16_t ESP32CryptoProvider::aesCbcEncrypt ( const uint8_t * in,
uint16_t len,
uint8_t * out,
const uint8_t * key,
uint8_t keyLen,
uint8_t * iv,
bool bitPadding )
override

Encrypt a buffer with AES-CBC.

AES-CBC encrypt with optional ISO/IEC 9797-1 Method 2 bit padding.

Uses mbedTLS AES-CBC with optional ISO/IEC 7816-4 bit-padding. The IV is updated in-place after each block so that the caller can chain calls for streaming encryption.

Parameters
[in]inPlaintext input buffer (must not be NULL).
[in]lenLength of in in bytes.
[out]outCiphertext output buffer; must be at least len + 16 bytes to accommodate padding.
[in]keyAES key bytes (must not be NULL).
[in]keyLenKey length in bytes (16 for AES-128, 24 for AES-192, 32 for AES-256).
[in,out]iv16-byte IV; updated in-place after the call.
[in]bitPaddingWhen true, applies ISO/IEC 7816-4 bit padding before encrypting.
Returns
Number of ciphertext bytes written to out, or 0 on failure.

Definition at line 89 of file esp32_crypto_provider.cpp.

References AES_BLOCK_SIZE_BYTES, AES_KEY_BITS_PER_BYTE, AES_PAD_BUF_SIZE, BIT_PADDING_MARKER, MBEDTLS_OK, and PADDING_ZERO_FILL.

◆ ecdh()

bool ESP32CryptoProvider::ecdh ( const uint8_t * pubKey,
const uint8_t * privKey,
uint8_t * secret,
CW_Curve curve )
override

Compute an ECDH shared secret.

Compute ECDH shared secret: X-coordinate of privKey * pubKey point.

Performs the standard Diffie-Hellman point multiplication secret = privKey × pubKey on the specified curve via the uECC shim.

Parameters
[in]pubKeyUncompressed peer public key (64 bytes, X||Y; no 0x04 prefix; must not be NULL).
[in]privKey32-byte private scalar (must not be NULL).
[out]secret32-byte shared-secret output buffer (must not be NULL).
[in]curveElliptic curve selector (CW_CURVE_SECP256R1 or CW_CURVE_SECP256K1).
Returns
true on success, false if point multiplication fails or a pointer argument is NULL.

Definition at line 211 of file esp32_crypto_provider.cpp.

References toCurve(), uECC_shared_secret(), and UECC_SUCCESS.

◆ ecdsaVerify()

bool ESP32CryptoProvider::ecdsaVerify ( const uint8_t * pubKey64,
const uint8_t * hash,
size_t hashLen,
const uint8_t * sig,
CW_Curve curve )
override

Verify an ECDSA signature.

Verify an ECDSA signature (raw r||s) against a hash on the specified curve.

Checks that sig is a valid low-S DER-encoded ECDSA signature over hash produced by the private key corresponding to pubKey64.

Parameters
[in]pubKey6464-byte uncompressed public key (X||Y, no 0x04 prefix; must not be NULL).
[in]hashMessage digest to verify against (must not be NULL).
[in]hashLenLength of hash in bytes.
[in]sigDER-encoded signature bytes (must not be NULL).
[in]curveElliptic curve selector (CW_CURVE_SECP256R1 or CW_CURVE_SECP256K1).
Returns
true if the signature is valid, false otherwise (including on NULL arguments or malformed DER).

Definition at line 250 of file esp32_crypto_provider.cpp.

References toCurve(), UECC_SUCCESS, and uECC_verify().

◆ makeKey()

bool ESP32CryptoProvider::makeKey ( uint8_t * pubKey,
uint8_t * privKey,
CW_Curve curve )
override

Generate an ephemeral EC key pair.

Generate an ECC key pair via mbedTLS and the ESP32 hardware RNG.

Uses the hardware TRNG (via random) as the entropy source for the private scalar.

Parameters
[out]pubKey64-byte uncompressed public key output (X||Y, no 0x04 prefix; must not be NULL).
[out]privKey32-byte private key output (must not be NULL).
[in]curveElliptic curve selector (CW_CURVE_SECP256R1 or CW_CURVE_SECP256K1).
Returns
true on success, false on RNG or key-generation failure.
Warning
Ensure Wi-Fi or BT is active before calling this to guarantee full TRNG entropy (SEC-001).

Definition at line 223 of file esp32_crypto_provider.cpp.

References toCurve(), uECC_make_key(), and UECC_SUCCESS.

◆ random()

bool ESP32CryptoProvider::random ( uint8_t * dest,
unsigned size )
override

Fill a buffer with cryptographically random bytes.

Fill dest with size cryptographically random bytes from the ESP32 hardware RNG.

Calls esp_fill_random() which reads from the ESP32 hardware TRNG. Full entropy requires Wi-Fi or Bluetooth to be active; without a live radio the TRNG operates in reduced-entropy mode (thermal noise and ring-oscillator jitter only — see SEC-001).

Parameters
[out]destBuffer to fill (must not be NULL).
[in]sizeNumber of random bytes to generate.
Returns
true on success, false if dest is NULL or size is 0.
Warning
In production firmware, call this only after Wi-Fi or BT has been started to ensure full entropy.

Definition at line 239 of file esp32_crypto_provider.cpp.

◆ sha256()

bool ESP32CryptoProvider::sha256 ( const uint8_t * data,
size_t len,
uint8_t * out )
override

Compute SHA-256 over a contiguous buffer.

Compute SHA-256 over the input buffer, writing 32 bytes to out.

Uses the mbedTLS mbedtls_sha256 API, which is hardware-accelerated on ESP32-S3.

Parameters
[in]dataPointer to the input data (must not be NULL).
[in]lenLength of data in bytes.
[out]out32-byte output buffer for the digest (must not be NULL).
Returns
true on success, false if data or out is NULL or if the mbedTLS call fails.

Definition at line 73 of file esp32_crypto_provider.cpp.

References MBEDTLS_OK, and MBEDTLS_SHA256_MODE.

◆ sha512()

bool ESP32CryptoProvider::sha512 ( const uint8_t * data,
size_t len,
uint8_t * out )
override

Compute SHA-512 over a contiguous buffer.

Compute SHA-512 over the input buffer, writing 64 bytes to out.

Uses the mbedTLS mbedtls_sha512 API, which is hardware-accelerated on ESP32-S3.

Parameters
[in]dataPointer to the input data (must not be NULL).
[in]lenLength of data in bytes.
[out]out64-byte output buffer for the digest (must not be NULL).
Returns
true on success, false if data or out is NULL or if the mbedTLS call fails.

Definition at line 79 of file esp32_crypto_provider.cpp.

References MBEDTLS_OK, and MBEDTLS_SHA512_MODE.


The documentation for this class was generated from the following files: