blob: c640367e3bff617d56b2ccc9fc2b7166ba54a88c [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,
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060023 load_openssl_vectors, load_hash_vectors, check_for_iface,
Paul Kehrer60fc8da2013-12-26 20:19:34 -060024 check_backend_support
Alex Gaynorafdddca2013-10-21 21:00:20 -070025)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040026
27
Alex Gaynor2b3f9422013-12-24 21:55:24 -080028class FakeInterface(object):
29 pass
30
31
32def test_check_for_iface():
33 item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True})
34 with pytest.raises(pytest.skip.Exception) as exc_info:
35 check_for_iface("fake_name", FakeInterface, item)
36 assert exc_info.value.args[0] == "True backend does not support fake_name"
37
38 item = pretend.stub(
39 keywords=["fake_name"],
40 funcargs={"backend": FakeInterface()}
41 )
42 check_for_iface("fake_name", FakeInterface, item)
43
44
Paul Kehrer60fc8da2013-12-26 20:19:34 -060045def test_check_backend_support_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060046 supported = pretend.stub(
47 kwargs={"only_if": lambda backend: False, "skip_message": "Nope"}
48 )
49 item = pretend.stub(keywords={"supported": supported},
50 funcargs={"backend": True})
51 with pytest.raises(pytest.skip.Exception) as exc_info:
Paul Kehrer60fc8da2013-12-26 20:19:34 -060052 check_backend_support(item)
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060053 assert exc_info.value.args[0] == "Nope"
54
55
Paul Kehrer60fc8da2013-12-26 20:19:34 -060056def test_check_backend_support_no_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060057 supported = pretend.stub(
58 kwargs={"only_if": lambda backend: True, "skip_message": "Nope"}
59 )
60 item = pretend.stub(keywords={"supported": supported},
61 funcargs={"backend": True})
Paul Kehrer60fc8da2013-12-26 20:19:34 -060062 assert check_backend_support(item) is None
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060063
64
Paul Kehrer60fc8da2013-12-26 20:19:34 -060065def test_check_backend_support_no_backend():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060066 supported = pretend.stub(
67 kwargs={"only_if": "notalambda", "skip_message": "Nope"}
68 )
69 item = pretend.stub(keywords={"supported": supported},
70 funcargs={})
Paul Kehrerec495502013-12-27 15:51:40 -060071 with pytest.raises(ValueError):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060072 check_backend_support(item)
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060073
74
Alex Gaynorcf5fb332013-11-11 15:39:52 -080075def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040076 vector_data = textwrap.dedent("""
77 # CAVS 11.1
78 # Config info for aes_values
79 # AESVS GFSbox test data for CBC
80 # State : Encrypt and Decrypt
81 # Key Length : 128
82 # Generated on Fri Apr 22 15:11:33 2011
83
84 [ENCRYPT]
85
86 COUNT = 0
87 KEY = 00000000000000000000000000000000
88 IV = 00000000000000000000000000000000
89 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
90 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
91
92 COUNT = 1
93 KEY = 00000000000000000000000000000000
94 IV = 00000000000000000000000000000000
95 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
96 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
97
98 [DECRYPT]
99
100 COUNT = 0
101 KEY = 00000000000000000000000000000000
102 IV = 00000000000000000000000000000000
103 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
104 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
105
106 COUNT = 1
107 KEY = 00000000000000000000000000000000
108 IV = 00000000000000000000000000000000
109 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
110 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
111 """).splitlines()
112
Alex Gaynord3ce7032013-11-11 14:46:20 -0800113 assert load_nist_vectors(vector_data) == [
114 {
115 "key": b"00000000000000000000000000000000",
116 "iv": b"00000000000000000000000000000000",
117 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
118 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
119 },
120 {
121 "key": b"00000000000000000000000000000000",
122 "iv": b"00000000000000000000000000000000",
123 "plaintext": b"9798c4640bad75c7c3227db910174e72",
124 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
125 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700126 {
127 "key": b"00000000000000000000000000000000",
128 "iv": b"00000000000000000000000000000000",
129 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
130 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
131 },
132 {
133 "key": b"00000000000000000000000000000000",
134 "iv": b"00000000000000000000000000000000",
135 "plaintext": b"9798c4640bad75c7c3227db910174e72",
136 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
137 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400138 ]
139
140
Paul Kehrer1951bf62013-09-15 12:05:43 -0500141def test_load_cryptrec_vectors():
142 vector_data = textwrap.dedent("""
143 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
144 # Download is t_camelia.txt
145
146 # Camellia with 128-bit key
147
148 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
149
150 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
151 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
152
153 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
154 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
155
156 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
157
158 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
159 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
160 """).splitlines()
161
162 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700163 {
164 "key": b"00000000000000000000000000000000",
165 "plaintext": b"80000000000000000000000000000000",
166 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
167 },
168 {
169 "key": b"00000000000000000000000000000000",
170 "plaintext": b"40000000000000000000000000000000",
171 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
172 },
173 {
174 "key": b"10000000000000000000000000000000",
175 "plaintext": b"80000000000000000000000000000000",
176 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
177 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500178 ]
179
180
Donald Stufft3359d7e2013-10-19 19:33:06 -0400181def test_load_cryptrec_vectors_invalid():
182 vector_data = textwrap.dedent("""
183 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
184 # Download is t_camelia.txt
185
186 # Camellia with 128-bit key
187
188 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
189 """).splitlines()
190
191 with pytest.raises(ValueError):
192 load_cryptrec_vectors(vector_data)
193
194
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500195def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500196 vector_data = textwrap.dedent(
197 """
198 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
199 # For all CFB128 encrypts and decrypts, the transformed sequence is
200 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
201 # CFB128-CAMELLIA128.Encrypt
202 """
203 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
204 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
205 "14F7646187817EB586599146B82BD719:1\n"
206 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
207 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
208 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
209 "# CFB128-CAMELLIA128.Decrypt\n"
210 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
211 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
212 "14F7646187817EB586599146B82BD719:0\n"
213 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
214 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
215 "A53D28BB82DF741103EA4F921A44880B:0"
216 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500217
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500218 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700219 {
220 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
221 "iv": b"000102030405060708090A0B0C0D0E0F",
222 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
223 "ciphertext": b"14F7646187817EB586599146B82BD719",
224 },
225 {
226 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
227 "iv": b"14F7646187817EB586599146B82BD719",
228 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
229 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
230 },
231 {
232 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
233 "iv": b"000102030405060708090A0B0C0D0E0F",
234 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
235 "ciphertext": b"14F7646187817EB586599146B82BD719",
236 },
237 {
238 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
239 "iv": b"14F7646187817EB586599146B82BD719",
240 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
241 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
242 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500243 ]
244
245
Paul Kehrer69e06522013-10-18 17:28:39 -0500246def test_load_hash_vectors():
247 vector_data = textwrap.dedent("""
248
249 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500250 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500251
252 Len = 0
253 Msg = 00
254 MD = d41d8cd98f00b204e9800998ecf8427e
255
256 Len = 8
257 Msg = 61
258 MD = 0cc175b9c0f1b6a831c399e269772661
259
260 Len = 24
261 Msg = 616263
262 MD = 900150983cd24fb0d6963f7d28e17f72
263
264 Len = 112
265 Msg = 6d65737361676520646967657374
266 MD = f96b697d7cb7938d525a2f31aaf161d0
267 """).splitlines()
268 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500269 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
270 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
271 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
272 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500273 ]
274
275
Paul Kehrer0317b042013-10-28 17:34:27 -0500276def test_load_hmac_vectors():
277 vector_data = textwrap.dedent("""
278Len = 224
279# "Jefe"
280Key = 4a656665
281# "what do ya want for nothing?"
282Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
283MD = 750c783e6ab0b503eaa86e310a5db738
284 """).splitlines()
285 assert load_hash_vectors(vector_data) == [
286 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
287 "750c783e6ab0b503eaa86e310a5db738",
288 b"4a656665"),
289 ]
290
291
Paul Kehrer69e06522013-10-18 17:28:39 -0500292def test_load_hash_vectors_bad_data():
293 vector_data = textwrap.dedent("""
294 # http://tools.ietf.org/html/rfc1321
295
296 Len = 0
297 Msg = 00
298 UNKNOWN=Hello World
299 """).splitlines()
300 with pytest.raises(ValueError):
301 load_hash_vectors(vector_data)
302
Alex Gaynor41172ab2013-11-12 10:00:42 -0800303
Alex Gaynorab53bc52013-11-12 09:37:59 -0800304def test_load_vectors_from_file():
305 vectors = load_vectors_from_file(
306 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
307 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500308 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800309 assert vectors == [
310 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800311 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
312 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800313 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800314 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800315 ),
316 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800317 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800318 ),
319 }
320 ]
Paul Kehrera43b6692013-11-12 15:35:49 -0600321
322
323def test_load_nist_gcm_vectors():
324 vector_data = textwrap.dedent("""
325 [Keylen = 128]
326 [IVlen = 96]
327 [PTlen = 0]
328 [AADlen = 0]
329 [Taglen = 128]
330
331 Count = 0
332 Key = 11754cd72aec309bf52f7687212e8957
333 IV = 3c819d9a9bed087615030b65
334 PT =
335 AAD =
336 CT =
337 Tag = 250327c674aaf477aef2675748cf6971
338
339 Count = 1
340 Key = 272f16edb81a7abbea887357a58c1917
341 IV = 794ec588176c703d3d2a7a07
342 PT =
343 AAD =
344 CT =
345 Tag = b6e6f197168f5049aeda32dafbdaeb
346
347 Count = 2
348 Key = a49a5e26a2f8cb63d05546c2a62f5343
349 IV = 907763b19b9b4ab6bd4f0281
350 CT =
351 AAD =
352 Tag = a2be08210d8c470a8df6e8fbd79ec5cf
353 FAIL
354
355 Count = 3
356 Key = 5c1155084cc0ede76b3bc22e9f7574ef
357 IV = 9549e4ba69a61cad7856efc1
358 PT = d1448fa852b84408e2dad8381f363de7
359 AAD = e98e9d9c618e46fef32660976f854ee3
360 CT = f78b60ca125218493bea1c50a2e12ef4
361 Tag = d72da7f5c6cf0bca7242c71835809449
362
363 [Keylen = 128]
364 [IVlen = 96]
365 [PTlen = 0]
366 [AADlen = 0]
367 [Taglen = 120]
368
369 Count = 0
370 Key = eac258e99c55e6ae8ef1da26640613d7
371 IV = 4e8df20faaf2c8eebe922902
372 CT =
373 AAD =
374 Tag = e39aeaebe86aa309a4d062d6274339
375 PT =
376
377 Count = 1
378 Key = 3726cf02fcc6b8639a5497652c94350d
379 IV = 55fef82cde693ce76efcc193
380 CT =
381 AAD =
382 Tag = 3d68111a81ed22d2ef5bccac4fc27f
383 FAIL
384
385 Count = 2
386 Key = f202299d5fd74f03b12d2119a6c4c038
387 IV = eec51e7958c3f20a1bb71815
388 CT =
389 AAD =
390 Tag = a81886b3fb26e51fca87b267e1e157
391 FAIL
392
393 Count = 3
394 Key = fd52925f39546b4c55ffb6b20c59898c
395 IV = f5cf3227444afd905a5f6dba
396 CT =
397 AAD =
398 Tag = 1665b0f1a0b456e1664cfd3de08ccd
399 PT =
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600400
401 [Keylen = 128]
402 [IVlen = 8]
403 [PTlen = 104]
404 [AADlen = 0]
405 [Taglen = 128]
406
407 Count = 0
408 Key = 58fab7632bcf10d2bcee58520bf37414
409 IV = 3c
410 CT = 15c4db4cbb451211179d57017f
411 AAD =
412 Tag = eae841d4355feeb3f786bc86625f1e5b
413 FAIL
Paul Kehrera43b6692013-11-12 15:35:49 -0600414 """).splitlines()
415 assert load_nist_vectors(vector_data) == [
416 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600417 'pt': b'',
418 'iv': b'3c819d9a9bed087615030b65',
419 'tag': b'250327c674aaf477aef2675748cf6971',
420 'key': b'11754cd72aec309bf52f7687212e8957',
421 'ct': b''},
422 {'aad': b'',
423 'pt': b'',
424 'iv': b'794ec588176c703d3d2a7a07',
425 'tag': b'b6e6f197168f5049aeda32dafbdaeb',
426 'key': b'272f16edb81a7abbea887357a58c1917',
427 'ct': b''},
428 {'aad': b'',
429 'iv': b'907763b19b9b4ab6bd4f0281',
430 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf',
431 'key': b'a49a5e26a2f8cb63d05546c2a62f5343',
432 'ct': b'',
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600433 'fail': True},
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600434 {'aad': b'e98e9d9c618e46fef32660976f854ee3',
435 'pt': b'd1448fa852b84408e2dad8381f363de7',
436 'iv': b'9549e4ba69a61cad7856efc1',
437 'tag': b'd72da7f5c6cf0bca7242c71835809449',
438 'key': b'5c1155084cc0ede76b3bc22e9f7574ef',
439 'ct': b'f78b60ca125218493bea1c50a2e12ef4'},
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600440 {'aad': b'',
Paul Kehrera43b6692013-11-12 15:35:49 -0600441 'pt': b'',
442 'iv': b'4e8df20faaf2c8eebe922902',
443 'tag': b'e39aeaebe86aa309a4d062d6274339',
444 'key': b'eac258e99c55e6ae8ef1da26640613d7',
445 'ct': b''},
446 {'aad': b'',
447 'iv': b'55fef82cde693ce76efcc193',
448 'tag': b'3d68111a81ed22d2ef5bccac4fc27f',
449 'key': b'3726cf02fcc6b8639a5497652c94350d',
450 'ct': b'',
451 'fail': True},
452 {'aad': b'',
453 'iv': b'eec51e7958c3f20a1bb71815',
454 'tag': b'a81886b3fb26e51fca87b267e1e157',
455 'key': b'f202299d5fd74f03b12d2119a6c4c038',
456 'ct': b'',
457 'fail': True},
458 {'aad': b'',
459 'pt': b'',
460 'iv': b'f5cf3227444afd905a5f6dba',
461 'tag': b'1665b0f1a0b456e1664cfd3de08ccd',
462 'key': b'fd52925f39546b4c55ffb6b20c59898c',
463 'ct': b''},
464 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600465 'iv': b'3c',
466 'tag': b'eae841d4355feeb3f786bc86625f1e5b',
467 'key': b'58fab7632bcf10d2bcee58520bf37414',
468 'ct': b'15c4db4cbb451211179d57017f',
Paul Kehrera43b6692013-11-12 15:35:49 -0600469 'fail': True},
Paul Kehrera43b6692013-11-12 15:35:49 -0600470 ]