cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
uECC_esp32.cpp File Reference
#include "uECC.h"
#include "CW_Utils.h"
#include "mbedtls/ecp.h"
#include "mbedtls/ecdsa.h"
#include "esp_log.h"
#include <algorithm>
Include dependency graph for uECC_esp32.cpp:

Go to the source code of this file.

Classes

struct  uECC_Curve_t

Macros

#define COORD_SIZE_BYTES   (32U) /* bytes per coordinate (256-bit curve) */
#define ECC_XY_KEY_SIZE   (COORD_SIZE_BYTES * 2U) /* X[32] || Y[32] without 0x04 prefix */
#define UNCOMPRESSED_PUB_SIZE   (65U) /* 0x04 || X[32] || Y[32] */
#define UNCOMPRESSED_PREFIX   (0x04U)
#define POINT_PREFIX_OFFSET   (0U) /* byte offset of the 0x04 prefix */
#define COORD_X_OFFSET   (1U) /* byte offset of X in 65-byte key */
#define RAW_SIG_R_OFFSET   (0U) /* byte offset of r in 64-byte raw sig */
#define RAW_SIG_S_OFFSET   (32U) /* byte offset of s in 64-byte raw sig */
#define UECC_SUCCESS   (1)
#define UECC_FAILURE   (0)
#define MBEDTLS_OK   (0)
#define RNG_ERROR   (-1) /* returned by RNG callback on invalid arguments */

Functions

static int esp32_mbedtls_rng (void *ctx, unsigned char *output, size_t len)
const uECC_Curve_tuECC_secp256r1 (void)
 Return the static secp256r1 curve descriptor.
const uECC_Curve_tuECC_secp256k1 (void)
 Return the static secp256k1 curve descriptor.
void uECC_set_rng (uECC_RNG_Function rng_function)
 No-op: ESP32 hardware RNG is used internally; no external callback needed.
int uECC_make_key (uint8_t *public_key, uint8_t *private_key, const uECC_Curve_t *curve)
 Generate an ECC key pair using mbedTLS and the ESP32 hardware RNG.
int uECC_shared_secret (const uint8_t *public_key, const uint8_t *private_key, uint8_t *secret, const uECC_Curve_t *curve)
 Compute ECDH shared secret (X-coordinate of privKey * pubKey).
int uECC_verify (const uint8_t *public_key, const uint8_t *hash, unsigned hash_size, const uint8_t *signature, const uECC_Curve_t *curve)
 Verify an ECDSA signature (raw 64-byte r||s) against a hash.

Variables

static const char *const UECC_LOG_TAG = "uECC_esp32"
static const uECC_Curve_t s_secp256r1 = { MBEDTLS_ECP_DP_SECP256R1 }
static const uECC_Curve_t s_secp256k1 = { MBEDTLS_ECP_DP_SECP256K1 }

Macro Definition Documentation

◆ COORD_SIZE_BYTES

#define COORD_SIZE_BYTES   (32U) /* bytes per coordinate (256-bit curve) */

Definition at line 17 of file uECC_esp32.cpp.

Referenced by uECC_make_key(), uECC_shared_secret(), and uECC_verify().

◆ COORD_X_OFFSET

#define COORD_X_OFFSET   (1U) /* byte offset of X in 65-byte key */

Definition at line 23 of file uECC_esp32.cpp.

Referenced by uECC_make_key(), uECC_shared_secret(), and uECC_verify().

◆ ECC_XY_KEY_SIZE

#define ECC_XY_KEY_SIZE   (COORD_SIZE_BYTES * 2U) /* X[32] || Y[32] without 0x04 prefix */

Definition at line 19 of file uECC_esp32.cpp.

Referenced by uECC_make_key(), uECC_shared_secret(), and uECC_verify().

◆ MBEDTLS_OK

#define MBEDTLS_OK   (0)

Definition at line 28 of file uECC_esp32.cpp.

◆ POINT_PREFIX_OFFSET

#define POINT_PREFIX_OFFSET   (0U) /* byte offset of the 0x04 prefix */

Definition at line 22 of file uECC_esp32.cpp.

Referenced by uECC_shared_secret(), and uECC_verify().

◆ RAW_SIG_R_OFFSET

#define RAW_SIG_R_OFFSET   (0U) /* byte offset of r in 64-byte raw sig */

Definition at line 24 of file uECC_esp32.cpp.

Referenced by uECC_verify().

◆ RAW_SIG_S_OFFSET

#define RAW_SIG_S_OFFSET   (32U) /* byte offset of s in 64-byte raw sig */

Definition at line 25 of file uECC_esp32.cpp.

Referenced by uECC_verify().

◆ RNG_ERROR

#define RNG_ERROR   (-1) /* returned by RNG callback on invalid arguments */

Definition at line 29 of file uECC_esp32.cpp.

Referenced by esp32_mbedtls_rng().

◆ UECC_FAILURE

#define UECC_FAILURE   (0)

Definition at line 27 of file uECC_esp32.cpp.

Referenced by uECC_make_key(), uECC_shared_secret(), and uECC_verify().

◆ UECC_SUCCESS

#define UECC_SUCCESS   (1)

Definition at line 26 of file uECC_esp32.cpp.

◆ UNCOMPRESSED_PREFIX

#define UNCOMPRESSED_PREFIX   (0x04U)

Definition at line 21 of file uECC_esp32.cpp.

Referenced by uECC_shared_secret(), and uECC_verify().

◆ UNCOMPRESSED_PUB_SIZE

#define UNCOMPRESSED_PUB_SIZE   (65U) /* 0x04 || X[32] || Y[32] */

Definition at line 20 of file uECC_esp32.cpp.

Referenced by uECC_make_key(), uECC_shared_secret(), and uECC_verify().

Function Documentation

◆ esp32_mbedtls_rng()

int esp32_mbedtls_rng ( void * ctx,
unsigned char * output,
size_t len )
static

Definition at line 50 of file uECC_esp32.cpp.

References MBEDTLS_OK, and RNG_ERROR.

Referenced by uECC_make_key(), and uECC_shared_secret().

◆ uECC_make_key()

int uECC_make_key ( uint8_t * public_key,
uint8_t * private_key,
const uECC_Curve_t * curve )

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

Definition at line 92 of file uECC_esp32.cpp.

References COORD_SIZE_BYTES, COORD_X_OFFSET, ECC_XY_KEY_SIZE, esp32_mbedtls_rng(), uECC_Curve_t::grp_id, MBEDTLS_OK, UECC_FAILURE, UECC_SUCCESS, and UNCOMPRESSED_PUB_SIZE.

Referenced by ESP32CryptoProvider::makeKey().

◆ uECC_secp256k1()

const uECC_Curve_t * uECC_secp256k1 ( void )

Return the static secp256k1 curve descriptor.

Definition at line 75 of file uECC_esp32.cpp.

References s_secp256k1.

Referenced by toCurve().

◆ uECC_secp256r1()

const uECC_Curve_t * uECC_secp256r1 ( void )

Return the static secp256r1 curve descriptor.

Definition at line 70 of file uECC_esp32.cpp.

References s_secp256r1.

Referenced by toCurve().

◆ uECC_set_rng()

void uECC_set_rng ( uECC_RNG_Function rng_function)

No-op: ESP32 hardware RNG is used internally; no external callback needed.

Definition at line 80 of file uECC_esp32.cpp.

References UECC_LOG_TAG.

◆ uECC_shared_secret()

int uECC_shared_secret ( const uint8_t * public_key,
const uint8_t * private_key,
uint8_t * secret,
const uECC_Curve_t * curve )

◆ uECC_verify()

int uECC_verify ( const uint8_t * public_key,
const uint8_t * hash,
unsigned hash_size,
const uint8_t * signature,
const uECC_Curve_t * curve )

Variable Documentation

◆ s_secp256k1

const uECC_Curve_t s_secp256k1 = { MBEDTLS_ECP_DP_SECP256K1 }
static

Definition at line 44 of file uECC_esp32.cpp.

Referenced by uECC_secp256k1().

◆ s_secp256r1

const uECC_Curve_t s_secp256r1 = { MBEDTLS_ECP_DP_SECP256R1 }
static

Definition at line 43 of file uECC_esp32.cpp.

Referenced by uECC_secp256r1().

◆ UECC_LOG_TAG

const char* const UECC_LOG_TAG = "uECC_esp32"
static

Definition at line 18 of file uECC_esp32.cpp.

Referenced by uECC_set_rng().