test_ECC_448.py 15 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_Ed448(unittest.TestCase):
  39. Gxy = {"x": 0x4f1970c66bed0ded221d15a622bf36da9e146570470f1767ea6de324a3d3a46412ae1af72ab66511433b80e18b00938e2626a82bc70cc05e,
  40. "y": 0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14}
  41. G2xy = {"x": 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa955555555555555555555555555555555555555555555555555555555,
  42. "y": 0xae05e9634ad7048db359d6205086c2b0036ed7a035884dd7b7e36d728ad8c4b80d6565833a2a3098bbbcb2bed1cda06bdaeafbcdea9386ed}
  43. G3xy = {"x": 0x865886b9108af6455bd64316cb6943332241b8b8cda82c7e2ba077a4a3fcfe8daa9cbf7f6271fd6e862b769465da8575728173286ff2f8f,
  44. "y": 0xe005a8dbd5125cf706cbda7ad43aa6449a4a8d952356c3b9fce43c82ec4e1d58bb3a331bdb6767f0bffa9a68fed02dafb822ac13588ed6fc}
  45. pointG = EccPoint(Gxy['x'], Gxy['y'], curve="Ed448")
  46. pointG2 = EccPoint(G2xy['x'], G2xy['y'], curve="Ed448")
  47. pointG3 = EccPoint(G3xy['x'], G3xy['y'], curve="Ed448")
  48. def test_init_xy(self):
  49. EccPoint(self.Gxy['x'], self.Gxy['y'], curve="Ed448")
  50. # Neutral point
  51. pai = EccPoint(0, 1, curve="Ed448")
  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, 0x4f1970c66bed0ded221d15a622bf36da9e146570470f1767ea6de324a3d3a46412ae1af72ab66511433b80e18b00938e2626a82bc70cc05e)
  58. self.assertEqual(bp.y, 0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14)
  59. self.assertEqual(bp.xy, (bp.x, bp.y))
  60. # 2G
  61. bp2 = self.pointG2.copy()
  62. self.assertEqual(bp2.x, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa955555555555555555555555555555555555555555555555555555555)
  63. self.assertEqual(bp2.y, 0xae05e9634ad7048db359d6205086c2b0036ed7a035884dd7b7e36d728ad8c4b80d6565833a2a3098bbbcb2bed1cda06bdaeafbcdea9386ed)
  64. self.assertEqual(bp2.xy, (bp2.x, bp2.y))
  65. # 5G
  66. EccPoint(x=0x7a9f9335a48dcb0e2ba7601eedb50def80cbcf728562ada756d761e8958812808bc0d57a920c3c96f07b2d8cefc6f950d0a99d1092030034,
  67. y=0xadfd751a2517edd3b9109ce4fd580ade260ca1823ab18fced86551f7b698017127d7a4ee59d2b33c58405512881f225443b4731472f435eb,
  68. curve="Ed448")
  69. # Catch if point is not on the curve
  70. self.assertRaises(ValueError, EccPoint, 34, 35, curve="Ed448")
  71. def test_set(self):
  72. pointW = EccPoint(0, 1, curve="Ed448")
  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="Ed448")
  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, 0x7a9f9335a48dcb0e2ba7601eedb50def80cbcf728562ada756d761e8958812808bc0d57a920c3c96f07b2d8cefc6f950d0a99d1092030034)
  100. self.assertEqual(G5.y, 0xadfd751a2517edd3b9109ce4fd580ade260ca1823ab18fced86551f7b698017127d7a4ee59d2b33c58405512881f225443b4731472f435eb)
  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, 0x49dcbc5c6c0cce2c1419a17226f929ea255a09cf4e0891c693fda4be70c74cc301b7bdf1515dd8ba21aee1798949e120e2ce42ac48ba7f30)
  139. self.assertEqual(pointH.y, 0xd49077e4accde527164b33a5de021b979cb7c02f0457d845c90dc3227b8a5bc1c0d8f97ea1ca9472b5d444285d0d4f5b32e236f86de51839)
  140. d = 5
  141. pointH = d * self.pointG
  142. self.assertEqual(pointH.x, 0x7a9f9335a48dcb0e2ba7601eedb50def80cbcf728562ada756d761e8958812808bc0d57a920c3c96f07b2d8cefc6f950d0a99d1092030034)
  143. self.assertEqual(pointH.y, 0xadfd751a2517edd3b9109ce4fd580ade260ca1823ab18fced86551f7b698017127d7a4ee59d2b33c58405512881f225443b4731472f435eb)
  144. d = 10
  145. pointH = d * self.pointG
  146. self.assertEqual(pointH.x, 0x77486f9d19f6411cdd35d30d1c3235f71936452c787e5c034134d3e8172278aca61622bc805761ce3dab65118a0122d73b403165d0ed303d)
  147. self.assertEqual(pointH.y, 0x4d2fea0b026be11024f1f0fe7e94e618e8ac17381ada1d1bf7ee293a68ff5d0bf93c1997dc1aabdc0c7e6381428d85b6b1954a89e4cddf67)
  148. d = 20
  149. pointH = d * self.pointG
  150. self.assertEqual(pointH.x, 0x3c236422354600fe6763defcc1503737e4ed89e262d0de3ec1e552020f2a56fe3b9e1e012d021072598c3c2821e18268bb8fb8339c0d1216)
  151. self.assertEqual(pointH.y, 0xb555b9721f630ccb05fc466de4c74d3d2781e69eca88e1b040844f04cab39fd946f91c688fa42402bb38fb9c3e61231017020b219b4396e1)
  152. d = 255
  153. pointH = d * self.pointG
  154. self.assertEqual(pointH.x, 0xbeb7f8388b05cd9c1aa2e3c0dcf31e2b563659361826225390e7748654f627d5c36cbe627e9019936b56d15d4dad7c337c09bac64ff4197f)
  155. self.assertEqual(pointH.y, 0x1e37312b2dd4e9440c43c6e7725fc4fa3d11e582d4863f1d018e28f50c0efdb1f53f9b01ada7c87fa162b1f0d72401015d57613d25f1ad53)
  156. d = 256
  157. pointH = d * self.pointG
  158. self.assertEqual(pointH.x, 0xf19c34feb56730e3e2be761ac0a2a2b24853b281dda019fc35a5ab58e3696beb39609ae756b0d20fb7ccf0d79aaf5f3bca2e4fdb25bfac1c)
  159. self.assertEqual(pointH.y, 0x3beb69cc9111bffcaddc61d363ce6fe5dd44da4aadce78f52e92e985d5442344ced72c4611ed0daac9f4f5661eab73d7a12d25ce8a30241e)
  160. def test_sizes(self):
  161. self.assertEqual(self.pointG.size_in_bits(), 448)
  162. self.assertEqual(self.pointG.size_in_bytes(), 56)
  163. class TestEccKey_Ed448(unittest.TestCase):
  164. def test_private_key(self):
  165. seed = unhexlify("4adf5d37ac6785e83e99a924f92676d366a78690af59c92b6bdf14f9cdbcf26fdad478109607583d633b60078d61d51d81b7509c5433b0d4c9")
  166. Px = 0x72a01eea003a35f9ac44231dc4aae2a382f351d80bf32508175b0855edcf389aa2bbf308dd961ce361a6e7c2091bc78957f6ebcf3002a617
  167. Py = 0x9e0d08d84586e9aeefecacb41d049b831f1a3ee0c3eada63e34557b30702b50ab59fb372feff7c30b8cbb7dd51afbe88444ec56238722ec1
  168. key = EccKey(curve="Ed448", seed=seed)
  169. self.assertEqual(key.seed, seed)
  170. self.assertEqual(key.d, 0xb07cf179604f83433186e5178760c759c15125ee54ff6f8dcde46e872b709ac82ed0bd0a4e036d774034dcb18a9fb11894657a1485895f80)
  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, "ed448")
  175. key = EccKey(curve="Ed448", seed=seed, point=point)
  176. self.assertEqual(key.d, 0xb07cf179604f83433186e5178760c759c15125ee54ff6f8dcde46e872b709ac82ed0bd0a4e036d774034dcb18a9fb11894657a1485895f80)
  177. self.assertTrue(key.has_private())
  178. self.assertEqual(key.pointQ, point)
  179. # Other names
  180. key = EccKey(curve="ed448", seed=seed)
  181. # Must not accept d parameter
  182. self.assertRaises(ValueError, EccKey, curve="ed448", d=1)
  183. def test_public_key(self):
  184. point = EccPoint(_curves['ed448'].Gx, _curves['ed448'].Gy, curve='ed448')
  185. key = EccKey(curve="ed448", 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="ed448", seed=b'H'*57)
  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="ed448", seed=b'H' * 56))
  195. def test_equality(self):
  196. private_key = ECC.construct(seed=b'H'*57, curve="Ed448")
  197. private_key2 = ECC.construct(seed=b'H'*57, curve="ed448")
  198. private_key3 = ECC.construct(seed=b'C'*57, curve="Ed448")
  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='ed448')
  209. self.assertIn("curve='Ed448'", repr(key))
  210. self.assertEqual(key.curve, 'Ed448')
  211. self.assertEqual(key.public_key().curve, 'Ed448')
  212. class TestEccModule_Ed448(unittest.TestCase):
  213. def test_generate(self):
  214. key = ECC.generate(curve="Ed448")
  215. self.assertTrue(key.has_private())
  216. point = EccPoint(_curves['Ed448'].Gx, _curves['Ed448'].Gy, curve="Ed448") * key.d
  217. self.assertEqual(key.pointQ, point)
  218. # Always random
  219. key2 = ECC.generate(curve="Ed448")
  220. self.assertNotEqual(key, key2)
  221. # Other names
  222. ECC.generate(curve="Ed448")
  223. # Random source
  224. key1 = ECC.generate(curve="Ed448", randfunc=SHAKE128.new().read)
  225. key2 = ECC.generate(curve="Ed448", randfunc=SHAKE128.new().read)
  226. self.assertEqual(key1, key2)
  227. def test_construct(self):
  228. seed = unhexlify("4adf5d37ac6785e83e99a924f92676d366a78690af59c92b6bdf14f9cdbcf26fdad478109607583d633b60078d61d51d81b7509c5433b0d4c9")
  229. Px = 0x72a01eea003a35f9ac44231dc4aae2a382f351d80bf32508175b0855edcf389aa2bbf308dd961ce361a6e7c2091bc78957f6ebcf3002a617
  230. Py = 0x9e0d08d84586e9aeefecacb41d049b831f1a3ee0c3eada63e34557b30702b50ab59fb372feff7c30b8cbb7dd51afbe88444ec56238722ec1
  231. d = 0xb07cf179604f83433186e5178760c759c15125ee54ff6f8dcde46e872b709ac82ed0bd0a4e036d774034dcb18a9fb11894657a1485895f80
  232. point = EccPoint(Px, Py, curve="Ed448")
  233. # Private key only
  234. key = ECC.construct(curve="Ed448", seed=seed)
  235. self.assertEqual(key.pointQ, point)
  236. self.assertTrue(key.has_private())
  237. # Public key only
  238. key = ECC.construct(curve="Ed448", 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="Ed448", 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="ed448", seed=seed)
  247. def test_negative_construct(self):
  248. coord = dict(point_x=10, point_y=4)
  249. coordG = dict(point_x=_curves['ed448'].Gx, point_y=_curves['ed448'].Gy)
  250. self.assertRaises(ValueError, ECC.construct, curve="Ed448", **coord)
  251. self.assertRaises(ValueError, ECC.construct, curve="Ed448", d=2, **coordG)
  252. self.assertRaises(ValueError, ECC.construct, curve="Ed448", seed=b'H'*58)
  253. def get_tests(config={}):
  254. tests = []
  255. tests += list_test_cases(TestEccPoint_Ed448)
  256. tests += list_test_cases(TestEccKey_Ed448)
  257. tests += list_test_cases(TestEccModule_Ed448)
  258. return tests
  259. if __name__ == '__main__':
  260. def suite():
  261. return unittest.TestSuite(get_tests())
  262. unittest.main(defaultTest='suite')