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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! # hefesto
//!
//! Double envelope encryption for multi-tenant applications.
//!
//! Each value is encrypted with two independent keys — a per-tenant key and a
//! global master key — so a breach of either key alone does not expose
//! plaintext. The outer layer also binds the ciphertext to the tenant via AEAD
//! additional data, preventing cross-tenant replay.
//!
//! ## Security model
//!
//! ```text
//! plaintext
//! └─▶ AES-256-GCM(key = Argon2id(tenant_key, salt₁)) ← inner layer
//! └─▶ AES-256-GCM(key = Argon2id(master_key, salt₂),
//! aad = tenant_key) ← outer layer
//! └─▶ base64(version ‖ salt₁ ‖ salt₂ ‖ ciphertext)
//! ```
//!
//! | Property | Mechanism |
//! |---|---|
//! | Confidentiality | AES-256-GCM (two layers) |
//! | Key hardening | Argon2id (64 MiB, 3 iterations) |
//! | Non-determinism | 96-bit random nonce per encryption |
//! | Tenant isolation | `tenant_key` used as AAD on outer layer |
//! | Memory safety | `zeroize` zeroes key material on drop |
//!
//! ## Quick start
//!
//! ```rust
//! use hefesto::{encrypt, decrypt, hash_password, verify_password, hash_for_lookup};
//!
//! let tenant_key = "per_tenant_secret_key_32bytes___";
//! let master_key = "global_master_key_32bytes_______";
//!
//! // Encrypt and decrypt a sensitive value
//! let ciphertext = encrypt("user@example.com", tenant_key, master_key).unwrap();
//! let plaintext = decrypt(&ciphertext, tenant_key, master_key).unwrap();
//! assert_eq!(plaintext, "user@example.com");
//!
//! // Hash a password for storage (Argon2id PHC string)
//! let hash = hash_password("s3cr3t").unwrap();
//! assert!(verify_password("s3cr3t", &hash));
//!
//! // Deterministic lookup token — same value + same tenant_key → same hash
//! let token = hash_for_lookup("user@example.com", tenant_key).unwrap();
//! assert_eq!(token, hash_for_lookup("user@example.com", tenant_key).unwrap());
//! ```
//!
//! ## Minimum key length
//!
//! Both `tenant_key` and `master_key` must be at least **16 bytes**. Shorter
//! keys are rejected with [`HefestoError::InvalidKey`]. Keys of 32+ bytes are
//! strongly recommended in production.
//!
//! ## Payload versioning
//!
//! The ciphertext payload begins with a version byte (`0x02`). Payloads from
//! hefesto v1.x (version byte `0x01`) are not compatible and will be rejected.
//!
//! ## Feature flags
//!
//! This crate has no optional features. All primitives are always compiled.
pub use HefestoError;
use Result;
/// Minimum byte length enforced on both `tenant_key` and `master_key`.
///
/// 16 bytes gives 128 bits of key material before Argon2id hardening. Keys of
/// 32+ bytes (256 bits) are strongly recommended in production.
const MIN_KEY_LEN: usize = 16;
/// Encrypts `value` using double envelope encryption.
///
/// Applies two independent AES-256-GCM layers — first with a key derived from
/// `tenant_key`, then with a key derived from `master_key`. The outer layer
/// uses `tenant_key` as additional authenticated data (AAD), binding the
/// ciphertext to this tenant.
///
/// Each call produces a different ciphertext because both layers use a fresh
/// random nonce and salt.
///
/// # Arguments
///
/// * `value` — plaintext to encrypt (any UTF-8 string, including empty)
/// * `tenant_key` — per-tenant secret; minimum 8 bytes
/// * `master_key` — global master secret; minimum 8 bytes
///
/// # Returns
///
/// Base64-encoded payload: `version(1) ‖ salt₁(16) ‖ salt₂(16) ‖ ciphertext`.
///
/// # Errors
///
/// * [`HefestoError::InvalidKey`] — either key is shorter than 8 bytes
/// * [`HefestoError::KeyDerivationFailed`] — Argon2id parameterisation failed
/// * [`HefestoError::EncryptionFailed`] — AES-GCM encryption error
///
/// # Examples
///
/// ```rust
/// let ct = hefesto::encrypt("secret", "tenant_secret_key_32", "master_secret_key_32").unwrap();
/// assert!(!ct.is_empty());
///
/// // Non-deterministic: two calls with the same inputs produce different ciphertexts
/// let ct2 = hefesto::encrypt("secret", "tenant_secret_key_32", "master_secret_key_32").unwrap();
/// assert_ne!(ct, ct2);
/// ```
/// Decrypts a ciphertext produced by [`encrypt`].
///
/// Reverses the double envelope: first strips the outer AES-256-GCM layer
/// (keyed from `master_key`, with `tenant_key` as AAD), then strips the inner
/// layer (keyed from `tenant_key`).
///
/// # Arguments
///
/// * `ciphertext` — base64-encoded payload from [`encrypt`]
/// * `tenant_key` — per-tenant secret used during encryption
/// * `master_key` — global master secret used during encryption
///
/// # Errors
///
/// * [`HefestoError::InvalidKey`] — either key is shorter than 8 bytes
/// * [`HefestoError::InvalidPayload`] — not valid base64 or version byte mismatch
/// * [`HefestoError::PayloadTooShort`] — payload is too short to be valid
/// * [`HefestoError::DecryptionFailed`] — wrong key, wrong tenant, or tampered data
/// * [`HefestoError::InvalidUtf8`] — decrypted bytes are not valid UTF-8
///
/// # Examples
///
/// ```rust
/// let ct = hefesto::encrypt("hello", "tenant_secret_key_32", "master_secret_key_32").unwrap();
/// let pt = hefesto::decrypt(&ct, "tenant_secret_key_32", "master_secret_key_32").unwrap();
/// assert_eq!(pt, "hello");
/// ```
///
/// Wrong keys are rejected:
///
/// ```rust
/// let ct = hefesto::encrypt("hello", "tenant_secret_key_32", "master_secret_key_32").unwrap();
/// assert!(hefesto::decrypt(&ct, "wrong_tenant_key_32_", "master_secret_key_32").is_err());
/// assert!(hefesto::decrypt(&ct, "tenant_secret_key_32", "wrong_master_key_32_").is_err());
/// ```
/// Hashes a password using Argon2id and returns a PHC-format string.
///
/// Uses `Argon2::default()` parameters (Argon2id, version 0x13) with a
/// randomly generated salt. The output is a self-describing PHC string that
/// can be stored directly and passed to [`verify_password`].
///
/// Each call produces a different hash because the salt is random.
///
/// # Errors
///
/// * [`HefestoError::PasswordHashFailed`] — Argon2 hashing failed
///
/// # Examples
///
/// ```rust
/// let hash = hefesto::hash_password("my_password").unwrap();
/// assert!(hash.starts_with("$argon2id$"));
/// assert!(hefesto::verify_password("my_password", &hash));
/// ```
/// Verifies a password against an Argon2id PHC hash produced by [`hash_password`].
///
/// Returns `true` when the password matches the hash, `false` for any mismatch
/// or if `hash` is not a valid PHC string.
///
/// # Examples
///
/// ```rust
/// let hash = hefesto::hash_password("correct").unwrap();
/// assert!( hefesto::verify_password("correct", &hash));
/// assert!(!hefesto::verify_password("wrong", &hash));
/// assert!(!hefesto::verify_password("correct", "not_a_valid_hash"));
/// ```
/// Computes a deterministic, tenant-scoped lookup token for a plaintext value.
///
/// Uses HMAC-SHA256 keyed with `tenant_key`. The output is a lowercase hex
/// string (64 characters). Because the same `tenant_key` always produces the
/// same token for the same `value`, this can be used to build a searchable
/// index over encrypted data (e.g., look up a user by email without decrypting
/// every row).
///
/// Two tenants with the same plaintext value produce different tokens, so
/// cross-tenant correlation is not possible from tokens alone.
///
/// # Arguments
///
/// * `value` — plaintext to hash (e.g., an email address)
/// * `tenant_key` — per-tenant secret that scopes the token; minimum 16 bytes
///
/// # Returns
///
/// 64-character lowercase hex string (256-bit HMAC-SHA256 output).
///
/// # Errors
///
/// * [`HefestoError::InvalidKey`] — `tenant_key` is shorter than 16 bytes
///
/// # Examples
///
/// ```rust
/// let token = hefesto::hash_for_lookup("user@example.com", "tenant_secret_key_32").unwrap();
///
/// // Deterministic: same inputs → same token
/// assert_eq!(token, hefesto::hash_for_lookup("user@example.com", "tenant_secret_key_32").unwrap());
///
/// // Tenant-scoped: different tenant → different token
/// assert_ne!(token, hefesto::hash_for_lookup("user@example.com", "other_tenant_key_32_").unwrap());
/// ```