Skip to content

Commit ab3fbd6

Browse files
committed
format NIST curves as they are in FIPS 186-4
1 parent 69e9735 commit ab3fbd6

File tree

2 files changed

+201
-25
lines changed

2 files changed

+201
-25
lines changed

src/ecdsa/_compat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Common functions for providing cross-python version compatibility.
33
"""
44
import sys
5+
import re
56
from six import integer_types
67

78

@@ -23,6 +24,18 @@ def normalise_bytes(buffer_object):
2324
def hmac_compat(ret):
2425
return ret
2526

27+
if sys.version_info < (2, 7) or sys.version_info < (2, 7, 4):
28+
29+
def remove_whitespace(text):
30+
"""Removes all whitespace from passed in string"""
31+
return re.sub(r"\s+", "", text)
32+
33+
else:
34+
35+
def remove_whitespace(text):
36+
"""Removes all whitespace from passed in string"""
37+
return re.sub(r"\s+", "", text, flags=re.UNICODE)
38+
2639

2740
else:
2841
if sys.version_info < (3, 4):
@@ -41,3 +54,7 @@ def hmac_compat(data):
4154
def normalise_bytes(buffer_object):
4255
"""Cast the input into array of bytes."""
4356
return memoryview(buffer_object).cast("B")
57+
58+
def remove_whitespace(text):
59+
"""Removes all whitespace from passed in string"""
60+
return re.sub(r"\s+", "", text, flags=re.UNICODE)

src/ecdsa/ecdsa.py

Lines changed: 184 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from . import ellipticcurve
5858
from . import numbertheory
5959
from .util import bit_length
60+
from ._compat import remove_whitespace
6061

6162

6263
class RSZeroError(RuntimeError):
@@ -298,9 +299,27 @@ def point_is_valid(generator, x, y):
298299
_r = 6277101735386680763835789423176059013767194773182842284081
299300
# s = 0x3045ae6fc8422f64ed579528d38120eae12196d5L
300301
# c = 0x3099d2bbbfcb2538542dcd5fb078b6ef5f3d6fe2c745de65L
301-
_b = 0x64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1
302-
_Gx = 0x188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012
303-
_Gy = 0x07192B95FFC8DA78631011ED6B24CDD573F977A11E794811
302+
_b = int(
303+
remove_whitespace(
304+
"""
305+
64210519 E59C80E7 0FA7E9AB 72243049 FEB8DEEC C146B9B1"""
306+
),
307+
16,
308+
)
309+
_Gx = int(
310+
remove_whitespace(
311+
"""
312+
188DA80E B03090F6 7CBF20EB 43A18800 F4FF0AFD 82FF1012"""
313+
),
314+
16,
315+
)
316+
_Gy = int(
317+
remove_whitespace(
318+
"""
319+
07192B95 FFC8DA78 631011ED 6B24CDD5 73F977A1 1E794811"""
320+
),
321+
16,
322+
)
304323

305324
curve_192 = ellipticcurve.CurveFp(_p, -3, _b, 1)
306325
generator_192 = ellipticcurve.PointJacobi(
@@ -309,55 +328,195 @@ def point_is_valid(generator, x, y):
309328

310329

311330
# NIST Curve P-224:
312-
_p = 26959946667150639794667015087019630673557916260026308143510066298881
313-
_r = 26959946667150639794667015087019625940457807714424391721682722368061
331+
_p = int(
332+
remove_whitespace(
333+
"""
334+
2695994666715063979466701508701963067355791626002630814351
335+
0066298881"""
336+
)
337+
)
338+
_r = int(
339+
remove_whitespace(
340+
"""
341+
2695994666715063979466701508701962594045780771442439172168
342+
2722368061"""
343+
)
344+
)
314345
# s = 0xbd71344799d5c7fcdc45b59fa3b9ab8f6a948bc5L
315346
# c = 0x5b056c7e11dd68f40469ee7f3c7a7d74f7d121116506d031218291fbL
316-
_b = 0xB4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4
317-
_Gx = 0xB70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21
318-
_Gy = 0xBD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34
347+
_b = int(
348+
remove_whitespace(
349+
"""
350+
B4050A85 0C04B3AB F5413256 5044B0B7 D7BFD8BA 270B3943
351+
2355FFB4"""
352+
),
353+
16,
354+
)
355+
_Gx = int(
356+
remove_whitespace(
357+
"""
358+
B70E0CBD 6BB4BF7F 321390B9 4A03C1D3 56C21122 343280D6
359+
115C1D21"""
360+
),
361+
16,
362+
)
363+
_Gy = int(
364+
remove_whitespace(
365+
"""
366+
BD376388 B5F723FB 4C22DFE6 CD4375A0 5A074764 44D58199
367+
85007E34"""
368+
),
369+
16,
370+
)
319371

320372
curve_224 = ellipticcurve.CurveFp(_p, -3, _b, 1)
321373
generator_224 = ellipticcurve.PointJacobi(
322374
curve_224, _Gx, _Gy, 1, _r, generator=True
323375
)
324376

325377
# NIST Curve P-256:
326-
_p = 115792089210356248762697446949407573530086143415290314195533631308867097853951
327-
_r = 115792089210356248762697446949407573529996955224135760342422259061068512044369
378+
_p = int(
379+
remove_whitespace(
380+
"""
381+
1157920892103562487626974469494075735300861434152903141955
382+
33631308867097853951"""
383+
)
384+
)
385+
_r = int(
386+
remove_whitespace(
387+
"""
388+
115792089210356248762697446949407573529996955224135760342
389+
422259061068512044369"""
390+
)
391+
)
328392
# s = 0xc49d360886e704936a6678e1139d26b7819f7e90L
329393
# c = 0x7efba1662985be9403cb055c75d4f7e0ce8d84a9c5114abcaf3177680104fa0dL
330-
_b = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B
331-
_Gx = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296
332-
_Gy = 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5
394+
_b = int(
395+
remove_whitespace(
396+
"""
397+
5AC635D8 AA3A93E7 B3EBBD55 769886BC 651D06B0 CC53B0F6
398+
3BCE3C3E 27D2604B"""
399+
),
400+
16,
401+
)
402+
_Gx = int(
403+
remove_whitespace(
404+
"""
405+
6B17D1F2 E12C4247 F8BCE6E5 63A440F2 77037D81 2DEB33A0
406+
F4A13945 D898C296"""
407+
),
408+
16,
409+
)
410+
_Gy = int(
411+
remove_whitespace(
412+
"""
413+
4FE342E2 FE1A7F9B 8EE7EB4A 7C0F9E16 2BCE3357 6B315ECE
414+
CBB64068 37BF51F5"""
415+
),
416+
16,
417+
)
333418

334419
curve_256 = ellipticcurve.CurveFp(_p, -3, _b, 1)
335420
generator_256 = ellipticcurve.PointJacobi(
336421
curve_256, _Gx, _Gy, 1, _r, generator=True
337422
)
338423

339424
# NIST Curve P-384:
340-
_p = 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
341-
_r = 39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643
425+
_p = int(
426+
remove_whitespace(
427+
"""
428+
3940200619639447921227904010014361380507973927046544666794
429+
8293404245721771496870329047266088258938001861606973112319"""
430+
)
431+
)
432+
_r = int(
433+
remove_whitespace(
434+
"""
435+
3940200619639447921227904010014361380507973927046544666794
436+
6905279627659399113263569398956308152294913554433653942643"""
437+
)
438+
)
342439
# s = 0xa335926aa319a27a1d00896a6773a4827acdac73L
343-
# c = 0x79d1e655f868f02fff48dcdee14151ddb80643c1406d0ca10dfe6fc52009540a495e8042ea5f744f6e184667cc722483L
344-
_b = 0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF
345-
_Gx = 0xAA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7
346-
_Gy = 0x3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F
440+
# c = int(remove_whitespace(
441+
# """
442+
# 79d1e655 f868f02f ff48dcde e14151dd b80643c1 406d0ca1
443+
# 0dfe6fc5 2009540a 495e8042 ea5f744f 6e184667 cc722483"""
444+
# ), 16)
445+
_b = int(
446+
remove_whitespace(
447+
"""
448+
B3312FA7 E23EE7E4 988E056B E3F82D19 181D9C6E FE814112
449+
0314088F 5013875A C656398D 8A2ED19D 2A85C8ED D3EC2AEF"""
450+
),
451+
16,
452+
)
453+
_Gx = int(
454+
remove_whitespace(
455+
"""
456+
AA87CA22 BE8B0537 8EB1C71E F320AD74 6E1D3B62 8BA79B98
457+
59F741E0 82542A38 5502F25D BF55296C 3A545E38 72760AB7"""
458+
),
459+
16,
460+
)
461+
_Gy = int(
462+
remove_whitespace(
463+
"""
464+
3617DE4A 96262C6F 5D9E98BF 9292DC29 F8F41DBD 289A147C
465+
E9DA3113 B5F0B8C0 0A60B1CE 1D7E819D 7A431D7C 90EA0E5F"""
466+
),
467+
16,
468+
)
347469

348470
curve_384 = ellipticcurve.CurveFp(_p, -3, _b, 1)
349471
generator_384 = ellipticcurve.PointJacobi(
350472
curve_384, _Gx, _Gy, 1, _r, generator=True
351473
)
352474

353475
# NIST Curve P-521:
354-
_p = 6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151
355-
_r = 6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449
476+
_p = int(
477+
"686479766013060971498190079908139321726943530014330540939"
478+
"446345918554318339765605212255964066145455497729631139148"
479+
"0858037121987999716643812574028291115057151"
480+
)
481+
_r = int(
482+
"686479766013060971498190079908139321726943530014330540939"
483+
"446345918554318339765539424505774633321719753296399637136"
484+
"3321113864768612440380340372808892707005449"
485+
)
356486
# s = 0xd09e8800291cb85396cc6717393284aaa0da64baL
357-
# c = 0x0b48bfa5f420a34949539d2bdfc264eeeeb077688e44fbf0ad8f6d0edb37bd6b533281000518e19f1b9ffbe0fe9ed8a3c2200b8f875e523868c70c1e5bf55bad637L
358-
_b = 0x051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00
359-
_Gx = 0xC6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66
360-
_Gy = 0x11839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650
487+
# c = int(remove_whitespace(
488+
# """
489+
# 0b4 8bfa5f42 0a349495 39d2bdfc 264eeeeb 077688e4
490+
# 4fbf0ad8 f6d0edb3 7bd6b533 28100051 8e19f1b9 ffbe0fe9
491+
# ed8a3c22 00b8f875 e523868c 70c1e5bf 55bad637"""
492+
# ), 16)
493+
_b = int(
494+
remove_whitespace(
495+
"""
496+
051 953EB961 8E1C9A1F 929A21A0 B68540EE A2DA725B
497+
99B315F3 B8B48991 8EF109E1 56193951 EC7E937B 1652C0BD
498+
3BB1BF07 3573DF88 3D2C34F1 EF451FD4 6B503F00"""
499+
),
500+
16,
501+
)
502+
_Gx = int(
503+
remove_whitespace(
504+
"""
505+
C6 858E06B7 0404E9CD 9E3ECB66 2395B442 9C648139
506+
053FB521 F828AF60 6B4D3DBA A14B5E77 EFE75928 FE1DC127
507+
A2FFA8DE 3348B3C1 856A429B F97E7E31 C2E5BD66"""
508+
),
509+
16,
510+
)
511+
_Gy = int(
512+
remove_whitespace(
513+
"""
514+
118 39296A78 9A3BC004 5C8A5FB4 2C7D1BD9 98F54449
515+
579B4468 17AFBD17 273E662C 97EE7299 5EF42640 C550B901
516+
3FAD0761 353C7086 A272C240 88BE9476 9FD16650"""
517+
),
518+
16,
519+
)
361520

362521
curve_521 = ellipticcurve.CurveFp(_p, -3, _b, 1)
363522
generator_521 = ellipticcurve.PointJacobi(

0 commit comments

Comments
 (0)