26#if (PC_ENABLE_HTTP3 || PC_ENABLE_DTLS || PC_ENABLE_SMB)
29#include <mbedtls/aes.h>
30#include <mbedtls/gcm.h>
37 mbedtls_aes_context mbed;
40static_assert(
sizeof(pc_aes128) <=
PC_WORK_AES128,
"pc_aes128 outgrew PC_WORK_AES128 - raise it in protocore_config.h");
42pc_aes128 *pc_aes128_wants(
void)
45 return pc_span_ok(ws) ?
reinterpret_cast<pc_aes128 *
>(ws.
buf) : nullptr;
48void pc_aes128_init(pc_aes128 *ctx,
const uint8_t key[16])
50 mbedtls_aes_init(&ctx->mbed);
51 mbedtls_aes_setkey_enc(&ctx->mbed, key, 128);
54void pc_aes128_encrypt_block(pc_aes128 *ctx,
const uint8_t in[16], uint8_t out[16])
56 mbedtls_aes_crypt_ecb(&ctx->mbed, MBEDTLS_AES_ENCRYPT, in, out);
59void pc_aes128_wipe(pc_aes128 *ctx)
61 mbedtls_aes_free(&ctx->mbed);
66 "mbedtls_gcm_context outgrew PC_WORK_AES128GCM - raise it in protocore_config.h, which derives "
67 "PC_SECURE_ARENA_SIZE from it");
69pc_aes128gcm_key *pc_aes128gcm_key_init(
void *storage,
const uint8_t key[PC_AES128GCM_KEY_LEN])
71 mbedtls_gcm_context *g =
reinterpret_cast<mbedtls_gcm_context *
>(storage);
73 if (mbedtls_gcm_setkey(g, MBEDTLS_CIPHER_ID_AES, key, 128) != 0)
78 return reinterpret_cast<pc_aes128gcm_key *
>(g);
81void pc_aes128gcm_key_wipe(pc_aes128gcm_key *k)
83 mbedtls_gcm_context *g =
reinterpret_cast<mbedtls_gcm_context *
>(k);
85 pc_secure_wipe(
reinterpret_cast<uint8_t *
>(g),
sizeof(mbedtls_gcm_context));
88pc_cspan pc_aes128gcm_seal(pc_aes128gcm_key *k,
const uint8_t nonce[PC_AES128GCM_IV_LEN],
const uint8_t *aad,
89 size_t aad_len,
const uint8_t *pt,
size_t pt_len, uint8_t *ct_out,
90 uint8_t tag_out[PC_AES128GCM_TAG_LEN])
92 mbedtls_gcm_context *g =
reinterpret_cast<mbedtls_gcm_context *
>(k);
93 if (mbedtls_gcm_crypt_and_tag(g, MBEDTLS_GCM_ENCRYPT, pt_len, nonce, PC_AES128GCM_IV_LEN, aad, aad_len, pt, ct_out,
94 PC_AES128GCM_TAG_LEN, tag_out) != 0)
101bool pc_aes128gcm_open(pc_aes128gcm_key *k,
const uint8_t nonce[PC_AES128GCM_IV_LEN],
const uint8_t *aad,
102 size_t aad_len,
const uint8_t *ct,
size_t ct_len,
const uint8_t tag[PC_AES128GCM_TAG_LEN],
105 mbedtls_gcm_context *g =
reinterpret_cast<mbedtls_gcm_context *
>(k);
106 return mbedtls_gcm_auth_decrypt(g, ct_len, nonce, PC_AES128GCM_IV_LEN, aad, aad_len, tag, PC_AES128GCM_TAG_LEN, ct,
AES-128 block cipher + AEAD_AES_128_GCM (RFC 5116 / NIST SP 800-38D).
Per-translation-unit optimization override for hot, pure-integer crypto.
User-facing configuration for ProtoCore.
#define PC_WORK_AES128GCM
pc_span pc_secure_span(size_t n, size_t align)
Borrow n secure bytes as a span whose capacity is bound to the allocation.
Secure pool accessor - borrows that hold key material.
pc_cspan pc_cspan_from(const uint8_t *p, size_t len)
Bind a read-only span to memory whose extent is only known at run time.
bool pc_span_ok(const pc_span &s)
True when the span refers to real storage and every write so far has fit.
A writable byte region: the storage, the capacity that belongs to it, and what has been produced into...
uint8_t * buf
first byte, or nullptr when the region could not be obtained