krypton-core 0.4.0

A memory-safe, high-performance Rust library for modern file encryption and secure vaults.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! The universal krypton container format.
//!
//! Every encrypted object the library produces — single-file `.krf`
//! containers, vault entry blobs and vault manifests — uses this one
//! envelope. Future object kinds are new trailer payload types, never new
//! formats.
//!
//! # Wire layout
//!
//! ```text
//! b"KRYPTON\0"     8-byte magic (never changes)
//! [u16 LE = 1]     envelope format version
//! [u8 kind]        key-derivation selector:
//!                    0 = content key is HKDF(master, object_salt[32])
//!                    1 = password-derived: argon2 salt[32] +
//!                        object salt[32] + KDF parameters[12]
//! [kind data]      kind-specific bytes (see above)
//! [len u32][ct||tag] …   chunk records (64 KiB plaintext each)
//! [00 00 00 00]    end-of-chunks marker
//! [len u32][ct||tag]     authenticated trailer record
//! ```
//!
//! All header bytes are bound to key derivation: flipping any of them
//! yields a wrong key and authentication fails. The payload type is bound
//! into every chunk and trailer AAD, so records cannot be transplanted
//! between containers of different types.

use std::io::{Read, Write};

use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

use crate::crypto::{self, Key};
use crate::error::{Error, Result};
use crate::kdf::KdfParams;
use crate::stream;

/// Magic header shared by every krypton container.
///
/// The trailing NUL is a control byte: it prevents the magic from occurring
/// in ordinary text and survives naive character-set mangling.
pub const MAGIC: &[u8; 8] = b"KRYPTON\0";

/// Current envelope version. Written into every container; readers reject
/// any other value with [`Error::UnsupportedVersion`].
pub const ENVELOPE_VERSION: u16 = 1;

pub(crate) const SALT_LEN: usize = crate::crypto::SALT_LEN;
const HKDF_INFO: &[u8] = b"krypton-container-v1";
const KIND_HKDF: u8 = 0;
const KIND_PASSWORD: u8 = 1;

/// What a container holds. Serialized into the authenticated trailer and
/// bound into chunk AAD, so records cannot be transplanted between payload
/// types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PayloadType {
    /// Encrypted file contents (standalone `.krf` or vault blob).
    File,
    /// Directory placeholder (vault only); carries no chunks.
    Directory,
    /// Vault index JSON (vault only).
    Manifest,
}

impl PayloadType {
    fn context(self) -> Vec<u8> {
        let mut c = b"krypton-container-v1/".to_vec();
        c.extend_from_slice(match self {
            PayloadType::File => &b"file"[..],
            PayloadType::Directory => &b"directory"[..],
            PayloadType::Manifest => &b"manifest"[..],
        });
        c
    }
}

/// How a container's content key is derived.
#[derive(Debug, Clone)]
pub(crate) enum KeyDerivation {
    /// Content key = HKDF(master key, random per-object salt). Used inside
    /// vaults, where the master key is already unlocked in memory.
    Hkdf {
        /// Random HKDF salt; gives each object an independent subkey.
        object_salt: [u8; SALT_LEN],
    },
    /// Content key = HKDF(Argon2id(password, argon2_salt, params),
    /// object_salt). Used for standalone `.krf` files decryptable from a
    /// password alone.
    Password {
        /// Argon2id salt.
        argon_salt: [u8; SALT_LEN],
        /// Per-object HKDF salt.
        object_salt: [u8; SALT_LEN],
        /// Argon2id cost parameters, stored so defaults can evolve.
        params: KdfParams,
    },
}

impl KeyDerivation {
    fn kind(&self) -> u8 {
        match self {
            KeyDerivation::Hkdf { .. } => KIND_HKDF,
            KeyDerivation::Password { .. } => KIND_PASSWORD,
        }
    }

    fn header_len(&self) -> usize {
        MAGIC.len()
            + 2
            + 1
            + match self {
                KeyDerivation::Hkdf { .. } => SALT_LEN,
                KeyDerivation::Password { .. } => 2 * SALT_LEN + KdfParams::SERIALIZED_LEN,
            }
    }

    fn write_header<W: Write>(&self, w: &mut W) -> Result<()> {
        let mut head = Vec::with_capacity(self.header_len());
        head.extend_from_slice(MAGIC);
        head.extend_from_slice(&ENVELOPE_VERSION.to_le_bytes());
        head.push(self.kind());
        match self {
            KeyDerivation::Hkdf { object_salt } => head.extend_from_slice(object_salt),
            KeyDerivation::Password {
                argon_salt,
                object_salt,
                params,
            } => {
                let mut pbuf = [0u8; KdfParams::SERIALIZED_LEN];
                params.write_to(&mut pbuf);
                head.extend_from_slice(argon_salt);
                head.extend_from_slice(object_salt);
                head.extend_from_slice(&pbuf);
            }
        }
        w.write_all(&head)?;
        Ok(())
    }

    fn parse_header(head: &[u8]) -> Result<Self> {
        if head.len() < MAGIC.len() + 3 || &head[..MAGIC.len()] != MAGIC {
            return Err(retired_or_invalid(head));
        }
        let version = u16::from_le_bytes([head[8], head[9]]);
        if version != ENVELOPE_VERSION {
            return Err(Error::UnsupportedVersion(u32::from(version)));
        }
        let body = &head[11..];
        match head[10] {
            KIND_HKDF => {
                if body.len() != SALT_LEN {
                    return Err(Error::InvalidHeader);
                }
                let mut object_salt = [0u8; SALT_LEN];
                object_salt.copy_from_slice(body);
                Ok(KeyDerivation::Hkdf { object_salt })
            }
            KIND_PASSWORD => {
                if body.len() != 2 * SALT_LEN + KdfParams::SERIALIZED_LEN {
                    return Err(Error::InvalidHeader);
                }
                let mut argon_salt = [0u8; SALT_LEN];
                argon_salt.copy_from_slice(&body[..SALT_LEN]);
                let mut object_salt = [0u8; SALT_LEN];
                object_salt.copy_from_slice(&body[SALT_LEN..2 * SALT_LEN]);
                let params = KdfParams::read_from(&body[2 * SALT_LEN..])?;
                Ok(KeyDerivation::Password {
                    argon_salt,
                    object_salt,
                    params,
                })
            }
            _ => Err(Error::InvalidHeader),
        }
    }

    fn content_key(
        &self,
        password: Option<&str>,
        master: Option<&Key>,
        identity: &[u8],
    ) -> Result<Key> {
        let mut info = Vec::with_capacity(HKDF_INFO.len() + 1 + identity.len());
        info.extend_from_slice(HKDF_INFO);
        info.extend_from_slice(b"/");
        info.extend_from_slice(identity);
        match self {
            KeyDerivation::Hkdf { object_salt } => {
                let m = master.ok_or(Error::InvalidHeader)?;
                Ok(crypto::derive_subkey(m, object_salt, &info))
            }
            KeyDerivation::Password {
                argon_salt,
                object_salt,
                params,
            } => {
                let pw = password.ok_or(Error::InvalidHeader)?;
                let kek = crypto::derive_key(pw.as_bytes(), argon_salt, *params)?;
                Ok(crypto::derive_subkey(&kek, object_salt, &info))
            }
        }
    }
}

/// Recognizes retired pre-0.4 magics so old files produce a helpful error
/// instead of a generic one. No parsing of retired formats remains.
fn retired_or_invalid(head: &[u8]) -> Error {
    const RETIRED: [&[u8]; 2] = [b"KRYPTON2\n", b"KRYPTON3\n"];
    if RETIRED.iter().any(|m| head.starts_with(m)) {
        Error::RetiredFormat
    } else {
        Error::InvalidHeader
    }
}

/// Authenticated trailer describing the whole container.
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Trailer {
    /// Payload type; also bound into all record AADs.
    pub typ: PayloadType,
    /// Original filename (single-file `.krf` only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Total plaintext length across all chunks.
    pub size: u64,
    /// Number of preceding chunk records.
    pub chunk_count: u32,
}

fn trailer_aad(context: &[u8]) -> Vec<u8> {
    let mut aad = Vec::with_capacity(context.len() + 8);
    aad.extend_from_slice(context);
    aad.extend_from_slice(b"/trailer");
    aad
}

/// Writes a complete container: header, chunks streamed from `src`, then
/// the authenticated trailer. Returns `(plaintext_bytes, chunk_count)`.
///
/// `identity` binds the container to its logical location (e.g. a vault
/// entry path); readers must pass the same value or authentication fails.
/// Pass `b""` for self-contained objects. `src: None` yields chunk-less
/// payloads (directory placeholders). Exactly one of `password`/`master`
/// is required, matching `kd`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn write_container<W: Write>(
    dst: &mut W,
    kd: &KeyDerivation,
    password: Option<&str>,
    master: Option<&Key>,
    typ: PayloadType,
    identity: &[u8],
    src: Option<&mut dyn Read>,
    trailer_name: Option<&str>,
) -> Result<(u64, u32)> {
    let content_key = kd.content_key(password, master, identity)?;
    kd.write_header(dst)?;
    let context = typ.context();

    let (total, count) = match src {
        Some(r) => stream::write_chunks(dst, r, &content_key, &context)?,
        None => (0, 0),
    };

    let mut payload = Zeroizing::new(
        serde_json::to_vec(&Trailer {
            typ,
            name: trailer_name.map(String::from),
            size: total,
            chunk_count: count,
        })
        .map_err(|_| Error::Encryption)?,
    );
    crypto::seal_in_place(
        &stream::TRAILER_NONCE,
        &mut payload,
        &content_key,
        &trailer_aad(&context),
    )?;
    dst.write_all(&stream::CHUNKS_END_MARKER)?;
    dst.write_all(&(payload.len() as u32).to_le_bytes())?;
    dst.write_all(payload.as_slice())?;
    Ok((total, count))
}

/// Reads and authenticates a whole container, streaming plaintext chunks to
/// `sink`. Verifies the trailer against observed bytes and checks the
/// payload type matches `expected`. Returns the trailer.
pub(crate) fn read_container<R: Read>(
    mut inner: R,
    password: Option<&str>,
    master: Option<&Key>,
    expected: PayloadType,
    identity: &[u8],
    sink: impl FnMut(&[u8]) -> Result<()>,
) -> Result<Trailer> {
    let mut first = vec![0u8; MAGIC.len() + 3];
    inner
        .read_exact(&mut first)
        .map_err(|_| Error::InvalidHeader)?;
    if &first[..MAGIC.len()] != MAGIC {
        return Err(retired_or_invalid(&first));
    }
    let extra = match first[10] {
        KIND_HKDF => SALT_LEN,
        KIND_PASSWORD => 2 * SALT_LEN + KdfParams::SERIALIZED_LEN,
        _ => return Err(Error::InvalidHeader),
    };
    let mut rest = vec![0u8; extra];
    inner
        .read_exact(&mut rest)
        .map_err(|_| Error::InvalidHeader)?;
    first.extend_from_slice(&rest);

    let kd = KeyDerivation::parse_header(&first)?;
    let content_key = kd.content_key(password, master, identity)?;
    let context = expected.context();

    let (total, count) = stream::read_chunks(&mut inner, &content_key, &context, sink)?;

    let mut rec = Zeroizing::new(stream::read_record(&mut inner, stream::MAX_TRAILER_LEN)?);
    crypto::open_in_place(
        &stream::TRAILER_NONCE,
        &mut rec,
        &content_key,
        &trailer_aad(&context),
    )?;
    let trailer: Trailer =
        serde_json::from_slice(rec.as_slice()).map_err(|_| Error::MalformedPayload)?;

    if trailer.typ != expected || trailer.size != total || trailer.chunk_count != count {
        return Err(Error::Authentication);
    }

    // Nothing may follow the trailer.
    let mut probe = [0u8; 1];
    match inner.read(&mut probe) {
        Ok(0) => {}
        Ok(_) => return Err(Error::InvalidHeader),
        Err(e) => return Err(e.into()),
    }
    Ok(trailer)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    fn hkdf_kd() -> KeyDerivation {
        KeyDerivation::Hkdf {
            object_salt: [7u8; SALT_LEN],
        }
    }

    fn pw_kd() -> KeyDerivation {
        KeyDerivation::Password {
            argon_salt: [9u8; SALT_LEN],
            object_salt: [8u8; SALT_LEN],
            params: KdfParams {
                m_cost_kib: 8 * 1024,
                t_cost: 1,
                p_cost: 1,
            },
        }
    }

    fn seal(
        kd: &KeyDerivation,
        pw: Option<&str>,
        m: Option<&Key>,
        data: &[u8],
        typ: PayloadType,
    ) -> Vec<u8> {
        let mut out = Vec::new();
        let mut src = Cursor::new(data.to_vec());
        write_container(&mut out, kd, pw, m, typ, b"", Some(&mut src), None).unwrap();
        out
    }

    fn unseal(blob: &[u8], pw: Option<&str>, m: Option<&Key>, typ: PayloadType) -> Trailer {
        let mut collected = Vec::new();
        let t = read_container(Cursor::new(blob.to_vec()), pw, m, typ, b"", |c| {
            collected.extend_from_slice(c);
            Ok(())
        })
        .unwrap();
        assert_eq!(collected.len(), t.size as usize);
        t
    }

    fn unseal_id(
        blob: &[u8],
        pw: Option<&str>,
        m: Option<&Key>,
        typ: PayloadType,
        identity: &[u8],
    ) -> Result<Trailer> {
        read_container(Cursor::new(blob.to_vec()), pw, m, typ, identity, |_| Ok(()))
    }

    #[test]
    fn roundtrip_both_kinds_and_types() {
        let master = Key::generate();
        let cases: [(KeyDerivation, Option<&str>, Option<&Key>, PayloadType); 3] = [
            (pw_kd(), Some("pw"), None, PayloadType::File),
            (hkdf_kd(), None, Some(&master), PayloadType::Manifest),
            (hkdf_kd(), None, Some(&master), PayloadType::Directory),
        ];
        for (kd, pw, m, typ) in cases {
            for data in [&b""[..], b"hello", &[42u8; 200_000][..]] {
                let blob = seal(&kd, pw, m, data, typ);
                let t = unseal(&blob, pw, m, typ);
                assert_eq!(t.typ, typ);
                assert_eq!(t.size, data.len() as u64);
            }
        }
    }

    #[test]
    fn wrong_payload_type_rejected() {
        let blob = seal(
            &hkdf_kd(),
            None,
            Some(&Key::generate()),
            b"data",
            PayloadType::File,
        );
        assert!(read_container(
            Cursor::new(blob),
            None,
            Some(&Key::generate()),
            PayloadType::Manifest,
            b"",
            |_| Ok(())
        )
        .is_err());
    }

    #[test]
    fn wrong_password_rejected() {
        let blob = seal(&pw_kd(), Some("pw"), None, b"data", PayloadType::File);
        assert!(matches!(
            read_container(
                Cursor::new(blob.clone()),
                Some("nope"),
                None,
                PayloadType::File,
                b"",
                |_| Ok(())
            ),
            Err(Error::Authentication)
        ));
    }

    #[test]
    fn tampered_byte_rejected_everywhere() {
        let blob = seal(
            &pw_kd(),
            Some("pw"),
            None,
            b"important data",
            PayloadType::File,
        );
        for pos in [0usize, 12, 40, blob.len() - 30, blob.len() - 1] {
            let mut corrupt = blob.clone();
            corrupt[pos] ^= 0x01;
            assert!(
                read_container(
                    Cursor::new(corrupt),
                    Some("pw"),
                    None,
                    PayloadType::File,
                    b"",
                    |_| Ok(())
                )
                .is_err(),
                "tamper at byte {pos} was not detected"
            );
        }
    }

    #[test]
    fn truncation_and_extension_rejected() {
        let blob = seal(&pw_kd(), Some("pw"), None, &[1u8; 500], PayloadType::File);
        assert!(read_container(
            Cursor::new(blob[..blob.len() - 1].to_vec()),
            Some("pw"),
            None,
            PayloadType::File,
            b"",
            |_| Ok(())
        )
        .is_err());
        let mut ext = blob.clone();
        ext.push(0);
        assert!(read_container(
            Cursor::new(ext),
            Some("pw"),
            None,
            PayloadType::File,
            b"",
            |_| Ok(())
        )
        .is_err());
    }

    #[test]
    fn retired_magics_reported() {
        let err = read_container(
            Cursor::new(b"KRYPTON3\nrest".to_vec()),
            None,
            None,
            PayloadType::File,
            b"",
            |_| Ok(()),
        )
        .unwrap_err();
        assert!(matches!(err, Error::RetiredFormat));
    }

    #[test]
    fn bad_version_rejected() {
        let mut blob = seal(
            &hkdf_kd(),
            None,
            Some(&Key::generate()),
            b"x",
            PayloadType::Manifest,
        );
        blob[8] = 99; // envelope version low byte
        assert!(matches!(
            read_container(
                Cursor::new(blob),
                None,
                Some(&Key::generate()),
                PayloadType::Manifest,
                b"",
                |_| Ok(())
            ),
            Err(Error::UnsupportedVersion(99))
        ));
    }

    #[test]
    fn wrong_identity_rejected() {
        let master = Key::generate();
        let blob = seal(&hkdf_kd(), None, Some(&master), b"data", PayloadType::File);
        // Whole-container transplant to a different logical location.
        assert!(unseal_id(
            &blob,
            None,
            Some(&master),
            PayloadType::File,
            b"other-entry"
        )
        .is_err());
        // Correct location still works and returns the trailer.
        assert!(unseal_id(&blob, None, Some(&master), PayloadType::File, b"").is_ok());
    }
}