test_ECC_Ed25519.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2022, 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. from Crypto.Hash import SHAKE128
  38. class TestEccPoint_Ed25519(unittest.TestCase):
  39. Gxy = {"x": 15112221349535400772501151409588531511454012693041857206046113283949847762202,
  40. "y": 46316835694926478169428394003475163141307993866256225615783033603165251855960}
  41. G2xy = {"x": 24727413235106541002554574571675588834622768167397638456726423682521233608206,
  42. "y": 15549675580280190176352668710449542251549572066445060580507079593062643049417}
  43. G3xy = {"x": 46896733464454938657123544595386787789046198280132665686241321779790909858396,
  44. "y": 8324843778533443976490377120369201138301417226297555316741202210403726505172}
  45. pointG = EccPoint(Gxy['x'], Gxy['y'], curve="ed25519")
  46. pointG2 = EccPoint(G2xy['x'], G2xy['y'], curve="ed25519")
  47. pointG3 = EccPoint(G3xy['x'], G3xy['y'], curve="ed25519")
  48. def test_curve_attribute(self):
  49. self.assertEqual(self.pointG.curve, "Ed25519")
  50. def test_init_xy(self):
  51. EccPoint(self.Gxy['x'], self.Gxy['y'], curve="Ed25519")
  52. # Neutral point
  53. pai = EccPoint(0, 1, curve="Ed25519")
  54. self.assertEqual(pai.x, 0)
  55. self.assertEqual(pai.y, 1)
  56. self.assertEqual(pai.xy, (0, 1))
  57. # G
  58. bp = self.pointG.copy()
  59. self.assertEqual(bp.x, 15112221349535400772501151409588531511454012693041857206046113283949847762202)
  60. self.assertEqual(bp.y, 46316835694926478169428394003475163141307993866256225615783033603165251855960)
  61. self.assertEqual(bp.xy, (bp.x, bp.y))
  62. # 2G
  63. bp2 = self.pointG2.copy()
  64. self.assertEqual(bp2.x, 24727413235106541002554574571675588834622768167397638456726423682521233608206)
  65. self.assertEqual(bp2.y, 15549675580280190176352668710449542251549572066445060580507079593062643049417)
  66. self.assertEqual(bp2.xy, (bp2.x, bp2.y))
  67. # 5G
  68. EccPoint(x=33467004535436536005251147249499675200073690106659565782908757308821616914995,
  69. y=43097193783671926753355113395909008640284023746042808659097434958891230611693,
  70. curve="Ed25519")
  71. # Catch if point is not on the curve
  72. self.assertRaises(ValueError, EccPoint, 34, 35, curve="Ed25519")
  73. def test_set(self):
  74. pointW = EccPoint(0, 1, curve="Ed25519")
  75. pointW.set(self.pointG)
  76. self.assertEqual(pointW.x, self.pointG.x)
  77. self.assertEqual(pointW.y, self.pointG.y)
  78. def test_copy(self):
  79. pointW = self.pointG.copy()
  80. self.assertEqual(pointW.x, self.pointG.x)
  81. self.assertEqual(pointW.y, self.pointG.y)
  82. def test_equal(self):
  83. pointH = self.pointG.copy()
  84. pointI = self.pointG2.copy()
  85. self.assertEqual(self.pointG, pointH)
  86. self.assertNotEqual(self.pointG, pointI)
  87. def test_pai(self):
  88. pai = EccPoint(0, 1, curve="Ed25519")
  89. self.assertTrue(pai.is_point_at_infinity())
  90. self.assertEqual(pai, pai.point_at_infinity())
  91. def test_negate(self):
  92. negG = -self.pointG
  93. G100 = self.pointG * 100
  94. sum_zero = G100 + negG * 100
  95. self.assertTrue(sum_zero.is_point_at_infinity())
  96. sum_99 = G100 + negG
  97. expected = self.pointG * 99
  98. self.assertEqual(sum_99, expected)
  99. def test_addition(self):
  100. self.assertEqual(self.pointG + self.pointG2, self.pointG3)
  101. self.assertEqual(self.pointG2 + self.pointG, self.pointG3)
  102. self.assertEqual(self.pointG2 + self.pointG.point_at_infinity(), self.pointG2)
  103. self.assertEqual(self.pointG.point_at_infinity() + self.pointG2, self.pointG2)
  104. G5 = self.pointG2 + self.pointG3
  105. self.assertEqual(G5.x, 33467004535436536005251147249499675200073690106659565782908757308821616914995)
  106. self.assertEqual(G5.y, 43097193783671926753355113395909008640284023746042808659097434958891230611693)
  107. def test_inplace_addition(self):
  108. pointH = self.pointG.copy()
  109. pointH += self.pointG
  110. self.assertEqual(pointH, self.pointG2)
  111. pointH += self.pointG
  112. self.assertEqual(pointH, self.pointG3)
  113. pointH += self.pointG.point_at_infinity()
  114. self.assertEqual(pointH, self.pointG3)
  115. def test_doubling(self):
  116. pointH = self.pointG.copy()
  117. pointH.double()
  118. self.assertEqual(pointH.x, self.pointG2.x)
  119. self.assertEqual(pointH.y, self.pointG2.y)
  120. # 2*0
  121. pai = self.pointG.point_at_infinity()
  122. pointR = pai.copy()
  123. pointR.double()
  124. self.assertEqual(pointR, pai)
  125. def test_scalar_multiply(self):
  126. d = 0
  127. pointH = d * self.pointG
  128. self.assertEqual(pointH.x, 0)
  129. self.assertEqual(pointH.y, 1)
  130. d = 1
  131. pointH = d * self.pointG
  132. self.assertEqual(pointH.x, self.pointG.x)
  133. self.assertEqual(pointH.y, self.pointG.y)
  134. d = 2
  135. pointH = d * self.pointG
  136. self.assertEqual(pointH.x, self.pointG2.x)
  137. self.assertEqual(pointH.y, self.pointG2.y)
  138. d = 3
  139. pointH = d * self.pointG
  140. self.assertEqual(pointH.x, self.pointG3.x)
  141. self.assertEqual(pointH.y, self.pointG3.y)
  142. d = 4
  143. pointH = d * self.pointG
  144. self.assertEqual(pointH.x, 14582954232372986451776170844943001818709880559417862259286374126315108956272)
  145. self.assertEqual(pointH.y, 32483318716863467900234833297694612235682047836132991208333042722294373421359)
  146. d = 5
  147. pointH = d * self.pointG
  148. self.assertEqual(pointH.x, 33467004535436536005251147249499675200073690106659565782908757308821616914995)
  149. self.assertEqual(pointH.y, 43097193783671926753355113395909008640284023746042808659097434958891230611693)
  150. d = 10
  151. pointH = d * self.pointG
  152. self.assertEqual(pointH.x, 43500613248243327786121022071801015118933854441360174117148262713429272820047)
  153. self.assertEqual(pointH.y, 45005105423099817237495816771148012388779685712352441364231470781391834741548)
  154. d = 20
  155. pointH = d * self.pointG
  156. self.assertEqual(pointH.x, 46694936775300686710656303283485882876784402425210400817529601134760286812591)
  157. self.assertEqual(pointH.y, 8786390172762935853260670851718824721296437982862763585171334833968259029560)
  158. d = 255
  159. pointH = d * self.pointG
  160. self.assertEqual(pointH.x, 36843863416400016952258312492144504209624961884991522125275155377549541182230)
  161. self.assertEqual(pointH.y, 22327030283879720808995671630924669697661065034121040761798775626517750047180)
  162. d = 256
  163. pointH = d * self.pointG
  164. self.assertEqual(pointH.x, 42740085206947573681423002599456489563927820004573071834350074001818321593686)
  165. self.assertEqual(pointH.y, 6935684722522267618220753829624209639984359598320562595061366101608187623111)
  166. def test_sizes(self):
  167. self.assertEqual(self.pointG.size_in_bits(), 255)
  168. self.assertEqual(self.pointG.size_in_bytes(), 32)
  169. class TestEccKey_Ed25519(unittest.TestCase):
  170. def test_private_key(self):
  171. seed = unhexlify("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")
  172. Px = 38815646466658113194383306759739515082307681141926459231621296960732224964046
  173. Py = 11903303657706407974989296177215005343713679411332034699907763981919547054807
  174. key = EccKey(curve="Ed25519", seed=seed)
  175. self.assertEqual(key.seed, seed)
  176. self.assertEqual(key.d, 36144925721603087658594284515452164870581325872720374094707712194495455132720)
  177. self.assertTrue(key.has_private())
  178. self.assertEqual(key.pointQ.x, Px)
  179. self.assertEqual(key.pointQ.y, Py)
  180. point = EccPoint(Px, Py, "ed25519")
  181. key = EccKey(curve="Ed25519", seed=seed, point=point)
  182. self.assertEqual(key.d, 36144925721603087658594284515452164870581325872720374094707712194495455132720)
  183. self.assertTrue(key.has_private())
  184. self.assertEqual(key.pointQ, point)
  185. # Other names
  186. key = EccKey(curve="ed25519", seed=seed)
  187. # Must not accept d parameter
  188. self.assertRaises(ValueError, EccKey, curve="ed25519", d=1)
  189. def test_public_key(self):
  190. point = EccPoint(_curves['ed25519'].Gx, _curves['ed25519'].Gy, curve='ed25519')
  191. key = EccKey(curve="ed25519", point=point)
  192. self.assertFalse(key.has_private())
  193. self.assertEqual(key.pointQ, point)
  194. def test_public_key_derived(self):
  195. priv_key = EccKey(curve="ed25519", seed=b'H'*32)
  196. pub_key = priv_key.public_key()
  197. self.assertFalse(pub_key.has_private())
  198. self.assertEqual(priv_key.pointQ, pub_key.pointQ)
  199. def test_invalid_seed(self):
  200. self.assertRaises(ValueError, lambda: EccKey(curve="ed25519", seed=b'H' * 31))
  201. def test_equality(self):
  202. private_key = ECC.construct(seed=b'H'*32, curve="Ed25519")
  203. private_key2 = ECC.construct(seed=b'H'*32, curve="ed25519")
  204. private_key3 = ECC.construct(seed=b'C'*32, curve="Ed25519")
  205. public_key = private_key.public_key()
  206. public_key2 = private_key2.public_key()
  207. public_key3 = private_key3.public_key()
  208. self.assertEqual(private_key, private_key2)
  209. self.assertNotEqual(private_key, private_key3)
  210. self.assertEqual(public_key, public_key2)
  211. self.assertNotEqual(public_key, public_key3)
  212. self.assertNotEqual(public_key, private_key)
  213. def test_name_consistency(self):
  214. key = ECC.generate(curve='ed25519')
  215. self.assertIn("curve='Ed25519'", repr(key))
  216. self.assertEqual(key.curve, 'Ed25519')
  217. self.assertEqual(key.public_key().curve, 'Ed25519')
  218. class TestEccModule_Ed25519(unittest.TestCase):
  219. def test_generate(self):
  220. key = ECC.generate(curve="Ed25519")
  221. self.assertTrue(key.has_private())
  222. point = EccPoint(_curves['Ed25519'].Gx, _curves['Ed25519'].Gy, curve="Ed25519") * key.d
  223. self.assertEqual(key.pointQ, point)
  224. # Always random
  225. key2 = ECC.generate(curve="Ed25519")
  226. self.assertNotEqual(key, key2)
  227. # Other names
  228. ECC.generate(curve="Ed25519")
  229. # Random source
  230. key1 = ECC.generate(curve="Ed25519", randfunc=SHAKE128.new().read)
  231. key2 = ECC.generate(curve="Ed25519", randfunc=SHAKE128.new().read)
  232. self.assertEqual(key1, key2)
  233. def test_construct(self):
  234. seed = unhexlify("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")
  235. Px = 38815646466658113194383306759739515082307681141926459231621296960732224964046
  236. Py = 11903303657706407974989296177215005343713679411332034699907763981919547054807
  237. d = 36144925721603087658594284515452164870581325872720374094707712194495455132720
  238. point = EccPoint(Px, Py, curve="Ed25519")
  239. # Private key only
  240. key = ECC.construct(curve="Ed25519", seed=seed)
  241. self.assertEqual(key.pointQ, point)
  242. self.assertTrue(key.has_private())
  243. # Public key only
  244. key = ECC.construct(curve="Ed25519", point_x=Px, point_y=Py)
  245. self.assertEqual(key.pointQ, point)
  246. self.assertFalse(key.has_private())
  247. # Private and public key
  248. key = ECC.construct(curve="Ed25519", seed=seed, point_x=Px, point_y=Py)
  249. self.assertEqual(key.pointQ, point)
  250. self.assertTrue(key.has_private())
  251. # Other names
  252. key = ECC.construct(curve="ed25519", seed=seed)
  253. def test_negative_construct(self):
  254. coord = dict(point_x=10, point_y=4)
  255. coordG = dict(point_x=_curves['ed25519'].Gx, point_y=_curves['ed25519'].Gy)
  256. self.assertRaises(ValueError, ECC.construct, curve="Ed25519", **coord)
  257. self.assertRaises(ValueError, ECC.construct, curve="Ed25519", d=2, **coordG)
  258. self.assertRaises(ValueError, ECC.construct, curve="Ed25519", seed=b'H'*31)
  259. def get_tests(config={}):
  260. tests = []
  261. tests += list_test_cases(TestEccPoint_Ed25519)
  262. tests += list_test_cases(TestEccKey_Ed25519)
  263. tests += list_test_cases(TestEccModule_Ed25519)
  264. return tests
  265. if __name__ == '__main__':
  266. def suite():
  267. return unittest.TestSuite(get_tests())
  268. unittest.main(defaultTest='suite')