You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1095 line
31 KiB

  1. #! /usr/bin/env python
  2. """
  3. Low level implementation of Elliptic-Curve Digital Signatures.
  4. .. note ::
  5. You're most likely looking for the :py:class:`~ecdsa.keys` module.
  6. This is a low-level implementation of the ECDSA that operates on
  7. integers, not byte strings.
  8. NOTE: This a low level implementation of ECDSA, for normal applications
  9. you should be looking at the keys.py module.
  10. Classes and methods for elliptic-curve signatures:
  11. private keys, public keys, signatures,
  12. and definitions of prime-modulus curves.
  13. Example:
  14. .. code-block:: python
  15. # (In real-life applications, you would probably want to
  16. # protect against defects in SystemRandom.)
  17. from random import SystemRandom
  18. randrange = SystemRandom().randrange
  19. # Generate a public/private key pair using the NIST Curve P-192:
  20. g = generator_192
  21. n = g.order()
  22. secret = randrange( 1, n )
  23. pubkey = Public_key( g, g * secret )
  24. privkey = Private_key( pubkey, secret )
  25. # Signing a hash value:
  26. hash = randrange( 1, n )
  27. signature = privkey.sign( hash, randrange( 1, n ) )
  28. # Verifying a signature for a hash value:
  29. if pubkey.verifies( hash, signature ):
  30. print("Demo verification succeeded.")
  31. else:
  32. print("*** Demo verification failed.")
  33. # Verification fails if the hash value is modified:
  34. if pubkey.verifies( hash-1, signature ):
  35. print("**** Demo verification failed to reject tampered hash.")
  36. else:
  37. print("Demo verification correctly rejected tampered hash.")
  38. Revision history:
  39. 2005.12.31 - Initial version.
  40. 2008.11.25 - Substantial revisions introducing new classes.
  41. 2009.05.16 - Warn against using random.randrange in real applications.
  42. 2009.05.17 - Use random.SystemRandom by default.
  43. Originally written in 2005 by Peter Pearson and placed in the public domain,
  44. modified as part of the python-ecdsa package.
  45. """
  46. import warnings
  47. from six import int2byte
  48. from . import ellipticcurve
  49. from . import numbertheory
  50. from .util import bit_length
  51. from ._compat import remove_whitespace
  52. class RSZeroError(RuntimeError):
  53. pass
  54. class InvalidPointError(RuntimeError):
  55. pass
  56. class Signature(object):
  57. """
  58. ECDSA signature.
  59. :ivar int r: the ``r`` element of the ECDSA signature
  60. :ivar int s: the ``s`` element of the ECDSA signature
  61. """
  62. def __init__(self, r, s):
  63. self.r = r
  64. self.s = s
  65. def recover_public_keys(self, hash, generator):
  66. """
  67. Returns two public keys for which the signature is valid
  68. :param int hash: signed hash
  69. :param AbstractPoint generator: is the generator used in creation
  70. of the signature
  71. :rtype: tuple(Public_key, Public_key)
  72. :return: a pair of public keys that can validate the signature
  73. """
  74. curve = generator.curve()
  75. n = generator.order()
  76. r = self.r
  77. s = self.s
  78. e = hash
  79. x = r
  80. # Compute the curve point with x as x-coordinate
  81. alpha = (
  82. pow(x, 3, curve.p()) + (curve.a() * x) + curve.b()
  83. ) % curve.p()
  84. beta = numbertheory.square_root_mod_prime(alpha, curve.p())
  85. y = beta if beta % 2 == 0 else curve.p() - beta
  86. # Compute the public key
  87. R1 = ellipticcurve.PointJacobi(curve, x, y, 1, n)
  88. Q1 = numbertheory.inverse_mod(r, n) * (s * R1 + (-e % n) * generator)
  89. Pk1 = Public_key(generator, Q1)
  90. # And the second solution
  91. R2 = ellipticcurve.PointJacobi(curve, x, -y, 1, n)
  92. Q2 = numbertheory.inverse_mod(r, n) * (s * R2 + (-e % n) * generator)
  93. Pk2 = Public_key(generator, Q2)
  94. return [Pk1, Pk2]
  95. class Public_key(object):
  96. """Public key for ECDSA."""
  97. def __init__(self, generator, point, verify=True):
  98. """Low level ECDSA public key object.
  99. :param generator: the Point that generates the group (the base point)
  100. :param point: the Point that defines the public key
  101. :param bool verify: if True check if point is valid point on curve
  102. :raises InvalidPointError: if the point parameters are invalid or
  103. point does not lay on the curve
  104. """
  105. self.curve = generator.curve()
  106. self.generator = generator
  107. self.point = point
  108. n = generator.order()
  109. p = self.curve.p()
  110. if not (0 <= point.x() < p) or not (0 <= point.y() < p):
  111. raise InvalidPointError(
  112. "The public point has x or y out of range."
  113. )
  114. if verify and not self.curve.contains_point(point.x(), point.y()):
  115. raise InvalidPointError("Point does not lay on the curve")
  116. if not n:
  117. raise InvalidPointError("Generator point must have order.")
  118. # for curve parameters with base point with cofactor 1, all points
  119. # that are on the curve are scalar multiples of the base point, so
  120. # verifying that is not necessary. See Section 3.2.2.1 of SEC 1 v2
  121. if (
  122. verify
  123. and self.curve.cofactor() != 1
  124. and not n * point == ellipticcurve.INFINITY
  125. ):
  126. raise InvalidPointError("Generator point order is bad.")
  127. def __eq__(self, other):
  128. """Return True if the keys are identical, False otherwise.
  129. Note: for comparison, only placement on the same curve and point
  130. equality is considered, use of the same generator point is not
  131. considered.
  132. """
  133. if isinstance(other, Public_key):
  134. return self.curve == other.curve and self.point == other.point
  135. return NotImplemented
  136. def __ne__(self, other):
  137. """Return False if the keys are identical, True otherwise."""
  138. return not self == other
  139. def verifies(self, hash, signature):
  140. """Verify that signature is a valid signature of hash.
  141. Return True if the signature is valid.
  142. """
  143. # From X9.62 J.3.1.
  144. G = self.generator
  145. n = G.order()
  146. r = signature.r
  147. s = signature.s
  148. if r < 1 or r > n - 1:
  149. return False
  150. if s < 1 or s > n - 1:
  151. return False
  152. c = numbertheory.inverse_mod(s, n)
  153. u1 = (hash * c) % n
  154. u2 = (r * c) % n
  155. if hasattr(G, "mul_add"):
  156. xy = G.mul_add(u1, self.point, u2)
  157. else:
  158. xy = u1 * G + u2 * self.point
  159. v = xy.x() % n
  160. return v == r
  161. class Private_key(object):
  162. """Private key for ECDSA."""
  163. def __init__(self, public_key, secret_multiplier):
  164. """public_key is of class Public_key;
  165. secret_multiplier is a large integer.
  166. """
  167. self.public_key = public_key
  168. self.secret_multiplier = secret_multiplier
  169. def __eq__(self, other):
  170. """Return True if the points are identical, False otherwise."""
  171. if isinstance(other, Private_key):
  172. return (
  173. self.public_key == other.public_key
  174. and self.secret_multiplier == other.secret_multiplier
  175. )
  176. return NotImplemented
  177. def __ne__(self, other):
  178. """Return False if the points are identical, True otherwise."""
  179. return not self == other
  180. def sign(self, hash, random_k):
  181. """Return a signature for the provided hash, using the provided
  182. random nonce. It is absolutely vital that random_k be an unpredictable
  183. number in the range [1, self.public_key.point.order()-1]. If
  184. an attacker can guess random_k, he can compute our private key from a
  185. single signature. Also, if an attacker knows a few high-order
  186. bits (or a few low-order bits) of random_k, he can compute our private
  187. key from many signatures. The generation of nonces with adequate
  188. cryptographic strength is very difficult and far beyond the scope
  189. of this comment.
  190. May raise RuntimeError, in which case retrying with a new
  191. random value k is in order.
  192. """
  193. G = self.public_key.generator
  194. n = G.order()
  195. k = random_k % n
  196. # Fix the bit-length of the random nonce,
  197. # so that it doesn't leak via timing.
  198. # This does not change that ks = k mod n
  199. ks = k + n
  200. kt = ks + n
  201. if bit_length(ks) == bit_length(n):
  202. p1 = kt * G
  203. else:
  204. p1 = ks * G
  205. r = p1.x() % n
  206. if r == 0:
  207. raise RSZeroError("amazingly unlucky random number r")
  208. s = (
  209. numbertheory.inverse_mod(k, n)
  210. * (hash + (self.secret_multiplier * r) % n)
  211. ) % n
  212. if s == 0:
  213. raise RSZeroError("amazingly unlucky random number s")
  214. return Signature(r, s)
  215. def int_to_string(x): # pragma: no cover
  216. """Convert integer x into a string of bytes, as per X9.62."""
  217. # deprecated in 0.19
  218. warnings.warn(
  219. "Function is unused in library code. If you use this code, "
  220. "change to util.number_to_string.",
  221. DeprecationWarning,
  222. )
  223. assert x >= 0
  224. if x == 0:
  225. return b"\0"
  226. result = []
  227. while x:
  228. ordinal = x & 0xFF
  229. result.append(int2byte(ordinal))
  230. x >>= 8
  231. result.reverse()
  232. return b"".join(result)
  233. def string_to_int(s): # pragma: no cover
  234. """Convert a string of bytes into an integer, as per X9.62."""
  235. # deprecated in 0.19
  236. warnings.warn(
  237. "Function is unused in library code. If you use this code, "
  238. "change to util.string_to_number.",
  239. DeprecationWarning,
  240. )
  241. result = 0
  242. for c in s:
  243. if not isinstance(c, int):
  244. c = ord(c)
  245. result = 256 * result + c
  246. return result
  247. def digest_integer(m): # pragma: no cover
  248. """Convert an integer into a string of bytes, compute
  249. its SHA-1 hash, and convert the result to an integer."""
  250. # deprecated in 0.19
  251. warnings.warn(
  252. "Function is unused in library code. If you use this code, "
  253. "change to a one-liner with util.number_to_string and "
  254. "util.string_to_number methods.",
  255. DeprecationWarning,
  256. )
  257. #
  258. # I don't expect this function to be used much. I wrote
  259. # it in order to be able to duplicate the examples
  260. # in ECDSAVS.
  261. #
  262. from hashlib import sha1
  263. return string_to_int(sha1(int_to_string(m)).digest())
  264. def point_is_valid(generator, x, y):
  265. """Is (x,y) a valid public key based on the specified generator?"""
  266. # These are the tests specified in X9.62.
  267. n = generator.order()
  268. curve = generator.curve()
  269. p = curve.p()
  270. if not (0 <= x < p) or not (0 <= y < p):
  271. return False
  272. if not curve.contains_point(x, y):
  273. return False
  274. if (
  275. curve.cofactor() != 1
  276. and not n * ellipticcurve.PointJacobi(curve, x, y, 1)
  277. == ellipticcurve.INFINITY
  278. ):
  279. return False
  280. return True
  281. # secp112r1 curve
  282. _p = int(remove_whitespace("DB7C 2ABF62E3 5E668076 BEAD208B"), 16)
  283. # s = 00F50B02 8E4D696E 67687561 51752904 72783FB1
  284. _a = int(remove_whitespace("DB7C 2ABF62E3 5E668076 BEAD2088"), 16)
  285. _b = int(remove_whitespace("659E F8BA0439 16EEDE89 11702B22"), 16)
  286. _Gx = int(remove_whitespace("09487239 995A5EE7 6B55F9C2 F098"), 16)
  287. _Gy = int(remove_whitespace("A89C E5AF8724 C0A23E0E 0FF77500"), 16)
  288. _r = int(remove_whitespace("DB7C 2ABF62E3 5E7628DF AC6561C5"), 16)
  289. _h = 1
  290. curve_112r1 = ellipticcurve.CurveFp(_p, _a, _b, _h)
  291. generator_112r1 = ellipticcurve.PointJacobi(
  292. curve_112r1, _Gx, _Gy, 1, _r, generator=True
  293. )
  294. # secp112r2 curve
  295. _p = int(remove_whitespace("DB7C 2ABF62E3 5E668076 BEAD208B"), 16)
  296. # s = 022757A1 114D69E 67687561 51755316 C05E0BD4
  297. _a = int(remove_whitespace("6127 C24C05F3 8A0AAAF6 5C0EF02C"), 16)
  298. _b = int(remove_whitespace("51DE F1815DB5 ED74FCC3 4C85D709"), 16)
  299. _Gx = int(remove_whitespace("4BA30AB5 E892B4E1 649DD092 8643"), 16)
  300. _Gy = int(remove_whitespace("ADCD 46F5882E 3747DEF3 6E956E97"), 16)
  301. _r = int(remove_whitespace("36DF 0AAFD8B8 D7597CA1 0520D04B"), 16)
  302. _h = 4
  303. curve_112r2 = ellipticcurve.CurveFp(_p, _a, _b, _h)
  304. generator_112r2 = ellipticcurve.PointJacobi(
  305. curve_112r2, _Gx, _Gy, 1, _r, generator=True
  306. )
  307. # secp128r1 curve
  308. _p = int(remove_whitespace("FFFFFFFD FFFFFFFF FFFFFFFF FFFFFFFF"), 16)
  309. # S = 000E0D4D 69E6768 75615175 0CC03A44 73D03679
  310. # a and b are mod p, so a is equal to p-3, or simply -3
  311. # _a = -3
  312. _b = int(remove_whitespace("E87579C1 1079F43D D824993C 2CEE5ED3"), 16)
  313. _Gx = int(remove_whitespace("161FF752 8B899B2D 0C28607C A52C5B86"), 16)
  314. _Gy = int(remove_whitespace("CF5AC839 5BAFEB13 C02DA292 DDED7A83"), 16)
  315. _r = int(remove_whitespace("FFFFFFFE 00000000 75A30D1B 9038A115"), 16)
  316. _h = 1
  317. curve_128r1 = ellipticcurve.CurveFp(_p, -3, _b, _h)
  318. generator_128r1 = ellipticcurve.PointJacobi(
  319. curve_128r1, _Gx, _Gy, 1, _r, generator=True
  320. )
  321. # secp160r1
  322. _p = int(remove_whitespace("FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 7FFFFFFF"), 16)
  323. # S = 1053CDE4 2C14D696 E6768756 1517533B F3F83345
  324. # a and b are mod p, so a is equal to p-3, or simply -3
  325. # _a = -3
  326. _b = int(remove_whitespace("1C97BEFC 54BD7A8B 65ACF89F 81D4D4AD C565FA45"), 16)
  327. _Gx = int(
  328. remove_whitespace("4A96B568 8EF57328 46646989 68C38BB9 13CBFC82"),
  329. 16,
  330. )
  331. _Gy = int(
  332. remove_whitespace("23A62855 3168947D 59DCC912 04235137 7AC5FB32"),
  333. 16,
  334. )
  335. _r = int(
  336. remove_whitespace("01 00000000 00000000 0001F4C8 F927AED3 CA752257"),
  337. 16,
  338. )
  339. _h = 1
  340. curve_160r1 = ellipticcurve.CurveFp(_p, -3, _b, _h)
  341. generator_160r1 = ellipticcurve.PointJacobi(
  342. curve_160r1, _Gx, _Gy, 1, _r, generator=True
  343. )
  344. # NIST Curve P-192:
  345. _p = 6277101735386680763835789423207666416083908700390324961279
  346. _r = 6277101735386680763835789423176059013767194773182842284081
  347. # s = 0x3045ae6fc8422f64ed579528d38120eae12196d5L
  348. # c = 0x3099d2bbbfcb2538542dcd5fb078b6ef5f3d6fe2c745de65L
  349. _b = int(
  350. remove_whitespace(
  351. """
  352. 64210519 E59C80E7 0FA7E9AB 72243049 FEB8DEEC C146B9B1"""
  353. ),
  354. 16,
  355. )
  356. _Gx = int(
  357. remove_whitespace(
  358. """
  359. 188DA80E B03090F6 7CBF20EB 43A18800 F4FF0AFD 82FF1012"""
  360. ),
  361. 16,
  362. )
  363. _Gy = int(
  364. remove_whitespace(
  365. """
  366. 07192B95 FFC8DA78 631011ED 6B24CDD5 73F977A1 1E794811"""
  367. ),
  368. 16,
  369. )
  370. curve_192 = ellipticcurve.CurveFp(_p, -3, _b, 1)
  371. generator_192 = ellipticcurve.PointJacobi(
  372. curve_192, _Gx, _Gy, 1, _r, generator=True
  373. )
  374. # NIST Curve P-224:
  375. _p = int(
  376. remove_whitespace(
  377. """
  378. 2695994666715063979466701508701963067355791626002630814351
  379. 0066298881"""
  380. )
  381. )
  382. _r = int(
  383. remove_whitespace(
  384. """
  385. 2695994666715063979466701508701962594045780771442439172168
  386. 2722368061"""
  387. )
  388. )
  389. # s = 0xbd71344799d5c7fcdc45b59fa3b9ab8f6a948bc5L
  390. # c = 0x5b056c7e11dd68f40469ee7f3c7a7d74f7d121116506d031218291fbL
  391. _b = int(
  392. remove_whitespace(
  393. """
  394. B4050A85 0C04B3AB F5413256 5044B0B7 D7BFD8BA 270B3943
  395. 2355FFB4"""
  396. ),
  397. 16,
  398. )
  399. _Gx = int(
  400. remove_whitespace(
  401. """
  402. B70E0CBD 6BB4BF7F 321390B9 4A03C1D3 56C21122 343280D6
  403. 115C1D21"""
  404. ),
  405. 16,
  406. )
  407. _Gy = int(
  408. remove_whitespace(
  409. """
  410. BD376388 B5F723FB 4C22DFE6 CD4375A0 5A074764 44D58199
  411. 85007E34"""
  412. ),
  413. 16,
  414. )
  415. curve_224 = ellipticcurve.CurveFp(_p, -3, _b, 1)
  416. generator_224 = ellipticcurve.PointJacobi(
  417. curve_224, _Gx, _Gy, 1, _r, generator=True
  418. )
  419. # NIST Curve P-256:
  420. _p = int(
  421. remove_whitespace(
  422. """
  423. 1157920892103562487626974469494075735300861434152903141955
  424. 33631308867097853951"""
  425. )
  426. )
  427. _r = int(
  428. remove_whitespace(
  429. """
  430. 115792089210356248762697446949407573529996955224135760342
  431. 422259061068512044369"""
  432. )
  433. )
  434. # s = 0xc49d360886e704936a6678e1139d26b7819f7e90L
  435. # c = 0x7efba1662985be9403cb055c75d4f7e0ce8d84a9c5114abcaf3177680104fa0dL
  436. _b = int(
  437. remove_whitespace(
  438. """
  439. 5AC635D8 AA3A93E7 B3EBBD55 769886BC 651D06B0 CC53B0F6
  440. 3BCE3C3E 27D2604B"""
  441. ),
  442. 16,
  443. )
  444. _Gx = int(
  445. remove_whitespace(
  446. """
  447. 6B17D1F2 E12C4247 F8BCE6E5 63A440F2 77037D81 2DEB33A0
  448. F4A13945 D898C296"""
  449. ),
  450. 16,
  451. )
  452. _Gy = int(
  453. remove_whitespace(
  454. """
  455. 4FE342E2 FE1A7F9B 8EE7EB4A 7C0F9E16 2BCE3357 6B315ECE
  456. CBB64068 37BF51F5"""
  457. ),
  458. 16,
  459. )
  460. curve_256 = ellipticcurve.CurveFp(_p, -3, _b, 1)
  461. generator_256 = ellipticcurve.PointJacobi(
  462. curve_256, _Gx, _Gy, 1, _r, generator=True
  463. )
  464. # NIST Curve P-384:
  465. _p = int(
  466. remove_whitespace(
  467. """
  468. 3940200619639447921227904010014361380507973927046544666794
  469. 8293404245721771496870329047266088258938001861606973112319"""
  470. )
  471. )
  472. _r = int(
  473. remove_whitespace(
  474. """
  475. 3940200619639447921227904010014361380507973927046544666794
  476. 6905279627659399113263569398956308152294913554433653942643"""
  477. )
  478. )
  479. # s = 0xa335926aa319a27a1d00896a6773a4827acdac73L
  480. # c = int(remove_whitespace(
  481. # """
  482. # 79d1e655 f868f02f ff48dcde e14151dd b80643c1 406d0ca1
  483. # 0dfe6fc5 2009540a 495e8042 ea5f744f 6e184667 cc722483"""
  484. # ), 16)
  485. _b = int(
  486. remove_whitespace(
  487. """
  488. B3312FA7 E23EE7E4 988E056B E3F82D19 181D9C6E FE814112
  489. 0314088F 5013875A C656398D 8A2ED19D 2A85C8ED D3EC2AEF"""
  490. ),
  491. 16,
  492. )
  493. _Gx = int(
  494. remove_whitespace(
  495. """
  496. AA87CA22 BE8B0537 8EB1C71E F320AD74 6E1D3B62 8BA79B98
  497. 59F741E0 82542A38 5502F25D BF55296C 3A545E38 72760AB7"""
  498. ),
  499. 16,
  500. )
  501. _Gy = int(
  502. remove_whitespace(
  503. """
  504. 3617DE4A 96262C6F 5D9E98BF 9292DC29 F8F41DBD 289A147C
  505. E9DA3113 B5F0B8C0 0A60B1CE 1D7E819D 7A431D7C 90EA0E5F"""
  506. ),
  507. 16,
  508. )
  509. curve_384 = ellipticcurve.CurveFp(_p, -3, _b, 1)
  510. generator_384 = ellipticcurve.PointJacobi(
  511. curve_384, _Gx, _Gy, 1, _r, generator=True
  512. )
  513. # NIST Curve P-521:
  514. _p = int(
  515. "686479766013060971498190079908139321726943530014330540939"
  516. "446345918554318339765605212255964066145455497729631139148"
  517. "0858037121987999716643812574028291115057151"
  518. )
  519. _r = int(
  520. "686479766013060971498190079908139321726943530014330540939"
  521. "446345918554318339765539424505774633321719753296399637136"
  522. "3321113864768612440380340372808892707005449"
  523. )
  524. # s = 0xd09e8800291cb85396cc6717393284aaa0da64baL
  525. # c = int(remove_whitespace(
  526. # """
  527. # 0b4 8bfa5f42 0a349495 39d2bdfc 264eeeeb 077688e4
  528. # 4fbf0ad8 f6d0edb3 7bd6b533 28100051 8e19f1b9 ffbe0fe9
  529. # ed8a3c22 00b8f875 e523868c 70c1e5bf 55bad637"""
  530. # ), 16)
  531. _b = int(
  532. remove_whitespace(
  533. """
  534. 051 953EB961 8E1C9A1F 929A21A0 B68540EE A2DA725B
  535. 99B315F3 B8B48991 8EF109E1 56193951 EC7E937B 1652C0BD
  536. 3BB1BF07 3573DF88 3D2C34F1 EF451FD4 6B503F00"""
  537. ),
  538. 16,
  539. )
  540. _Gx = int(
  541. remove_whitespace(
  542. """
  543. C6 858E06B7 0404E9CD 9E3ECB66 2395B442 9C648139
  544. 053FB521 F828AF60 6B4D3DBA A14B5E77 EFE75928 FE1DC127
  545. A2FFA8DE 3348B3C1 856A429B F97E7E31 C2E5BD66"""
  546. ),
  547. 16,
  548. )
  549. _Gy = int(
  550. remove_whitespace(
  551. """
  552. 118 39296A78 9A3BC004 5C8A5FB4 2C7D1BD9 98F54449
  553. 579B4468 17AFBD17 273E662C 97EE7299 5EF42640 C550B901
  554. 3FAD0761 353C7086 A272C240 88BE9476 9FD16650"""
  555. ),
  556. 16,
  557. )
  558. curve_521 = ellipticcurve.CurveFp(_p, -3, _b, 1)
  559. generator_521 = ellipticcurve.PointJacobi(
  560. curve_521, _Gx, _Gy, 1, _r, generator=True
  561. )
  562. # Certicom secp256-k1
  563. _a = 0x0000000000000000000000000000000000000000000000000000000000000000
  564. _b = 0x0000000000000000000000000000000000000000000000000000000000000007
  565. _p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
  566. _Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
  567. _Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
  568. _r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
  569. curve_secp256k1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  570. generator_secp256k1 = ellipticcurve.PointJacobi(
  571. curve_secp256k1, _Gx, _Gy, 1, _r, generator=True
  572. )
  573. # Brainpool P-160-r1
  574. _a = 0x340E7BE2A280EB74E2BE61BADA745D97E8F7C300
  575. _b = 0x1E589A8595423412134FAA2DBDEC95C8D8675E58
  576. _p = 0xE95E4A5F737059DC60DFC7AD95B3D8139515620F
  577. _Gx = 0xBED5AF16EA3F6A4F62938C4631EB5AF7BDBCDBC3
  578. _Gy = 0x1667CB477A1A8EC338F94741669C976316DA6321
  579. _q = 0xE95E4A5F737059DC60DF5991D45029409E60FC09
  580. curve_brainpoolp160r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  581. generator_brainpoolp160r1 = ellipticcurve.PointJacobi(
  582. curve_brainpoolp160r1, _Gx, _Gy, 1, _q, generator=True
  583. )
  584. # Brainpool P-160-t1
  585. _a = 0xE95E4A5F737059DC60DFC7AD95B3D8139515620C
  586. _b = 0x7A556B6DAE535B7B51ED2C4D7DAA7A0B5C55F380
  587. # _z = 0x24DBFF5DEC9B986BBFE5295A29BFBAE45E0F5D0B
  588. _Gx = 0xB199B13B9B34EFC1397E64BAEB05ACC265FF2378
  589. _Gy = 0xADD6718B7C7C1961F0991B842443772152C9E0AD
  590. _q = 0xE95E4A5F737059DC60DF5991D45029409E60FC09
  591. curve_brainpoolp160t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  592. generator_brainpoolp160t1 = ellipticcurve.PointJacobi(
  593. curve_brainpoolp160t1, _Gx, _Gy, 1, _q, generator=True
  594. )
  595. # Brainpool P-192-r1
  596. _a = 0x6A91174076B1E0E19C39C031FE8685C1CAE040E5C69A28EF
  597. _b = 0x469A28EF7C28CCA3DC721D044F4496BCCA7EF4146FBF25C9
  598. _p = 0xC302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297
  599. _Gx = 0xC0A0647EAAB6A48753B033C56CB0F0900A2F5C4853375FD6
  600. _Gy = 0x14B690866ABD5BB88B5F4828C1490002E6773FA2FA299B8F
  601. _q = 0xC302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1
  602. curve_brainpoolp192r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  603. generator_brainpoolp192r1 = ellipticcurve.PointJacobi(
  604. curve_brainpoolp192r1, _Gx, _Gy, 1, _q, generator=True
  605. )
  606. # Brainpool P-192-t1
  607. _a = 0xC302F41D932A36CDA7A3463093D18DB78FCE476DE1A86294
  608. _b = 0x13D56FFAEC78681E68F9DEB43B35BEC2FB68542E27897B79
  609. # _z = 0x1B6F5CC8DB4DC7AF19458A9CB80DC2295E5EB9C3732104CB
  610. _Gx = 0x3AE9E58C82F63C30282E1FE7BBF43FA72C446AF6F4618129
  611. _Gy = 0x097E2C5667C2223A902AB5CA449D0084B7E5B3DE7CCC01C9
  612. _q = 0xC302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1
  613. curve_brainpoolp192t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  614. generator_brainpoolp192t1 = ellipticcurve.PointJacobi(
  615. curve_brainpoolp192t1, _Gx, _Gy, 1, _q, generator=True
  616. )
  617. # Brainpool P-224-r1
  618. _a = 0x68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43
  619. _b = 0x2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B
  620. _p = 0xD7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF
  621. _Gx = 0x0D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D
  622. _Gy = 0x58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD
  623. _q = 0xD7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F
  624. curve_brainpoolp224r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  625. generator_brainpoolp224r1 = ellipticcurve.PointJacobi(
  626. curve_brainpoolp224r1, _Gx, _Gy, 1, _q, generator=True
  627. )
  628. # Brainpool P-224-t1
  629. _a = 0xD7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FC
  630. _b = 0x4B337D934104CD7BEF271BF60CED1ED20DA14C08B3BB64F18A60888D
  631. # _z = 0x2DF271E14427A346910CF7A2E6CFA7B3F484E5C2CCE1C8B730E28B3F
  632. _Gx = 0x6AB1E344CE25FF3896424E7FFE14762ECB49F8928AC0C76029B4D580
  633. _Gy = 0x0374E9F5143E568CD23F3F4D7C0D4B1E41C8CC0D1C6ABD5F1A46DB4C
  634. _q = 0xD7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F
  635. curve_brainpoolp224t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  636. generator_brainpoolp224t1 = ellipticcurve.PointJacobi(
  637. curve_brainpoolp224t1, _Gx, _Gy, 1, _q, generator=True
  638. )
  639. # Brainpool P-256-r1
  640. _a = 0x7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9
  641. _b = 0x26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6
  642. _p = 0xA9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377
  643. _Gx = 0x8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262
  644. _Gy = 0x547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997
  645. _q = 0xA9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7
  646. curve_brainpoolp256r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  647. generator_brainpoolp256r1 = ellipticcurve.PointJacobi(
  648. curve_brainpoolp256r1, _Gx, _Gy, 1, _q, generator=True
  649. )
  650. # Brainpool P-256-t1
  651. _a = 0xA9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5374
  652. _b = 0x662C61C430D84EA4FE66A7733D0B76B7BF93EBC4AF2F49256AE58101FEE92B04
  653. # _z = 0x3E2D4BD9597B58639AE7AA669CAB9837CF5CF20A2C852D10F655668DFC150EF0
  654. _Gx = 0xA3E8EB3CC1CFE7B7732213B23A656149AFA142C47AAFBC2B79A191562E1305F4
  655. _Gy = 0x2D996C823439C56D7F7B22E14644417E69BCB6DE39D027001DABE8F35B25C9BE
  656. _q = 0xA9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7
  657. curve_brainpoolp256t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  658. generator_brainpoolp256t1 = ellipticcurve.PointJacobi(
  659. curve_brainpoolp256t1, _Gx, _Gy, 1, _q, generator=True
  660. )
  661. # Brainpool P-320-r1
  662. _a = int(
  663. remove_whitespace(
  664. """
  665. 3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9
  666. F492F375A97D860EB4"""
  667. ),
  668. 16,
  669. )
  670. _b = int(
  671. remove_whitespace(
  672. """
  673. 520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539
  674. 816F5EB4AC8FB1F1A6"""
  675. ),
  676. 16,
  677. )
  678. _p = int(
  679. remove_whitespace(
  680. """
  681. D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC
  682. 28FCD412B1F1B32E27"""
  683. ),
  684. 16,
  685. )
  686. _Gx = int(
  687. remove_whitespace(
  688. """
  689. 43BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599
  690. C710AF8D0D39E20611"""
  691. ),
  692. 16,
  693. )
  694. _Gy = int(
  695. remove_whitespace(
  696. """
  697. 14FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6A
  698. C7D35245D1692E8EE1"""
  699. ),
  700. 16,
  701. )
  702. _q = int(
  703. remove_whitespace(
  704. """
  705. D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658
  706. E98691555B44C59311"""
  707. ),
  708. 16,
  709. )
  710. curve_brainpoolp320r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  711. generator_brainpoolp320r1 = ellipticcurve.PointJacobi(
  712. curve_brainpoolp320r1, _Gx, _Gy, 1, _q, generator=True
  713. )
  714. # Brainpool P-320-t1
  715. _a = int(
  716. remove_whitespace(
  717. """
  718. D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC
  719. 28FCD412B1F1B32E24"""
  720. ),
  721. 16,
  722. )
  723. _b = int(
  724. remove_whitespace(
  725. """
  726. A7F561E038EB1ED560B3D147DB782013064C19F27ED27C6780AAF77FB8A547
  727. CEB5B4FEF422340353"""
  728. ),
  729. 16,
  730. )
  731. # _z = int(
  732. # remove_whitespace(
  733. # """
  734. # 15F75CAF668077F7E85B42EB01F0A81FF56ECD6191D55CB82B7D861458A18F
  735. # EFC3E5AB7496F3C7B1"""
  736. # ),
  737. # 16,
  738. # )
  739. _Gx = int(
  740. remove_whitespace(
  741. """
  742. 925BE9FB01AFC6FB4D3E7D4990010F813408AB106C4F09CB7EE07868CC136F
  743. FF3357F624A21BED52"""
  744. ),
  745. 16,
  746. )
  747. _Gy = int(
  748. remove_whitespace(
  749. """
  750. 63BA3A7A27483EBF6671DBEF7ABB30EBEE084E58A0B077AD42A5A0989D1EE7
  751. 1B1B9BC0455FB0D2C3"""
  752. ),
  753. 16,
  754. )
  755. _q = int(
  756. remove_whitespace(
  757. """
  758. D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658
  759. E98691555B44C59311"""
  760. ),
  761. 16,
  762. )
  763. curve_brainpoolp320t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  764. generator_brainpoolp320t1 = ellipticcurve.PointJacobi(
  765. curve_brainpoolp320t1, _Gx, _Gy, 1, _q, generator=True
  766. )
  767. # Brainpool P-384-r1
  768. _a = int(
  769. remove_whitespace(
  770. """
  771. 7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F9
  772. 0F8AA5814A503AD4EB04A8C7DD22CE2826"""
  773. ),
  774. 16,
  775. )
  776. _b = int(
  777. remove_whitespace(
  778. """
  779. 04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62
  780. D57CB4390295DBC9943AB78696FA504C11"""
  781. ),
  782. 16,
  783. )
  784. _p = int(
  785. remove_whitespace(
  786. """
  787. 8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711
  788. 23ACD3A729901D1A71874700133107EC53"""
  789. ),
  790. 16,
  791. )
  792. _Gx = int(
  793. remove_whitespace(
  794. """
  795. 1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10
  796. E8E826E03436D646AAEF87B2E247D4AF1E"""
  797. ),
  798. 16,
  799. )
  800. _Gy = int(
  801. remove_whitespace(
  802. """
  803. 8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF991292
  804. 80E4646217791811142820341263C5315"""
  805. ),
  806. 16,
  807. )
  808. _q = int(
  809. remove_whitespace(
  810. """
  811. 8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425
  812. A7CF3AB6AF6B7FC3103B883202E9046565"""
  813. ),
  814. 16,
  815. )
  816. curve_brainpoolp384r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  817. generator_brainpoolp384r1 = ellipticcurve.PointJacobi(
  818. curve_brainpoolp384r1, _Gx, _Gy, 1, _q, generator=True
  819. )
  820. _a = int(
  821. remove_whitespace(
  822. """
  823. 8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711
  824. 23ACD3A729901D1A71874700133107EC50"""
  825. ),
  826. 16,
  827. )
  828. _b = int(
  829. remove_whitespace(
  830. """
  831. 7F519EADA7BDA81BD826DBA647910F8C4B9346ED8CCDC64E4B1ABD11756DCE
  832. 1D2074AA263B88805CED70355A33B471EE"""
  833. ),
  834. 16,
  835. )
  836. # _z = int(
  837. # remove_whitespace(
  838. # """
  839. # 41DFE8DD399331F7166A66076734A89CD0D2BCDB7D068E44E1F378F41ECBAE
  840. # 97D2D63DBC87BCCDDCCC5DA39E8589291C"""
  841. # ),
  842. # 16,
  843. # )
  844. _Gx = int(
  845. remove_whitespace(
  846. """
  847. 18DE98B02DB9A306F2AFCD7235F72A819B80AB12EBD653172476FECD462AAB
  848. FFC4FF191B946A5F54D8D0AA2F418808CC"""
  849. ),
  850. 16,
  851. )
  852. _Gy = int(
  853. remove_whitespace(
  854. """
  855. 25AB056962D30651A114AFD2755AD336747F93475B7A1FCA3B88F2B6A208CC
  856. FE469408584DC2B2912675BF5B9E582928"""
  857. ),
  858. 16,
  859. )
  860. _q = int(
  861. remove_whitespace(
  862. """
  863. 8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425
  864. A7CF3AB6AF6B7FC3103B883202E9046565"""
  865. ),
  866. 16,
  867. )
  868. curve_brainpoolp384t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  869. generator_brainpoolp384t1 = ellipticcurve.PointJacobi(
  870. curve_brainpoolp384t1, _Gx, _Gy, 1, _q, generator=True
  871. )
  872. # Brainpool P-512-r1
  873. _a = int(
  874. remove_whitespace(
  875. """
  876. 7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863
  877. BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA"""
  878. ),
  879. 16,
  880. )
  881. _b = int(
  882. remove_whitespace(
  883. """
  884. 3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117
  885. A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723"""
  886. ),
  887. 16,
  888. )
  889. _p = int(
  890. remove_whitespace(
  891. """
  892. AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308
  893. 717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3"""
  894. ),
  895. 16,
  896. )
  897. _Gx = int(
  898. remove_whitespace(
  899. """
  900. 81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009
  901. 8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822"""
  902. ),
  903. 16,
  904. )
  905. _Gy = int(
  906. remove_whitespace(
  907. """
  908. 7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81
  909. 11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"""
  910. ),
  911. 16,
  912. )
  913. _q = int(
  914. remove_whitespace(
  915. """
  916. AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308
  917. 70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"""
  918. ),
  919. 16,
  920. )
  921. curve_brainpoolp512r1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  922. generator_brainpoolp512r1 = ellipticcurve.PointJacobi(
  923. curve_brainpoolp512r1, _Gx, _Gy, 1, _q, generator=True
  924. )
  925. # Brainpool P-512-t1
  926. _a = int(
  927. remove_whitespace(
  928. """
  929. AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308
  930. 717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F0"""
  931. ),
  932. 16,
  933. )
  934. _b = int(
  935. remove_whitespace(
  936. """
  937. 7CBBBCF9441CFAB76E1890E46884EAE321F70C0BCB4981527897504BEC3E36
  938. A62BCDFA2304976540F6450085F2DAE145C22553B465763689180EA2571867423E"""
  939. ),
  940. 16,
  941. )
  942. # _z = int(
  943. # remove_whitespace(
  944. # """
  945. # 12EE58E6764838B69782136F0F2D3BA06E27695716054092E60A80BEDB212B
  946. # 64E585D90BCE13761F85C3F1D2A64E3BE8FEA2220F01EBA5EEB0F35DBD29D922AB"""
  947. # ),
  948. # 16,
  949. # )
  950. _Gx = int(
  951. remove_whitespace(
  952. """
  953. 640ECE5C12788717B9C1BA06CBC2A6FEBA85842458C56DDE9DB1758D39C031
  954. 3D82BA51735CDB3EA499AA77A7D6943A64F7A3F25FE26F06B51BAA2696FA9035DA"""
  955. ),
  956. 16,
  957. )
  958. _Gy = int(
  959. remove_whitespace(
  960. """
  961. 5B534BD595F5AF0FA2C892376C84ACE1BB4E3019B71634C01131159CAE03CE
  962. E9D9932184BEEF216BD71DF2DADF86A627306ECFF96DBB8BACE198B61E00F8B332"""
  963. ),
  964. 16,
  965. )
  966. _q = int(
  967. remove_whitespace(
  968. """
  969. AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308
  970. 70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069"""
  971. ),
  972. 16,
  973. )
  974. curve_brainpoolp512t1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
  975. generator_brainpoolp512t1 = ellipticcurve.PointJacobi(
  976. curve_brainpoolp512t1, _Gx, _Gy, 1, _q, generator=True
  977. )