blob: f96cf004d11d18992332a069f9bf7faf9cfdacaf [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 Kehrer79c16e92013-10-18 17:44:36 -050016import pytest
17
Alex Gaynorafdddca2013-10-21 21:00:20 -070018from .utils import (
19 load_nist_vectors, load_nist_vectors_from_file, load_cryptrec_vectors,
20 load_cryptrec_vectors_from_file, load_openssl_vectors,
21 load_openssl_vectors_from_file, load_hash_vectors,
22 load_hash_vectors_from_file
23)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040024
25
26def test_load_nist_vectors_encrypt():
27 vector_data = textwrap.dedent("""
28 # CAVS 11.1
29 # Config info for aes_values
30 # AESVS GFSbox test data for CBC
31 # State : Encrypt and Decrypt
32 # Key Length : 128
33 # Generated on Fri Apr 22 15:11:33 2011
34
35 [ENCRYPT]
36
37 COUNT = 0
38 KEY = 00000000000000000000000000000000
39 IV = 00000000000000000000000000000000
40 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
41 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
42
43 COUNT = 1
44 KEY = 00000000000000000000000000000000
45 IV = 00000000000000000000000000000000
46 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
47 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
48
49 [DECRYPT]
50
51 COUNT = 0
52 KEY = 00000000000000000000000000000000
53 IV = 00000000000000000000000000000000
54 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
55 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
56
57 COUNT = 1
58 KEY = 00000000000000000000000000000000
59 IV = 00000000000000000000000000000000
60 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
61 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
62 """).splitlines()
63
Alex Gaynor1fe70b12013-10-16 11:59:17 -070064 assert load_nist_vectors(vector_data, "ENCRYPT") == [
65 {
66 "key": b"00000000000000000000000000000000",
67 "iv": b"00000000000000000000000000000000",
68 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
69 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
70 },
71 {
72 "key": b"00000000000000000000000000000000",
73 "iv": b"00000000000000000000000000000000",
74 "plaintext": b"9798c4640bad75c7c3227db910174e72",
75 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
76 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -040077 ]
78
79
80def test_load_nist_vectors_decrypt():
81 vector_data = textwrap.dedent("""
82 # CAVS 11.1
83 # Config info for aes_values
84 # AESVS GFSbox test data for CBC
85 # State : Encrypt and Decrypt
86 # Key Length : 128
87 # Generated on Fri Apr 22 15:11:33 2011
88
89 [ENCRYPT]
90
91 COUNT = 0
92 KEY = 00000000000000000000000000000000
93 IV = 00000000000000000000000000000000
94 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
95 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
96
97 COUNT = 1
98 KEY = 00000000000000000000000000000000
99 IV = 00000000000000000000000000000000
100 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
101 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
102
103 [DECRYPT]
104
105 COUNT = 0
106 KEY = 00000000000000000000000000000000
107 IV = 00000000000000000000000000000000
108 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
109 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
110
111 COUNT = 1
112 KEY = 00000000000000000000000000000000
113 IV = 00000000000000000000000000000000
114 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
115 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
116 """).splitlines()
117
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700118 assert load_nist_vectors(vector_data, "DECRYPT") == [
119 {
120 "key": b"00000000000000000000000000000000",
121 "iv": b"00000000000000000000000000000000",
122 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
123 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
124 },
125 {
126 "key": b"00000000000000000000000000000000",
127 "iv": b"00000000000000000000000000000000",
128 "plaintext": b"9798c4640bad75c7c3227db910174e72",
129 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
130 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400131 ]
132
133
134def test_load_nist_vectors_from_file_encrypt():
135 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400136 "AES/KAT/CBCGFSbox128.rsp",
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700137 "ENCRYPT"
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400138 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700139 {
140 "key": b"00000000000000000000000000000000",
141 "iv": b"00000000000000000000000000000000",
142 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
143 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
144 },
145 {
146 "key": b"00000000000000000000000000000000",
147 "iv": b"00000000000000000000000000000000",
148 "plaintext": b"9798c4640bad75c7c3227db910174e72",
149 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
150 },
151 {
152 "key": b"00000000000000000000000000000000",
153 "iv": b"00000000000000000000000000000000",
154 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
155 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
156 },
157 {
158 "key": b"00000000000000000000000000000000",
159 "iv": b"00000000000000000000000000000000",
160 "plaintext": b"6a118a874519e64e9963798a503f1d35",
161 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
162 },
163 {
164 "key": b"00000000000000000000000000000000",
165 "iv": b"00000000000000000000000000000000",
166 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
167 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
168 },
169 {
170 "key": b"00000000000000000000000000000000",
171 "iv": b"00000000000000000000000000000000",
172 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
173 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
174 },
175 {
176 "key": b"00000000000000000000000000000000",
177 "iv": b"00000000000000000000000000000000",
178 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
179 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
180 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400181 ]
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",
Alex Gaynor745c95c2013-10-16 14:41:35 -0700188 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700189 {
190 "key": b"00000000000000000000000000000000",
191 "iv": b"00000000000000000000000000000000",
192 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
193 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
194 },
195 {
196 "key": b"00000000000000000000000000000000",
197 "iv": b"00000000000000000000000000000000",
198 "plaintext": b"9798c4640bad75c7c3227db910174e72",
199 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
200 },
201 {
202 "key": b"00000000000000000000000000000000",
203 "iv": b"00000000000000000000000000000000",
204 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
205 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
206 },
207 {
208 "key": b"00000000000000000000000000000000",
209 "iv": b"00000000000000000000000000000000",
210 "plaintext": b"6a118a874519e64e9963798a503f1d35",
211 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
212 },
213 {
214 "key": b"00000000000000000000000000000000",
215 "iv": b"00000000000000000000000000000000",
216 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
217 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
218 },
219 {
220 "key": b"00000000000000000000000000000000",
221 "iv": b"00000000000000000000000000000000",
222 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
223 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
224 },
225 {
226 "key": b"00000000000000000000000000000000",
227 "iv": b"00000000000000000000000000000000",
228 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
229 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
230 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400231 ]
Paul Kehrer1951bf62013-09-15 12:05:43 -0500232
233
234def test_load_cryptrec_vectors():
235 vector_data = textwrap.dedent("""
236 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
237 # Download is t_camelia.txt
238
239 # Camellia with 128-bit key
240
241 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
242
243 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
244 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
245
246 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
247 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
248
249 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
250
251 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
252 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
253 """).splitlines()
254
255 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700256 {
257 "key": b"00000000000000000000000000000000",
258 "plaintext": b"80000000000000000000000000000000",
259 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
260 },
261 {
262 "key": b"00000000000000000000000000000000",
263 "plaintext": b"40000000000000000000000000000000",
264 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
265 },
266 {
267 "key": b"10000000000000000000000000000000",
268 "plaintext": b"80000000000000000000000000000000",
269 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
270 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500271 ]
272
273
Donald Stufft3359d7e2013-10-19 19:33:06 -0400274def test_load_cryptrec_vectors_invalid():
275 vector_data = textwrap.dedent("""
276 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
277 # Download is t_camelia.txt
278
279 # Camellia with 128-bit key
280
281 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
282 """).splitlines()
283
284 with pytest.raises(ValueError):
285 load_cryptrec_vectors(vector_data)
286
287
Paul Kehrer1951bf62013-09-15 12:05:43 -0500288def test_load_cryptrec_vectors_from_file_encrypt():
289 test_set = load_cryptrec_vectors_from_file(
290 "Camellia/NTT/camellia-128-ecb.txt"
291 )
292 assert test_set[0] == (
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700293 {
294 "key": b"00000000000000000000000000000000",
295 "plaintext": b"80000000000000000000000000000000",
296 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
297 }
Paul Kehrer1951bf62013-09-15 12:05:43 -0500298 )
299 assert len(test_set) == 1280
300
301
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500302def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500303 vector_data = textwrap.dedent(
304 """
305 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
306 # For all CFB128 encrypts and decrypts, the transformed sequence is
307 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
308 # CFB128-CAMELLIA128.Encrypt
309 """
310 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
311 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
312 "14F7646187817EB586599146B82BD719:1\n"
313 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
314 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
315 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
316 "# CFB128-CAMELLIA128.Decrypt\n"
317 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
318 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
319 "14F7646187817EB586599146B82BD719:0\n"
320 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
321 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
322 "A53D28BB82DF741103EA4F921A44880B:0"
323 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500324
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500325 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700326 {
327 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
328 "iv": b"000102030405060708090A0B0C0D0E0F",
329 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
330 "ciphertext": b"14F7646187817EB586599146B82BD719",
331 },
332 {
333 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
334 "iv": b"14F7646187817EB586599146B82BD719",
335 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
336 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
337 },
338 {
339 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
340 "iv": b"000102030405060708090A0B0C0D0E0F",
341 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
342 "ciphertext": b"14F7646187817EB586599146B82BD719",
343 },
344 {
345 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
346 "iv": b"14F7646187817EB586599146B82BD719",
347 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
348 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
349 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500350 ]
351
352
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500353def test_load_openssl_vectors_from_file():
354 test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt")
355 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500356 assert test_list[:4] == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700357 {
358 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
359 "iv": b"000102030405060708090A0B0C0D0E0F",
360 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
361 "ciphertext": b"14F7646187817EB586599146B82BD719",
362 },
363 {
364 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
365 "iv": b"50FE67CC996D32B6DA0937E99BAFEC60",
366 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
367 "ciphertext": b"25623DB569CA51E01482649977E28D84",
368 },
369 {
370 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
371 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
372 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
373 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
374 },
375 {
376 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
377 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
378 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
379 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
380 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500381 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500382
383
384def test_load_hash_vectors():
385 vector_data = textwrap.dedent("""
386
387 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500388 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500389
390 Len = 0
391 Msg = 00
392 MD = d41d8cd98f00b204e9800998ecf8427e
393
394 Len = 8
395 Msg = 61
396 MD = 0cc175b9c0f1b6a831c399e269772661
397
398 Len = 24
399 Msg = 616263
400 MD = 900150983cd24fb0d6963f7d28e17f72
401
402 Len = 112
403 Msg = 6d65737361676520646967657374
404 MD = f96b697d7cb7938d525a2f31aaf161d0
405 """).splitlines()
406 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500407 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
408 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
409 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
410 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500411 ]
412
413
414def test_load_hash_vectors_bad_data():
415 vector_data = textwrap.dedent("""
416 # http://tools.ietf.org/html/rfc1321
417
418 Len = 0
419 Msg = 00
420 UNKNOWN=Hello World
421 """).splitlines()
422 with pytest.raises(ValueError):
423 load_hash_vectors(vector_data)
424
425
426def test_load_hash_vectors_from_file():
427 test_list = load_hash_vectors_from_file("RFC/MD5/rfc-1321.txt")
428 assert len(test_list) == 7
429 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500430 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
431 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
432 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
433 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500434 ]