Debian Codegraph @

lib/Crypto/SelfTest/PublicKey/test_ECC_25519.py

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