Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / third_party / betocore / xts_aes / tests / tests.rs

tests.rs119 lines · 4.3 KiBRaw
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
15#![allow(missing_docs)]
16
17use crypto_provider::aes::{Aes, BLOCK_SIZE};
18use crypto_provider::CryptoProvider;
19use crypto_provider_default::CryptoProviderImpl;
20use ldt_tbc::{TweakableBlockCipherDecrypter, TweakableBlockCipherEncrypter};
21use xts_aes::{XtsAes128Key, XtsAes256Key, XtsDecrypter, XtsEncrypter, XtsError, XtsKey};
22
23const MAX_XTS_SIZE: usize = (1 << 20) * BLOCK_SIZE;
24
25#[test]
26fn too_small_payload() {
27 let key = [0_u8; 32];
28 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes128, _>(
29 &XtsAes128Key::from(&key),
30 );
31 let tweak = [0u8; 16];
32 let mut payload = [0u8; BLOCK_SIZE - 1];
33 assert_eq!(enc.encrypt_data_unit(tweak.into(), &mut payload), Err(XtsError::DataTooShort));
34 assert_eq!(dec.decrypt_data_unit(tweak.into(), &mut payload), Err(XtsError::DataTooShort));
35
36 let key = [0u8; 64];
37 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes256, _>(
38 &XtsAes256Key::from(&key),
39 );
40 assert_eq!(enc.encrypt_data_unit(tweak.into(), &mut payload), Err(XtsError::DataTooShort));
41 assert_eq!(dec.decrypt_data_unit(tweak.into(), &mut payload), Err(XtsError::DataTooShort));
42}
43
44#[test]
45fn block_size_payload() {
46 let key = [0_u8; 32];
47 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes128, _>(
48 &XtsAes128Key::from(&key),
49 );
50 let tweak = [0u8; 16];
51 let mut payload = [0u8; BLOCK_SIZE];
52 assert!(enc.encrypt_data_unit(tweak.into(), &mut payload).is_ok());
53 assert!(dec.decrypt_data_unit(tweak.into(), &mut payload).is_ok());
54
55 let key = [0u8; 64];
56 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes256, _>(
57 &XtsAes256Key::from(&key),
58 );
59 assert!(enc.encrypt_data_unit(tweak.into(), &mut payload).is_ok());
60 assert!(dec.decrypt_data_unit(tweak.into(), &mut payload).is_ok());
61}
62
63#[test]
64fn max_xts_sized_payload() {
65 let key = [0_u8; 32];
66 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes128, _>(
67 &XtsAes128Key::from(&key),
68 );
69 let tweak = [0u8; 16];
70 let mut payload = vec![0u8; MAX_XTS_SIZE];
71 assert!(enc.encrypt_data_unit(tweak.into(), payload.as_mut_slice()).is_ok());
72 assert!(dec.decrypt_data_unit(tweak.into(), payload.as_mut_slice()).is_ok());
73
74 let key = [0u8; 64];
75 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes256, _>(
76 &XtsAes256Key::from(&key),
77 );
78 assert!(enc.encrypt_data_unit(tweak.into(), payload.as_mut_slice()).is_ok());
79 assert!(dec.decrypt_data_unit(tweak.into(), payload.as_mut_slice()).is_ok());
80}
81
82#[test]
83fn too_large_payload() {
84 let key = [0_u8; 32];
85 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes128, _>(
86 &XtsAes128Key::from(&key),
87 );
88 let tweak = [0u8; 16];
89 let mut payload = vec![0u8; MAX_XTS_SIZE + 1];
90 assert_eq!(
91 enc.encrypt_data_unit(tweak.into(), payload.as_mut_slice()),
92 Err(XtsError::DataTooLong)
93 );
94 assert_eq!(
95 dec.decrypt_data_unit(tweak.into(), payload.as_mut_slice()),
96 Err(XtsError::DataTooLong)
97 );
98
99 let key = [0u8; 64];
100 let (enc, dec) = build_ciphers::<<CryptoProviderImpl as CryptoProvider>::Aes256, _>(
101 &XtsAes256Key::from(&key),
102 );
103 assert_eq!(
104 enc.encrypt_data_unit(tweak.into(), payload.as_mut_slice()),
105 Err(XtsError::DataTooLong)
106 );
107 assert_eq!(
108 dec.decrypt_data_unit(tweak.into(), payload.as_mut_slice()),
109 Err(XtsError::DataTooLong)
110 );
111}
112
113fn build_ciphers<A: Aes<Key = K::BlockCipherKey>, K: XtsKey + ldt_tbc::TweakableBlockCipherKey>(
114 key: &K,
115) -> (XtsEncrypter<A, K>, XtsDecrypter<A, K>) {
116 let enc = XtsEncrypter::<A, _>::new(key);
117 let dec = XtsDecrypter::<A, _>::new(key);
118 (enc, dec)
119}