ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
aes256ctr.cpp File Reference

AES-256-CTR implementation (stateless, see aes256ctr.h). More...

#include "crypto/cipher/aes256ctr.h"
#include "crypto/crypto_opt.h"
#include "server/mmgr/secure.h"
#include <string.h>
#include <mbedtls/aes.h>

Go to the source code of this file.

Classes

struct  Aes256CtrWork
 

Functions

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.
 

Detailed Description

AES-256-CTR implementation (stateless, see aes256ctr.h).

The ephemeral key schedule and keystream block are borrowed from the shared crypto scratch from the secure pool - never a stack or BSS local - and wiped on release, so expanded key material is funneled through the one hardened, single-op region rather than scattered across the address space.

Arduino path: mbedtls_aes_crypt_ctr(), which the ESP32 mbedtls port routes to the hardware AES accelerator. Native path: a compact software AES-256 (256-byte forward S-box + GF(2^8) MixColumns), for host-side unit tests only.

Definition in file aes256ctr.cpp.

Function Documentation

◆ pc_aes256ctr_crypt()

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).

Rebuilds the key schedule from key on the stack, produces the CTR keystream starting at counter, and XORs it into out. counter is advanced in place by ceil(len / 16) blocks so the next call continues the same stream. in and out may alias (in-place is the mode ssh_packet.cpp uses).

Parameters
key32-byte AES-256 key.
counter16-byte counter block (big-endian); updated in place to the next unused block.
inInput bytes.
outOutput bytes (may equal in).
lenNumber of bytes to process.

Definition at line 45 of file aes256ctr.cpp.

References Aes256CtrWork::aes, pc_span::buf, Aes256CtrWork::ks, pc_span_ok(), and SecureBorrow::span().

Referenced by ssh_pkt_send().

◆ pc_aes256ctr_get_length()

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.

Mirrors pc_chachapoly_get_length: lets the receiver learn a packet's length (and thus how many bytes to wait for) before the whole packet has arrived, without consuming counter state. The key schedule and keystream block live in the shared crypto scratch; no cipher state touches the stack.

Parameters
key32-byte AES-256 key.
countercurrent 16-byte counter block (read only; not advanced).
enc4the 4 encrypted length bytes (start of the packet).
Returns
the decrypted big-endian SSH packet_length.

Definition at line 117 of file aes256ctr.cpp.

References Aes256CtrWork::aes, pc_span::buf, Aes256CtrWork::ks, pc_aes_encrypt_block(), pc_aes_key_expand(), pc_span_ok(), and SecureBorrow::span().