22#include <mbedtls/aes.h>
38 mbedtls_aes_context
aes;
42 "Aes256CtrWork outgrew PC_WORK_AES256CTR - raise it in protocore_config.h, which derives "
43 "PC_SECURE_ARENA_SIZE from it");
46 const uint8_t *in, uint8_t *out,
size_t len)
56 mbedtls_aes_context *aes = &w->
aes;
58 mbedtls_aes_init(aes);
59 mbedtls_aes_setkey_enc(aes, key, 256);
61 mbedtls_aes_crypt_ctr(aes, len, &nc_off, counter, ks, in, out);
62 mbedtls_aes_free(aes);
79 const uint8_t *in, uint8_t *out,
size_t len)
83 const pc_span &ws = ws_b.span();
93 for (
size_t i = 0; i < len; i++)
98 for (
int j = 15; j >= 0; j--)
106 out[i] = in[i] ^ ks[pos];
107 pos = (uint8_t)((pos + 1u) & 0x0fu);
118 const uint8_t enc4[4])
131 mbedtls_aes_init(&w->
aes);
132 mbedtls_aes_setkey_enc(&w->
aes, key, 256);
133 mbedtls_aes_crypt_ecb(&w->
aes, MBEDTLS_AES_ENCRYPT, counter, ks);
134 mbedtls_aes_free(&w->
aes);
139 uint32_t len = ((uint32_t)(enc4[0] ^ ks[0]) << 24) | ((uint32_t)(enc4[1] ^ ks[1]) << 16) |
140 ((uint32_t)(enc4[2] ^ ks[2]) << 8) | (uint32_t)(enc4[3] ^ ks[3]);
void pc_aes256ctr_crypt(const uint8_t key[PC_AES256CTR_KEY_LEN], uint8_t counter[PC_AES256CTR_CTR_LEN], const uint8_t *in, uint8_t *out, size_t len)
Encrypt or decrypt len bytes with AES-256-CTR (the two are identical).
uint32_t pc_aes256ctr_get_length(const uint8_t key[PC_AES256CTR_KEY_LEN], const uint8_t counter[PC_AES256CTR_CTR_LEN], const uint8_t enc4[4])
Decrypt only the 4-byte SSH packet_length prefix WITHOUT advancing counter.
AES-256-CTR stream cipher (aes256-ctr, RFC 4344 ยง4) - stateless one-shot API.
#define PC_AES256CTR_CTR_LEN
AES-256-CTR counter/IV block length (bytes).
#define PC_AES256CTR_KEY_LEN
AES-256-CTR key length (bytes).
Compact table-free software AES key schedule + single-block encrypt (FIPS 197) - one source of truth.
void pc_aes_key_expand(const uint8_t *key, int nk, uint32_t *rk)
AES key expansion (FIPS 197 sec 5.2). nk key words (4=AES-128, 8=AES-256); rk receives 4*(nk + 7) rou...
void pc_aes_encrypt_block(const uint32_t *rk, int nr, const uint8_t in[16], uint8_t out[16])
AES single-block encrypt (FIPS 197 sec 5.1), nr rounds (10=AES-128, 14=AES-256). State is column-majo...
A secure borrow whose acquire and release are one call each.
const pc_span & span() const
The borrowed region; empty if the pool could not satisfy it.
Per-translation-unit optimization override for hot, pure-integer crypto.
#define PC_WORK_AES256CTR
Secure pool accessor - borrows that hold key material.
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