blob: 0692c8d1b248b0682854d3720afee4acd08428c7 [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
Paul Kehrer2b758672013-10-30 09:01:38 -050014import os
Donald Stufft9e1a48b2013-08-09 00:32:30 -040015import textwrap
16
Paul Kehrer79c16e92013-10-18 17:44:36 -050017import pytest
18
Alex Gaynorafdddca2013-10-21 21:00:20 -070019from .utils import (
20 load_nist_vectors, load_nist_vectors_from_file, load_cryptrec_vectors,
21 load_cryptrec_vectors_from_file, load_openssl_vectors,
22 load_openssl_vectors_from_file, load_hash_vectors,
23 load_hash_vectors_from_file
24)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040025
26
27def test_load_nist_vectors_encrypt():
28 vector_data = textwrap.dedent("""
29 # CAVS 11.1
30 # Config info for aes_values
31 # AESVS GFSbox test data for CBC
32 # State : Encrypt and Decrypt
33 # Key Length : 128
34 # Generated on Fri Apr 22 15:11:33 2011
35
36 [ENCRYPT]
37
38 COUNT = 0
39 KEY = 00000000000000000000000000000000
40 IV = 00000000000000000000000000000000
41 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
42 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
43
44 COUNT = 1
45 KEY = 00000000000000000000000000000000
46 IV = 00000000000000000000000000000000
47 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
48 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
49
50 [DECRYPT]
51
52 COUNT = 0
53 KEY = 00000000000000000000000000000000
54 IV = 00000000000000000000000000000000
55 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
56 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
57
58 COUNT = 1
59 KEY = 00000000000000000000000000000000
60 IV = 00000000000000000000000000000000
61 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
62 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
63 """).splitlines()
64
Alex Gaynor1fe70b12013-10-16 11:59:17 -070065 assert load_nist_vectors(vector_data, "ENCRYPT") == [
66 {
67 "key": b"00000000000000000000000000000000",
68 "iv": b"00000000000000000000000000000000",
69 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
70 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
71 },
72 {
73 "key": b"00000000000000000000000000000000",
74 "iv": b"00000000000000000000000000000000",
75 "plaintext": b"9798c4640bad75c7c3227db910174e72",
76 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
77 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -040078 ]
79
80
81def test_load_nist_vectors_decrypt():
82 vector_data = textwrap.dedent("""
83 # CAVS 11.1
84 # Config info for aes_values
85 # AESVS GFSbox test data for CBC
86 # State : Encrypt and Decrypt
87 # Key Length : 128
88 # Generated on Fri Apr 22 15:11:33 2011
89
90 [ENCRYPT]
91
92 COUNT = 0
93 KEY = 00000000000000000000000000000000
94 IV = 00000000000000000000000000000000
95 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
96 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
97
98 COUNT = 1
99 KEY = 00000000000000000000000000000000
100 IV = 00000000000000000000000000000000
101 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
102 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
103
104 [DECRYPT]
105
106 COUNT = 0
107 KEY = 00000000000000000000000000000000
108 IV = 00000000000000000000000000000000
109 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
110 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
111
112 COUNT = 1
113 KEY = 00000000000000000000000000000000
114 IV = 00000000000000000000000000000000
115 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
116 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
117 """).splitlines()
118
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700119 assert load_nist_vectors(vector_data, "DECRYPT") == [
120 {
121 "key": b"00000000000000000000000000000000",
122 "iv": b"00000000000000000000000000000000",
123 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
124 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
125 },
126 {
127 "key": b"00000000000000000000000000000000",
128 "iv": b"00000000000000000000000000000000",
129 "plaintext": b"9798c4640bad75c7c3227db910174e72",
130 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
131 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400132 ]
133
134
135def test_load_nist_vectors_from_file_encrypt():
136 assert load_nist_vectors_from_file(
Paul Kehrer20884bf2013-10-31 12:08:11 -0500137 os.path.join("ciphers", "AES", "CBC", "CBCGFSbox128.rsp"),
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700138 "ENCRYPT"
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400139 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700140 {
141 "key": b"00000000000000000000000000000000",
142 "iv": b"00000000000000000000000000000000",
143 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
144 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
145 },
146 {
147 "key": b"00000000000000000000000000000000",
148 "iv": b"00000000000000000000000000000000",
149 "plaintext": b"9798c4640bad75c7c3227db910174e72",
150 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
151 },
152 {
153 "key": b"00000000000000000000000000000000",
154 "iv": b"00000000000000000000000000000000",
155 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
156 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
157 },
158 {
159 "key": b"00000000000000000000000000000000",
160 "iv": b"00000000000000000000000000000000",
161 "plaintext": b"6a118a874519e64e9963798a503f1d35",
162 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
163 },
164 {
165 "key": b"00000000000000000000000000000000",
166 "iv": b"00000000000000000000000000000000",
167 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
168 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
169 },
170 {
171 "key": b"00000000000000000000000000000000",
172 "iv": b"00000000000000000000000000000000",
173 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
174 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
175 },
176 {
177 "key": b"00000000000000000000000000000000",
178 "iv": b"00000000000000000000000000000000",
179 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
180 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
181 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400182 ]
183
184
Paul Kehrer1951bf62013-09-15 12:05:43 -0500185def test_load_nist_vectors_from_file_decrypt():
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400186 assert load_nist_vectors_from_file(
Paul Kehrer20884bf2013-10-31 12:08:11 -0500187 os.path.join("ciphers", "AES", "CBC", "CBCGFSbox128.rsp"),
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400188 "DECRYPT",
Alex Gaynor745c95c2013-10-16 14:41:35 -0700189 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700190 {
191 "key": b"00000000000000000000000000000000",
192 "iv": b"00000000000000000000000000000000",
193 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
194 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
195 },
196 {
197 "key": b"00000000000000000000000000000000",
198 "iv": b"00000000000000000000000000000000",
199 "plaintext": b"9798c4640bad75c7c3227db910174e72",
200 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
201 },
202 {
203 "key": b"00000000000000000000000000000000",
204 "iv": b"00000000000000000000000000000000",
205 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
206 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
207 },
208 {
209 "key": b"00000000000000000000000000000000",
210 "iv": b"00000000000000000000000000000000",
211 "plaintext": b"6a118a874519e64e9963798a503f1d35",
212 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
213 },
214 {
215 "key": b"00000000000000000000000000000000",
216 "iv": b"00000000000000000000000000000000",
217 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
218 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
219 },
220 {
221 "key": b"00000000000000000000000000000000",
222 "iv": b"00000000000000000000000000000000",
223 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
224 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
225 },
226 {
227 "key": b"00000000000000000000000000000000",
228 "iv": b"00000000000000000000000000000000",
229 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
230 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
231 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400232 ]
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) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700257 {
258 "key": b"00000000000000000000000000000000",
259 "plaintext": b"80000000000000000000000000000000",
260 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
261 },
262 {
263 "key": b"00000000000000000000000000000000",
264 "plaintext": b"40000000000000000000000000000000",
265 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
266 },
267 {
268 "key": b"10000000000000000000000000000000",
269 "plaintext": b"80000000000000000000000000000000",
270 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
271 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500272 ]
273
274
Donald Stufft3359d7e2013-10-19 19:33:06 -0400275def test_load_cryptrec_vectors_invalid():
276 vector_data = textwrap.dedent("""
277 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
278 # Download is t_camelia.txt
279
280 # Camellia with 128-bit key
281
282 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
283 """).splitlines()
284
285 with pytest.raises(ValueError):
286 load_cryptrec_vectors(vector_data)
287
288
Paul Kehrer1951bf62013-09-15 12:05:43 -0500289def test_load_cryptrec_vectors_from_file_encrypt():
290 test_set = load_cryptrec_vectors_from_file(
Paul Kehrer2b758672013-10-30 09:01:38 -0500291 os.path.join("ciphers", "Camellia", "camellia-128-ecb.txt"),
Paul Kehrer1951bf62013-09-15 12:05:43 -0500292 )
293 assert test_set[0] == (
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700294 {
295 "key": b"00000000000000000000000000000000",
296 "plaintext": b"80000000000000000000000000000000",
297 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
298 }
Paul Kehrer1951bf62013-09-15 12:05:43 -0500299 )
300 assert len(test_set) == 1280
301
302
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500303def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500304 vector_data = textwrap.dedent(
305 """
306 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
307 # For all CFB128 encrypts and decrypts, the transformed sequence is
308 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
309 # CFB128-CAMELLIA128.Encrypt
310 """
311 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
312 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
313 "14F7646187817EB586599146B82BD719:1\n"
314 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
315 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
316 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
317 "# CFB128-CAMELLIA128.Decrypt\n"
318 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
319 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
320 "14F7646187817EB586599146B82BD719:0\n"
321 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
322 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
323 "A53D28BB82DF741103EA4F921A44880B:0"
324 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500325
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500326 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700327 {
328 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
329 "iv": b"000102030405060708090A0B0C0D0E0F",
330 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
331 "ciphertext": b"14F7646187817EB586599146B82BD719",
332 },
333 {
334 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
335 "iv": b"14F7646187817EB586599146B82BD719",
336 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
337 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
338 },
339 {
340 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
341 "iv": b"000102030405060708090A0B0C0D0E0F",
342 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
343 "ciphertext": b"14F7646187817EB586599146B82BD719",
344 },
345 {
346 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
347 "iv": b"14F7646187817EB586599146B82BD719",
348 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
349 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
350 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500351 ]
352
353
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500354def test_load_openssl_vectors_from_file():
Paul Kehrer2b758672013-10-30 09:01:38 -0500355 test_list = load_openssl_vectors_from_file(
356 os.path.join("ciphers", "Camellia", "camellia-ofb.txt")
357 )
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500358 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500359 assert test_list[:4] == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700360 {
361 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
362 "iv": b"000102030405060708090A0B0C0D0E0F",
363 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
364 "ciphertext": b"14F7646187817EB586599146B82BD719",
365 },
366 {
367 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
368 "iv": b"50FE67CC996D32B6DA0937E99BAFEC60",
369 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
370 "ciphertext": b"25623DB569CA51E01482649977E28D84",
371 },
372 {
373 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
374 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
375 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
376 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
377 },
378 {
379 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
380 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
381 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
382 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
383 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500384 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500385
386
387def test_load_hash_vectors():
388 vector_data = textwrap.dedent("""
389
390 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500391 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500392
393 Len = 0
394 Msg = 00
395 MD = d41d8cd98f00b204e9800998ecf8427e
396
397 Len = 8
398 Msg = 61
399 MD = 0cc175b9c0f1b6a831c399e269772661
400
401 Len = 24
402 Msg = 616263
403 MD = 900150983cd24fb0d6963f7d28e17f72
404
405 Len = 112
406 Msg = 6d65737361676520646967657374
407 MD = f96b697d7cb7938d525a2f31aaf161d0
408 """).splitlines()
409 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500410 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
411 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
412 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
413 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500414 ]
415
416
Paul Kehrer0317b042013-10-28 17:34:27 -0500417def test_load_hmac_vectors():
418 vector_data = textwrap.dedent("""
419Len = 224
420# "Jefe"
421Key = 4a656665
422# "what do ya want for nothing?"
423Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
424MD = 750c783e6ab0b503eaa86e310a5db738
425 """).splitlines()
426 assert load_hash_vectors(vector_data) == [
427 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
428 "750c783e6ab0b503eaa86e310a5db738",
429 b"4a656665"),
430 ]
431
432
Paul Kehrer69e06522013-10-18 17:28:39 -0500433def test_load_hash_vectors_bad_data():
434 vector_data = textwrap.dedent("""
435 # http://tools.ietf.org/html/rfc1321
436
437 Len = 0
438 Msg = 00
439 UNKNOWN=Hello World
440 """).splitlines()
441 with pytest.raises(ValueError):
442 load_hash_vectors(vector_data)
443
444
445def test_load_hash_vectors_from_file():
Paul Kehrer2b758672013-10-30 09:01:38 -0500446 test_list = load_hash_vectors_from_file(
447 os.path.join("hashes", "MD5", "rfc-1321.txt")
448 )
Paul Kehrer69e06522013-10-18 17:28:39 -0500449 assert len(test_list) == 7
450 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500451 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
452 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
453 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
454 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500455 ]