blob: 352085af460cbc14ef0ad4040ead15fa0dd57d94 [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 Gaynorc37feed2014-03-08 08:32:56 -080014from __future__ import absolute_import, division, print_function
15
Alex Gaynorab53bc52013-11-12 09:37:59 -080016import os
Donald Stufft9e1a48b2013-08-09 00:32:30 -040017import textwrap
18
Alex Gaynor2b3f9422013-12-24 21:55:24 -080019import pretend
20
Paul Kehrer79c16e92013-10-18 17:44:36 -050021import pytest
22
Alex Gaynorafdddca2013-10-21 21:00:20 -070023from .utils import (
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060024 load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors,
Paul Kehrerd5c9f5a2014-02-15 22:17:14 -060025 load_hash_vectors, check_for_iface, check_backend_support,
26 select_backends, load_pkcs1_vectors
Alex Gaynorafdddca2013-10-21 21:00:20 -070027)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040028
29
Alex Gaynor2b3f9422013-12-24 21:55:24 -080030class FakeInterface(object):
31 pass
32
33
Paul Kehrerc421e632014-01-18 09:22:21 -060034def test_select_one_backend():
Paul Kehrer34c075e2014-01-13 21:52:08 -050035 b1 = pretend.stub(name="b1")
36 b2 = pretend.stub(name="b2")
37 b3 = pretend.stub(name="b3")
38 backends = [b1, b2, b3]
39 name = "b2"
Paul Kehreraed9e172014-01-19 12:09:27 -060040 selected_backends = select_backends(name, backends)
41 assert len(selected_backends) == 1
42 assert selected_backends[0] == b2
Paul Kehrer34c075e2014-01-13 21:52:08 -050043
44
Paul Kehrerc421e632014-01-18 09:22:21 -060045def test_select_no_backend():
Paul Kehrer34c075e2014-01-13 21:52:08 -050046 b1 = pretend.stub(name="b1")
47 b2 = pretend.stub(name="b2")
48 b3 = pretend.stub(name="b3")
49 backends = [b1, b2, b3]
50 name = "back!"
51 with pytest.raises(ValueError):
Paul Kehrerc421e632014-01-18 09:22:21 -060052 select_backends(name, backends)
53
54
55def test_select_backends_none():
56 b1 = pretend.stub(name="b1")
57 b2 = pretend.stub(name="b2")
58 b3 = pretend.stub(name="b3")
59 backends = [b1, b2, b3]
60 name = None
Paul Kehreraed9e172014-01-19 12:09:27 -060061 selected_backends = select_backends(name, backends)
62 assert len(selected_backends) == 3
Paul Kehrerc421e632014-01-18 09:22:21 -060063
64
65def test_select_two_backends():
66 b1 = pretend.stub(name="b1")
67 b2 = pretend.stub(name="b2")
68 b3 = pretend.stub(name="b3")
69 backends = [b1, b2, b3]
70 name = "b2 ,b1 "
Paul Kehreraed9e172014-01-19 12:09:27 -060071 selected_backends = select_backends(name, backends)
72 assert len(selected_backends) == 2
73 assert selected_backends == [b1, b2]
Paul Kehrer34c075e2014-01-13 21:52:08 -050074
75
Alex Gaynor2b3f9422013-12-24 21:55:24 -080076def test_check_for_iface():
77 item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True})
78 with pytest.raises(pytest.skip.Exception) as exc_info:
79 check_for_iface("fake_name", FakeInterface, item)
80 assert exc_info.value.args[0] == "True backend does not support fake_name"
81
82 item = pretend.stub(
83 keywords=["fake_name"],
84 funcargs={"backend": FakeInterface()}
85 )
86 check_for_iface("fake_name", FakeInterface, item)
87
88
Paul Kehrer60fc8da2013-12-26 20:19:34 -060089def test_check_backend_support_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060090 supported = pretend.stub(
91 kwargs={"only_if": lambda backend: False, "skip_message": "Nope"}
92 )
93 item = pretend.stub(keywords={"supported": supported},
94 funcargs={"backend": True})
95 with pytest.raises(pytest.skip.Exception) as exc_info:
Paul Kehrer60fc8da2013-12-26 20:19:34 -060096 check_backend_support(item)
Paul Kehrerf03334e2014-01-02 23:16:14 -060097 assert exc_info.value.args[0] == "Nope (True)"
Paul Kehrer5a8fdf82013-12-26 20:13:45 -060098
99
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600100def test_check_backend_support_no_skip():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600101 supported = pretend.stub(
102 kwargs={"only_if": lambda backend: True, "skip_message": "Nope"}
103 )
104 item = pretend.stub(keywords={"supported": supported},
105 funcargs={"backend": True})
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600106 assert check_backend_support(item) is None
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600107
108
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600109def test_check_backend_support_no_backend():
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600110 supported = pretend.stub(
111 kwargs={"only_if": "notalambda", "skip_message": "Nope"}
112 )
113 item = pretend.stub(keywords={"supported": supported},
114 funcargs={})
Paul Kehrerec495502013-12-27 15:51:40 -0600115 with pytest.raises(ValueError):
Paul Kehrer60fc8da2013-12-26 20:19:34 -0600116 check_backend_support(item)
Paul Kehrer5a8fdf82013-12-26 20:13:45 -0600117
118
Alex Gaynorcf5fb332013-11-11 15:39:52 -0800119def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400120 vector_data = textwrap.dedent("""
121 # CAVS 11.1
122 # Config info for aes_values
123 # AESVS GFSbox test data for CBC
124 # State : Encrypt and Decrypt
125 # Key Length : 128
126 # Generated on Fri Apr 22 15:11:33 2011
127
128 [ENCRYPT]
129
130 COUNT = 0
131 KEY = 00000000000000000000000000000000
132 IV = 00000000000000000000000000000000
133 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
134 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
135
136 COUNT = 1
137 KEY = 00000000000000000000000000000000
138 IV = 00000000000000000000000000000000
139 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
140 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
141
142 [DECRYPT]
143
144 COUNT = 0
145 KEY = 00000000000000000000000000000000
146 IV = 00000000000000000000000000000000
147 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
148 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
149
150 COUNT = 1
151 KEY = 00000000000000000000000000000000
152 IV = 00000000000000000000000000000000
153 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
154 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
155 """).splitlines()
156
Alex Gaynord3ce7032013-11-11 14:46:20 -0800157 assert load_nist_vectors(vector_data) == [
158 {
159 "key": b"00000000000000000000000000000000",
160 "iv": b"00000000000000000000000000000000",
161 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
162 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
163 },
164 {
165 "key": b"00000000000000000000000000000000",
166 "iv": b"00000000000000000000000000000000",
167 "plaintext": b"9798c4640bad75c7c3227db910174e72",
168 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
169 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700170 {
171 "key": b"00000000000000000000000000000000",
172 "iv": b"00000000000000000000000000000000",
173 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
174 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
175 },
176 {
177 "key": b"00000000000000000000000000000000",
178 "iv": b"00000000000000000000000000000000",
179 "plaintext": b"9798c4640bad75c7c3227db910174e72",
180 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
181 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400182 ]
183
184
Paul Kehrer6fb1a5a2014-01-29 13:44:07 -0600185def test_load_nist_vectors_with_null_chars():
186 vector_data = textwrap.dedent("""
187 COUNT = 0
188 KEY = thing\\0withnulls
189
190 COUNT = 1
191 KEY = 00000000000000000000000000000000
192 """).splitlines()
193
194 assert load_nist_vectors(vector_data) == [
195 {
196 "key": b"thing\x00withnulls",
197 },
198 {
199 "key": b"00000000000000000000000000000000",
200 },
201 ]
202
203
Paul Kehrer1951bf62013-09-15 12:05:43 -0500204def test_load_cryptrec_vectors():
205 vector_data = textwrap.dedent("""
206 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
207 # Download is t_camelia.txt
208
209 # Camellia with 128-bit key
210
211 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
212
213 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
214 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
215
216 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
217 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
218
219 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
220
221 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
222 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
223 """).splitlines()
224
225 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700226 {
227 "key": b"00000000000000000000000000000000",
228 "plaintext": b"80000000000000000000000000000000",
229 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
230 },
231 {
232 "key": b"00000000000000000000000000000000",
233 "plaintext": b"40000000000000000000000000000000",
234 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
235 },
236 {
237 "key": b"10000000000000000000000000000000",
238 "plaintext": b"80000000000000000000000000000000",
239 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
240 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500241 ]
242
243
Donald Stufft3359d7e2013-10-19 19:33:06 -0400244def test_load_cryptrec_vectors_invalid():
245 vector_data = textwrap.dedent("""
246 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
247 # Download is t_camelia.txt
248
249 # Camellia with 128-bit key
250
251 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
252 """).splitlines()
253
254 with pytest.raises(ValueError):
255 load_cryptrec_vectors(vector_data)
256
257
Paul Kehrer69e06522013-10-18 17:28:39 -0500258def test_load_hash_vectors():
259 vector_data = textwrap.dedent("""
260
261 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500262 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500263
264 Len = 0
265 Msg = 00
266 MD = d41d8cd98f00b204e9800998ecf8427e
267
268 Len = 8
269 Msg = 61
270 MD = 0cc175b9c0f1b6a831c399e269772661
271
272 Len = 24
273 Msg = 616263
274 MD = 900150983cd24fb0d6963f7d28e17f72
275
276 Len = 112
277 Msg = 6d65737361676520646967657374
278 MD = f96b697d7cb7938d525a2f31aaf161d0
279 """).splitlines()
280 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500281 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
282 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
283 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
284 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500285 ]
286
287
Paul Kehrer0317b042013-10-28 17:34:27 -0500288def test_load_hmac_vectors():
289 vector_data = textwrap.dedent("""
290Len = 224
291# "Jefe"
292Key = 4a656665
293# "what do ya want for nothing?"
294Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
295MD = 750c783e6ab0b503eaa86e310a5db738
296 """).splitlines()
297 assert load_hash_vectors(vector_data) == [
298 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
299 "750c783e6ab0b503eaa86e310a5db738",
300 b"4a656665"),
301 ]
302
303
Paul Kehrer69e06522013-10-18 17:28:39 -0500304def test_load_hash_vectors_bad_data():
305 vector_data = textwrap.dedent("""
306 # http://tools.ietf.org/html/rfc1321
307
308 Len = 0
309 Msg = 00
310 UNKNOWN=Hello World
311 """).splitlines()
312 with pytest.raises(ValueError):
313 load_hash_vectors(vector_data)
314
Alex Gaynor41172ab2013-11-12 10:00:42 -0800315
Alex Gaynorab53bc52013-11-12 09:37:59 -0800316def test_load_vectors_from_file():
317 vectors = load_vectors_from_file(
318 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
319 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500320 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800321 assert vectors == [
322 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800323 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
324 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800325 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800326 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800327 ),
328 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800329 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800330 ),
331 }
332 ]
Paul Kehrera43b6692013-11-12 15:35:49 -0600333
334
335def test_load_nist_gcm_vectors():
336 vector_data = textwrap.dedent("""
337 [Keylen = 128]
338 [IVlen = 96]
339 [PTlen = 0]
340 [AADlen = 0]
341 [Taglen = 128]
342
343 Count = 0
344 Key = 11754cd72aec309bf52f7687212e8957
345 IV = 3c819d9a9bed087615030b65
346 PT =
347 AAD =
348 CT =
349 Tag = 250327c674aaf477aef2675748cf6971
350
351 Count = 1
352 Key = 272f16edb81a7abbea887357a58c1917
353 IV = 794ec588176c703d3d2a7a07
354 PT =
355 AAD =
356 CT =
357 Tag = b6e6f197168f5049aeda32dafbdaeb
358
359 Count = 2
360 Key = a49a5e26a2f8cb63d05546c2a62f5343
361 IV = 907763b19b9b4ab6bd4f0281
362 CT =
363 AAD =
364 Tag = a2be08210d8c470a8df6e8fbd79ec5cf
365 FAIL
366
367 Count = 3
368 Key = 5c1155084cc0ede76b3bc22e9f7574ef
369 IV = 9549e4ba69a61cad7856efc1
370 PT = d1448fa852b84408e2dad8381f363de7
371 AAD = e98e9d9c618e46fef32660976f854ee3
372 CT = f78b60ca125218493bea1c50a2e12ef4
373 Tag = d72da7f5c6cf0bca7242c71835809449
374
375 [Keylen = 128]
376 [IVlen = 96]
377 [PTlen = 0]
378 [AADlen = 0]
379 [Taglen = 120]
380
381 Count = 0
382 Key = eac258e99c55e6ae8ef1da26640613d7
383 IV = 4e8df20faaf2c8eebe922902
384 CT =
385 AAD =
386 Tag = e39aeaebe86aa309a4d062d6274339
387 PT =
388
389 Count = 1
390 Key = 3726cf02fcc6b8639a5497652c94350d
391 IV = 55fef82cde693ce76efcc193
392 CT =
393 AAD =
394 Tag = 3d68111a81ed22d2ef5bccac4fc27f
395 FAIL
396
397 Count = 2
398 Key = f202299d5fd74f03b12d2119a6c4c038
399 IV = eec51e7958c3f20a1bb71815
400 CT =
401 AAD =
402 Tag = a81886b3fb26e51fca87b267e1e157
403 FAIL
404
405 Count = 3
406 Key = fd52925f39546b4c55ffb6b20c59898c
407 IV = f5cf3227444afd905a5f6dba
408 CT =
409 AAD =
410 Tag = 1665b0f1a0b456e1664cfd3de08ccd
411 PT =
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600412
413 [Keylen = 128]
414 [IVlen = 8]
415 [PTlen = 104]
416 [AADlen = 0]
417 [Taglen = 128]
418
419 Count = 0
420 Key = 58fab7632bcf10d2bcee58520bf37414
421 IV = 3c
422 CT = 15c4db4cbb451211179d57017f
423 AAD =
424 Tag = eae841d4355feeb3f786bc86625f1e5b
425 FAIL
Paul Kehrera43b6692013-11-12 15:35:49 -0600426 """).splitlines()
427 assert load_nist_vectors(vector_data) == [
428 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600429 'pt': b'',
430 'iv': b'3c819d9a9bed087615030b65',
431 'tag': b'250327c674aaf477aef2675748cf6971',
432 'key': b'11754cd72aec309bf52f7687212e8957',
433 'ct': b''},
434 {'aad': b'',
435 'pt': b'',
436 'iv': b'794ec588176c703d3d2a7a07',
437 'tag': b'b6e6f197168f5049aeda32dafbdaeb',
438 'key': b'272f16edb81a7abbea887357a58c1917',
439 'ct': b''},
440 {'aad': b'',
441 'iv': b'907763b19b9b4ab6bd4f0281',
442 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf',
443 'key': b'a49a5e26a2f8cb63d05546c2a62f5343',
444 'ct': b'',
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600445 'fail': True},
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600446 {'aad': b'e98e9d9c618e46fef32660976f854ee3',
447 'pt': b'd1448fa852b84408e2dad8381f363de7',
448 'iv': b'9549e4ba69a61cad7856efc1',
449 'tag': b'd72da7f5c6cf0bca7242c71835809449',
450 'key': b'5c1155084cc0ede76b3bc22e9f7574ef',
451 'ct': b'f78b60ca125218493bea1c50a2e12ef4'},
Paul Kehrerc985dbb2013-11-18 14:11:55 -0600452 {'aad': b'',
Paul Kehrera43b6692013-11-12 15:35:49 -0600453 'pt': b'',
454 'iv': b'4e8df20faaf2c8eebe922902',
455 'tag': b'e39aeaebe86aa309a4d062d6274339',
456 'key': b'eac258e99c55e6ae8ef1da26640613d7',
457 'ct': b''},
458 {'aad': b'',
459 'iv': b'55fef82cde693ce76efcc193',
460 'tag': b'3d68111a81ed22d2ef5bccac4fc27f',
461 'key': b'3726cf02fcc6b8639a5497652c94350d',
462 'ct': b'',
463 'fail': True},
464 {'aad': b'',
465 'iv': b'eec51e7958c3f20a1bb71815',
466 'tag': b'a81886b3fb26e51fca87b267e1e157',
467 'key': b'f202299d5fd74f03b12d2119a6c4c038',
468 'ct': b'',
469 'fail': True},
470 {'aad': b'',
471 'pt': b'',
472 'iv': b'f5cf3227444afd905a5f6dba',
473 'tag': b'1665b0f1a0b456e1664cfd3de08ccd',
474 'key': b'fd52925f39546b4c55ffb6b20c59898c',
475 'ct': b''},
476 {'aad': b'',
Paul Kehrer749ac5b2013-11-18 18:12:41 -0600477 'iv': b'3c',
478 'tag': b'eae841d4355feeb3f786bc86625f1e5b',
479 'key': b'58fab7632bcf10d2bcee58520bf37414',
480 'ct': b'15c4db4cbb451211179d57017f',
Paul Kehrera43b6692013-11-12 15:35:49 -0600481 'fail': True},
Paul Kehrera43b6692013-11-12 15:35:49 -0600482 ]
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000483
484
485def test_load_pkcs1_vectors():
486 vector_data = textwrap.dedent("""
487 Test vectors for RSA-PSS
488 ========================
489
490 This file contains an extract of the original pss-vect.txt
491
492 Key lengths:
493
494 Key 8: 1031 bits
495 Key 9: 1536 bits
496 ===========================================================================
497
498 <snip>
499
500 # Example 8: A 1031-bit RSA key pair
501 # -----------------------------------
502
503
504 # Public key
505 # ----------
506
507 # Modulus:
508 49 53 70 a1 fb 18 54 3c 16 d3 63 1e 31 63 25 5d
509 f6 2b e6 ee e8 90 d5 f2 55 09 e4 f7 78 a8 ea 6f
510 bb bc df 85 df f6 4e 0d 97 20 03 ab 36 81 fb ba
511 6d d4 1f d5 41 82 9b 2e 58 2d e9 f2 a4 a4 e0 a2
512 d0 90 0b ef 47 53 db 3c ee 0e e0 6c 7d fa e8 b1
513 d5 3b 59 53 21 8f 9c ce ea 69 5b 08 66 8e de aa
514 dc ed 94 63 b1 d7 90 d5 eb f2 7e 91 15 b4 6c ad
515 4d 9a 2b 8e fa b0 56 1b 08 10 34 47 39 ad a0 73
516 3f
517
518 # Exponent:
519 01 00 01
520
521 # Private key
522 # -----------
523
524 # Modulus:
525 49 53 70 a1 fb 18 54 3c 16 d3 63 1e 31 63 25 5d
526 f6 2b e6 ee e8 90 d5 f2 55 09 e4 f7 78 a8 ea 6f
527 bb bc df 85 df f6 4e 0d 97 20 03 ab 36 81 fb ba
528 6d d4 1f d5 41 82 9b 2e 58 2d e9 f2 a4 a4 e0 a2
529 d0 90 0b ef 47 53 db 3c ee 0e e0 6c 7d fa e8 b1
530 d5 3b 59 53 21 8f 9c ce ea 69 5b 08 66 8e de aa
531 dc ed 94 63 b1 d7 90 d5 eb f2 7e 91 15 b4 6c ad
532 4d 9a 2b 8e fa b0 56 1b 08 10 34 47 39 ad a0 73
533 3f
534
535 # Public exponent:
536 01 00 01
537
538 # Exponent:
539 6c 66 ff e9 89 80 c3 8f cd ea b5 15 98 98 83 61
540 65 f4 b4 b8 17 c4 f6 a8 d4 86 ee 4e a9 13 0f e9
541 b9 09 2b d1 36 d1 84 f9 5f 50 4a 60 7e ac 56 58
542 46 d2 fd d6 59 7a 89 67 c7 39 6e f9 5a 6e ee bb
543 45 78 a6 43 96 6d ca 4d 8e e3 de 84 2d e6 32 79
544 c6 18 15 9c 1a b5 4a 89 43 7b 6a 61 20 e4 93 0a
545 fb 52 a4 ba 6c ed 8a 49 47 ac 64 b3 0a 34 97 cb
546 e7 01 c2 d6 26 6d 51 72 19 ad 0e c6 d3 47 db e9
547
548 # Prime 1:
549 08 da d7 f1 13 63 fa a6 23 d5 d6 d5 e8 a3 19 32
550 8d 82 19 0d 71 27 d2 84 6c 43 9b 0a b7 26 19 b0
551 a4 3a 95 32 0e 4e c3 4f c3 a9 ce a8 76 42 23 05
552 bd 76 c5 ba 7b e9 e2 f4 10 c8 06 06 45 a1 d2 9e
553 db
554
555 # Prime 2:
556 08 47 e7 32 37 6f c7 90 0f 89 8e a8 2e b2 b0 fc
557 41 85 65 fd ae 62 f7 d9 ec 4c e2 21 7b 97 99 0d
558 d2 72 db 15 7f 99 f6 3c 0d cb b9 fb ac db d4 c4
559 da db 6d f6 77 56 35 8c a4 17 48 25 b4 8f 49 70
560 6d
561
562 # Prime exponent 1:
563 05 c2 a8 3c 12 4b 36 21 a2 aa 57 ea 2c 3e fe 03
564 5e ff 45 60 f3 3d de bb 7a da b8 1f ce 69 a0 c8
565 c2 ed c1 65 20 dd a8 3d 59 a2 3b e8 67 96 3a c6
566 5f 2c c7 10 bb cf b9 6e e1 03 de b7 71 d1 05 fd
567 85
568
569 # Prime exponent 2:
570 04 ca e8 aa 0d 9f aa 16 5c 87 b6 82 ec 14 0b 8e
571 d3 b5 0b 24 59 4b 7a 3b 2c 22 0b 36 69 bb 81 9f
572 98 4f 55 31 0a 1a e7 82 36 51 d4 a0 2e 99 44 79
573 72 59 51 39 36 34 34 e5 e3 0a 7e 7d 24 15 51 e1
574 b9
575
576 # Coefficient:
577 07 d3 e4 7b f6 86 60 0b 11 ac 28 3c e8 8d bb 3f
578 60 51 e8 ef d0 46 80 e4 4c 17 1e f5 31 b8 0b 2b
579 7c 39 fc 76 63 20 e2 cf 15 d8 d9 98 20 e9 6f f3
580 0d c6 96 91 83 9c 4b 40 d7 b0 6e 45 30 7d c9 1f
581 3f
582
583 # RSA-PSS signing of 6 random messages with random salts
584 # -------------------------------------------------------
Paul Kehrerefca2802014-02-17 20:55:13 -0600585 # PSS Example 8.1
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000586
Paul Kehrerefca2802014-02-17 20:55:13 -0600587 # -----------------
588
589 # Message to be signed:
590 81 33 2f 4b e6 29 48 41 5e a1 d8 99 79 2e ea cf
591 6c 6e 1d b1 da 8b e1 3b 5c ea 41 db 2f ed 46 70
592 92 e1 ff 39 89 14 c7 14 25 97 75 f5 95 f8 54 7f
593 73 56 92 a5 75 e6 92 3a f7 8f 22 c6 99 7d db 90
594 fb 6f 72 d7 bb 0d d5 74 4a 31 de cd 3d c3 68 58
595 49 83 6e d3 4a ec 59 63 04 ad 11 84 3c 4f 88 48
596 9f 20 97 35 f5 fb 7f da f7 ce c8 ad dc 58 18 16
597 8f 88 0a cb f4 90 d5 10 05 b7 a8 e8 4e 43 e5 42
598 87 97 75 71 dd 99 ee a4 b1 61 eb 2d f1 f5 10 8f
599 12 a4 14 2a 83 32 2e db 05 a7 54 87 a3 43 5c 9a
600 78 ce 53 ed 93 bc 55 08 57 d7 a9 fb
601
602 # Salt:
603 1d 65 49 1d 79 c8 64 b3 73 00 9b e6 f6 f2 46 7b
604 ac 4c 78 fa
605
606 # Signature:
607 02 62 ac 25 4b fa 77 f3 c1 ac a2 2c 51 79 f8 f0
608 40 42 2b 3c 5b af d4 0a 8f 21 cf 0f a5 a6 67 cc
609 d5 99 3d 42 db af b4 09 c5 20 e2 5f ce 2b 1e e1
610 e7 16 57 7f 1e fa 17 f3 da 28 05 2f 40 f0 41 9b
611 23 10 6d 78 45 aa f0 11 25 b6 98 e7 a4 df e9 2d
612 39 67 bb 00 c4 d0 d3 5b a3 55 2a b9 a8 b3 ee f0
613 7c 7f ec db c5 42 4a c4 db 1e 20 cb 37 d0 b2 74
614 47 69 94 0e a9 07 e1 7f bb ca 67 3b 20 52 23 80
615 c5
616
617 # PSS Example 8.2
618
619 # -----------------
620
621 # Message to be signed:
622 e2 f9 6e af 0e 05 e7 ba 32 6e cc a0 ba 7f d2 f7
623 c0 23 56 f3 ce de 9d 0f aa bf 4f cc 8e 60 a9 73
624 e5 59 5f d9 ea 08
625
626 # Salt:
627 43 5c 09 8a a9 90 9e b2 37 7f 12 48 b0 91 b6 89
628 87 ff 18 38
629
630 # Signature:
631 27 07 b9 ad 51 15 c5 8c 94 e9 32 e8 ec 0a 28 0f
632 56 33 9e 44 a1 b5 8d 4d dc ff 2f 31 2e 5f 34 dc
633 fe 39 e8 9c 6a 94 dc ee 86 db bd ae 5b 79 ba 4e
634 08 19 a9 e7 bf d9 d9 82 e7 ee 6c 86 ee 68 39 6e
635 8b 3a 14 c9 c8 f3 4b 17 8e b7 41 f9 d3 f1 21 10
636 9b f5 c8 17 2f ad a2 e7 68 f9 ea 14 33 03 2c 00
637 4a 8a a0 7e b9 90 00 0a 48 dc 94 c8 ba c8 aa be
638 2b 09 b1 aa 46 c0 a2 aa 0e 12 f6 3f bb a7 75 ba
639 7e
640
641 # <snip>
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000642
643 # =============================================
644
645 # Example 9: A 1536-bit RSA key pair
646 # -----------------------------------
647
648
649 # Public key
650 # ----------
651
652 # Modulus:
653 e6 bd 69 2a c9 66 45 79 04 03 fd d0 f5 be b8 b9
654 bf 92 ed 10 00 7f c3 65 04 64 19 dd 06 c0 5c 5b
655 5b 2f 48 ec f9 89 e4 ce 26 91 09 97 9c bb 40 b4
656 a0 ad 24 d2 24 83 d1 ee 31 5a d4 cc b1 53 42 68
657 35 26 91 c5 24 f6 dd 8e 6c 29 d2 24 cf 24 69 73
658 ae c8 6c 5b f6 b1 40 1a 85 0d 1b 9a d1 bb 8c bc
659 ec 47 b0 6f 0f 8c 7f 45 d3 fc 8f 31 92 99 c5 43
660 3d db c2 b3 05 3b 47 de d2 ec d4 a4 ca ef d6 14
661 83 3d c8 bb 62 2f 31 7e d0 76 b8 05 7f e8 de 3f
662 84 48 0a d5 e8 3e 4a 61 90 4a 4f 24 8f b3 97 02
663 73 57 e1 d3 0e 46 31 39 81 5c 6f d4 fd 5a c5 b8
664 17 2a 45 23 0e cb 63 18 a0 4f 14 55 d8 4e 5a 8b
665
666 # Exponent:
667 01 00 01
668
669 # Private key
670 # -----------
671
672 # Modulus:
673 e6 bd 69 2a c9 66 45 79 04 03 fd d0 f5 be b8 b9
674 bf 92 ed 10 00 7f c3 65 04 64 19 dd 06 c0 5c 5b
675 5b 2f 48 ec f9 89 e4 ce 26 91 09 97 9c bb 40 b4
676 a0 ad 24 d2 24 83 d1 ee 31 5a d4 cc b1 53 42 68
677 35 26 91 c5 24 f6 dd 8e 6c 29 d2 24 cf 24 69 73
678 ae c8 6c 5b f6 b1 40 1a 85 0d 1b 9a d1 bb 8c bc
679 ec 47 b0 6f 0f 8c 7f 45 d3 fc 8f 31 92 99 c5 43
680 3d db c2 b3 05 3b 47 de d2 ec d4 a4 ca ef d6 14
681 83 3d c8 bb 62 2f 31 7e d0 76 b8 05 7f e8 de 3f
682 84 48 0a d5 e8 3e 4a 61 90 4a 4f 24 8f b3 97 02
683 73 57 e1 d3 0e 46 31 39 81 5c 6f d4 fd 5a c5 b8
684 17 2a 45 23 0e cb 63 18 a0 4f 14 55 d8 4e 5a 8b
685
686 # Public exponent:
687 01 00 01
688
689 # Exponent:
690 6a 7f d8 4f b8 5f ad 07 3b 34 40 6d b7 4f 8d 61
691 a6 ab c1 21 96 a9 61 dd 79 56 5e 9d a6 e5 18 7b
692 ce 2d 98 02 50 f7 35 95 75 35 92 70 d9 15 90 bb
693 0e 42 7c 71 46 0b 55 d5 14 10 b1 91 bc f3 09 fe
694 a1 31 a9 2c 8e 70 27 38 fa 71 9f 1e 00 41 f5 2e
695 40 e9 1f 22 9f 4d 96 a1 e6 f1 72 e1 55 96 b4 51
696 0a 6d ae c2 61 05 f2 be bc 53 31 6b 87 bd f2 13
697 11 66 60 70 e8 df ee 69 d5 2c 71 a9 76 ca ae 79
698 c7 2b 68 d2 85 80 dc 68 6d 9f 51 29 d2 25 f8 2b
699 3d 61 55 13 a8 82 b3 db 91 41 6b 48 ce 08 88 82
700 13 e3 7e eb 9a f8 00 d8 1c ab 32 8c e4 20 68 99
701 03 c0 0c 7b 5f d3 1b 75 50 3a 6d 41 96 84 d6 29
702
703 # Prime 1:
704 f8 eb 97 e9 8d f1 26 64 ee fd b7 61 59 6a 69 dd
705 cd 0e 76 da ec e6 ed 4b f5 a1 b5 0a c0 86 f7 92
706 8a 4d 2f 87 26 a7 7e 51 5b 74 da 41 98 8f 22 0b
707 1c c8 7a a1 fc 81 0c e9 9a 82 f2 d1 ce 82 1e dc
708 ed 79 4c 69 41 f4 2c 7a 1a 0b 8c 4d 28 c7 5e c6
709 0b 65 22 79 f6 15 4a 76 2a ed 16 5d 47 de e3 67
710
711 # Prime 2:
712 ed 4d 71 d0 a6 e2 4b 93 c2 e5 f6 b4 bb e0 5f 5f
713 b0 af a0 42 d2 04 fe 33 78 d3 65 c2 f2 88 b6 a8
714 da d7 ef e4 5d 15 3e ef 40 ca cc 7b 81 ff 93 40
715 02 d1 08 99 4b 94 a5 e4 72 8c d9 c9 63 37 5a e4
716 99 65 bd a5 5c bf 0e fe d8 d6 55 3b 40 27 f2 d8
717 62 08 a6 e6 b4 89 c1 76 12 80 92 d6 29 e4 9d 3d
718
719 # Prime exponent 1:
720 2b b6 8b dd fb 0c 4f 56 c8 55 8b ff af 89 2d 80
721 43 03 78 41 e7 fa 81 cf a6 1a 38 c5 e3 9b 90 1c
722 8e e7 11 22 a5 da 22 27 bd 6c de eb 48 14 52 c1
723 2a d3 d6 1d 5e 4f 77 6a 0a b5 56 59 1b ef e3 e5
724 9e 5a 7f dd b8 34 5e 1f 2f 35 b9 f4 ce e5 7c 32
725 41 4c 08 6a ec 99 3e 93 53 e4 80 d9 ee c6 28 9f
726
727 # Prime exponent 2:
728 4f f8 97 70 9f ad 07 97 46 49 45 78 e7 0f d8 54
729 61 30 ee ab 56 27 c4 9b 08 0f 05 ee 4a d9 f3 e4
730 b7 cb a9 d6 a5 df f1 13 a4 1c 34 09 33 68 33 f1
731 90 81 6d 8a 6b c4 2e 9b ec 56 b7 56 7d 0f 3c 9c
732 69 6d b6 19 b2 45 d9 01 dd 85 6d b7 c8 09 2e 77
733 e9 a1 cc cd 56 ee 4d ba 42 c5 fd b6 1a ec 26 69
734
735 # Coefficient:
736 77 b9 d1 13 7b 50 40 4a 98 27 29 31 6e fa fc 7d
737 fe 66 d3 4e 5a 18 26 00 d5 f3 0a 0a 85 12 05 1c
738 56 0d 08 1d 4d 0a 18 35 ec 3d 25 a6 0f 4e 4d 6a
739 a9 48 b2 bf 3d bb 5b 12 4c bb c3 48 92 55 a3 a9
740 48 37 2f 69 78 49 67 45 f9 43 e1 db 4f 18 38 2c
741 ea a5 05 df c6 57 57 bb 3f 85 7a 58 dc e5 21 56
742
Paul Kehrerefca2802014-02-17 20:55:13 -0600743 # PKCS#1 v1.5 Signature Example 2.17
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000744
Paul Kehrerefca2802014-02-17 20:55:13 -0600745 # -----------------
746
747 # Message to be signed:
748 06 ad d7 5a b6 89 de 06 77 44 e6 9a 2e bd 4b 90
749 fa 93 83 00 3c d0 5f f5 36 cb f2 94 cd 21 5f 09
750 23 b7 fc 90 04 f0 aa 18 52 71 a1 d0 06 1f d0 e9
751 77 7a d1 ec 0c 71 59 1f 57 8b f7 b8 e5 a1
752
753 # Signature:
754 45 14 21 0e 54 1d 5b ad 7d d6 0a e5 49 b9 43 ac
755 c4 4f 21 39 0d f5 b6 13 18 45 5a 17 61 0d f5 b7
756 4d 84 ae d2 32 f1 7e 59 d9 1d d2 65 99 22 f8 12
757 db d4 96 81 69 03 84 b9 54 e9 ad fb 9b 1a 96 8c
758 0c bf f7 63 ec ee d6 27 50 c5 91 64 b5 e0 80 a8
759 fe f3 d5 5b fe 2a cf ad 27 52 a6 a8 45 9f a1 fa
760 b4 9a d3 78 c6 96 4b 23 ee 97 fd 10 34 61 0c 5c
761 c1 4c 61 e0 eb fb 17 11 f8 ad e9 6f e6 55 7b 38
762
763 # <snip>
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000764
765 # =============================================
766
Paul Kehrerefca2802014-02-17 20:55:13 -0600767 # <snip>
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000768 """).splitlines()
769
770 vectors = tuple(load_pkcs1_vectors(vector_data))
771 expected = (
772 (
773 {
774 'modulus': int(
775 '495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f77'
776 '8a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e58'
777 '2de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218'
778 'f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a'
779 '2b8efab0561b0810344739ada0733f', 16),
780 'public_exponent': int('10001', 16),
781 'private_exponent': int(
782 '6c66ffe98980c38fcdeab5159898836165f4b4b817c4f6a8d486ee4ea'
783 '9130fe9b9092bd136d184f95f504a607eac565846d2fdd6597a8967c7'
784 '396ef95a6eeebb4578a643966dca4d8ee3de842de63279c618159c1ab'
785 '54a89437b6a6120e4930afb52a4ba6ced8a4947ac64b30a3497cbe701'
786 'c2d6266d517219ad0ec6d347dbe9', 16),
787 'p': int(
788 '8dad7f11363faa623d5d6d5e8a319328d82190d7127d2846c439b0ab7'
789 '2619b0a43a95320e4ec34fc3a9cea876422305bd76c5ba7be9e2f410c'
790 '8060645a1d29edb', 16),
791 'q': int(
792 '847e732376fc7900f898ea82eb2b0fc418565fdae62f7d9ec4ce2217b'
793 '97990dd272db157f99f63c0dcbb9fbacdbd4c4dadb6df67756358ca41'
Paul Kehrer09328bb2014-02-12 23:57:27 -0600794 '74825b48f49706d', 16),
795 'dmp1': int(
796 '05c2a83c124b3621a2aa57ea2c3efe035eff4560f33ddebb7adab81fc'
797 'e69a0c8c2edc16520dda83d59a23be867963ac65f2cc710bbcfb96ee1'
798 '03deb771d105fd85', 16),
799 'dmq1': int(
800 '04cae8aa0d9faa165c87b682ec140b8ed3b50b24594b7a3b2c220b366'
801 '9bb819f984f55310a1ae7823651d4a02e99447972595139363434e5e3'
802 '0a7e7d241551e1b9', 16),
803 'iqmp': int(
804 '07d3e47bf686600b11ac283ce88dbb3f6051e8efd04680e44c171ef53'
805 '1b80b2b7c39fc766320e2cf15d8d99820e96ff30dc69691839c4b40d7'
Paul Kehrerefca2802014-02-17 20:55:13 -0600806 'b06e45307dc91f3f', 16),
807 'examples': [
808 {
Paul Kehrer26811802014-02-19 16:32:11 -0600809 'message': b'81332f4be62948415ea1d899792eeacf6c6e1db1d'
810 b'a8be13b5cea41db2fed467092e1ff398914c71425'
811 b'9775f595f8547f735692a575e6923af78f22c6997'
812 b'ddb90fb6f72d7bb0dd5744a31decd3dc368584983'
813 b'6ed34aec596304ad11843c4f88489f209735f5fb7'
814 b'fdaf7cec8addc5818168f880acbf490d51005b7a8'
815 b'e84e43e54287977571dd99eea4b161eb2df1f5108'
816 b'f12a4142a83322edb05a75487a3435c9a78ce53ed'
817 b'93bc550857d7a9fb',
818 'salt': b'1d65491d79c864b373009be6f6f2467bac4c78fa',
819 'signature': b'0262ac254bfa77f3c1aca22c5179f8f040422b3'
820 b'c5bafd40a8f21cf0fa5a667ccd5993d42dbafb4'
821 b'09c520e25fce2b1ee1e716577f1efa17f3da280'
822 b'52f40f0419b23106d7845aaf01125b698e7a4df'
823 b'e92d3967bb00c4d0d35ba3552ab9a8b3eef07c7'
824 b'fecdbc5424ac4db1e20cb37d0b2744769940ea9'
825 b'07e17fbbca673b20522380c5'
Paul Kehrerefca2802014-02-17 20:55:13 -0600826 }, {
Paul Kehrer26811802014-02-19 16:32:11 -0600827 'message': b'e2f96eaf0e05e7ba326ecca0ba7fd2f7c02356f3c'
828 b'ede9d0faabf4fcc8e60a973e5595fd9ea08',
829 'salt': b'435c098aa9909eb2377f1248b091b68987ff1838',
830 'signature': b'2707b9ad5115c58c94e932e8ec0a280f56339e4'
831 b'4a1b58d4ddcff2f312e5f34dcfe39e89c6a94dc'
832 b'ee86dbbdae5b79ba4e0819a9e7bfd9d982e7ee6'
833 b'c86ee68396e8b3a14c9c8f34b178eb741f9d3f1'
834 b'21109bf5c8172fada2e768f9ea1433032c004a8'
835 b'aa07eb990000a48dc94c8bac8aabe2b09b1aa46'
836 b'c0a2aa0e12f63fbba775ba7e'
Paul Kehrerefca2802014-02-17 20:55:13 -0600837 }
838 ]
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000839 },
840
841 {
842 'modulus': int(
843 '495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f77'
844 '8a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e58'
845 '2de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218'
846 'f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a'
847 '2b8efab0561b0810344739ada0733f', 16),
848 'public_exponent': int('10001', 16)
849 }
850 ),
851 (
852 {
853 'modulus': int(
854 'e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd0'
855 '6c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee31'
856 '5ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b'
857 '1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddb'
858 'c2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8d'
859 'e3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6f'
860 'd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b', 16),
861 'public_exponent': int('10001', 16),
862 'private_exponent': int(
863 '6a7fd84fb85fad073b34406db74f8d61a6abc12196a961dd79565e9da'
864 '6e5187bce2d980250f7359575359270d91590bb0e427c71460b55d514'
865 '10b191bcf309fea131a92c8e702738fa719f1e0041f52e40e91f229f4'
866 'd96a1e6f172e15596b4510a6daec26105f2bebc53316b87bdf2131166'
867 '6070e8dfee69d52c71a976caae79c72b68d28580dc686d9f5129d225f'
868 '82b3d615513a882b3db91416b48ce08888213e37eeb9af800d81cab32'
869 '8ce420689903c00c7b5fd31b75503a6d419684d629', 16),
870 'p': int(
871 'f8eb97e98df12664eefdb761596a69ddcd0e76daece6ed4bf5a1b50ac'
872 '086f7928a4d2f8726a77e515b74da41988f220b1cc87aa1fc810ce99a'
873 '82f2d1ce821edced794c6941f42c7a1a0b8c4d28c75ec60b652279f61'
874 '54a762aed165d47dee367', 16),
875 'q': int(
876 'ed4d71d0a6e24b93c2e5f6b4bbe05f5fb0afa042d204fe3378d365c2f'
877 '288b6a8dad7efe45d153eef40cacc7b81ff934002d108994b94a5e472'
878 '8cd9c963375ae49965bda55cbf0efed8d6553b4027f2d86208a6e6b48'
Paul Kehrer09328bb2014-02-12 23:57:27 -0600879 '9c176128092d629e49d3d', 16),
880 'dmp1': int(
881 '2bb68bddfb0c4f56c8558bffaf892d8043037841e7fa81cfa61a38c5e'
882 '39b901c8ee71122a5da2227bd6cdeeb481452c12ad3d61d5e4f776a0a'
883 'b556591befe3e59e5a7fddb8345e1f2f35b9f4cee57c32414c086aec9'
884 '93e9353e480d9eec6289f', 16),
885 'dmq1': int(
886 '4ff897709fad079746494578e70fd8546130eeab5627c49b080f05ee4'
887 'ad9f3e4b7cba9d6a5dff113a41c3409336833f190816d8a6bc42e9bec'
888 '56b7567d0f3c9c696db619b245d901dd856db7c8092e77e9a1cccd56e'
889 'e4dba42c5fdb61aec2669', 16),
890 'iqmp': int(
891 '77b9d1137b50404a982729316efafc7dfe66d34e5a182600d5f30a0a8'
892 '512051c560d081d4d0a1835ec3d25a60f4e4d6aa948b2bf3dbb5b124c'
893 'bbc3489255a3a948372f6978496745f943e1db4f18382ceaa505dfc65'
Paul Kehrerefca2802014-02-17 20:55:13 -0600894 '757bb3f857a58dce52156', 16),
895 'examples': [
896 {
Paul Kehrer26811802014-02-19 16:32:11 -0600897 'message': b'06add75ab689de067744e69a2ebd4b90fa9383003'
898 b'cd05ff536cbf294cd215f0923b7fc9004f0aa1852'
899 b'71a1d0061fd0e9777ad1ec0c71591f578bf7b8e5a'
900 b'1',
901 'signature': b'4514210e541d5bad7dd60ae549b943acc44f213'
902 b'90df5b61318455a17610df5b74d84aed232f17e'
903 b'59d91dd2659922f812dbd49681690384b954e9a'
904 b'dfb9b1a968c0cbff763eceed62750c59164b5e0'
905 b'80a8fef3d55bfe2acfad2752a6a8459fa1fab49'
906 b'ad378c6964b23ee97fd1034610c5cc14c61e0eb'
907 b'fb1711f8ade96fe6557b38'
Paul Kehrerefca2802014-02-17 20:55:13 -0600908 }
909 ]
Alex Stapleton58f27ac2014-02-02 19:30:03 +0000910 },
911
912 {
913 'modulus': int(
914 'e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd0'
915 '6c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee31'
916 '5ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b'
917 '1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddb'
918 'c2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8d'
919 'e3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6f'
920 'd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b', 16),
921 'public_exponent': int('10001', 16)
922 }
923 )
924 )
925 assert vectors == expected
Ayrx4300f6c2014-02-09 15:15:13 +0800926
927
928def test_load_hotp_vectors():
929 vector_data = textwrap.dedent("""
930 # HOTP Test Vectors
931 # RFC 4226 Appendix D
932
933 COUNT = 0
934 COUNTER = 0
935 INTERMEDIATE = cc93cf18508d94934c64b65d8ba7667fb7cde4b0
936 TRUNCATED = 4c93cf18
937 HOTP = 755224
Ayrxefc68382014-02-10 00:01:05 +0800938 SECRET = 12345678901234567890
Ayrx4300f6c2014-02-09 15:15:13 +0800939
940 COUNT = 1
941 COUNTER = 1
942 INTERMEDIATE = 75a48a19d4cbe100644e8ac1397eea747a2d33ab
943 TRUNCATED = 41397eea
944 HOTP = 287082
Ayrxefc68382014-02-10 00:01:05 +0800945 SECRET = 12345678901234567890
946
Ayrx4300f6c2014-02-09 15:15:13 +0800947
948 COUNT = 2
949 COUNTER = 2
950 INTERMEDIATE = 0bacb7fa082fef30782211938bc1c5e70416ff44
951 TRUNCATED = 82fef30
952 HOTP = 359152
Ayrxefc68382014-02-10 00:01:05 +0800953 SECRET = 12345678901234567890
954
Ayrx4300f6c2014-02-09 15:15:13 +0800955
956 COUNT = 3
957 COUNTER = 3
958 INTERMEDIATE = 66c28227d03a2d5529262ff016a1e6ef76557ece
959 TRUNCATED = 66ef7655
960 HOTP = 969429
Ayrxefc68382014-02-10 00:01:05 +0800961 SECRET = 12345678901234567890
Ayrx4300f6c2014-02-09 15:15:13 +0800962 """).splitlines()
963
964 assert load_nist_vectors(vector_data) == [
965 {
966 "counter": b"0",
967 "intermediate": b"cc93cf18508d94934c64b65d8ba7667fb7cde4b0",
968 "truncated": b"4c93cf18",
969 "hotp": b"755224",
Ayrxefc68382014-02-10 00:01:05 +0800970 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +0800971 },
972 {
973 "counter": b"1",
974 "intermediate": b"75a48a19d4cbe100644e8ac1397eea747a2d33ab",
975 "truncated": b"41397eea",
976 "hotp": b"287082",
Ayrxefc68382014-02-10 00:01:05 +0800977 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +0800978 },
979 {
980 "counter": b"2",
981 "intermediate": b"0bacb7fa082fef30782211938bc1c5e70416ff44",
982 "truncated": b"82fef30",
983 "hotp": b"359152",
Ayrxefc68382014-02-10 00:01:05 +0800984 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +0800985 },
986 {
987 "counter": b"3",
988 "intermediate": b"66c28227d03a2d5529262ff016a1e6ef76557ece",
989 "truncated": b"66ef7655",
990 "hotp": b"969429",
Ayrxefc68382014-02-10 00:01:05 +0800991 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +0800992 },
993 ]
994
995
996def test_load_totp_vectors():
997 vector_data = textwrap.dedent("""
998 # TOTP Test Vectors
999 # RFC 6238 Appendix B
1000
1001 COUNT = 0
1002 TIME = 59
1003 TOTP = 94287082
1004 MODE = SHA1
Ayrxefc68382014-02-10 00:01:05 +08001005 SECRET = 12345678901234567890
Ayrx4300f6c2014-02-09 15:15:13 +08001006
1007 COUNT = 1
1008 TIME = 59
1009 TOTP = 46119246
1010 MODE = SHA256
Ayrxefc68382014-02-10 00:01:05 +08001011 SECRET = 12345678901234567890
Ayrx4300f6c2014-02-09 15:15:13 +08001012
1013 COUNT = 2
1014 TIME = 59
1015 TOTP = 90693936
1016 MODE = SHA512
Ayrxefc68382014-02-10 00:01:05 +08001017 SECRET = 12345678901234567890
Ayrx4300f6c2014-02-09 15:15:13 +08001018 """).splitlines()
1019
1020 assert load_nist_vectors(vector_data) == [
1021 {
1022 "time": b"59",
1023 "totp": b"94287082",
1024 "mode": b"SHA1",
Ayrxefc68382014-02-10 00:01:05 +08001025 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +08001026 },
1027 {
1028 "time": b"59",
1029 "totp": b"46119246",
1030 "mode": b"SHA256",
Ayrxefc68382014-02-10 00:01:05 +08001031 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +08001032 },
1033 {
1034 "time": b"59",
1035 "totp": b"90693936",
1036 "mode": b"SHA512",
Ayrxefc68382014-02-10 00:01:05 +08001037 "secret": b"12345678901234567890",
Ayrx4300f6c2014-02-09 15:15:13 +08001038 },
1039 ]