blob: 73394a510540c7fc6588c45532370a6f7ce7aeba [file] [log] [blame]
Alex Gaynorf312a5c2013-08-10 15:23:38 -04001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
Donald Stufft9e1a48b2013-08-09 00:32:30 -040014import textwrap
15
Paul Kehrer1951bf62013-09-15 12:05:43 -050016from .utils import (load_nist_vectors, load_nist_vectors_from_file,
17 load_cryptrec_vectors, load_cryptrec_vectors_from_file,
18 load_openssl_vectors, load_openssl_vectors_from_file)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040019
20
21def test_load_nist_vectors_encrypt():
22 vector_data = textwrap.dedent("""
23 # CAVS 11.1
24 # Config info for aes_values
25 # AESVS GFSbox test data for CBC
26 # State : Encrypt and Decrypt
27 # Key Length : 128
28 # Generated on Fri Apr 22 15:11:33 2011
29
30 [ENCRYPT]
31
32 COUNT = 0
33 KEY = 00000000000000000000000000000000
34 IV = 00000000000000000000000000000000
35 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
36 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
37
38 COUNT = 1
39 KEY = 00000000000000000000000000000000
40 IV = 00000000000000000000000000000000
41 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
42 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
43
44 [DECRYPT]
45
46 COUNT = 0
47 KEY = 00000000000000000000000000000000
48 IV = 00000000000000000000000000000000
49 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
50 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
51
52 COUNT = 1
53 KEY = 00000000000000000000000000000000
54 IV = 00000000000000000000000000000000
55 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
56 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
57 """).splitlines()
58
59 assert load_nist_vectors(vector_data, "ENCRYPT",
60 ["key", "iv", "plaintext", "ciphertext"],
61 ) == [
62 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -040063 b"00000000000000000000000000000000",
64 b"00000000000000000000000000000000",
65 b"f34481ec3cc627bacd5dc3fb08f273e6",
66 b"0336763e966d92595a567cc9ce537f5e",
Donald Stufft9e1a48b2013-08-09 00:32:30 -040067 ),
68 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -040069 b"00000000000000000000000000000000",
70 b"00000000000000000000000000000000",
71 b"9798c4640bad75c7c3227db910174e72",
72 b"a9a1631bf4996954ebc093957b234589",
Donald Stufft9e1a48b2013-08-09 00:32:30 -040073 ),
74 ]
75
76
77def test_load_nist_vectors_decrypt():
78 vector_data = textwrap.dedent("""
79 # CAVS 11.1
80 # Config info for aes_values
81 # AESVS GFSbox test data for CBC
82 # State : Encrypt and Decrypt
83 # Key Length : 128
84 # Generated on Fri Apr 22 15:11:33 2011
85
86 [ENCRYPT]
87
88 COUNT = 0
89 KEY = 00000000000000000000000000000000
90 IV = 00000000000000000000000000000000
91 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
92 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
93
94 COUNT = 1
95 KEY = 00000000000000000000000000000000
96 IV = 00000000000000000000000000000000
97 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
98 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
99
100 [DECRYPT]
101
102 COUNT = 0
103 KEY = 00000000000000000000000000000000
104 IV = 00000000000000000000000000000000
105 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
106 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
107
108 COUNT = 1
109 KEY = 00000000000000000000000000000000
110 IV = 00000000000000000000000000000000
111 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
112 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
113 """).splitlines()
114
115 assert load_nist_vectors(vector_data, "DECRYPT",
116 ["key", "iv", "ciphertext", "plaintext"],
117 ) == [
118 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400119 b"00000000000000000000000000000000",
120 b"00000000000000000000000000000000",
121 b"0336763e966d92595a567cc9ce537f5e",
122 b"f34481ec3cc627bacd5dc3fb08f273e6",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400123 ),
124 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400125 b"00000000000000000000000000000000",
126 b"00000000000000000000000000000000",
127 b"a9a1631bf4996954ebc093957b234589",
128 b"9798c4640bad75c7c3227db910174e72",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400129 ),
130 ]
131
132
133def test_load_nist_vectors_from_file_encrypt():
134 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400135 "AES/KAT/CBCGFSbox128.rsp",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400136 "ENCRYPT",
137 ["key", "iv", "plaintext", "ciphertext"],
138 ) == [
139 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400140 b"00000000000000000000000000000000",
141 b"00000000000000000000000000000000",
142 b"f34481ec3cc627bacd5dc3fb08f273e6",
143 b"0336763e966d92595a567cc9ce537f5e",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400144 ),
145 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400146 b"00000000000000000000000000000000",
147 b"00000000000000000000000000000000",
148 b"9798c4640bad75c7c3227db910174e72",
149 b"a9a1631bf4996954ebc093957b234589",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400150 ),
151 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400152 b"00000000000000000000000000000000",
153 b"00000000000000000000000000000000",
154 b"96ab5c2ff612d9dfaae8c31f30c42168",
155 b"ff4f8391a6a40ca5b25d23bedd44a597",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400156 ),
157 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400158 b"00000000000000000000000000000000",
159 b"00000000000000000000000000000000",
160 b"6a118a874519e64e9963798a503f1d35",
161 b"dc43be40be0e53712f7e2bf5ca707209",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400162 ),
163 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400164 b"00000000000000000000000000000000",
165 b"00000000000000000000000000000000",
166 b"cb9fceec81286ca3e989bd979b0cb284",
167 b"92beedab1895a94faa69b632e5cc47ce",
168 ),
169 (
170 b"00000000000000000000000000000000",
171 b"00000000000000000000000000000000",
172 b"b26aeb1874e47ca8358ff22378f09144",
173 b"459264f4798f6a78bacb89c15ed3d601",
174 ),
175 (
176 b"00000000000000000000000000000000",
177 b"00000000000000000000000000000000",
178 b"58c8e00b2631686d54eab84b91f0aca1",
179 b"08a4e2efec8a8e3312ca7460b9040bbf",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400180 ),
181 ]
182
183
Paul Kehrer1951bf62013-09-15 12:05:43 -0500184def test_load_nist_vectors_from_file_decrypt():
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400185 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400186 "AES/KAT/CBCGFSbox128.rsp",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400187 "DECRYPT",
188 ["key", "iv", "ciphertext", "plaintext"],
189 ) == [
190 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400191 b"00000000000000000000000000000000",
192 b"00000000000000000000000000000000",
193 b"0336763e966d92595a567cc9ce537f5e",
194 b"f34481ec3cc627bacd5dc3fb08f273e6",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400195 ),
196 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400197 b"00000000000000000000000000000000",
198 b"00000000000000000000000000000000",
199 b"a9a1631bf4996954ebc093957b234589",
200 b"9798c4640bad75c7c3227db910174e72",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400201 ),
202 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400203 b"00000000000000000000000000000000",
204 b"00000000000000000000000000000000",
205 b"ff4f8391a6a40ca5b25d23bedd44a597",
206 b"96ab5c2ff612d9dfaae8c31f30c42168",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400207 ),
208 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400209 b"00000000000000000000000000000000",
210 b"00000000000000000000000000000000",
211 b"dc43be40be0e53712f7e2bf5ca707209",
212 b"6a118a874519e64e9963798a503f1d35",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400213 ),
214 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400215 b"00000000000000000000000000000000",
216 b"00000000000000000000000000000000",
217 b"92beedab1895a94faa69b632e5cc47ce",
218 b"cb9fceec81286ca3e989bd979b0cb284",
219 ),
220 (
221 b"00000000000000000000000000000000",
222 b"00000000000000000000000000000000",
223 b"459264f4798f6a78bacb89c15ed3d601",
224 b"b26aeb1874e47ca8358ff22378f09144"
225 ),
226 (
227 b"00000000000000000000000000000000",
228 b"00000000000000000000000000000000",
229 b"08a4e2efec8a8e3312ca7460b9040bbf",
230 b"58c8e00b2631686d54eab84b91f0aca1"
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400231 ),
232 ]
Paul Kehrer1951bf62013-09-15 12:05:43 -0500233
234
235def test_load_cryptrec_vectors():
236 vector_data = textwrap.dedent("""
237 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
238 # Download is t_camelia.txt
239
240 # Camellia with 128-bit key
241
242 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
243
244 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
245 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
246
247 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
248 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
249
250 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
251
252 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
253 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
254 """).splitlines()
255
256 assert load_cryptrec_vectors(vector_data) == [
257 (
258 b"00000000000000000000000000000000",
259 b"80000000000000000000000000000000",
260 b"07923A39EB0A817D1C4D87BDB82D1F1C",
261 ),
262 (
263 b"00000000000000000000000000000000",
264 b"40000000000000000000000000000000",
265 b"48CD6419809672D2349260D89A08D3D3",
266 ),
267 (
268 b"10000000000000000000000000000000",
269 b"80000000000000000000000000000000",
270 b"07923A39EB0A817D1C4D87BDB82D1F1C",
271 ),
272 ]
273
274
275def test_load_cryptrec_vectors_from_file_encrypt():
276 test_set = load_cryptrec_vectors_from_file(
277 "Camellia/NTT/camellia-128-ecb.txt"
278 )
279 assert test_set[0] == (
280 (
281 b"00000000000000000000000000000000",
282 b"80000000000000000000000000000000",
283 b"07923A39EB0A817D1C4D87BDB82D1F1C",
284 )
285 )
286 assert len(test_set) == 1280
287
288
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500289def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500290 vector_data = textwrap.dedent(
291 """
292 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
293 # For all CFB128 encrypts and decrypts, the transformed sequence is
294 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
295 # CFB128-CAMELLIA128.Encrypt
296 """
297 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
298 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
299 "14F7646187817EB586599146B82BD719:1\n"
300 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
301 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
302 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
303 "# CFB128-CAMELLIA128.Decrypt\n"
304 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
305 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
306 "14F7646187817EB586599146B82BD719:0\n"
307 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
308 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
309 "A53D28BB82DF741103EA4F921A44880B:0"
310 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500311
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500312 assert load_openssl_vectors(vector_data) == [
313 (
314 b"2B7E151628AED2A6ABF7158809CF4F3C",
315 b"000102030405060708090A0B0C0D0E0F",
316 b"6BC1BEE22E409F96E93D7E117393172A",
317 b"14F7646187817EB586599146B82BD719",
318 ),
319 (
320 b"2B7E151628AED2A6ABF7158809CF4F3C",
321 b"14F7646187817EB586599146B82BD719",
322 b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
323 b"A53D28BB82DF741103EA4F921A44880B",
324 ),
Paul Kehrer1951bf62013-09-15 12:05:43 -0500325 (
326 b"2B7E151628AED2A6ABF7158809CF4F3C",
327 b"000102030405060708090A0B0C0D0E0F",
328 b"6BC1BEE22E409F96E93D7E117393172A",
329 b"14F7646187817EB586599146B82BD719",
330 ),
331 (
332 b"2B7E151628AED2A6ABF7158809CF4F3C",
333 b"14F7646187817EB586599146B82BD719",
334 b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
335 b"A53D28BB82DF741103EA4F921A44880B",
336 ),
337 ]
338
339
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500340def test_load_openssl_vectors_from_file():
341 test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt")
342 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500343 assert test_list[:4] == [
344 (
345 b"2B7E151628AED2A6ABF7158809CF4F3C",
346 b"000102030405060708090A0B0C0D0E0F",
347 b"6BC1BEE22E409F96E93D7E117393172A",
348 b"14F7646187817EB586599146B82BD719",
349 ),
350 (
351 b"2B7E151628AED2A6ABF7158809CF4F3C",
352 b"50FE67CC996D32B6DA0937E99BAFEC60",
353 b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
354 b"25623DB569CA51E01482649977E28D84",
355 ),
356 (
357 b"2B7E151628AED2A6ABF7158809CF4F3C",
358 b"D9A4DADA0892239F6B8B3D7680E15674",
359 b"30C81C46A35CE411E5FBC1191A0A52EF",
360 b"C776634A60729DC657D12B9FCA801E98",
361 ),
362 (
363 b"2B7E151628AED2A6ABF7158809CF4F3C",
364 b"A78819583F0308E7A6BF36B1386ABF23",
365 b"F69F2445DF4F9B17AD2B417BE66C3710",
366 b"D776379BE0E50825E681DA1A4C980E8E",
367 ),
368 ]