1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
7 #![warn(clippy::pedantic)]
8 #![cfg(not(feature = "fuzzing"))]
11 constants::{Cipher, TLS_AES_128_GCM_SHA256, TLS_VERSION_1_3},
14 use test_fixture::fixture_init;
17 0xc1, 0xff, 0x00, 0x00, 0x12, 0x05, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x00, 0x40,
20 const PLAINTEXT: &[u8] = &[
21 0x0d, 0x00, 0x00, 0x00, 0x00, 0x18, 0x41, 0x0a, 0x02, 0x00, 0x00, 0x56, 0x03, 0x03, 0xee, 0xfc,
22 0xe7, 0xf7, 0xb3, 0x7b, 0xa1, 0xd1, 0x63, 0x2e, 0x96, 0x67, 0x78, 0x25, 0xdd, 0xf7, 0x39, 0x88,
23 0xcf, 0xc7, 0x98, 0x25, 0xdf, 0x56, 0x6d, 0xc5, 0x43, 0x0b, 0x9a, 0x04, 0x5a, 0x12, 0x00, 0x13,
24 0x01, 0x00, 0x00, 0x2e, 0x00, 0x33, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x9d, 0x3c, 0x94, 0x0d,
25 0x89, 0x69, 0x0b, 0x84, 0xd0, 0x8a, 0x60, 0x99, 0x3c, 0x14, 0x4e, 0xca, 0x68, 0x4d, 0x10, 0x81,
26 0x28, 0x7c, 0x83, 0x4d, 0x53, 0x11, 0xbc, 0xf3, 0x2b, 0xb9, 0xda, 0x1a, 0x00, 0x2b, 0x00, 0x02,
30 fn make_aead(cipher: Cipher) -> Aead {
33 let secret = hkdf::import_key(
36 0x47, 0xb2, 0xea, 0xea, 0x6c, 0x26, 0x6e, 0x32, 0xc0, 0x69, 0x7a, 0x9e, 0x2a, 0x89,
37 0x8b, 0xdf, 0x5c, 0x4f, 0xb3, 0xe5, 0xac, 0x34, 0xf0, 0xe5, 0x49, 0xbf, 0x2c, 0x58,
38 0x58, 0x1a, 0x38, 0x11,
41 .expect("make a secret");
47 "quic ", // QUICv1 label prefix; note the trailing space here.
49 .expect("can make an AEAD")
53 fn aead_encrypt_decrypt() {
54 const TOGGLE: u8 = 77;
55 let aead = make_aead(TLS_AES_128_GCM_SHA256);
56 let ciphertext_buf = &mut [0; 1024]; // Can't use PLAINTEXT.len() here.
58 .encrypt(1, AAD, PLAINTEXT, ciphertext_buf)
59 .expect("encrypt should work");
60 let expected_ciphertext: &[u8] = &[
61 0x5f, 0x01, 0xc4, 0xc2, 0xa2, 0x30, 0x3d, 0x29, 0x7e, 0x3c, 0x51, 0x9b, 0xf6, 0xb2, 0x23,
62 0x86, 0xe3, 0xd0, 0xbd, 0x6d, 0xfc, 0x66, 0x12, 0x16, 0x77, 0x29, 0x80, 0x31, 0x04, 0x1b,
63 0xb9, 0xa7, 0x9c, 0x9f, 0x0f, 0x9d, 0x4c, 0x58, 0x77, 0x27, 0x0a, 0x66, 0x0f, 0x5d, 0xa3,
64 0x62, 0x07, 0xd9, 0x8b, 0x73, 0x83, 0x9b, 0x2f, 0xdf, 0x2e, 0xf8, 0xe7, 0xdf, 0x5a, 0x51,
65 0xb1, 0x7b, 0x8c, 0x68, 0xd8, 0x64, 0xfd, 0x3e, 0x70, 0x8c, 0x6c, 0x1b, 0x71, 0xa9, 0x8a,
66 0x33, 0x18, 0x15, 0x59, 0x9e, 0xf5, 0x01, 0x4e, 0xa3, 0x8c, 0x44, 0xbd, 0xfd, 0x38, 0x7c,
67 0x03, 0xb5, 0x27, 0x5c, 0x35, 0xe0, 0x09, 0xb6, 0x23, 0x8f, 0x83, 0x14, 0x20, 0x04, 0x7c,
68 0x72, 0x71, 0x28, 0x1c, 0xcb, 0x54, 0xdf, 0x78, 0x84,
70 assert_eq!(ciphertext, expected_ciphertext);
72 let plaintext_buf = &mut [0; 1024]; // Can't use PLAINTEXT.len() here.
74 .decrypt(1, AAD, ciphertext, plaintext_buf)
75 .expect("decrypt should also work");
76 assert_eq!(plaintext, PLAINTEXT);
78 // Decryption failures...
80 let res = aead.decrypt(2, AAD, ciphertext, plaintext_buf);
81 assert!(res.is_err());
83 // Front-truncate ciphertext.
84 let res = aead.decrypt(1, AAD, &ciphertext[1..], plaintext_buf);
85 assert!(res.is_err());
87 // End-truncate ciphertext.
88 let ciphertext_last = ciphertext.len() - 1;
89 let res = aead.decrypt(1, AAD, &ciphertext[..ciphertext_last], plaintext_buf);
90 assert!(res.is_err());
92 // Mess with the buffer.
93 let mut scratch = Vec::new();
94 scratch.extend_from_slice(ciphertext);
96 // Toggle first octet.
98 let res = aead.decrypt(1, AAD, &scratch[..], plaintext_buf);
99 assert!(res.is_err());
101 // Toggle the auth tag.
102 scratch[0] ^= TOGGLE;
103 scratch[ciphertext_last] ^= TOGGLE;
104 let res = aead.decrypt(1, AAD, &scratch[..], plaintext_buf);
105 assert!(res.is_err());
107 // Mess with the AAD.
109 scratch.extend_from_slice(AAD);
112 let res = aead.decrypt(1, &scratch[1..], ciphertext, plaintext_buf);
113 assert!(res.is_err());
116 let aad_last = AAD.len() - 1;
117 let res = aead.decrypt(1, &scratch[..aad_last], ciphertext, plaintext_buf);
118 assert!(res.is_err());
120 scratch[0] ^= TOGGLE;
121 let res = aead.decrypt(1, &scratch[..], ciphertext, plaintext_buf);
122 assert!(res.is_err());