Skip to main content

bitwarden_crypto_primitives/
aes_gcm.rs

1//! AES-256-GCM.
2
3use crate::{Error, backend};
4
5/// Key length in bytes.
6pub const KEY_LEN: usize = 32;
7/// Nonce length in bytes.
8pub const NONCE_LEN: usize = 12;
9/// Authentication tag length in bytes.
10pub const TAG_LEN: usize = 16;
11
12/// Encrypts `plaintext` and authenticates it with `aad`. Returns `ciphertext || tag`.
13///
14/// The caller must never reuse a nonce under the same key.
15pub fn encrypt(
16    key: &[u8; KEY_LEN],
17    nonce: &[u8; NONCE_LEN],
18    aad: &[u8],
19    plaintext: &[u8],
20) -> Vec<u8> {
21    backend::aes_gcm::encrypt(key, nonce, aad, plaintext)
22}
23
24/// Verifies and decrypts `ciphertext || tag`.
25pub fn decrypt(
26    key: &[u8; KEY_LEN],
27    nonce: &[u8; NONCE_LEN],
28    aad: &[u8],
29    ciphertext: &[u8],
30) -> Result<Vec<u8>, Error> {
31    backend::aes_gcm::decrypt(key, nonce, aad, ciphertext)
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::test_vectors::{array, bytes};
38
39    const KEY: &str = "0707070707070707070707070707070707070707070707070707070707070707";
40    const NONCE: &str = "090909090909090909090909";
41
42    #[test]
43    fn roundtrip() {
44        let ct = encrypt(&array(KEY), &array(NONCE), b"aad", b"plaintext");
45        assert_eq!(ct.len(), b"plaintext".len() + TAG_LEN);
46        assert_eq!(
47            decrypt(&array(KEY), &array(NONCE), b"aad", &ct),
48            Ok(b"plaintext".to_vec())
49        );
50    }
51
52    #[test]
53    fn rejects_tampering() {
54        let mut ct = encrypt(&array(KEY), &array(NONCE), b"aad", b"plaintext");
55        assert_eq!(
56            decrypt(&array(KEY), &array(NONCE), b"other", &ct),
57            Err(Error::DecryptionFailed)
58        );
59
60        ct[0] ^= 1;
61        assert_eq!(
62            decrypt(&array(KEY), &array(NONCE), b"aad", &ct),
63            Err(Error::DecryptionFailed)
64        );
65        assert_eq!(
66            decrypt(&array(KEY), &array(NONCE), b"aad", &ct[..4]),
67            Err(Error::DecryptionFailed)
68        );
69    }
70
71    #[test]
72    fn known_answer_empty() {
73        // Zero key, zero nonce, empty message (McGrew & Viega test case 13).
74        let ct = encrypt(
75            &array("0000000000000000000000000000000000000000000000000000000000000000"),
76            &array("000000000000000000000000"),
77            b"",
78            b"",
79        );
80        assert_eq!(hex::encode(ct), "530f8afbc74536b9a963b4f1c4cb738b");
81    }
82
83    /// Edge cases under fixed key and nonce: empty plaintext with AAD (tag only), a plaintext one
84    /// byte past a block without AAD, and a multi-block plaintext with AAD.
85    #[test]
86    fn known_answers() {
87        const KEY_42: &str = "4242424242424242424242424242424242424242424242424242424242424242";
88        const NONCE_24: &str = "242424242424242424242424";
89
90        // (aad, plaintext, ciphertext || tag), all hex
91        let cases = [
92            ("616164", "", "ffee61d26a6387b554650541381cb141"),
93            (
94                "",
95                "1111111111111111111111111111111111",
96                "0480d550f8d7d72f37f933bbac2382b77d0d51d0a9e90eb0177e0ad9a1c2901b\
97                b8",
98            ),
99            (
100                "616164",
101                "54686520717569636b2062726f776e20666f78206a756d7073206f7665722074\
102                6865206c617a7920646f67",
103                "41f9a16198b3af5d4dc840d8d245fd860a944b50dcde3d1dc384a32c33b7c767\
104                6a6e71e91c3ed33cb129d70d2ce96ac992bf6360767ed0e49256a3",
105            ),
106        ];
107        let (key, nonce) = (array::<KEY_LEN>(KEY_42), array::<NONCE_LEN>(NONCE_24));
108        for (aad, plaintext, expected) in cases {
109            let (aad, plaintext) = (bytes(aad), bytes(plaintext));
110            let ciphertext = encrypt(&key, &nonce, &aad, &plaintext);
111            assert_eq!(hex::encode(&ciphertext), expected);
112            assert_eq!(decrypt(&key, &nonce, &aad, &ciphertext), Ok(plaintext));
113        }
114    }
115}