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

HMAC-SHA2-256 implementation (RFC 2104). More...

#include "crypto/mac/hmac_sha256.h"
#include "crypto/crypto_opt.h"
#include "server/mmgr/secure.h"
#include <string.h>

Go to the source code of this file.

Classes

struct  PC_CRYPTO_HOT::HmacWork
 

Namespaces

namespace  PC_CRYPTO_HOT
 

Functions

void PC_CRYPTO_HOT::build_key_block (const uint8_t *key, size_t key_len, uint8_t block[64], uint8_t pad_byte, uint8_t scratch[64])
 
void pc_hmac_sha256_init (pc_hmac_sha256_ctx *ctx, const uint8_t *key, size_t key_len)
 Initialize a streaming HMAC-SHA2-256 context.
 
void pc_hmac_sha256_update (pc_hmac_sha256_ctx *ctx, const uint8_t *data, size_t len)
 Feed len bytes into the running HMAC.
 
void pc_hmac_sha256_final (pc_hmac_sha256_ctx *ctx, uint8_t mac[PC_HMAC_SHA256_LEN])
 Finalize and write the 32-byte MAC.
 
void pc_hmac_sha256 (const uint8_t *key, size_t key_len, const uint8_t *data, size_t len, uint8_t mac[PC_HMAC_SHA256_LEN])
 Compute HMAC-SHA2-256 over a single contiguous buffer.
 

Detailed Description

HMAC-SHA2-256 implementation (RFC 2104).

Implemented in terms of the pc_sha256 streaming functions so it compiles identically on Arduino and native. The inner SHA-256 hardware acceleration (where present) is transparent through those calls.

RFC 2104 construction: HMAC(K, m) = H((K XOR opad) || H((K XOR ipad) || m)), H = SHA-256, ipad = 0x36 repeated, opad = 0x5c repeated. Keys > 64 bytes are pre-hashed; keys <= 64 are zero-padded to the 64-byte block. SSH-derived MAC keys are 32 bytes, so they are padded, not pre-hashed.

The transient working memory that touches the key (the padded ipad/opad blocks, the intermediate inner digest, and the one outer / one-shot hash context) lives in the shared crypto scratch (HMAC-256 region) and is wiped on the way out - never on the stack. The caller-owned streaming context (pc_hmac_sha256_ctx: the opad key block + the inner hash state) is per-session state the caller wipes at teardown, so it is NOT kept in the scratch (a long-lived value there would be clobbered by the next op).

Definition in file hmac_sha256.cpp.

Function Documentation

◆ pc_hmac_sha256_init()

void pc_hmac_sha256_init ( pc_hmac_sha256_ctx ctx,
const uint8_t *  key,
size_t  key_len 
)

Initialize a streaming HMAC-SHA2-256 context.

Parameters
ctxContext to initialize.
keyKey bytes (keys > 64 are pre-hashed per RFC 2104).
key_lenLength of key in bytes.

Definition at line 74 of file hmac_sha256.cpp.

References pc_span::buf, pc_hmac_sha256_ctx::inner, pc_hmac_sha256_ctx::okey, pc_sha256_init(), pc_sha256_update(), pc_span_ok(), and SecureBorrow::span().

◆ pc_hmac_sha256_update()

void pc_hmac_sha256_update ( pc_hmac_sha256_ctx ctx,
const uint8_t *  data,
size_t  len 
)

Feed len bytes into the running HMAC.

Definition at line 90 of file hmac_sha256.cpp.

References pc_hmac_sha256_ctx::inner, and pc_sha256_update().

◆ pc_hmac_sha256_final()

void pc_hmac_sha256_final ( pc_hmac_sha256_ctx ctx,
uint8_t  mac[PC_HMAC_SHA256_LEN] 
)

◆ pc_hmac_sha256()

void pc_hmac_sha256 ( const uint8_t *  key,
size_t  key_len,
const uint8_t *  data,
size_t  len,
uint8_t  mac[PC_HMAC_SHA256_LEN] 
)

Compute HMAC-SHA2-256 over a single contiguous buffer.

Parameters
keyMAC key bytes.
key_lenKey length in bytes. Keys > 64 bytes are pre-hashed per RFC 2104; keys <= 64 bytes are zero-padded to the block length. SSH-derived keys are always 32 bytes.
dataInput bytes.
lenInput length.
macOutput buffer, must be PC_HMAC_SHA256_LEN bytes.

Definition at line 113 of file hmac_sha256.cpp.

References pc_span::buf, PC_SHA256_DIGEST_LEN, pc_sha256_final(), pc_sha256_init(), pc_sha256_update(), pc_span_ok(), and SecureBorrow::span().