Files
ZephCore/tests/support/identity_utils_boundary.cpp
T

64 lines
2.8 KiB
C++

#include <mesh/Utils.h>
#include <stdexcept>
#include <cstring>
// Identity tests exercise fromSeed, sign/verify, ECDH and storage serialization.
// Other constructors/private-key recovery need Utils.cpp/PSA integration later.
// Fail loudly if a new test accidentally reaches those unimplemented boundaries.
// Hex helpers are pure; copied from src/Utils.cpp for the prefs.json tests
// (ConfigSerializer stores binary blobs as hex).
static uint8_t hexVal(char c) {
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= '0' && c <= '9') return c - '0';
return 0;
}
bool mesh::Utils::fromHex(uint8_t *dest, int dest_size, const char *src_hex) {
size_t len = strlen(src_hex);
if (len != (size_t)(dest_size * 2)) return false;
uint8_t *dp = dest;
while ((size_t)(dp - dest) < (size_t)dest_size) {
char ch = *src_hex++;
char cl = *src_hex++;
*dp++ = (uint8_t)((hexVal(ch) << 4) | hexVal(cl));
}
return true;
}
void mesh::Utils::printHex(Stream &s, const uint8_t *src, size_t len) {
static const char hex_chars[] = "0123456789ABCDEF";
while (len > 0) {
uint8_t b = *src++;
s.print(hex_chars[b >> 4]);
s.print(hex_chars[b & 0x0F]);
len--;
}
}
void mesh::Utils::secureZeroize(void*, size_t) {
throw std::runtime_error("Identity RNG/recovery path requires real Utils integration");
}
bool mesh::Utils::constantTimeEqual(const void*, const void*, size_t) {
throw std::runtime_error("Identity validation requires real Utils integration");
}
// KissModem's hash and encrypt/decrypt commands (not exercised by its tests;
// the one-fragment sha256 is hash_capture.cpp's).
void mesh::Utils::sha256(uint8_t*, size_t, const uint8_t*, int, const uint8_t*, int) {
throw std::runtime_error("KISS hash command requires real Utils integration");
}
int mesh::Utils::encryptThenMAC(const uint8_t*, uint8_t*, const uint8_t*, int) {
throw std::runtime_error("KISS encrypt command requires real Utils integration");
}
// A test that needs MAC checks to pass or fail sets this; otherwise reaching one throws.
int (*test_mac_then_decrypt)(const uint8_t*, uint8_t*, const uint8_t*, int) = nullptr;
int mesh::Utils::MACThenDecrypt(const uint8_t* secret, uint8_t* dest, const uint8_t* src, int src_len) {
if (test_mac_then_decrypt) return test_mac_then_decrypt(secret, dest, src, src_len);
throw std::runtime_error("KISS decrypt command requires real Utils integration");
}
// Pure; copied from src/Utils.cpp for the BaseChatMesh contact tests (Mesh.cpp
// draws retransmit delays from it).
uint32_t mesh::RNG::nextInt(uint32_t _min, uint32_t _max) {
uint32_t num;
random((uint8_t *)&num, sizeof(num));
if (_max <= _min) return _min;
return (num % (_max - _min)) + _min;
}