test_ECC_NIST.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. from binascii import unhexlify
  32. from Crypto.SelfTest.st_common import list_test_cases
  33. from Crypto.SelfTest.loader import load_test_vectors
  34. from Crypto.PublicKey import ECC
  35. from Crypto.PublicKey.ECC import EccPoint, _curves, EccKey
  36. from Crypto.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. class OtherKeyType:
  52. pass
  53. self.assertFalse(p1 == OtherKeyType())
  54. self.assertTrue(p1 != OtherKeyType())
  55. def test_repr(self):
  56. p1 = ECC.construct(curve='P-256',
  57. d=75467964919405407085864614198393977741148485328036093939970922195112333446269,
  58. point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381,
  59. point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389)
  60. self.assertEqual(repr(p1), "EccKey(curve='NIST P-256', point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381, point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389, d=75467964919405407085864614198393977741148485328036093939970922195112333446269)")
  61. class TestEccPoint_NIST_P192(unittest.TestCase):
  62. """Tests defined in section 4.1 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
  63. pointS = EccPoint(
  64. 0xd458e7d127ae671b0c330266d246769353a012073e97acf8,
  65. 0x325930500d851f336bddc050cf7fb11b5673a1645086df3b,
  66. curve='p192')
  67. pointT = EccPoint(
  68. 0xf22c4395213e9ebe67ddecdd87fdbd01be16fb059b9753a4,
  69. 0x264424096af2b3597796db48f8dfb41fa9cecc97691a9c79,
  70. curve='p192')
  71. def test_set(self):
  72. pointW = EccPoint(0, 0)
  73. pointW.set(self.pointS)
  74. self.assertEqual(pointW, self.pointS)
  75. def test_copy(self):
  76. pointW = self.pointS.copy()
  77. self.assertEqual(pointW, self.pointS)
  78. pointW.set(self.pointT)
  79. self.assertEqual(pointW, self.pointT)
  80. self.assertNotEqual(self.pointS, self.pointT)
  81. def test_negate(self):
  82. negS = -self.pointS
  83. sum = self.pointS + negS
  84. self.assertEqual(sum, self.pointS.point_at_infinity())
  85. def test_addition(self):
  86. pointRx = 0x48e1e4096b9b8e5ca9d0f1f077b8abf58e843894de4d0290
  87. pointRy = 0x408fa77c797cd7dbfb16aa48a3648d3d63c94117d7b6aa4b
  88. pointR = self.pointS + self.pointT
  89. self.assertEqual(pointR.x, pointRx)
  90. self.assertEqual(pointR.y, pointRy)
  91. pai = pointR.point_at_infinity()
  92. # S + 0
  93. pointR = self.pointS + pai
  94. self.assertEqual(pointR, self.pointS)
  95. # 0 + S
  96. pointR = pai + self.pointS
  97. self.assertEqual(pointR, self.pointS)
  98. # 0 + 0
  99. pointR = pai + pai
  100. self.assertEqual(pointR, pai)
  101. def test_inplace_addition(self):
  102. pointRx = 0x48e1e4096b9b8e5ca9d0f1f077b8abf58e843894de4d0290
  103. pointRy = 0x408fa77c797cd7dbfb16aa48a3648d3d63c94117d7b6aa4b
  104. pointR = self.pointS.copy()
  105. pointR += self.pointT
  106. self.assertEqual(pointR.x, pointRx)
  107. self.assertEqual(pointR.y, pointRy)
  108. pai = pointR.point_at_infinity()
  109. # S + 0
  110. pointR = self.pointS.copy()
  111. pointR += pai
  112. self.assertEqual(pointR, self.pointS)
  113. # 0 + S
  114. pointR = pai.copy()
  115. pointR += self.pointS
  116. self.assertEqual(pointR, self.pointS)
  117. # 0 + 0
  118. pointR = pai.copy()
  119. pointR += pai
  120. self.assertEqual(pointR, pai)
  121. def test_doubling(self):
  122. pointRx = 0x30c5bc6b8c7da25354b373dc14dd8a0eba42d25a3f6e6962
  123. pointRy = 0x0dde14bc4249a721c407aedbf011e2ddbbcb2968c9d889cf
  124. pointR = self.pointS.copy()
  125. pointR.double()
  126. self.assertEqual(pointR.x, pointRx)
  127. self.assertEqual(pointR.y, pointRy)
  128. # 2*0
  129. pai = self.pointS.point_at_infinity()
  130. pointR = pai.copy()
  131. pointR.double()
  132. self.assertEqual(pointR, pai)
  133. # S + S
  134. pointR = self.pointS.copy()
  135. pointR += pointR
  136. self.assertEqual(pointR.x, pointRx)
  137. self.assertEqual(pointR.y, pointRy)
  138. def test_scalar_multiply(self):
  139. d = 0xa78a236d60baec0c5dd41b33a542463a8255391af64c74ee
  140. pointRx = 0x1faee4205a4f669d2d0a8f25e3bcec9a62a6952965bf6d31
  141. pointRy = 0x5ff2cdfa508a2581892367087c696f179e7a4d7e8260fb06
  142. pointR = self.pointS * d
  143. self.assertEqual(pointR.x, pointRx)
  144. self.assertEqual(pointR.y, pointRy)
  145. # 0*S
  146. pai = self.pointS.point_at_infinity()
  147. pointR = self.pointS * 0
  148. self.assertEqual(pointR, pai)
  149. # -1*S
  150. self.assertRaises(ValueError, lambda: self.pointS * -1)
  151. # Reverse order
  152. pointR = d * self.pointS
  153. self.assertEqual(pointR.x, pointRx)
  154. self.assertEqual(pointR.y, pointRy)
  155. pointR = Integer(d) * self.pointS
  156. self.assertEqual(pointR.x, pointRx)
  157. self.assertEqual(pointR.y, pointRy)
  158. def test_joint_scalar_multiply(self):
  159. d = 0xa78a236d60baec0c5dd41b33a542463a8255391af64c74ee
  160. e = 0xc4be3d53ec3089e71e4de8ceab7cce889bc393cd85b972bc
  161. pointRx = 0x019f64eed8fa9b72b7dfea82c17c9bfa60ecb9e1778b5bde
  162. pointRy = 0x16590c5fcd8655fa4ced33fb800e2a7e3c61f35d83503644
  163. pointR = self.pointS * d + self.pointT * e
  164. self.assertEqual(pointR.x, pointRx)
  165. self.assertEqual(pointR.y, pointRy)
  166. def test_sizes(self):
  167. self.assertEqual(self.pointS.size_in_bits(), 192)
  168. self.assertEqual(self.pointS.size_in_bytes(), 24)
  169. class TestEccPoint_NIST_P224(unittest.TestCase):
  170. """Tests defined in section 4.2 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
  171. pointS = EccPoint(
  172. 0x6eca814ba59a930843dc814edd6c97da95518df3c6fdf16e9a10bb5b,
  173. 0xef4b497f0963bc8b6aec0ca0f259b89cd80994147e05dc6b64d7bf22,
  174. curve='p224')
  175. pointT = EccPoint(
  176. 0xb72b25aea5cb03fb88d7e842002969648e6ef23c5d39ac903826bd6d,
  177. 0xc42a8a4d34984f0b71b5b4091af7dceb33ea729c1a2dc8b434f10c34,
  178. curve='p224')
  179. def test_set(self):
  180. pointW = EccPoint(0, 0)
  181. pointW.set(self.pointS)
  182. self.assertEqual(pointW, self.pointS)
  183. def test_copy(self):
  184. pointW = self.pointS.copy()
  185. self.assertEqual(pointW, self.pointS)
  186. pointW.set(self.pointT)
  187. self.assertEqual(pointW, self.pointT)
  188. self.assertNotEqual(self.pointS, self.pointT)
  189. def test_negate(self):
  190. negS = -self.pointS
  191. sum = self.pointS + negS
  192. self.assertEqual(sum, self.pointS.point_at_infinity())
  193. def test_addition(self):
  194. pointRx = 0x236f26d9e84c2f7d776b107bd478ee0a6d2bcfcaa2162afae8d2fd15
  195. pointRy = 0xe53cc0a7904ce6c3746f6a97471297a0b7d5cdf8d536ae25bb0fda70
  196. pointR = self.pointS + self.pointT
  197. self.assertEqual(pointR.x, pointRx)
  198. self.assertEqual(pointR.y, pointRy)
  199. pai = pointR.point_at_infinity()
  200. # S + 0
  201. pointR = self.pointS + pai
  202. self.assertEqual(pointR, self.pointS)
  203. # 0 + S
  204. pointR = pai + self.pointS
  205. self.assertEqual(pointR, self.pointS)
  206. # 0 + 0
  207. pointR = pai + pai
  208. self.assertEqual(pointR, pai)
  209. def test_inplace_addition(self):
  210. pointRx = 0x236f26d9e84c2f7d776b107bd478ee0a6d2bcfcaa2162afae8d2fd15
  211. pointRy = 0xe53cc0a7904ce6c3746f6a97471297a0b7d5cdf8d536ae25bb0fda70
  212. pointR = self.pointS.copy()
  213. pointR += self.pointT
  214. self.assertEqual(pointR.x, pointRx)
  215. self.assertEqual(pointR.y, pointRy)
  216. pai = pointR.point_at_infinity()
  217. # S + 0
  218. pointR = self.pointS.copy()
  219. pointR += pai
  220. self.assertEqual(pointR, self.pointS)
  221. # 0 + S
  222. pointR = pai.copy()
  223. pointR += self.pointS
  224. self.assertEqual(pointR, self.pointS)
  225. # 0 + 0
  226. pointR = pai.copy()
  227. pointR += pai
  228. self.assertEqual(pointR, pai)
  229. def test_doubling(self):
  230. pointRx = 0xa9c96f2117dee0f27ca56850ebb46efad8ee26852f165e29cb5cdfc7
  231. pointRy = 0xadf18c84cf77ced4d76d4930417d9579207840bf49bfbf5837dfdd7d
  232. pointR = self.pointS.copy()
  233. pointR.double()
  234. self.assertEqual(pointR.x, pointRx)
  235. self.assertEqual(pointR.y, pointRy)
  236. # 2*0
  237. pai = self.pointS.point_at_infinity()
  238. pointR = pai.copy()
  239. pointR.double()
  240. self.assertEqual(pointR, pai)
  241. # S + S
  242. pointR = self.pointS.copy()
  243. pointR += pointR
  244. self.assertEqual(pointR.x, pointRx)
  245. self.assertEqual(pointR.y, pointRy)
  246. def test_scalar_multiply(self):
  247. d = 0xa78ccc30eaca0fcc8e36b2dd6fbb03df06d37f52711e6363aaf1d73b
  248. pointRx = 0x96a7625e92a8d72bff1113abdb95777e736a14c6fdaacc392702bca4
  249. pointRy = 0x0f8e5702942a3c5e13cd2fd5801915258b43dfadc70d15dbada3ed10
  250. pointR = self.pointS * d
  251. self.assertEqual(pointR.x, pointRx)
  252. self.assertEqual(pointR.y, pointRy)
  253. # 0*S
  254. pai = self.pointS.point_at_infinity()
  255. pointR = self.pointS * 0
  256. self.assertEqual(pointR, pai)
  257. # -1*S
  258. self.assertRaises(ValueError, lambda: self.pointS * -1)
  259. # Reverse order
  260. pointR = d * self.pointS
  261. self.assertEqual(pointR.x, pointRx)
  262. self.assertEqual(pointR.y, pointRy)
  263. pointR = Integer(d) * self.pointS
  264. self.assertEqual(pointR.x, pointRx)
  265. self.assertEqual(pointR.y, pointRy)
  266. def test_joing_scalar_multiply(self):
  267. d = 0xa78ccc30eaca0fcc8e36b2dd6fbb03df06d37f52711e6363aaf1d73b
  268. e = 0x54d549ffc08c96592519d73e71e8e0703fc8177fa88aa77a6ed35736
  269. pointRx = 0xdbfe2958c7b2cda1302a67ea3ffd94c918c5b350ab838d52e288c83e
  270. pointRy = 0x2f521b83ac3b0549ff4895abcc7f0c5a861aacb87acbc5b8147bb18b
  271. pointR = self.pointS * d + self.pointT * e
  272. self.assertEqual(pointR.x, pointRx)
  273. self.assertEqual(pointR.y, pointRy)
  274. def test_sizes(self):
  275. self.assertEqual(self.pointS.size_in_bits(), 224)
  276. self.assertEqual(self.pointS.size_in_bytes(), 28)
  277. class TestEccPoint_NIST_P256(unittest.TestCase):
  278. """Tests defined in section 4.3 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
  279. pointS = EccPoint(
  280. 0xde2444bebc8d36e682edd27e0f271508617519b3221a8fa0b77cab3989da97c9,
  281. 0xc093ae7ff36e5380fc01a5aad1e66659702de80f53cec576b6350b243042a256)
  282. pointT = EccPoint(
  283. 0x55a8b00f8da1d44e62f6b3b25316212e39540dc861c89575bb8cf92e35e0986b,
  284. 0x5421c3209c2d6c704835d82ac4c3dd90f61a8a52598b9e7ab656e9d8c8b24316)
  285. def test_set(self):
  286. pointW = EccPoint(0, 0)
  287. pointW.set(self.pointS)
  288. self.assertEqual(pointW, self.pointS)
  289. def test_copy(self):
  290. pointW = self.pointS.copy()
  291. self.assertEqual(pointW, self.pointS)
  292. pointW.set(self.pointT)
  293. self.assertEqual(pointW, self.pointT)
  294. self.assertNotEqual(self.pointS, self.pointT)
  295. def test_negate(self):
  296. negS = -self.pointS
  297. sum = self.pointS + negS
  298. self.assertEqual(sum, self.pointS.point_at_infinity())
  299. def test_addition(self):
  300. pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
  301. pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
  302. pointR = self.pointS + self.pointT
  303. self.assertEqual(pointR.x, pointRx)
  304. self.assertEqual(pointR.y, pointRy)
  305. pai = pointR.point_at_infinity()
  306. # S + 0
  307. pointR = self.pointS + pai
  308. self.assertEqual(pointR, self.pointS)
  309. # 0 + S
  310. pointR = pai + self.pointS
  311. self.assertEqual(pointR, self.pointS)
  312. # 0 + 0
  313. pointR = pai + pai
  314. self.assertEqual(pointR, pai)
  315. def test_inplace_addition(self):
  316. pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
  317. pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
  318. pointR = self.pointS.copy()
  319. pointR += self.pointT
  320. self.assertEqual(pointR.x, pointRx)
  321. self.assertEqual(pointR.y, pointRy)
  322. pai = pointR.point_at_infinity()
  323. # S + 0
  324. pointR = self.pointS.copy()
  325. pointR += pai
  326. self.assertEqual(pointR, self.pointS)
  327. # 0 + S
  328. pointR = pai.copy()
  329. pointR += self.pointS
  330. self.assertEqual(pointR, self.pointS)
  331. # 0 + 0
  332. pointR = pai.copy()
  333. pointR += pai
  334. self.assertEqual(pointR, pai)
  335. def test_doubling(self):
  336. pointRx = 0x7669e6901606ee3ba1a8eef1e0024c33df6c22f3b17481b82a860ffcdb6127b0
  337. pointRy = 0xfa878162187a54f6c39f6ee0072f33de389ef3eecd03023de10ca2c1db61d0c7
  338. pointR = self.pointS.copy()
  339. pointR.double()
  340. self.assertEqual(pointR.x, pointRx)
  341. self.assertEqual(pointR.y, pointRy)
  342. # 2*0
  343. pai = self.pointS.point_at_infinity()
  344. pointR = pai.copy()
  345. pointR.double()
  346. self.assertEqual(pointR, pai)
  347. # S + S
  348. pointR = self.pointS.copy()
  349. pointR += pointR
  350. self.assertEqual(pointR.x, pointRx)
  351. self.assertEqual(pointR.y, pointRy)
  352. def test_scalar_multiply(self):
  353. d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
  354. pointRx = 0x51d08d5f2d4278882946d88d83c97d11e62becc3cfc18bedacc89ba34eeca03f
  355. pointRy = 0x75ee68eb8bf626aa5b673ab51f6e744e06f8fcf8a6c0cf3035beca956a7b41d5
  356. pointR = self.pointS * d
  357. self.assertEqual(pointR.x, pointRx)
  358. self.assertEqual(pointR.y, pointRy)
  359. # 0*S
  360. pai = self.pointS.point_at_infinity()
  361. pointR = self.pointS * 0
  362. self.assertEqual(pointR, pai)
  363. # -1*S
  364. self.assertRaises(ValueError, lambda: self.pointS * -1)
  365. # Reverse order
  366. pointR = d * self.pointS
  367. self.assertEqual(pointR.x, pointRx)
  368. self.assertEqual(pointR.y, pointRy)
  369. pointR = Integer(d) * self.pointS
  370. self.assertEqual(pointR.x, pointRx)
  371. self.assertEqual(pointR.y, pointRy)
  372. def test_joing_scalar_multiply(self):
  373. d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
  374. e = 0xd37f628ece72a462f0145cbefe3f0b355ee8332d37acdd83a358016aea029db7
  375. pointRx = 0xd867b4679221009234939221b8046245efcf58413daacbeff857b8588341f6b8
  376. pointRy = 0xf2504055c03cede12d22720dad69c745106b6607ec7e50dd35d54bd80f615275
  377. pointR = self.pointS * d + self.pointT * e
  378. self.assertEqual(pointR.x, pointRx)
  379. self.assertEqual(pointR.y, pointRy)
  380. def test_sizes(self):
  381. self.assertEqual(self.pointS.size_in_bits(), 256)
  382. self.assertEqual(self.pointS.size_in_bytes(), 32)
  383. class TestEccPoint_NIST_P384(unittest.TestCase):
  384. """Tests defined in section 4.4 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
  385. pointS = EccPoint(
  386. 0xfba203b81bbd23f2b3be971cc23997e1ae4d89e69cb6f92385dda82768ada415ebab4167459da98e62b1332d1e73cb0e,
  387. 0x5ffedbaefdeba603e7923e06cdb5d0c65b22301429293376d5c6944e3fa6259f162b4788de6987fd59aed5e4b5285e45,
  388. "p384")
  389. pointT = EccPoint(
  390. 0xaacc05202e7fda6fc73d82f0a66220527da8117ee8f8330ead7d20ee6f255f582d8bd38c5a7f2b40bcdb68ba13d81051,
  391. 0x84009a263fefba7c2c57cffa5db3634d286131afc0fca8d25afa22a7b5dce0d9470da89233cee178592f49b6fecb5092,
  392. "p384")
  393. def test_set(self):
  394. pointW = EccPoint(0, 0, "p384")
  395. pointW.set(self.pointS)
  396. self.assertEqual(pointW, self.pointS)
  397. def test_copy(self):
  398. pointW = self.pointS.copy()
  399. self.assertEqual(pointW, self.pointS)
  400. pointW.set(self.pointT)
  401. self.assertEqual(pointW, self.pointT)
  402. self.assertNotEqual(self.pointS, self.pointT)
  403. def test_negate(self):
  404. negS = -self.pointS
  405. sum = self.pointS + negS
  406. self.assertEqual(sum, self.pointS.point_at_infinity())
  407. def test_addition(self):
  408. pointRx = 0x12dc5ce7acdfc5844d939f40b4df012e68f865b89c3213ba97090a247a2fc009075cf471cd2e85c489979b65ee0b5eed
  409. pointRy = 0x167312e58fe0c0afa248f2854e3cddcb557f983b3189b67f21eee01341e7e9fe67f6ee81b36988efa406945c8804a4b0
  410. pointR = self.pointS + self.pointT
  411. self.assertEqual(pointR.x, pointRx)
  412. self.assertEqual(pointR.y, pointRy)
  413. pai = pointR.point_at_infinity()
  414. # S + 0
  415. pointR = self.pointS + pai
  416. self.assertEqual(pointR, self.pointS)
  417. # 0 + S
  418. pointR = pai + self.pointS
  419. self.assertEqual(pointR, self.pointS)
  420. # 0 + 0
  421. pointR = pai + pai
  422. self.assertEqual(pointR, pai)
  423. def _test_inplace_addition(self):
  424. pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
  425. pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
  426. pointR = self.pointS.copy()
  427. pointR += self.pointT
  428. self.assertEqual(pointR.x, pointRx)
  429. self.assertEqual(pointR.y, pointRy)
  430. pai = pointR.point_at_infinity()
  431. # S + 0
  432. pointR = self.pointS.copy()
  433. pointR += pai
  434. self.assertEqual(pointR, self.pointS)
  435. # 0 + S
  436. pointR = pai.copy()
  437. pointR += self.pointS
  438. self.assertEqual(pointR, self.pointS)
  439. # 0 + 0
  440. pointR = pai.copy()
  441. pointR += pai
  442. self.assertEqual(pointR, pai)
  443. def test_doubling(self):
  444. pointRx = 0x2a2111b1e0aa8b2fc5a1975516bc4d58017ff96b25e1bdff3c229d5fac3bacc319dcbec29f9478f42dee597b4641504c
  445. pointRy = 0xfa2e3d9dc84db8954ce8085ef28d7184fddfd1344b4d4797343af9b5f9d837520b450f726443e4114bd4e5bdb2f65ddd
  446. pointR = self.pointS.copy()
  447. pointR.double()
  448. self.assertEqual(pointR.x, pointRx)
  449. self.assertEqual(pointR.y, pointRy)
  450. # 2*0
  451. pai = self.pointS.point_at_infinity()
  452. pointR = pai.copy()
  453. pointR.double()
  454. self.assertEqual(pointR, pai)
  455. # S + S
  456. pointR = self.pointS.copy()
  457. pointR += pointR
  458. self.assertEqual(pointR.x, pointRx)
  459. self.assertEqual(pointR.y, pointRy)
  460. def test_scalar_multiply(self):
  461. d = 0xa4ebcae5a665983493ab3e626085a24c104311a761b5a8fdac052ed1f111a5c44f76f45659d2d111a61b5fdd97583480
  462. pointRx = 0xe4f77e7ffeb7f0958910e3a680d677a477191df166160ff7ef6bb5261f791aa7b45e3e653d151b95dad3d93ca0290ef2
  463. pointRy = 0xac7dee41d8c5f4a7d5836960a773cfc1376289d3373f8cf7417b0c6207ac32e913856612fc9ff2e357eb2ee05cf9667f
  464. pointR = self.pointS * d
  465. self.assertEqual(pointR.x, pointRx)
  466. self.assertEqual(pointR.y, pointRy)
  467. # 0*S
  468. pai = self.pointS.point_at_infinity()
  469. pointR = self.pointS * 0
  470. self.assertEqual(pointR, pai)
  471. # -1*S
  472. self.assertRaises(ValueError, lambda: self.pointS * -1)
  473. def test_joing_scalar_multiply(self):
  474. d = 0xa4ebcae5a665983493ab3e626085a24c104311a761b5a8fdac052ed1f111a5c44f76f45659d2d111a61b5fdd97583480
  475. e = 0xafcf88119a3a76c87acbd6008e1349b29f4ba9aa0e12ce89bcfcae2180b38d81ab8cf15095301a182afbc6893e75385d
  476. pointRx = 0x917ea28bcd641741ae5d18c2f1bd917ba68d34f0f0577387dc81260462aea60e2417b8bdc5d954fc729d211db23a02dc
  477. pointRy = 0x1a29f7ce6d074654d77b40888c73e92546c8f16a5ff6bcbd307f758d4aee684beff26f6742f597e2585c86da908f7186
  478. pointR = self.pointS * d + self.pointT * e
  479. self.assertEqual(pointR.x, pointRx)
  480. self.assertEqual(pointR.y, pointRy)
  481. def test_sizes(self):
  482. self.assertEqual(self.pointS.size_in_bits(), 384)
  483. self.assertEqual(self.pointS.size_in_bytes(), 48)
  484. class TestEccPoint_NIST_P521(unittest.TestCase):
  485. """Tests defined in section 4.5 of https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.9073&rep=rep1&type=pdf"""
  486. pointS = EccPoint(
  487. 0x000001d5c693f66c08ed03ad0f031f937443458f601fd098d3d0227b4bf62873af50740b0bb84aa157fc847bcf8dc16a8b2b8bfd8e2d0a7d39af04b089930ef6dad5c1b4,
  488. 0x00000144b7770963c63a39248865ff36b074151eac33549b224af5c8664c54012b818ed037b2b7c1a63ac89ebaa11e07db89fcee5b556e49764ee3fa66ea7ae61ac01823,
  489. "p521")
  490. pointT = EccPoint(
  491. 0x000000f411f2ac2eb971a267b80297ba67c322dba4bb21cec8b70073bf88fc1ca5fde3ba09e5df6d39acb2c0762c03d7bc224a3e197feaf760d6324006fe3be9a548c7d5,
  492. 0x000001fdf842769c707c93c630df6d02eff399a06f1b36fb9684f0b373ed064889629abb92b1ae328fdb45534268384943f0e9222afe03259b32274d35d1b9584c65e305,
  493. "p521")
  494. def test_set(self):
  495. pointW = EccPoint(0, 0)
  496. pointW.set(self.pointS)
  497. self.assertEqual(pointW, self.pointS)
  498. def test_copy(self):
  499. pointW = self.pointS.copy()
  500. self.assertEqual(pointW, self.pointS)
  501. pointW.set(self.pointT)
  502. self.assertEqual(pointW, self.pointT)
  503. self.assertNotEqual(self.pointS, self.pointT)
  504. def test_negate(self):
  505. negS = -self.pointS
  506. sum = self.pointS + negS
  507. self.assertEqual(sum, self.pointS.point_at_infinity())
  508. def test_addition(self):
  509. pointRx = 0x000001264ae115ba9cbc2ee56e6f0059e24b52c8046321602c59a339cfb757c89a59c358a9a8e1f86d384b3f3b255ea3f73670c6dc9f45d46b6a196dc37bbe0f6b2dd9e9
  510. pointRy = 0x00000062a9c72b8f9f88a271690bfa017a6466c31b9cadc2fc544744aeb817072349cfddc5ad0e81b03f1897bd9c8c6efbdf68237dc3bb00445979fb373b20c9a967ac55
  511. pointR = self.pointS + self.pointT
  512. self.assertEqual(pointR.x, pointRx)
  513. self.assertEqual(pointR.y, pointRy)
  514. pai = pointR.point_at_infinity()
  515. # S + 0
  516. pointR = self.pointS + pai
  517. self.assertEqual(pointR, self.pointS)
  518. # 0 + S
  519. pointR = pai + self.pointS
  520. self.assertEqual(pointR, self.pointS)
  521. # 0 + 0
  522. pointR = pai + pai
  523. self.assertEqual(pointR, pai)
  524. def test_inplace_addition(self):
  525. pointRx = 0x000001264ae115ba9cbc2ee56e6f0059e24b52c8046321602c59a339cfb757c89a59c358a9a8e1f86d384b3f3b255ea3f73670c6dc9f45d46b6a196dc37bbe0f6b2dd9e9
  526. pointRy = 0x00000062a9c72b8f9f88a271690bfa017a6466c31b9cadc2fc544744aeb817072349cfddc5ad0e81b03f1897bd9c8c6efbdf68237dc3bb00445979fb373b20c9a967ac55
  527. pointR = self.pointS.copy()
  528. pointR += self.pointT
  529. self.assertEqual(pointR.x, pointRx)
  530. self.assertEqual(pointR.y, pointRy)
  531. pai = pointR.point_at_infinity()
  532. # S + 0
  533. pointR = self.pointS.copy()
  534. pointR += pai
  535. self.assertEqual(pointR, self.pointS)
  536. # 0 + S
  537. pointR = pai.copy()
  538. pointR += self.pointS
  539. self.assertEqual(pointR, self.pointS)
  540. # 0 + 0
  541. pointR = pai.copy()
  542. pointR += pai
  543. self.assertEqual(pointR, pai)
  544. def test_doubling(self):
  545. pointRx = 0x0000012879442f2450c119e7119a5f738be1f1eba9e9d7c6cf41b325d9ce6d643106e9d61124a91a96bcf201305a9dee55fa79136dc700831e54c3ca4ff2646bd3c36bc6
  546. pointRy = 0x0000019864a8b8855c2479cbefe375ae553e2393271ed36fadfc4494fc0583f6bd03598896f39854abeae5f9a6515a021e2c0eef139e71de610143f53382f4104dccb543
  547. pointR = self.pointS.copy()
  548. pointR.double()
  549. self.assertEqual(pointR.x, pointRx)
  550. self.assertEqual(pointR.y, pointRy)
  551. # 2*0
  552. pai = self.pointS.point_at_infinity()
  553. pointR = pai.copy()
  554. pointR.double()
  555. self.assertEqual(pointR, pai)
  556. # S + S
  557. pointR = self.pointS.copy()
  558. pointR += pointR
  559. self.assertEqual(pointR.x, pointRx)
  560. self.assertEqual(pointR.y, pointRy)
  561. def test_scalar_multiply(self):
  562. d = 0x000001eb7f81785c9629f136a7e8f8c674957109735554111a2a866fa5a166699419bfa9936c78b62653964df0d6da940a695c7294d41b2d6600de6dfcf0edcfc89fdcb1
  563. pointRx = 0x00000091b15d09d0ca0353f8f96b93cdb13497b0a4bb582ae9ebefa35eee61bf7b7d041b8ec34c6c00c0c0671c4ae063318fb75be87af4fe859608c95f0ab4774f8c95bb
  564. pointRy = 0x00000130f8f8b5e1abb4dd94f6baaf654a2d5810411e77b7423965e0c7fd79ec1ae563c207bd255ee9828eb7a03fed565240d2cc80ddd2cecbb2eb50f0951f75ad87977f
  565. pointR = self.pointS * d
  566. self.assertEqual(pointR.x, pointRx)
  567. self.assertEqual(pointR.y, pointRy)
  568. # 0*S
  569. pai = self.pointS.point_at_infinity()
  570. pointR = self.pointS * 0
  571. self.assertEqual(pointR, pai)
  572. # -1*S
  573. self.assertRaises(ValueError, lambda: self.pointS * -1)
  574. def test_joing_scalar_multiply(self):
  575. d = 0x000001eb7f81785c9629f136a7e8f8c674957109735554111a2a866fa5a166699419bfa9936c78b62653964df0d6da940a695c7294d41b2d6600de6dfcf0edcfc89fdcb1
  576. e = 0x00000137e6b73d38f153c3a7575615812608f2bab3229c92e21c0d1c83cfad9261dbb17bb77a63682000031b9122c2f0cdab2af72314be95254de4291a8f85f7c70412e3
  577. pointRx = 0x0000009d3802642b3bea152beb9e05fba247790f7fc168072d363340133402f2585588dc1385d40ebcb8552f8db02b23d687cae46185b27528adb1bf9729716e4eba653d
  578. pointRy = 0x0000000fe44344e79da6f49d87c1063744e5957d9ac0a505bafa8281c9ce9ff25ad53f8da084a2deb0923e46501de5797850c61b229023dd9cf7fc7f04cd35ebb026d89d
  579. pointR = self.pointS * d
  580. pointR += self.pointT * e
  581. self.assertEqual(pointR.x, pointRx)
  582. self.assertEqual(pointR.y, pointRy)
  583. def test_sizes(self):
  584. self.assertEqual(self.pointS.size_in_bits(), 521)
  585. self.assertEqual(self.pointS.size_in_bytes(), 66)
  586. class TestEccPoint_PAI_P192(unittest.TestCase):
  587. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  588. curve = _curves['p192']
  589. pointG = EccPoint(curve.Gx, curve.Gy, "p192")
  590. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  591. "point-at-infinity.org-P192.txt",
  592. "P-192 tests from point-at-infinity.org",
  593. {"k": lambda k: int(k),
  594. "x": lambda x: int(x, 16),
  595. "y": lambda y: int(y, 16)}) or []
  596. for tv in tv_pai:
  597. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  598. result = self.pointG * scalar
  599. self.assertEqual(result.x, x)
  600. self.assertEqual(result.y, y)
  601. setattr(TestEccPoint_PAI_P192, "test_%d" % tv.count, new_test)
  602. class TestEccPoint_PAI_P224(unittest.TestCase):
  603. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  604. curve = _curves['p224']
  605. pointG = EccPoint(curve.Gx, curve.Gy, "p224")
  606. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  607. "point-at-infinity.org-P224.txt",
  608. "P-224 tests from point-at-infinity.org",
  609. {"k": lambda k: int(k),
  610. "x": lambda x: int(x, 16),
  611. "y": lambda y: int(y, 16)}) or []
  612. for tv in tv_pai:
  613. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  614. result = self.pointG * scalar
  615. self.assertEqual(result.x, x)
  616. self.assertEqual(result.y, y)
  617. setattr(TestEccPoint_PAI_P224, "test_%d" % tv.count, new_test)
  618. class TestEccPoint_PAI_P256(unittest.TestCase):
  619. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  620. curve = _curves['p256']
  621. pointG = EccPoint(curve.Gx, curve.Gy, "p256")
  622. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  623. "point-at-infinity.org-P256.txt",
  624. "P-256 tests from point-at-infinity.org",
  625. {"k": lambda k: int(k),
  626. "x": lambda x: int(x, 16),
  627. "y": lambda y: int(y, 16)}) or []
  628. for tv in tv_pai:
  629. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  630. result = self.pointG * scalar
  631. self.assertEqual(result.x, x)
  632. self.assertEqual(result.y, y)
  633. setattr(TestEccPoint_PAI_P256, "test_%d" % tv.count, new_test)
  634. class TestEccPoint_PAI_P384(unittest.TestCase):
  635. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  636. curve = _curves['p384']
  637. pointG = EccPoint(curve.Gx, curve.Gy, "p384")
  638. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  639. "point-at-infinity.org-P384.txt",
  640. "P-384 tests from point-at-infinity.org",
  641. {"k": lambda k: int(k),
  642. "x": lambda x: int(x, 16),
  643. "y": lambda y: int(y, 16)}) or []
  644. for tv in tv_pai:
  645. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  646. result = self.pointG * scalar
  647. self.assertEqual(result.x, x)
  648. self.assertEqual(result.y, y)
  649. setattr(TestEccPoint_PAI_P384, "test_%d" % tv.count, new_test)
  650. class TestEccPoint_PAI_P521(unittest.TestCase):
  651. """Test vectors from http://point-at-infinity.org/ecc/nisttv"""
  652. curve = _curves['p521']
  653. pointG = EccPoint(curve.Gx, curve.Gy, "p521")
  654. tv_pai = load_test_vectors(("PublicKey", "ECC"),
  655. "point-at-infinity.org-P521.txt",
  656. "P-521 tests from point-at-infinity.org",
  657. {"k": lambda k: int(k),
  658. "x": lambda x: int(x, 16),
  659. "y": lambda y: int(y, 16)}) or []
  660. for tv in tv_pai:
  661. def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
  662. result = self.pointG * scalar
  663. self.assertEqual(result.x, x)
  664. self.assertEqual(result.y, y)
  665. setattr(TestEccPoint_PAI_P521, "test_%d" % tv.count, new_test)
  666. class TestEccKey_P192(unittest.TestCase):
  667. def test_private_key(self):
  668. key = EccKey(curve="P-192", d=1)
  669. self.assertEqual(key.d, 1)
  670. self.assertTrue(key.has_private())
  671. self.assertEqual(key.pointQ.x, _curves['p192'].Gx)
  672. self.assertEqual(key.pointQ.y, _curves['p192'].Gy)
  673. point = EccPoint(_curves['p192'].Gx, _curves['p192'].Gy, curve='P-192')
  674. key = EccKey(curve="P-192", d=1, point=point)
  675. self.assertEqual(key.d, 1)
  676. self.assertTrue(key.has_private())
  677. self.assertEqual(key.pointQ, point)
  678. # Other names
  679. key = EccKey(curve="secp192r1", d=1)
  680. key = EccKey(curve="prime192v1", d=1)
  681. def test_public_key(self):
  682. point = EccPoint(_curves['p192'].Gx, _curves['p192'].Gy, curve='P-192')
  683. key = EccKey(curve="P-192", point=point)
  684. self.assertFalse(key.has_private())
  685. self.assertEqual(key.pointQ, point)
  686. def test_public_key_derived(self):
  687. priv_key = EccKey(curve="P-192", d=3)
  688. pub_key = priv_key.public_key()
  689. self.assertFalse(pub_key.has_private())
  690. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  691. def test_invalid_curve(self):
  692. self.assertRaises(ValueError, lambda: EccKey(curve="P-193", d=1))
  693. def test_invalid_d(self):
  694. self.assertRaises(ValueError, lambda: EccKey(curve="P-192", d=0))
  695. self.assertRaises(ValueError, lambda: EccKey(curve="P-192",
  696. d=_curves['p192'].order))
  697. def test_equality(self):
  698. private_key = ECC.construct(d=3, curve="P-192")
  699. private_key2 = ECC.construct(d=3, curve="P-192")
  700. private_key3 = ECC.construct(d=4, curve="P-192")
  701. public_key = private_key.public_key()
  702. public_key2 = private_key2.public_key()
  703. public_key3 = private_key3.public_key()
  704. self.assertEqual(private_key, private_key2)
  705. self.assertNotEqual(private_key, private_key3)
  706. self.assertEqual(public_key, public_key2)
  707. self.assertNotEqual(public_key, public_key3)
  708. self.assertNotEqual(public_key, private_key)
  709. def test_name_consistency(self):
  710. key = ECC.generate(curve='p192')
  711. self.assertIn("curve='NIST P-192'", repr(key))
  712. self.assertEqual(key.curve, 'NIST P-192')
  713. self.assertEqual(key.public_key().curve, 'NIST P-192')
  714. class TestEccKey_P224(unittest.TestCase):
  715. def test_private_key(self):
  716. key = EccKey(curve="P-224", d=1)
  717. self.assertEqual(key.d, 1)
  718. self.assertTrue(key.has_private())
  719. self.assertEqual(key.pointQ.x, _curves['p224'].Gx)
  720. self.assertEqual(key.pointQ.y, _curves['p224'].Gy)
  721. point = EccPoint(_curves['p224'].Gx, _curves['p224'].Gy, curve='P-224')
  722. key = EccKey(curve="P-224", d=1, point=point)
  723. self.assertEqual(key.d, 1)
  724. self.assertTrue(key.has_private())
  725. self.assertEqual(key.pointQ, point)
  726. # Other names
  727. key = EccKey(curve="secp224r1", d=1)
  728. key = EccKey(curve="prime224v1", d=1)
  729. def test_public_key(self):
  730. point = EccPoint(_curves['p224'].Gx, _curves['p224'].Gy, curve='P-224')
  731. key = EccKey(curve="P-224", point=point)
  732. self.assertFalse(key.has_private())
  733. self.assertEqual(key.pointQ, point)
  734. def test_public_key_derived(self):
  735. priv_key = EccKey(curve="P-224", d=3)
  736. pub_key = priv_key.public_key()
  737. self.assertFalse(pub_key.has_private())
  738. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  739. def test_invalid_curve(self):
  740. self.assertRaises(ValueError, lambda: EccKey(curve="P-225", d=1))
  741. def test_invalid_d(self):
  742. self.assertRaises(ValueError, lambda: EccKey(curve="P-224", d=0))
  743. self.assertRaises(ValueError, lambda: EccKey(curve="P-224",
  744. d=_curves['p224'].order))
  745. def test_equality(self):
  746. private_key = ECC.construct(d=3, curve="P-224")
  747. private_key2 = ECC.construct(d=3, curve="P-224")
  748. private_key3 = ECC.construct(d=4, curve="P-224")
  749. public_key = private_key.public_key()
  750. public_key2 = private_key2.public_key()
  751. public_key3 = private_key3.public_key()
  752. self.assertEqual(private_key, private_key2)
  753. self.assertNotEqual(private_key, private_key3)
  754. self.assertEqual(public_key, public_key2)
  755. self.assertNotEqual(public_key, public_key3)
  756. self.assertNotEqual(public_key, private_key)
  757. def test_name_consistency(self):
  758. key = ECC.generate(curve='p224')
  759. self.assertIn("curve='NIST P-224'", repr(key))
  760. self.assertEqual(key.curve, 'NIST P-224')
  761. self.assertEqual(key.public_key().curve, 'NIST P-224')
  762. class TestEccKey_P256(unittest.TestCase):
  763. def test_private_key(self):
  764. key = EccKey(curve="P-256", d=1)
  765. self.assertEqual(key.d, 1)
  766. self.assertTrue(key.has_private())
  767. self.assertEqual(key.pointQ.x, _curves['p256'].Gx)
  768. self.assertEqual(key.pointQ.y, _curves['p256'].Gy)
  769. point = EccPoint(_curves['p256'].Gx, _curves['p256'].Gy)
  770. key = EccKey(curve="P-256", d=1, point=point)
  771. self.assertEqual(key.d, 1)
  772. self.assertTrue(key.has_private())
  773. self.assertEqual(key.pointQ, point)
  774. # Other names
  775. key = EccKey(curve="secp256r1", d=1)
  776. key = EccKey(curve="prime256v1", d=1)
  777. # Must not accept d parameter
  778. self.assertRaises(ValueError, EccKey, curve="p256", seed=b'H'*32)
  779. def test_public_key(self):
  780. point = EccPoint(_curves['p256'].Gx, _curves['p256'].Gy)
  781. key = EccKey(curve="P-256", point=point)
  782. self.assertFalse(key.has_private())
  783. self.assertEqual(key.pointQ, point)
  784. def test_public_key_derived(self):
  785. priv_key = EccKey(curve="P-256", d=3)
  786. pub_key = priv_key.public_key()
  787. self.assertFalse(pub_key.has_private())
  788. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  789. def test_invalid_curve(self):
  790. self.assertRaises(ValueError, lambda: EccKey(curve="P-257", d=1))
  791. def test_invalid_d(self):
  792. self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=0))
  793. self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=_curves['p256'].order))
  794. def test_equality(self):
  795. private_key = ECC.construct(d=3, curve="P-256")
  796. private_key2 = ECC.construct(d=3, curve="P-256")
  797. private_key3 = ECC.construct(d=4, curve="P-256")
  798. public_key = private_key.public_key()
  799. public_key2 = private_key2.public_key()
  800. public_key3 = private_key3.public_key()
  801. self.assertEqual(private_key, private_key2)
  802. self.assertNotEqual(private_key, private_key3)
  803. self.assertEqual(public_key, public_key2)
  804. self.assertNotEqual(public_key, public_key3)
  805. self.assertNotEqual(public_key, private_key)
  806. def test_name_consistency(self):
  807. key = ECC.generate(curve='p256')
  808. self.assertIn("curve='NIST P-256'", repr(key))
  809. self.assertEqual(key.curve, 'NIST P-256')
  810. self.assertEqual(key.public_key().curve, 'NIST P-256')
  811. class TestEccKey_P384(unittest.TestCase):
  812. def test_private_key(self):
  813. p384 = _curves['p384']
  814. key = EccKey(curve="P-384", d=1)
  815. self.assertEqual(key.d, 1)
  816. self.assertTrue(key.has_private())
  817. self.assertEqual(key.pointQ.x, p384.Gx)
  818. self.assertEqual(key.pointQ.y, p384.Gy)
  819. point = EccPoint(p384.Gx, p384.Gy, "p384")
  820. key = EccKey(curve="P-384", d=1, point=point)
  821. self.assertEqual(key.d, 1)
  822. self.assertTrue(key.has_private())
  823. self.assertEqual(key.pointQ, point)
  824. # Other names
  825. key = EccKey(curve="p384", d=1)
  826. key = EccKey(curve="secp384r1", d=1)
  827. key = EccKey(curve="prime384v1", d=1)
  828. def test_public_key(self):
  829. p384 = _curves['p384']
  830. point = EccPoint(p384.Gx, p384.Gy, 'p384')
  831. key = EccKey(curve="P-384", point=point)
  832. self.assertFalse(key.has_private())
  833. self.assertEqual(key.pointQ, point)
  834. def test_public_key_derived(self):
  835. priv_key = EccKey(curve="P-384", d=3)
  836. pub_key = priv_key.public_key()
  837. self.assertFalse(pub_key.has_private())
  838. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  839. def test_invalid_curve(self):
  840. self.assertRaises(ValueError, lambda: EccKey(curve="P-385", d=1))
  841. def test_invalid_d(self):
  842. self.assertRaises(ValueError, lambda: EccKey(curve="P-384", d=0))
  843. self.assertRaises(ValueError, lambda: EccKey(curve="P-384",
  844. d=_curves['p384'].order))
  845. def test_equality(self):
  846. private_key = ECC.construct(d=3, curve="P-384")
  847. private_key2 = ECC.construct(d=3, curve="P-384")
  848. private_key3 = ECC.construct(d=4, curve="P-384")
  849. public_key = private_key.public_key()
  850. public_key2 = private_key2.public_key()
  851. public_key3 = private_key3.public_key()
  852. self.assertEqual(private_key, private_key2)
  853. self.assertNotEqual(private_key, private_key3)
  854. self.assertEqual(public_key, public_key2)
  855. self.assertNotEqual(public_key, public_key3)
  856. self.assertNotEqual(public_key, private_key)
  857. def test_name_consistency(self):
  858. key = ECC.generate(curve='p384')
  859. self.assertIn("curve='NIST P-384'", repr(key))
  860. self.assertEqual(key.curve, 'NIST P-384')
  861. self.assertEqual(key.public_key().curve, 'NIST P-384')
  862. class TestEccKey_P521(unittest.TestCase):
  863. def test_private_key(self):
  864. p521 = _curves['p521']
  865. key = EccKey(curve="P-521", d=1)
  866. self.assertEqual(key.d, 1)
  867. self.assertTrue(key.has_private())
  868. self.assertEqual(key.pointQ.x, p521.Gx)
  869. self.assertEqual(key.pointQ.y, p521.Gy)
  870. point = EccPoint(p521.Gx, p521.Gy, "p521")
  871. key = EccKey(curve="P-521", d=1, point=point)
  872. self.assertEqual(key.d, 1)
  873. self.assertTrue(key.has_private())
  874. self.assertEqual(key.pointQ, point)
  875. # Other names
  876. key = EccKey(curve="p521", d=1)
  877. key = EccKey(curve="secp521r1", d=1)
  878. key = EccKey(curve="prime521v1", d=1)
  879. def test_public_key(self):
  880. p521 = _curves['p521']
  881. point = EccPoint(p521.Gx, p521.Gy, 'p521')
  882. key = EccKey(curve="P-384", point=point)
  883. self.assertFalse(key.has_private())
  884. self.assertEqual(key.pointQ, point)
  885. def test_public_key_derived(self):
  886. priv_key = EccKey(curve="P-521", d=3)
  887. pub_key = priv_key.public_key()
  888. self.assertFalse(pub_key.has_private())
  889. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  890. def test_invalid_curve(self):
  891. self.assertRaises(ValueError, lambda: EccKey(curve="P-522", d=1))
  892. def test_invalid_d(self):
  893. self.assertRaises(ValueError, lambda: EccKey(curve="P-521", d=0))
  894. self.assertRaises(ValueError, lambda: EccKey(curve="P-521",
  895. d=_curves['p521'].order))
  896. def test_equality(self):
  897. private_key = ECC.construct(d=3, curve="P-521")
  898. private_key2 = ECC.construct(d=3, curve="P-521")
  899. private_key3 = ECC.construct(d=4, curve="P-521")
  900. public_key = private_key.public_key()
  901. public_key2 = private_key2.public_key()
  902. public_key3 = private_key3.public_key()
  903. self.assertEqual(private_key, private_key2)
  904. self.assertNotEqual(private_key, private_key3)
  905. self.assertEqual(public_key, public_key2)
  906. self.assertNotEqual(public_key, public_key3)
  907. self.assertNotEqual(public_key, private_key)
  908. def test_name_consistency(self):
  909. key = ECC.generate(curve='p521')
  910. self.assertIn("curve='NIST P-521'", repr(key))
  911. self.assertEqual(key.curve, 'NIST P-521')
  912. self.assertEqual(key.public_key().curve, 'NIST P-521')
  913. class TestEccModule_P192(unittest.TestCase):
  914. def test_generate(self):
  915. key = ECC.generate(curve="P-192")
  916. self.assertTrue(key.has_private())
  917. self.assertEqual(key.pointQ, EccPoint(_curves['p192'].Gx,
  918. _curves['p192'].Gy,
  919. "P-192") * key.d,
  920. "p192")
  921. # Other names
  922. ECC.generate(curve="secp192r1")
  923. ECC.generate(curve="prime192v1")
  924. def test_construct(self):
  925. key = ECC.construct(curve="P-192", d=1)
  926. self.assertTrue(key.has_private())
  927. self.assertEqual(key.pointQ, _curves['p192'].G)
  928. key = ECC.construct(curve="P-192", point_x=_curves['p192'].Gx,
  929. point_y=_curves['p192'].Gy)
  930. self.assertFalse(key.has_private())
  931. self.assertEqual(key.pointQ, _curves['p192'].G)
  932. # Other names
  933. ECC.construct(curve="p192", d=1)
  934. ECC.construct(curve="secp192r1", d=1)
  935. ECC.construct(curve="prime192v1", d=1)
  936. def test_negative_construct(self):
  937. coord = dict(point_x=10, point_y=4)
  938. coordG = dict(point_x=_curves['p192'].Gx, point_y=_curves['p192'].Gy)
  939. self.assertRaises(ValueError, ECC.construct, curve="P-192", **coord)
  940. self.assertRaises(ValueError, ECC.construct, curve="P-192", d=2, **coordG)
  941. class TestEccModule_P224(unittest.TestCase):
  942. def test_generate(self):
  943. key = ECC.generate(curve="P-224")
  944. self.assertTrue(key.has_private())
  945. self.assertEqual(key.pointQ, EccPoint(_curves['p224'].Gx,
  946. _curves['p224'].Gy,
  947. "P-224") * key.d,
  948. "p224")
  949. # Other names
  950. ECC.generate(curve="secp224r1")
  951. ECC.generate(curve="prime224v1")
  952. def test_construct(self):
  953. key = ECC.construct(curve="P-224", d=1)
  954. self.assertTrue(key.has_private())
  955. self.assertEqual(key.pointQ, _curves['p224'].G)
  956. key = ECC.construct(curve="P-224", point_x=_curves['p224'].Gx,
  957. point_y=_curves['p224'].Gy)
  958. self.assertFalse(key.has_private())
  959. self.assertEqual(key.pointQ, _curves['p224'].G)
  960. # Other names
  961. ECC.construct(curve="p224", d=1)
  962. ECC.construct(curve="secp224r1", d=1)
  963. ECC.construct(curve="prime224v1", d=1)
  964. def test_negative_construct(self):
  965. coord = dict(point_x=10, point_y=4)
  966. coordG = dict(point_x=_curves['p224'].Gx, point_y=_curves['p224'].Gy)
  967. self.assertRaises(ValueError, ECC.construct, curve="P-224", **coord)
  968. self.assertRaises(ValueError, ECC.construct, curve="P-224", d=2, **coordG)
  969. class TestEccModule_P256(unittest.TestCase):
  970. def test_generate(self):
  971. key = ECC.generate(curve="P-256")
  972. self.assertTrue(key.has_private())
  973. self.assertEqual(key.pointQ, EccPoint(_curves['p256'].Gx,
  974. _curves['p256'].Gy) * key.d,
  975. "p256")
  976. # Other names
  977. ECC.generate(curve="secp256r1")
  978. ECC.generate(curve="prime256v1")
  979. def test_construct(self):
  980. key = ECC.construct(curve="P-256", d=1)
  981. self.assertTrue(key.has_private())
  982. self.assertEqual(key.pointQ, _curves['p256'].G)
  983. key = ECC.construct(curve="P-256", point_x=_curves['p256'].Gx,
  984. point_y=_curves['p256'].Gy)
  985. self.assertFalse(key.has_private())
  986. self.assertEqual(key.pointQ, _curves['p256'].G)
  987. # Other names
  988. ECC.construct(curve="p256", d=1)
  989. ECC.construct(curve="secp256r1", d=1)
  990. ECC.construct(curve="prime256v1", d=1)
  991. def test_negative_construct(self):
  992. coord = dict(point_x=10, point_y=4)
  993. coordG = dict(point_x=_curves['p256'].Gx, point_y=_curves['p256'].Gy)
  994. self.assertRaises(ValueError, ECC.construct, curve="P-256", **coord)
  995. self.assertRaises(ValueError, ECC.construct, curve="P-256", d=2, **coordG)
  996. class TestEccModule_P384(unittest.TestCase):
  997. def test_generate(self):
  998. curve = _curves['p384']
  999. key = ECC.generate(curve="P-384")
  1000. self.assertTrue(key.has_private())
  1001. self.assertEqual(key.pointQ, EccPoint(curve.Gx, curve.Gy, "p384") * key.d)
  1002. # Other names
  1003. ECC.generate(curve="secp384r1")
  1004. ECC.generate(curve="prime384v1")
  1005. def test_construct(self):
  1006. curve = _curves['p384']
  1007. key = ECC.construct(curve="P-384", d=1)
  1008. self.assertTrue(key.has_private())
  1009. self.assertEqual(key.pointQ, _curves['p384'].G)
  1010. key = ECC.construct(curve="P-384", point_x=curve.Gx, point_y=curve.Gy)
  1011. self.assertFalse(key.has_private())
  1012. self.assertEqual(key.pointQ, curve.G)
  1013. # Other names
  1014. ECC.construct(curve="p384", d=1)
  1015. ECC.construct(curve="secp384r1", d=1)
  1016. ECC.construct(curve="prime384v1", d=1)
  1017. def test_negative_construct(self):
  1018. coord = dict(point_x=10, point_y=4)
  1019. coordG = dict(point_x=_curves['p384'].Gx, point_y=_curves['p384'].Gy)
  1020. self.assertRaises(ValueError, ECC.construct, curve="P-384", **coord)
  1021. self.assertRaises(ValueError, ECC.construct, curve="P-384", d=2, **coordG)
  1022. class TestEccModule_P521(unittest.TestCase):
  1023. def test_generate(self):
  1024. curve = _curves['p521']
  1025. key = ECC.generate(curve="P-521")
  1026. self.assertTrue(key.has_private())
  1027. self.assertEqual(key.pointQ, EccPoint(curve.Gx, curve.Gy, "p521") * key.d)
  1028. # Other names
  1029. ECC.generate(curve="secp521r1")
  1030. ECC.generate(curve="prime521v1")
  1031. def test_construct(self):
  1032. curve = _curves['p521']
  1033. key = ECC.construct(curve="P-521", d=1)
  1034. self.assertTrue(key.has_private())
  1035. self.assertEqual(key.pointQ, _curves['p521'].G)
  1036. key = ECC.construct(curve="P-521", point_x=curve.Gx, point_y=curve.Gy)
  1037. self.assertFalse(key.has_private())
  1038. self.assertEqual(key.pointQ, curve.G)
  1039. # Other names
  1040. ECC.construct(curve="p521", d=1)
  1041. ECC.construct(curve="secp521r1", d=1)
  1042. ECC.construct(curve="prime521v1", d=1)
  1043. def test_negative_construct(self):
  1044. coord = dict(point_x=10, point_y=4)
  1045. coordG = dict(point_x=_curves['p521'].Gx, point_y=_curves['p521'].Gy)
  1046. self.assertRaises(ValueError, ECC.construct, curve="P-521", **coord)
  1047. self.assertRaises(ValueError, ECC.construct, curve="P-521", d=2, **coordG)
  1048. def get_tests(config={}):
  1049. tests = []
  1050. tests += list_test_cases(TestEccPoint)
  1051. tests += list_test_cases(TestEccPoint_NIST_P192)
  1052. tests += list_test_cases(TestEccPoint_NIST_P224)
  1053. tests += list_test_cases(TestEccPoint_NIST_P256)
  1054. tests += list_test_cases(TestEccPoint_NIST_P384)
  1055. tests += list_test_cases(TestEccPoint_NIST_P521)
  1056. tests += list_test_cases(TestEccPoint_PAI_P192)
  1057. tests += list_test_cases(TestEccPoint_PAI_P224)
  1058. tests += list_test_cases(TestEccPoint_PAI_P256)
  1059. tests += list_test_cases(TestEccPoint_PAI_P384)
  1060. tests += list_test_cases(TestEccPoint_PAI_P521)
  1061. tests += list_test_cases(TestEccKey_P192)
  1062. tests += list_test_cases(TestEccKey_P224)
  1063. tests += list_test_cases(TestEccKey_P256)
  1064. tests += list_test_cases(TestEccKey_P384)
  1065. tests += list_test_cases(TestEccKey_P521)
  1066. tests += list_test_cases(TestEccModule_P192)
  1067. tests += list_test_cases(TestEccModule_P224)
  1068. tests += list_test_cases(TestEccModule_P256)
  1069. tests += list_test_cases(TestEccModule_P384)
  1070. tests += list_test_cases(TestEccModule_P521)
  1071. return tests
  1072. if __name__ == '__main__':
  1073. suite = lambda: unittest.TestSuite(get_tests())
  1074. unittest.main(defaultTest='suite')