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 pub mod cbc;
16 pub mod ctr;
17
18 pub use crate::prelude::*;
19
20 use core::marker;
21 use crypto_provider::aes::*;
22 use hex_literal::hex;
23 use rstest_reuse::template;
24
25 /// Test encryption with AES-128
aes_128_test_encrypt<A: AesEncryptCipher<Key = Aes128Key>>(_marker: marker::PhantomData<A>)26 pub fn aes_128_test_encrypt<A: AesEncryptCipher<Key = Aes128Key>>(_marker: marker::PhantomData<A>) {
27 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.1
28 let key: Aes128Key = hex!("2b7e151628aed2a6abf7158809cf4f3c").into();
29 let mut block = [0_u8; 16];
30 let enc_cipher = A::new(&key);
31
32 block.copy_from_slice(&hex!("6bc1bee22e409f96e93d7e117393172a"));
33 enc_cipher.encrypt(&mut block);
34 assert_eq!(hex!("3ad77bb40d7a3660a89ecaf32466ef97"), block);
35
36 block.copy_from_slice(&hex!("ae2d8a571e03ac9c9eb76fac45af8e51"));
37 enc_cipher.encrypt(&mut block);
38 assert_eq!(hex!("f5d3d58503b9699de785895a96fdbaaf"), block);
39
40 block.copy_from_slice(&hex!("30c81c46a35ce411e5fbc1191a0a52ef"));
41 enc_cipher.encrypt(&mut block);
42 assert_eq!(hex!("43b1cd7f598ece23881b00e3ed030688"), block);
43
44 block.copy_from_slice(&hex!("f69f2445df4f9b17ad2b417be66c3710"));
45 enc_cipher.encrypt(&mut block);
46 assert_eq!(hex!("7b0c785e27e8ad3f8223207104725dd4"), block);
47 }
48
49 /// Test decryption with AES-128
aes_128_test_decrypt<A: AesDecryptCipher<Key = Aes128Key>>(_marker: marker::PhantomData<A>)50 pub fn aes_128_test_decrypt<A: AesDecryptCipher<Key = Aes128Key>>(_marker: marker::PhantomData<A>) {
51 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.2
52 let key: Aes128Key = hex!("2b7e151628aed2a6abf7158809cf4f3c").into();
53 let mut block = [0_u8; 16];
54 let dec_cipher = A::new(&key);
55
56 block.copy_from_slice(&hex!("3ad77bb40d7a3660a89ecaf32466ef97"));
57 dec_cipher.decrypt(&mut block);
58 assert_eq!(hex!("6bc1bee22e409f96e93d7e117393172a"), block);
59
60 block.copy_from_slice(&hex!("f5d3d58503b9699de785895a96fdbaaf"));
61 dec_cipher.decrypt(&mut block);
62 assert_eq!(hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), block);
63
64 block.copy_from_slice(&hex!("43b1cd7f598ece23881b00e3ed030688"));
65 dec_cipher.decrypt(&mut block);
66 assert_eq!(hex!("30c81c46a35ce411e5fbc1191a0a52ef"), block);
67
68 block.copy_from_slice(&hex!("7b0c785e27e8ad3f8223207104725dd4"));
69 dec_cipher.decrypt(&mut block);
70 assert_eq!(hex!("f69f2445df4f9b17ad2b417be66c3710"), block);
71 }
72
73 /// Test encryption with AES-256
aes_256_test_encrypt<A: AesEncryptCipher<Key = Aes256Key>>(_marker: marker::PhantomData<A>)74 pub fn aes_256_test_encrypt<A: AesEncryptCipher<Key = Aes256Key>>(_marker: marker::PhantomData<A>) {
75 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.5
76 let key: Aes256Key =
77 hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").into();
78 let mut block: [u8; 16];
79 let enc_cipher = A::new(&key);
80
81 block = hex!("6bc1bee22e409f96e93d7e117393172a");
82 enc_cipher.encrypt(&mut block);
83 assert_eq!(hex!("f3eed1bdb5d2a03c064b5a7e3db181f8"), block);
84
85 block = hex!("ae2d8a571e03ac9c9eb76fac45af8e51");
86 enc_cipher.encrypt(&mut block);
87 assert_eq!(hex!("591ccb10d410ed26dc5ba74a31362870"), block);
88
89 block = hex!("30c81c46a35ce411e5fbc1191a0a52ef");
90 enc_cipher.encrypt(&mut block);
91 assert_eq!(hex!("b6ed21b99ca6f4f9f153e7b1beafed1d"), block);
92
93 block = hex!("f69f2445df4f9b17ad2b417be66c3710");
94 enc_cipher.encrypt(&mut block);
95 assert_eq!(hex!("23304b7a39f9f3ff067d8d8f9e24ecc7"), block);
96 }
97
98 /// Test decryption with AES-256
aes_256_test_decrypt<A: AesDecryptCipher<Key = Aes256Key>>(_marker: marker::PhantomData<A>)99 pub fn aes_256_test_decrypt<A: AesDecryptCipher<Key = Aes256Key>>(_marker: marker::PhantomData<A>) {
100 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.1.6
101 let key: Aes256Key =
102 hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").into();
103 let mut block: [u8; 16];
104 let dec_cipher = A::new(&key);
105
106 block = hex!("f3eed1bdb5d2a03c064b5a7e3db181f8");
107 dec_cipher.decrypt(&mut block);
108 assert_eq!(hex!("6bc1bee22e409f96e93d7e117393172a"), block);
109
110 block = hex!("591ccb10d410ed26dc5ba74a31362870");
111 dec_cipher.decrypt(&mut block);
112 assert_eq!(hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), block);
113
114 block = hex!("b6ed21b99ca6f4f9f153e7b1beafed1d");
115 dec_cipher.decrypt(&mut block);
116 assert_eq!(hex!("30c81c46a35ce411e5fbc1191a0a52ef"), block);
117
118 block = hex!("23304b7a39f9f3ff067d8d8f9e24ecc7");
119 dec_cipher.decrypt(&mut block);
120 assert_eq!(hex!("f69f2445df4f9b17ad2b417be66c3710"), block);
121 }
122
123 /// Generates the test cases to validate the AES-128 implementation.
124 /// For example, to test `MyAes128Impl`:
125 ///
126 /// ```
127 /// use crypto_provider::aes::testing::*;
128 ///
129 /// mod tests {
130 /// #[apply(aes_128_encrypt_test_cases)]
131 /// fn aes_128_tests(f: CryptoProviderTestCase<MyAes128Impl>) {
132 /// f(MyAes128Impl);
133 /// }
134 /// }
135 /// ```
136 #[template]
137 #[export]
138 #[rstest]
139 #[case::encrypt(aes_128_test_encrypt)]
aes_128_encrypt_test_cases<A: AesFactory<Key = Aes128Key>>( #[case] testcase: CryptoProviderTestCase<F>, )140 fn aes_128_encrypt_test_cases<A: AesFactory<Key = Aes128Key>>(
141 #[case] testcase: CryptoProviderTestCase<F>,
142 ) {
143 }
144
145 /// Generates the test cases to validate the AES-128 implementation.
146 /// For example, to test `MyAes128Impl`:
147 ///
148 /// ```
149 /// use crypto_provider::aes::testing::*;
150 ///
151 /// mod tests {
152 /// #[apply(aes_128_decrypt_test_cases)]
153 /// fn aes_128_tests(f: CryptoProviderTestCase<MyAes128Impl>) {
154 /// f(MyAes128Impl);
155 /// }
156 /// }
157 /// ```
158 #[template]
159 #[export]
160 #[rstest]
161 #[case::decrypt(aes_128_test_decrypt)]
aes_128_decrypt_test_cases<F: AesFactory<Key = Aes128Key>>( #[case] testcase: CryptoProviderTestCase<F>, )162 fn aes_128_decrypt_test_cases<F: AesFactory<Key = Aes128Key>>(
163 #[case] testcase: CryptoProviderTestCase<F>,
164 ) {
165 }
166
167 /// Generates the test cases to validate the AES-256 implementation.
168 /// For example, to test `MyAes256Impl`:
169 ///
170 /// ```
171 /// use crypto_provider::aes::testing::*;
172 ///
173 /// mod tests {
174 /// #[apply(aes_256_encrypt_test_cases)]
175 /// fn aes_256_tests(f: CryptoProviderTestCase<MyAes256Impl>) {
176 /// f(MyAes256Impl);
177 /// }
178 /// }
179 /// ```
180 #[template]
181 #[export]
182 #[rstest]
183 #[case::encrypt(aes_256_test_encrypt)]
aes_256_encrypt_test_cases<F: AesFactory<Key = Aes256Key>>( #[case] testcase: CryptoProviderTestCase<F>, )184 fn aes_256_encrypt_test_cases<F: AesFactory<Key = Aes256Key>>(
185 #[case] testcase: CryptoProviderTestCase<F>,
186 ) {
187 }
188
189 /// Generates the test cases to validate the AES-256 implementation.
190 /// For example, to test `MyAes256Impl`:
191 ///
192 /// ```
193 /// use crypto_provider::aes::testing::*;
194 ///
195 /// mod tests {
196 /// #[apply(aes_256_decrypt_test_cases)]
197 /// fn aes_256_tests(f: CryptoProviderTestCase<MyAes256Impl>) {
198 /// f(MyAes256Impl);
199 /// }
200 /// }
201 /// ```
202 #[template]
203 #[export]
204 #[rstest]
205 #[case::decrypt(aes_256_test_decrypt)]
aes_256_decrypt_test_cases<F: AesFactory<Key = Aes256Key>>( #[case] testcase: CryptoProviderTestCase<F>, )206 fn aes_256_decrypt_test_cases<F: AesFactory<Key = Aes256Key>>(
207 #[case] testcase: CryptoProviderTestCase<F>,
208 ) {
209 }
210