encryptman-keyring 0.1.0

OS keychain-backed master key storage for encryptman
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
//! # encryptman-keyring
//!
//! OS keychain-backed master key storage for
//! [encryptman](https://crates.io/crates/encryptman).
//!
//! This crate eliminates the need to manage raw key files by storing the
//! master key in the operating system's native credential store:
//!
//! - **Windows** — Credential Manager
//! - **macOS** — Keychain Services
//! - **Linux** — Secret Service (DBus)
//!
//! ## Quick Start
//!
//! ```no_run
//! use encryptman_keyring::Vault;
//!
//! // First call: generates a new master key and stores it in the OS keychain.
//! // Subsequent calls: loads the existing key from the keychain.
//! let vault = Vault::new("my-app").unwrap();
//!
//! let encrypted = vault.encrypt("my_database_password").unwrap();
//! let decrypted = vault.decrypt(&encrypted).unwrap();
//!
//! assert_eq!(decrypted, "my_database_password");
//!
//! // Delete the key from the keychain when no longer needed
//! Vault::delete("my-app").unwrap();
//! ```
//!
//! ## Design
//!
//! ```text
//! Vault::new("my-app")
//!//!     ├── keyring::Entry::new("my-app", "master-key")
//!     │       │
//!     │       ├── get_secret() → OK → MasterKey::from_bytes()
//!     │       └── get_secret() → NoEntry → generate + set_secret()
//!//!     └── encryptman::encrypt / decrypt using the master key
//! ```
//!
//! The `service` name passed to [`Vault::new`] is used as both the keyring
//! service identifier **and** the HKDF context for encryptman, providing
//! domain isolation between different applications.
//!
//! ## Migration from file-based keys
//!
//! Use [`Vault::migrate_from_file`] to import an existing `.tb_key` file
//! into the OS keychain and delete the file:
//!
//! ```no_run
//! use encryptman_keyring::Vault;
//!
//! let vault = Vault::migrate_from_file("my-app", std::path::Path::new("/path/to/.tb_key")).unwrap();
//! ```
//!
//! ## When NOT to use this crate
//!
//! - **Headless / CI environments** — the OS keychain may not be available.
//!   Use file-based key storage instead.
//! - **Multi-user servers** — keyring entries are per-user; consider a
//!   shared secret store like Vault or AWS Secrets Manager.

use encryptman::MasterKey;
use thiserror::Error;

/// The keyring username used to store the master key.
const KEY_USERNAME: &str = "master-key";

/// Errors that can occur during vault operations.
#[derive(Debug, Error)]
pub enum Error {
    /// The OS keychain returned an error.
    #[error("keychain error: {0}")]
    Keychain(#[from] keyring::Error),

    /// The master key stored in the keychain is corrupted or has the wrong length.
    #[error("invalid master key in keychain: expected 32 bytes, got {0}")]
    InvalidKeyLength(usize),

    /// A file-based migration source could not be read.
    #[error("failed to read key file: {0}")]
    FileRead(#[from] std::io::Error),

    /// The file-based key has the wrong length.
    #[error("invalid key file: expected 32 bytes, got {0}")]
    InvalidFileKeyLength(usize),

    /// Encryption or decryption failed.
    #[error("crypto error: {0}")]
    Crypto(#[from] encryptman::CryptoError),
}

/// A vault that stores its master key in the OS keychain and delegates
/// encryption/decryption to `encryptman`.
///
/// Each `Vault` instance is bound to a **service name** (and optionally a
/// **target username**) that identifies the keychain entry. The same service
/// name is also used as the HKDF context in `encryptman`, so different
/// service names produce different encryption keys from the same underlying
/// keychain entry.
///
/// # Examples
///
/// ```no_run
/// use encryptman_keyring::Vault;
///
/// let vault = Vault::new("my-app").unwrap();
/// let ct = vault.encrypt("secret").unwrap();
/// let pt = vault.decrypt(&ct).unwrap();
/// assert_eq!(pt, "secret");
/// Vault::delete("my-app").unwrap();
/// ```
pub struct Vault {
    service: String,
    master_key: MasterKey,
}

impl Vault {
    /// Create or open a vault with the given service name.
    ///
    /// On first call, a new random master key is generated and stored in the
    /// OS keychain. On subsequent calls, the existing key is loaded.
    ///
    /// The `service` is used as the keyring service name and as the encryptman
    /// HKDF context (`"encryptman:{service}"`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Keychain`] if the OS keychain is unavailable.
    pub fn new(service: &str) -> Result<Self, Error> {
        Self::new_with_target(service, KEY_USERNAME)
    }

    /// Create or open a vault with a custom target (username) in the keyring.
    ///
    /// This is useful when multiple independent vaults are needed within the
    /// same service namespace.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Keychain`] if the OS keychain is unavailable.
    pub fn new_with_target(service: &str, target: &str) -> Result<Self, Error> {
        let entry = keyring::Entry::new(service, target)?;
        let master_key = match entry.get_secret() {
            Ok(bytes) => MasterKey::try_from(bytes.as_slice())
                .map_err(|_| Error::InvalidKeyLength(bytes.len()))?,
            Err(keyring::Error::NoEntry) => {
                let key = MasterKey::generate();
                entry.set_secret(key.as_bytes())?;
                key
            }
            Err(e) => return Err(e.into()),
        };
        Ok(Self {
            service: service.to_string(),
            master_key,
        })
    }

    /// Migrate a file-based key into the OS keychain.
    ///
    /// Reads a 32-byte key from `key_path`, stores it in the keychain under
    /// the given `service` name, and deletes the file on success.
    ///
    /// Returns the vault ready for use.
    ///
    /// # Errors
    ///
    /// - [`Error::FileRead`] if the file cannot be read.
    /// - [`Error::InvalidFileKeyLength`] if the file is not exactly 32 bytes.
    /// - [`Error::Keychain`] if the OS keychain is unavailable.
    pub fn migrate_from_file(service: &str, key_path: &std::path::Path) -> Result<Self, Error> {
        Self::migrate_from_file_with_target(service, KEY_USERNAME, key_path)
    }

    /// Migrate a file-based key with a custom target (username).
    ///
    /// See [`Vault::migrate_from_file`] for details.
    pub fn migrate_from_file_with_target(
        service: &str,
        target: &str,
        key_path: &std::path::Path,
    ) -> Result<Self, Error> {
        let raw = std::fs::read(key_path)?;
        if raw.len() != 32 {
            return Err(Error::InvalidFileKeyLength(raw.len()));
        }
        let mut bytes = [0u8; 32];
        bytes.copy_from_slice(&raw);

        let entry = keyring::Entry::new(service, target)?;
        entry.set_secret(&bytes)?;

        // Delete the file after successful migration
        std::fs::remove_file(key_path)?;

        let master_key = MasterKey::from_bytes(bytes);
        Ok(Self {
            service: service.to_string(),
            master_key,
        })
    }

    /// Encrypt a plaintext string using the vault's master key.
    ///
    /// Delegates to `encryptman::encrypt`. Each call produces a unique
    /// ciphertext (random nonce).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if encryption fails.
    pub fn encrypt(&self, plaintext: &str) -> Result<String, Error> {
        Ok(encryptman::encrypt(&self.master_key, plaintext)?)
    }

    /// Decrypt a ciphertext string using the vault's master key.
    ///
    /// Delegates to `encryptman::decrypt`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if decryption fails (wrong key, corrupted
    /// data, or invalid base64).
    pub fn decrypt(&self, ciphertext: &str) -> Result<String, Error> {
        Ok(encryptman::decrypt(&self.master_key, ciphertext)?)
    }

    /// Encrypt with a custom HKDF context.
    ///
    /// The `context` is appended to `"encryptman:"` to derive a
    /// domain-specific AES key from the master key.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if encryption fails.
    pub fn encrypt_with_context(&self, context: &str, plaintext: &str) -> Result<String, Error> {
        Ok(encryptman::encrypt_with_context(
            &self.master_key,
            context,
            plaintext,
        )?)
    }

    /// Decrypt with a custom HKDF context.
    ///
    /// The `context` must match the one used during encryption.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if decryption fails.
    pub fn decrypt_with_context(&self, context: &str, ciphertext: &str) -> Result<String, Error> {
        Ok(encryptman::decrypt_with_context(
            &self.master_key,
            context,
            ciphertext,
        )?)
    }

    /// Return a reference to the underlying master key.
    ///
    /// This is useful when you need direct access to the key for advanced
    /// use cases (e.g., custom encryption contexts or binary data).
    pub fn master_key(&self) -> &MasterKey {
        &self.master_key
    }

    /// Encrypt arbitrary bytes using the vault's master key.
    ///
    /// Delegates to `encryptman::encrypt_bytes_with_context` using the
    /// vault's service name as context.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if encryption fails.
    pub fn encrypt_bytes(&self, plaintext: &[u8]) -> Result<Vec<u8>, Error> {
        Ok(encryptman::encrypt_bytes_with_context(
            &self.master_key,
            &self.service,
            plaintext,
        )?)
    }

    /// Decrypt arbitrary bytes using the vault's master key.
    ///
    /// Delegates to `encryptman::decrypt_bytes_with_context` using the
    /// vault's service name as context.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if decryption fails.
    pub fn decrypt_bytes(&self, packed: &[u8]) -> Result<Vec<u8>, Error> {
        Ok(encryptman::decrypt_bytes_with_context(
            &self.master_key,
            &self.service,
            packed,
        )?)
    }

    /// Encrypt arbitrary bytes with a custom HKDF context.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if encryption fails.
    pub fn encrypt_bytes_with_context(
        &self,
        context: &str,
        plaintext: &[u8],
    ) -> Result<Vec<u8>, Error> {
        Ok(encryptman::encrypt_bytes_with_context(
            &self.master_key,
            context,
            plaintext,
        )?)
    }

    /// Decrypt arbitrary bytes with a custom HKDF context.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Crypto`] if decryption fails.
    pub fn decrypt_bytes_with_context(
        &self,
        context: &str,
        packed: &[u8],
    ) -> Result<Vec<u8>, Error> {
        Ok(encryptman::decrypt_bytes_with_context(
            &self.master_key,
            context,
            packed,
        )?)
    }

    /// Delete the master key from the OS keychain.
    ///
    /// This is an associated function because deletion only requires the
    /// service name — no vault instance (or master key) is needed.
    ///
    /// **Warning**: This is destructive. All encrypted data will become
    /// unrecoverable unless you have a backup of the key.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Keychain`] if the keychain entry cannot be deleted.
    pub fn delete(service: &str) -> Result<(), Error> {
        Self::delete_with_target(service, KEY_USERNAME)
    }

    /// Delete the master key with a custom target from the OS keychain.
    ///
    /// This is an associated function — no vault instance needed.
    ///
    /// See [`Vault::delete`] for details.
    pub fn delete_with_target(service: &str, target: &str) -> Result<(), Error> {
        let entry = keyring::Entry::new(service, target)?;
        entry.delete_credential()?;
        Ok(())
    }
}

impl std::fmt::Debug for Vault {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Vault")
            .field("service", &self.service)
            .field("master_key", &"***")
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;

    #[test]
    #[serial]
    fn encrypt_decrypt_roundtrip() {
        let vault = Vault::new("test-encryptman-keyring").unwrap();
        let original = "my_secret_password_123!";
        let encrypted = vault.encrypt(original).unwrap();
        let decrypted = vault.decrypt(&encrypted).unwrap();
        assert_eq!(original, decrypted);
        let _ = Vault::delete("test-encryptman-keyring");
    }

    #[test]
    #[serial]
    fn encrypt_produces_different_output_each_time() {
        let vault = Vault::new("test-encryptman-keyring-ne").unwrap();
        let a = vault.encrypt("same_password").unwrap();
        let b = vault.encrypt("same_password").unwrap();
        assert_ne!(a, b);
        let _ = Vault::delete("test-encryptman-keyring-ne");
    }

    #[test]
    #[serial]
    fn wrong_key_fails() {
        let vault1 = Vault::new("test-encryptman-keyring-wk1").unwrap();
        let vault2 = Vault::new("test-encryptman-keyring-wk2").unwrap();
        let encrypted = vault1.encrypt("secret").unwrap();
        assert!(vault2.decrypt(&encrypted).is_err());
        let _ = Vault::delete("test-encryptman-keyring-wk1");
        let _ = Vault::delete("test-encryptman-keyring-wk2");
    }

    #[test]
    #[serial]
    fn unicode_roundtrip() {
        let vault = Vault::new("test-encryptman-keyring-unicode").unwrap();
        let original = "รหัสผ่านภาษาไทย 🔐";
        let encrypted = vault.encrypt(original).unwrap();
        let decrypted = vault.decrypt(&encrypted).unwrap();
        assert_eq!(original, decrypted);
        let _ = Vault::delete("test-encryptman-keyring-unicode");
    }

    #[test]
    #[serial]
    fn context_isolation() {
        let vault = Vault::new("test-encryptman-keyring-ctx").unwrap();
        let enc_a = vault.encrypt_with_context("ctx-a", "same").unwrap();
        let enc_b = vault.encrypt_with_context("ctx-b", "same").unwrap();
        assert_ne!(enc_a, enc_b);
        assert!(vault.decrypt_with_context("ctx-b", &enc_a).is_err());
        let _ = Vault::delete("test-encryptman-keyring-ctx");
    }

    #[test]
    #[serial]
    fn debug_does_not_leak_key() {
        let vault = Vault::new("test-encryptman-keyring-debug").unwrap();
        let debug = format!("{:?}", vault);
        assert_eq!(
            debug,
            "Vault { service: \"test-encryptman-keyring-debug\", master_key: \"***\" }"
        );
        let _ = Vault::delete("test-encryptman-keyring-debug");
    }

    #[test]
    #[serial]
    fn new_with_target() {
        let vault =
            Vault::new_with_target("test-encryptman-keyring-target", "custom-user").unwrap();
        let ct = vault.encrypt("hello").unwrap();
        let pt = vault.decrypt(&ct).unwrap();
        assert_eq!(pt, "hello");
        let _ = Vault::delete_with_target("test-encryptman-keyring-target", "custom-user");
    }

    #[test]
    #[serial]
    fn migrate_from_file() {
        use std::fs;
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let key_path = dir.path().join(".tb_key");
        let key_bytes = [42u8; 32];
        fs::write(&key_path, key_bytes).unwrap();

        let vault = Vault::migrate_from_file("test-encryptman-keyring-migrate", &key_path).unwrap();
        assert_eq!(vault.master_key().as_bytes(), &key_bytes);
        assert!(
            !key_path.exists(),
            "key file should be deleted after migration"
        );

        let ct = vault.encrypt("migrated").unwrap();
        let pt = vault.decrypt(&ct).unwrap();
        assert_eq!(pt, "migrated");
        let _ = Vault::delete("test-encryptman-keyring-migrate");
    }

    #[test]
    #[serial]
    fn migrate_from_file_wrong_length() {
        use std::fs;
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let key_path = dir.path().join(".tb_key");
        fs::write(&key_path, [1u8; 16]).unwrap();

        let result = Vault::migrate_from_file("test-encryptman-keyring-migrate-err", &key_path);
        assert!(result.is_err());
        assert!(key_path.exists());
    }

    #[test]
    #[serial]
    fn encrypt_bytes_roundtrip() {
        let vault = Vault::new("test-encryptman-keyring-bytes").unwrap();
        let data = b"binary secret data";
        let encrypted = vault.encrypt_bytes(data).unwrap();
        assert_ne!(encrypted, data.to_vec());
        let decrypted = vault.decrypt_bytes(&encrypted).unwrap();
        assert_eq!(decrypted, data);
        let _ = Vault::delete("test-encryptman-keyring-bytes");
    }

    #[test]
    #[serial]
    fn encrypt_bytes_produces_different_output_each_time() {
        let vault = Vault::new("test-encryptman-keyring-bytes-ne").unwrap();
        let data = b"same data";
        let a = vault.encrypt_bytes(data).unwrap();
        let b = vault.encrypt_bytes(data).unwrap();
        assert_ne!(a, b);
        let _ = Vault::delete("test-encryptman-keyring-bytes-ne");
    }

    #[test]
    #[serial]
    fn encrypt_bytes_context_isolation() {
        let vault = Vault::new("test-encryptman-keyring-bytes-ctx").unwrap();
        let data = b"same data";
        let a = vault.encrypt_bytes_with_context("ctx-a", data).unwrap();
        let b = vault.encrypt_bytes_with_context("ctx-b", data).unwrap();
        assert_ne!(a, b);
        assert!(vault.decrypt_bytes_with_context("ctx-b", &a).is_err());
        let _ = Vault::delete("test-encryptman-keyring-bytes-ctx");
    }

    #[test]
    #[serial]
    fn delete_as_associated_function() {
        let vault = Vault::new("test-encryptman-keyring-del").unwrap();
        let ct = vault.encrypt("test").unwrap();
        Vault::delete("test-encryptman-keyring-del").unwrap();
        assert!(
            Vault::new("test-encryptman-keyring-del")
                .unwrap()
                .decrypt(&ct)
                .is_err()
        );
    }
}