11#include "mbedtls/sha256.h"
12#include "mbedtls/md.h"
13#include "mbedtls/base64.h"
14#include "esp_random.h"
23const char BASE32_ALPHABET[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
32 esp_fill_random(buf, len);
40 if (se && se->getRandomStrict(buf, len))
return HOST_OK;
44int host_sha256(
const uint8_t* data,
size_t len, uint8_t out[32])
52 const uint8_t* data,
size_t dlen, uint8_t out[32])
55 const mbedtls_md_info_t* info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
57 if (mbedtls_md_hmac(info, key, klen, data, dlen, out) != 0)
return HOST_ERR_GENERIC;
62 const uint8_t* aad,
size_t aad_len,
63 const uint8_t* pt,
size_t pt_len,
64 uint8_t* ct, uint8_t tag[16])
73 const uint8_t* aad,
size_t aad_len,
74 const uint8_t* ct,
size_t ct_len,
75 const uint8_t tag[16], uint8_t* pt)
89 size_t needed = (in_len * 8 + 4) / 5 + 1;
94 for (
size_t i = 0; i < in_len; ++i) {
95 buffer = (buffer << 8) | in[i];
98 out[out_pos++] = BASE32_ALPHABET[(buffer >> (bits - 5)) & 0x1F];
103 out[out_pos++] = BASE32_ALPHABET[(buffer << (5 - bits)) & 0x1F];
115 for (
size_t i = 0; i < in_len; ++i) {
117 if (c ==
'=' || c ==
' ' || c ==
'\n' || c ==
'\r')
continue;
119 if (c >=
'A' && c <=
'Z') v = c -
'A';
120 else if (c >=
'a' && c <=
'z') v = c -
'a';
121 else if (c >=
'2' && c <=
'7') v = c -
'2' + 26;
123 buffer = (buffer << 5) | static_cast<uint32_t>(v);
127 out[out_pos++] =
static_cast<uint8_t
>((buffer >> (bits - 8)) & 0xFF);
131 return static_cast<int>(out_pos);
138 int rc = mbedtls_base64_encode(
reinterpret_cast<unsigned char*
>(out), out_size,
143 while (olen > 0 && out[olen - 1] ==
'=') --olen;
156 norm.reserve(in_len + 4);
157 for (
size_t i = 0; i < in_len; ++i) {
159 if (c ==
' ' || c ==
'\n' || c ==
'\r' || c ==
'\t')
continue;
162 while (norm.size() % 4 != 0) norm.push_back(
'=');
164 int rc = mbedtls_base64_decode(out, out_size, &olen,
165 reinterpret_cast<const unsigned char*
>(norm.data()),
168 return static_cast<int>(olen);
175 static const char HEX[] =
"0123456789ABCDEF";
176 for (
size_t i = 0; i < in_len; ++i) {
177 out[i * 2] = HEX[in[i] >> 4];
178 out[i * 2 + 1] = HEX[in[i] & 0x0F];
180 out[in_len * 2] =
'\0';
188 size_t need = in_len / 2;
190 for (
size_t i = 0; i < need; ++i) {
194 out[i] =
static_cast<uint8_t
>((hi << 4) | lo);
196 return static_cast<int>(need);
Shared AES-256-GCM helpers built on mbedTLS.
Hex-digit decoding shared by the plugin_manager source files.
int host_hex_encode(const uint8_t *in, size_t in_len, char *out, size_t out_size)
Lowercase-hex-encode in into NUL-terminated out.
int host_random_strict(uint8_t *buf, size_t len)
Fill buf with hardware-RNG bytes only; fails without TRNG.
int host_base64_decode(const char *in, size_t in_len, uint8_t *out, size_t out_size)
Base64-decode in into raw bytes in out.
int host_base32_encode(const uint8_t *in, size_t in_len, char *out, size_t out_size)
Base32-encode in into NUL-terminated out.
int host_random(uint8_t *buf, size_t len)
Fill buf with hardware-RNG bytes; may fall back to PRNG.
int host_aes_gcm_decrypt(const uint8_t *key, const uint8_t *iv, const uint8_t *aad, size_t aad_len, const uint8_t *ct, size_t ct_len, const uint8_t tag[16], uint8_t *pt)
AES-256-GCM decrypt and verify.
int host_base64_encode(const uint8_t *in, size_t in_len, char *out, size_t out_size)
Base64-encode in into NUL-terminated out.
int host_aes_gcm_encrypt(const uint8_t *key, const uint8_t *iv, const uint8_t *aad, size_t aad_len, const uint8_t *pt, size_t pt_len, uint8_t *ct, uint8_t tag[16])
AES-256-GCM encrypt.
int host_sha256(const uint8_t *data, size_t len, uint8_t out[32])
SHA-256 hash of data into the 32-byte out.
int host_hmac_sha256(const uint8_t *key, size_t klen, const uint8_t *data, size_t dlen, uint8_t out[32])
HMAC-SHA-256 of data under key into the 32-byte out.
int host_base32_decode(const char *in, size_t in_len, uint8_t *out, size_t out_size)
Base32-decode in into raw bytes in out.
int host_hex_decode(const char *in, size_t in_len, uint8_t *out, size_t out_size)
Hex-decode in (case-insensitive) into raw bytes in out.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_ERR_NOT_SUPPORTED
#define HOST_ERR_INVALID_ARG
#define HOST_ERR_NO_MEMORY
bool aesGcm256Seal(const uint8_t key[32], const uint8_t *iv, size_t ivLen, const uint8_t *aad, size_t aadLen, const uint8_t *pt, size_t ptLen, uint8_t *ctOut, uint8_t tagOut[16])
Encrypts pt with AES-256-GCM and produces a 16-byte tag.
bool aesGcm256Open(const uint8_t key[32], const uint8_t *iv, size_t ivLen, const uint8_t *aad, size_t aadLen, const uint8_t *ct, size_t ctLen, const uint8_t tag[16], uint8_t *ptOut)
Authenticates and decrypts ct with AES-256-GCM.
ISecureElement * getSecureElementInstance()
Returns singleton secure-element stub instance.
int hex_val(char c)
Convert a single hex digit to its numeric value.