ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
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 sha256.cpp
6 * @brief SHA-256 implementation (FIPS 180-4).
7 *
8 * On Arduino builds streaming + one-shot delegate to mbedtls (ESP32 hardware SHA accelerator); on
9 * native builds the software path below is used. Shared by SSH, TLS 1.3 / QUIC / DTLS, SNMPv3, JWT,
10 * CSRF, and SMB 2.x message signing.
11 */
12
13#include "crypto/hash/sha256.h"
14#include "crypto/crypto_opt.h"
15#include <string.h>
16#ifdef ARDUINO
17#include <mbedtls/sha256.h> // hardware SHA accelerator
18#else
19#include "shared_primitives/endian.h" // native software SHA-256
20#endif
22
23#ifdef ARDUINO
24
25// ---------------------------------------------------------------------------
26// Arduino (ESP32): streaming + one-shot via mbedtls (hardware SHA accelerator).
27// ---------------------------------------------------------------------------
28
30{
31 mbedtls_sha256_init(&ctx->mbed);
32#if MBEDTLS_VERSION_MAJOR >= 3
33 mbedtls_sha256_starts(&ctx->mbed, 0 /* 0 = SHA-256 */);
34#else
35 mbedtls_sha256_starts_ret(&ctx->mbed, 0);
36#endif
37}
38
39void pc_sha256_update(pc_sha256_ctx *ctx, const uint8_t *data, size_t len)
40{
41#if MBEDTLS_VERSION_MAJOR >= 3
42 mbedtls_sha256_update(&ctx->mbed, data, len);
43#else
44 mbedtls_sha256_update_ret(&ctx->mbed, data, len);
45#endif
46}
47
49{
50#if MBEDTLS_VERSION_MAJOR >= 3
51 mbedtls_sha256_finish(&ctx->mbed, digest);
52#else
53 mbedtls_sha256_finish_ret(&ctx->mbed, digest);
54#endif
55 mbedtls_sha256_free(&ctx->mbed);
56}
57
58void pc_sha256(const uint8_t *data, size_t len, uint8_t digest[PC_SHA256_DIGEST_LEN])
59{
60 (void)mbedtls_sha256(data, len, digest, 0 /* 0 = SHA-256, 1 = SHA-224 */);
61}
62
63#else // native software path
64
65// ---------------------------------------------------------------------------
66// Software SHA-256 (FIPS 180-4) - native/test builds only
67// ---------------------------------------------------------------------------
68
69static const uint32_t K256[64] = {
70 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
71 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
72 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
73 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
74 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
75 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
76 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
77 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u,
78};
79
80static const uint32_t H0[8] = {
81 0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au, 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u,
82};
83
84static inline uint32_t rotr32(uint32_t x, uint32_t n)
85{
86 return (x >> n) | (x << (32 - n));
87}
88
89// Compress one 64-byte block into the running hash state h[0..7] (FIPS 180-4 §6.2.2). The caller
90// handles padding and length so this sees full blocks only.
91static void sha256_block(uint32_t h[8], const uint8_t blk[64])
92{
93 // Message schedule W[0..63]. The first 16 words are the block read as big-endian; the rest are
94 // extended with the sigma-0/sigma-1 recurrence.
95 uint32_t W[64];
96 for (int i = 0; i < 16; i++)
97 {
98 W[i] = pc_rd32be(blk + i * 4);
99 }
100 for (int i = 16; i < 64; i++)
101 {
102 uint32_t s0 = rotr32(W[i - 15], 7U) ^ rotr32(W[i - 15], 18U) ^ (W[i - 15] >> 3U); // σ0
103 uint32_t s1 = rotr32(W[i - 2], 17U) ^ rotr32(W[i - 2], 19U) ^ (W[i - 2] >> 10U); // σ1
104 W[i] = W[i - 16] + s0 + W[i - 7] + s1;
105 }
106
107 // Working variables seeded from the current hash state.
108 uint32_t a = h[0];
109 uint32_t b = h[1];
110 uint32_t c = h[2];
111 uint32_t d = h[3];
112 uint32_t e = h[4];
113 uint32_t f = h[5];
114 uint32_t g = h[6];
115 uint32_t hh = h[7];
116
117 // 64 compression rounds. Each mixes in one schedule word W[i] and round constant K256[i] using the
118 // Ch/Maj choice/majority functions and the Sigma-0/Sigma-1 rotations.
119 for (int i = 0; i < 64; i++)
120 {
121 uint32_t S1 = rotr32(e, 6U) ^ rotr32(e, 11U) ^ rotr32(e, 25U); // Σ1
122 uint32_t ch = (e & f) ^ (~e & g); // Ch(e,f,g)
123 uint32_t tmp1 = hh + S1 + ch + K256[i] + W[i]; // T1
124 uint32_t S0 = rotr32(a, 2U) ^ rotr32(a, 13U) ^ rotr32(a, 22U); // Σ0
125 uint32_t maj = (a & b) ^ (a & c) ^ (b & c); // Maj(a,b,c)
126 uint32_t tmp2 = S0 + maj; // T2
127 // Rotate the eight working variables; only e and a take new values.
128 hh = g;
129 g = f;
130 f = e;
131 e = d + tmp1;
132 d = c;
133 c = b;
134 b = a;
135 a = tmp1 + tmp2;
136 }
137
138 // Feed-forward: add the compressed working variables back into the state.
139 h[0] += a;
140 h[1] += b;
141 h[2] += c;
142 h[3] += d;
143 h[4] += e;
144 h[5] += f;
145 h[6] += g;
146 h[7] += hh;
147}
148
150{
151 for (int i = 0; i < 8; i++)
152 {
153 ctx->s[i] = H0[i];
154 }
155 ctx->n = 0;
156 ctx->buflen = 0;
157 memset(ctx->buf, 0, sizeof(ctx->buf));
158}
159
160void pc_sha256_update(pc_sha256_ctx *ctx, const uint8_t *data, size_t len)
161{
162 ctx->n += len;
163 while (len > 0)
164 {
165 uint32_t space = 64 - ctx->buflen;
166 uint32_t take = (uint32_t)len < space ? (uint32_t)len : space;
167 memcpy(ctx->buf + ctx->buflen, data, take);
168 ctx->buflen += take;
169 data += take;
170 len -= take;
171 if (ctx->buflen == 64)
172 {
173 sha256_block(ctx->s, ctx->buf);
174 ctx->buflen = 0;
175 }
176 }
177}
178
179void pc_sha256_final(pc_sha256_ctx *ctx, uint8_t digest[PC_SHA256_DIGEST_LEN])
180{
181 uint64_t bitlen = ctx->n * 8;
182
183 ctx->buf[ctx->buflen++] = 0x80;
184
185 if (ctx->buflen > 56)
186 {
187 while (ctx->buflen < 64)
188 {
189 ctx->buf[ctx->buflen++] = 0x00;
190 }
191 sha256_block(ctx->s, ctx->buf);
192 ctx->buflen = 0;
193 }
194
195 while (ctx->buflen < 56)
196 {
197 ctx->buf[ctx->buflen++] = 0x00;
198 }
199
200 pc_wr64be(ctx->buf + 56, bitlen);
201 sha256_block(ctx->s, ctx->buf);
202
203 for (int i = 0; i < 8; i++)
204 {
205 pc_wr32be(digest + i * 4, ctx->s[i]);
206 }
207}
208
209void pc_sha256(const uint8_t *data, size_t len, uint8_t digest[PC_SHA256_DIGEST_LEN])
210{
211 pc_sha256_ctx ctx;
212 pc_sha256_init(&ctx);
213 pc_sha256_update(&ctx, data, len);
214 pc_sha256_final(&ctx, digest);
215}
216
217#endif // !ARDUINO (native software path)
Per-translation-unit optimization override for hot, pure-integer crypto.
Fixed-width integer serializers into a raw uint8_t* buffer - one source of truth.
size_t pc_wr64be(uint8_t *p, uint64_t v)
Write v big-endian at p.
Definition endian.h:103
uint32_t pc_rd32be(const uint8_t *p)
Read a big-endian u32 at p.
Definition endian.h:119
size_t pc_wr32be(uint8_t *p, uint32_t v)
Write v big-endian at p.
Definition endian.h:93
PC_CRYPTO_HOT void pc_sha256_init(pc_sha256_ctx *ctx)
Initialize a streaming SHA-256 context (ctx must not be NULL).
Definition sha256.cpp:29
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
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
SHA-256 (FIPS 180-4) - streaming context and one-shot API.
#define PC_SHA256_DIGEST_LEN
SHA-256 digest length in bytes.
Definition sha256.h:25
Streaming SHA-256 context.
Definition sha256.h:40
mbedtls_sha256_context mbed
HW-accelerated SHA-256 state (ESP32 mbedtls).
Definition sha256.h:41