DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ssh_sha256.cpp
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.cpp
6 * @brief SHA-256 implementation.
7 *
8 * The streaming functions (init/update/final) are software on both
9 * platforms - they are only used for KEX hash construction (once per
10 * connection).
11 *
12 * The one-shot ssh_sha256() uses the ESP32 hardware SHA accelerator via
13 * mbedtls on Arduino builds, and the software path on native builds.
14 */
15
17#include <string.h>
18
19#ifdef ARDUINO
20
21// ---------------------------------------------------------------------------
22// Arduino (ESP32): streaming + one-shot via mbedtls (hardware SHA accelerator).
23// The software FIPS-180-4 path below is compiled only on native.
24// ---------------------------------------------------------------------------
25
26#include <mbedtls/sha256.h>
27
29{
30 mbedtls_sha256_init(&ctx->mbed);
31#if MBEDTLS_VERSION_MAJOR >= 3
32 mbedtls_sha256_starts(&ctx->mbed, 0 /* 0 = SHA-256 */);
33#else
34 mbedtls_sha256_starts_ret(&ctx->mbed, 0);
35#endif
36}
37
38void ssh_sha256_update(SshSha256Ctx *ctx, const uint8_t *data, size_t len)
39{
40#if MBEDTLS_VERSION_MAJOR >= 3
41 mbedtls_sha256_update(&ctx->mbed, data, len);
42#else
43 mbedtls_sha256_update_ret(&ctx->mbed, data, len);
44#endif
45}
46
48{
49#if MBEDTLS_VERSION_MAJOR >= 3
50 mbedtls_sha256_finish(&ctx->mbed, digest);
51#else
52 mbedtls_sha256_finish_ret(&ctx->mbed, digest);
53#endif
54 mbedtls_sha256_free(&ctx->mbed);
55}
56
57void ssh_sha256(const uint8_t *data, size_t len, uint8_t digest[SSH_SHA256_DIGEST_LEN])
58{
59 (void)mbedtls_sha256(data, len, digest, 0 /* 0 = SHA-256, 1 = SHA-224 */);
60}
61
62#else // native software path
63
64// ---------------------------------------------------------------------------
65// Software SHA-256 (FIPS 180-4) - native/test builds only
66// ---------------------------------------------------------------------------
67
68static const uint32_t K256[64] = {
69 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
70 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
71 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
72 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
73 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
74 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
75 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
76 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u,
77};
78
79static const uint32_t H0[8] = {
80 0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au, 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u,
81};
82
83static inline uint32_t rotr32(uint32_t x, uint32_t n)
84{
85 return (x >> n) | (x << (32 - n));
86}
87
88static inline uint32_t load_be32(const uint8_t *p)
89{
90 return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3];
91}
92
93static inline void store_be32(uint8_t *p, uint32_t v)
94{
95 p[0] = (uint8_t)(v >> 24);
96 p[1] = (uint8_t)(v >> 16);
97 p[2] = (uint8_t)(v >> 8);
98 p[3] = (uint8_t)(v);
99}
100
101static inline void store_be64(uint8_t *p, uint64_t v)
102{
103 store_be32(p, (uint32_t)(v >> 32));
104 store_be32(p + 4, (uint32_t)(v));
105}
106
107// Compress one 64-byte block into the running hash state h[0..7] (FIPS 180-4
108// §6.2.2). The caller handles padding and length so this sees full blocks only.
109static void sha256_block(uint32_t h[8], const uint8_t blk[64])
110{
111 // Message schedule W[0..63]. The first 16 words are the block read as
112 // big-endian; the rest are extended with the sigma-0/sigma-1 recurrence.
113 uint32_t W[64];
114 for (int i = 0; i < 16; i++)
115 W[i] = load_be32(blk + i * 4);
116 for (int i = 16; i < 64; i++)
117 {
118 uint32_t s0 = rotr32(W[i - 15], 7U) ^ rotr32(W[i - 15], 18U) ^ (W[i - 15] >> 3U); // σ0
119 uint32_t s1 = rotr32(W[i - 2], 17U) ^ rotr32(W[i - 2], 19U) ^ (W[i - 2] >> 10U); // σ1
120 W[i] = W[i - 16] + s0 + W[i - 7] + s1;
121 }
122
123 // Working variables seeded from the current hash state.
124 uint32_t a = h[0];
125 uint32_t b = h[1];
126 uint32_t c = h[2];
127 uint32_t d = h[3];
128 uint32_t e = h[4];
129 uint32_t f = h[5];
130 uint32_t g = h[6];
131 uint32_t hh = h[7];
132
133 // 64 compression rounds. Each mixes in one schedule word W[i] and round
134 // constant K256[i] using the Ch/Maj choice/majority functions and the
135 // Sigma-0/Sigma-1 rotations.
136 for (int i = 0; i < 64; i++)
137 {
138 uint32_t S1 = rotr32(e, 6U) ^ rotr32(e, 11U) ^ rotr32(e, 25U); // Σ1
139 uint32_t ch = (e & f) ^ (~e & g); // Ch(e,f,g)
140 uint32_t tmp1 = hh + S1 + ch + K256[i] + W[i]; // T1
141 uint32_t S0 = rotr32(a, 2U) ^ rotr32(a, 13U) ^ rotr32(a, 22U); // Σ0
142 uint32_t maj = (a & b) ^ (a & c) ^ (b & c); // Maj(a,b,c)
143 uint32_t tmp2 = S0 + maj; // T2
144 // Rotate the eight working variables; only e and a take new values.
145 hh = g;
146 g = f;
147 f = e;
148 e = d + tmp1;
149 d = c;
150 c = b;
151 b = a;
152 a = tmp1 + tmp2;
153 }
154
155 // Feed-forward: add the compressed working variables back into the state.
156 h[0] += a;
157 h[1] += b;
158 h[2] += c;
159 h[3] += d;
160 h[4] += e;
161 h[5] += f;
162 h[6] += g;
163 h[7] += hh;
164}
165
166// ---------------------------------------------------------------------------
167// Streaming API (software, native only)
168// ---------------------------------------------------------------------------
169
171{
172 for (int i = 0; i < 8; i++)
173 ctx->s[i] = H0[i];
174 ctx->n = 0;
175 ctx->buflen = 0;
176 memset(ctx->buf, 0, sizeof(ctx->buf));
177}
178
179void ssh_sha256_update(SshSha256Ctx *ctx, const uint8_t *data, size_t len)
180{
181 ctx->n += len;
182 while (len > 0)
183 {
184 uint32_t space = 64 - ctx->buflen;
185 uint32_t take = (uint32_t)len < space ? (uint32_t)len : space;
186 memcpy(ctx->buf + ctx->buflen, data, take);
187 ctx->buflen += take;
188 data += take;
189 len -= take;
190 if (ctx->buflen == 64)
191 {
192 sha256_block(ctx->s, ctx->buf);
193 ctx->buflen = 0;
194 }
195 }
196}
197
198void ssh_sha256_final(SshSha256Ctx *ctx, uint8_t digest[SSH_SHA256_DIGEST_LEN])
199{
200 uint64_t bitlen = ctx->n * 8;
201
202 ctx->buf[ctx->buflen++] = 0x80;
203
204 if (ctx->buflen > 56)
205 {
206 while (ctx->buflen < 64)
207 ctx->buf[ctx->buflen++] = 0x00;
208 sha256_block(ctx->s, ctx->buf);
209 ctx->buflen = 0;
210 }
211
212 while (ctx->buflen < 56)
213 ctx->buf[ctx->buflen++] = 0x00;
214
215 store_be64(ctx->buf + 56, bitlen);
216 sha256_block(ctx->s, ctx->buf);
217
218 for (int i = 0; i < 8; i++)
219 store_be32(digest + i * 4, ctx->s[i]);
220}
221
222// ---------------------------------------------------------------------------
223// One-shot (software)
224// ---------------------------------------------------------------------------
225
226void ssh_sha256(const uint8_t *data, size_t len, uint8_t digest[SSH_SHA256_DIGEST_LEN])
227{
228 SshSha256Ctx ctx;
229 ssh_sha256_init(&ctx);
230 ssh_sha256_update(&ctx, data, len);
231 ssh_sha256_final(&ctx, digest);
232}
233
234#endif // !ARDUINO (native software path)
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.
SHA-256 (FIPS 180-4) - streaming context and one-shot API.
#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