SolarEnergy/esp32_device/main/crypto.c

51 lines
1.3 KiB
C
Raw Normal View History

2024-07-27 14:15:35 +00:00
#include "crypto.h"
#include "system.h"
#include <mbedtls/build_info.h>
#include <mbedtls/platform.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/ecdsa.h>
#include <mbedtls/sha256.h>
#include <mbedtls/pk.h>
/*
* Uncomment to force use of a specific curve
*/
#define ECPARAMS MBEDTLS_ECP_DP_SECP256R1
bool crypto_gen_priv_key()
{
int ret = 1;
const char *pers = "ecdsa";
mbedtls_entropy_context entropy;
mbedtls_entropy_init(&entropy);
mbedtls_ecdsa_context ctx_sign;
mbedtls_ecdsa_init(&ctx_sign);
mbedtls_ctr_drbg_context ctr_drbg;
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *)pers,
strlen(pers))) != 0)
{
mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
reboot();
}
// Generate private key
if ((ret = mbedtls_ecdsa_genkey(&ctx_sign, ECPARAMS,
mbedtls_ctr_drbg_random, &ctr_drbg)) != 0)
{
mbedtls_printf(" failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret);
reboot();
}
mbedtls_pk_context key_ctx;
mbedtls_ecp_gen_keypair
mbedtls_pk_write_key_pem()
}