blob: 3fe9e57053a517b8f256f8a0cfe65e197ebbf2f7 [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
Donald Stufft3359d7e2013-10-19 19:33:06 -0400272def test_load_cryptrec_vectors_invalid():
273 vector_data = textwrap.dedent("""
274 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
275 # Download is t_camelia.txt
276
277 # Camellia with 128-bit key
278
279 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
280 """).splitlines()
281
282 with pytest.raises(ValueError):
283 load_cryptrec_vectors(vector_data)
284
285
Paul Kehrer1951bf62013-09-15 12:05:43 -0500286def test_load_cryptrec_vectors_from_file_encrypt():
287 test_set = load_cryptrec_vectors_from_file(
288 "Camellia/NTT/camellia-128-ecb.txt"
289 )
290 assert test_set[0] == (
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700291 {
292 "key": b"00000000000000000000000000000000",
293 "plaintext": b"80000000000000000000000000000000",
294 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
295 }
Paul Kehrer1951bf62013-09-15 12:05:43 -0500296 )
297 assert len(test_set) == 1280
298
299
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500300def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500301 vector_data = textwrap.dedent(
302 """
303 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
304 # For all CFB128 encrypts and decrypts, the transformed sequence is
305 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
306 # CFB128-CAMELLIA128.Encrypt
307 """
308 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
309 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
310 "14F7646187817EB586599146B82BD719:1\n"
311 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
312 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
313 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
314 "# CFB128-CAMELLIA128.Decrypt\n"
315 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
316 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
317 "14F7646187817EB586599146B82BD719:0\n"
318 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
319 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
320 "A53D28BB82DF741103EA4F921A44880B:0"
321 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500322
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500323 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700324 {
325 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
326 "iv": b"000102030405060708090A0B0C0D0E0F",
327 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
328 "ciphertext": b"14F7646187817EB586599146B82BD719",
329 },
330 {
331 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
332 "iv": b"14F7646187817EB586599146B82BD719",
333 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
334 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
335 },
336 {
337 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
338 "iv": b"000102030405060708090A0B0C0D0E0F",
339 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
340 "ciphertext": b"14F7646187817EB586599146B82BD719",
341 },
342 {
343 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
344 "iv": b"14F7646187817EB586599146B82BD719",
345 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
346 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
347 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500348 ]
349
350
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500351def test_load_openssl_vectors_from_file():
352 test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt")
353 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500354 assert test_list[:4] == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700355 {
356 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
357 "iv": b"000102030405060708090A0B0C0D0E0F",
358 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
359 "ciphertext": b"14F7646187817EB586599146B82BD719",
360 },
361 {
362 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
363 "iv": b"50FE67CC996D32B6DA0937E99BAFEC60",
364 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
365 "ciphertext": b"25623DB569CA51E01482649977E28D84",
366 },
367 {
368 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
369 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
370 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
371 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
372 },
373 {
374 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
375 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
376 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
377 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
378 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500379 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500380
381
382def test_load_hash_vectors():
383 vector_data = textwrap.dedent("""
384
385 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500386 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500387
388 Len = 0
389 Msg = 00
390 MD = d41d8cd98f00b204e9800998ecf8427e
391
392 Len = 8
393 Msg = 61
394 MD = 0cc175b9c0f1b6a831c399e269772661
395
396 Len = 24
397 Msg = 616263
398 MD = 900150983cd24fb0d6963f7d28e17f72
399
400 Len = 112
401 Msg = 6d65737361676520646967657374
402 MD = f96b697d7cb7938d525a2f31aaf161d0
403 """).splitlines()
404 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500405 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
406 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
407 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
408 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500409 ]
410
411
412def test_load_hash_vectors_bad_data():
413 vector_data = textwrap.dedent("""
414 # http://tools.ietf.org/html/rfc1321
415
416 Len = 0
417 Msg = 00
418 UNKNOWN=Hello World
419 """).splitlines()
420 with pytest.raises(ValueError):
421 load_hash_vectors(vector_data)
422
423
424def test_load_hash_vectors_from_file():
425 test_list = load_hash_vectors_from_file("RFC/MD5/rfc-1321.txt")
426 assert len(test_list) == 7
427 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500428 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
429 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
430 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
431 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500432 ]