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 use crate::aes::{Aes128Key, Aes256Key};
15 pub use crate::prelude;
16 use core::marker;
17 use crypto_provider::aes::ctr::{AesCtr, NonceAndCounter};
18 use hex_literal::hex;
19 use rstest_reuse::template;
20
21 /// Test AES-128-CTR encryption
aes_128_ctr_test_encrypt<A: AesCtr<Key = Aes128Key>>(_marker: marker::PhantomData<A>)22 pub fn aes_128_ctr_test_encrypt<A: AesCtr<Key = Aes128Key>>(_marker: marker::PhantomData<A>) {
23 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.5.1
24 let key: Aes128Key = hex!("2b7e151628aed2a6abf7158809cf4f3c").into();
25 let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
26 let mut block: [u8; 16];
27 let mut cipher = A::new(&key, NonceAndCounter::from_block(iv));
28
29 block = hex!("6bc1bee22e409f96e93d7e117393172a");
30 cipher.apply_keystream(&mut block);
31 let expected_ciphertext_1 = hex!("874d6191b620e3261bef6864990db6ce");
32 assert_eq!(expected_ciphertext_1, block);
33
34 block = hex!("ae2d8a571e03ac9c9eb76fac45af8e51");
35 cipher.apply_keystream(&mut block);
36 let expected_ciphertext_2 = hex!("9806f66b7970fdff8617187bb9fffdff");
37 assert_eq!(expected_ciphertext_2, block);
38
39 block = hex!("30c81c46a35ce411e5fbc1191a0a52ef");
40 cipher.apply_keystream(&mut block);
41 let expected_ciphertext_3 = hex!("5ae4df3edbd5d35e5b4f09020db03eab");
42 assert_eq!(expected_ciphertext_3, block);
43
44 block = hex!("f69f2445df4f9b17ad2b417be66c3710");
45 cipher.apply_keystream(&mut block);
46 let expected_ciphertext_3 = hex!("1e031dda2fbe03d1792170a0f3009cee");
47 assert_eq!(expected_ciphertext_3, block);
48 }
49
50 /// Test AES-128-CTR decryption
aes_128_ctr_test_decrypt<A: AesCtr<Key = Aes128Key>>(_marker: marker::PhantomData<A>)51 pub fn aes_128_ctr_test_decrypt<A: AesCtr<Key = Aes128Key>>(_marker: marker::PhantomData<A>) {
52 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.5.2
53 let key: Aes128Key = hex!("2b7e151628aed2a6abf7158809cf4f3c").into();
54 let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
55 let mut block: [u8; 16];
56 let mut cipher = A::new(&key, NonceAndCounter::from_block(iv));
57
58 block = hex!("874d6191b620e3261bef6864990db6ce");
59 cipher.apply_keystream(&mut block);
60 let expected_plaintext_1 = hex!("6bc1bee22e409f96e93d7e117393172a");
61 assert_eq!(expected_plaintext_1, block);
62
63 block = hex!("9806f66b7970fdff8617187bb9fffdff");
64 cipher.apply_keystream(&mut block);
65 let expected_plaintext_2 = hex!("ae2d8a571e03ac9c9eb76fac45af8e51");
66 assert_eq!(expected_plaintext_2, block);
67
68 block = hex!("5ae4df3edbd5d35e5b4f09020db03eab");
69 cipher.apply_keystream(&mut block);
70 let expected_plaintext_3 = hex!("30c81c46a35ce411e5fbc1191a0a52ef");
71 assert_eq!(expected_plaintext_3, block);
72
73 block = hex!("1e031dda2fbe03d1792170a0f3009cee");
74 cipher.apply_keystream(&mut block);
75 let expected_plaintext_3 = hex!("f69f2445df4f9b17ad2b417be66c3710");
76 assert_eq!(expected_plaintext_3, block);
77 }
78
79 /// Test AES-256-CTR encryption
aes_256_ctr_test_encrypt<A: AesCtr<Key = Aes256Key>>(_marker: marker::PhantomData<A>)80 pub fn aes_256_ctr_test_encrypt<A: AesCtr<Key = Aes256Key>>(_marker: marker::PhantomData<A>) {
81 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.5.5
82 let key: Aes256Key =
83 hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").into();
84 let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
85 let mut block: [u8; 16];
86 let mut cipher = A::new(&key, NonceAndCounter::from_block(iv));
87
88 block = hex!("6bc1bee22e409f96e93d7e117393172a");
89 cipher.apply_keystream(&mut block);
90 let expected_ciphertext_1 = hex!("601ec313775789a5b7a7f504bbf3d228");
91 assert_eq!(expected_ciphertext_1, block);
92
93 block = hex!("ae2d8a571e03ac9c9eb76fac45af8e51");
94 cipher.apply_keystream(&mut block);
95 let expected_ciphertext_2 = hex!("f443e3ca4d62b59aca84e990cacaf5c5");
96 assert_eq!(expected_ciphertext_2, block);
97
98 block = hex!("30c81c46a35ce411e5fbc1191a0a52ef");
99 cipher.apply_keystream(&mut block);
100 let expected_ciphertext_3 = hex!("2b0930daa23de94ce87017ba2d84988d");
101 assert_eq!(expected_ciphertext_3, block);
102
103 block = hex!("f69f2445df4f9b17ad2b417be66c3710");
104 cipher.apply_keystream(&mut block);
105 let expected_ciphertext_3 = hex!("dfc9c58db67aada613c2dd08457941a6");
106 assert_eq!(expected_ciphertext_3, block);
107 }
108
109 /// Test AES-256-CTR decryption
aes_256_ctr_test_decrypt<A: AesCtr<Key = Aes256Key>>(_marker: marker::PhantomData<A>)110 pub fn aes_256_ctr_test_decrypt<A: AesCtr<Key = Aes256Key>>(_marker: marker::PhantomData<A>) {
111 // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf F.5.6
112 let key: Aes256Key =
113 hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4").into();
114 let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
115 let mut block: [u8; 16];
116 let mut cipher = A::new(&key, NonceAndCounter::from_block(iv));
117
118 block = hex!("601ec313775789a5b7a7f504bbf3d228");
119 cipher.apply_keystream(&mut block);
120 let expected_plaintext_1 = hex!("6bc1bee22e409f96e93d7e117393172a");
121 assert_eq!(expected_plaintext_1, block);
122
123 block = hex!("f443e3ca4d62b59aca84e990cacaf5c5");
124 cipher.apply_keystream(&mut block);
125 let expected_plaintext_2 = hex!("ae2d8a571e03ac9c9eb76fac45af8e51");
126 assert_eq!(expected_plaintext_2, block);
127
128 block = hex!("2b0930daa23de94ce87017ba2d84988d");
129 cipher.apply_keystream(&mut block);
130 let expected_plaintext_3 = hex!("30c81c46a35ce411e5fbc1191a0a52ef");
131 assert_eq!(expected_plaintext_3, block);
132
133 block = hex!("dfc9c58db67aada613c2dd08457941a6");
134 cipher.apply_keystream(&mut block);
135 let expected_plaintext_3 = hex!("f69f2445df4f9b17ad2b417be66c3710");
136 assert_eq!(expected_plaintext_3, block);
137 }
138
139 /// Generates the test cases to validate the AES-128-CTR implementation.
140 /// For example, to test `MyAesCtr128Impl`:
141 ///
142 /// ```
143 /// use crypto_provider::aes::ctr::testing::*;
144 ///
145 /// mod tests {
146 /// #[apply(aes_128_ctr_test_cases)]
147 /// fn aes_128_ctr_tests(testcase: CryptoProviderTestCase<MyAesCtr128Impl>) {
148 /// testcase(MyAesCtr128Impl);
149 /// }
150 /// }
151 /// ```
152 #[template]
153 #[export]
154 #[rstest]
155 #[case::encrypt(aes_128_ctr_test_encrypt)]
156 #[case::decrypt(aes_128_ctr_test_decrypt)]
aes_128_ctr_test_cases<F: AesCtrFactory<Key = Aes128Key>>( #[case] testcase: CryptoProviderTestCase<F>, )157 fn aes_128_ctr_test_cases<F: AesCtrFactory<Key = Aes128Key>>(
158 #[case] testcase: CryptoProviderTestCase<F>,
159 ) {
160 }
161
162 /// Generates the test cases to validate the AES-256-CTR implementation.
163 /// For example, to test `MyAesCtr256Impl`:
164 ///
165 /// ```
166 /// use crypto_provider::aes::ctr::testing::*;
167 ///
168 /// mod tests {
169 /// #[apply(aes_256_ctr_test_cases_impl)]
170 /// fn aes_256_ctr_tests(testcase: CryptoProviderTestCase<MyAesCtr256Impl>) {
171 /// testcase(MyAesCtr256Impl);
172 /// }
173 /// }
174 /// ```
175 #[template]
176 #[export]
177 #[rstest]
178 #[case::encrypt(aes_256_ctr_test_encrypt)]
179 #[case::decrypt(aes_256_ctr_test_decrypt)]
aes_256_ctr_test_cases<F: AesCtrFactory<Key = Aes256Key>>( #[case] testcase: CryptoProviderTestCase<F>, )180 fn aes_256_ctr_test_cases<F: AesCtrFactory<Key = Aes256Key>>(
181 #[case] testcase: CryptoProviderTestCase<F>,
182 ) {
183 }
184