ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sha256.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sha256.h
6 * @brief SHA-256 (FIPS 180-4) - streaming context and one-shot API.
7 *
8 * The shared SHA-256 primitive for the whole library (SSH per-packet HMAC and KEX exchange-hash, TLS
9 * 1.3 / QUIC / DTLS key schedules, SNMPv3, JWT, CSRF, SMB 2.x message signing). On Arduino (ESP32)
10 * BOTH the streaming context and the one-shot delegate to mbedtls, which routes SHA-256 to the
11 * hardware accelerator; on native builds the software FIPS-180-4 implementation below is used.
12 * Mirrors the sha512 dual-path structure.
13 *
14 * @author Douglas Quigg (dstroy0)
15 * @date 2026
16 */
17
18#ifndef PROTOCORE_SHA256_H
19#define PROTOCORE_SHA256_H
20
21#include <stddef.h>
22#include <stdint.h>
23
24/** @brief SHA-256 digest length in bytes. */
25#define PC_SHA256_DIGEST_LEN 32
26
27/** @brief SHA-256 block size in bytes. */
28#define PC_SHA256_BLOCK_LEN 64
29
30/**
31 * @brief Streaming SHA-256 context.
32 *
33 * Holds the running hash state so data can be fed in multiple chunks. Backs the per-packet HMAC (run
34 * on every inbound and outbound SSH packet) and the KEX exchange-hash assembled from several
35 * separately-encoded fields, so hardware acceleration matters for bulk throughput, not just handshakes.
36 */
37#ifdef ARDUINO
38#include <mbedtls/sha256.h>
39typedef struct
40{
41 mbedtls_sha256_context mbed; ///< HW-accelerated SHA-256 state (ESP32 mbedtls).
43#else
44typedef struct
45{
46 uint32_t s[8]; ///< Running hash words (H0..H7).
47 uint64_t n; ///< Total bytes processed so far.
48 uint8_t buf[64]; ///< Partial block accumulator.
49 uint32_t buflen; ///< Bytes valid in buf[].
51#endif
52
53/** @brief Initialize a streaming SHA-256 context (@p ctx must not be NULL). */
55
56/** @brief Feed @p len bytes of @p data into the running hash. */
57void pc_sha256_update(pc_sha256_ctx *ctx, const uint8_t *data, size_t len);
58
59/**
60 * @brief Finalize the hash and write the 32-byte digest. The context is undefined afterwards; call
61 * init() again before reuse.
62 * @param digest Output buffer, PC_SHA256_DIGEST_LEN bytes.
63 */
64void pc_sha256_final(pc_sha256_ctx *ctx, uint8_t digest[PC_SHA256_DIGEST_LEN]);
65
66/** @brief One-shot SHA-256: hash @p len bytes of @p data into @p digest (32 bytes). */
67void pc_sha256(const uint8_t *data, size_t len, uint8_t digest[PC_SHA256_DIGEST_LEN]);
68
69#endif // PROTOCORE_SHA256_H
void pc_sha256_final(pc_sha256_ctx *ctx, uint8_t digest[PC_SHA256_DIGEST_LEN])
Finalize the hash and write the 32-byte digest. The context is undefined afterwards; call init() agai...
Definition sha256.cpp:48
void pc_sha256_update(pc_sha256_ctx *ctx, const uint8_t *data, size_t len)
Feed len bytes of data into the running hash.
Definition sha256.cpp:39
#define PC_SHA256_DIGEST_LEN
SHA-256 digest length in bytes.
Definition sha256.h:25
void pc_sha256(const uint8_t *data, size_t len, uint8_t digest[PC_SHA256_DIGEST_LEN])
One-shot SHA-256: hash len bytes of data into digest (32 bytes).
Definition sha256.cpp:58
void pc_sha256_init(pc_sha256_ctx *ctx)
Initialize a streaming SHA-256 context (ctx must not be NULL).
Definition sha256.cpp:29
Streaming SHA-256 context.
Definition sha256.h:40
mbedtls_sha256_context mbed
HW-accelerated SHA-256 state (ESP32 mbedtls).
Definition sha256.h:41