blob: 901c028109e7180de958b32be025e78e156e3bc9 [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,
Alex Stapleton58f27ac2014-02-02 19:30:03 +000024 check_backend_support, select_backends, load_pkcs1_vectors
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 Kehrerc421e632014-01-18 09:22:21 -060032def test_select_one_backend():
Paul Kehrer34c075e2014-01-13 21:52:08 -050033 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"
Paul Kehreraed9e172014-01-19 12:09:27 -060038 selected_backends = select_backends(name, backends)
39 assert len(selected_backends) == 1
40 assert selected_backends[0] == b2
Paul Kehrer34c075e2014-01-13 21:52:08 -050041
42
Paul Kehrerc421e632014-01-18 09:22:21 -060043def test_select_no_backend():
Paul Kehrer34c075e2014-01-13 21:52:08 -050044 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):
Paul Kehrerc421e632014-01-18 09:22:21 -060050 select_backends(name, backends)
51
52
53def test_select_backends_none():
54 b1 = pretend.stub(name="b1")
55 b2 = pretend.stub(name="b2")
56 b3 = pretend.stub(name="b3")
57 backends = [b1, b2, b3]
58 name = None
Paul Kehreraed9e172014-01-19 12:09:27 -060059 selected_backends = select_backends(name, backends)
60 assert len(selected_backends) == 3
Paul Kehrerc421e632014-01-18 09:22:21 -060061
62
63def test_select_two_backends():
64 b1 = pretend.stub(name="b1")
65 b2 = pretend.stub(name="b2")
66 b3 = pretend.stub(name="b3")
67 backends = [b1, b2, b3]
68 name = "b2 ,b1 "
Paul Kehreraed9e172014-01-19 12:09:27 -060069 selected_backends = select_backends(name, backends)
70 assert len(selected_backends) == 2
71 assert selected_backends == [b1, b2]
Paul Kehrer34c075e2014-01-13 21:52:08 -050072
73
Alex Gaynor2b3f9422013-12-24 21:55:24 -080074def test_check_for_iface():
75 item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True})
76 with pytest.raises(pytest.skip.Exception) as exc_info:
77 check_for_iface("fake_name", FakeInterface, item)
78 assert exc_info.value.args[0] == "True backend does not support fake_name"
79
80 item = pretend.stub(
81 keywords=["fake_name"],
82 funcargs={"backend": FakeInterface()}
83 )
84 check_for_iface("fake_name", FakeInterface, item)
85
86
Paul Kehrer60fc8da2013-12-26 20:19:34 -060087def test_check_backend_support_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060088 supported = pretend.stub(
89 kwargs={"only_if": lambda backend: False, "skip_message": "Nope"}
90 )
91 item = pretend.stub(keywords={"supported": supported},
92 funcargs={"backend": True})
93 with pytest.raises(pytest.skip.Exception) as exc_info:
Paul Kehrer60fc8da2013-12-26 20:19:34 -060094 check_backend_support(item)
Paul Kehrerf03334e2014-01-02 23:16:14 -060095 assert exc_info.value.args[0] == "Nope (True)"
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060096
97
Paul Kehrer60fc8da2013-12-26 20:19:34 -060098def test_check_backend_support_no_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060099 supported = pretend.stub(
100 kwargs={"only_if": lambda backend: True, "skip_message": "Nope"}
101 )
102 item = pretend.stub(keywords={"supported": supported},
103 funcargs={"backend": True})
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600104 assert check_backend_support(item) is None
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600105
106
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600107def test_check_backend_support_no_backend():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600108 supported = pretend.stub(
109 kwargs={"only_if": "notalambda", "skip_message": "Nope"}
110 )
111 item = pretend.stub(keywords={"supported": supported},
112 funcargs={})
Paul Kehrerec495502013-12-27 15:51:40 -0600113 with pytest.raises(ValueError):
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600114 check_backend_support(item)
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600115
116
Alex Gaynorcf5fb332013-11-11 15:39:52 -0800117def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400118 vector_data = textwrap.dedent("""
119 # CAVS 11.1
120 # Config info for aes_values
121 # AESVS GFSbox test data for CBC
122 # State : Encrypt and Decrypt
123 # Key Length : 128
124 # Generated on Fri Apr 22 15:11:33 2011
125
126 [ENCRYPT]
127
128 COUNT = 0
129 KEY = 00000000000000000000000000000000
130 IV = 00000000000000000000000000000000
131 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
132 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
133
134 COUNT = 1
135 KEY = 00000000000000000000000000000000
136 IV = 00000000000000000000000000000000
137 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
138 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
139
140 [DECRYPT]
141
142 COUNT = 0
143 KEY = 00000000000000000000000000000000
144 IV = 00000000000000000000000000000000
145 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
146 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
147
148 COUNT = 1
149 KEY = 00000000000000000000000000000000
150 IV = 00000000000000000000000000000000
151 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
152 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
153 """).splitlines()
154
Alex Gaynord3ce7032013-11-11 14:46:20 -0800155 assert load_nist_vectors(vector_data) == [
156 {
157 "key": b"00000000000000000000000000000000",
158 "iv": b"00000000000000000000000000000000",
159 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
160 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
161 },
162 {
163 "key": b"00000000000000000000000000000000",
164 "iv": b"00000000000000000000000000000000",
165 "plaintext": b"9798c4640bad75c7c3227db910174e72",
166 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
167 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700168 {
169 "key": b"00000000000000000000000000000000",
170 "iv": b"00000000000000000000000000000000",
171 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
172 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
173 },
174 {
175 "key": b"00000000000000000000000000000000",
176 "iv": b"00000000000000000000000000000000",
177 "plaintext": b"9798c4640bad75c7c3227db910174e72",
178 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
179 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400180 ]
181
182
Paul Kehrer6fb1a5a2014-01-29 13:44:07 -0600183def test_load_nist_vectors_with_null_chars():
184 vector_data = textwrap.dedent("""
185 COUNT = 0
186 KEY = thing\\0withnulls
187
188 COUNT = 1
189 KEY = 00000000000000000000000000000000
190 """).splitlines()
191
192 assert load_nist_vectors(vector_data) == [
193 {
194 "key": b"thing\x00withnulls",
195 },
196 {
197 "key": b"00000000000000000000000000000000",
198 },
199 ]
200
201
Paul Kehrer1951bf62013-09-15 12:05:43 -0500202def test_load_cryptrec_vectors():
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 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
210
211 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
212 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
213
214 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
215 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
216
217 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
218
219 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
220 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
221 """).splitlines()
222
223 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700224 {
225 "key": b"00000000000000000000000000000000",
226 "plaintext": b"80000000000000000000000000000000",
227 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
228 },
229 {
230 "key": b"00000000000000000000000000000000",
231 "plaintext": b"40000000000000000000000000000000",
232 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
233 },
234 {
235 "key": b"10000000000000000000000000000000",
236 "plaintext": b"80000000000000000000000000000000",
237 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
238 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500239 ]
240
241
Donald Stufft3359d7e2013-10-19 19:33:06 -0400242def test_load_cryptrec_vectors_invalid():
243 vector_data = textwrap.dedent("""
244 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
245 # Download is t_camelia.txt
246
247 # Camellia with 128-bit key
248
249 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
250 """).splitlines()
251
252 with pytest.raises(ValueError):
253 load_cryptrec_vectors(vector_data)
254
255
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500256def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500257 vector_data = textwrap.dedent(
258 """
259 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
260 # For all CFB128 encrypts and decrypts, the transformed sequence is
261 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
262 # CFB128-CAMELLIA128.Encrypt
263 """
264 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
265 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
266 "14F7646187817EB586599146B82BD719:1\n"
267 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
268 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
269 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
270 "# CFB128-CAMELLIA128.Decrypt\n"
271 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
272 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
273 "14F7646187817EB586599146B82BD719:0\n"
274 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
275 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
276 "A53D28BB82DF741103EA4F921A44880B:0"
277 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500278
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500279 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700280 {
281 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
282 "iv": b"000102030405060708090A0B0C0D0E0F",
283 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
284 "ciphertext": b"14F7646187817EB586599146B82BD719",
285 },
286 {
287 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
288 "iv": b"14F7646187817EB586599146B82BD719",
289 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
290 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
291 },
292 {
293 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
294 "iv": b"000102030405060708090A0B0C0D0E0F",
295 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
296 "ciphertext": b"14F7646187817EB586599146B82BD719",
297 },
298 {
299 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
300 "iv": b"14F7646187817EB586599146B82BD719",
301 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
302 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
303 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500304 ]
305
306
Paul Kehrer69e06522013-10-18 17:28:39 -0500307def test_load_hash_vectors():
308 vector_data = textwrap.dedent("""
309
310 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500311 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500312
313 Len = 0
314 Msg = 00
315 MD = d41d8cd98f00b204e9800998ecf8427e
316
317 Len = 8
318 Msg = 61
319 MD = 0cc175b9c0f1b6a831c399e269772661
320
321 Len = 24
322 Msg = 616263
323 MD = 900150983cd24fb0d6963f7d28e17f72
324
325 Len = 112
326 Msg = 6d65737361676520646967657374
327 MD = f96b697d7cb7938d525a2f31aaf161d0
328 """).splitlines()
329 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500330 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
331 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
332 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
333 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500334 ]
335
336
Paul Kehrer0317b042013-10-28 17:34:27 -0500337def test_load_hmac_vectors():
338 vector_data = textwrap.dedent("""
339Len = 224
340# "Jefe"
341Key = 4a656665
342# "what do ya want for nothing?"
343Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
344MD = 750c783e6ab0b503eaa86e310a5db738
345 """).splitlines()
346 assert load_hash_vectors(vector_data) == [
347 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
348 "750c783e6ab0b503eaa86e310a5db738",
349 b"4a656665"),
350 ]
351
352
Paul Kehrer69e06522013-10-18 17:28:39 -0500353def test_load_hash_vectors_bad_data():
354 vector_data = textwrap.dedent("""
355 # http://tools.ietf.org/html/rfc1321
356
357 Len = 0
358 Msg = 00
359 UNKNOWN=Hello World
360 """).splitlines()
361 with pytest.raises(ValueError):
362 load_hash_vectors(vector_data)
363
Alex Gaynor41172ab2013-11-12 10:00:42 -0800364
Alex Gaynorab53bc52013-11-12 09:37:59 -0800365def test_load_vectors_from_file():
366 vectors = load_vectors_from_file(
367 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
368 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500369 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800370 assert vectors == [
371 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800372 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
373 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800374 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800375 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800376 ),
377 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800378 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800379 ),
380 }
381 ]
Paul Kehrera43b6692013-11-12 15:35:49 -0600382
383
384def test_load_nist_gcm_vectors():
385 vector_data = textwrap.dedent("""
386 [Keylen = 128]
387 [IVlen = 96]
388 [PTlen = 0]
389 [AADlen = 0]
390 [Taglen = 128]
391
392 Count = 0
393 Key = 11754cd72aec309bf52f7687212e8957
394 IV = 3c819d9a9bed087615030b65
395 PT =
396 AAD =
397 CT =
398 Tag = 250327c674aaf477aef2675748cf6971
399
400 Count = 1
401 Key = 272f16edb81a7abbea887357a58c1917
402 IV = 794ec588176c703d3d2a7a07
403 PT =
404 AAD =
405 CT =
406 Tag = b6e6f197168f5049aeda32dafbdaeb
407
408 Count = 2
409 Key = a49a5e26a2f8cb63d05546c2a62f5343
410 IV = 907763b19b9b4ab6bd4f0281
411 CT =
412 AAD =
413 Tag = a2be08210d8c470a8df6e8fbd79ec5cf
414 FAIL
415
416 Count = 3
417 Key = 5c1155084cc0ede76b3bc22e9f7574ef
418 IV = 9549e4ba69a61cad7856efc1
419 PT = d1448fa852b84408e2dad8381f363de7
420 AAD = e98e9d9c618e46fef32660976f854ee3
421 CT = f78b60ca125218493bea1c50a2e12ef4
422 Tag = d72da7f5c6cf0bca7242c71835809449
423
424 [Keylen = 128]
425 [IVlen = 96]
426 [PTlen = 0]
427 [AADlen = 0]
428 [Taglen = 120]
429
430 Count = 0
431 Key = eac258e99c55e6ae8ef1da26640613d7
432 IV = 4e8df20faaf2c8eebe922902
433 CT =
434 AAD =
435 Tag = e39aeaebe86aa309a4d062d6274339
436 PT =
437
438 Count = 1
439 Key = 3726cf02fcc6b8639a5497652c94350d
440 IV = 55fef82cde693ce76efcc193
441 CT =
442 AAD =
443 Tag = 3d68111a81ed22d2ef5bccac4fc27f
444 FAIL
445
446 Count = 2
447 Key = f202299d5fd74f03b12d2119a6c4c038
448 IV = eec51e7958c3f20a1bb71815
449 CT =
450 AAD =
451 Tag = a81886b3fb26e51fca87b267e1e157
452 FAIL
453
454 Count = 3
455 Key = fd52925f39546b4c55ffb6b20c59898c
456 IV = f5cf3227444afd905a5f6dba
457 CT =
458 AAD =
459 Tag = 1665b0f1a0b456e1664cfd3de08ccd
460 PT =
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600461
462 [Keylen = 128]
463 [IVlen = 8]
464 [PTlen = 104]
465 [AADlen = 0]
466 [Taglen = 128]
467
468 Count = 0
469 Key = 58fab7632bcf10d2bcee58520bf37414
470 IV = 3c
471 CT = 15c4db4cbb451211179d57017f
472 AAD =
473 Tag = eae841d4355feeb3f786bc86625f1e5b
474 FAIL
Paul Kehrera43b6692013-11-12 15:35:49 -0600475 """).splitlines()
476 assert load_nist_vectors(vector_data) == [
477 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600478 'pt': b'',
479 'iv': b'3c819d9a9bed087615030b65',
480 'tag': b'250327c674aaf477aef2675748cf6971',
481 'key': b'11754cd72aec309bf52f7687212e8957',
482 'ct': b''},
483 {'aad': b'',
484 'pt': b'',
485 'iv': b'794ec588176c703d3d2a7a07',
486 'tag': b'b6e6f197168f5049aeda32dafbdaeb',
487 'key': b'272f16edb81a7abbea887357a58c1917',
488 'ct': b''},
489 {'aad': b'',
490 'iv': b'907763b19b9b4ab6bd4f0281',
491 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf',
492 'key': b'a49a5e26a2f8cb63d05546c2a62f5343',
493 'ct': b'',
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600494 'fail': True},
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600495 {'aad': b'e98e9d9c618e46fef32660976f854ee3',
496 'pt': b'd1448fa852b84408e2dad8381f363de7',
497 'iv': b'9549e4ba69a61cad7856efc1',
498 'tag': b'd72da7f5c6cf0bca7242c71835809449',
499 'key': b'5c1155084cc0ede76b3bc22e9f7574ef',
500 'ct': b'f78b60ca125218493bea1c50a2e12ef4'},
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600501 {'aad': b'',
Paul Kehrera43b6692013-11-12 15:35:49 -0600502 'pt': b'',
503 'iv': b'4e8df20faaf2c8eebe922902',
504 'tag': b'e39aeaebe86aa309a4d062d6274339',
505 'key': b'eac258e99c55e6ae8ef1da26640613d7',
506 'ct': b''},
507 {'aad': b'',
508 'iv': b'55fef82cde693ce76efcc193',
509 'tag': b'3d68111a81ed22d2ef5bccac4fc27f',
510 'key': b'3726cf02fcc6b8639a5497652c94350d',
511 'ct': b'',
512 'fail': True},
513 {'aad': b'',
514 'iv': b'eec51e7958c3f20a1bb71815',
515 'tag': b'a81886b3fb26e51fca87b267e1e157',
516 'key': b'f202299d5fd74f03b12d2119a6c4c038',
517 'ct': b'',
518 'fail': True},
519 {'aad': b'',
520 'pt': b'',
521 'iv': b'f5cf3227444afd905a5f6dba',
522 'tag': b'1665b0f1a0b456e1664cfd3de08ccd',
523 'key': b'fd52925f39546b4c55ffb6b20c59898c',
524 'ct': b''},
525 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600526 'iv': b'3c',
527 'tag': b'eae841d4355feeb3f786bc86625f1e5b',
528 'key': b'58fab7632bcf10d2bcee58520bf37414',
529 'ct': b'15c4db4cbb451211179d57017f',
Paul Kehrera43b6692013-11-12 15:35:49 -0600530 'fail': True},
Paul Kehrera43b6692013-11-12 15:35:49 -0600531 ]
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000532
533
534def test_load_pkcs1_vectors():
535 vector_data = textwrap.dedent("""
536 Test vectors for RSA-PSS
537 ========================
538
539 This file contains an extract of the original pss-vect.txt
540
541 Key lengths:
542
543 Key 8: 1031 bits
544 Key 9: 1536 bits
545 ===========================================================================
546
547 <snip>
548
549 # Example 8: A 1031-bit RSA key pair
550 # -----------------------------------
551
552
553 # Public key
554 # ----------
555
556 # Modulus:
557 49 53 70 a1 fb 18 54 3c 16 d3 63 1e 31 63 25 5d
558 f6 2b e6 ee e8 90 d5 f2 55 09 e4 f7 78 a8 ea 6f
559 bb bc df 85 df f6 4e 0d 97 20 03 ab 36 81 fb ba
560 6d d4 1f d5 41 82 9b 2e 58 2d e9 f2 a4 a4 e0 a2
561 d0 90 0b ef 47 53 db 3c ee 0e e0 6c 7d fa e8 b1
562 d5 3b 59 53 21 8f 9c ce ea 69 5b 08 66 8e de aa
563 dc ed 94 63 b1 d7 90 d5 eb f2 7e 91 15 b4 6c ad
564 4d 9a 2b 8e fa b0 56 1b 08 10 34 47 39 ad a0 73
565 3f
566
567 # Exponent:
568 01 00 01
569
570 # Private key
571 # -----------
572
573 # Modulus:
574 49 53 70 a1 fb 18 54 3c 16 d3 63 1e 31 63 25 5d
575 f6 2b e6 ee e8 90 d5 f2 55 09 e4 f7 78 a8 ea 6f
576 bb bc df 85 df f6 4e 0d 97 20 03 ab 36 81 fb ba
577 6d d4 1f d5 41 82 9b 2e 58 2d e9 f2 a4 a4 e0 a2
578 d0 90 0b ef 47 53 db 3c ee 0e e0 6c 7d fa e8 b1
579 d5 3b 59 53 21 8f 9c ce ea 69 5b 08 66 8e de aa
580 dc ed 94 63 b1 d7 90 d5 eb f2 7e 91 15 b4 6c ad
581 4d 9a 2b 8e fa b0 56 1b 08 10 34 47 39 ad a0 73
582 3f
583
584 # Public exponent:
585 01 00 01
586
587 # Exponent:
588 6c 66 ff e9 89 80 c3 8f cd ea b5 15 98 98 83 61
589 65 f4 b4 b8 17 c4 f6 a8 d4 86 ee 4e a9 13 0f e9
590 b9 09 2b d1 36 d1 84 f9 5f 50 4a 60 7e ac 56 58
591 46 d2 fd d6 59 7a 89 67 c7 39 6e f9 5a 6e ee bb
592 45 78 a6 43 96 6d ca 4d 8e e3 de 84 2d e6 32 79
593 c6 18 15 9c 1a b5 4a 89 43 7b 6a 61 20 e4 93 0a
594 fb 52 a4 ba 6c ed 8a 49 47 ac 64 b3 0a 34 97 cb
595 e7 01 c2 d6 26 6d 51 72 19 ad 0e c6 d3 47 db e9
596
597 # Prime 1:
598 08 da d7 f1 13 63 fa a6 23 d5 d6 d5 e8 a3 19 32
599 8d 82 19 0d 71 27 d2 84 6c 43 9b 0a b7 26 19 b0
600 a4 3a 95 32 0e 4e c3 4f c3 a9 ce a8 76 42 23 05
601 bd 76 c5 ba 7b e9 e2 f4 10 c8 06 06 45 a1 d2 9e
602 db
603
604 # Prime 2:
605 08 47 e7 32 37 6f c7 90 0f 89 8e a8 2e b2 b0 fc
606 41 85 65 fd ae 62 f7 d9 ec 4c e2 21 7b 97 99 0d
607 d2 72 db 15 7f 99 f6 3c 0d cb b9 fb ac db d4 c4
608 da db 6d f6 77 56 35 8c a4 17 48 25 b4 8f 49 70
609 6d
610
611 # Prime exponent 1:
612 05 c2 a8 3c 12 4b 36 21 a2 aa 57 ea 2c 3e fe 03
613 5e ff 45 60 f3 3d de bb 7a da b8 1f ce 69 a0 c8
614 c2 ed c1 65 20 dd a8 3d 59 a2 3b e8 67 96 3a c6
615 5f 2c c7 10 bb cf b9 6e e1 03 de b7 71 d1 05 fd
616 85
617
618 # Prime exponent 2:
619 04 ca e8 aa 0d 9f aa 16 5c 87 b6 82 ec 14 0b 8e
620 d3 b5 0b 24 59 4b 7a 3b 2c 22 0b 36 69 bb 81 9f
621 98 4f 55 31 0a 1a e7 82 36 51 d4 a0 2e 99 44 79
622 72 59 51 39 36 34 34 e5 e3 0a 7e 7d 24 15 51 e1
623 b9
624
625 # Coefficient:
626 07 d3 e4 7b f6 86 60 0b 11 ac 28 3c e8 8d bb 3f
627 60 51 e8 ef d0 46 80 e4 4c 17 1e f5 31 b8 0b 2b
628 7c 39 fc 76 63 20 e2 cf 15 d8 d9 98 20 e9 6f f3
629 0d c6 96 91 83 9c 4b 40 d7 b0 6e 45 30 7d c9 1f
630 3f
631
632 # RSA-PSS signing of 6 random messages with random salts
633 # -------------------------------------------------------
634
635 <snip>
636
637 # =============================================
638
639 # Example 9: A 1536-bit RSA key pair
640 # -----------------------------------
641
642
643 # Public key
644 # ----------
645
646 # Modulus:
647 e6 bd 69 2a c9 66 45 79 04 03 fd d0 f5 be b8 b9
648 bf 92 ed 10 00 7f c3 65 04 64 19 dd 06 c0 5c 5b
649 5b 2f 48 ec f9 89 e4 ce 26 91 09 97 9c bb 40 b4
650 a0 ad 24 d2 24 83 d1 ee 31 5a d4 cc b1 53 42 68
651 35 26 91 c5 24 f6 dd 8e 6c 29 d2 24 cf 24 69 73
652 ae c8 6c 5b f6 b1 40 1a 85 0d 1b 9a d1 bb 8c bc
653 ec 47 b0 6f 0f 8c 7f 45 d3 fc 8f 31 92 99 c5 43
654 3d db c2 b3 05 3b 47 de d2 ec d4 a4 ca ef d6 14
655 83 3d c8 bb 62 2f 31 7e d0 76 b8 05 7f e8 de 3f
656 84 48 0a d5 e8 3e 4a 61 90 4a 4f 24 8f b3 97 02
657 73 57 e1 d3 0e 46 31 39 81 5c 6f d4 fd 5a c5 b8
658 17 2a 45 23 0e cb 63 18 a0 4f 14 55 d8 4e 5a 8b
659
660 # Exponent:
661 01 00 01
662
663 # Private key
664 # -----------
665
666 # Modulus:
667 e6 bd 69 2a c9 66 45 79 04 03 fd d0 f5 be b8 b9
668 bf 92 ed 10 00 7f c3 65 04 64 19 dd 06 c0 5c 5b
669 5b 2f 48 ec f9 89 e4 ce 26 91 09 97 9c bb 40 b4
670 a0 ad 24 d2 24 83 d1 ee 31 5a d4 cc b1 53 42 68
671 35 26 91 c5 24 f6 dd 8e 6c 29 d2 24 cf 24 69 73
672 ae c8 6c 5b f6 b1 40 1a 85 0d 1b 9a d1 bb 8c bc
673 ec 47 b0 6f 0f 8c 7f 45 d3 fc 8f 31 92 99 c5 43
674 3d db c2 b3 05 3b 47 de d2 ec d4 a4 ca ef d6 14
675 83 3d c8 bb 62 2f 31 7e d0 76 b8 05 7f e8 de 3f
676 84 48 0a d5 e8 3e 4a 61 90 4a 4f 24 8f b3 97 02
677 73 57 e1 d3 0e 46 31 39 81 5c 6f d4 fd 5a c5 b8
678 17 2a 45 23 0e cb 63 18 a0 4f 14 55 d8 4e 5a 8b
679
680 # Public exponent:
681 01 00 01
682
683 # Exponent:
684 6a 7f d8 4f b8 5f ad 07 3b 34 40 6d b7 4f 8d 61
685 a6 ab c1 21 96 a9 61 dd 79 56 5e 9d a6 e5 18 7b
686 ce 2d 98 02 50 f7 35 95 75 35 92 70 d9 15 90 bb
687 0e 42 7c 71 46 0b 55 d5 14 10 b1 91 bc f3 09 fe
688 a1 31 a9 2c 8e 70 27 38 fa 71 9f 1e 00 41 f5 2e
689 40 e9 1f 22 9f 4d 96 a1 e6 f1 72 e1 55 96 b4 51
690 0a 6d ae c2 61 05 f2 be bc 53 31 6b 87 bd f2 13
691 11 66 60 70 e8 df ee 69 d5 2c 71 a9 76 ca ae 79
692 c7 2b 68 d2 85 80 dc 68 6d 9f 51 29 d2 25 f8 2b
693 3d 61 55 13 a8 82 b3 db 91 41 6b 48 ce 08 88 82
694 13 e3 7e eb 9a f8 00 d8 1c ab 32 8c e4 20 68 99
695 03 c0 0c 7b 5f d3 1b 75 50 3a 6d 41 96 84 d6 29
696
697 # Prime 1:
698 f8 eb 97 e9 8d f1 26 64 ee fd b7 61 59 6a 69 dd
699 cd 0e 76 da ec e6 ed 4b f5 a1 b5 0a c0 86 f7 92
700 8a 4d 2f 87 26 a7 7e 51 5b 74 da 41 98 8f 22 0b
701 1c c8 7a a1 fc 81 0c e9 9a 82 f2 d1 ce 82 1e dc
702 ed 79 4c 69 41 f4 2c 7a 1a 0b 8c 4d 28 c7 5e c6
703 0b 65 22 79 f6 15 4a 76 2a ed 16 5d 47 de e3 67
704
705 # Prime 2:
706 ed 4d 71 d0 a6 e2 4b 93 c2 e5 f6 b4 bb e0 5f 5f
707 b0 af a0 42 d2 04 fe 33 78 d3 65 c2 f2 88 b6 a8
708 da d7 ef e4 5d 15 3e ef 40 ca cc 7b 81 ff 93 40
709 02 d1 08 99 4b 94 a5 e4 72 8c d9 c9 63 37 5a e4
710 99 65 bd a5 5c bf 0e fe d8 d6 55 3b 40 27 f2 d8
711 62 08 a6 e6 b4 89 c1 76 12 80 92 d6 29 e4 9d 3d
712
713 # Prime exponent 1:
714 2b b6 8b dd fb 0c 4f 56 c8 55 8b ff af 89 2d 80
715 43 03 78 41 e7 fa 81 cf a6 1a 38 c5 e3 9b 90 1c
716 8e e7 11 22 a5 da 22 27 bd 6c de eb 48 14 52 c1
717 2a d3 d6 1d 5e 4f 77 6a 0a b5 56 59 1b ef e3 e5
718 9e 5a 7f dd b8 34 5e 1f 2f 35 b9 f4 ce e5 7c 32
719 41 4c 08 6a ec 99 3e 93 53 e4 80 d9 ee c6 28 9f
720
721 # Prime exponent 2:
722 4f f8 97 70 9f ad 07 97 46 49 45 78 e7 0f d8 54
723 61 30 ee ab 56 27 c4 9b 08 0f 05 ee 4a d9 f3 e4
724 b7 cb a9 d6 a5 df f1 13 a4 1c 34 09 33 68 33 f1
725 90 81 6d 8a 6b c4 2e 9b ec 56 b7 56 7d 0f 3c 9c
726 69 6d b6 19 b2 45 d9 01 dd 85 6d b7 c8 09 2e 77
727 e9 a1 cc cd 56 ee 4d ba 42 c5 fd b6 1a ec 26 69
728
729 # Coefficient:
730 77 b9 d1 13 7b 50 40 4a 98 27 29 31 6e fa fc 7d
731 fe 66 d3 4e 5a 18 26 00 d5 f3 0a 0a 85 12 05 1c
732 56 0d 08 1d 4d 0a 18 35 ec 3d 25 a6 0f 4e 4d 6a
733 a9 48 b2 bf 3d bb 5b 12 4c bb c3 48 92 55 a3 a9
734 48 37 2f 69 78 49 67 45 f9 43 e1 db 4f 18 38 2c
735 ea a5 05 df c6 57 57 bb 3f 85 7a 58 dc e5 21 56
736
737 # RSA-PSS signing of 6 random messages with random salts
738 # -------------------------------------------------------
739
740 <snip>
741
742 # =============================================
743
744 <snip>
745 """).splitlines()
746
747 vectors = tuple(load_pkcs1_vectors(vector_data))
748 expected = (
749 (
750 {
751 'modulus': int(
752 '495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f77'
753 '8a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e58'
754 '2de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218'
755 'f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a'
756 '2b8efab0561b0810344739ada0733f', 16),
757 'public_exponent': int('10001', 16),
758 'private_exponent': int(
759 '6c66ffe98980c38fcdeab5159898836165f4b4b817c4f6a8d486ee4ea'
760 '9130fe9b9092bd136d184f95f504a607eac565846d2fdd6597a8967c7'
761 '396ef95a6eeebb4578a643966dca4d8ee3de842de63279c618159c1ab'
762 '54a89437b6a6120e4930afb52a4ba6ced8a4947ac64b30a3497cbe701'
763 'c2d6266d517219ad0ec6d347dbe9', 16),
764 'p': int(
765 '8dad7f11363faa623d5d6d5e8a319328d82190d7127d2846c439b0ab7'
766 '2619b0a43a95320e4ec34fc3a9cea876422305bd76c5ba7be9e2f410c'
767 '8060645a1d29edb', 16),
768 'q': int(
769 '847e732376fc7900f898ea82eb2b0fc418565fdae62f7d9ec4ce2217b'
770 '97990dd272db157f99f63c0dcbb9fbacdbd4c4dadb6df67756358ca41'
771 '74825b48f49706d', 16)
772 },
773
774 {
775 'modulus': int(
776 '495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f77'
777 '8a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e58'
778 '2de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218'
779 'f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a'
780 '2b8efab0561b0810344739ada0733f', 16),
781 'public_exponent': int('10001', 16)
782 }
783 ),
784 (
785 {
786 'modulus': int(
787 'e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd0'
788 '6c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee31'
789 '5ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b'
790 '1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddb'
791 'c2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8d'
792 'e3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6f'
793 'd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b', 16),
794 'public_exponent': int('10001', 16),
795 'private_exponent': int(
796 '6a7fd84fb85fad073b34406db74f8d61a6abc12196a961dd79565e9da'
797 '6e5187bce2d980250f7359575359270d91590bb0e427c71460b55d514'
798 '10b191bcf309fea131a92c8e702738fa719f1e0041f52e40e91f229f4'
799 'd96a1e6f172e15596b4510a6daec26105f2bebc53316b87bdf2131166'
800 '6070e8dfee69d52c71a976caae79c72b68d28580dc686d9f5129d225f'
801 '82b3d615513a882b3db91416b48ce08888213e37eeb9af800d81cab32'
802 '8ce420689903c00c7b5fd31b75503a6d419684d629', 16),
803 'p': int(
804 'f8eb97e98df12664eefdb761596a69ddcd0e76daece6ed4bf5a1b50ac'
805 '086f7928a4d2f8726a77e515b74da41988f220b1cc87aa1fc810ce99a'
806 '82f2d1ce821edced794c6941f42c7a1a0b8c4d28c75ec60b652279f61'
807 '54a762aed165d47dee367', 16),
808 'q': int(
809 'ed4d71d0a6e24b93c2e5f6b4bbe05f5fb0afa042d204fe3378d365c2f'
810 '288b6a8dad7efe45d153eef40cacc7b81ff934002d108994b94a5e472'
811 '8cd9c963375ae49965bda55cbf0efed8d6553b4027f2d86208a6e6b48'
812 '9c176128092d629e49d3d', 16)
813 },
814
815 {
816 'modulus': int(
817 'e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd0'
818 '6c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee31'
819 '5ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b'
820 '1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddb'
821 'c2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8d'
822 'e3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6f'
823 'd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b', 16),
824 'public_exponent': int('10001', 16)
825 }
826 )
827 )
828 assert vectors == expected
Ayrx4300f6c2014-02-09 15:15:13 +0800829
830
831def test_load_hotp_vectors():
832 vector_data = textwrap.dedent("""
833 # HOTP Test Vectors
834 # RFC 4226 Appendix D
835
836 COUNT = 0
837 COUNTER = 0
838 INTERMEDIATE = cc93cf18508d94934c64b65d8ba7667fb7cde4b0
839 TRUNCATED = 4c93cf18
840 HOTP = 755224
841
842 COUNT = 1
843 COUNTER = 1
844 INTERMEDIATE = 75a48a19d4cbe100644e8ac1397eea747a2d33ab
845 TRUNCATED = 41397eea
846 HOTP = 287082
847
848 COUNT = 2
849 COUNTER = 2
850 INTERMEDIATE = 0bacb7fa082fef30782211938bc1c5e70416ff44
851 TRUNCATED = 82fef30
852 HOTP = 359152
853
854 COUNT = 3
855 COUNTER = 3
856 INTERMEDIATE = 66c28227d03a2d5529262ff016a1e6ef76557ece
857 TRUNCATED = 66ef7655
858 HOTP = 969429
859 """).splitlines()
860
861 assert load_nist_vectors(vector_data) == [
862 {
863 "counter": b"0",
864 "intermediate": b"cc93cf18508d94934c64b65d8ba7667fb7cde4b0",
865 "truncated": b"4c93cf18",
866 "hotp": b"755224",
867 },
868 {
869 "counter": b"1",
870 "intermediate": b"75a48a19d4cbe100644e8ac1397eea747a2d33ab",
871 "truncated": b"41397eea",
872 "hotp": b"287082",
873 },
874 {
875 "counter": b"2",
876 "intermediate": b"0bacb7fa082fef30782211938bc1c5e70416ff44",
877 "truncated": b"82fef30",
878 "hotp": b"359152",
879 },
880 {
881 "counter": b"3",
882 "intermediate": b"66c28227d03a2d5529262ff016a1e6ef76557ece",
883 "truncated": b"66ef7655",
884 "hotp": b"969429",
885 },
886 ]
887
888
889def test_load_totp_vectors():
890 vector_data = textwrap.dedent("""
891 # TOTP Test Vectors
892 # RFC 6238 Appendix B
893
894 COUNT = 0
895 TIME = 59
896 TOTP = 94287082
897 MODE = SHA1
898
899 COUNT = 1
900 TIME = 59
901 TOTP = 46119246
902 MODE = SHA256
903
904 COUNT = 2
905 TIME = 59
906 TOTP = 90693936
907 MODE = SHA512
908 """).splitlines()
909
910 assert load_nist_vectors(vector_data) == [
911 {
912 "time": b"59",
913 "totp": b"94287082",
914 "mode": b"SHA1",
915 },
916 {
917 "time": b"59",
918 "totp": b"46119246",
919 "mode": b"SHA256",
920 },
921 {
922 "time": b"59",
923 "totp": b"90693936",
924 "mode": b"SHA512",
925 },
926 ]