ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sha512.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_SHA512_H
5#define PROTOCORE_SHA512_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
10
11/**
12 * @file sha512.h
13 * @brief SHA-512 (FIPS 180-4) - streaming and one-shot digest.
14 *
15 * The shared SHA-512 primitive for the whole library (SSH Ed25519 / kex hashing, PQC, SMB 3.1.1
16 * preauth integrity). The entries below are one surface over both arms: a part with a hashing
17 * peripheral compresses on it, a part without runs the FIPS 180-4 rounds. Mirrors the sha256
18 * structure.
19 *
20 * @ref Sha512Ns::final leaves the running digest where it was: the padded blocks compress into a copy
21 * of the state, so the hash keeps taking data afterwards. That is what lets SSH read the exchange
22 * hash at every stage the key exchange asks for without snapshotting anything.
23 *
24 * @c work is PROTOCORE_SHA512_BORROW secure bytes the CALLER took, at an address it knows. It is not held past the
25 * call, so nothing here aliases it. The caller releases it, and the pool wipes on release; this module neither takes
26 * it, holds it, releases it, nor wipes it. The borrow IS the digest, so two running hashes are two borrows and never
27 * collide.
28 *
29 * @author Douglas Quigg (dstroy0)
30 * @date 2026
31 */
32
33/** @brief SHA-512 digest length in bytes. */
34#define PROTOCORE_SHA512_DIGEST_LEN 64
35
36/** @brief SHA-512 block size in bytes. */
37#define PROTOCORE_SHA512_BLOCK_LEN 128
38
39/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
40typedef struct
41{
42 proto_bool (*init)(uint8_t *);
43 proto_bool (*update)(uint8_t *, const uint8_t *, size_t);
44 proto_bool (*final)(uint8_t *, uint8_t *);
45 proto_bool (*hash)(uint8_t *, const uint8_t *, size_t, uint8_t *);
46} Sha512Ns;
47PROTOCORE_NS_LAYOUT(Sha512Ns, init, update, final, hash);
48
49/**
50 * @brief Start a digest.
51 * @param work PROTOCORE_SHA512_BORROW bytes the caller took. Not held past the call.
52 * @return PROTO_TRUE on success.
53 */
55/**
56 * @brief Feed the running digest a chunk.
57 * @param work PROTOCORE_SHA512_BORROW bytes the caller took. Not held past the call.
58 * @param data the bytes
59 * @param len how many
60 * @return PROTO_TRUE on success.
61 */
62proto_bool protocore_sha512_update(uint8_t *work, const uint8_t *data, size_t len);
63/**
64 * @brief Pad, compress the last block, write the 64 bytes out.
65 * @param work PROTOCORE_SHA512_BORROW bytes the caller took. Not held past the call.
66 * @param out PROTOCORE_SHA512_DIGEST_LEN bytes
67 * @return PROTO_TRUE on success.
68 */
69proto_bool protocore_sha512_final(uint8_t *work, uint8_t *out);
70/**
71 * @brief Init, update and final in one call, for a message already whole.
72 * @param work PROTOCORE_SHA512_BORROW bytes the caller took. Not held past the call.
73 * @param data the message
74 * @param len its length
75 * @param out PROTOCORE_SHA512_DIGEST_LEN bytes
76 * @return PROTO_TRUE on success.
77 */
78proto_bool protocore_sha512_hash(uint8_t *work, const uint8_t *data, size_t len, uint8_t *out);
79
80/** @brief Module namespace. */
85
87
88#endif // PROTOCORE_SHA512_H
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
proto_bool protocore_sha512_update(uint8_t *work, const uint8_t *data, size_t len)
Feed the running digest a chunk.
proto_bool protocore_sha512_final(uint8_t *work, uint8_t *out)
Pad, compress the last block, write the 64 bytes out.
PROTOCORE_NS Sha512Ns Sha512 PROTOCORE_UNUSED
Module namespace.
Definition sha512.h:81
proto_bool protocore_sha512_init(uint8_t *work)
Start a digest.
proto_bool protocore_sha512_hash(uint8_t *work, const uint8_t *data, size_t len, uint8_t *out)
Init, update and final in one call, for a message already whole.
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition sha512.h:41
proto_bool(* init)(uint8_t *)
Definition sha512.h:42
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97