gunnar_crypt/lib.rs
1//! **Encrypted repositories**: the operator of a gunnar server cannot read a
2//! repository's content; only its members can, each with their own
3//! encryption key, opened by their SSH key or their security key.
4//!
5//! The design, the threat model and what still leaks are in
6//! `.nornir/encrypted-repos.md`. This crate is the part every door shares:
7//!
8//! * [`envelope`] — the format of one encrypted file (blob): a 13-byte header
9//! naming the key epoch, then AES-256-SIV over the content. Deterministic on
10//! purpose: the same content under the same key gives the same blob, so git's
11//! clean filter is stable and `git status` stays quiet, and the blob id is a
12//! KEYED id (it depends on the secret key), so nobody without the key can
13//! confirm that a known file is in the repository.
14//! * [`keyring`] — the repository's data keys, one per epoch. A rotation adds
15//! an epoch; old epochs stay so members can still read old content.
16//! * [`members`] — the plaintext list of members' PUBLIC keys
17//! (`.gunnar-crypt/members`). The key ring is wrapped to every key in it.
18//! * [`policy`] — what a server checks on a push to an encrypted repository:
19//! every blob is an envelope, except the three files the scheme itself needs.
20//! It reads formats, never keys.
21//! * `userkey` (feature `user-key`) — the USER's encryption key: one key
22//! pair per user (the hybrid `age1pq1…`, quantum-safe, unless the person
23//! asked for a classic age X25519 key), its private half stored on the server only as age files,
24//! one per unlock method: an SSH key file (CLI), a security key's WebAuthn
25//! PRF / FIDO2 hmac-secret output through HKDF-SHA256 (browser and CLI), an
26//! `age1pq1…` key file, or a printed recovery code (age's scrypt).
27//! * [`skwrap`] — the text form of a security key's wrap v2 (CRYPT-PQ-SK):
28//! symmetric under HKDF of the PRF output, XChaCha20-Poly1305, quantum-safe.
29//! Format only; the server checks it.
30//! * `wrap` (feature `user-key`) — the age wrap of the key ring to every
31//! member's user public key. A repository never names an SSH key or a
32//! security key; adding or removing an unlock method touches no repository.
33//! Feature `pq` adds the quantum-safe user key and ring, `wrap` the SSH
34//! unlock method.
35//! * `pq` (feature `pq`) — that hybrid recipient and its identity, until an
36//! age release carries its own.
37//!
38//! **Quantum-safe is the default** (CRYPT-PQ-DEFAULT, owner 2026-10-30): a new
39//! repository ([`members::Members::new`]) and a new key are at
40//! [`members::Level::NEW`]; classic is the visible opt-out. A members file
41//! without a `level` line stays classic, so an existing repository keeps its
42//! level.
43//!
44//! * [`diff`] (feature `diff`) — line diffs of decrypted content. The server
45//! cannot diff ciphertext and keeps no pre-made diffs; the client does it.
46//!
47//! **One crate, two targets.** The CLI and the browser console run this same
48//! code, native and wasm32: fetch ciphertext → open the user key with an
49//! unlock method → unwrap the key ring → open and diff locally → seal before
50//! push. The CLI opens the user key with the SSH key file in `~/.ssh` (or a
51//! security key over FIDO2 hmac-secret); the browser opens it with a security
52//! key (WebAuthn PRF), in the tab, with no helper. `tests/vectors.rs` and
53//! `tests/userkey.rs` run the same vectors on both targets.
54//!
55//! Who links what: gunnar-server takes `default-features = false` (policy and
56//! format only; no cipher, no key), the browser console takes `cipher`,
57//! `diff`, `user-key` and `pq`, and the CLI takes everything.
58
59#![forbid(unsafe_code)]
60#![doc(
61 html_logo_url = "https://gunnar.rs/download/gunnar.webp",
62 html_favicon_url = "https://gunnar.rs/download/favicon.svg"
63)]
64
65/// zeroize, for callers holding key material.
66#[cfg(feature = "cipher")]
67pub use zeroize;
68
69#[cfg(feature = "diff")]
70pub mod diff;
71pub mod door;
72pub mod envelope;
73#[cfg(feature = "cipher")]
74pub mod keyring;
75pub mod members;
76pub mod policy;
77#[cfg(feature = "pq")]
78pub mod pq;
79pub mod skwrap;
80#[cfg(feature = "user-key")]
81pub mod userkey;
82#[cfg(feature = "user-key")]
83pub mod wrap;
84
85/// `N` bytes from the OS random source (keys, recovery codes).
86#[cfg(feature = "keygen")]
87pub fn random_bytes<const N: usize>() -> [u8; N] {
88 let mut b = [0u8; N];
89 getrandom::getrandom(&mut b).expect("the OS random source is available");
90 b
91}
92
93/// Open a blob as text: an envelope is opened with `ring`; anything else is
94/// returned as it is (history from before `gunnar crypt init` is plaintext).
95#[cfg(feature = "cipher")]
96pub fn open_blob(ring: &keyring::Keyring, blob: &[u8]) -> Result<Vec<u8>, envelope::OpenError> {
97 if envelope::has_magic(blob) {
98 envelope::open(ring, blob)
99 } else {
100 Ok(blob.to_vec())
101 }
102}
103
104/// The diff between two stored blobs of one path (`None` = the path does not
105/// exist on that side), opened with `ring`. What `gunnar crypt diff` prints
106/// and what the console shows for an encrypted repository.
107#[cfg(all(feature = "cipher", feature = "diff"))]
108pub fn diff_blobs(
109 ring: &keyring::Keyring,
110 old: Option<&[u8]>,
111 new: Option<&[u8]>,
112) -> Result<diff::FileDiff, envelope::OpenError> {
113 let old = old
114 .map(|b| open_blob(ring, b))
115 .transpose()?
116 .unwrap_or_default();
117 let new = new
118 .map(|b| open_blob(ring, b))
119 .transpose()?
120 .unwrap_or_default();
121 Ok(diff::hunks(
122 &String::from_utf8_lossy(&old),
123 &String::from_utf8_lossy(&new),
124 ))
125}
126
127/// Where the scheme's own files live in a repository.
128pub mod paths {
129 /// The directory of the scheme's files.
130 pub const DIR: &str = ".gunnar-crypt";
131 /// The key ring, age-encrypted to every member key (armored).
132 pub const KEYS: &str = ".gunnar-crypt/keys";
133 /// The members and their PUBLIC keys, plaintext.
134 pub const MEMBERS: &str = ".gunnar-crypt/members";
135 /// git's attributes file: it names the filter, so it must stay plaintext.
136 pub const GITATTRIBUTES: &str = ".gitattributes";
137}
138
139/// The name of the git filter, diff and merge drivers.
140pub const DRIVER: &str = "gunnar-crypt";
141
142/// The `.gitattributes` lines `gunnar crypt init` writes.
143///
144/// Everything goes through the filter except the scheme's own files, which
145/// must stay readable before a clone is unlocked.
146pub const GITATTRIBUTES_LINES: &[&str] = &[
147 "* filter=gunnar-crypt diff=gunnar-crypt merge=gunnar-crypt",
148 ".gitattributes !filter !diff !merge",
149 ".gunnar-crypt/** !filter !diff !merge",
150];