DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ssh_hmac_sha256.cpp File Reference

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

Go to the source code of this file.

Functions

void ssh_hmac_sha256_init (SshHmacCtx *ctx, const uint8_t *key, size_t key_len)
 Initialize a streaming HMAC-SHA2-256 context.
 
void ssh_hmac_sha256_update (SshHmacCtx *ctx, const uint8_t *data, size_t len)
 Feed len bytes into the running HMAC.
 
void ssh_hmac_sha256_final (SshHmacCtx *ctx, uint8_t mac[SSH_HMAC_SHA256_LEN])
 Finalize and write the 32-byte MAC.
 
void ssh_hmac_sha256 (const uint8_t *key, size_t key_len, const uint8_t *data, size_t len, uint8_t mac[SSH_HMAC_SHA256_LEN])
 Compute HMAC-SHA2-256 over a single contiguous buffer.
 

Detailed Description

HMAC-SHA2-256 implementation (RFC 2104).

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

RFC 2104 construction: HMAC(K, m) = H((K XOR opad) || H((K XOR ipad) || m))

where ipad = 0x36 repeated, opad = 0x5c repeated, H = SHA-256.

Since SSH-derived MAC keys are exactly 32 bytes (< SHA-256 block size of 64 bytes), the key is NOT pre-hashed - it is padded with zeros to 64 bytes internally and used directly as the HMAC key.

Definition in file ssh_hmac_sha256.cpp.

Function Documentation

◆ ssh_hmac_sha256_init()

void ssh_hmac_sha256_init ( SshHmacCtx ctx,
const uint8_t *  key,
size_t  key_len 
)

Initialize a streaming HMAC-SHA2-256 context.

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

Definition at line 53 of file ssh_hmac_sha256.cpp.

References SshHmacCtx::inner, SshHmacCtx::okey, ssh_sha256_init(), and ssh_sha256_update().

Referenced by ssh_hmac_sha256().

◆ ssh_hmac_sha256_update()

void ssh_hmac_sha256_update ( SshHmacCtx ctx,
const uint8_t *  data,
size_t  len 
)

Feed len bytes into the running HMAC.

Definition at line 63 of file ssh_hmac_sha256.cpp.

References SshHmacCtx::inner, and ssh_sha256_update().

Referenced by ssh_hmac_sha256().

◆ ssh_hmac_sha256_final()

void ssh_hmac_sha256_final ( SshHmacCtx ctx,
uint8_t  mac[SSH_HMAC_SHA256_LEN] 
)

Finalize and write the 32-byte MAC.

Definition at line 68 of file ssh_hmac_sha256.cpp.

References SshHmacCtx::inner, SshHmacCtx::okey, SSH_SHA256_DIGEST_LEN, ssh_sha256_final(), ssh_sha256_init(), and ssh_sha256_update().

Referenced by ssh_hmac_sha256().

◆ ssh_hmac_sha256()

void ssh_hmac_sha256 ( const uint8_t *  key,
size_t  key_len,
const uint8_t *  data,
size_t  len,
uint8_t  mac[SSH_HMAC_SHA256_LEN] 
)

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

Equivalent to calling the streaming variant with one update call.

Parameters
keyMAC key bytes.
key_lenKey length in bytes (must be ≤ 64; 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 SSH_HMAC_SHA256_LEN bytes.

Definition at line 85 of file ssh_hmac_sha256.cpp.

References ssh_hmac_sha256_final(), ssh_hmac_sha256_init(), and ssh_hmac_sha256_update().