blob: a9bb6a8725cd0596e9ca96b52e98fa49854cdea0 [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
Paul Kehrer1951bf62013-09-15 12:05:43 -050018from .utils import (load_nist_vectors, load_nist_vectors_from_file,
19 load_cryptrec_vectors, load_cryptrec_vectors_from_file,
Paul Kehrer69e06522013-10-18 17:28:39 -050020 load_openssl_vectors, load_openssl_vectors_from_file, load_hash_vectors,
21 load_hash_vectors_from_file)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040022
23
24def test_load_nist_vectors_encrypt():
25 vector_data = textwrap.dedent("""
26 # CAVS 11.1
27 # Config info for aes_values
28 # AESVS GFSbox test data for CBC
29 # State : Encrypt and Decrypt
30 # Key Length : 128
31 # Generated on Fri Apr 22 15:11:33 2011
32
33 [ENCRYPT]
34
35 COUNT = 0
36 KEY = 00000000000000000000000000000000
37 IV = 00000000000000000000000000000000
38 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
39 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
40
41 COUNT = 1
42 KEY = 00000000000000000000000000000000
43 IV = 00000000000000000000000000000000
44 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
45 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
46
47 [DECRYPT]
48
49 COUNT = 0
50 KEY = 00000000000000000000000000000000
51 IV = 00000000000000000000000000000000
52 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
53 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
54
55 COUNT = 1
56 KEY = 00000000000000000000000000000000
57 IV = 00000000000000000000000000000000
58 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
59 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
60 """).splitlines()
61
Alex Gaynor1fe70b12013-10-16 11:59:17 -070062 assert load_nist_vectors(vector_data, "ENCRYPT") == [
63 {
64 "key": b"00000000000000000000000000000000",
65 "iv": b"00000000000000000000000000000000",
66 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
67 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
68 },
69 {
70 "key": b"00000000000000000000000000000000",
71 "iv": b"00000000000000000000000000000000",
72 "plaintext": b"9798c4640bad75c7c3227db910174e72",
73 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
74 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -040075 ]
76
77
78def test_load_nist_vectors_decrypt():
79 vector_data = textwrap.dedent("""
80 # CAVS 11.1
81 # Config info for aes_values
82 # AESVS GFSbox test data for CBC
83 # State : Encrypt and Decrypt
84 # Key Length : 128
85 # Generated on Fri Apr 22 15:11:33 2011
86
87 [ENCRYPT]
88
89 COUNT = 0
90 KEY = 00000000000000000000000000000000
91 IV = 00000000000000000000000000000000
92 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
93 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
94
95 COUNT = 1
96 KEY = 00000000000000000000000000000000
97 IV = 00000000000000000000000000000000
98 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
99 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
100
101 [DECRYPT]
102
103 COUNT = 0
104 KEY = 00000000000000000000000000000000
105 IV = 00000000000000000000000000000000
106 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
107 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
108
109 COUNT = 1
110 KEY = 00000000000000000000000000000000
111 IV = 00000000000000000000000000000000
112 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
113 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
114 """).splitlines()
115
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700116 assert load_nist_vectors(vector_data, "DECRYPT") == [
117 {
118 "key": b"00000000000000000000000000000000",
119 "iv": b"00000000000000000000000000000000",
120 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
121 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
122 },
123 {
124 "key": b"00000000000000000000000000000000",
125 "iv": b"00000000000000000000000000000000",
126 "plaintext": b"9798c4640bad75c7c3227db910174e72",
127 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
128 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400129 ]
130
131
132def test_load_nist_vectors_from_file_encrypt():
133 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400134 "AES/KAT/CBCGFSbox128.rsp",
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700135 "ENCRYPT"
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400136 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700137 {
138 "key": b"00000000000000000000000000000000",
139 "iv": b"00000000000000000000000000000000",
140 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
141 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
142 },
143 {
144 "key": b"00000000000000000000000000000000",
145 "iv": b"00000000000000000000000000000000",
146 "plaintext": b"9798c4640bad75c7c3227db910174e72",
147 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
148 },
149 {
150 "key": b"00000000000000000000000000000000",
151 "iv": b"00000000000000000000000000000000",
152 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
153 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
154 },
155 {
156 "key": b"00000000000000000000000000000000",
157 "iv": b"00000000000000000000000000000000",
158 "plaintext": b"6a118a874519e64e9963798a503f1d35",
159 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
160 },
161 {
162 "key": b"00000000000000000000000000000000",
163 "iv": b"00000000000000000000000000000000",
164 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
165 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
166 },
167 {
168 "key": b"00000000000000000000000000000000",
169 "iv": b"00000000000000000000000000000000",
170 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
171 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
172 },
173 {
174 "key": b"00000000000000000000000000000000",
175 "iv": b"00000000000000000000000000000000",
176 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
177 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
178 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400179 ]
180
181
Paul Kehrer1951bf62013-09-15 12:05:43 -0500182def test_load_nist_vectors_from_file_decrypt():
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400183 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400184 "AES/KAT/CBCGFSbox128.rsp",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400185 "DECRYPT",
Alex Gaynor745c95c2013-10-16 14:41:35 -0700186 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700187 {
188 "key": b"00000000000000000000000000000000",
189 "iv": b"00000000000000000000000000000000",
190 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
191 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
192 },
193 {
194 "key": b"00000000000000000000000000000000",
195 "iv": b"00000000000000000000000000000000",
196 "plaintext": b"9798c4640bad75c7c3227db910174e72",
197 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
198 },
199 {
200 "key": b"00000000000000000000000000000000",
201 "iv": b"00000000000000000000000000000000",
202 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
203 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
204 },
205 {
206 "key": b"00000000000000000000000000000000",
207 "iv": b"00000000000000000000000000000000",
208 "plaintext": b"6a118a874519e64e9963798a503f1d35",
209 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
210 },
211 {
212 "key": b"00000000000000000000000000000000",
213 "iv": b"00000000000000000000000000000000",
214 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
215 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
216 },
217 {
218 "key": b"00000000000000000000000000000000",
219 "iv": b"00000000000000000000000000000000",
220 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
221 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
222 },
223 {
224 "key": b"00000000000000000000000000000000",
225 "iv": b"00000000000000000000000000000000",
226 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
227 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
228 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400229 ]
Paul Kehrer1951bf62013-09-15 12:05:43 -0500230
231
232def test_load_cryptrec_vectors():
233 vector_data = textwrap.dedent("""
234 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
235 # Download is t_camelia.txt
236
237 # Camellia with 128-bit key
238
239 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
240
241 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
242 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
243
244 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
245 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
246
247 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
248
249 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
250 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
251 """).splitlines()
252
253 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700254 {
255 "key": b"00000000000000000000000000000000",
256 "plaintext": b"80000000000000000000000000000000",
257 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
258 },
259 {
260 "key": b"00000000000000000000000000000000",
261 "plaintext": b"40000000000000000000000000000000",
262 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
263 },
264 {
265 "key": b"10000000000000000000000000000000",
266 "plaintext": b"80000000000000000000000000000000",
267 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
268 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500269 ]
270
271
272def test_load_cryptrec_vectors_from_file_encrypt():
273 test_set = load_cryptrec_vectors_from_file(
274 "Camellia/NTT/camellia-128-ecb.txt"
275 )
276 assert test_set[0] == (
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700277 {
278 "key": b"00000000000000000000000000000000",
279 "plaintext": b"80000000000000000000000000000000",
280 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
281 }
Paul Kehrer1951bf62013-09-15 12:05:43 -0500282 )
283 assert len(test_set) == 1280
284
285
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500286def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500287 vector_data = textwrap.dedent(
288 """
289 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
290 # For all CFB128 encrypts and decrypts, the transformed sequence is
291 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
292 # CFB128-CAMELLIA128.Encrypt
293 """
294 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
295 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
296 "14F7646187817EB586599146B82BD719:1\n"
297 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
298 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
299 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
300 "# CFB128-CAMELLIA128.Decrypt\n"
301 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
302 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
303 "14F7646187817EB586599146B82BD719:0\n"
304 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
305 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
306 "A53D28BB82DF741103EA4F921A44880B:0"
307 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500308
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500309 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700310 {
311 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
312 "iv": b"000102030405060708090A0B0C0D0E0F",
313 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
314 "ciphertext": b"14F7646187817EB586599146B82BD719",
315 },
316 {
317 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
318 "iv": b"14F7646187817EB586599146B82BD719",
319 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
320 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
321 },
322 {
323 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
324 "iv": b"000102030405060708090A0B0C0D0E0F",
325 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
326 "ciphertext": b"14F7646187817EB586599146B82BD719",
327 },
328 {
329 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
330 "iv": b"14F7646187817EB586599146B82BD719",
331 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
332 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
333 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500334 ]
335
336
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500337def test_load_openssl_vectors_from_file():
338 test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt")
339 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500340 assert test_list[:4] == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700341 {
342 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
343 "iv": b"000102030405060708090A0B0C0D0E0F",
344 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
345 "ciphertext": b"14F7646187817EB586599146B82BD719",
346 },
347 {
348 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
349 "iv": b"50FE67CC996D32B6DA0937E99BAFEC60",
350 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
351 "ciphertext": b"25623DB569CA51E01482649977E28D84",
352 },
353 {
354 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
355 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
356 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
357 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
358 },
359 {
360 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
361 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
362 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
363 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
364 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500365 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500366
367
368def test_load_hash_vectors():
369 vector_data = textwrap.dedent("""
370
371 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500372 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500373
374 Len = 0
375 Msg = 00
376 MD = d41d8cd98f00b204e9800998ecf8427e
377
378 Len = 8
379 Msg = 61
380 MD = 0cc175b9c0f1b6a831c399e269772661
381
382 Len = 24
383 Msg = 616263
384 MD = 900150983cd24fb0d6963f7d28e17f72
385
386 Len = 112
387 Msg = 6d65737361676520646967657374
388 MD = f96b697d7cb7938d525a2f31aaf161d0
389 """).splitlines()
390 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500391 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
392 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
393 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
394 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500395 ]
396
397
398def test_load_hash_vectors_bad_data():
399 vector_data = textwrap.dedent("""
400 # http://tools.ietf.org/html/rfc1321
401
402 Len = 0
403 Msg = 00
404 UNKNOWN=Hello World
405 """).splitlines()
406 with pytest.raises(ValueError):
407 load_hash_vectors(vector_data)
408
409
410def test_load_hash_vectors_from_file():
411 test_list = load_hash_vectors_from_file("RFC/MD5/rfc-1321.txt")
412 assert len(test_list) == 7
413 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500414 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
415 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
416 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
417 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500418 ]