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::*;
19 use xts_aes::XtsAes128;
20 
21 #[fuzztest]
test(data: LdtFuzzInput)22 fn test(data: LdtFuzzInput) {
23     let ldt_enc = LdtEncryptCipher::<16, XtsAes128<RustCrypto>, Swap>::new(
24         &LdtKey::from_concatenated(&data.ldt_key),
25     );
26     let ldt_dec = LdtDecryptCipher::<16, XtsAes128<RustCrypto>, Swap>::new(
27         &LdtKey::from_concatenated(&data.ldt_key),
28     );
29     let len = 16 + (data.len as usize % 16);
30     let padder: XorPadder<16> = data.xor_padder.into();
31 
32     let mut buffer = data.plaintext;
33     ldt_enc.encrypt(&mut buffer[..len], &padder).unwrap();
34     ldt_dec.decrypt(&mut buffer[..len], &padder).unwrap();
35     assert_eq!(data.plaintext, buffer);
36 }
37 
38 #[derive(Clone, Debug, arbitrary::Arbitrary)]
39 struct LdtFuzzInput {
40     // XTS-AES128 keys
41     ldt_key: [u8; 64],
42     xor_padder: [u8; 16],
43     // max length = 2 * AES block size - 1
44     plaintext: [u8; 31],
45     // will % 16 to get a [0-15] value for how much of the plaintext to use past the first block
46     len: u8,
47 }
48