rainier-crypt 1.0.1

Authenticated encryption and message signing for Rainier.
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
//! Authenticated encryption — the [`Encrypter`] port and its AEAD
//! implementation.

use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use base64::Engine as _;
use rainier_support::{Error, Result};
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::cipher::Cipher;
use crate::key::KeyRing;

/// Reversible, tamper-evident encryption.
///
/// A port rather than a concrete type for the same reason [`Hasher`] is one in
/// `rainier-auth`: the right primitive changes over time, and an application
/// migrating between two should be able to run both.
///
/// [`Hasher`]: https://docs.rs/rainier-auth
pub trait Encrypter: Send + Sync + 'static {
    /// Encrypt `plain`, returning a self-describing, URL-safe payload.
    fn encrypt_bytes(&self, plain: &[u8]) -> Result<String>;

    /// Decrypt a payload produced by [`encrypt_bytes`](Self::encrypt_bytes).
    ///
    /// Fails if the payload was truncated, altered, or produced by a key this
    /// encrypter does not hold. There is deliberately no way to decrypt
    /// without authenticating.
    fn decrypt_bytes(&self, payload: &str) -> Result<Vec<u8>>;
}

/// The typed conveniences every [`Encrypter`] gets.
///
/// Separate from the trait so that stays object-safe — the generic methods
/// would otherwise keep `Arc<dyn Encrypter>` from existing.
pub trait EncrypterExt: Encrypter {
    /// Encrypt a string.
    fn encrypt(&self, plain: &str) -> Result<String> {
        self.encrypt_bytes(plain.as_bytes())
    }

    /// Decrypt to a string.
    fn decrypt(&self, payload: &str) -> Result<String> {
        let bytes = self.decrypt_bytes(payload)?;
        String::from_utf8(bytes)
            .map_err(|_| Error::internal("the decrypted value is not valid UTF-8"))
    }

    /// Encrypt any serialisable value as JSON.
    fn encrypt_json<T: Serialize>(&self, value: &T) -> Result<String> {
        let json = serde_json::to_vec(value)?;
        self.encrypt_bytes(&json)
    }

    /// Decrypt JSON back into a value.
    fn decrypt_json<T: DeserializeOwned>(&self, payload: &str) -> Result<T> {
        let bytes = self.decrypt_bytes(payload)?;
        serde_json::from_slice(&bytes).map_err(|e| {
            // The payload authenticated, so this is our own format changing
            // under us rather than anything an attacker did.
            Error::internal(format!("the decrypted value is not the expected shape: {e}"))
        })
    }
}

impl<E: Encrypter + ?Sized> EncrypterExt for E {}

/// An AEAD [`Cipher`] over a [`KeyRing`].
///
/// Writes with one cipher and reads **any** of them, because the payload names
/// its own algorithm. So changing cipher is the same kind of change as
/// [rotating a key](KeyRing): deploy the new setting, and everything already
/// written still opens.
///
/// ```
/// # use rainier_crypt::{AeadEncrypter, Cipher, EncrypterExt, Key, KeyRing};
/// # fn main() -> rainier_support::Result<()> {
/// let keys = KeyRing::new(Key::generate());
/// let written = AeadEncrypter::new(keys.clone()).encrypt("before")?;
///
/// // Switched to AES; the old payload still reads.
/// let now = AeadEncrypter::new(keys).with_cipher(Cipher::Aes256Gcm);
/// assert_eq!(now.decrypt(&written)?, "before");
/// assert!(now.encrypt("after")?.starts_with("a256gcm."));
/// # Ok(()) }
/// ```
pub struct AeadEncrypter {
    keys: KeyRing,
    cipher: Cipher,
}

impl AeadEncrypter {
    /// An encrypter over `keys`, using the default cipher.
    pub fn new(keys: KeyRing) -> Self {
        Self { keys, cipher: Cipher::default() }
    }

    /// Write with a specific cipher. Reading is unaffected.
    pub fn with_cipher(mut self, cipher: Cipher) -> Self {
        self.cipher = cipher;
        self
    }

    /// The key ring, for a diagnostic command.
    pub fn keys(&self) -> &KeyRing {
        &self.keys
    }

    /// The cipher new payloads are written with.
    pub fn cipher(&self) -> Cipher {
        self.cipher
    }
}

impl Encrypter for AeadEncrypter {
    fn encrypt_bytes(&self, plain: &[u8]) -> Result<String> {
        let key = self.keys.current();
        let nonce = self.cipher.nonce();

        // The header is authenticated but not encrypted, so a payload cannot
        // be relabelled with a different cipher or key id and still open.
        let header = format!("{}.{}", self.cipher.id(), key.id());
        let sealed = self.cipher.encrypt(key, &nonce, header.as_bytes(), plain)?;

        Ok(format!("{header}.{}.{}", B64.encode(&nonce), B64.encode(&sealed)))
    }

    fn decrypt_bytes(&self, payload: &str) -> Result<Vec<u8>> {
        // One message for every malformed-or-forged case. Distinguishing "the
        // padding was wrong" from "the tag was wrong" is how padding-oracle
        // attacks start, and the caller can do nothing useful with the
        // difference anyway.
        let invalid = || Error::bad_request("the payload could not be decrypted");

        let mut parts = payload.split('.');
        let (Some(algorithm), Some(key_id), Some(nonce), Some(sealed), None) =
            (parts.next(), parts.next(), parts.next(), parts.next(), parts.next())
        else {
            return Err(invalid());
        };

        let cipher = Cipher::from_id(algorithm).ok_or_else(invalid)?;

        let key = self.keys.find(key_id).ok_or_else(|| {
            // Worth its own message: this one is an operator error — a key was
            // dropped from the ring while payloads written with it still
            // exist — not an attack, and the fix is to put the key back.
            Error::internal(format!(
                "no key with id `{key_id}` is on the ring; it is still needed to read \
                 payloads written with it"
            ))
        })?;

        let nonce = B64.decode(nonce).map_err(|_| invalid())?;
        let sealed = B64.decode(sealed).map_err(|_| invalid())?;

        // The header as written, not as reconstructed from the parsed cipher —
        // so a `v1` payload authenticates against `v1` rather than `xc20p`.
        let header = format!("{algorithm}.{key_id}");
        cipher.decrypt(key, &nonce, header.as_bytes(), &sealed)
    }
}

impl std::fmt::Debug for AeadEncrypter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AeadEncrypter")
            .field("cipher", &self.cipher.id())
            .field("keys", &self.keys.ids())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::key::Key;

    fn encrypter() -> AeadEncrypter {
        AeadEncrypter::new(KeyRing::new(Key::generate()))
    }

    #[test]
    fn a_value_round_trips() {
        let crypt = encrypter();
        let payload = crypt.encrypt("hello, world").unwrap();

        assert_eq!(crypt.decrypt(&payload).unwrap(), "hello, world");
    }

    #[test]
    fn the_ciphertext_does_not_contain_the_plaintext() {
        let payload = encrypter().encrypt("correct-horse-battery-staple").unwrap();
        assert!(!payload.contains("correct-horse"), "{payload}");
    }

    #[test]
    fn the_same_value_encrypts_differently_every_time() {
        let crypt = encrypter();

        // A random nonce per message, so equal plaintexts are not detectable
        // as equal — which is what makes an encrypted column safe to index on
        // nothing and unsafe to compare for equality.
        assert_ne!(crypt.encrypt("same").unwrap(), crypt.encrypt("same").unwrap());
    }

    #[test]
    fn a_payload_is_url_safe() {
        let payload = encrypter().encrypt("a value with / and + in mind").unwrap();

        assert!(
            payload.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_')),
            "{payload}"
        );
    }

    #[test]
    fn tampering_with_the_ciphertext_is_detected() {
        let crypt = encrypter();
        let payload = crypt.encrypt("transfer 10").unwrap();

        let mut parts: Vec<&str> = payload.split('.').collect();
        let flipped = {
            let mut bytes = B64.decode(parts[3]).unwrap();
            bytes[0] ^= 0x01;
            B64.encode(bytes)
        };
        parts[3] = &flipped;

        assert!(crypt.decrypt(&parts.join(".")).is_err());
    }

    #[test]
    fn tampering_with_the_nonce_is_detected() {
        let crypt = encrypter();
        let payload = crypt.encrypt("transfer 10").unwrap();

        let mut parts: Vec<&str> = payload.split('.').collect();
        let other = B64.encode(vec![0u8; Cipher::default().nonce_len()]);
        parts[2] = &other;

        assert!(crypt.decrypt(&parts.join(".")).is_err());
    }

    #[test]
    fn relabelling_the_cipher_is_detected() {
        // The algorithm is part of the authenticated header, so claiming a
        // payload was written with a different one does not open it.
        let crypt = encrypter();
        let payload = crypt.encrypt("secret").unwrap();

        let mut parts: Vec<&str> = payload.split('.').collect();
        parts[0] = "a256gcm";

        assert!(crypt.decrypt(&parts.join(".")).is_err());
    }

    #[test]
    fn every_cipher_round_trips_through_the_encrypter() {
        let keys = KeyRing::new(Key::generate());

        for cipher in Cipher::ALL {
            let crypt = AeadEncrypter::new(keys.clone()).with_cipher(cipher);
            let payload = crypt.encrypt("the message").unwrap();

            assert!(payload.starts_with(&format!("{}.", cipher.id())), "{payload}");
            assert_eq!(crypt.decrypt(&payload).unwrap(), "the message", "{cipher}");
        }
    }

    #[test]
    fn a_payload_written_by_any_cipher_reads_under_any_default() {
        // The point of naming the algorithm in the payload: switching the
        // configured cipher is a deploy, not a migration.
        let keys = KeyRing::new(Key::generate());
        let written: Vec<String> = Cipher::ALL
            .iter()
            .map(|c| AeadEncrypter::new(keys.clone()).with_cipher(*c).encrypt("old").unwrap())
            .collect();

        for reader in Cipher::ALL {
            let crypt = AeadEncrypter::new(keys.clone()).with_cipher(reader);
            for payload in &written {
                assert_eq!(crypt.decrypt(payload).unwrap(), "old", "{reader} reading {payload}");
            }
        }
    }

    #[test]
    fn an_unknown_cipher_id_is_refused() {
        let crypt = encrypter();
        assert_eq!(crypt.decrypt("rot13.abc.def.ghi").unwrap_err().status(), 400);
    }

    #[test]
    fn a_legacy_v1_payload_still_opens() {
        // What the first release wrote: `v1` in place of an algorithm name,
        // XChaCha20-Poly1305, and the header authenticated as written.
        let key = Key::generate();
        let cipher = Cipher::XChaCha20Poly1305;
        let nonce = cipher.nonce();
        let header = format!("v1.{}", key.id());
        let sealed = cipher.encrypt(&key, &nonce, header.as_bytes(), b"from before").unwrap();
        let payload = format!("{header}.{}.{}", B64.encode(&nonce), B64.encode(&sealed));

        let crypt = AeadEncrypter::new(KeyRing::new(key));
        assert_eq!(crypt.decrypt(&payload).unwrap(), "from before");
    }

    #[test]
    fn relabelling_the_header_is_detected() {
        // The header is the AEAD's associated data, so claiming a payload was
        // written by a different key does not let it open with that key.
        let old = Key::generate();
        let current = Key::generate();
        let crypt = AeadEncrypter::new(KeyRing::new(current).with_previous(old.clone()));

        let payload = crypt.encrypt("secret").unwrap();
        let mut parts: Vec<&str> = payload.split('.').collect();
        parts[1] = old.id();

        assert!(crypt.decrypt(&parts.join(".")).is_err());
    }

    #[test]
    fn a_malformed_payload_is_a_400_not_a_500() {
        let crypt = encrypter();

        for rubbish in ["", "nonsense", "v1.abc", "v1.abc.def.ghi.jkl", "v2.abc.def.ghi"] {
            let err = crypt.decrypt(rubbish).unwrap_err();
            assert_eq!(err.status(), 400, "{rubbish}");
        }
    }

    #[test]
    fn every_failure_reports_the_same_thing() {
        // No padding oracle: the caller cannot tell *why* it failed.
        let crypt = encrypter();
        let truncated = crypt.decrypt("v1.abc.def.ghi").unwrap_err();
        let garbage = crypt.decrypt("v1.abc.!!!.!!!").unwrap_err();

        assert_eq!(truncated.message(), garbage.message());
    }

    #[test]
    fn another_applications_key_cannot_read_the_payload() {
        let payload = encrypter().encrypt("secret").unwrap();
        assert!(encrypter().decrypt(&payload).is_err());
    }

    #[test]
    fn a_retired_key_still_reads_what_it_wrote() {
        let old = Key::generate();
        let written = AeadEncrypter::new(KeyRing::new(old.clone())).encrypt("before").unwrap();

        // Rotation: a new current key, the old one kept for reading.
        let rotated = AeadEncrypter::new(KeyRing::new(Key::generate()).with_previous(old));

        assert_eq!(rotated.decrypt(&written).unwrap(), "before");
        assert!(
            rotated.encrypt("after").unwrap().contains(rotated.keys().current().id()),
            "new payloads use the new key"
        );
    }

    #[test]
    fn dropping_a_key_that_is_still_needed_says_so() {
        let written = AeadEncrypter::new(KeyRing::new(Key::generate())).encrypt("x").unwrap();
        let err = encrypter().decrypt(&written).unwrap_err();

        assert_eq!(err.status(), 500, "an operator error, not a client one");
        assert!(err.message().contains("still needed"), "{}", err.message());
    }

    #[test]
    fn json_round_trips() {
        #[derive(serde::Serialize, serde::Deserialize, PartialEq, Debug)]
        struct Token {
            user: u64,
            scopes: Vec<String>,
        }

        let crypt = encrypter();
        let token = Token { user: 7, scopes: vec!["read".into()] };
        let payload = crypt.encrypt_json(&token).unwrap();

        assert_eq!(crypt.decrypt_json::<Token>(&payload).unwrap(), token);
    }

    #[test]
    fn an_empty_value_round_trips() {
        let crypt = encrypter();
        assert_eq!(crypt.decrypt(&crypt.encrypt("").unwrap()).unwrap(), "");
    }

    #[test]
    fn binary_round_trips() {
        let crypt = encrypter();
        let bytes: Vec<u8> = (0..=255u8).collect();

        assert_eq!(crypt.decrypt_bytes(&crypt.encrypt_bytes(&bytes).unwrap()).unwrap(), bytes);
    }

    #[test]
    fn the_port_is_object_safe() {
        let crypt: std::sync::Arc<dyn Encrypter> = std::sync::Arc::new(encrypter());
        // The ext trait must reach through the trait object, or the whole
        // split was pointless.
        assert_eq!(crypt.decrypt(&crypt.encrypt("via dyn").unwrap()).unwrap(), "via dyn");
    }
}