test_ECC_NIST.py 51 KB

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