blob: a65091ffb36d6a371477b89c292ca170394b9f1a [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
Alex Gaynor2b3f9422013-12-24 21:55:24 -080017import pretend
18
Paul Kehrer79c16e92013-10-18 17:44:36 -050019import pytest
20
Alex Gaynorafdddca2013-10-21 21:00:20 -070021from .utils import (
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060022 load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors,
Alex Gaynor2b3f9422013-12-24 21:55:24 -080023 load_openssl_vectors, load_hash_vectors, check_for_iface
Alex Gaynorafdddca2013-10-21 21:00:20 -070024)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040025
26
Alex Gaynor2b3f9422013-12-24 21:55:24 -080027class FakeInterface(object):
28 pass
29
30
31def test_check_for_iface():
32 item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True})
33 with pytest.raises(pytest.skip.Exception) as exc_info:
34 check_for_iface("fake_name", FakeInterface, item)
35 assert exc_info.value.args[0] == "True backend does not support fake_name"
36
37 item = pretend.stub(
38 keywords=["fake_name"],
39 funcargs={"backend": FakeInterface()}
40 )
41 check_for_iface("fake_name", FakeInterface, item)
42
43
Alex Gaynorcf5fb332013-11-11 15:39:52 -080044def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040045 vector_data = textwrap.dedent("""
46 # CAVS 11.1
47 # Config info for aes_values
48 # AESVS GFSbox test data for CBC
49 # State : Encrypt and Decrypt
50 # Key Length : 128
51 # Generated on Fri Apr 22 15:11:33 2011
52
53 [ENCRYPT]
54
55 COUNT = 0
56 KEY = 00000000000000000000000000000000
57 IV = 00000000000000000000000000000000
58 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
59 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
60
61 COUNT = 1
62 KEY = 00000000000000000000000000000000
63 IV = 00000000000000000000000000000000
64 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
65 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
66
67 [DECRYPT]
68
69 COUNT = 0
70 KEY = 00000000000000000000000000000000
71 IV = 00000000000000000000000000000000
72 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
73 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
74
75 COUNT = 1
76 KEY = 00000000000000000000000000000000
77 IV = 00000000000000000000000000000000
78 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
79 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
80 """).splitlines()
81
Alex Gaynord3ce7032013-11-11 14:46:20 -080082 assert load_nist_vectors(vector_data) == [
83 {
84 "key": b"00000000000000000000000000000000",
85 "iv": b"00000000000000000000000000000000",
86 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
87 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
88 },
89 {
90 "key": b"00000000000000000000000000000000",
91 "iv": b"00000000000000000000000000000000",
92 "plaintext": b"9798c4640bad75c7c3227db910174e72",
93 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
94 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -070095 {
96 "key": b"00000000000000000000000000000000",
97 "iv": b"00000000000000000000000000000000",
98 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
99 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
100 },
101 {
102 "key": b"00000000000000000000000000000000",
103 "iv": b"00000000000000000000000000000000",
104 "plaintext": b"9798c4640bad75c7c3227db910174e72",
105 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
106 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400107 ]
108
109
Paul Kehrer1951bf62013-09-15 12:05:43 -0500110def test_load_cryptrec_vectors():
111 vector_data = textwrap.dedent("""
112 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
113 # Download is t_camelia.txt
114
115 # Camellia with 128-bit key
116
117 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
118
119 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
120 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
121
122 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
123 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
124
125 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
126
127 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
128 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
129 """).splitlines()
130
131 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700132 {
133 "key": b"00000000000000000000000000000000",
134 "plaintext": b"80000000000000000000000000000000",
135 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
136 },
137 {
138 "key": b"00000000000000000000000000000000",
139 "plaintext": b"40000000000000000000000000000000",
140 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
141 },
142 {
143 "key": b"10000000000000000000000000000000",
144 "plaintext": b"80000000000000000000000000000000",
145 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
146 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500147 ]
148
149
Donald Stufft3359d7e2013-10-19 19:33:06 -0400150def test_load_cryptrec_vectors_invalid():
151 vector_data = textwrap.dedent("""
152 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
153 # Download is t_camelia.txt
154
155 # Camellia with 128-bit key
156
157 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
158 """).splitlines()
159
160 with pytest.raises(ValueError):
161 load_cryptrec_vectors(vector_data)
162
163
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500164def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500165 vector_data = textwrap.dedent(
166 """
167 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
168 # For all CFB128 encrypts and decrypts, the transformed sequence is
169 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
170 # CFB128-CAMELLIA128.Encrypt
171 """
172 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
173 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
174 "14F7646187817EB586599146B82BD719:1\n"
175 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
176 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
177 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
178 "# CFB128-CAMELLIA128.Decrypt\n"
179 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
180 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
181 "14F7646187817EB586599146B82BD719:0\n"
182 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
183 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
184 "A53D28BB82DF741103EA4F921A44880B:0"
185 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500186
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500187 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700188 {
189 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
190 "iv": b"000102030405060708090A0B0C0D0E0F",
191 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
192 "ciphertext": b"14F7646187817EB586599146B82BD719",
193 },
194 {
195 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
196 "iv": b"14F7646187817EB586599146B82BD719",
197 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
198 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
199 },
200 {
201 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
202 "iv": b"000102030405060708090A0B0C0D0E0F",
203 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
204 "ciphertext": b"14F7646187817EB586599146B82BD719",
205 },
206 {
207 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
208 "iv": b"14F7646187817EB586599146B82BD719",
209 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
210 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
211 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500212 ]
213
214
Paul Kehrer69e06522013-10-18 17:28:39 -0500215def test_load_hash_vectors():
216 vector_data = textwrap.dedent("""
217
218 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500219 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500220
221 Len = 0
222 Msg = 00
223 MD = d41d8cd98f00b204e9800998ecf8427e
224
225 Len = 8
226 Msg = 61
227 MD = 0cc175b9c0f1b6a831c399e269772661
228
229 Len = 24
230 Msg = 616263
231 MD = 900150983cd24fb0d6963f7d28e17f72
232
233 Len = 112
234 Msg = 6d65737361676520646967657374
235 MD = f96b697d7cb7938d525a2f31aaf161d0
236 """).splitlines()
237 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500238 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
239 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
240 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
241 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500242 ]
243
244
Paul Kehrer0317b042013-10-28 17:34:27 -0500245def test_load_hmac_vectors():
246 vector_data = textwrap.dedent("""
247Len = 224
248# "Jefe"
249Key = 4a656665
250# "what do ya want for nothing?"
251Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
252MD = 750c783e6ab0b503eaa86e310a5db738
253 """).splitlines()
254 assert load_hash_vectors(vector_data) == [
255 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
256 "750c783e6ab0b503eaa86e310a5db738",
257 b"4a656665"),
258 ]
259
260
Paul Kehrer69e06522013-10-18 17:28:39 -0500261def test_load_hash_vectors_bad_data():
262 vector_data = textwrap.dedent("""
263 # http://tools.ietf.org/html/rfc1321
264
265 Len = 0
266 Msg = 00
267 UNKNOWN=Hello World
268 """).splitlines()
269 with pytest.raises(ValueError):
270 load_hash_vectors(vector_data)
271
Alex Gaynor41172ab2013-11-12 10:00:42 -0800272
Alex Gaynorab53bc52013-11-12 09:37:59 -0800273def test_load_vectors_from_file():
274 vectors = load_vectors_from_file(
275 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
276 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500277 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800278 assert vectors == [
279 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800280 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
281 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800282 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800283 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800284 ),
285 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800286 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800287 ),
288 }
289 ]
Paul Kehrera43b6692013-11-12 15:35:49 -0600290
291
292def test_load_nist_gcm_vectors():
293 vector_data = textwrap.dedent("""
294 [Keylen = 128]
295 [IVlen = 96]
296 [PTlen = 0]
297 [AADlen = 0]
298 [Taglen = 128]
299
300 Count = 0
301 Key = 11754cd72aec309bf52f7687212e8957
302 IV = 3c819d9a9bed087615030b65
303 PT =
304 AAD =
305 CT =
306 Tag = 250327c674aaf477aef2675748cf6971
307
308 Count = 1
309 Key = 272f16edb81a7abbea887357a58c1917
310 IV = 794ec588176c703d3d2a7a07
311 PT =
312 AAD =
313 CT =
314 Tag = b6e6f197168f5049aeda32dafbdaeb
315
316 Count = 2
317 Key = a49a5e26a2f8cb63d05546c2a62f5343
318 IV = 907763b19b9b4ab6bd4f0281
319 CT =
320 AAD =
321 Tag = a2be08210d8c470a8df6e8fbd79ec5cf
322 FAIL
323
324 Count = 3
325 Key = 5c1155084cc0ede76b3bc22e9f7574ef
326 IV = 9549e4ba69a61cad7856efc1
327 PT = d1448fa852b84408e2dad8381f363de7
328 AAD = e98e9d9c618e46fef32660976f854ee3
329 CT = f78b60ca125218493bea1c50a2e12ef4
330 Tag = d72da7f5c6cf0bca7242c71835809449
331
332 [Keylen = 128]
333 [IVlen = 96]
334 [PTlen = 0]
335 [AADlen = 0]
336 [Taglen = 120]
337
338 Count = 0
339 Key = eac258e99c55e6ae8ef1da26640613d7
340 IV = 4e8df20faaf2c8eebe922902
341 CT =
342 AAD =
343 Tag = e39aeaebe86aa309a4d062d6274339
344 PT =
345
346 Count = 1
347 Key = 3726cf02fcc6b8639a5497652c94350d
348 IV = 55fef82cde693ce76efcc193
349 CT =
350 AAD =
351 Tag = 3d68111a81ed22d2ef5bccac4fc27f
352 FAIL
353
354 Count = 2
355 Key = f202299d5fd74f03b12d2119a6c4c038
356 IV = eec51e7958c3f20a1bb71815
357 CT =
358 AAD =
359 Tag = a81886b3fb26e51fca87b267e1e157
360 FAIL
361
362 Count = 3
363 Key = fd52925f39546b4c55ffb6b20c59898c
364 IV = f5cf3227444afd905a5f6dba
365 CT =
366 AAD =
367 Tag = 1665b0f1a0b456e1664cfd3de08ccd
368 PT =
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600369
370 [Keylen = 128]
371 [IVlen = 8]
372 [PTlen = 104]
373 [AADlen = 0]
374 [Taglen = 128]
375
376 Count = 0
377 Key = 58fab7632bcf10d2bcee58520bf37414
378 IV = 3c
379 CT = 15c4db4cbb451211179d57017f
380 AAD =
381 Tag = eae841d4355feeb3f786bc86625f1e5b
382 FAIL
Paul Kehrera43b6692013-11-12 15:35:49 -0600383 """).splitlines()
384 assert load_nist_vectors(vector_data) == [
385 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600386 'pt': b'',
387 'iv': b'3c819d9a9bed087615030b65',
388 'tag': b'250327c674aaf477aef2675748cf6971',
389 'key': b'11754cd72aec309bf52f7687212e8957',
390 'ct': b''},
391 {'aad': b'',
392 'pt': b'',
393 'iv': b'794ec588176c703d3d2a7a07',
394 'tag': b'b6e6f197168f5049aeda32dafbdaeb',
395 'key': b'272f16edb81a7abbea887357a58c1917',
396 'ct': b''},
397 {'aad': b'',
398 'iv': b'907763b19b9b4ab6bd4f0281',
399 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf',
400 'key': b'a49a5e26a2f8cb63d05546c2a62f5343',
401 'ct': b'',
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600402 'fail': True},
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600403 {'aad': b'e98e9d9c618e46fef32660976f854ee3',
404 'pt': b'd1448fa852b84408e2dad8381f363de7',
405 'iv': b'9549e4ba69a61cad7856efc1',
406 'tag': b'd72da7f5c6cf0bca7242c71835809449',
407 'key': b'5c1155084cc0ede76b3bc22e9f7574ef',
408 'ct': b'f78b60ca125218493bea1c50a2e12ef4'},
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600409 {'aad': b'',
Paul Kehrera43b6692013-11-12 15:35:49 -0600410 'pt': b'',
411 'iv': b'4e8df20faaf2c8eebe922902',
412 'tag': b'e39aeaebe86aa309a4d062d6274339',
413 'key': b'eac258e99c55e6ae8ef1da26640613d7',
414 'ct': b''},
415 {'aad': b'',
416 'iv': b'55fef82cde693ce76efcc193',
417 'tag': b'3d68111a81ed22d2ef5bccac4fc27f',
418 'key': b'3726cf02fcc6b8639a5497652c94350d',
419 'ct': b'',
420 'fail': True},
421 {'aad': b'',
422 'iv': b'eec51e7958c3f20a1bb71815',
423 'tag': b'a81886b3fb26e51fca87b267e1e157',
424 'key': b'f202299d5fd74f03b12d2119a6c4c038',
425 'ct': b'',
426 'fail': True},
427 {'aad': b'',
428 'pt': b'',
429 'iv': b'f5cf3227444afd905a5f6dba',
430 'tag': b'1665b0f1a0b456e1664cfd3de08ccd',
431 'key': b'fd52925f39546b4c55ffb6b20c59898c',
432 'ct': b''},
433 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600434 'iv': b'3c',
435 'tag': b'eae841d4355feeb3f786bc86625f1e5b',
436 'key': b'58fab7632bcf10d2bcee58520bf37414',
437 'ct': b'15c4db4cbb451211179d57017f',
Paul Kehrera43b6692013-11-12 15:35:49 -0600438 'fail': True},
Paul Kehrera43b6692013-11-12 15:35:49 -0600439 ]