blob: 457cd86969196a6cfdffee47289ab3080b046ef3 [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 Gaynord3ce7032013-11-11 14:46:20 -080065 assert load_nist_vectors(vector_data) == [
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 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -070078 {
79 "key": b"00000000000000000000000000000000",
80 "iv": b"00000000000000000000000000000000",
81 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
82 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
83 },
84 {
85 "key": b"00000000000000000000000000000000",
86 "iv": b"00000000000000000000000000000000",
87 "plaintext": b"9798c4640bad75c7c3227db910174e72",
88 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
89 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -040090 ]
91
92
93def test_load_nist_vectors_decrypt():
94 vector_data = textwrap.dedent("""
95 # CAVS 11.1
96 # Config info for aes_values
97 # AESVS GFSbox test data for CBC
98 # State : Encrypt and Decrypt
99 # Key Length : 128
100 # Generated on Fri Apr 22 15:11:33 2011
101
102 [ENCRYPT]
103
104 COUNT = 0
105 KEY = 00000000000000000000000000000000
106 IV = 00000000000000000000000000000000
107 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
108 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
109
110 COUNT = 1
111 KEY = 00000000000000000000000000000000
112 IV = 00000000000000000000000000000000
113 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
114 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
115
116 [DECRYPT]
117
118 COUNT = 0
119 KEY = 00000000000000000000000000000000
120 IV = 00000000000000000000000000000000
121 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
122 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
123
124 COUNT = 1
125 KEY = 00000000000000000000000000000000
126 IV = 00000000000000000000000000000000
127 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
128 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
129 """).splitlines()
130
Alex Gaynord3ce7032013-11-11 14:46:20 -0800131 assert load_nist_vectors(vector_data) == [
132 {
133 "key": b"00000000000000000000000000000000",
134 "iv": b"00000000000000000000000000000000",
135 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
136 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
137 },
138 {
139 "key": b"00000000000000000000000000000000",
140 "iv": b"00000000000000000000000000000000",
141 "plaintext": b"9798c4640bad75c7c3227db910174e72",
142 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
143 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700144 {
145 "key": b"00000000000000000000000000000000",
146 "iv": b"00000000000000000000000000000000",
147 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
148 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
149 },
150 {
151 "key": b"00000000000000000000000000000000",
152 "iv": b"00000000000000000000000000000000",
153 "plaintext": b"9798c4640bad75c7c3227db910174e72",
154 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
155 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400156 ]
157
158
159def test_load_nist_vectors_from_file_encrypt():
160 assert load_nist_vectors_from_file(
Alex Gaynord3ce7032013-11-11 14:46:20 -0800161 os.path.join("ciphers", "AES", "CBC", "CBCGFSbox128.rsp")
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400162 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700163 {
164 "key": b"00000000000000000000000000000000",
165 "iv": b"00000000000000000000000000000000",
166 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
167 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
168 },
169 {
170 "key": b"00000000000000000000000000000000",
171 "iv": b"00000000000000000000000000000000",
172 "plaintext": b"9798c4640bad75c7c3227db910174e72",
173 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
174 },
175 {
176 "key": b"00000000000000000000000000000000",
177 "iv": b"00000000000000000000000000000000",
178 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
179 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
180 },
181 {
182 "key": b"00000000000000000000000000000000",
183 "iv": b"00000000000000000000000000000000",
184 "plaintext": b"6a118a874519e64e9963798a503f1d35",
185 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
186 },
187 {
188 "key": b"00000000000000000000000000000000",
189 "iv": b"00000000000000000000000000000000",
190 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
191 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
192 },
193 {
194 "key": b"00000000000000000000000000000000",
195 "iv": b"00000000000000000000000000000000",
196 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
197 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
198 },
199 {
200 "key": b"00000000000000000000000000000000",
201 "iv": b"00000000000000000000000000000000",
202 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
203 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
204 },
Alex Gaynord3ce7032013-11-11 14:46:20 -0800205 {
206 "key": b"00000000000000000000000000000000",
207 "iv": b"00000000000000000000000000000000",
208 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
209 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
210 },
211 {
212 "key": b"00000000000000000000000000000000",
213 "iv": b"00000000000000000000000000000000",
214 "plaintext": b"9798c4640bad75c7c3227db910174e72",
215 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
216 },
217 {
218 "key": b"00000000000000000000000000000000",
219 "iv": b"00000000000000000000000000000000",
220 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
221 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
222 },
223 {
224 "key": b"00000000000000000000000000000000",
225 "iv": b"00000000000000000000000000000000",
226 "plaintext": b"6a118a874519e64e9963798a503f1d35",
227 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
228 },
229 {
230 "key": b"00000000000000000000000000000000",
231 "iv": b"00000000000000000000000000000000",
232 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
233 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
234 },
235 {
236 "key": b"00000000000000000000000000000000",
237 "iv": b"00000000000000000000000000000000",
238 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
239 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
240 },
241 {
242 "key": b"00000000000000000000000000000000",
243 "iv": b"00000000000000000000000000000000",
244 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
245 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
246 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400247 ]
248
249
Paul Kehrer1951bf62013-09-15 12:05:43 -0500250def test_load_nist_vectors_from_file_decrypt():
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400251 assert load_nist_vectors_from_file(
Paul Kehrer20884bf2013-10-31 12:08:11 -0500252 os.path.join("ciphers", "AES", "CBC", "CBCGFSbox128.rsp"),
Alex Gaynor745c95c2013-10-16 14:41:35 -0700253 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700254 {
255 "key": b"00000000000000000000000000000000",
256 "iv": b"00000000000000000000000000000000",
257 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
258 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
259 },
260 {
261 "key": b"00000000000000000000000000000000",
262 "iv": b"00000000000000000000000000000000",
263 "plaintext": b"9798c4640bad75c7c3227db910174e72",
264 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
265 },
266 {
267 "key": b"00000000000000000000000000000000",
268 "iv": b"00000000000000000000000000000000",
269 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
270 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
271 },
272 {
273 "key": b"00000000000000000000000000000000",
274 "iv": b"00000000000000000000000000000000",
275 "plaintext": b"6a118a874519e64e9963798a503f1d35",
276 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
277 },
278 {
279 "key": b"00000000000000000000000000000000",
280 "iv": b"00000000000000000000000000000000",
281 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
282 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
283 },
284 {
285 "key": b"00000000000000000000000000000000",
286 "iv": b"00000000000000000000000000000000",
287 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
288 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
289 },
290 {
291 "key": b"00000000000000000000000000000000",
292 "iv": b"00000000000000000000000000000000",
293 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
294 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
295 },
Alex Gaynord3ce7032013-11-11 14:46:20 -0800296 {
297 "key": b"00000000000000000000000000000000",
298 "iv": b"00000000000000000000000000000000",
299 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
300 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
301 },
302 {
303 "key": b"00000000000000000000000000000000",
304 "iv": b"00000000000000000000000000000000",
305 "plaintext": b"9798c4640bad75c7c3227db910174e72",
306 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
307 },
308 {
309 "key": b"00000000000000000000000000000000",
310 "iv": b"00000000000000000000000000000000",
311 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
312 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
313 },
314 {
315 "key": b"00000000000000000000000000000000",
316 "iv": b"00000000000000000000000000000000",
317 "plaintext": b"6a118a874519e64e9963798a503f1d35",
318 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
319 },
320 {
321 "key": b"00000000000000000000000000000000",
322 "iv": b"00000000000000000000000000000000",
323 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
324 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
325 },
326 {
327 "key": b"00000000000000000000000000000000",
328 "iv": b"00000000000000000000000000000000",
329 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
330 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
331 },
332 {
333 "key": b"00000000000000000000000000000000",
334 "iv": b"00000000000000000000000000000000",
335 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
336 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
337 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400338 ]
Paul Kehrer1951bf62013-09-15 12:05:43 -0500339
340
341def test_load_cryptrec_vectors():
342 vector_data = textwrap.dedent("""
343 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
344 # Download is t_camelia.txt
345
346 # Camellia with 128-bit key
347
348 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
349
350 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
351 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
352
353 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
354 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
355
356 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
357
358 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
359 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
360 """).splitlines()
361
362 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700363 {
364 "key": b"00000000000000000000000000000000",
365 "plaintext": b"80000000000000000000000000000000",
366 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
367 },
368 {
369 "key": b"00000000000000000000000000000000",
370 "plaintext": b"40000000000000000000000000000000",
371 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
372 },
373 {
374 "key": b"10000000000000000000000000000000",
375 "plaintext": b"80000000000000000000000000000000",
376 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
377 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500378 ]
379
380
Donald Stufft3359d7e2013-10-19 19:33:06 -0400381def test_load_cryptrec_vectors_invalid():
382 vector_data = textwrap.dedent("""
383 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
384 # Download is t_camelia.txt
385
386 # Camellia with 128-bit key
387
388 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
389 """).splitlines()
390
391 with pytest.raises(ValueError):
392 load_cryptrec_vectors(vector_data)
393
394
Paul Kehrer1951bf62013-09-15 12:05:43 -0500395def test_load_cryptrec_vectors_from_file_encrypt():
396 test_set = load_cryptrec_vectors_from_file(
Paul Kehrer2b758672013-10-30 09:01:38 -0500397 os.path.join("ciphers", "Camellia", "camellia-128-ecb.txt"),
Paul Kehrer1951bf62013-09-15 12:05:43 -0500398 )
399 assert test_set[0] == (
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700400 {
401 "key": b"00000000000000000000000000000000",
402 "plaintext": b"80000000000000000000000000000000",
403 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
404 }
Paul Kehrer1951bf62013-09-15 12:05:43 -0500405 )
406 assert len(test_set) == 1280
407
408
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500409def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500410 vector_data = textwrap.dedent(
411 """
412 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
413 # For all CFB128 encrypts and decrypts, the transformed sequence is
414 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
415 # CFB128-CAMELLIA128.Encrypt
416 """
417 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
418 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
419 "14F7646187817EB586599146B82BD719:1\n"
420 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
421 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
422 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
423 "# CFB128-CAMELLIA128.Decrypt\n"
424 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
425 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
426 "14F7646187817EB586599146B82BD719:0\n"
427 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
428 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
429 "A53D28BB82DF741103EA4F921A44880B:0"
430 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500431
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500432 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700433 {
434 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
435 "iv": b"000102030405060708090A0B0C0D0E0F",
436 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
437 "ciphertext": b"14F7646187817EB586599146B82BD719",
438 },
439 {
440 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
441 "iv": b"14F7646187817EB586599146B82BD719",
442 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
443 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
444 },
445 {
446 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
447 "iv": b"000102030405060708090A0B0C0D0E0F",
448 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
449 "ciphertext": b"14F7646187817EB586599146B82BD719",
450 },
451 {
452 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
453 "iv": b"14F7646187817EB586599146B82BD719",
454 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
455 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
456 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500457 ]
458
459
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500460def test_load_openssl_vectors_from_file():
Paul Kehrer2b758672013-10-30 09:01:38 -0500461 test_list = load_openssl_vectors_from_file(
462 os.path.join("ciphers", "Camellia", "camellia-ofb.txt")
463 )
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500464 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500465 assert test_list[:4] == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700466 {
467 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
468 "iv": b"000102030405060708090A0B0C0D0E0F",
469 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
470 "ciphertext": b"14F7646187817EB586599146B82BD719",
471 },
472 {
473 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
474 "iv": b"50FE67CC996D32B6DA0937E99BAFEC60",
475 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
476 "ciphertext": b"25623DB569CA51E01482649977E28D84",
477 },
478 {
479 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
480 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
481 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
482 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
483 },
484 {
485 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
486 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
487 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
488 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
489 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500490 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500491
492
493def test_load_hash_vectors():
494 vector_data = textwrap.dedent("""
495
496 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500497 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500498
499 Len = 0
500 Msg = 00
501 MD = d41d8cd98f00b204e9800998ecf8427e
502
503 Len = 8
504 Msg = 61
505 MD = 0cc175b9c0f1b6a831c399e269772661
506
507 Len = 24
508 Msg = 616263
509 MD = 900150983cd24fb0d6963f7d28e17f72
510
511 Len = 112
512 Msg = 6d65737361676520646967657374
513 MD = f96b697d7cb7938d525a2f31aaf161d0
514 """).splitlines()
515 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500516 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
517 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
518 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
519 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500520 ]
521
522
Paul Kehrer0317b042013-10-28 17:34:27 -0500523def test_load_hmac_vectors():
524 vector_data = textwrap.dedent("""
525Len = 224
526# "Jefe"
527Key = 4a656665
528# "what do ya want for nothing?"
529Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
530MD = 750c783e6ab0b503eaa86e310a5db738
531 """).splitlines()
532 assert load_hash_vectors(vector_data) == [
533 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
534 "750c783e6ab0b503eaa86e310a5db738",
535 b"4a656665"),
536 ]
537
538
Paul Kehrer69e06522013-10-18 17:28:39 -0500539def test_load_hash_vectors_bad_data():
540 vector_data = textwrap.dedent("""
541 # http://tools.ietf.org/html/rfc1321
542
543 Len = 0
544 Msg = 00
545 UNKNOWN=Hello World
546 """).splitlines()
547 with pytest.raises(ValueError):
548 load_hash_vectors(vector_data)
549
550
551def test_load_hash_vectors_from_file():
Paul Kehrer2b758672013-10-30 09:01:38 -0500552 test_list = load_hash_vectors_from_file(
553 os.path.join("hashes", "MD5", "rfc-1321.txt")
554 )
Paul Kehrer69e06522013-10-18 17:28:39 -0500555 assert len(test_list) == 7
556 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500557 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
558 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
559 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
560 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500561 ]