1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #![allow(clippy::indexing_slicing, clippy::unwrap_used, clippy::panic, clippy::expect_used)]
15 
16 extern crate std;
17 
18 use std::{
19     fs,
20     io::{self, BufRead as _},
21     vec::Vec,
22 };
23 
24 use crate::TweakState;
25 
26 #[test]
advance_from_all_ones()27 fn advance_from_all_ones() {
28     let mut tweak_state = TweakState::new([1; 16]);
29 
30     tweak_state.advance_to_block(1);
31 
32     assert_eq!(1, tweak_state.block_num);
33     assert_eq!([2; 16], tweak_state.tweak);
34 }
35 
36 #[test]
advance_with_carry()37 fn advance_with_carry() {
38     let mut tweak_state = TweakState::new([0xF0; 16]);
39     tweak_state.advance_to_block(1);
40 
41     assert_eq!(1, tweak_state.block_num);
42     let mut expected_state = [225; 16];
43     expected_state[0] = 103;
44     assert_eq!(expected_state, tweak_state.tweak);
45 }
46 
47 #[test]
tweak_test_vectors()48 fn tweak_test_vectors() {
49     let file_path = test_helper::get_data_file(
50         "presence/xts_aes/resources/test/tweak-state-advance-test-vectors.txt",
51     );
52     let file = fs::File::open(file_path).expect("Should be able to open file");
53 
54     let count = io::BufReader::new(file)
55         .lines()
56         .map(|r| r.unwrap())
57         .filter(|l| !l.starts_with('#'))
58         .inspect(|l| {
59             let chunks = l.split(' ').collect::<Vec<_>>();
60             let start_bytes = hex::decode(chunks[0]).unwrap();
61             let end_bytes = hex::decode(chunks[2]).unwrap();
62             let block_num: u32 = chunks[1].parse().unwrap();
63 
64             let mut state = TweakState::new(start_bytes.try_into().unwrap());
65             state.advance_to_block(block_num);
66             assert_eq!(end_bytes, state.tweak);
67         })
68         .count();
69 
70     assert_eq!(1000, count);
71 }
72