DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ssh_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 ssh_sha256.h
6 * @brief SHA-256 (FIPS 180-4) - streaming context and one-shot API.
7 *
8 * On Arduino (ESP32) targets BOTH the one-shot and the streaming context
9 * (init / update / final) delegate to mbedtls, which routes SHA-256 to the
10 * hardware accelerator. The streaming path backs the per-packet HMAC
11 * (ssh_hmac_sha256, run on every inbound and outbound SSH packet) as well as
12 * the KEX exchange-hash, so hardware acceleration matters for bulk throughput,
13 * not just the handshake. On native builds the software implementation is used.
14 *
15 * The context therefore holds an mbedtls_sha256_context on Arduino (pulling in
16 * <mbedtls/sha256.h>) and the software hash state on native.
17 *
18 * @author Douglas Quigg (dstroy0)
19 * @date 2026
20 */
21
22#ifndef DETERMINISTICESPASYNCWEBSERVER_SSH_SHA256_H
23#define DETERMINISTICESPASYNCWEBSERVER_SSH_SHA256_H
24
25#include <stddef.h>
26#include <stdint.h>
27
28/** @brief SHA-256 digest length in bytes. */
29#define SSH_SHA256_DIGEST_LEN 32
30
31/** @brief SHA-256 block size in bytes. */
32#define SSH_SHA256_BLOCK_LEN 64
33
34/**
35 * @brief Streaming SHA-256 context.
36 *
37 * Holds the running hash state so data can be fed in multiple chunks.
38 * Used for the KEX exchange-hash which is assembled from several
39 * separately-encoded fields (V_C, V_S, I_C, I_S, K_S, e, f, K).
40 */
41#ifdef ARDUINO
42#include <mbedtls/sha256.h>
43typedef struct
44{
45 mbedtls_sha256_context mbed; ///< HW-accelerated SHA-256 state (ESP32 mbedtls).
47#else
48typedef struct
49{
50 uint32_t s[8]; ///< Running hash words (H0..H7).
51 uint64_t n; ///< Total bytes processed so far.
52 uint8_t buf[64]; ///< Partial block accumulator.
53 uint32_t buflen; ///< Bytes valid in buf[].
55#endif
56
57/**
58 * @brief Initialize a streaming SHA-256 context.
59 * @param ctx Must not be NULL.
60 */
62
63/**
64 * @brief Feed @p len bytes of @p data into the running hash.
65 * @param ctx Initialized context.
66 * @param data Input bytes.
67 * @param len Number of input bytes.
68 */
69void ssh_sha256_update(SshSha256Ctx *ctx, const uint8_t *data, size_t len);
70
71/**
72 * @brief Finalize the hash and write the 32-byte digest.
73 *
74 * The context is in an undefined state after this call; do not call
75 * update() again without first calling init().
76 *
77 * @param ctx Initialized context with all data already fed.
78 * @param digest Output buffer, must be SSH_SHA256_DIGEST_LEN bytes.
79 */
80void ssh_sha256_final(SshSha256Ctx *ctx, uint8_t digest[SSH_SHA256_DIGEST_LEN]);
81
82/**
83 * @brief One-shot SHA-256: hash @p len bytes and write the digest.
84 *
85 * On Arduino uses the mbedtls hardware-accelerated path.
86 * On native uses the software streaming implementation.
87 *
88 * @param data Input bytes.
89 * @param len Number of input bytes.
90 * @param digest Output buffer, must be SSH_SHA256_DIGEST_LEN bytes.
91 */
92void ssh_sha256(const uint8_t *data, size_t len, uint8_t digest[SSH_SHA256_DIGEST_LEN]);
93
94#endif // DETERMINISTICESPASYNCWEBSERVER_SSH_SHA256_H
void ssh_sha256(const uint8_t *data, size_t len, uint8_t digest[SSH_SHA256_DIGEST_LEN])
One-shot SHA-256: hash len bytes and write the digest.
void ssh_sha256_init(SshSha256Ctx *ctx)
Initialize a streaming SHA-256 context.
void ssh_sha256_update(SshSha256Ctx *ctx, const uint8_t *data, size_t len)
Feed len bytes of data into the running hash.
void ssh_sha256_final(SshSha256Ctx *ctx, uint8_t digest[SSH_SHA256_DIGEST_LEN])
Finalize the hash and write the 32-byte digest.
#define SSH_SHA256_DIGEST_LEN
SHA-256 digest length in bytes.
Definition ssh_sha256.h:29
Streaming SHA-256 context.
Definition ssh_sha256.h:44
mbedtls_sha256_context mbed
HW-accelerated SHA-256 state (ESP32 mbedtls).
Definition ssh_sha256.h:45