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
//! # HKDF-SHA512 (RFC 5869)
//!
//! HMAC-based Key Derivation Function (HKDF) using SHA‑512 as the underlying hash.
//! This is a key derivation function that transforms any initial keying material
//! (IKM) into one or more cryptographically strong secret keys.
//!
//! ## Overview
//!
//! HKDF follows the **extract‑then‑expand** paradigm:
//!
//! 1. **Extract** – takes the input keying material (IKM) and an optional salt,
//! produces a pseudorandom key (PRK) of fixed length (64 bytes for SHA‑512).
//! 2. **Expand** – takes the PRK, optional context information (info), and
//! desired output length, produces the output keying material (OKM).
//!
//! This separation allows:
//! - **Extract** – to "compress" potentially weak or non‑uniform IKM into a strong key.
//! - **Expand** – to generate multiple keys from the same PRK, for different contexts.
//!
//! ## Security Properties
//!
//! - **PRF‑security**: the PRK is computationally indistinguishable from random,
//! provided the salt is random or the IKM has sufficient entropy.
//! - **Key independence**: outputs for different `info` values are independent.
//! - **Domain separation**: using distinct `info` avoids key reuse across applications.
//! - **Constant‑time**: all operations run in constant time to resist timing attacks.
//!
//! ## Usage Examples
//!
//! ### Basic key derivation
//! ```
//! use libvctrl_sha512::HKDF;
//!
//! // Input keying material (IKM) – can be a password, shared secret, etc.
//! let ikm = b"my-secret-input-material";
//!
//! // Salt (optional) – should be random but can be static; if omitted, empty salt is used.
//! let salt = b"random-salt-value";
//!
//! // Extract phase: derive a pseudorandom key (PRK)
//! let prk = HKDF::extract(salt, ikm);
//!
//! // Info (optional) – context string to separate different uses.
//! let info = b"encryption-key-v1";
//!
//! // Expand phase: generate 32 bytes of output keying material (OKM)
//! let mut okm = [0u8; 32];
//! HKDF::expand(&mut okm, prk, info);
//!
//! // Use `okm` as your AES‑256 key, etc.
//! ```
//!
//! ### Generating multiple keys from one PRK
//! ```
//! use libvctrl_sha512::HKDF;
//!
//! let ikm = b"master-key";
//! let salt = b"app-salt";
//! let prk = HKDF::extract(salt, ikm);
//!
//! let mut enc_key = [0u8; 32];
//! let mut mac_key = [0u8; 64];
//!
//! // Derive separate keys for encryption and MAC
//! HKDF::expand(&mut enc_key, prk, b"encryption");
//! HKDF::expand(&mut mac_key, prk, b"mac");
//! ```
//!
//! ### With empty salt and info
//! ```
//! use libvctrl_sha512::HKDF;
//!
//! let ikm = b"shared-secret";
//! let prk = HKDF::extract([], ikm);
//! let mut okm = [0u8; 48];
//! HKDF::expand(&mut okm, prk, []);
//! // OKM is now 48 bytes of derived key material.
//! ```
//!
//! ## Panics
//!
//! The `expand` function panics if the requested output length exceeds
//! `255 * 64 = 16320` bytes, as mandated by RFC 5869.
//!
//! ## When to use HKDF
//!
//! - Deriving keys from Diffie‑Hellman shared secrets.
//! - Deriving multiple keys from a master secret.
//! - Key stretching (though for passwords, consider Argon2 or PBKDF2).
//! - Domain‑specific key derivation (e.g., separate keys for encryption and signing).
//!
//! ## Security Notes
//!
//! - The **salt** should be random and non‑secret; using a fixed salt is allowed
//! but reduces security when IKM is weak.
//! - The **info** string should be unique per context; it provides domain separation.
//! - Never reuse a PRK with different `info` values that could collide.
//! - HKDF does **not** perform key stretching; for low‑entropy inputs (passwords),
//! combine with a KDF like Argon2.
//!
//! ## References
//!
//! - [RFC 5869 – HMAC‑based Extract‑and‑Expand Key Derivation Function (HKDF)](https://datatracker.ietf.org/doc/html/rfc5869)
//! - [NIST SP 800‑56C](https://csrc.nist.gov/publications/detail/sp/800-56c/rev-2/final)
use crateHMAC;
/// HKDF‑SHA512 implementation.
///
/// This struct holds no state; all methods are stateless.
/// It follows the HKDF specification (RFC 5869) using SHA‑512 as the hash function.
;