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(
15 clippy::indexing_slicing,
16 clippy::unwrap_used,
17 clippy::panic,
18 clippy::expect_used
19 )]
20
21 extern crate std;
22
23 use std::{
24 fs,
25 io::{self, BufRead as _},
26 vec::Vec,
27 };
28
29 use crate::TweakState;
30
31 #[test]
advance_from_all_ones()32 fn advance_from_all_ones() {
33 let mut tweak_state = TweakState::new([1; 16]);
34
35 tweak_state.advance_to_block(1);
36
37 assert_eq!(1, tweak_state.block_num);
38 assert_eq!([2; 16], tweak_state.tweak);
39 }
40
41 #[test]
advance_with_carry()42 fn advance_with_carry() {
43 let mut tweak_state = TweakState::new([0xF0; 16]);
44 tweak_state.advance_to_block(1);
45
46 assert_eq!(1, tweak_state.block_num);
47 let mut expected_state = [225; 16];
48 expected_state[0] = 103;
49 assert_eq!(expected_state, tweak_state.tweak);
50 }
51
52 #[test]
tweak_test_vectors()53 fn tweak_test_vectors() {
54 let file_path = test_helper::get_data_file(
55 "presence/xts_aes/resources/test/tweak-state-advance-test-vectors.txt",
56 );
57 let file = fs::File::open(file_path).expect("Should be able to open file");
58
59 let count = io::BufReader::new(file)
60 .lines()
61 .map(|r| r.unwrap())
62 .filter(|l| !l.starts_with('#'))
63 .inspect(|l| {
64 let chunks = l.split(' ').collect::<Vec<_>>();
65 let start_bytes = hex::decode(chunks[0]).unwrap();
66 let end_bytes = hex::decode(chunks[2]).unwrap();
67 let block_num: u32 = chunks[1].parse().unwrap();
68
69 let mut state = TweakState::new(start_bytes.try_into().unwrap());
70 state.advance_to_block(block_num);
71 assert_eq!(end_bytes, state.tweak);
72 })
73 .count();
74
75 assert_eq!(1000, count);
76 }
77