blob: 5c58fd76e1bc82ac8067ee23539b9b59066dbac9 [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
Alex Gaynorab53bc52013-11-12 09:37:59 -080014import 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
Paul Kehrer1951bf62013-09-15 12:05:43 -050091def test_load_cryptrec_vectors():
92 vector_data = textwrap.dedent("""
93 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
94 # Download is t_camelia.txt
95
96 # Camellia with 128-bit key
97
98 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
99
100 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
101 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
102
103 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
104 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
105
106 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
107
108 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
109 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
110 """).splitlines()
111
112 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700113 {
114 "key": b"00000000000000000000000000000000",
115 "plaintext": b"80000000000000000000000000000000",
116 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
117 },
118 {
119 "key": b"00000000000000000000000000000000",
120 "plaintext": b"40000000000000000000000000000000",
121 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
122 },
123 {
124 "key": b"10000000000000000000000000000000",
125 "plaintext": b"80000000000000000000000000000000",
126 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
127 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500128 ]
129
130
Donald Stufft3359d7e2013-10-19 19:33:06 -0400131def test_load_cryptrec_vectors_invalid():
132 vector_data = textwrap.dedent("""
133 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
134 # Download is t_camelia.txt
135
136 # Camellia with 128-bit key
137
138 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
139 """).splitlines()
140
141 with pytest.raises(ValueError):
142 load_cryptrec_vectors(vector_data)
143
144
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500145def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500146 vector_data = textwrap.dedent(
147 """
148 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
149 # For all CFB128 encrypts and decrypts, the transformed sequence is
150 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
151 # CFB128-CAMELLIA128.Encrypt
152 """
153 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
154 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
155 "14F7646187817EB586599146B82BD719:1\n"
156 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
157 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
158 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
159 "# CFB128-CAMELLIA128.Decrypt\n"
160 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
161 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
162 "14F7646187817EB586599146B82BD719:0\n"
163 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
164 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
165 "A53D28BB82DF741103EA4F921A44880B:0"
166 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500167
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500168 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700169 {
170 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
171 "iv": b"000102030405060708090A0B0C0D0E0F",
172 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
173 "ciphertext": b"14F7646187817EB586599146B82BD719",
174 },
175 {
176 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
177 "iv": b"14F7646187817EB586599146B82BD719",
178 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
179 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
180 },
181 {
182 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
183 "iv": b"000102030405060708090A0B0C0D0E0F",
184 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
185 "ciphertext": b"14F7646187817EB586599146B82BD719",
186 },
187 {
188 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
189 "iv": b"14F7646187817EB586599146B82BD719",
190 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
191 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
192 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500193 ]
194
195
Paul Kehrer69e06522013-10-18 17:28:39 -0500196def test_load_hash_vectors():
197 vector_data = textwrap.dedent("""
198
199 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500200 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500201
202 Len = 0
203 Msg = 00
204 MD = d41d8cd98f00b204e9800998ecf8427e
205
206 Len = 8
207 Msg = 61
208 MD = 0cc175b9c0f1b6a831c399e269772661
209
210 Len = 24
211 Msg = 616263
212 MD = 900150983cd24fb0d6963f7d28e17f72
213
214 Len = 112
215 Msg = 6d65737361676520646967657374
216 MD = f96b697d7cb7938d525a2f31aaf161d0
217 """).splitlines()
218 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500219 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
220 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
221 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
222 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500223 ]
224
225
Paul Kehrer0317b042013-10-28 17:34:27 -0500226def test_load_hmac_vectors():
227 vector_data = textwrap.dedent("""
228Len = 224
229# "Jefe"
230Key = 4a656665
231# "what do ya want for nothing?"
232Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
233MD = 750c783e6ab0b503eaa86e310a5db738
234 """).splitlines()
235 assert load_hash_vectors(vector_data) == [
236 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
237 "750c783e6ab0b503eaa86e310a5db738",
238 b"4a656665"),
239 ]
240
241
Paul Kehrer69e06522013-10-18 17:28:39 -0500242def test_load_hash_vectors_bad_data():
243 vector_data = textwrap.dedent("""
244 # http://tools.ietf.org/html/rfc1321
245
246 Len = 0
247 Msg = 00
248 UNKNOWN=Hello World
249 """).splitlines()
250 with pytest.raises(ValueError):
251 load_hash_vectors(vector_data)
252
Alex Gaynor41172ab2013-11-12 10:00:42 -0800253
Alex Gaynorab53bc52013-11-12 09:37:59 -0800254def test_load_vectors_from_file():
255 vectors = load_vectors_from_file(
256 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
257 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500258 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800259 assert vectors == [
260 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800261 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
262 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800263 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800264 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800265 ),
266 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800267 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800268 ),
269 }
270 ]
Paul Kehrera43b6692013-11-12 15:35:49 -0600271
272
273def test_load_nist_gcm_vectors():
274 vector_data = textwrap.dedent("""
275 [Keylen = 128]
276 [IVlen = 96]
277 [PTlen = 0]
278 [AADlen = 0]
279 [Taglen = 128]
280
281 Count = 0
282 Key = 11754cd72aec309bf52f7687212e8957
283 IV = 3c819d9a9bed087615030b65
284 PT =
285 AAD =
286 CT =
287 Tag = 250327c674aaf477aef2675748cf6971
288
289 Count = 1
290 Key = 272f16edb81a7abbea887357a58c1917
291 IV = 794ec588176c703d3d2a7a07
292 PT =
293 AAD =
294 CT =
295 Tag = b6e6f197168f5049aeda32dafbdaeb
296
297 Count = 2
298 Key = a49a5e26a2f8cb63d05546c2a62f5343
299 IV = 907763b19b9b4ab6bd4f0281
300 CT =
301 AAD =
302 Tag = a2be08210d8c470a8df6e8fbd79ec5cf
303 FAIL
304
305 Count = 3
306 Key = 5c1155084cc0ede76b3bc22e9f7574ef
307 IV = 9549e4ba69a61cad7856efc1
308 PT = d1448fa852b84408e2dad8381f363de7
309 AAD = e98e9d9c618e46fef32660976f854ee3
310 CT = f78b60ca125218493bea1c50a2e12ef4
311 Tag = d72da7f5c6cf0bca7242c71835809449
312
313 [Keylen = 128]
314 [IVlen = 96]
315 [PTlen = 0]
316 [AADlen = 0]
317 [Taglen = 120]
318
319 Count = 0
320 Key = eac258e99c55e6ae8ef1da26640613d7
321 IV = 4e8df20faaf2c8eebe922902
322 CT =
323 AAD =
324 Tag = e39aeaebe86aa309a4d062d6274339
325 PT =
326
327 Count = 1
328 Key = 3726cf02fcc6b8639a5497652c94350d
329 IV = 55fef82cde693ce76efcc193
330 CT =
331 AAD =
332 Tag = 3d68111a81ed22d2ef5bccac4fc27f
333 FAIL
334
335 Count = 2
336 Key = f202299d5fd74f03b12d2119a6c4c038
337 IV = eec51e7958c3f20a1bb71815
338 CT =
339 AAD =
340 Tag = a81886b3fb26e51fca87b267e1e157
341 FAIL
342
343 Count = 3
344 Key = fd52925f39546b4c55ffb6b20c59898c
345 IV = f5cf3227444afd905a5f6dba
346 CT =
347 AAD =
348 Tag = 1665b0f1a0b456e1664cfd3de08ccd
349 PT =
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600350
351 [Keylen = 128]
352 [IVlen = 8]
353 [PTlen = 104]
354 [AADlen = 0]
355 [Taglen = 128]
356
357 Count = 0
358 Key = 58fab7632bcf10d2bcee58520bf37414
359 IV = 3c
360 CT = 15c4db4cbb451211179d57017f
361 AAD =
362 Tag = eae841d4355feeb3f786bc86625f1e5b
363 FAIL
Paul Kehrera43b6692013-11-12 15:35:49 -0600364 """).splitlines()
365 assert load_nist_vectors(vector_data) == [
366 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600367 'pt': b'',
368 'iv': b'3c819d9a9bed087615030b65',
369 'tag': b'250327c674aaf477aef2675748cf6971',
370 'key': b'11754cd72aec309bf52f7687212e8957',
371 'ct': b''},
372 {'aad': b'',
373 'pt': b'',
374 'iv': b'794ec588176c703d3d2a7a07',
375 'tag': b'b6e6f197168f5049aeda32dafbdaeb',
376 'key': b'272f16edb81a7abbea887357a58c1917',
377 'ct': b''},
378 {'aad': b'',
379 'iv': b'907763b19b9b4ab6bd4f0281',
380 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf',
381 'key': b'a49a5e26a2f8cb63d05546c2a62f5343',
382 'ct': b'',
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600383 'fail': True},
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600384 {'aad': b'e98e9d9c618e46fef32660976f854ee3',
385 'pt': b'd1448fa852b84408e2dad8381f363de7',
386 'iv': b'9549e4ba69a61cad7856efc1',
387 'tag': b'd72da7f5c6cf0bca7242c71835809449',
388 'key': b'5c1155084cc0ede76b3bc22e9f7574ef',
389 'ct': b'f78b60ca125218493bea1c50a2e12ef4'},
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600390 {'aad': b'',
Paul Kehrera43b6692013-11-12 15:35:49 -0600391 'pt': b'',
392 'iv': b'4e8df20faaf2c8eebe922902',
393 'tag': b'e39aeaebe86aa309a4d062d6274339',
394 'key': b'eac258e99c55e6ae8ef1da26640613d7',
395 'ct': b''},
396 {'aad': b'',
397 'iv': b'55fef82cde693ce76efcc193',
398 'tag': b'3d68111a81ed22d2ef5bccac4fc27f',
399 'key': b'3726cf02fcc6b8639a5497652c94350d',
400 'ct': b'',
401 'fail': True},
402 {'aad': b'',
403 'iv': b'eec51e7958c3f20a1bb71815',
404 'tag': b'a81886b3fb26e51fca87b267e1e157',
405 'key': b'f202299d5fd74f03b12d2119a6c4c038',
406 'ct': b'',
407 'fail': True},
408 {'aad': b'',
409 'pt': b'',
410 'iv': b'f5cf3227444afd905a5f6dba',
411 'tag': b'1665b0f1a0b456e1664cfd3de08ccd',
412 'key': b'fd52925f39546b4c55ffb6b20c59898c',
413 'ct': b''},
414 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600415 'iv': b'3c',
416 'tag': b'eae841d4355feeb3f786bc86625f1e5b',
417 'key': b'58fab7632bcf10d2bcee58520bf37414',
418 'ct': b'15c4db4cbb451211179d57017f',
Paul Kehrera43b6692013-11-12 15:35:49 -0600419 'fail': True},
Paul Kehrera43b6692013-11-12 15:35:49 -0600420 ]