Files
ZephCore/zephcore/include/mesh/Identity.h
T
liquidraver 799d694914 crypto: simplify entropy path after audit review
- Lift duplicated identity-gen block from main_companion.cpp +
  main_repeater.cpp into ZephyrRNG::generateFirstBootIdentity().
  Both mains shrink from ~40 lines to a 3-line helper call.
- Add LocalIdentity::fromSeed() so seed-derived keygen doesn't need
  a one-shot RNG wrapper; delete SeededRNG.
- Drop the per-byte ADC sampling loop: getBattMilliVolts() does an
  8-sample average + 10ms regulator settle internally, costing
  300-480ms of real wall-time and actively destroying the LSB jitter
  it was meant to harvest. Jitter mixer already dwarfs it.
- Centralize the printk + sys_reboot pattern as
  Utils::cryptoPanicReboot(); drop the 2000ms pre-reboot k_msleep
  (printk is synchronous, sleep just blocked the mesh thread on
  the ZephyrRNG::random() retry-failure path).
- Inline sample_cpu_jitter health check via online scalars instead
  of a 512-byte deltas[] array. Saves 1.5KB stack churn across boot
  and tracks every sample instead of only the first 128.
- extract_via_aes_ctr now uses Utils::sha256 instead of open-coding
  psa_hash_compute.
2026-05-29 07:58:54 +02:00

69 lines
2.2 KiB
C++

/*
* SPDX-License-Identifier: Apache-2.0
* ZephCore Identity - Ed25519 key pairs
*/
#pragma once
#include <mesh/Utils.h>
#include <mesh/MeshCore.h>
#include <mesh/RNG.h>
#include <stddef.h>
#include <string.h>
namespace mesh {
class Identity {
public:
uint8_t pub_key[PUB_KEY_SIZE];
Identity();
Identity(const char *pub_hex);
Identity(const uint8_t *_pub) { memcpy(pub_key, _pub, PUB_KEY_SIZE); }
int copyHashTo(uint8_t *dest) const {
memcpy(dest, pub_key, PATH_HASH_SIZE);
return PATH_HASH_SIZE;
}
int copyHashTo(uint8_t *dest, uint8_t len) const {
memcpy(dest, pub_key, len);
return len;
}
bool isHashMatch(const uint8_t *hash) const {
return memcmp(hash, pub_key, PATH_HASH_SIZE) == 0;
}
bool isHashMatch(const uint8_t *hash, uint8_t len) const {
return memcmp(hash, pub_key, len) == 0;
}
bool verify(const uint8_t *sig, const uint8_t *message, int msg_len) const;
bool matches(const Identity &other) const { return memcmp(pub_key, other.pub_key, PUB_KEY_SIZE) == 0; }
bool matches(const uint8_t *other_pubkey) const { return memcmp(pub_key, other_pubkey, PUB_KEY_SIZE) == 0; }
bool readFrom(const uint8_t *src, size_t len);
bool writeTo(uint8_t *dest, size_t max_len) const;
};
class LocalIdentity : public Identity {
uint8_t prv_key[PRV_KEY_SIZE];
public:
LocalIdentity();
LocalIdentity(const char *prv_hex, const char *pub_hex);
LocalIdentity(RNG *rng);
/* Derive Ed25519 keypair from a 32-byte seed. Use this when you've
* already produced a high-quality seed externally (e.g. via the
* layered ZephyrRNG::mixIdentitySeed entropy mixer) — avoids the
* one-shot-RNG-wrapper dance otherwise needed to feed bytes
* through the LocalIdentity(RNG*) constructor. */
void fromSeed(const uint8_t seed[SEED_SIZE]);
void sign(uint8_t *sig, const uint8_t *message, int msg_len) const;
void calcSharedSecret(uint8_t *secret, const Identity &other) const { calcSharedSecret(secret, other.pub_key); }
void calcSharedSecret(uint8_t *secret, const uint8_t *other_pub_key) const;
static bool validatePrivateKey(const uint8_t prv[64]);
bool readFrom(const uint8_t *src, size_t len);
size_t writeTo(uint8_t *dest, size_t max_len) const;
};
} /* namespace mesh */