Files
ZephCore/zephcore/include/mesh/Utils.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

51 lines
2.2 KiB
C++

/*
* SPDX-License-Identifier: Apache-2.0
* ZephCore Utils - crypto and helpers (Zephyr port)
*/
#pragma once
#include <mesh/MeshCore.h>
#include <string.h>
#include <stddef.h>
namespace mesh {
class Utils {
public:
static void sha256(uint8_t *hash, size_t hash_len, const uint8_t *msg, int msg_len);
static void sha256(uint8_t *hash, size_t hash_len, const uint8_t *frag1, int frag1_len, const uint8_t *frag2, int frag2_len);
static int encrypt(const uint8_t *shared_secret, uint8_t *dest, const uint8_t *src, int src_len);
static int decrypt(const uint8_t *shared_secret, uint8_t *dest, const uint8_t *src, int src_len);
static int encryptThenMAC(const uint8_t *shared_secret, uint8_t *dest, const uint8_t *src, int src_len);
static int MACThenDecrypt(const uint8_t *shared_secret, uint8_t *dest, const uint8_t *src, int src_len);
/* Constant-time byte-equality. Returns true iff every byte of `a`
* matches `b`. No early exit — timing is independent of input,
* defeating timing-leak attacks on MAC/password/secret compares.
* `volatile` accumulator survives `-Os` LTO — disassembly-verified
* on Cortex-M4 (rak3401_1watt). Pattern from rweather/arduinolibs. */
static bool constantTimeEqual(const void *a, const void *b, size_t n);
/* Securely zero a buffer such that the compiler cannot elide the
* writes as dead-store optimization. Uses volatile pointer writes —
* standard idiom for clearing crypto secrets before stack unwind.
* Use this for any buffer holding key material, seeds, or shared
* secrets after their last use. */
static void secureZeroize(void *buf, size_t n);
/* Log a crypto-invariant failure to printk and cold-reboot.
* Used when an entropy source, KDF, or other primitive cannot
* produce a safe result — proceeding would risk weak keys or
* bypassed authentication, so we restart rather than continue.
* Does not return. */
[[noreturn]] static void cryptoPanicReboot(const char *msg);
static void toHex(char *dest, const uint8_t *src, size_t len);
static bool fromHex(uint8_t *dest, int dest_size, const char *src_hex);
static bool isHexChar(char c);
static int parseTextParts(char *text, const char *parts[], int max_num, char separator = ',');
};
} /* namespace mesh */