1 #![cfg_attr(fuzzing, no_main)]
2 // Copyright 2022 Google LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 use crypto_provider_rustcrypto::RustCrypto;
17 use derive_fuzztest::fuzztest;
18 use ldt_tbc::{TweakableBlockCipherDecrypter, TweakableBlockCipherEncrypter};
19 use xts_aes::*;
20
21 #[fuzztest]
test(data: XtsFuzzInput)22 fn test(data: XtsFuzzInput) {
23 // XTS requires at least one block
24 if data.plaintext.len() < 16 {
25 return;
26 }
27
28 let xts_enc =
29 <XtsAes128<RustCrypto> as ldt_tbc::TweakableBlockCipher<16>>::EncryptionCipher::new(
30 &XtsAes128Key::from(&data.key),
31 );
32 let xts_dec =
33 <XtsAes128<RustCrypto> as ldt_tbc::TweakableBlockCipher<16>>::DecryptionCipher::new(
34 &XtsAes128Key::from(&data.key),
35 );
36
37 let tweak: Tweak = data.tweak.into();
38
39 let mut buffer = data.plaintext.clone();
40
41 xts_enc.encrypt_data_unit(tweak.clone(), &mut buffer[..]).unwrap();
42 xts_dec.decrypt_data_unit(tweak, &mut buffer[..]).unwrap();
43 assert_eq!(data.plaintext, buffer);
44 }
45
46 #[derive(Clone, Debug, arbitrary::Arbitrary)]
47 struct XtsFuzzInput {
48 key: [u8; 32],
49 tweak: [u8; 16],
50 // min length = AES block size
51 plaintext: Vec<u8>,
52 }
53