blob: e8037c1de284adebff91db1362c4e9c6698732d0 [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 Kehrer34c075e2014-01-13 21:52:08 -050024 check_backend_support, modify_backend_list
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
Paul Kehrer34c075e2014-01-13 21:52:08 -050032def test_modify_backend_list_leave_one():
33 b1 = pretend.stub(name="b1")
34 b2 = pretend.stub(name="b2")
35 b3 = pretend.stub(name="b3")
36 backends = [b1, b2, b3]
37 name = "b2"
38 modify_backend_list(name, backends)
39 assert len(backends) == 1
40 assert backends[0] == b2
41
42
43def test_modify_backend_list_error_none():
44 b1 = pretend.stub(name="b1")
45 b2 = pretend.stub(name="b2")
46 b3 = pretend.stub(name="b3")
47 backends = [b1, b2, b3]
48 name = "back!"
49 with pytest.raises(ValueError):
50 modify_backend_list(name, backends)
51
52
Alex Gaynor2b3f9422013-12-24 21:55:24 -080053def test_check_for_iface():
54 item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True})
55 with pytest.raises(pytest.skip.Exception) as exc_info:
56 check_for_iface("fake_name", FakeInterface, item)
57 assert exc_info.value.args[0] == "True backend does not support fake_name"
58
59 item = pretend.stub(
60 keywords=["fake_name"],
61 funcargs={"backend": FakeInterface()}
62 )
63 check_for_iface("fake_name", FakeInterface, item)
64
65
Paul Kehrer60fc8da2013-12-26 20:19:34 -060066def test_check_backend_support_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060067 supported = pretend.stub(
68 kwargs={"only_if": lambda backend: False, "skip_message": "Nope"}
69 )
70 item = pretend.stub(keywords={"supported": supported},
71 funcargs={"backend": True})
72 with pytest.raises(pytest.skip.Exception) as exc_info:
Paul Kehrer60fc8da2013-12-26 20:19:34 -060073 check_backend_support(item)
Paul Kehrerf03334e2014-01-02 23:16:14 -060074 assert exc_info.value.args[0] == "Nope (True)"
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060075
76
Paul Kehrer60fc8da2013-12-26 20:19:34 -060077def test_check_backend_support_no_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060078 supported = pretend.stub(
79 kwargs={"only_if": lambda backend: True, "skip_message": "Nope"}
80 )
81 item = pretend.stub(keywords={"supported": supported},
82 funcargs={"backend": True})
Paul Kehrer60fc8da2013-12-26 20:19:34 -060083 assert check_backend_support(item) is None
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060084
85
Paul Kehrer60fc8da2013-12-26 20:19:34 -060086def test_check_backend_support_no_backend():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060087 supported = pretend.stub(
88 kwargs={"only_if": "notalambda", "skip_message": "Nope"}
89 )
90 item = pretend.stub(keywords={"supported": supported},
91 funcargs={})
Paul Kehrerec495502013-12-27 15:51:40 -060092 with pytest.raises(ValueError):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060093 check_backend_support(item)
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060094
95
Alex Gaynorcf5fb332013-11-11 15:39:52 -080096def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040097 vector_data = textwrap.dedent("""
98 # CAVS 11.1
99 # Config info for aes_values
100 # AESVS GFSbox test data for CBC
101 # State : Encrypt and Decrypt
102 # Key Length : 128
103 # Generated on Fri Apr 22 15:11:33 2011
104
105 [ENCRYPT]
106
107 COUNT = 0
108 KEY = 00000000000000000000000000000000
109 IV = 00000000000000000000000000000000
110 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
111 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
112
113 COUNT = 1
114 KEY = 00000000000000000000000000000000
115 IV = 00000000000000000000000000000000
116 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
117 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
118
119 [DECRYPT]
120
121 COUNT = 0
122 KEY = 00000000000000000000000000000000
123 IV = 00000000000000000000000000000000
124 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
125 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
126
127 COUNT = 1
128 KEY = 00000000000000000000000000000000
129 IV = 00000000000000000000000000000000
130 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
131 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
132 """).splitlines()
133
Alex Gaynord3ce7032013-11-11 14:46:20 -0800134 assert load_nist_vectors(vector_data) == [
135 {
136 "key": b"00000000000000000000000000000000",
137 "iv": b"00000000000000000000000000000000",
138 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
139 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
140 },
141 {
142 "key": b"00000000000000000000000000000000",
143 "iv": b"00000000000000000000000000000000",
144 "plaintext": b"9798c4640bad75c7c3227db910174e72",
145 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
146 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700147 {
148 "key": b"00000000000000000000000000000000",
149 "iv": b"00000000000000000000000000000000",
150 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
151 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
152 },
153 {
154 "key": b"00000000000000000000000000000000",
155 "iv": b"00000000000000000000000000000000",
156 "plaintext": b"9798c4640bad75c7c3227db910174e72",
157 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
158 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400159 ]
160
161
Paul Kehrer1951bf62013-09-15 12:05:43 -0500162def test_load_cryptrec_vectors():
163 vector_data = textwrap.dedent("""
164 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
165 # Download is t_camelia.txt
166
167 # Camellia with 128-bit key
168
169 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
170
171 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
172 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
173
174 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
175 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
176
177 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
178
179 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
180 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
181 """).splitlines()
182
183 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700184 {
185 "key": b"00000000000000000000000000000000",
186 "plaintext": b"80000000000000000000000000000000",
187 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
188 },
189 {
190 "key": b"00000000000000000000000000000000",
191 "plaintext": b"40000000000000000000000000000000",
192 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
193 },
194 {
195 "key": b"10000000000000000000000000000000",
196 "plaintext": b"80000000000000000000000000000000",
197 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
198 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500199 ]
200
201
Donald Stufft3359d7e2013-10-19 19:33:06 -0400202def test_load_cryptrec_vectors_invalid():
203 vector_data = textwrap.dedent("""
204 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
205 # Download is t_camelia.txt
206
207 # Camellia with 128-bit key
208
209 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
210 """).splitlines()
211
212 with pytest.raises(ValueError):
213 load_cryptrec_vectors(vector_data)
214
215
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500216def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500217 vector_data = textwrap.dedent(
218 """
219 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
220 # For all CFB128 encrypts and decrypts, the transformed sequence is
221 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
222 # CFB128-CAMELLIA128.Encrypt
223 """
224 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
225 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
226 "14F7646187817EB586599146B82BD719:1\n"
227 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
228 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
229 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
230 "# CFB128-CAMELLIA128.Decrypt\n"
231 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
232 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
233 "14F7646187817EB586599146B82BD719:0\n"
234 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
235 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
236 "A53D28BB82DF741103EA4F921A44880B:0"
237 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500238
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500239 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700240 {
241 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
242 "iv": b"000102030405060708090A0B0C0D0E0F",
243 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
244 "ciphertext": b"14F7646187817EB586599146B82BD719",
245 },
246 {
247 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
248 "iv": b"14F7646187817EB586599146B82BD719",
249 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
250 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
251 },
252 {
253 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
254 "iv": b"000102030405060708090A0B0C0D0E0F",
255 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
256 "ciphertext": b"14F7646187817EB586599146B82BD719",
257 },
258 {
259 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
260 "iv": b"14F7646187817EB586599146B82BD719",
261 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
262 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
263 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500264 ]
265
266
Paul Kehrer69e06522013-10-18 17:28:39 -0500267def test_load_hash_vectors():
268 vector_data = textwrap.dedent("""
269
270 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500271 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500272
273 Len = 0
274 Msg = 00
275 MD = d41d8cd98f00b204e9800998ecf8427e
276
277 Len = 8
278 Msg = 61
279 MD = 0cc175b9c0f1b6a831c399e269772661
280
281 Len = 24
282 Msg = 616263
283 MD = 900150983cd24fb0d6963f7d28e17f72
284
285 Len = 112
286 Msg = 6d65737361676520646967657374
287 MD = f96b697d7cb7938d525a2f31aaf161d0
288 """).splitlines()
289 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500290 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
291 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
292 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
293 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500294 ]
295
296
Paul Kehrer0317b042013-10-28 17:34:27 -0500297def test_load_hmac_vectors():
298 vector_data = textwrap.dedent("""
299Len = 224
300# "Jefe"
301Key = 4a656665
302# "what do ya want for nothing?"
303Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
304MD = 750c783e6ab0b503eaa86e310a5db738
305 """).splitlines()
306 assert load_hash_vectors(vector_data) == [
307 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
308 "750c783e6ab0b503eaa86e310a5db738",
309 b"4a656665"),
310 ]
311
312
Paul Kehrer69e06522013-10-18 17:28:39 -0500313def test_load_hash_vectors_bad_data():
314 vector_data = textwrap.dedent("""
315 # http://tools.ietf.org/html/rfc1321
316
317 Len = 0
318 Msg = 00
319 UNKNOWN=Hello World
320 """).splitlines()
321 with pytest.raises(ValueError):
322 load_hash_vectors(vector_data)
323
Alex Gaynor41172ab2013-11-12 10:00:42 -0800324
Alex Gaynorab53bc52013-11-12 09:37:59 -0800325def test_load_vectors_from_file():
326 vectors = load_vectors_from_file(
327 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
328 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500329 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800330 assert vectors == [
331 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800332 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
333 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800334 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800335 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800336 ),
337 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800338 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800339 ),
340 }
341 ]
Paul Kehrera43b6692013-11-12 15:35:49 -0600342
343
344def test_load_nist_gcm_vectors():
345 vector_data = textwrap.dedent("""
346 [Keylen = 128]
347 [IVlen = 96]
348 [PTlen = 0]
349 [AADlen = 0]
350 [Taglen = 128]
351
352 Count = 0
353 Key = 11754cd72aec309bf52f7687212e8957
354 IV = 3c819d9a9bed087615030b65
355 PT =
356 AAD =
357 CT =
358 Tag = 250327c674aaf477aef2675748cf6971
359
360 Count = 1
361 Key = 272f16edb81a7abbea887357a58c1917
362 IV = 794ec588176c703d3d2a7a07
363 PT =
364 AAD =
365 CT =
366 Tag = b6e6f197168f5049aeda32dafbdaeb
367
368 Count = 2
369 Key = a49a5e26a2f8cb63d05546c2a62f5343
370 IV = 907763b19b9b4ab6bd4f0281
371 CT =
372 AAD =
373 Tag = a2be08210d8c470a8df6e8fbd79ec5cf
374 FAIL
375
376 Count = 3
377 Key = 5c1155084cc0ede76b3bc22e9f7574ef
378 IV = 9549e4ba69a61cad7856efc1
379 PT = d1448fa852b84408e2dad8381f363de7
380 AAD = e98e9d9c618e46fef32660976f854ee3
381 CT = f78b60ca125218493bea1c50a2e12ef4
382 Tag = d72da7f5c6cf0bca7242c71835809449
383
384 [Keylen = 128]
385 [IVlen = 96]
386 [PTlen = 0]
387 [AADlen = 0]
388 [Taglen = 120]
389
390 Count = 0
391 Key = eac258e99c55e6ae8ef1da26640613d7
392 IV = 4e8df20faaf2c8eebe922902
393 CT =
394 AAD =
395 Tag = e39aeaebe86aa309a4d062d6274339
396 PT =
397
398 Count = 1
399 Key = 3726cf02fcc6b8639a5497652c94350d
400 IV = 55fef82cde693ce76efcc193
401 CT =
402 AAD =
403 Tag = 3d68111a81ed22d2ef5bccac4fc27f
404 FAIL
405
406 Count = 2
407 Key = f202299d5fd74f03b12d2119a6c4c038
408 IV = eec51e7958c3f20a1bb71815
409 CT =
410 AAD =
411 Tag = a81886b3fb26e51fca87b267e1e157
412 FAIL
413
414 Count = 3
415 Key = fd52925f39546b4c55ffb6b20c59898c
416 IV = f5cf3227444afd905a5f6dba
417 CT =
418 AAD =
419 Tag = 1665b0f1a0b456e1664cfd3de08ccd
420 PT =
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600421
422 [Keylen = 128]
423 [IVlen = 8]
424 [PTlen = 104]
425 [AADlen = 0]
426 [Taglen = 128]
427
428 Count = 0
429 Key = 58fab7632bcf10d2bcee58520bf37414
430 IV = 3c
431 CT = 15c4db4cbb451211179d57017f
432 AAD =
433 Tag = eae841d4355feeb3f786bc86625f1e5b
434 FAIL
Paul Kehrera43b6692013-11-12 15:35:49 -0600435 """).splitlines()
436 assert load_nist_vectors(vector_data) == [
437 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600438 'pt': b'',
439 'iv': b'3c819d9a9bed087615030b65',
440 'tag': b'250327c674aaf477aef2675748cf6971',
441 'key': b'11754cd72aec309bf52f7687212e8957',
442 'ct': b''},
443 {'aad': b'',
444 'pt': b'',
445 'iv': b'794ec588176c703d3d2a7a07',
446 'tag': b'b6e6f197168f5049aeda32dafbdaeb',
447 'key': b'272f16edb81a7abbea887357a58c1917',
448 'ct': b''},
449 {'aad': b'',
450 'iv': b'907763b19b9b4ab6bd4f0281',
451 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf',
452 'key': b'a49a5e26a2f8cb63d05546c2a62f5343',
453 'ct': b'',
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600454 'fail': True},
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600455 {'aad': b'e98e9d9c618e46fef32660976f854ee3',
456 'pt': b'd1448fa852b84408e2dad8381f363de7',
457 'iv': b'9549e4ba69a61cad7856efc1',
458 'tag': b'd72da7f5c6cf0bca7242c71835809449',
459 'key': b'5c1155084cc0ede76b3bc22e9f7574ef',
460 'ct': b'f78b60ca125218493bea1c50a2e12ef4'},
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600461 {'aad': b'',
Paul Kehrera43b6692013-11-12 15:35:49 -0600462 'pt': b'',
463 'iv': b'4e8df20faaf2c8eebe922902',
464 'tag': b'e39aeaebe86aa309a4d062d6274339',
465 'key': b'eac258e99c55e6ae8ef1da26640613d7',
466 'ct': b''},
467 {'aad': b'',
468 'iv': b'55fef82cde693ce76efcc193',
469 'tag': b'3d68111a81ed22d2ef5bccac4fc27f',
470 'key': b'3726cf02fcc6b8639a5497652c94350d',
471 'ct': b'',
472 'fail': True},
473 {'aad': b'',
474 'iv': b'eec51e7958c3f20a1bb71815',
475 'tag': b'a81886b3fb26e51fca87b267e1e157',
476 'key': b'f202299d5fd74f03b12d2119a6c4c038',
477 'ct': b'',
478 'fail': True},
479 {'aad': b'',
480 'pt': b'',
481 'iv': b'f5cf3227444afd905a5f6dba',
482 'tag': b'1665b0f1a0b456e1664cfd3de08ccd',
483 'key': b'fd52925f39546b4c55ffb6b20c59898c',
484 'ct': b''},
485 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600486 'iv': b'3c',
487 'tag': b'eae841d4355feeb3f786bc86625f1e5b',
488 'key': b'58fab7632bcf10d2bcee58520bf37414',
489 'ct': b'15c4db4cbb451211179d57017f',
Paul Kehrera43b6692013-11-12 15:35:49 -0600490 'fail': True},
Paul Kehrera43b6692013-11-12 15:35:49 -0600491 ]