This commit is contained in:
Pierre HUBERT 2024-07-27 16:15:35 +02:00
parent 900b436856
commit 0c6c0f4a7f
9 changed files with 120 additions and 12 deletions

View File

@ -12,6 +12,14 @@
"stdio.h": "c",
"storage.h": "c",
"cstdlib": "c",
"cstring": "c"
"cstring": "c",
"crypto.h": "c",
"entropy.h": "c",
"ctr_drbg.h": "c",
"sha256.h": "c",
"ecdsa.h": "c",
"platform.h": "c",
"build_info.h": "c",
"config_adjust_ssl.h": "c"
}
}

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "random.c" "storage.c" "main.c"
idf_component_register(SRCS "system.c" "crypto.c" "random.c" "storage.c" "main.c"
"dev_name.c"
INCLUDE_DIRS ".")

View File

@ -0,0 +1,51 @@
#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()
}

View File

@ -0,0 +1,21 @@
/**
* Cryptographic functions
*/
#pragma once
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* Generate device private key, if required
*/
bool crypto_gen_priv_key();
#ifdef __cplusplus
}
#endif

View File

@ -8,7 +8,7 @@
#define DEV_PREFIX "ESP32 "
bool dev_generate_name_if_required()
bool dev_generate_name()
{
// Check if a device name has already been defined
if (storage_get_dev_name(NULL) > 0)

View File

@ -14,12 +14,7 @@ extern "C"
/**
* Generate random device name, if not existent
*/
bool dev_generate_name_if_required();
/**
* Clear device name
*/
void dev_remove_name();
bool dev_generate_name();
/**
* Get current device name. This value MUST be freed after usage

View File

@ -3,6 +3,7 @@
#include "dev_name.h"
#include "storage.h"
#include "system.h"
void app_main(void)
{
@ -11,15 +12,19 @@ void app_main(void)
if (storage_init() == false)
{
printf("Failed to init storage!\n");
fflush(stdout);
esp_restart();
reboot();
}
if (dev_generate_name_if_required())
if (dev_generate_name())
{
printf("Generated a new device name\n");
}
if (crypto_gen_priv_key())
{
printf("Generated device private key!\n");
}
char *name = dev_name();
printf("Dev name: %s\n", name);
free(name);

View File

@ -0,0 +1,9 @@
#include "system.h"
#include "esp_system.h"
void reboot()
{
fflush(stdout);
esp_restart();
}

View File

@ -0,0 +1,19 @@
/**
* System functions
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
/**
* Reboot ESP32
*/
void reboot();
#ifdef __cplusplus
}
#endif