8#include "mbedtls/ecp.h"
9#include "mbedtls/ecdsa.h"
17#define COORD_SIZE_BYTES (32U)
19#define ECC_XY_KEY_SIZE (COORD_SIZE_BYTES * 2U)
20#define UNCOMPRESSED_PUB_SIZE (65U)
21#define UNCOMPRESSED_PREFIX (0x04U)
22#define POINT_PREFIX_OFFSET (0U)
23#define COORD_X_OFFSET (1U)
24#define RAW_SIG_R_OFFSET (0U)
25#define RAW_SIG_S_OFFSET (32U)
26#define UECC_SUCCESS (1)
27#define UECC_FAILURE (0)
54 if ((output != NULL) && (len > 0U)) {
55 bool rng_result = CW_Utils::fill_secure_random(
56 reinterpret_cast<uint8_t *
>(output), len);
81 if (rng_function != NULL) {
86 "uECC_set_rng: callback ignored — ESP32 uses hardware RNG internally");
96 if ((public_key != NULL) && (private_key != NULL) && (curve != NULL)) {
97 mbedtls_ecp_group grp = {};
99 mbedtls_ecp_point Q = {};
101 mbedtls_ecp_group_init(&grp);
102 mbedtls_mpi_init(&d);
103 mbedtls_ecp_point_init(&Q);
105 int ret = mbedtls_ecp_group_load(&grp, curve->
grp_id);
108 ret = mbedtls_ecp_gen_keypair(&grp, &d, &Q,
113 ret = mbedtls_mpi_write_binary(&d, private_key,
120 ret = mbedtls_ecp_point_write_binary(&grp, &Q,
121 MBEDTLS_ECP_PF_UNCOMPRESSED,
123 pub65,
sizeof(pub65));
135 mbedtls_ecp_point_free(&Q);
136 mbedtls_mpi_free(&d);
137 mbedtls_ecp_group_free(&grp);
148 if ((public_key != NULL) && (private_key != NULL) &&
149 (secret != NULL) && (curve != NULL)) {
150 mbedtls_ecp_group grp = {};
151 mbedtls_ecp_point remote_Q = {};
152 mbedtls_mpi local_d = {};
153 mbedtls_ecp_point shared_R = {};
155 mbedtls_ecp_group_init(&grp);
156 mbedtls_ecp_point_init(&remote_Q);
157 mbedtls_mpi_init(&local_d);
158 mbedtls_ecp_point_init(&shared_R);
160 int ret = mbedtls_ecp_group_load(&grp, curve->
grp_id);
165 (void)std::copy_n(public_key,
168 ret = mbedtls_ecp_point_read_binary(&grp, &remote_Q,
169 pub65,
sizeof(pub65));
173 ret = mbedtls_mpi_read_binary(&local_d, private_key,
178 ret = mbedtls_ecp_mul(&grp, &shared_R, &local_d, &remote_Q,
185 ret = mbedtls_ecp_point_write_binary(&grp, &shared_R,
186 MBEDTLS_ECP_PF_UNCOMPRESSED,
188 shared65,
sizeof(shared65));
200 mbedtls_ecp_point_free(&shared_R);
201 mbedtls_mpi_free(&local_d);
202 mbedtls_ecp_point_free(&remote_Q);
203 mbedtls_ecp_group_free(&grp);
210int uECC_verify(
const uint8_t *public_key,
const uint8_t *hash,
unsigned hash_size,
214 if ((public_key != NULL) && (hash != NULL) &&
215 (signature != NULL) && (curve != NULL)) {
216 mbedtls_ecp_group grp = {};
217 mbedtls_ecp_point Q = {};
221 mbedtls_ecp_group_init(&grp);
222 mbedtls_ecp_point_init(&Q);
223 mbedtls_mpi_init(&r);
224 mbedtls_mpi_init(&s);
226 int ret = mbedtls_ecp_group_load(&grp, curve->
grp_id);
231 (void)std::copy_n(public_key,
234 ret = mbedtls_ecp_point_read_binary(&grp, &Q,
235 pub65,
sizeof(pub65));
239 ret = mbedtls_mpi_read_binary(&r,
245 ret = mbedtls_mpi_read_binary(&s,
251 ret = mbedtls_ecdsa_verify(&grp, hash,
252 static_cast<size_t>(hash_size),
260 mbedtls_mpi_free(&s);
261 mbedtls_mpi_free(&r);
262 mbedtls_ecp_point_free(&Q);
263 mbedtls_ecp_group_free(&grp);
mbedtls_ecp_group_id grp_id
int(* uECC_RNG_Function)(uint8_t *dest, unsigned size)
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).
#define UNCOMPRESSED_PREFIX
const uECC_Curve_t * uECC_secp256k1(void)
Return the static secp256k1 curve descriptor.
const uECC_Curve_t * uECC_secp256r1(void)
Return the static secp256r1 curve descriptor.
#define POINT_PREFIX_OFFSET
static int esp32_mbedtls_rng(void *ctx, unsigned char *output, size_t len)
void uECC_set_rng(uECC_RNG_Function rng_function)
No-op: ESP32 hardware RNG is used internally; no external callback needed.
static const char *const UECC_LOG_TAG
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_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.
#define UNCOMPRESSED_PUB_SIZE
static const uECC_Curve_t s_secp256r1
static const uECC_Curve_t s_secp256k1