1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! # libvctrl_sha512
//!
//! A **self‑contained**, **zero‑dependency**, `#![no_std]` Rust implementation of the
//! SHA‑512 cryptographic hash function, the HMAC‑SHA‑512 message authentication code,
//! and the HKDF‑SHA‑512 key derivation function (RFC 5869). An optional `sha384`
//! feature adds SHA‑384, HMAC‑SHA‑384, and HKDF‑SHA‑384.
//!
//! This crate is built as a trusted foundation for cryptographic operations in
//! resource‑constrained or bare‑metal environments, as well as in standard
//! applications that require **auditable**, **minimal dependency** code.
//!
//! ## Why this crate?
//!
//! - **Zero external dependencies** – only the Rust `core` library, no `std`,
//! no `alloc`. This minimises the attack surface and simplifies supply‑chain
//! audits.
//! - **FIPS 180‑4 / RFC 2104 / RFC 5869 compliant** – output matches official
//! test vectors.
//! - **Constant‑time where it matters** – MAC and hash verification use a
//! data‑independent comparison with a volatile read barrier to prevent
//! timing side‑channels.
//! - **Memory zeroisation** – secret key material is overwritten in memory
//! after use, both in one‑shot and streaming APIs.
//! - **`#![no_std]` compatible** – works on bare‑metal, kernels, and
//! WebAssembly.
//! - **Optional SHA‑384** – feature‑gated, shares the same compression
//! function.
//!
//! ## Compliance & Auditing
//!
//! The implementation has undergone a security audit (v0.2.0) with all findings
//! addressed in this release. Key improvements over the original fork include:
//!
//! - **FIPS 180‑4 compliant message padding** – the 128‑bit big‑endian length is
//! written in full, with the upper 64 bits zeroed, ensuring correctness for
//! messages up to 2<sup>128</sup>‑1 bits (well beyond any practical limit).
//! - **PRK length validation** – `HKDF::expand` asserts that the supplied PRK
//! is exactly the required length (64 bytes for SHA‑512, 48 bytes for SHA‑384),
//! preventing silent misuse.
//! - **Edition 2021** – the crate uses a stable Rust edition, guaranteeing
//! compatibility with current toolchains.
//! - **Idiomatic endian‑handling** – `load_be`/`store_be` use the built‑in
//! `u64::from_be_bytes`/`to_be_bytes`, improving readability and optimizer
//! friendliness.
//! - **Memory zeroisation** – HMAC instances wipe padded keys from the stack on
//! drop, and the one‑shot `mac` function clears temporary key material.
//!
//! All cryptographic primitives produce output that matches the standard test
//! vectors (e.g., RFC 4231, RFC 5869, FIPS 180‑4 examples).
//!
//! ## Core components
//!
//! ### SHA‑512
//! The [`sha512`] module provides the fundamental hash function. It supports:
//! - **One‑shot hashing** via `Hash::hash(data) -> [u8; 64]`
//! - **Streaming hashing** with `Hash::new()`, `update()`, `finalize()`
//! - **Constant‑time verification** with `Hash::verify(expected)`
//!
//! ### HMAC‑SHA‑512
//! The [`hmac`] module implements RFC 2104 using SHA‑512. All MAC comparisons
//! use a timing‑attack resistant equality check. The API includes:
//! - `HMAC::mac(data, key)` for one‑shot MAC generation.
//! - `HMAC::new(key)` / `update()` / `finalize()` for incremental processing.
//! - `HMAC::verify(data, key, expected)` and `finalize_verify(expected)` for
//! constant‑time verification.
//!
//! ### HKDF‑SHA‑512
//! The [`hkdf`] module implements the HMAC‑based Key Derivation Function (RFC 5869).
//! It follows the **extract‑then‑expand** paradigm:
//! - `HKDF::extract(salt, ikm) -> [u8; 64]` produces a pseudorandom key (PRK).
//! - `HKDF::expand(out, prk, info)` expands the PRK into arbitrary‑length output
//! keying material (OKM).
//!
//! ### SHA‑384 (optional)
//! When the `sha384` feature is enabled (default), the [`sha384`] module provides
//! analogous types for SHA‑384, HMAC‑SHA‑384, and HKDF‑SHA‑384. These share the
//! same underlying compression function as SHA‑512 but differ in the initial vector
//! and output size (48 bytes).
//!
//! ## Design principles
//!
//! - **No standard library** – the crate only uses `core`, making it suitable for
//! embedded systems, kernels, and WebAssembly. It avoids heap allocation entirely.
//! - **Minimal trusted code base** – no external dependencies beyond `core`,
//! reducing supply‑chain risk and audit surface.
//! - **Constant‑time comparisons** – the [`utils::verify`] function compares byte
//! slices in a data‑independent loop with a volatile read barrier, preventing
//! compiler optimizations that could leak timing information. This is used for
//! all MAC and hash verifications.
//! - **Zeroisation** – material derived from secret keys is explicitly overwritten
//! when no longer needed. `HMAC::mac` zeroises its temporary buffers; `Drop`
//! implementations clear the padded key.
//! - **Thread safety** – the `Hash` and `HMAC` types are `Copy` (or consume on
//! finalize) and contain no interior mutability; they are safe to move between
//! threads.
//!
//! ## Quick start examples
//!
//! ### SHA‑512 hashing
//! ```rust
//! use libvctrl_sha512::Hash;
//!
//! // One‑shot
//! let digest = Hash::hash(b"hello world");
//! assert_eq!(digest.len(), 64);
//!
//! // Streaming
//! let mut hasher = Hash::new();
//! hasher.update(b"hello ");
//! hasher.update(b"world");
//! let d = hasher.finalize();
//! assert_eq!(d, digest);
//!
//! // Constant‑time verification
//! let mut verifier = Hash::new();
//! verifier.update(b"hello world");
//! assert!(verifier.verify(&digest));
//! ```
//!
//! ### HMAC‑SHA‑512
//! ```rust
//! use libvctrl_sha512::HMAC;
//!
//! let mac = HMAC::mac(b"message", b"secret-key");
//! assert!(HMAC::verify(b"message", b"secret-key", &mac));
//!
//! // Streaming
//! let mut hmac = HMAC::new(b"secret-key");
//! hmac.update(b"first part ");
//! hmac.update(b"second part");
//! let m = hmac.finalize();
//! ```
//!
//! ### HKDF‑SHA‑512
//! ```rust
//! use libvctrl_sha512::HKDF;
//!
//! let ikm = b"input-key-material";
//! let salt = b"optional-salt";
//! let info = b"context-info";
//!
//! let prk = HKDF::extract(salt, ikm);
//! let mut out = [0u8; 32];
//! HKDF::expand(&mut out, prk, info);
//! // `out` is now 32 bytes of derived key material.
//! ```
//!
//! ## Feature flags
//!
//! - **`sha384`** *(enabled by default)* – activates the `sha384` module
//! containing SHA‑384, HMAC‑SHA‑384, and HKDF‑SHA‑384.
//! - **`opt_size`** – trades some performance for reduced binary size. The
//! `expand` and compression functions are marked `inline(never)`, yielding
//! roughly 75% smaller code at a ~16% speed penalty.
//!
//! ## Minimum Supported Rust Version
//!
//! This crate requires Rust **1.70** or later. The `edition = "2021"` setting
//! ensures compatibility with current stable releases.
//!
//! ## License
//!
//! Licensed under the ISC License, the same as the original
//! [hmac-sha512](https://github.com/jedisct1/rust-hmac-sha512) from which this
//! crate is derived.
//!
//! ## Acknowledgement
//!
//! This project is a fork and modularisation of Frank Denis's excellent
//! [`hmac-sha512`](https://github.com/jedisct1/rust-hmac-sha512) crate. All core
//! cryptographic logic originates from his work.
pub use HKDF;
pub use HMAC;
pub use Hash;
pub use ;