Debian Codegraph @

third_party/rust/neqo-crypto/tests/aead.rs

raw
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.
6
7 #![warn(clippy::pedantic)]
8 #![cfg(not(feature = "disable-encryption"))]
9
10 use neqo_crypto::{
11 constants::{Cipher, TLS_AES_128_GCM_SHA256, TLS_VERSION_1_3},
12 hkdf, Aead,
13 };
14 use test_fixture::fixture_init;
15
16 const AAD: &[u8] = &[
17 0xc1, 0xff, 0x00, 0x00, 0x12, 0x05, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, 0x00, 0x40,
18 0x74, 0x00, 0x01,
19 ];
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,
27 0x03, 0x04,
28 ];
29
30 fn make_aead(cipher: Cipher) -> Aead {
31 fixture_init();
32
33 let secret = hkdf::import_key(
34 TLS_VERSION_1_3,
35 &[
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,
39 ],
40 )
41 .expect("make a secret");
42 Aead::new(
43 TLS_VERSION_1_3,
44 cipher,
45 &secret,
46 "quic ", // QUICv1 label prefix; note the trailing space here.
47 )
48 .expect("can make an AEAD")
49 }
50
51 #[test]
52 fn aead_encrypt_decrypt() {
53 const TOGGLE: u8 = 77;
54 let aead = make_aead(TLS_AES_128_GCM_SHA256);
55 let ciphertext_buf = &mut [0; 1024]; // Can't use PLAINTEXT.len() here.
56 let ciphertext = aead
57 .encrypt(1, AAD, PLAINTEXT, ciphertext_buf)
58 .expect("encrypt should work");
59 let expected_ciphertext: &[u8] = &[
60 0x5f, 0x01, 0xc4, 0xc2, 0xa2, 0x30, 0x3d, 0x29, 0x7e, 0x3c, 0x51, 0x9b, 0xf6, 0xb2, 0x23,
61 0x86, 0xe3, 0xd0, 0xbd, 0x6d, 0xfc, 0x66, 0x12, 0x16, 0x77, 0x29, 0x80, 0x31, 0x04, 0x1b,
62 0xb9, 0xa7, 0x9c, 0x9f, 0x0f, 0x9d, 0x4c, 0x58, 0x77, 0x27, 0x0a, 0x66, 0x0f, 0x5d, 0xa3,
63 0x62, 0x07, 0xd9, 0x8b, 0x73, 0x83, 0x9b, 0x2f, 0xdf, 0x2e, 0xf8, 0xe7, 0xdf, 0x5a, 0x51,
64 0xb1, 0x7b, 0x8c, 0x68, 0xd8, 0x64, 0xfd, 0x3e, 0x70, 0x8c, 0x6c, 0x1b, 0x71, 0xa9, 0x8a,
65 0x33, 0x18, 0x15, 0x59, 0x9e, 0xf5, 0x01, 0x4e, 0xa3, 0x8c, 0x44, 0xbd, 0xfd, 0x38, 0x7c,
66 0x03, 0xb5, 0x27, 0x5c, 0x35, 0xe0, 0x09, 0xb6, 0x23, 0x8f, 0x83, 0x14, 0x20, 0x04, 0x7c,
67 0x72, 0x71, 0x28, 0x1c, 0xcb, 0x54, 0xdf, 0x78, 0x84,
68 ];
69 assert_eq!(ciphertext, expected_ciphertext);
70
71 let plaintext_buf = &mut [0; 1024]; // Can't use PLAINTEXT.len() here.
72 let plaintext = aead
73 .decrypt(1, AAD, ciphertext, plaintext_buf)
74 .expect("decrypt should also work");
75 assert_eq!(plaintext, PLAINTEXT);
76
77 // Decryption failures...
78 // Different counter.
79 let res = aead.decrypt(2, AAD, ciphertext, plaintext_buf);
80 assert!(res.is_err());
81
82 // Front-truncate ciphertext.
83 let res = aead.decrypt(1, AAD, &ciphertext[1..], plaintext_buf);
84 assert!(res.is_err());
85
86 // End-truncate ciphertext.
87 let ciphertext_last = ciphertext.len() - 1;
88 let res = aead.decrypt(1, AAD, &ciphertext[..ciphertext_last], plaintext_buf);
89 assert!(res.is_err());
90
91 // Mess with the buffer.
92 let mut scratch = Vec::new();
93 scratch.extend_from_slice(ciphertext);
94
95 // Toggle first octet.
96 scratch[0] ^= TOGGLE;
97 let res = aead.decrypt(1, AAD, &scratch[..], plaintext_buf);
98 assert!(res.is_err());
99
100 // Toggle the auth tag.
101 scratch[0] ^= TOGGLE;
102 scratch[ciphertext_last] ^= TOGGLE;
103 let res = aead.decrypt(1, AAD, &scratch[..], plaintext_buf);
104 assert!(res.is_err());
105
106 // Mess with the AAD.
107 scratch.clear();
108 scratch.extend_from_slice(AAD);
109
110 // Front-truncate.
111 let res = aead.decrypt(1, &scratch[1..], ciphertext, plaintext_buf);
112 assert!(res.is_err());
113
114 // End-truncate.
115 let aad_last = AAD.len() - 1;
116 let res = aead.decrypt(1, &scratch[..aad_last], ciphertext, plaintext_buf);
117 assert!(res.is_err());
118
119 scratch[0] ^= TOGGLE;
120 let res = aead.decrypt(1, &scratch[..], ciphertext, plaintext_buf);
121 assert!(res.is_err());
122 }