blob: 435690b7e7bc385feda98ed65766bdeafd6fdb7c [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
Alex Gaynorcf5fb332013-11-11 15:39:52 -080027def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040028 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
Alex Gaynorcf5fb332013-11-11 15:39:52 -080093def test_load_nist_vectors_from_file():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040094 assert load_nist_vectors_from_file(
Alex Gaynord3ce7032013-11-11 14:46:20 -080095 os.path.join("ciphers", "AES", "CBC", "CBCGFSbox128.rsp")
Donald Stufft9e1a48b2013-08-09 00:32:30 -040096 ) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -070097 {
98 "key": b"00000000000000000000000000000000",
99 "iv": b"00000000000000000000000000000000",
100 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
101 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
102 },
103 {
104 "key": b"00000000000000000000000000000000",
105 "iv": b"00000000000000000000000000000000",
106 "plaintext": b"9798c4640bad75c7c3227db910174e72",
107 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
108 },
109 {
110 "key": b"00000000000000000000000000000000",
111 "iv": b"00000000000000000000000000000000",
112 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
113 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
114 },
115 {
116 "key": b"00000000000000000000000000000000",
117 "iv": b"00000000000000000000000000000000",
118 "plaintext": b"6a118a874519e64e9963798a503f1d35",
119 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
120 },
121 {
122 "key": b"00000000000000000000000000000000",
123 "iv": b"00000000000000000000000000000000",
124 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
125 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
126 },
127 {
128 "key": b"00000000000000000000000000000000",
129 "iv": b"00000000000000000000000000000000",
130 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
131 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
132 },
133 {
134 "key": b"00000000000000000000000000000000",
135 "iv": b"00000000000000000000000000000000",
136 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
137 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
138 },
Alex Gaynord3ce7032013-11-11 14:46:20 -0800139 {
140 "key": b"00000000000000000000000000000000",
141 "iv": b"00000000000000000000000000000000",
142 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
143 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
144 },
145 {
146 "key": b"00000000000000000000000000000000",
147 "iv": b"00000000000000000000000000000000",
148 "plaintext": b"9798c4640bad75c7c3227db910174e72",
149 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
150 },
151 {
152 "key": b"00000000000000000000000000000000",
153 "iv": b"00000000000000000000000000000000",
154 "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168",
155 "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597",
156 },
157 {
158 "key": b"00000000000000000000000000000000",
159 "iv": b"00000000000000000000000000000000",
160 "plaintext": b"6a118a874519e64e9963798a503f1d35",
161 "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209",
162 },
163 {
164 "key": b"00000000000000000000000000000000",
165 "iv": b"00000000000000000000000000000000",
166 "plaintext": b"cb9fceec81286ca3e989bd979b0cb284",
167 "ciphertext": b"92beedab1895a94faa69b632e5cc47ce",
168 },
169 {
170 "key": b"00000000000000000000000000000000",
171 "iv": b"00000000000000000000000000000000",
172 "plaintext": b"b26aeb1874e47ca8358ff22378f09144",
173 "ciphertext": b"459264f4798f6a78bacb89c15ed3d601",
174 },
175 {
176 "key": b"00000000000000000000000000000000",
177 "iv": b"00000000000000000000000000000000",
178 "plaintext": b"58c8e00b2631686d54eab84b91f0aca1",
179 "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf",
180 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400181 ]
182
183
Paul Kehrer1951bf62013-09-15 12:05:43 -0500184def test_load_cryptrec_vectors():
185 vector_data = textwrap.dedent("""
186 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
187 # Download is t_camelia.txt
188
189 # Camellia with 128-bit key
190
191 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
192
193 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
194 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
195
196 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
197 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
198
199 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
200
201 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
202 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
203 """).splitlines()
204
205 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700206 {
207 "key": b"00000000000000000000000000000000",
208 "plaintext": b"80000000000000000000000000000000",
209 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
210 },
211 {
212 "key": b"00000000000000000000000000000000",
213 "plaintext": b"40000000000000000000000000000000",
214 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
215 },
216 {
217 "key": b"10000000000000000000000000000000",
218 "plaintext": b"80000000000000000000000000000000",
219 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
220 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500221 ]
222
223
Donald Stufft3359d7e2013-10-19 19:33:06 -0400224def test_load_cryptrec_vectors_invalid():
225 vector_data = textwrap.dedent("""
226 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
227 # Download is t_camelia.txt
228
229 # Camellia with 128-bit key
230
231 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
232 """).splitlines()
233
234 with pytest.raises(ValueError):
235 load_cryptrec_vectors(vector_data)
236
237
Paul Kehrer1951bf62013-09-15 12:05:43 -0500238def test_load_cryptrec_vectors_from_file_encrypt():
239 test_set = load_cryptrec_vectors_from_file(
Paul Kehrer2b758672013-10-30 09:01:38 -0500240 os.path.join("ciphers", "Camellia", "camellia-128-ecb.txt"),
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 Kehrer2b758672013-10-30 09:01:38 -0500304 test_list = load_openssl_vectors_from_file(
305 os.path.join("ciphers", "Camellia", "camellia-ofb.txt")
306 )
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500307 assert len(test_list) == 24
Paul Kehrer1951bf62013-09-15 12:05:43 -0500308 assert test_list[:4] == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700309 {
310 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
311 "iv": b"000102030405060708090A0B0C0D0E0F",
312 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
313 "ciphertext": b"14F7646187817EB586599146B82BD719",
314 },
315 {
316 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
317 "iv": b"50FE67CC996D32B6DA0937E99BAFEC60",
318 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
319 "ciphertext": b"25623DB569CA51E01482649977E28D84",
320 },
321 {
322 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
323 "iv": b"D9A4DADA0892239F6B8B3D7680E15674",
324 "plaintext": b"30C81C46A35CE411E5FBC1191A0A52EF",
325 "ciphertext": b"C776634A60729DC657D12B9FCA801E98",
326 },
327 {
328 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
329 "iv": b"A78819583F0308E7A6BF36B1386ABF23",
330 "plaintext": b"F69F2445DF4F9B17AD2B417BE66C3710",
331 "ciphertext": b"D776379BE0E50825E681DA1A4C980E8E",
332 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500333 ]
Paul Kehrer69e06522013-10-18 17:28:39 -0500334
335
336def test_load_hash_vectors():
337 vector_data = textwrap.dedent("""
338
339 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500340 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500341
342 Len = 0
343 Msg = 00
344 MD = d41d8cd98f00b204e9800998ecf8427e
345
346 Len = 8
347 Msg = 61
348 MD = 0cc175b9c0f1b6a831c399e269772661
349
350 Len = 24
351 Msg = 616263
352 MD = 900150983cd24fb0d6963f7d28e17f72
353
354 Len = 112
355 Msg = 6d65737361676520646967657374
356 MD = f96b697d7cb7938d525a2f31aaf161d0
357 """).splitlines()
358 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500359 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
360 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
361 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
362 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500363 ]
364
365
Paul Kehrer0317b042013-10-28 17:34:27 -0500366def test_load_hmac_vectors():
367 vector_data = textwrap.dedent("""
368Len = 224
369# "Jefe"
370Key = 4a656665
371# "what do ya want for nothing?"
372Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
373MD = 750c783e6ab0b503eaa86e310a5db738
374 """).splitlines()
375 assert load_hash_vectors(vector_data) == [
376 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
377 "750c783e6ab0b503eaa86e310a5db738",
378 b"4a656665"),
379 ]
380
381
Paul Kehrer69e06522013-10-18 17:28:39 -0500382def test_load_hash_vectors_bad_data():
383 vector_data = textwrap.dedent("""
384 # http://tools.ietf.org/html/rfc1321
385
386 Len = 0
387 Msg = 00
388 UNKNOWN=Hello World
389 """).splitlines()
390 with pytest.raises(ValueError):
391 load_hash_vectors(vector_data)
392
393
394def test_load_hash_vectors_from_file():
Paul Kehrer2b758672013-10-30 09:01:38 -0500395 test_list = load_hash_vectors_from_file(
396 os.path.join("hashes", "MD5", "rfc-1321.txt")
397 )
Paul Kehrer69e06522013-10-18 17:28:39 -0500398 assert len(test_list) == 7
399 assert test_list[:4] == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500400 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
401 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
402 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
403 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500404 ]