blob: ea8b48fb2f30706872da540d4ffa8432658a650c [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 (
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060020 load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors,
21 load_openssl_vectors, load_hash_vectors,
Alex Gaynorafdddca2013-10-21 21:00:20 -070022)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040023
24
Alex Gaynorcf5fb332013-11-11 15:39:52 -080025def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040026 vector_data = textwrap.dedent("""
27 # CAVS 11.1
28 # Config info for aes_values
29 # AESVS GFSbox test data for CBC
30 # State : Encrypt and Decrypt
31 # Key Length : 128
32 # Generated on Fri Apr 22 15:11:33 2011
33
34 [ENCRYPT]
35
36 COUNT = 0
37 KEY = 00000000000000000000000000000000
38 IV = 00000000000000000000000000000000
39 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
40 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
41
42 COUNT = 1
43 KEY = 00000000000000000000000000000000
44 IV = 00000000000000000000000000000000
45 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
46 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
47
48 [DECRYPT]
49
50 COUNT = 0
51 KEY = 00000000000000000000000000000000
52 IV = 00000000000000000000000000000000
53 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
54 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
55
56 COUNT = 1
57 KEY = 00000000000000000000000000000000
58 IV = 00000000000000000000000000000000
59 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
60 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
61 """).splitlines()
62
Alex Gaynord3ce7032013-11-11 14:46:20 -080063 assert load_nist_vectors(vector_data) == [
64 {
65 "key": b"00000000000000000000000000000000",
66 "iv": b"00000000000000000000000000000000",
67 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
68 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
69 },
70 {
71 "key": b"00000000000000000000000000000000",
72 "iv": b"00000000000000000000000000000000",
73 "plaintext": b"9798c4640bad75c7c3227db910174e72",
74 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
75 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -070076 {
77 "key": b"00000000000000000000000000000000",
78 "iv": b"00000000000000000000000000000000",
79 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
80 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
81 },
82 {
83 "key": b"00000000000000000000000000000000",
84 "iv": b"00000000000000000000000000000000",
85 "plaintext": b"9798c4640bad75c7c3227db910174e72",
86 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
87 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -040088 ]
89
90
Alex Gaynorcf5fb332013-11-11 15:39:52 -080091def test_load_nist_vectors_from_file():
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060092 assert load_vectors_from_file(
93 os.path.join("ciphers", "AES", "CBC", "CBCGFSbox128.rsp"),
94 load_nist_vectors
Donald Stufft9e1a48b2013-08-09 00:32:30 -040095 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -070096 {
97 "key": b"00000000000000000000000000000000",
98 "iv": b"00000000000000000000000000000000",
99 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
100 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
101 },
102 {
103 "key": b"00000000000000000000000000000000",
104 "iv": b"00000000000000000000000000000000",
105 "plaintext": b"9798c4640bad75c7c3227db910174e72",
106 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
107 },
108 {
109 "key": b"00000000000000000000000000000000",
110 "iv": b"00000000000000000000000000000000",
111 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
112 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
113 },
114 {
115 "key": b"00000000000000000000000000000000",
116 "iv": b"00000000000000000000000000000000",
117 "plaintext": b"6a118a874519e64e9963798a503f1d35",
118 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
119 },
120 {
121 "key": b"00000000000000000000000000000000",
122 "iv": b"00000000000000000000000000000000",
123 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
124 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
125 },
126 {
127 "key": b"00000000000000000000000000000000",
128 "iv": b"00000000000000000000000000000000",
129 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
130 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
131 },
132 {
133 "key": b"00000000000000000000000000000000",
134 "iv": b"00000000000000000000000000000000",
135 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
136 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
137 },
Alex Gaynord3ce7032013-11-11 14:46:20 -0800138 {
139 "key": b"00000000000000000000000000000000",
140 "iv": b"00000000000000000000000000000000",
141 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
142 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
143 },
144 {
145 "key": b"00000000000000000000000000000000",
146 "iv": b"00000000000000000000000000000000",
147 "plaintext": b"9798c4640bad75c7c3227db910174e72",
148 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
149 },
150 {
151 "key": b"00000000000000000000000000000000",
152 "iv": b"00000000000000000000000000000000",
153 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
154 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
155 },
156 {
157 "key": b"00000000000000000000000000000000",
158 "iv": b"00000000000000000000000000000000",
159 "plaintext": b"6a118a874519e64e9963798a503f1d35",
160 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
161 },
162 {
163 "key": b"00000000000000000000000000000000",
164 "iv": b"00000000000000000000000000000000",
165 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
166 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
167 },
168 {
169 "key": b"00000000000000000000000000000000",
170 "iv": b"00000000000000000000000000000000",
171 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
172 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
173 },
174 {
175 "key": b"00000000000000000000000000000000",
176 "iv": b"00000000000000000000000000000000",
177 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
178 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
179 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400180 ]
181
182
Paul Kehrer1951bf62013-09-15 12:05:43 -0500183def test_load_cryptrec_vectors():
184 vector_data = textwrap.dedent("""
185 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
186 # Download is t_camelia.txt
187
188 # Camellia with 128-bit key
189
190 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
191
192 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
193 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
194
195 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
196 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
197
198 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
199
200 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
201 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
202 """).splitlines()
203
204 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700205 {
206 "key": b"00000000000000000000000000000000",
207 "plaintext": b"80000000000000000000000000000000",
208 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
209 },
210 {
211 "key": b"00000000000000000000000000000000",
212 "plaintext": b"40000000000000000000000000000000",
213 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
214 },
215 {
216 "key": b"10000000000000000000000000000000",
217 "plaintext": b"80000000000000000000000000000000",
218 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
219 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500220 ]
221
222
Donald Stufft3359d7e2013-10-19 19:33:06 -0400223def test_load_cryptrec_vectors_invalid():
224 vector_data = textwrap.dedent("""
225 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
226 # Download is t_camelia.txt
227
228 # Camellia with 128-bit key
229
230 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
231 """).splitlines()
232
233 with pytest.raises(ValueError):
234 load_cryptrec_vectors(vector_data)
235
236
Paul Kehrer1951bf62013-09-15 12:05:43 -0500237def test_load_cryptrec_vectors_from_file_encrypt():
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -0600238 test_set = load_vectors_from_file(
Paul Kehrer2b758672013-10-30 09:01:38 -0500239 os.path.join("ciphers", "Camellia", "camellia-128-ecb.txt"),
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -0600240 load_cryptrec_vectors
Paul Kehrer1951bf62013-09-15 12:05:43 -0500241 )
242 assert test_set[0] == (
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700243 {
244 "key": b"00000000000000000000000000000000",
245 "plaintext": b"80000000000000000000000000000000",
246 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
247 }
Paul Kehrer1951bf62013-09-15 12:05:43 -0500248 )
249 assert len(test_set) == 1280
250
251
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500252def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500253 vector_data = textwrap.dedent(
254 """
255 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
256 # For all CFB128 encrypts and decrypts, the transformed sequence is
257 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
258 # CFB128-CAMELLIA128.Encrypt
259 """
260 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
261 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
262 "14F7646187817EB586599146B82BD719:1\n"
263 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
264 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
265 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
266 "# CFB128-CAMELLIA128.Decrypt\n"
267 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
268 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
269 "14F7646187817EB586599146B82BD719:0\n"
270 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
271 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
272 "A53D28BB82DF741103EA4F921A44880B:0"
273 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500274
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500275 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700276 {
277 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
278 "iv": b"000102030405060708090A0B0C0D0E0F",
279 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
280 "ciphertext": b"14F7646187817EB586599146B82BD719",
281 },
282 {
283 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
284 "iv": b"14F7646187817EB586599146B82BD719",
285 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
286 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
287 },
288 {
289 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
290 "iv": b"000102030405060708090A0B0C0D0E0F",
291 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
292 "ciphertext": b"14F7646187817EB586599146B82BD719",
293 },
294 {
295 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
296 "iv": b"14F7646187817EB586599146B82BD719",
297 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
298 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
299 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500300 ]
301
302
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500303def test_load_openssl_vectors_from_file():
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -0600304 test_list = load_vectors_from_file(
305 os.path.join("ciphers", "Camellia", "camellia-ofb.txt"),
306 load_openssl_vectors
Paul Kehrer2b758672013-10-30 09:01:38 -0500307 )
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500308 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500309 assert test_list[:4] == [
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"50FE67CC996D32B6DA0937E99BAFEC60",
319 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
320 "ciphertext": b"25623DB569CA51E01482649977E28D84",
321 },
322 {
323 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
324 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
325 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
326 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
327 },
328 {
329 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
330 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
331 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
332 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
333 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500334 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500335
336
337def test_load_hash_vectors():
338 vector_data = textwrap.dedent("""
339
340 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500341 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500342
343 Len = 0
344 Msg = 00
345 MD = d41d8cd98f00b204e9800998ecf8427e
346
347 Len = 8
348 Msg = 61
349 MD = 0cc175b9c0f1b6a831c399e269772661
350
351 Len = 24
352 Msg = 616263
353 MD = 900150983cd24fb0d6963f7d28e17f72
354
355 Len = 112
356 Msg = 6d65737361676520646967657374
357 MD = f96b697d7cb7938d525a2f31aaf161d0
358 """).splitlines()
359 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500360 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
361 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
362 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
363 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500364 ]
365
366
Paul Kehrer0317b042013-10-28 17:34:27 -0500367def test_load_hmac_vectors():
368 vector_data = textwrap.dedent("""
369Len = 224
370# "Jefe"
371Key = 4a656665
372# "what do ya want for nothing?"
373Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
374MD = 750c783e6ab0b503eaa86e310a5db738
375 """).splitlines()
376 assert load_hash_vectors(vector_data) == [
377 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
378 "750c783e6ab0b503eaa86e310a5db738",
379 b"4a656665"),
380 ]
381
382
Paul Kehrer69e06522013-10-18 17:28:39 -0500383def test_load_hash_vectors_bad_data():
384 vector_data = textwrap.dedent("""
385 # http://tools.ietf.org/html/rfc1321
386
387 Len = 0
388 Msg = 00
389 UNKNOWN=Hello World
390 """).splitlines()
391 with pytest.raises(ValueError):
392 load_hash_vectors(vector_data)
393
394
395def test_load_hash_vectors_from_file():
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -0600396 test_list = load_vectors_from_file(
397 os.path.join("hashes", "MD5", "rfc-1321.txt"),
398 load_hash_vectors
Paul Kehrer2b758672013-10-30 09:01:38 -0500399 )
Paul Kehrer69e06522013-10-18 17:28:39 -0500400 assert len(test_list) == 7
401 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500402 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
403 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
404 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
405 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500406 ]