test_ECC_25519.py 13 KB

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