4#include <mbedtls/sha256.h>
9static const char*
TAG =
"AttestKey";
42 if (!
init())
return false;
66 lastAttemptMs_ = nowMs;
79bool AttestationKeyService::loadStoredHash(uint8_t* out,
size_t outLen) {
80 if (!out || outLen == 0)
return false;
82 if (!nvs)
return false;
85 return err == ESP_OK && len == outLen;
94bool AttestationKeyService::saveStoredHash(
const uint8_t* data,
size_t len) {
95 if (!data || len == 0)
return false;
97 if (!nvs)
return false;
99 if (err == ESP_OK) err = nvs.commit();
100 return err == ESP_OK;
107bool AttestationKeyService::ensureKey() {
108 if (!secureElement_) {
109 LOG_W(
TAG,
"Secure element not set");
112 if (!secureElement_->isSessionActive()) {
113 if (!secureElement_->sessionStart()) {
114 LOG_W(
TAG,
"Secure element session not active");
124 LOG_I(
TAG,
"Attestation slot empty, generating key");
127 LOG_E(
TAG,
"Failed to generate attestation key");
134 LOG_W(
TAG,
"Attestation key read failed: %d",
static_cast<int>(res));
139 LOG_W(
TAG,
"Attestation key wrong curve, regenerating");
143 LOG_E(
TAG,
"Failed to regenerate attestation key");
151 mbedtls_sha256(pubkey,
sizeof(pubkey), hash, 0);
154 if (loadStoredHash(stored,
sizeof(stored))) {
155 if (memcmp(stored, hash,
sizeof(hash)) == 0) {
158 LOG_W(
TAG,
"Attestation key mismatch, regenerating");
162 LOG_E(
TAG,
"Failed to regenerate attestation key");
167 mbedtls_sha256(pubkey,
sizeof(pubkey), hash, 0);
170 if (!saveStoredHash(hash,
sizeof(hash))) {
171 LOG_W(
TAG,
"Failed to store attestation key hash");
static constexpr uint32_t RETRY_INTERVAL_MS
static constexpr size_t P256_PUBKEY_RAW_SIZE
Uncompressed P-256 public key, raw X||Y coordinates (no SEC1 0x04 prefix).
static constexpr const char * NVS_KEY_PUBHASH
Shared RAII wrappers for firmware resources.
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
#define LOG_I(tag, fmt,...)
#define LOG_E(tag, fmt,...)
bool start() override
Starts service, ensures initialized state, and attempts to provision the attestation key synchronousl...
void stop() override
Stops attestation-key background processing.
void onTick(uint32_t nowMs)
Periodically attempts to ensure attestation key exists and is valid.
bool init() override
Initializes service state.
static constexpr uint8_t ATTESTATION_ECC_SLOT
RAII wrapper for an NVS handle.
#define SHA256_DIGEST_SIZE
SHA-256 digest output size in bytes (FIPS 180-4).
static constexpr const char * NVS_NAMESPACE