/usr/lib/python3/dist-packages/Cryptodome/SelfTest/PublicKey/test_ECC.py is in python3-pycryptodome 3.4.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | # ===================================================================
#
# Copyright (c) 2015, Legrandin <helderijs@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ===================================================================
import unittest
import time
from Cryptodome.SelfTest.st_common import list_test_cases
from Cryptodome.SelfTest.loader import load_tests
from Cryptodome.PublicKey import ECC
from Cryptodome.PublicKey.ECC import EccPoint, _curve, EccKey
class TestEccPoint_NIST(unittest.TestCase):
"""Tests defined in section 4.3 of https://www.nsa.gov/ia/_files/nist-routines.pdf"""
pointS = EccPoint(
0xde2444bebc8d36e682edd27e0f271508617519b3221a8fa0b77cab3989da97c9,
0xc093ae7ff36e5380fc01a5aad1e66659702de80f53cec576b6350b243042a256)
pointT = EccPoint(
0x55a8b00f8da1d44e62f6b3b25316212e39540dc861c89575bb8cf92e35e0986b,
0x5421c3209c2d6c704835d82ac4c3dd90f61a8a52598b9e7ab656e9d8c8b24316)
def test_set(self):
pointW = EccPoint(0, 0)
pointW.set(self.pointS)
self.assertEqual(pointW, self.pointS)
def test_copy(self):
pointW = self.pointS.copy()
self.assertEqual(pointW, self.pointS)
pointW.set(self.pointT)
self.assertEqual(pointW, self.pointT)
self.assertNotEqual(self.pointS, self.pointT)
def test_addition(self):
pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
pointR = self.pointS + self.pointT
self.assertEqual(pointR.x, pointRx)
self.assertEqual(pointR.y, pointRy)
pai = EccPoint.point_at_infinity()
# S + 0
pointR = self.pointS + pai
self.assertEqual(pointR, self.pointS)
# 0 + S
pointR = pai + self.pointS
self.assertEqual(pointR, self.pointS)
# 0 + 0
pointR = pai + pai
self.assertEqual(pointR, pai)
def test_inplace_addition(self):
pointRx = 0x72b13dd4354b6b81745195e98cc5ba6970349191ac476bd4553cf35a545a067e
pointRy = 0x8d585cbb2e1327d75241a8a122d7620dc33b13315aa5c9d46d013011744ac264
pointR = self.pointS.copy()
pointR += self.pointT
self.assertEqual(pointR.x, pointRx)
self.assertEqual(pointR.y, pointRy)
pai = EccPoint.point_at_infinity()
# S + 0
pointR = self.pointS.copy()
pointR += pai
self.assertEqual(pointR, self.pointS)
# 0 + S
pointR = pai.copy()
pointR += self.pointS
self.assertEqual(pointR, self.pointS)
# 0 + 0
pointR = pai.copy()
pointR += pai
self.assertEqual(pointR, pai)
def test_doubling(self):
pointRx = 0x7669e6901606ee3ba1a8eef1e0024c33df6c22f3b17481b82a860ffcdb6127b0
pointRy = 0xfa878162187a54f6c39f6ee0072f33de389ef3eecd03023de10ca2c1db61d0c7
pointR = self.pointS.copy()
pointR.double()
self.assertEqual(pointR.x, pointRx)
self.assertEqual(pointR.y, pointRy)
# 2*0
pai = self.pointS.point_at_infinity()
pointR = pai.copy()
pointR.double()
self.assertEqual(pointR, pai)
# S + S
pointR = self.pointS.copy()
pointR += pointR
self.assertEqual(pointR.x, pointRx)
self.assertEqual(pointR.y, pointRy)
def test_scalar_multiply(self):
d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
pointRx = 0x51d08d5f2d4278882946d88d83c97d11e62becc3cfc18bedacc89ba34eeca03f
pointRy = 0x75ee68eb8bf626aa5b673ab51f6e744e06f8fcf8a6c0cf3035beca956a7b41d5
pointR = self.pointS * d
self.assertEqual(pointR.x, pointRx)
self.assertEqual(pointR.y, pointRy)
# 0*S
pai = self.pointS.point_at_infinity()
pointR = self.pointS * 0
self.assertEqual(pointR, pai)
# -1*S
self.assertRaises(ValueError, lambda: self.pointS * -1)
def test_joing_scalar_multiply(self):
d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd
e = 0xd37f628ece72a462f0145cbefe3f0b355ee8332d37acdd83a358016aea029db7
pointRx = 0xd867b4679221009234939221b8046245efcf58413daacbeff857b8588341f6b8
pointRy = 0xf2504055c03cede12d22720dad69c745106b6607ec7e50dd35d54bd80f615275
pointR = self.pointS * d + self.pointT * e
self.assertEqual(pointR.x, pointRx)
self.assertEqual(pointR.y, pointRy)
class TestEccPoint_PAI(unittest.TestCase):
"""Test vectors from http://point-at-infinity.org/ecc/nisttv"""
pointG = EccPoint(_curve.Gx, _curve.Gy)
tv_pai = load_tests(("Cryptodome", "SelfTest", "PublicKey", "test_vectors", "ECC"),
"point-at-infinity.org-P256.txt",
"P-256 tests from point-at-infinity.org",
{ "k" : lambda k: int(k),
"x" : lambda x: int(x, 16),
"y" : lambda y: int(y, 16)} )
assert(tv_pai)
for tv in tv_pai:
def new_test(self, scalar=tv.k, x=tv.x, y=tv.y):
result = self.pointG * scalar
self.assertEqual(result.x, x)
self.assertEqual(result.y, y)
setattr(TestEccPoint_PAI, "test_%d" % tv.count, new_test)
class TestEccKey(unittest.TestCase):
def test_private_key(self):
key = EccKey(curve="P-256", d=1)
self.assertEqual(key.d, 1)
self.assertTrue(key.has_private())
self.assertEqual(key.pointQ.x, _curve.Gx)
self.assertEqual(key.pointQ.y, _curve.Gy)
point = EccPoint(_curve.Gx, _curve.Gy)
key = EccKey(curve="P-256", d=1, point=point)
self.assertEqual(key.d, 1)
self.assertTrue(key.has_private())
self.assertEqual(key.pointQ, point)
# Other names
key = EccKey(curve="secp256r1", d=1)
key = EccKey(curve="prime256v1", d=1)
def test_public_key(self):
point = EccPoint(_curve.Gx, _curve.Gy)
key = EccKey(curve="P-256", point=point)
self.assertFalse(key.has_private())
self.assertEqual(key.pointQ, point)
def test_public_key_derived(self):
priv_key = EccKey(curve="P-256", d=3)
pub_key = priv_key.public_key()
self.assertFalse(pub_key.has_private())
self.assertEqual(priv_key.pointQ, pub_key.pointQ)
def test_invalid_curve(self):
self.assertRaises(ValueError, lambda: EccKey(curve="P-257", d=1))
def test_invalid_d(self):
self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=0))
self.assertRaises(ValueError, lambda: EccKey(curve="P-256", d=_curve.order))
def test_equality(self):
private_key = ECC.construct(d=3, curve="P-256")
private_key2 = ECC.construct(d=3, curve="P-256")
private_key3 = ECC.construct(d=4, curve="P-256")
public_key = private_key.public_key()
public_key2 = private_key2.public_key()
public_key3 = private_key3.public_key()
self.assertEqual(private_key, private_key2)
self.assertNotEqual(private_key, private_key3)
self.assertEqual(public_key, public_key2)
self.assertNotEqual(public_key, public_key3)
self.assertNotEqual(public_key, private_key)
class TestEccModule(unittest.TestCase):
def test_generate(self):
key = ECC.generate(curve="P-256")
self.assertTrue(key.has_private())
self.assertEqual(key.pointQ, EccPoint(_curve.Gx, _curve.Gy) * key.d)
# Other names
ECC.generate(curve="secp256r1")
ECC.generate(curve="prime256v1")
def test_construct(self):
key = ECC.construct(curve="P-256", d=1)
self.assertTrue(key.has_private())
self.assertEqual(key.pointQ, _curve.G)
key = ECC.construct(curve="P-256", point_x=_curve.Gx, point_y=_curve.Gy)
self.assertFalse(key.has_private())
self.assertEqual(key.pointQ, _curve.G)
# Other names
ECC.construct(curve="secp256r1", d=1)
ECC.construct(curve="prime256v1", d=1)
def test_negative_construct(self):
coord = dict(point_x=10, point_y=4)
coordG = dict(point_x=_curve.Gx, point_y=_curve.Gy)
self.assertRaises(ValueError, ECC.construct, curve="P-256", **coord)
self.assertRaises(ValueError, ECC.construct, curve="P-256", d=2, **coordG)
def get_tests(config={}):
tests = []
tests += list_test_cases(TestEccPoint_NIST)
tests += list_test_cases(TestEccPoint_PAI)
tests += list_test_cases(TestEccKey)
tests += list_test_cases(TestEccModule)
return tests
if __name__ == '__main__':
suite = lambda: unittest.TestSuite(get_tests())
unittest.main(defaultTest='suite')
|