ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hmac_sha256.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_HMAC_SHA256_H
5#define PROTOCORE_HMAC_SHA256_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
10
11/**
12 * @file hmac_sha256.h
13 * @brief HMAC-SHA2-256 (RFC 2104 + FIPS 198-1) - streaming context and one-shot API.
14 *
15 * The shared keyed-MAC primitive for the whole library: SSH binary-packet MAC (RFC 4253 ยง6.4), the
16 * TLS 1.3 / QUIC / DTLS HKDF PRF, SNMPv3 usmHMACSHAAuthProtocol, JWT HS256, CSRF tokens, and SMB 2.x
17 * message signing / the SP800-108 KDF. Built over the @ref Sha256Ns entries, so which arm compresses
18 * the inner hash is not visible here.
19 *
20 * RFC 2104 construction: HMAC(K, m) = H((K XOR opad) || H((K XOR ipad) || m)), H = SHA-256.
21 *
22 * SECURITY NOTE - a MAC must be verified before the covered plaintext is acted upon; that ordering
23 * guarantee lives in each protocol's packet layer, not here. These functions are pure crypto.
24 *
25 * For a MAC assembled from separate pieces - the SSH packet MAC over
26 * (uint32_be(seq_num) || plaintext_packet):
27 *
28 * @c work is PROTOCORE_HMAC_SHA256_BORROW secure bytes the CALLER took, at an address it knows. It
29 * is not held past the call, so nothing here aliases it. The caller releases
30 * it, and the pool wipes on release; this module neither takes it, holds it, releases it, nor wipes
31 * it. A connection takes those bytes once for its slot and passes them on every packet.
32 *
33 * @author Douglas Quigg (dstroy0)
34 * @date 2026
35 */
36
37/** @brief HMAC-SHA2-256 output length in bytes. */
38#define PROTOCORE_HMAC_SHA256_LEN 32
39
40/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
41typedef struct
42{
43 proto_bool (*init)(uint8_t *, const uint8_t *, size_t);
44 proto_bool (*update)(uint8_t *, const uint8_t *, size_t);
45 proto_bool (*final)(uint8_t *, uint8_t *);
46 proto_bool (*mac)(uint8_t *, const uint8_t *, size_t, const uint8_t *, size_t, uint8_t *);
48PROTOCORE_NS_LAYOUT(HmacSha256Ns, init, update, final, mac);
49
50/**
51 * @brief Start a MAC under HmacSha256Ns::key_args.
52 * @param work PROTOCORE_HMAC_SHA256_BORROW bytes the caller took. Not held past the call.
53 * @param key MAC key bytes
54 * @param key_len key length; > 64 is pre-hashed (RFC 2104), shorter is zero-padded to the block
55 * @return PROTO_TRUE on success.
56 */
57proto_bool protocore_hmac_sha256_init(uint8_t *work, const uint8_t *key, size_t key_len);
58/**
59 * @brief Feed the running MAC a chunk.
60 * @param work PROTOCORE_HMAC_SHA256_BORROW bytes the caller took. Not held past the call.
61 * @param data the bytes
62 * @param len how many
63 * @return PROTO_TRUE on success.
64 */
65proto_bool protocore_hmac_sha256_update(uint8_t *work, const uint8_t *data, size_t len);
66/**
67 * @brief Finish, writing the 32 bytes out.
68 * @param work PROTOCORE_HMAC_SHA256_BORROW bytes the caller took. Not held past the call.
69 * @param out PROTOCORE_HMAC_SHA256_LEN bytes
70 * @return PROTO_TRUE on success.
71 */
72proto_bool protocore_hmac_sha256_final(uint8_t *work, uint8_t *out);
73/**
74 * @brief Init, update and final in one call, for a message already whole.
75 * @param work PROTOCORE_HMAC_SHA256_BORROW bytes the caller took. Not held past the call.
76 * @param key MAC key bytes
77 * @param key_len key length
78 * @param data the message
79 * @param len its length
80 * @param out PROTOCORE_HMAC_SHA256_LEN bytes
81 * @return PROTO_TRUE on success.
82 */
83proto_bool protocore_hmac_sha256_mac(uint8_t *work, const uint8_t *key, size_t key_len, const uint8_t *data, size_t len,
84 uint8_t *out);
85
86/** @brief Module namespace. */
91
93
94#endif // PROTOCORE_HMAC_SHA256_H
proto_bool protocore_hmac_sha256_final(uint8_t *work, uint8_t *out)
Finish, writing the 32 bytes out.
PROTOCORE_NS HmacSha256Ns HmacSha256 PROTOCORE_UNUSED
Module namespace.
Definition hmac_sha256.h:87
proto_bool protocore_hmac_sha256_mac(uint8_t *work, const uint8_t *key, size_t key_len, const uint8_t *data, size_t len, uint8_t *out)
Init, update and final in one call, for a message already whole.
proto_bool protocore_hmac_sha256_update(uint8_t *work, const uint8_t *data, size_t len)
Feed the running MAC a chunk.
proto_bool protocore_hmac_sha256_init(uint8_t *work, const uint8_t *key, size_t key_len)
Start a MAC under HmacSha256Ns::key_args.
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition hmac_sha256.h:42
proto_bool(* init)(uint8_t *, const uint8_t *, size_t)
Definition hmac_sha256.h:43
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97