Debian Codegraph @

lib/Crypto/SelfTest/PublicKey/test_ECC_NIST.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2015, Legrandin <[email protected]>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 # ===================================================================
30
31 import unittest
32 from binascii import unhexlify
33
34 from Crypto.SelfTest.st_common import list_test_cases
35 from Crypto.SelfTest.loader import load_test_vectors
36
37 from Crypto.PublicKey import ECC
38 from Crypto.PublicKey.ECC import EccPoint, _curves, EccKey
39
40 from Crypto.Math.Numbers import Integer
41
42
43 class TestEccPoint(unittest.TestCase):
44
45 def test_mix(self):
46
47 p1 = ECC.generate(curve='P-256').pointQ
48 p2 = ECC.generate(curve='P-384').pointQ
49
50 try:
51 p1 + p2
52 assert(False)
53 except ValueError as e:
54 assert "not on the same curve" in str(e)
55
56 try:
57 p1 += p2
58 assert(False)
59 except ValueError as e:
60 assert "not on the same curve" in str(e)
61
62 class OtherKeyType:
63 pass
64
65 self.assertFalse(p1 == OtherKeyType())
66 self.assertTrue(p1 != OtherKeyType())
67
68 def test_repr(self):
69 p1 = ECC.construct(curve='P-256',
70 d=75467964919405407085864614198393977741148485328036093939970922195112333446269,
71 point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381,
72 point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389)
73 self.assertEqual(repr(p1), "EccKey(curve='NIST P-256', point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381, point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389, d=75467964919405407085864614198393977741148485328036093939970922195112333446269)")
74
75
76 class TestEccPoint_NIST_P192(unittest.TestCase):
77 """Tests defined in section 4.1 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
78
79 pointS = EccPoint(
80 0xd458e7d127ae671b0c330266d246769353a012073e97acf8,
81 0x325930500d851f336bddc050cf7fb11b5673a1645086df3b,
82 curve='p192')
83
84 pointT = EccPoint(
85 0xf22c4395213e9ebe67ddecdd87fdbd01be16fb059b9753a4,
86 0x264424096af2b3597796db48f8dfb41fa9cecc97691a9c79,
87 curve='p192')
88
89 def test_set(self):
90 pointW = EccPoint(0, 0)
91 pointW.set(self.pointS)
92 self.assertEqual(pointW, self.pointS)
93
94 def test_copy(self):
95 pointW = self.pointS.copy()
96 self.assertEqual(pointW, self.pointS)
97 pointW.set(self.pointT)
98 self.assertEqual(pointW, self.pointT)
99 self.assertNotEqual(self.pointS, self.pointT)
100
101 def test_negate(self):
102 negS = -self.pointS
103 sum = self.pointS + negS
104 self.assertEqual(sum, self.pointS.point_at_infinity())
105
106 def test_addition(self):
107 pointRx = 0x48e1e4096b9b8e5ca9d0f1f077b8abf58e843894de4d0290
108 pointRy = 0x408fa77c797cd7dbfb16aa48a3648d3d63c94117d7b6aa4b
109
110 pointR = self.pointS + self.pointT
111 self.assertEqual(pointR.x, pointRx)
112 self.assertEqual(pointR.y, pointRy)
113
114 pai = pointR.point_at_infinity()
115
116 # S + 0
117 pointR = self.pointS + pai
118 self.assertEqual(pointR, self.pointS)
119
120 # 0 + S
121 pointR = pai + self.pointS
122 self.assertEqual(pointR, self.pointS)
123
124 # 0 + 0
125 pointR = pai + pai
126 self.assertEqual(pointR, pai)
127
128 def test_inplace_addition(self):
129 pointRx = 0x48e1e4096b9b8e5ca9d0f1f077b8abf58e843894de4d0290
130 pointRy = 0x408fa77c797cd7dbfb16aa48a3648d3d63c94117d7b6aa4b
131
132 pointR = self.pointS.copy()
133 pointR += self.pointT
134 self.assertEqual(pointR.x, pointRx)
135 self.assertEqual(pointR.y, pointRy)
136
137 pai = pointR.point_at_infinity()
138
139 # S + 0
140 pointR = self.pointS.copy()
141 pointR += pai
142 self.assertEqual(pointR, self.pointS)
143
144 # 0 + S
145 pointR = pai.copy()
146 pointR += self.pointS
147 self.assertEqual(pointR, self.pointS)
148
149 # 0 + 0
150 pointR = pai.copy()
151 pointR += pai
152 self.assertEqual(pointR, pai)
153
154 def test_doubling(self):
155 pointRx = 0x30c5bc6b8c7da25354b373dc14dd8a0eba42d25a3f6e6962
156 pointRy = 0x0dde14bc4249a721c407aedbf011e2ddbbcb2968c9d889cf
157
158 pointR = self.pointS.copy()
159 pointR.double()
160 self.assertEqual(pointR.x, pointRx)
161 self.assertEqual(pointR.y, pointRy)
162
163 # 2*0
164 pai = self.pointS.point_at_infinity()
165 pointR = pai.copy()
166 pointR.double()
167 self.assertEqual(pointR, pai)
168
169 # S + S
170 pointR = self.pointS.copy()
171 pointR += pointR
172 self.assertEqual(pointR.x, pointRx)
173 self.assertEqual(pointR.y, pointRy)
174
175 def test_scalar_multiply(self):
176 d = 0xa78a236d60baec0c5dd41b33a542463a8255391af64c74ee
177 pointRx = 0x1faee4205a4f669d2d0a8f25e3bcec9a62a6952965bf6d31
178 pointRy = 0x5ff2cdfa508a2581892367087c696f179e7a4d7e8260fb06
179
180 pointR = self.pointS * d
181 self.assertEqual(pointR.x, pointRx)
182 self.assertEqual(pointR.y, pointRy)
183
184 # 0*S
185 pai = self.pointS.point_at_infinity()
186 pointR = self.pointS * 0
187 self.assertEqual(pointR, pai)
188
189 # -1*S
190 self.assertRaises(ValueError, lambda: self.pointS * -1)
191
192 # Reverse order
193 pointR = d * self.pointS
194 self.assertEqual(pointR.x, pointRx)
195 self.assertEqual(pointR.y, pointRy)
196
197 pointR = Integer(d) * self.pointS
198 self.assertEqual(pointR.x, pointRx)
199 self.assertEqual(pointR.y, pointRy)
200
201 def test_joint_scalar_multiply(self):
202 d = 0xa78a236d60baec0c5dd41b33a542463a8255391af64c74ee
203 e = 0xc4be3d53ec3089e71e4de8ceab7cce889bc393cd85b972bc
204 pointRx = 0x019f64eed8fa9b72b7dfea82c17c9bfa60ecb9e1778b5bde
205 pointRy = 0x16590c5fcd8655fa4ced33fb800e2a7e3c61f35d83503644
206
207 pointR = self.pointS * d + self.pointT * e
208 self.assertEqual(pointR.x, pointRx)
209 self.assertEqual(pointR.y, pointRy)
210
211 def test_sizes(self):
212 self.assertEqual(self.pointS.size_in_bits(), 192)
213 self.assertEqual(self.pointS.size_in_bytes(), 24)
214
215
216 class TestEccPoint_NIST_P224(unittest.TestCase):
217 """Tests defined in section 4.2 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
218
219 pointS = EccPoint(
220 0x6eca814ba59a930843dc814edd6c97da95518df3c6fdf16e9a10bb5b,
221 0xef4b497f0963bc8b6aec0ca0f259b89cd80994147e05dc6b64d7bf22,
222 curve='p224')
223
224 pointT = EccPoint(
225 0xb72b25aea5cb03fb88d7e842002969648e6ef23c5d39ac903826bd6d,
226 0xc42a8a4d34984f0b71b5b4091af7dceb33ea729c1a2dc8b434f10c34,
227 curve='p224')
228
229 def test_set(self):
230 pointW = EccPoint(0, 0)
231 pointW.set(self.pointS)
232 self.assertEqual(pointW, self.pointS)
233
234 def test_copy(self):
235 pointW = self.pointS.copy()
236 self.assertEqual(pointW, self.pointS)
237 pointW.set(self.pointT)
238 self.assertEqual(pointW, self.pointT)
239 self.assertNotEqual(self.pointS, self.pointT)
240
241 def test_negate(self):
242 negS = -self.pointS
243 sum = self.pointS + negS
244 self.assertEqual(sum, self.pointS.point_at_infinity())
245
246 def test_addition(self):
247 pointRx = 0x236f26d9e84c2f7d776b107bd478ee0a6d2bcfcaa2162afae8d2fd15
248 pointRy = 0xe53cc0a7904ce6c3746f6a97471297a0b7d5cdf8d536ae25bb0fda70
249
250 pointR = self.pointS + self.pointT
251 self.assertEqual(pointR.x, pointRx)
252 self.assertEqual(pointR.y, pointRy)
253
254 pai = pointR.point_at_infinity()
255
256 # S + 0
257 pointR = self.pointS + pai
258 self.assertEqual(pointR, self.pointS)
259
260 # 0 + S
261 pointR = pai + self.pointS
262 self.assertEqual(pointR, self.pointS)
263
264 # 0 + 0
265 pointR = pai + pai
266 self.assertEqual(pointR, pai)
267
268 def test_inplace_addition(self):
269 pointRx = 0x236f26d9e84c2f7d776b107bd478ee0a6d2bcfcaa2162afae8d2fd15
270 pointRy = 0xe53cc0a7904ce6c3746f6a97471297a0b7d5cdf8d536ae25bb0fda70
271
272 pointR = self.pointS.copy()
273 pointR += self.pointT
274 self.assertEqual(pointR.x, pointRx)
275 self.assertEqual(pointR.y, pointRy)
276
277 pai = pointR.point_at_infinity()
278
279 # S + 0
280 pointR = self.pointS.copy()
281 pointR += pai
282 self.assertEqual(pointR, self.pointS)
283
284 # 0 + S
285 pointR = pai.copy()
286 pointR += self.pointS
287 self.assertEqual(pointR, self.pointS)
288
289 # 0 + 0
290 pointR = pai.copy()
291 pointR += pai
292 self.assertEqual(pointR, pai)
293
294 def test_doubling(self):
295 pointRx = 0xa9c96f2117dee0f27ca56850ebb46efad8ee26852f165e29cb5cdfc7
296 pointRy = 0xadf18c84cf77ced4d76d4930417d9579207840bf49bfbf5837dfdd7d
297
298 pointR = self.pointS.copy()
299 pointR.double()
300 self.assertEqual(pointR.x, pointRx)
301 self.assertEqual(pointR.y, pointRy)
302
303 # 2*0
304 pai = self.pointS.point_at_infinity()
305 pointR = pai.copy()
306 pointR.double()
307 self.assertEqual(pointR, pai)
308
309 # S + S
310 pointR = self.pointS.copy()
311 pointR += pointR
312 self.assertEqual(pointR.x, pointRx)
313 self.assertEqual(pointR.y, pointRy)
314
315 def test_scalar_multiply(self):
316 d = 0xa78ccc30eaca0fcc8e36b2dd6fbb03df06d37f52711e6363aaf1d73b
317 pointRx = 0x96a7625e92a8d72bff1113abdb95777e736a14c6fdaacc392702bca4
318 pointRy = 0x0f8e5702942a3c5e13cd2fd5801915258b43dfadc70d15dbada3ed10
319
320 pointR = self.pointS * d
321 self.assertEqual(pointR.x, pointRx)
322 self.assertEqual(pointR.y, pointRy)
323
324 # 0*S
325 pai = self.pointS.point_at_infinity()
326 pointR = self.pointS * 0
327 self.assertEqual(pointR, pai)
328
329 # -1*S
330 self.assertRaises(ValueError, lambda: self.pointS * -1)
331
332 # Reverse order
333 pointR = d * self.pointS
334 self.assertEqual(pointR.x, pointRx)
335 self.assertEqual(pointR.y, pointRy)
336
337 pointR = Integer(d) * self.pointS
338 self.assertEqual(pointR.x, pointRx)
339 self.assertEqual(pointR.y, pointRy)
340
341 def test_joing_scalar_multiply(self):
342 d = 0xa78ccc30eaca0fcc8e36b2dd6fbb03df06d37f52711e6363aaf1d73b
343 e = 0x54d549ffc08c96592519d73e71e8e0703fc8177fa88aa77a6ed35736
344 pointRx = 0xdbfe2958c7b2cda1302a67ea3ffd94c918c5b350ab838d52e288c83e
345 pointRy = 0x2f521b83ac3b0549ff4895abcc7f0c5a861aacb87acbc5b8147bb18b
346
347 pointR = self.pointS * d + self.pointT * e
348 self.assertEqual(pointR.x, pointRx)
349 self.assertEqual(pointR.y, pointRy)
350
351 def test_sizes(self):
352 self.assertEqual(self.pointS.size_in_bits(), 224)
353 self.assertEqual(self.pointS.size_in_bytes(), 28)
354
355
356 class TestEccPoint_NIST_P256(unittest.TestCase):
357 """Tests defined in section 4.3 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
358
359 pointS = EccPoint(
360 0xde2444bebc8d36e682edd27e0f271508617519b3221a8fa0b77cab3989da97c9,
361 0xc093ae7ff36e5380fc01a5aad1e66659702de80f53cec576b6350b243042a256)
362
363 pointT = EccPoint(
364 0x55a8b00f8da1d44e62f6b3b25316212e39540dc861c89575bb8cf92e35e0986b,
365 0x5421c3209c2d6c704835d82ac4c3dd90f61a8a52598b9e7ab656e9d8c8b24316)
366
367 def test_set(self):
368 pointW = EccPoint(0, 0)
369 pointW.set(self.pointS)
370 self.assertEqual(pointW, self.pointS)
371
372 def test_copy(self):
373 pointW = self.pointS.copy()
374 self.assertEqual(pointW, self.pointS)
375 pointW.set(self.pointT)
376 self.assertEqual(pointW, self.pointT)
377 self.assertNotEqual(self.pointS, self.pointT)
378
379 def test_negate(self):
380 negS = -self.pointS
381 sum = self.pointS + negS
382 self.assertEqual(sum, self.pointS.point_at_infinity())
383
384 def test_addition(self):
385 pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
386 pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
387
388 pointR = self.pointS + self.pointT
389 self.assertEqual(pointR.x, pointRx)
390 self.assertEqual(pointR.y, pointRy)
391
392 pai = pointR.point_at_infinity()
393
394 # S + 0
395 pointR = self.pointS + pai
396 self.assertEqual(pointR, self.pointS)
397
398 # 0 + S
399 pointR = pai + self.pointS
400 self.assertEqual(pointR, self.pointS)
401
402 # 0 + 0
403 pointR = pai + pai
404 self.assertEqual(pointR, pai)
405
406 def test_inplace_addition(self):
407 pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
408 pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
409
410 pointR = self.pointS.copy()
411 pointR += self.pointT
412 self.assertEqual(pointR.x, pointRx)
413 self.assertEqual(pointR.y, pointRy)
414
415 pai = pointR.point_at_infinity()
416
417 # S + 0
418 pointR = self.pointS.copy()
419 pointR += pai
420 self.assertEqual(pointR, self.pointS)
421
422 # 0 + S
423 pointR = pai.copy()
424 pointR += self.pointS
425 self.assertEqual(pointR, self.pointS)
426
427 # 0 + 0
428 pointR = pai.copy()
429 pointR += pai
430 self.assertEqual(pointR, pai)
431
432 def test_doubling(self):
433 pointRx = 0x7669e6901606ee3ba1a8eef1e0024c33df6c22f3b17481b82a860ffcdb6127b0
434 pointRy = 0xfa878162187a54f6c39f6ee0072f33de389ef3eecd03023de10ca2c1db61d0c7
435
436 pointR = self.pointS.copy()
437 pointR.double()
438 self.assertEqual(pointR.x, pointRx)
439 self.assertEqual(pointR.y, pointRy)
440
441 # 2*0
442 pai = self.pointS.point_at_infinity()
443 pointR = pai.copy()
444 pointR.double()
445 self.assertEqual(pointR, pai)
446
447 # S + S
448 pointR = self.pointS.copy()
449 pointR += pointR
450 self.assertEqual(pointR.x, pointRx)
451 self.assertEqual(pointR.y, pointRy)
452
453 def test_scalar_multiply(self):
454 d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
455 pointRx = 0x51d08d5f2d4278882946d88d83c97d11e62becc3cfc18bedacc89ba34eeca03f
456 pointRy = 0x75ee68eb8bf626aa5b673ab51f6e744e06f8fcf8a6c0cf3035beca956a7b41d5
457
458 pointR = self.pointS * d
459 self.assertEqual(pointR.x, pointRx)
460 self.assertEqual(pointR.y, pointRy)
461
462 # 0*S
463 pai = self.pointS.point_at_infinity()
464 pointR = self.pointS * 0
465 self.assertEqual(pointR, pai)
466
467 # -1*S
468 self.assertRaises(ValueError, lambda: self.pointS * -1)
469
470 # Reverse order
471 pointR = d * self.pointS
472 self.assertEqual(pointR.x, pointRx)
473 self.assertEqual(pointR.y, pointRy)
474
475 pointR = Integer(d) * self.pointS
476 self.assertEqual(pointR.x, pointRx)
477 self.assertEqual(pointR.y, pointRy)
478
479 def test_joing_scalar_multiply(self):
480 d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
481 e = 0xd37f628ece72a462f0145cbefe3f0b355ee8332d37acdd83a358016aea029db7
482 pointRx = 0xd867b4679221009234939221b8046245efcf58413daacbeff857b8588341f6b8
483 pointRy = 0xf2504055c03cede12d22720dad69c745106b6607ec7e50dd35d54bd80f615275
484
485 pointR = self.pointS * d + self.pointT * e
486 self.assertEqual(pointR.x, pointRx)
487 self.assertEqual(pointR.y, pointRy)
488
489 def test_sizes(self):
490 self.assertEqual(self.pointS.size_in_bits(), 256)
491 self.assertEqual(self.pointS.size_in_bytes(), 32)
492
493
494 class TestEccPoint_NIST_P384(unittest.TestCase):
495 """Tests defined in section 4.4 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
496
497 pointS = EccPoint(
498 0xfba203b81bbd23f2b3be971cc23997e1ae4d89e69cb6f92385dda82768ada415ebab4167459da98e62b1332d1e73cb0e,
499 0x5ffedbaefdeba603e7923e06cdb5d0c65b22301429293376d5c6944e3fa6259f162b4788de6987fd59aed5e4b5285e45,
500 "p384")
501
502 pointT = EccPoint(
503 0xaacc05202e7fda6fc73d82f0a66220527da8117ee8f8330ead7d20ee6f255f582d8bd38c5a7f2b40bcdb68ba13d81051,
504 0x84009a263fefba7c2c57cffa5db3634d286131afc0fca8d25afa22a7b5dce0d9470da89233cee178592f49b6fecb5092,
505 "p384")
506
507 def test_set(self):
508 pointW = EccPoint(0, 0, "p384")
509 pointW.set(self.pointS)
510 self.assertEqual(pointW, self.pointS)
511
512 def test_copy(self):
513 pointW = self.pointS.copy()
514 self.assertEqual(pointW, self.pointS)
515 pointW.set(self.pointT)
516 self.assertEqual(pointW, self.pointT)
517 self.assertNotEqual(self.pointS, self.pointT)
518
519 def test_negate(self):
520 negS = -self.pointS
521 sum = self.pointS + negS
522 self.assertEqual(sum, self.pointS.point_at_infinity())
523
524 def test_addition(self):
525 pointRx = 0x12dc5ce7acdfc5844d939f40b4df012e68f865b89c3213ba97090a247a2fc009075cf471cd2e85c489979b65ee0b5eed
526 pointRy = 0x167312e58fe0c0afa248f2854e3cddcb557f983b3189b67f21eee01341e7e9fe67f6ee81b36988efa406945c8804a4b0
527
528 pointR = self.pointS + self.pointT
529 self.assertEqual(pointR.x, pointRx)
530 self.assertEqual(pointR.y, pointRy)
531
532 pai = pointR.point_at_infinity()
533
534 # S + 0
535 pointR = self.pointS + pai
536 self.assertEqual(pointR, self.pointS)
537
538 # 0 + S
539 pointR = pai + self.pointS
540 self.assertEqual(pointR, self.pointS)
541
542 # 0 + 0
543 pointR = pai + pai
544 self.assertEqual(pointR, pai)
545
546 def _test_inplace_addition(self):
547 pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
548 pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
549
550 pointR = self.pointS.copy()
551 pointR += self.pointT
552 self.assertEqual(pointR.x, pointRx)
553 self.assertEqual(pointR.y, pointRy)
554
555 pai = pointR.point_at_infinity()
556
557 # S + 0
558 pointR = self.pointS.copy()
559 pointR += pai
560 self.assertEqual(pointR, self.pointS)
561
562 # 0 + S
563 pointR = pai.copy()
564 pointR += self.pointS
565 self.assertEqual(pointR, self.pointS)
566
567 # 0 + 0
568 pointR = pai.copy()
569 pointR += pai
570 self.assertEqual(pointR, pai)
571
572 def test_doubling(self):
573 pointRx = 0x2a2111b1e0aa8b2fc5a1975516bc4d58017ff96b25e1bdff3c229d5fac3bacc319dcbec29f9478f42dee597b4641504c
574 pointRy = 0xfa2e3d9dc84db8954ce8085ef28d7184fddfd1344b4d4797343af9b5f9d837520b450f726443e4114bd4e5bdb2f65ddd
575
576 pointR = self.pointS.copy()
577 pointR.double()
578 self.assertEqual(pointR.x, pointRx)
579 self.assertEqual(pointR.y, pointRy)
580
581 # 2*0
582 pai = self.pointS.point_at_infinity()
583 pointR = pai.copy()
584 pointR.double()
585 self.assertEqual(pointR, pai)
586
587 # S + S
588 pointR = self.pointS.copy()
589 pointR += pointR
590 self.assertEqual(pointR.x, pointRx)
591 self.assertEqual(pointR.y, pointRy)
592
593 def test_scalar_multiply(self):
594 d = 0xa4ebcae5a665983493ab3e626085a24c104311a761b5a8fdac052ed1f111a5c44f76f45659d2d111a61b5fdd97583480
595 pointRx = 0xe4f77e7ffeb7f0958910e3a680d677a477191df166160ff7ef6bb5261f791aa7b45e3e653d151b95dad3d93ca0290ef2
596 pointRy = 0xac7dee41d8c5f4a7d5836960a773cfc1376289d3373f8cf7417b0c6207ac32e913856612fc9ff2e357eb2ee05cf9667f
597
598 pointR = self.pointS * d
599 self.assertEqual(pointR.x, pointRx)
600 self.assertEqual(pointR.y, pointRy)
601
602 # 0*S
603 pai = self.pointS.point_at_infinity()
604 pointR = self.pointS * 0
605 self.assertEqual(pointR, pai)
606
607 # -1*S
608 self.assertRaises(ValueError, lambda: self.pointS * -1)
609
610 def test_joing_scalar_multiply(self):
611 d = 0xa4ebcae5a665983493ab3e626085a24c104311a761b5a8fdac052ed1f111a5c44f76f45659d2d111a61b5fdd97583480
612 e = 0xafcf88119a3a76c87acbd6008e1349b29f4ba9aa0e12ce89bcfcae2180b38d81ab8cf15095301a182afbc6893e75385d
613 pointRx = 0x917ea28bcd641741ae5d18c2f1bd917ba68d34f0f0577387dc81260462aea60e2417b8bdc5d954fc729d211db23a02dc
614 pointRy = 0x1a29f7ce6d074654d77b40888c73e92546c8f16a5ff6bcbd307f758d4aee684beff26f6742f597e2585c86da908f7186
615
616 pointR = self.pointS * d + self.pointT * e
617 self.assertEqual(pointR.x, pointRx)
618 self.assertEqual(pointR.y, pointRy)
619
620 def test_sizes(self):
621 self.assertEqual(self.pointS.size_in_bits(), 384)
622 self.assertEqual(self.pointS.size_in_bytes(), 48)
623
624
625 class TestEccPoint_NIST_P521(unittest.TestCase):
626 """Tests defined in section 4.5 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
627
628 pointS = EccPoint(
629 0x000001d5c693f66c08ed03ad0f031f937443458f601fd098d3d0227b4bf62873af50740b0bb84aa157fc847bcf8dc16a8b2b8bfd8e2d0a7d39af04b089930ef6dad5c1b4,
630 0x00000144b7770963c63a39248865ff36b074151eac33549b224af5c8664c54012b818ed037b2b7c1a63ac89ebaa11e07db89fcee5b556e49764ee3fa66ea7ae61ac01823,
631 "p521")
632
633 pointT = EccPoint(
634 0x000000f411f2ac2eb971a267b80297ba67c322dba4bb21cec8b70073bf88fc1ca5fde3ba09e5df6d39acb2c0762c03d7bc224a3e197feaf760d6324006fe3be9a548c7d5,
635 0x000001fdf842769c707c93c630df6d02eff399a06f1b36fb9684f0b373ed064889629abb92b1ae328fdb45534268384943f0e9222afe03259b32274d35d1b9584c65e305,
636 "p521")
637
638 def test_set(self):
639 pointW = EccPoint(0, 0)
640 pointW.set(self.pointS)
641 self.assertEqual(pointW, self.pointS)
642
643 def test_copy(self):
644 pointW = self.pointS.copy()
645 self.assertEqual(pointW, self.pointS)
646 pointW.set(self.pointT)
647 self.assertEqual(pointW, self.pointT)
648 self.assertNotEqual(self.pointS, self.pointT)
649
650 def test_negate(self):
651 negS = -self.pointS
652 sum = self.pointS + negS
653 self.assertEqual(sum, self.pointS.point_at_infinity())
654
655 def test_addition(self):
656 pointRx = 0x000001264ae115ba9cbc2ee56e6f0059e24b52c8046321602c59a339cfb757c89a59c358a9a8e1f86d384b3f3b255ea3f73670c6dc9f45d46b6a196dc37bbe0f6b2dd9e9
657 pointRy = 0x00000062a9c72b8f9f88a271690bfa017a6466c31b9cadc2fc544744aeb817072349cfddc5ad0e81b03f1897bd9c8c6efbdf68237dc3bb00445979fb373b20c9a967ac55
658
659 pointR = self.pointS + self.pointT
660 self.assertEqual(pointR.x, pointRx)
661 self.assertEqual(pointR.y, pointRy)
662
663 pai = pointR.point_at_infinity()
664
665 # S + 0
666 pointR = self.pointS + pai
667 self.assertEqual(pointR, self.pointS)
668
669 # 0 + S
670 pointR = pai + self.pointS
671 self.assertEqual(pointR, self.pointS)
672
673 # 0 + 0
674 pointR = pai + pai
675 self.assertEqual(pointR, pai)
676
677 def test_inplace_addition(self):
678 pointRx = 0x000001264ae115ba9cbc2ee56e6f0059e24b52c8046321602c59a339cfb757c89a59c358a9a8e1f86d384b3f3b255ea3f73670c6dc9f45d46b6a196dc37bbe0f6b2dd9e9
679 pointRy = 0x00000062a9c72b8f9f88a271690bfa017a6466c31b9cadc2fc544744aeb817072349cfddc5ad0e81b03f1897bd9c8c6efbdf68237dc3bb00445979fb373b20c9a967ac55
680
681 pointR = self.pointS.copy()
682 pointR += self.pointT
683 self.assertEqual(pointR.x, pointRx)
684 self.assertEqual(pointR.y, pointRy)
685
686 pai = pointR.point_at_infinity()
687
688 # S + 0
689 pointR = self.pointS.copy()
690 pointR += pai
691 self.assertEqual(pointR, self.pointS)
692
693 # 0 + S
694 pointR = pai.copy()
695 pointR += self.pointS
696 self.assertEqual(pointR, self.pointS)
697
698 # 0 + 0
699 pointR = pai.copy()
700 pointR += pai
701 self.assertEqual(pointR, pai)
702
703 def test_doubling(self):
704 pointRx = 0x0000012879442f2450c119e7119a5f738be1f1eba9e9d7c6cf41b325d9ce6d643106e9d61124a91a96bcf201305a9dee55fa79136dc700831e54c3ca4ff2646bd3c36bc6
705 pointRy = 0x0000019864a8b8855c2479cbefe375ae553e2393271ed36fadfc4494fc0583f6bd03598896f39854abeae5f9a6515a021e2c0eef139e71de610143f53382f4104dccb543
706
707 pointR = self.pointS.copy()
708 pointR.double()
709 self.assertEqual(pointR.x, pointRx)
710 self.assertEqual(pointR.y, pointRy)
711
712 # 2*0
713 pai = self.pointS.point_at_infinity()
714 pointR = pai.copy()
715 pointR.double()
716 self.assertEqual(pointR, pai)
717
718 # S + S
719 pointR = self.pointS.copy()
720 pointR += pointR
721 self.assertEqual(pointR.x, pointRx)
722 self.assertEqual(pointR.y, pointRy)
723
724 def test_scalar_multiply(self):
725 d = 0x000001eb7f81785c9629f136a7e8f8c674957109735554111a2a866fa5a166699419bfa9936c78b62653964df0d6da940a695c7294d41b2d6600de6dfcf0edcfc89fdcb1
726 pointRx = 0x00000091b15d09d0ca0353f8f96b93cdb13497b0a4bb582ae9ebefa35eee61bf7b7d041b8ec34c6c00c0c0671c4ae063318fb75be87af4fe859608c95f0ab4774f8c95bb
727 pointRy = 0x00000130f8f8b5e1abb4dd94f6baaf654a2d5810411e77b7423965e0c7fd79ec1ae563c207bd255ee9828eb7a03fed565240d2cc80ddd2cecbb2eb50f0951f75ad87977f
728
729 pointR = self.pointS * d
730 self.assertEqual(pointR.x, pointRx)
731 self.assertEqual(pointR.y, pointRy)
732
733 # 0*S
734 pai = self.pointS.point_at_infinity()
735 pointR = self.pointS * 0
736 self.assertEqual(pointR, pai)
737
738 # -1*S
739 self.assertRaises(ValueError, lambda: self.pointS * -1)
740
741 def test_joing_scalar_multiply(self):
742 d = 0x000001eb7f81785c9629f136a7e8f8c674957109735554111a2a866fa5a166699419bfa9936c78b62653964df0d6da940a695c7294d41b2d6600de6dfcf0edcfc89fdcb1
743 e = 0x00000137e6b73d38f153c3a7575615812608f2bab3229c92e21c0d1c83cfad9261dbb17bb77a63682000031b9122c2f0cdab2af72314be95254de4291a8f85f7c70412e3
744 pointRx = 0x0000009d3802642b3bea152beb9e05fba247790f7fc168072d363340133402f2585588dc1385d40ebcb8552f8db02b23d687cae46185b27528adb1bf9729716e4eba653d
745 pointRy = 0x0000000fe44344e79da6f49d87c1063744e5957d9ac0a505bafa8281c9ce9ff25ad53f8da084a2deb0923e46501de5797850c61b229023dd9cf7fc7f04cd35ebb026d89d
746
747 pointR = self.pointS * d
748 pointR += self.pointT * e
749 self.assertEqual(pointR.x, pointRx)
750 self.assertEqual(pointR.y, pointRy)
751
752 def test_sizes(self):
753 self.assertEqual(self.pointS.size_in_bits(), 521)
754 self.assertEqual(self.pointS.size_in_bytes(), 66)
755
756
757 class TestEccPoint_PAI_P192(unittest.TestCase):
758 """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
759
760 curve = _curves['p192']
761 pointG = EccPoint(curve.Gx, curve.Gy, "p192")
762
763
764 tv_pai = load_test_vectors(("PublicKey", "ECC"),
765 "point-at-infinity.org-P192.txt",
766 "P-192 tests from point-at-infinity.org",
767 {"k": lambda k: int(k),
768 "x": lambda x: int(x, 16),
769 "y": lambda y: int(y, 16)}) or []
770 for tv in tv_pai:
771 def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
772 result = self.pointG * scalar
773 self.assertEqual(result.x, x)
774 self.assertEqual(result.y, y)
775 setattr(TestEccPoint_PAI_P192, "test_%d" % tv.count, new_test)
776
777
778 class TestEccPoint_PAI_P224(unittest.TestCase):
779 """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
780
781 curve = _curves['p224']
782 pointG = EccPoint(curve.Gx, curve.Gy, "p224")
783
784
785 tv_pai = load_test_vectors(("PublicKey", "ECC"),
786 "point-at-infinity.org-P224.txt",
787 "P-224 tests from point-at-infinity.org",
788 {"k": lambda k: int(k),
789 "x": lambda x: int(x, 16),
790 "y": lambda y: int(y, 16)}) or []
791 for tv in tv_pai:
792 def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
793 result = self.pointG * scalar
794 self.assertEqual(result.x, x)
795 self.assertEqual(result.y, y)
796 setattr(TestEccPoint_PAI_P224, "test_%d" % tv.count, new_test)
797
798
799 class TestEccPoint_PAI_P256(unittest.TestCase):
800 """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
801
802 curve = _curves['p256']
803 pointG = EccPoint(curve.Gx, curve.Gy, "p256")
804
805
806 tv_pai = load_test_vectors(("PublicKey", "ECC"),
807 "point-at-infinity.org-P256.txt",
808 "P-256 tests from point-at-infinity.org",
809 {"k": lambda k: int(k),
810 "x": lambda x: int(x, 16),
811 "y": lambda y: int(y, 16)}) or []
812 for tv in tv_pai:
813 def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
814 result = self.pointG * scalar
815 self.assertEqual(result.x, x)
816 self.assertEqual(result.y, y)
817 setattr(TestEccPoint_PAI_P256, "test_%d" % tv.count, new_test)
818
819
820 class TestEccPoint_PAI_P384(unittest.TestCase):
821 """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
822
823 curve = _curves['p384']
824 pointG = EccPoint(curve.Gx, curve.Gy, "p384")
825
826
827 tv_pai = load_test_vectors(("PublicKey", "ECC"),
828 "point-at-infinity.org-P384.txt",
829 "P-384 tests from point-at-infinity.org",
830 {"k": lambda k: int(k),
831 "x": lambda x: int(x, 16),
832 "y": lambda y: int(y, 16)}) or []
833 for tv in tv_pai:
834 def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
835 result = self.pointG * scalar
836 self.assertEqual(result.x, x)
837 self.assertEqual(result.y, y)
838 setattr(TestEccPoint_PAI_P384, "test_%d" % tv.count, new_test)
839
840
841 class TestEccPoint_PAI_P521(unittest.TestCase):
842 """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
843
844 curve = _curves['p521']
845 pointG = EccPoint(curve.Gx, curve.Gy, "p521")
846
847
848 tv_pai = load_test_vectors(("PublicKey", "ECC"),
849 "point-at-infinity.org-P521.txt",
850 "P-521 tests from point-at-infinity.org",
851 {"k": lambda k: int(k),
852 "x": lambda x: int(x, 16),
853 "y": lambda y: int(y, 16)}) or []
854 for tv in tv_pai:
855 def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
856 result = self.pointG * scalar
857 self.assertEqual(result.x, x)
858 self.assertEqual(result.y, y)
859 setattr(TestEccPoint_PAI_P521, "test_%d" % tv.count, new_test)
860
861
862 class TestEccKey_P192(unittest.TestCase):
863
864 def test_private_key(self):
865
866 key = EccKey(curve="P-192", d=1)
867 self.assertEqual(key.d, 1)
868 self.assertTrue(key.has_private())
869 self.assertEqual(key.pointQ.x, _curves['p192'].Gx)
870 self.assertEqual(key.pointQ.y, _curves['p192'].Gy)
871
872 point = EccPoint(_curves['p192'].Gx, _curves['p192'].Gy, curve='P-192')
873 key = EccKey(curve="P-192", d=1, point=point)
874 self.assertEqual(key.d, 1)
875 self.assertTrue(key.has_private())
876 self.assertEqual(key.pointQ, point)
877
878 # Other names
879 key = EccKey(curve="secp192r1", d=1)
880 key = EccKey(curve="prime192v1", d=1)
881
882 def test_public_key(self):
883
884 point = EccPoint(_curves['p192'].Gx, _curves['p192'].Gy, curve='P-192')
885 key = EccKey(curve="P-192", point=point)
886 self.assertFalse(key.has_private())
887 self.assertEqual(key.pointQ, point)
888
889 def test_public_key_derived(self):
890
891 priv_key = EccKey(curve="P-192", d=3)
892 pub_key = priv_key.public_key()
893 self.assertFalse(pub_key.has_private())
894 self.assertEqual(priv_key.pointQ, pub_key.pointQ)
895
896 def test_invalid_curve(self):
897 self.assertRaises(ValueError, lambda: EccKey(curve="P-193", d=1))
898
899 def test_invalid_d(self):
900 self.assertRaises(ValueError, lambda: EccKey(curve="P-192", d=0))
901 self.assertRaises(ValueError, lambda: EccKey(curve="P-192",
902 d=_curves['p192'].order))
903
904 def test_equality(self):
905
906 private_key = ECC.construct(d=3, curve="P-192")
907 private_key2 = ECC.construct(d=3, curve="P-192")
908 private_key3 = ECC.construct(d=4, curve="P-192")
909
910 public_key = private_key.public_key()
911 public_key2 = private_key2.public_key()
912 public_key3 = private_key3.public_key()
913
914 self.assertEqual(private_key, private_key2)
915 self.assertNotEqual(private_key, private_key3)
916
917 self.assertEqual(public_key, public_key2)
918 self.assertNotEqual(public_key, public_key3)
919
920 self.assertNotEqual(public_key, private_key)
921
922 def test_name_consistency(self):
923 key = ECC.generate(curve='p192')
924 self.assertIn("curve='NIST P-192'", repr(key))
925 self.assertEqual(key.curve, 'NIST P-192')
926 self.assertEqual(key.public_key().curve, 'NIST P-192')
927
928
929 class TestEccKey_P224(unittest.TestCase):
930
931 def test_private_key(self):
932
933 key = EccKey(curve="P-224", d=1)
934 self.assertEqual(key.d, 1)
935 self.assertTrue(key.has_private())
936 self.assertEqual(key.pointQ.x, _curves['p224'].Gx)
937 self.assertEqual(key.pointQ.y, _curves['p224'].Gy)
938
939 point = EccPoint(_curves['p224'].Gx, _curves['p224'].Gy, curve='P-224')
940 key = EccKey(curve="P-224", d=1, point=point)
941 self.assertEqual(key.d, 1)
942 self.assertTrue(key.has_private())
943 self.assertEqual(key.pointQ, point)
944
945 # Other names
946 key = EccKey(curve="secp224r1", d=1)
947 key = EccKey(curve="prime224v1", d=1)
948
949 def test_public_key(self):
950
951 point = EccPoint(_curves['p224'].Gx, _curves['p224'].Gy, curve='P-224')
952 key = EccKey(curve="P-224", point=point)
953 self.assertFalse(key.has_private())
954 self.assertEqual(key.pointQ, point)
955
956 def test_public_key_derived(self):
957
958 priv_key = EccKey(curve="P-224", d=3)
959 pub_key = priv_key.public_key()
960 self.assertFalse(pub_key.has_private())
961 self.assertEqual(priv_key.pointQ, pub_key.pointQ)
962
963 def test_invalid_curve(self):
964 self.assertRaises(ValueError, lambda: EccKey(curve="P-225", d=1))
965
966 def test_invalid_d(self):
967 self.assertRaises(ValueError, lambda: EccKey(curve="P-224", d=0))
968 self.assertRaises(ValueError, lambda: EccKey(curve="P-224",
969 d=_curves['p224'].order))
970
971 def test_equality(self):
972
973 private_key = ECC.construct(d=3, curve="P-224")
974 private_key2 = ECC.construct(d=3, curve="P-224")
975 private_key3 = ECC.construct(d=4, curve="P-224")
976
977 public_key = private_key.public_key()
978 public_key2 = private_key2.public_key()
979 public_key3 = private_key3.public_key()
980
981 self.assertEqual(private_key, private_key2)
982 self.assertNotEqual(private_key, private_key3)
983
984 self.assertEqual(public_key, public_key2)
985 self.assertNotEqual(public_key, public_key3)
986
987 self.assertNotEqual(public_key, private_key)
988
989 def test_name_consistency(self):
990 key = ECC.generate(curve='p224')
991 self.assertIn("curve='NIST P-224'", repr(key))
992 self.assertEqual(key.curve, 'NIST P-224')
993 self.assertEqual(key.public_key().curve, 'NIST P-224')
994
995
996 class TestEccKey_P256(unittest.TestCase):
997
998 def test_private_key(self):
999
1000 key = EccKey(curve="P-256", d=1)
1001 self.assertEqual(key.d, 1)
1002 self.assertTrue(key.has_private())
1003 self.assertEqual(key.pointQ.x, _curves['p256'].Gx)
1004 self.assertEqual(key.pointQ.y, _curves['p256'].Gy)
1005
1006 point = EccPoint(_curves['p256'].Gx, _curves['p256'].Gy)
1007 key = EccKey(curve="P-256", d=1, point=point)
1008 self.assertEqual(key.d, 1)
1009 self.assertTrue(key.has_private())
1010 self.assertEqual(key.pointQ, point)
1011
1012 # Other names
1013 key = EccKey(curve="secp256r1", d=1)
1014 key = EccKey(curve="prime256v1", d=1)
1015
1016 # Must not accept d parameter
1017 self.assertRaises(ValueError, EccKey, curve="p256", seed=b'H'*32)
1018
1019 def test_public_key(self):
1020
1021 point = EccPoint(_curves['p256'].Gx, _curves['p256'].Gy)
1022 key = EccKey(curve="P-256", point=point)
1023 self.assertFalse(key.has_private())
1024 self.assertEqual(key.pointQ, point)
1025
1026 def test_public_key_derived(self):
1027
1028 priv_key = EccKey(curve="P-256", d=3)
1029 pub_key = priv_key.public_key()
1030 self.assertFalse(pub_key.has_private())
1031 self.assertEqual(priv_key.pointQ, pub_key.pointQ)
1032
1033 def test_invalid_curve(self):
1034 self.assertRaises(ValueError, lambda: EccKey(curve="P-257", d=1))
1035
1036 def test_invalid_d(self):
1037 self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=0))
1038 self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=_curves['p256'].order))
1039
1040 def test_equality(self):
1041
1042 private_key = ECC.construct(d=3, curve="P-256")
1043 private_key2 = ECC.construct(d=3, curve="P-256")
1044 private_key3 = ECC.construct(d=4, curve="P-256")
1045
1046 public_key = private_key.public_key()
1047 public_key2 = private_key2.public_key()
1048 public_key3 = private_key3.public_key()
1049
1050 self.assertEqual(private_key, private_key2)
1051 self.assertNotEqual(private_key, private_key3)
1052
1053 self.assertEqual(public_key, public_key2)
1054 self.assertNotEqual(public_key, public_key3)
1055
1056 self.assertNotEqual(public_key, private_key)
1057
1058 def test_name_consistency(self):
1059 key = ECC.generate(curve='p256')
1060 self.assertIn("curve='NIST P-256'", repr(key))
1061 self.assertEqual(key.curve, 'NIST P-256')
1062 self.assertEqual(key.public_key().curve, 'NIST P-256')
1063
1064
1065 class TestEccKey_P384(unittest.TestCase):
1066
1067 def test_private_key(self):
1068
1069 p384 = _curves['p384']
1070
1071 key = EccKey(curve="P-384", d=1)
1072 self.assertEqual(key.d, 1)
1073 self.assertTrue(key.has_private())
1074 self.assertEqual(key.pointQ.x, p384.Gx)
1075 self.assertEqual(key.pointQ.y, p384.Gy)
1076
1077 point = EccPoint(p384.Gx, p384.Gy, "p384")
1078 key = EccKey(curve="P-384", d=1, point=point)
1079 self.assertEqual(key.d, 1)
1080 self.assertTrue(key.has_private())
1081 self.assertEqual(key.pointQ, point)
1082
1083 # Other names
1084 key = EccKey(curve="p384", d=1)
1085 key = EccKey(curve="secp384r1", d=1)
1086 key = EccKey(curve="prime384v1", d=1)
1087
1088 def test_public_key(self):
1089
1090 p384 = _curves['p384']
1091 point = EccPoint(p384.Gx, p384.Gy, 'p384')
1092 key = EccKey(curve="P-384", point=point)
1093 self.assertFalse(key.has_private())
1094 self.assertEqual(key.pointQ, point)
1095
1096 def test_public_key_derived(self):
1097
1098 priv_key = EccKey(curve="P-384", d=3)
1099 pub_key = priv_key.public_key()
1100 self.assertFalse(pub_key.has_private())
1101 self.assertEqual(priv_key.pointQ, pub_key.pointQ)
1102
1103 def test_invalid_curve(self):
1104 self.assertRaises(ValueError, lambda: EccKey(curve="P-385", d=1))
1105
1106 def test_invalid_d(self):
1107 self.assertRaises(ValueError, lambda: EccKey(curve="P-384", d=0))
1108 self.assertRaises(ValueError, lambda: EccKey(curve="P-384",
1109 d=_curves['p384'].order))
1110
1111 def test_equality(self):
1112
1113 private_key = ECC.construct(d=3, curve="P-384")
1114 private_key2 = ECC.construct(d=3, curve="P-384")
1115 private_key3 = ECC.construct(d=4, curve="P-384")
1116
1117 public_key = private_key.public_key()
1118 public_key2 = private_key2.public_key()
1119 public_key3 = private_key3.public_key()
1120
1121 self.assertEqual(private_key, private_key2)
1122 self.assertNotEqual(private_key, private_key3)
1123
1124 self.assertEqual(public_key, public_key2)
1125 self.assertNotEqual(public_key, public_key3)
1126
1127 self.assertNotEqual(public_key, private_key)
1128
1129 def test_name_consistency(self):
1130 key = ECC.generate(curve='p384')
1131 self.assertIn("curve='NIST P-384'", repr(key))
1132 self.assertEqual(key.curve, 'NIST P-384')
1133 self.assertEqual(key.public_key().curve, 'NIST P-384')
1134
1135
1136 class TestEccKey_P521(unittest.TestCase):
1137
1138 def test_private_key(self):
1139
1140 p521 = _curves['p521']
1141
1142 key = EccKey(curve="P-521", d=1)
1143 self.assertEqual(key.d, 1)
1144 self.assertTrue(key.has_private())
1145 self.assertEqual(key.pointQ.x, p521.Gx)
1146 self.assertEqual(key.pointQ.y, p521.Gy)
1147
1148 point = EccPoint(p521.Gx, p521.Gy, "p521")
1149 key = EccKey(curve="P-521", d=1, point=point)
1150 self.assertEqual(key.d, 1)
1151 self.assertTrue(key.has_private())
1152 self.assertEqual(key.pointQ, point)
1153
1154 # Other names
1155 key = EccKey(curve="p521", d=1)
1156 key = EccKey(curve="secp521r1", d=1)
1157 key = EccKey(curve="prime521v1", d=1)
1158
1159 def test_public_key(self):
1160
1161 p521 = _curves['p521']
1162 point = EccPoint(p521.Gx, p521.Gy, 'p521')
1163 key = EccKey(curve="P-384", point=point)
1164 self.assertFalse(key.has_private())
1165 self.assertEqual(key.pointQ, point)
1166
1167 def test_public_key_derived(self):
1168
1169 priv_key = EccKey(curve="P-521", d=3)
1170 pub_key = priv_key.public_key()
1171 self.assertFalse(pub_key.has_private())
1172 self.assertEqual(priv_key.pointQ, pub_key.pointQ)
1173
1174 def test_invalid_curve(self):
1175 self.assertRaises(ValueError, lambda: EccKey(curve="P-522", d=1))
1176
1177 def test_invalid_d(self):
1178 self.assertRaises(ValueError, lambda: EccKey(curve="P-521", d=0))
1179 self.assertRaises(ValueError, lambda: EccKey(curve="P-521",
1180 d=_curves['p521'].order))
1181
1182 def test_equality(self):
1183
1184 private_key = ECC.construct(d=3, curve="P-521")
1185 private_key2 = ECC.construct(d=3, curve="P-521")
1186 private_key3 = ECC.construct(d=4, curve="P-521")
1187
1188 public_key = private_key.public_key()
1189 public_key2 = private_key2.public_key()
1190 public_key3 = private_key3.public_key()
1191
1192 self.assertEqual(private_key, private_key2)
1193 self.assertNotEqual(private_key, private_key3)
1194
1195 self.assertEqual(public_key, public_key2)
1196 self.assertNotEqual(public_key, public_key3)
1197
1198 self.assertNotEqual(public_key, private_key)
1199
1200 def test_name_consistency(self):
1201 key = ECC.generate(curve='p521')
1202 self.assertIn("curve='NIST P-521'", repr(key))
1203 self.assertEqual(key.curve, 'NIST P-521')
1204 self.assertEqual(key.public_key().curve, 'NIST P-521')
1205
1206
1207 class TestEccModule_P192(unittest.TestCase):
1208
1209 def test_generate(self):
1210
1211 key = ECC.generate(curve="P-192")
1212 self.assertTrue(key.has_private())
1213 self.assertEqual(key.pointQ, EccPoint(_curves['p192'].Gx,
1214 _curves['p192'].Gy,
1215 "P-192") * key.d,
1216 "p192")
1217
1218 # Other names
1219 ECC.generate(curve="secp192r1")
1220 ECC.generate(curve="prime192v1")
1221
1222 def test_construct(self):
1223
1224 key = ECC.construct(curve="P-192", d=1)
1225 self.assertTrue(key.has_private())
1226 self.assertEqual(key.pointQ, _curves['p192'].G)
1227
1228 key = ECC.construct(curve="P-192", point_x=_curves['p192'].Gx,
1229 point_y=_curves['p192'].Gy)
1230 self.assertFalse(key.has_private())
1231 self.assertEqual(key.pointQ, _curves['p192'].G)
1232
1233 # Other names
1234 ECC.construct(curve="p192", d=1)
1235 ECC.construct(curve="secp192r1", d=1)
1236 ECC.construct(curve="prime192v1", d=1)
1237
1238 def test_negative_construct(self):
1239 coord = dict(point_x=10, point_y=4)
1240 coordG = dict(point_x=_curves['p192'].Gx, point_y=_curves['p192'].Gy)
1241
1242 self.assertRaises(ValueError, ECC.construct, curve="P-192", **coord)
1243 self.assertRaises(ValueError, ECC.construct, curve="P-192", d=2, **coordG)
1244
1245
1246 class TestEccModule_P224(unittest.TestCase):
1247
1248 def test_generate(self):
1249
1250 key = ECC.generate(curve="P-224")
1251 self.assertTrue(key.has_private())
1252 self.assertEqual(key.pointQ, EccPoint(_curves['p224'].Gx,
1253 _curves['p224'].Gy,
1254 "P-224") * key.d,
1255 "p224")
1256
1257 # Other names
1258 ECC.generate(curve="secp224r1")
1259 ECC.generate(curve="prime224v1")
1260
1261 def test_construct(self):
1262
1263 key = ECC.construct(curve="P-224", d=1)
1264 self.assertTrue(key.has_private())
1265 self.assertEqual(key.pointQ, _curves['p224'].G)
1266
1267 key = ECC.construct(curve="P-224", point_x=_curves['p224'].Gx,
1268 point_y=_curves['p224'].Gy)
1269 self.assertFalse(key.has_private())
1270 self.assertEqual(key.pointQ, _curves['p224'].G)
1271
1272 # Other names
1273 ECC.construct(curve="p224", d=1)
1274 ECC.construct(curve="secp224r1", d=1)
1275 ECC.construct(curve="prime224v1", d=1)
1276
1277 def test_negative_construct(self):
1278 coord = dict(point_x=10, point_y=4)
1279 coordG = dict(point_x=_curves['p224'].Gx, point_y=_curves['p224'].Gy)
1280
1281 self.assertRaises(ValueError, ECC.construct, curve="P-224", **coord)
1282 self.assertRaises(ValueError, ECC.construct, curve="P-224", d=2, **coordG)
1283
1284
1285 class TestEccModule_P256(unittest.TestCase):
1286
1287 def test_generate(self):
1288
1289 key = ECC.generate(curve="P-256")
1290 self.assertTrue(key.has_private())
1291 self.assertEqual(key.pointQ, EccPoint(_curves['p256'].Gx,
1292 _curves['p256'].Gy) * key.d,
1293 "p256")
1294
1295 # Other names
1296 ECC.generate(curve="secp256r1")
1297 ECC.generate(curve="prime256v1")
1298
1299 def test_construct(self):
1300
1301 key = ECC.construct(curve="P-256", d=1)
1302 self.assertTrue(key.has_private())
1303 self.assertEqual(key.pointQ, _curves['p256'].G)
1304
1305 key = ECC.construct(curve="P-256", point_x=_curves['p256'].Gx,
1306 point_y=_curves['p256'].Gy)
1307 self.assertFalse(key.has_private())
1308 self.assertEqual(key.pointQ, _curves['p256'].G)
1309
1310 # Other names
1311 ECC.construct(curve="p256", d=1)
1312 ECC.construct(curve="secp256r1", d=1)
1313 ECC.construct(curve="prime256v1", d=1)
1314
1315 def test_negative_construct(self):
1316 coord = dict(point_x=10, point_y=4)
1317 coordG = dict(point_x=_curves['p256'].Gx, point_y=_curves['p256'].Gy)
1318
1319 self.assertRaises(ValueError, ECC.construct, curve="P-256", **coord)
1320 self.assertRaises(ValueError, ECC.construct, curve="P-256", d=2, **coordG)
1321
1322
1323 class TestEccModule_P384(unittest.TestCase):
1324
1325 def test_generate(self):
1326
1327 curve = _curves['p384']
1328 key = ECC.generate(curve="P-384")
1329 self.assertTrue(key.has_private())
1330 self.assertEqual(key.pointQ, EccPoint(curve.Gx, curve.Gy, "p384") * key.d)
1331
1332 # Other names
1333 ECC.generate(curve="secp384r1")
1334 ECC.generate(curve="prime384v1")
1335
1336 def test_construct(self):
1337
1338 curve = _curves['p384']
1339 key = ECC.construct(curve="P-384", d=1)
1340 self.assertTrue(key.has_private())
1341 self.assertEqual(key.pointQ, _curves['p384'].G)
1342
1343 key = ECC.construct(curve="P-384", point_x=curve.Gx, point_y=curve.Gy)
1344 self.assertFalse(key.has_private())
1345 self.assertEqual(key.pointQ, curve.G)
1346
1347 # Other names
1348 ECC.construct(curve="p384", d=1)
1349 ECC.construct(curve="secp384r1", d=1)
1350 ECC.construct(curve="prime384v1", d=1)
1351
1352 def test_negative_construct(self):
1353 coord = dict(point_x=10, point_y=4)
1354 coordG = dict(point_x=_curves['p384'].Gx, point_y=_curves['p384'].Gy)
1355
1356 self.assertRaises(ValueError, ECC.construct, curve="P-384", **coord)
1357 self.assertRaises(ValueError, ECC.construct, curve="P-384", d=2, **coordG)
1358
1359
1360 class TestEccModule_P521(unittest.TestCase):
1361
1362 def test_generate(self):
1363
1364 curve = _curves['p521']
1365 key = ECC.generate(curve="P-521")
1366 self.assertTrue(key.has_private())
1367 self.assertEqual(key.pointQ, EccPoint(curve.Gx, curve.Gy, "p521") * key.d)
1368
1369 # Other names
1370 ECC.generate(curve="secp521r1")
1371 ECC.generate(curve="prime521v1")
1372
1373 def test_construct(self):
1374
1375 curve = _curves['p521']
1376 key = ECC.construct(curve="P-521", d=1)
1377 self.assertTrue(key.has_private())
1378 self.assertEqual(key.pointQ, _curves['p521'].G)
1379
1380 key = ECC.construct(curve="P-521", point_x=curve.Gx, point_y=curve.Gy)
1381 self.assertFalse(key.has_private())
1382 self.assertEqual(key.pointQ, curve.G)
1383
1384 # Other names
1385 ECC.construct(curve="p521", d=1)
1386 ECC.construct(curve="secp521r1", d=1)
1387 ECC.construct(curve="prime521v1", d=1)
1388
1389 def test_negative_construct(self):
1390 coord = dict(point_x=10, point_y=4)
1391 coordG = dict(point_x=_curves['p521'].Gx, point_y=_curves['p521'].Gy)
1392
1393 self.assertRaises(ValueError, ECC.construct, curve="P-521", **coord)
1394 self.assertRaises(ValueError, ECC.construct, curve="P-521", d=2, **coordG)
1395
1396
1397 def get_tests(config={}):
1398 tests = []
1399 tests += list_test_cases(TestEccPoint)
1400 tests += list_test_cases(TestEccPoint_NIST_P192)
1401 tests += list_test_cases(TestEccPoint_NIST_P224)
1402 tests += list_test_cases(TestEccPoint_NIST_P256)
1403 tests += list_test_cases(TestEccPoint_NIST_P384)
1404 tests += list_test_cases(TestEccPoint_NIST_P521)
1405 tests += list_test_cases(TestEccPoint_PAI_P192)
1406 tests += list_test_cases(TestEccPoint_PAI_P224)
1407 tests += list_test_cases(TestEccPoint_PAI_P256)
1408 tests += list_test_cases(TestEccPoint_PAI_P384)
1409 tests += list_test_cases(TestEccPoint_PAI_P521)
1410 tests += list_test_cases(TestEccKey_P192)
1411 tests += list_test_cases(TestEccKey_P224)
1412 tests += list_test_cases(TestEccKey_P256)
1413 tests += list_test_cases(TestEccKey_P384)
1414 tests += list_test_cases(TestEccKey_P521)
1415 tests += list_test_cases(TestEccModule_P192)
1416 tests += list_test_cases(TestEccModule_P224)
1417 tests += list_test_cases(TestEccModule_P256)
1418 tests += list_test_cases(TestEccModule_P384)
1419 tests += list_test_cases(TestEccModule_P521)
1420 return tests
1421
1422
1423 if __name__ == '__main__':
1424 suite = lambda: unittest.TestSuite(get_tests())
1425 unittest.main(defaultTest='suite')