ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sha512.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 sha512.h
6 * @brief SHA-512 (FIPS 180-4) - streaming context and one-shot API.
7 *
8 * The shared SHA-512 primitive for the whole library (SSH Ed25519 / kex hashing, PQC, SMB 3.1.1
9 * preauth integrity). On Arduino (ESP32) the streaming context and one-shot delegate to mbedtls
10 * (hardware-accelerated where available); on native builds the software FIPS-180-4 implementation is
11 * used. Mirrors the sha256 dual-path structure.
12 *
13 * @author Douglas Quigg (dstroy0)
14 * @date 2026
15 */
16
17#ifndef PROTOCORE_SHA512_H
18#define PROTOCORE_SHA512_H
19
20#include <stddef.h>
21#include <stdint.h>
22
23/** @brief SHA-512 digest length in bytes. */
24#define PC_SHA512_DIGEST_LEN 64
25
26/** @brief SHA-512 block size in bytes. */
27#define PC_SHA512_BLOCK_LEN 128
28
29/** @brief Streaming SHA-512 context. */
30#ifdef ARDUINO
31#include <mbedtls/sha512.h>
32typedef struct
33{
34 mbedtls_sha512_context mbed; ///< mbedtls SHA-512 state (ESP32).
36#else
37typedef struct
38{
39 uint64_t s[8]; ///< Running hash words (H0..H7).
40 uint64_t n; ///< Total bytes processed so far.
41 uint8_t buf[128]; ///< Partial block accumulator.
42 uint32_t buflen; ///< Bytes valid in buf[].
44#endif
45
46/** @brief Initialize a streaming SHA-512 context (@p ctx must not be NULL). */
48
49/** @brief Feed @p len bytes of @p data into the running hash. */
50void pc_sha512_update(pc_sha512_ctx *ctx, const uint8_t *data, size_t len);
51
52/**
53 * @brief Finalize the hash and write the 64-byte digest. The context is undefined afterwards; call
54 * init() again before reuse.
55 * @param digest Output buffer, PC_SHA512_DIGEST_LEN bytes.
56 */
57void pc_sha512_final(pc_sha512_ctx *ctx, uint8_t digest[PC_SHA512_DIGEST_LEN]);
58
59/** @brief One-shot SHA-512: hash @p len bytes of @p data into @p digest (64 bytes). */
60void pc_sha512(const uint8_t *data, size_t len, uint8_t digest[PC_SHA512_DIGEST_LEN]);
61
62#endif // PROTOCORE_SHA512_H
void pc_sha512_update(pc_sha512_ctx *ctx, const uint8_t *data, size_t len)
Feed len bytes of data into the running hash.
Definition sha512.cpp:38
void pc_sha512_init(pc_sha512_ctx *ctx)
Initialize a streaming SHA-512 context (ctx must not be NULL).
Definition sha512.cpp:28
void pc_sha512(const uint8_t *data, size_t len, uint8_t digest[PC_SHA512_DIGEST_LEN])
One-shot SHA-512: hash len bytes of data into digest (64 bytes).
Definition sha512.cpp:57
void pc_sha512_final(pc_sha512_ctx *ctx, uint8_t digest[PC_SHA512_DIGEST_LEN])
Finalize the hash and write the 64-byte digest. The context is undefined afterwards; call init() agai...
Definition sha512.cpp:47
#define PC_SHA512_DIGEST_LEN
SHA-512 digest length in bytes.
Definition sha512.h:24
Streaming SHA-512 context.
Definition sha512.h:33
mbedtls_sha512_context mbed
mbedtls SHA-512 state (ESP32).
Definition sha512.h:34