26#include <mbedtls/sha256.h>
30 mbedtls_sha256_init(&ctx->
mbed);
31#if MBEDTLS_VERSION_MAJOR >= 3
32 mbedtls_sha256_starts(&ctx->
mbed, 0 );
34 mbedtls_sha256_starts_ret(&ctx->
mbed, 0);
40#if MBEDTLS_VERSION_MAJOR >= 3
41 mbedtls_sha256_update(&ctx->
mbed, data, len);
43 mbedtls_sha256_update_ret(&ctx->
mbed, data, len);
49#if MBEDTLS_VERSION_MAJOR >= 3
50 mbedtls_sha256_finish(&ctx->
mbed, digest);
52 mbedtls_sha256_finish_ret(&ctx->
mbed, digest);
54 mbedtls_sha256_free(&ctx->
mbed);
59 (void)mbedtls_sha256(data, len, digest, 0 );
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,
79static const uint32_t H0[8] = {
80 0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au, 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u,
83static inline uint32_t rotr32(uint32_t x, uint32_t n)
85 return (x >> n) | (x << (32 - n));
88static inline uint32_t load_be32(
const uint8_t *p)
90 return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3];
93static inline void store_be32(uint8_t *p, uint32_t v)
95 p[0] = (uint8_t)(v >> 24);
96 p[1] = (uint8_t)(v >> 16);
97 p[2] = (uint8_t)(v >> 8);
101static inline void store_be64(uint8_t *p, uint64_t v)
103 store_be32(p, (uint32_t)(v >> 32));
104 store_be32(p + 4, (uint32_t)(v));
109static void sha256_block(uint32_t h[8],
const uint8_t blk[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++)
118 uint32_t s0 = rotr32(W[i - 15], 7U) ^ rotr32(W[i - 15], 18U) ^ (W[i - 15] >> 3U);
119 uint32_t s1 = rotr32(W[i - 2], 17U) ^ rotr32(W[i - 2], 19U) ^ (W[i - 2] >> 10U);
120 W[i] = W[i - 16] + s0 + W[i - 7] + s1;
136 for (
int i = 0; i < 64; i++)
138 uint32_t S1 = rotr32(e, 6U) ^ rotr32(e, 11U) ^ rotr32(e, 25U);
139 uint32_t ch = (e & f) ^ (~e & g);
140 uint32_t tmp1 = hh + S1 + ch + K256[i] + W[i];
141 uint32_t S0 = rotr32(a, 2U) ^ rotr32(a, 13U) ^ rotr32(a, 22U);
142 uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
143 uint32_t tmp2 = S0 + maj;
172 for (
int i = 0; i < 8; i++)
176 memset(ctx->buf, 0,
sizeof(ctx->buf));
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);
190 if (ctx->buflen == 64)
192 sha256_block(ctx->s, ctx->buf);
200 uint64_t bitlen = ctx->n * 8;
202 ctx->buf[ctx->buflen++] = 0x80;
204 if (ctx->buflen > 56)
206 while (ctx->buflen < 64)
207 ctx->buf[ctx->buflen++] = 0x00;
208 sha256_block(ctx->s, ctx->buf);
212 while (ctx->buflen < 56)
213 ctx->buf[ctx->buflen++] = 0x00;
215 store_be64(ctx->buf + 56, bitlen);
216 sha256_block(ctx->s, ctx->buf);
218 for (
int i = 0; i < 8; i++)
219 store_be32(digest + i * 4, ctx->s[i]);
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.
Streaming SHA-256 context.
mbedtls_sha256_context mbed
HW-accelerated SHA-256 state (ESP32 mbedtls).