test_ECC.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  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. import unittest
  31. import time
  32. from Cryptodome.SelfTest.st_common import list_test_cases
  33. from Cryptodome.SelfTest.loader import load_test_vectors
  34. from Cryptodome.PublicKey import ECC
  35. from Cryptodome.PublicKey.ECC import EccPoint, _curves, EccKey
  36. from Cryptodome.Math.Numbers import Integer
  37. class TestEccPoint(unittest.TestCase):
  38. def test_mix(self):
  39. p1 = ECC.generate(curve='P-256').pointQ
  40. p2 = ECC.generate(curve='P-384').pointQ
  41. try:
  42. p1 + p2
  43. assert(False)
  44. except ValueError as e:
  45. assert "not on the same curve" in str(e)
  46. try:
  47. p1 += p2
  48. assert(False)
  49. except ValueError as e:
  50. assert "not on the same curve" in str(e)
  51. def test_repr(self):
  52. p1 = ECC.construct(curve='P-256',
  53. d=75467964919405407085864614198393977741148485328036093939970922195112333446269,
  54. point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381,
  55. point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389)
  56. self.assertEqual(repr(p1), "EccKey(curve='NIST P-256', point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381, point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389, d=75467964919405407085864614198393977741148485328036093939970922195112333446269)")
  57. class TestEccPoint_NIST_P256(unittest.TestCase):
  58. """Tests defined in section 4.3 of https://www.nsa.gov/ia/_files/nist-routines.pdf"""
  59. pointS = EccPoint(
  60. 0xde2444bebc8d36e682edd27e0f271508617519b3221a8fa0b77cab3989da97c9,
  61. 0xc093ae7ff36e5380fc01a5aad1e66659702de80f53cec576b6350b243042a256)
  62. pointT = EccPoint(
  63. 0x55a8b00f8da1d44e62f6b3b25316212e39540dc861c89575bb8cf92e35e0986b,
  64. 0x5421c3209c2d6c704835d82ac4c3dd90f61a8a52598b9e7ab656e9d8c8b24316)
  65. def test_set(self):
  66. pointW = EccPoint(0, 0)
  67. pointW.set(self.pointS)
  68. self.assertEqual(pointW, self.pointS)
  69. def test_copy(self):
  70. pointW = self.pointS.copy()
  71. self.assertEqual(pointW, self.pointS)
  72. pointW.set(self.pointT)
  73. self.assertEqual(pointW, self.pointT)
  74. self.assertNotEqual(self.pointS, self.pointT)
  75. def test_negate(self):
  76. negS = -self.pointS
  77. sum = self.pointS + negS
  78. self.assertEqual(sum, self.pointS.point_at_infinity())
  79. def test_addition(self):
  80. pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
  81. pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
  82. pointR = self.pointS + self.pointT
  83. self.assertEqual(pointR.x, pointRx)
  84. self.assertEqual(pointR.y, pointRy)
  85. pai = pointR.point_at_infinity()
  86. # S + 0
  87. pointR = self.pointS + pai
  88. self.assertEqual(pointR, self.pointS)
  89. # 0 + S
  90. pointR = pai + self.pointS
  91. self.assertEqual(pointR, self.pointS)
  92. # 0 + 0
  93. pointR = pai + pai
  94. self.assertEqual(pointR, pai)
  95. def test_inplace_addition(self):
  96. pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
  97. pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
  98. pointR = self.pointS.copy()
  99. pointR += self.pointT
  100. self.assertEqual(pointR.x, pointRx)
  101. self.assertEqual(pointR.y, pointRy)
  102. pai = pointR.point_at_infinity()
  103. # S + 0
  104. pointR = self.pointS.copy()
  105. pointR += pai
  106. self.assertEqual(pointR, self.pointS)
  107. # 0 + S
  108. pointR = pai.copy()
  109. pointR += self.pointS
  110. self.assertEqual(pointR, self.pointS)
  111. # 0 + 0
  112. pointR = pai.copy()
  113. pointR += pai
  114. self.assertEqual(pointR, pai)
  115. def test_doubling(self):
  116. pointRx = 0x7669e6901606ee3ba1a8eef1e0024c33df6c22f3b17481b82a860ffcdb6127b0
  117. pointRy = 0xfa878162187a54f6c39f6ee0072f33de389ef3eecd03023de10ca2c1db61d0c7
  118. pointR = self.pointS.copy()
  119. pointR.double()
  120. self.assertEqual(pointR.x, pointRx)
  121. self.assertEqual(pointR.y, pointRy)
  122. # 2*0
  123. pai = self.pointS.point_at_infinity()
  124. pointR = pai.copy()
  125. pointR.double()
  126. self.assertEqual(pointR, pai)
  127. # S + S
  128. pointR = self.pointS.copy()
  129. pointR += pointR
  130. self.assertEqual(pointR.x, pointRx)
  131. self.assertEqual(pointR.y, pointRy)
  132. def test_scalar_multiply(self):
  133. d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
  134. pointRx = 0x51d08d5f2d4278882946d88d83c97d11e62becc3cfc18bedacc89ba34eeca03f
  135. pointRy = 0x75ee68eb8bf626aa5b673ab51f6e744e06f8fcf8a6c0cf3035beca956a7b41d5
  136. pointR = self.pointS * d
  137. self.assertEqual(pointR.x, pointRx)
  138. self.assertEqual(pointR.y, pointRy)
  139. # 0*S
  140. pai = self.pointS.point_at_infinity()
  141. pointR = self.pointS * 0
  142. self.assertEqual(pointR, pai)
  143. # -1*S
  144. self.assertRaises(ValueError, lambda: self.pointS * -1)
  145. # Reverse order
  146. pointR = d * self.pointS
  147. self.assertEqual(pointR.x, pointRx)
  148. self.assertEqual(pointR.y, pointRy)
  149. pointR = Integer(d) * self.pointS
  150. self.assertEqual(pointR.x, pointRx)
  151. self.assertEqual(pointR.y, pointRy)
  152. def test_joing_scalar_multiply(self):
  153. d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
  154. e = 0xd37f628ece72a462f0145cbefe3f0b355ee8332d37acdd83a358016aea029db7
  155. pointRx = 0xd867b4679221009234939221b8046245efcf58413daacbeff857b8588341f6b8
  156. pointRy = 0xf2504055c03cede12d22720dad69c745106b6607ec7e50dd35d54bd80f615275
  157. t = self.pointS * d
  158. pointR = self.pointS * d + self.pointT * e
  159. self.assertEqual(pointR.x, pointRx)
  160. self.assertEqual(pointR.y, pointRy)
  161. def test_sizes(self):
  162. self.assertEqual(self.pointS.size_in_bits(), 256)
  163. self.assertEqual(self.pointS.size_in_bytes(), 32)
  164. class TestEccPoint_NIST_P384(unittest.TestCase):
  165. """Tests defined in section 4.4 of https://www.nsa.gov/ia/_files/nist-routines.pdf"""
  166. pointS = EccPoint(
  167. 0xfba203b81bbd23f2b3be971cc23997e1ae4d89e69cb6f92385dda82768ada415ebab4167459da98e62b1332d1e73cb0e,
  168. 0x5ffedbaefdeba603e7923e06cdb5d0c65b22301429293376d5c6944e3fa6259f162b4788de6987fd59aed5e4b5285e45,
  169. "p384")
  170. pointT = EccPoint(
  171. 0xaacc05202e7fda6fc73d82f0a66220527da8117ee8f8330ead7d20ee6f255f582d8bd38c5a7f2b40bcdb68ba13d81051,
  172. 0x84009a263fefba7c2c57cffa5db3634d286131afc0fca8d25afa22a7b5dce0d9470da89233cee178592f49b6fecb5092,
  173. "p384")
  174. def test_set(self):
  175. pointW = EccPoint(0, 0, "p384")
  176. pointW.set(self.pointS)
  177. self.assertEqual(pointW, self.pointS)
  178. def test_copy(self):
  179. pointW = self.pointS.copy()
  180. self.assertEqual(pointW, self.pointS)
  181. pointW.set(self.pointT)
  182. self.assertEqual(pointW, self.pointT)
  183. self.assertNotEqual(self.pointS, self.pointT)
  184. def test_negate(self):
  185. negS = -self.pointS
  186. sum = self.pointS + negS
  187. self.assertEqual(sum, self.pointS.point_at_infinity())
  188. def test_addition(self):
  189. pointRx = 0x12dc5ce7acdfc5844d939f40b4df012e68f865b89c3213ba97090a247a2fc009075cf471cd2e85c489979b65ee0b5eed
  190. pointRy = 0x167312e58fe0c0afa248f2854e3cddcb557f983b3189b67f21eee01341e7e9fe67f6ee81b36988efa406945c8804a4b0
  191. pointR = self.pointS + self.pointT
  192. self.assertEqual(pointR.x, pointRx)
  193. self.assertEqual(pointR.y, pointRy)
  194. pai = pointR.point_at_infinity()
  195. # S + 0
  196. pointR = self.pointS + pai
  197. self.assertEqual(pointR, self.pointS)
  198. # 0 + S
  199. pointR = pai + self.pointS
  200. self.assertEqual(pointR, self.pointS)
  201. # 0 + 0
  202. pointR = pai + pai
  203. self.assertEqual(pointR, pai)
  204. def _test_inplace_addition(self):
  205. pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
  206. pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
  207. pointR = self.pointS.copy()
  208. pointR += self.pointT
  209. self.assertEqual(pointR.x, pointRx)
  210. self.assertEqual(pointR.y, pointRy)
  211. pai = pointR.point_at_infinity()
  212. # S + 0
  213. pointR = self.pointS.copy()
  214. pointR += pai
  215. self.assertEqual(pointR, self.pointS)
  216. # 0 + S
  217. pointR = pai.copy()
  218. pointR += self.pointS
  219. self.assertEqual(pointR, self.pointS)
  220. # 0 + 0
  221. pointR = pai.copy()
  222. pointR += pai
  223. self.assertEqual(pointR, pai)
  224. def test_doubling(self):
  225. pointRx = 0x2a2111b1e0aa8b2fc5a1975516bc4d58017ff96b25e1bdff3c229d5fac3bacc319dcbec29f9478f42dee597b4641504c
  226. pointRy = 0xfa2e3d9dc84db8954ce8085ef28d7184fddfd1344b4d4797343af9b5f9d837520b450f726443e4114bd4e5bdb2f65ddd
  227. pointR = self.pointS.copy()
  228. pointR.double()
  229. self.assertEqual(pointR.x, pointRx)
  230. self.assertEqual(pointR.y, pointRy)
  231. # 2*0
  232. pai = self.pointS.point_at_infinity()
  233. pointR = pai.copy()
  234. pointR.double()
  235. self.assertEqual(pointR, pai)
  236. # S + S
  237. pointR = self.pointS.copy()
  238. pointR += pointR
  239. self.assertEqual(pointR.x, pointRx)
  240. self.assertEqual(pointR.y, pointRy)
  241. def test_scalar_multiply(self):
  242. d = 0xa4ebcae5a665983493ab3e626085a24c104311a761b5a8fdac052ed1f111a5c44f76f45659d2d111a61b5fdd97583480
  243. pointRx = 0xe4f77e7ffeb7f0958910e3a680d677a477191df166160ff7ef6bb5261f791aa7b45e3e653d151b95dad3d93ca0290ef2
  244. pointRy = 0xac7dee41d8c5f4a7d5836960a773cfc1376289d3373f8cf7417b0c6207ac32e913856612fc9ff2e357eb2ee05cf9667f
  245. pointR = self.pointS * d
  246. self.assertEqual(pointR.x, pointRx)
  247. self.assertEqual(pointR.y, pointRy)
  248. # 0*S
  249. pai = self.pointS.point_at_infinity()
  250. pointR = self.pointS * 0
  251. self.assertEqual(pointR, pai)
  252. # -1*S
  253. self.assertRaises(ValueError, lambda: self.pointS * -1)
  254. def test_joing_scalar_multiply(self):
  255. d = 0xa4ebcae5a665983493ab3e626085a24c104311a761b5a8fdac052ed1f111a5c44f76f45659d2d111a61b5fdd97583480
  256. e = 0xafcf88119a3a76c87acbd6008e1349b29f4ba9aa0e12ce89bcfcae2180b38d81ab8cf15095301a182afbc6893e75385d
  257. pointRx = 0x917ea28bcd641741ae5d18c2f1bd917ba68d34f0f0577387dc81260462aea60e2417b8bdc5d954fc729d211db23a02dc
  258. pointRy = 0x1a29f7ce6d074654d77b40888c73e92546c8f16a5ff6bcbd307f758d4aee684beff26f6742f597e2585c86da908f7186
  259. t = self.pointS * d
  260. pointR = self.pointS * d + self.pointT * e
  261. self.assertEqual(pointR.x, pointRx)
  262. self.assertEqual(pointR.y, pointRy)
  263. def test_sizes(self):
  264. self.assertEqual(self.pointS.size_in_bits(), 384)
  265. self.assertEqual(self.pointS.size_in_bytes(), 48)
  266. class TestEccPoint_NIST_P521(unittest.TestCase):
  267. """Tests defined in section 4.5 of https://www.nsa.gov/ia/_files/nist-routines.pdf"""
  268. pointS = EccPoint(
  269. 0x000001d5c693f66c08ed03ad0f031f937443458f601fd098d3d0227b4bf62873af50740b0bb84aa157fc847bcf8dc16a8b2b8bfd8e2d0a7d39af04b089930ef6dad5c1b4,
  270. 0x00000144b7770963c63a39248865ff36b074151eac33549b224af5c8664c54012b818ed037b2b7c1a63ac89ebaa11e07db89fcee5b556e49764ee3fa66ea7ae61ac01823,
  271. "p521")
  272. pointT = EccPoint(
  273. 0x000000f411f2ac2eb971a267b80297ba67c322dba4bb21cec8b70073bf88fc1ca5fde3ba09e5df6d39acb2c0762c03d7bc224a3e197feaf760d6324006fe3be9a548c7d5,
  274. 0x000001fdf842769c707c93c630df6d02eff399a06f1b36fb9684f0b373ed064889629abb92b1ae328fdb45534268384943f0e9222afe03259b32274d35d1b9584c65e305,
  275. "p521")
  276. def test_set(self):
  277. pointW = EccPoint(0, 0)
  278. pointW.set(self.pointS)
  279. self.assertEqual(pointW, self.pointS)
  280. def test_copy(self):
  281. pointW = self.pointS.copy()
  282. self.assertEqual(pointW, self.pointS)
  283. pointW.set(self.pointT)
  284. self.assertEqual(pointW, self.pointT)
  285. self.assertNotEqual(self.pointS, self.pointT)
  286. def test_negate(self):
  287. negS = -self.pointS
  288. sum = self.pointS + negS
  289. self.assertEqual(sum, self.pointS.point_at_infinity())
  290. def test_addition(self):
  291. pointRx = 0x000001264ae115ba9cbc2ee56e6f0059e24b52c8046321602c59a339cfb757c89a59c358a9a8e1f86d384b3f3b255ea3f73670c6dc9f45d46b6a196dc37bbe0f6b2dd9e9
  292. pointRy = 0x00000062a9c72b8f9f88a271690bfa017a6466c31b9cadc2fc544744aeb817072349cfddc5ad0e81b03f1897bd9c8c6efbdf68237dc3bb00445979fb373b20c9a967ac55
  293. pointR = self.pointS + self.pointT
  294. self.assertEqual(pointR.x, pointRx)
  295. self.assertEqual(pointR.y, pointRy)
  296. pai = pointR.point_at_infinity()
  297. # S + 0
  298. pointR = self.pointS + pai
  299. self.assertEqual(pointR, self.pointS)
  300. # 0 + S
  301. pointR = pai + self.pointS
  302. self.assertEqual(pointR, self.pointS)
  303. # 0 + 0
  304. pointR = pai + pai
  305. self.assertEqual(pointR, pai)
  306. def test_inplace_addition(self):
  307. pointRx = 0x000001264ae115ba9cbc2ee56e6f0059e24b52c8046321602c59a339cfb757c89a59c358a9a8e1f86d384b3f3b255ea3f73670c6dc9f45d46b6a196dc37bbe0f6b2dd9e9
  308. pointRy = 0x00000062a9c72b8f9f88a271690bfa017a6466c31b9cadc2fc544744aeb817072349cfddc5ad0e81b03f1897bd9c8c6efbdf68237dc3bb00445979fb373b20c9a967ac55
  309. pointR = self.pointS.copy()
  310. pointR += self.pointT
  311. self.assertEqual(pointR.x, pointRx)
  312. self.assertEqual(pointR.y, pointRy)
  313. pai = pointR.point_at_infinity()
  314. # S + 0
  315. pointR = self.pointS.copy()
  316. pointR += pai
  317. self.assertEqual(pointR, self.pointS)
  318. # 0 + S
  319. pointR = pai.copy()
  320. pointR += self.pointS
  321. self.assertEqual(pointR, self.pointS)
  322. # 0 + 0
  323. pointR = pai.copy()
  324. pointR += pai
  325. self.assertEqual(pointR, pai)
  326. def test_doubling(self):
  327. pointRx = 0x0000012879442f2450c119e7119a5f738be1f1eba9e9d7c6cf41b325d9ce6d643106e9d61124a91a96bcf201305a9dee55fa79136dc700831e54c3ca4ff2646bd3c36bc6
  328. pointRy = 0x0000019864a8b8855c2479cbefe375ae553e2393271ed36fadfc4494fc0583f6bd03598896f39854abeae5f9a6515a021e2c0eef139e71de610143f53382f4104dccb543
  329. pointR = self.pointS.copy()
  330. pointR.double()
  331. self.assertEqual(pointR.x, pointRx)
  332. self.assertEqual(pointR.y, pointRy)
  333. # 2*0
  334. pai = self.pointS.point_at_infinity()
  335. pointR = pai.copy()
  336. pointR.double()
  337. self.assertEqual(pointR, pai)
  338. # S + S
  339. pointR = self.pointS.copy()
  340. pointR += pointR
  341. self.assertEqual(pointR.x, pointRx)
  342. self.assertEqual(pointR.y, pointRy)
  343. def test_scalar_multiply(self):
  344. d = 0x000001eb7f81785c9629f136a7e8f8c674957109735554111a2a866fa5a166699419bfa9936c78b62653964df0d6da940a695c7294d41b2d6600de6dfcf0edcfc89fdcb1
  345. pointRx = 0x00000091b15d09d0ca0353f8f96b93cdb13497b0a4bb582ae9ebefa35eee61bf7b7d041b8ec34c6c00c0c0671c4ae063318fb75be87af4fe859608c95f0ab4774f8c95bb
  346. pointRy = 0x00000130f8f8b5e1abb4dd94f6baaf654a2d5810411e77b7423965e0c7fd79ec1ae563c207bd255ee9828eb7a03fed565240d2cc80ddd2cecbb2eb50f0951f75ad87977f
  347. pointR = self.pointS * d
  348. self.assertEqual(pointR.x, pointRx)
  349. self.assertEqual(pointR.y, pointRy)
  350. # 0*S
  351. pai = self.pointS.point_at_infinity()
  352. pointR = self.pointS * 0
  353. self.assertEqual(pointR, pai)
  354. # -1*S
  355. self.assertRaises(ValueError, lambda: self.pointS * -1)
  356. def test_joing_scalar_multiply(self):
  357. d = 0x000001eb7f81785c9629f136a7e8f8c674957109735554111a2a866fa5a166699419bfa9936c78b62653964df0d6da940a695c7294d41b2d6600de6dfcf0edcfc89fdcb1
  358. e = 0x00000137e6b73d38f153c3a7575615812608f2bab3229c92e21c0d1c83cfad9261dbb17bb77a63682000031b9122c2f0cdab2af72314be95254de4291a8f85f7c70412e3
  359. pointRx = 0x0000009d3802642b3bea152beb9e05fba247790f7fc168072d363340133402f2585588dc1385d40ebcb8552f8db02b23d687cae46185b27528adb1bf9729716e4eba653d
  360. pointRy = 0x0000000fe44344e79da6f49d87c1063744e5957d9ac0a505bafa8281c9ce9ff25ad53f8da084a2deb0923e46501de5797850c61b229023dd9cf7fc7f04cd35ebb026d89d
  361. t = self.pointS * d
  362. pointR = self.pointS * d
  363. pointR += self.pointT * e
  364. self.assertEqual(pointR.x, pointRx)
  365. self.assertEqual(pointR.y, pointRy)
  366. def test_sizes(self):
  367. self.assertEqual(self.pointS.size_in_bits(), 521)
  368. self.assertEqual(self.pointS.size_in_bytes(), 66)
  369. class TestEccPoint_PAI_P256(unittest.TestCase):
  370. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  371. curve = _curves['p256']
  372. pointG = EccPoint(curve.Gx, curve.Gy, "p256")
  373. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  374. "point-at-infinity.org-P256.txt",
  375. "P-256 tests from point-at-infinity.org",
  376. {"k": lambda k: int(k),
  377. "x": lambda x: int(x, 16),
  378. "y": lambda y: int(y, 16)}) or []
  379. for tv in tv_pai:
  380. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  381. result = self.pointG * scalar
  382. self.assertEqual(result.x, x)
  383. self.assertEqual(result.y, y)
  384. setattr(TestEccPoint_PAI_P256, "test_%d" % tv.count, new_test)
  385. class TestEccPoint_PAI_P384(unittest.TestCase):
  386. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  387. curve = _curves['p384']
  388. pointG = EccPoint(curve.Gx, curve.Gy, "p384")
  389. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  390. "point-at-infinity.org-P384.txt",
  391. "P-384 tests from point-at-infinity.org",
  392. {"k" : lambda k: int(k),
  393. "x" : lambda x: int(x, 16),
  394. "y" : lambda y: int(y, 16)}) or []
  395. for tv in tv_pai:
  396. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  397. result = self.pointG * scalar
  398. self.assertEqual(result.x, x)
  399. self.assertEqual(result.y, y)
  400. setattr(TestEccPoint_PAI_P384, "test_%d" % tv.count, new_test)
  401. class TestEccPoint_PAI_P521(unittest.TestCase):
  402. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  403. curve = _curves['p521']
  404. pointG = EccPoint(curve.Gx, curve.Gy, "p521")
  405. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  406. "point-at-infinity.org-P521.txt",
  407. "P-521 tests from point-at-infinity.org",
  408. {"k": lambda k: int(k),
  409. "x": lambda x: int(x, 16),
  410. "y": lambda y: int(y, 16)}) or []
  411. for tv in tv_pai:
  412. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  413. result = self.pointG * scalar
  414. self.assertEqual(result.x, x)
  415. self.assertEqual(result.y, y)
  416. setattr(TestEccPoint_PAI_P521, "test_%d" % tv.count, new_test)
  417. class TestEccKey_P256(unittest.TestCase):
  418. def test_private_key(self):
  419. key = EccKey(curve="P-256", d=1)
  420. self.assertEqual(key.d, 1)
  421. self.failUnless(key.has_private())
  422. self.assertEqual(key.pointQ.x, _curves['p256'].Gx)
  423. self.assertEqual(key.pointQ.y, _curves['p256'].Gy)
  424. point = EccPoint(_curves['p256'].Gx, _curves['p256'].Gy)
  425. key = EccKey(curve="P-256", d=1, point=point)
  426. self.assertEqual(key.d, 1)
  427. self.failUnless(key.has_private())
  428. self.assertEqual(key.pointQ, point)
  429. # Other names
  430. key = EccKey(curve="secp256r1", d=1)
  431. key = EccKey(curve="prime256v1", d=1)
  432. def test_public_key(self):
  433. point = EccPoint(_curves['p256'].Gx, _curves['p256'].Gy)
  434. key = EccKey(curve="P-256", point=point)
  435. self.failIf(key.has_private())
  436. self.assertEqual(key.pointQ, point)
  437. def test_public_key_derived(self):
  438. priv_key = EccKey(curve="P-256", d=3)
  439. pub_key = priv_key.public_key()
  440. self.failIf(pub_key.has_private())
  441. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  442. def test_invalid_curve(self):
  443. self.assertRaises(ValueError, lambda: EccKey(curve="P-257", d=1))
  444. def test_invalid_d(self):
  445. self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=0))
  446. self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=_curves['p256'].order))
  447. def test_equality(self):
  448. private_key = ECC.construct(d=3, curve="P-256")
  449. private_key2 = ECC.construct(d=3, curve="P-256")
  450. private_key3 = ECC.construct(d=4, curve="P-256")
  451. public_key = private_key.public_key()
  452. public_key2 = private_key2.public_key()
  453. public_key3 = private_key3.public_key()
  454. self.assertEqual(private_key, private_key2)
  455. self.assertNotEqual(private_key, private_key3)
  456. self.assertEqual(public_key, public_key2)
  457. self.assertNotEqual(public_key, public_key3)
  458. self.assertNotEqual(public_key, private_key)
  459. class TestEccKey_P384(unittest.TestCase):
  460. def test_private_key(self):
  461. p384 = _curves['p384']
  462. key = EccKey(curve="P-384", d=1)
  463. self.assertEqual(key.d, 1)
  464. self.failUnless(key.has_private())
  465. self.assertEqual(key.pointQ.x, p384.Gx)
  466. self.assertEqual(key.pointQ.y, p384.Gy)
  467. point = EccPoint(p384.Gx, p384.Gy, "p384")
  468. key = EccKey(curve="P-384", d=1, point=point)
  469. self.assertEqual(key.d, 1)
  470. self.failUnless(key.has_private())
  471. self.assertEqual(key.pointQ, point)
  472. # Other names
  473. key = EccKey(curve="p384", d=1)
  474. key = EccKey(curve="secp384r1", d=1)
  475. key = EccKey(curve="prime384v1", d=1)
  476. def test_public_key(self):
  477. p384 = _curves['p384']
  478. point = EccPoint(p384.Gx, p384.Gy, 'p384')
  479. key = EccKey(curve="P-384", point=point)
  480. self.failIf(key.has_private())
  481. self.assertEqual(key.pointQ, point)
  482. def test_public_key_derived(self):
  483. priv_key = EccKey(curve="P-384", d=3)
  484. pub_key = priv_key.public_key()
  485. self.failIf(pub_key.has_private())
  486. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  487. def test_invalid_curve(self):
  488. self.assertRaises(ValueError, lambda: EccKey(curve="P-385", d=1))
  489. def test_invalid_d(self):
  490. self.assertRaises(ValueError, lambda: EccKey(curve="P-384", d=0))
  491. self.assertRaises(ValueError, lambda: EccKey(curve="P-384",
  492. d=_curves['p384'].order))
  493. def test_equality(self):
  494. private_key = ECC.construct(d=3, curve="P-384")
  495. private_key2 = ECC.construct(d=3, curve="P-384")
  496. private_key3 = ECC.construct(d=4, curve="P-384")
  497. public_key = private_key.public_key()
  498. public_key2 = private_key2.public_key()
  499. public_key3 = private_key3.public_key()
  500. self.assertEqual(private_key, private_key2)
  501. self.assertNotEqual(private_key, private_key3)
  502. self.assertEqual(public_key, public_key2)
  503. self.assertNotEqual(public_key, public_key3)
  504. self.assertNotEqual(public_key, private_key)
  505. class TestEccKey_P521(unittest.TestCase):
  506. def test_private_key(self):
  507. p521 = _curves['p521']
  508. key = EccKey(curve="P-521", d=1)
  509. self.assertEqual(key.d, 1)
  510. self.failUnless(key.has_private())
  511. self.assertEqual(key.pointQ.x, p521.Gx)
  512. self.assertEqual(key.pointQ.y, p521.Gy)
  513. point = EccPoint(p521.Gx, p521.Gy, "p521")
  514. key = EccKey(curve="P-521", d=1, point=point)
  515. self.assertEqual(key.d, 1)
  516. self.failUnless(key.has_private())
  517. self.assertEqual(key.pointQ, point)
  518. # Other names
  519. key = EccKey(curve="p521", d=1)
  520. key = EccKey(curve="secp521r1", d=1)
  521. key = EccKey(curve="prime521v1", d=1)
  522. def test_public_key(self):
  523. p521 = _curves['p521']
  524. point = EccPoint(p521.Gx, p521.Gy, 'p521')
  525. key = EccKey(curve="P-384", point=point)
  526. self.failIf(key.has_private())
  527. self.assertEqual(key.pointQ, point)
  528. def test_public_key_derived(self):
  529. priv_key = EccKey(curve="P-521", d=3)
  530. pub_key = priv_key.public_key()
  531. self.failIf(pub_key.has_private())
  532. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  533. def test_invalid_curve(self):
  534. self.assertRaises(ValueError, lambda: EccKey(curve="P-522", d=1))
  535. def test_invalid_d(self):
  536. self.assertRaises(ValueError, lambda: EccKey(curve="P-521", d=0))
  537. self.assertRaises(ValueError, lambda: EccKey(curve="P-521",
  538. d=_curves['p521'].order))
  539. def test_equality(self):
  540. private_key = ECC.construct(d=3, curve="P-521")
  541. private_key2 = ECC.construct(d=3, curve="P-521")
  542. private_key3 = ECC.construct(d=4, curve="P-521")
  543. public_key = private_key.public_key()
  544. public_key2 = private_key2.public_key()
  545. public_key3 = private_key3.public_key()
  546. self.assertEqual(private_key, private_key2)
  547. self.assertNotEqual(private_key, private_key3)
  548. self.assertEqual(public_key, public_key2)
  549. self.assertNotEqual(public_key, public_key3)
  550. self.assertNotEqual(public_key, private_key)
  551. class TestEccModule_P256(unittest.TestCase):
  552. def test_generate(self):
  553. key = ECC.generate(curve="P-256")
  554. self.failUnless(key.has_private())
  555. self.assertEqual(key.pointQ, EccPoint(_curves['p256'].Gx,
  556. _curves['p256'].Gy) * key.d,
  557. "p256")
  558. # Other names
  559. ECC.generate(curve="secp256r1")
  560. ECC.generate(curve="prime256v1")
  561. def test_construct(self):
  562. key = ECC.construct(curve="P-256", d=1)
  563. self.failUnless(key.has_private())
  564. self.assertEqual(key.pointQ, _curves['p256'].G)
  565. key = ECC.construct(curve="P-256", point_x=_curves['p256'].Gx,
  566. point_y=_curves['p256'].Gy)
  567. self.failIf(key.has_private())
  568. self.assertEqual(key.pointQ, _curves['p256'].G)
  569. # Other names
  570. ECC.construct(curve="p256", d=1)
  571. ECC.construct(curve="secp256r1", d=1)
  572. ECC.construct(curve="prime256v1", d=1)
  573. def test_negative_construct(self):
  574. coord = dict(point_x=10, point_y=4)
  575. coordG = dict(point_x=_curves['p256'].Gx, point_y=_curves['p256'].Gy)
  576. self.assertRaises(ValueError, ECC.construct, curve="P-256", **coord)
  577. self.assertRaises(ValueError, ECC.construct, curve="P-256", d=2, **coordG)
  578. class TestEccModule_P384(unittest.TestCase):
  579. def test_generate(self):
  580. curve = _curves['p384']
  581. key = ECC.generate(curve="P-384")
  582. self.failUnless(key.has_private())
  583. self.assertEqual(key.pointQ, EccPoint(curve.Gx, curve.Gy, "p384") * key.d)
  584. # Other names
  585. ECC.generate(curve="secp384r1")
  586. ECC.generate(curve="prime384v1")
  587. def test_construct(self):
  588. curve = _curves['p384']
  589. key = ECC.construct(curve="P-384", d=1)
  590. self.failUnless(key.has_private())
  591. self.assertEqual(key.pointQ, _curves['p384'].G)
  592. key = ECC.construct(curve="P-384", point_x=curve.Gx, point_y=curve.Gy)
  593. self.failIf(key.has_private())
  594. self.assertEqual(key.pointQ, curve.G)
  595. # Other names
  596. ECC.construct(curve="p384", d=1)
  597. ECC.construct(curve="secp384r1", d=1)
  598. ECC.construct(curve="prime384v1", d=1)
  599. def test_negative_construct(self):
  600. coord = dict(point_x=10, point_y=4)
  601. coordG = dict(point_x=_curves['p384'].Gx, point_y=_curves['p384'].Gy)
  602. self.assertRaises(ValueError, ECC.construct, curve="P-384", **coord)
  603. self.assertRaises(ValueError, ECC.construct, curve="P-384", d=2, **coordG)
  604. class TestEccModule_P521(unittest.TestCase):
  605. def test_generate(self):
  606. curve = _curves['p521']
  607. key = ECC.generate(curve="P-521")
  608. self.failUnless(key.has_private())
  609. self.assertEqual(key.pointQ, EccPoint(curve.Gx, curve.Gy, "p521") * key.d)
  610. # Other names
  611. ECC.generate(curve="secp521r1")
  612. ECC.generate(curve="prime521v1")
  613. def test_construct(self):
  614. curve = _curves['p521']
  615. key = ECC.construct(curve="P-521", d=1)
  616. self.failUnless(key.has_private())
  617. self.assertEqual(key.pointQ, _curves['p521'].G)
  618. key = ECC.construct(curve="P-521", point_x=curve.Gx, point_y=curve.Gy)
  619. self.failIf(key.has_private())
  620. self.assertEqual(key.pointQ, curve.G)
  621. # Other names
  622. ECC.construct(curve="p521", d=1)
  623. ECC.construct(curve="secp521r1", d=1)
  624. ECC.construct(curve="prime521v1", d=1)
  625. def test_negative_construct(self):
  626. coord = dict(point_x=10, point_y=4)
  627. coordG = dict(point_x=_curves['p521'].Gx, point_y=_curves['p521'].Gy)
  628. self.assertRaises(ValueError, ECC.construct, curve="P-521", **coord)
  629. self.assertRaises(ValueError, ECC.construct, curve="P-521", d=2, **coordG)
  630. def get_tests(config={}):
  631. tests = []
  632. tests += list_test_cases(TestEccPoint)
  633. tests += list_test_cases(TestEccPoint_NIST_P256)
  634. tests += list_test_cases(TestEccPoint_NIST_P384)
  635. tests += list_test_cases(TestEccPoint_NIST_P521)
  636. tests += list_test_cases(TestEccPoint_PAI_P256)
  637. tests += list_test_cases(TestEccPoint_PAI_P384)
  638. tests += list_test_cases(TestEccPoint_PAI_P521)
  639. tests += list_test_cases(TestEccKey_P256)
  640. tests += list_test_cases(TestEccKey_P384)
  641. tests += list_test_cases(TestEccKey_P521)
  642. tests += list_test_cases(TestEccModule_P256)
  643. tests += list_test_cases(TestEccModule_P384)
  644. tests += list_test_cases(TestEccModule_P521)
  645. return tests
  646. if __name__ == '__main__':
  647. suite = lambda: unittest.TestSuite(get_tests())
  648. unittest.main(defaultTest='suite')