1 // Copyright 2018 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 
15 use ring::{aead::quic, test, test_file};
16 
17 #[test]
quic_aes_128()18 fn quic_aes_128() {
19     test_quic(&quic::AES_128, test_file!("quic_aes_128_tests.txt"));
20 }
21 
22 #[test]
quic_aes_256()23 fn quic_aes_256() {
24     test_quic(&quic::AES_256, test_file!("quic_aes_256_tests.txt"));
25 }
26 
27 #[test]
quic_chacha20()28 fn quic_chacha20() {
29     test_quic(&quic::CHACHA20, test_file!("quic_chacha20_tests.txt"));
30 }
31 
test_quic(alg: &'static quic::Algorithm, test_file: test::File)32 fn test_quic(alg: &'static quic::Algorithm, test_file: test::File) {
33     test_sample_len(alg);
34 
35     test::run(test_file, |section, test_case| {
36         assert_eq!(section, "");
37         let key_bytes = test_case.consume_bytes("KEY");
38         let sample = test_case.consume_bytes("SAMPLE");
39         let mask = test_case.consume_bytes("MASK");
40 
41         let key = quic::HeaderProtectionKey::new(alg, &key_bytes)?;
42 
43         assert_eq!(mask.as_ref(), key.new_mask(&sample)?);
44 
45         Ok(())
46     });
47 }
48 
49 #[allow(clippy::range_plus_one)]
test_sample_len(alg: &'static quic::Algorithm)50 fn test_sample_len(alg: &'static quic::Algorithm) {
51     let key_len = alg.key_len();
52     let key_data = vec![0u8; key_len];
53 
54     let key = quic::HeaderProtectionKey::new(alg, &key_data).unwrap();
55 
56     let sample_len = 16;
57     let sample_data = vec![0u8; sample_len + 2];
58 
59     // Sample is the right size.
60     assert!(key.new_mask(&sample_data[..sample_len]).is_ok());
61 
62     // Sample is one byte too small.
63     assert!(key.new_mask(&sample_data[..(sample_len - 1)]).is_err());
64 
65     // Sample is one byte too big.
66     assert!(key.new_mask(&sample_data[..(sample_len + 1)]).is_err());
67 
68     // Sample is empty.
69     assert!(key.new_mask(&[]).is_err());
70 }
71