18#include "mbedtls/sha256.h"
19#include "mbedtls/sha512.h"
20#include "mbedtls/aes.h"
29 case CW_CURVE_SECP256R1:
32 case CW_CURVE_SECP256K1:
47#define AES_BLOCK_SIZE_BYTES (16U)
48#define AES_KEY_BITS_PER_BYTE (8U)
50#define AES_PAD_BUF_MAX_INPUT (256U)
52#define AES_PAD_BUF_SIZE (AES_PAD_BUF_MAX_INPUT + AES_BLOCK_SIZE_BYTES)
55#define BIT_PADDING_MARKER (0x80U)
56#define PADDING_ZERO_FILL (0x00U)
59#define MBEDTLS_SHA256_MODE (0)
60#define MBEDTLS_SHA512_MODE (0)
66#define UECC_SUCCESS (1)
90 const uint8_t* key, uint8_t keyLen,
91 uint8_t* iv,
bool bitPadding) {
94 if ((in != NULL) && (out != NULL) && (key != NULL) && (iv != NULL)) {
101 uint16_t paddedLen =
static_cast<uint16_t
>(paddedLen32);
104 (void)CW_Utils::safe_memcpy(padBuf,
sizeof(padBuf), in,
static_cast<size_t>(len));
111 (void)CW_Utils::safe_memcpy(padBuf,
sizeof(padBuf), in,
static_cast<size_t>(len));
117 mbedtls_aes_context ctx = {};
118 mbedtls_aes_init(&ctx);
120 unsigned int keyBits =
static_cast<unsigned int>(
123 int ret = mbedtls_aes_setkey_enc(&ctx, key, keyBits);
126 ret = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT,
127 static_cast<size_t>(encLen),
131 mbedtls_aes_free(&ctx);
148 const uint8_t* key, uint8_t keyLen,
149 uint8_t* iv,
bool bitPadding) {
150 uint16_t result = 0U;
152 if ((in != NULL) && (out != NULL) && (key != NULL) && (iv != NULL) && (len > 0U)) {
156 mbedtls_aes_context ctx = {};
157 mbedtls_aes_init(&ctx);
159 unsigned int keyBits =
static_cast<unsigned int>(
162 int ret = mbedtls_aes_setkey_dec(&ctx, key, keyBits);
165 ret = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT,
166 static_cast<size_t>(len),
170 mbedtls_aes_free(&ctx);
177 uint16_t padPos = len;
179 bool searching =
true;
181 while ((padPos !=
static_cast<uint16_t
>(0U)) && searching) {
212 uint8_t* secret, CW_Curve curve) {
215 if (ueccCurve != NULL) {
227 if (ueccCurve != NULL) {
242 if ((dest != NULL) && (size > 0U)) {
243 result = CW_Utils::fill_secure_random(dest,
static_cast<size_t>(size));
251 size_t hashLen,
const uint8_t* sig,
255 if (ueccCurve != NULL) {
256 int ret =
uECC_verify(pubKey64, hash,
static_cast<unsigned>(hashLen),
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.
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 sha512(const uint8_t *data, size_t len, uint8_t *out) override
Compute SHA-512 over a contiguous buffer.
bool sha256(const uint8_t *data, size_t len, uint8_t *out) override
Compute SHA-256 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.
#define AES_BLOCK_SIZE_BYTES
#define MBEDTLS_SHA256_MODE
#define BIT_PADDING_MARKER
#define PADDING_ZERO_FILL
static const uECC_Curve_t * toCurve(CW_Curve curve)
#define MBEDTLS_SHA512_MODE
#define AES_KEY_BITS_PER_BYTE
CW_CryptoProvider implementation for ESP32 using mbedTLS and the hardware TRNG.
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).
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.
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.