| 1 |
import re |
| 2 |
import unittest |
| 3 |
from binascii import hexlify |
| 4 |
|
| 5 |
from Crypto.Util.py3compat import bord |
| 6 |
|
| 7 |
from Crypto.Hash import SHA256 |
| 8 |
from Crypto.PublicKey import ECC |
| 9 |
from Crypto.SelfTest.st_common import list_test_cases |
| 10 |
from Crypto.SelfTest.loader import load_test_vectors, load_test_vectors_wycheproof |
| 11 |
|
| 12 |
from Crypto.Protocol.DH import key_agreement |
| 13 |
|
| 14 |
|
| 15 |
class FIPS_ECDH_Tests_KAT(unittest.TestCase): |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
test_vectors_verify = load_test_vectors(("Protocol", ), |
| 20 |
"KAS_ECC_CDH_PrimitiveTest.txt", |
| 21 |
"ECC CDH Primitive (SP800-56A Section 5.7.1.2)", |
| 22 |
{ |
| 23 |
'qcavsx': lambda x: int(x, 16), |
| 24 |
'qcavsy': lambda x: int(x, 16), |
| 25 |
'diut': lambda x: int(x, 16), |
| 26 |
'qiutx': lambda x: int(x, 16), |
| 27 |
'qiuty': lambda x: int(x, 16), |
| 28 |
}) or [] |
| 29 |
|
| 30 |
for idx, tv in enumerate(test_vectors_verify): |
| 31 |
|
| 32 |
|
| 33 |
if isinstance(tv, str): |
| 34 |
res = re.match(r"\[([A-Za-z0-9-]+)\]", tv) |
| 35 |
assert res |
| 36 |
curve_name = res.group(1) |
| 37 |
continue |
| 38 |
|
| 39 |
public_key = ECC.construct(curve=curve_name, |
| 40 |
point_x=tv.qcavsx, |
| 41 |
point_y=tv.qcavsy) |
| 42 |
|
| 43 |
private_key = ECC.construct(curve=curve_name, |
| 44 |
d=tv.diut) |
| 45 |
|
| 46 |
exp_response = tv.ziut |
| 47 |
|
| 48 |
def ecdh_test(self, |
| 49 |
public_key=public_key, |
| 50 |
private_key=private_key, |
| 51 |
exp_response=exp_response): |
| 52 |
z = key_agreement( |
| 53 |
static_pub=public_key, |
| 54 |
static_priv=private_key, |
| 55 |
kdf=lambda x: x) |
| 56 |
self.assertEqual(z, exp_response) |
| 57 |
|
| 58 |
def ecdh_test_rev(self, |
| 59 |
public_key=public_key, |
| 60 |
private_key=private_key, |
| 61 |
exp_response=exp_response): |
| 62 |
z = key_agreement( |
| 63 |
static_pub=public_key, |
| 64 |
static_priv=private_key, |
| 65 |
kdf=lambda x: x) |
| 66 |
self.assertEqual(z, exp_response) |
| 67 |
|
| 68 |
setattr(FIPS_ECDH_Tests_KAT, "test_verify_positive_%d" % idx, ecdh_test) |
| 69 |
if idx == 1: |
| 70 |
setattr(FIPS_ECDH_Tests_KAT, "test_verify_positive_rev_%d" % idx, ecdh_test_rev) |
| 71 |
|
| 72 |
|
| 73 |
class TestVectorsECDHWycheproof(unittest.TestCase): |
| 74 |
|
| 75 |
desc = "Wycheproof ECDH tests" |
| 76 |
|
| 77 |
def add_tests(self, filename): |
| 78 |
|
| 79 |
def curve(g): |
| 80 |
return g['curve'] |
| 81 |
|
| 82 |
def private(u): |
| 83 |
return int(u['private'], 16) |
| 84 |
|
| 85 |
result = load_test_vectors_wycheproof(("Protocol", "wycheproof"), |
| 86 |
filename, |
| 87 |
"Wycheproof ECDH (%s)" |
| 88 |
% filename, |
| 89 |
group_tag={'curve': curve}, |
| 90 |
unit_tag={'private': private}, |
| 91 |
) |
| 92 |
self.tv += result |
| 93 |
|
| 94 |
def setUp(self): |
| 95 |
self.tv = [] |
| 96 |
self.desc = None |
| 97 |
|
| 98 |
self.add_tests("ecdh_secp224r1_ecpoint_test.json") |
| 99 |
self.add_tests("ecdh_secp256r1_ecpoint_test.json") |
| 100 |
self.add_tests("ecdh_secp384r1_ecpoint_test.json") |
| 101 |
self.add_tests("ecdh_secp521r1_ecpoint_test.json") |
| 102 |
|
| 103 |
self.add_tests("ecdh_secp224r1_test.json") |
| 104 |
self.add_tests("ecdh_secp256r1_test.json") |
| 105 |
self.add_tests("ecdh_secp384r1_test.json") |
| 106 |
self.add_tests("ecdh_secp521r1_test.json") |
| 107 |
|
| 108 |
def shortDescription(self): |
| 109 |
return self.desc |
| 110 |
|
| 111 |
def test_verify(self, tv): |
| 112 |
|
| 113 |
if len(tv.public) == 0: |
| 114 |
return |
| 115 |
|
| 116 |
try: |
| 117 |
if bord(tv.public[0]) == 4: |
| 118 |
public_key = ECC.import_key(tv.public, curve_name=tv.curve) |
| 119 |
else: |
| 120 |
public_key = ECC.import_key(tv.public) |
| 121 |
except ValueError: |
| 122 |
assert tv.warning or not tv.valid |
| 123 |
return |
| 124 |
|
| 125 |
private_key = ECC.construct(curve=tv.curve, d=tv.private) |
| 126 |
|
| 127 |
try: |
| 128 |
z = key_agreement(static_pub=public_key, |
| 129 |
static_priv=private_key, |
| 130 |
kdf=lambda x: x) |
| 131 |
except ValueError: |
| 132 |
assert not tv.valid |
| 133 |
except TypeError as e: |
| 134 |
assert not tv.valid |
| 135 |
assert "incompatible curve" in str(e) |
| 136 |
else: |
| 137 |
self.assertEqual(z, tv.shared) |
| 138 |
assert tv.valid |
| 139 |
|
| 140 |
def runTest(self): |
| 141 |
for tv in self.tv: |
| 142 |
self.desc = "Wycheproof ECDH Verify Test #%d (%s, %s)" % (tv.id, tv.comment, tv.filename) |
| 143 |
self.test_verify(tv) |
| 144 |
|
| 145 |
|
| 146 |
class ECDH_Tests(unittest.TestCase): |
| 147 |
|
| 148 |
static_priv = ECC.import_key('-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg9VHFVKh2a1aVFifH\n+BiyNaRa2kttEg3165Ye/dJxJ7KhRANCAARImIEXro5ZOcyWU2mq/+d79FEZXtTA\nbKkz1aICQXihQdCMzRNbeNtC9LFLzhu1slRKJ2xsDAlw9r6w6vwtkRzr\n-----END PRIVATE KEY-----') |
| 149 |
static_pub = ECC.import_key('-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgHhmv8zmZ+Nw8fsZd\ns8tlZflyfw2NE1CRS9DWr3Y3O46hRANCAAS3hZVUCbk+uk3w4S/YOraEVGG+WYpk\nNO/vrwzufUUks2GV2OnBQESe0EBk4Jq8gn4ij8Lvs3rZX2yT+XfeATYd\n-----END PRIVATE KEY-----').public_key() |
| 150 |
|
| 151 |
eph_priv = ECC.import_key('-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgGPdJmFFFKzLPspIr\nE1T2cEjeIf4ajS9CpneP0e2b3AyhRANCAAQBexAA5BYDcXHs2KOksTYUsst4HhPt\nkp0zkgI2virc3OGJFNGPaCCPfFCQJHwLRaEpiq3SoQlgoBwSc8ZPsl3y\n-----END PRIVATE KEY-----') |
| 152 |
|
| 153 |
eph_pub = ECC.import_key('-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghaVZXElSEGEojFKF\nOU0JCpxWUWHvWQUR81gwWrOp76ShRANCAATi1Ib2K+YR3AckD8wxypWef7pw5PRw\ntBaB3RDPyE7IjHZC6yu1DbcXoCdtaw+F5DM+4zpl59n5ZaIy/Yl1BdIy\n-----END PRIVATE KEY-----') |
| 154 |
|
| 155 |
def test_1(self): |
| 156 |
|
| 157 |
kdf = lambda x: SHA256.new(x).digest() |
| 158 |
z = key_agreement( |
| 159 |
kdf=kdf, |
| 160 |
static_pub=self.static_pub, |
| 161 |
static_priv=self.static_priv) |
| 162 |
self.assertEqual(hexlify(z), |
| 163 |
b"3960a1101d1193cbaffef4cc7202ebff783c22c6d2e0d5d530ffc66dc197ea9c") |
| 164 |
|
| 165 |
def test_2(self): |
| 166 |
|
| 167 |
kdf = lambda x: SHA256.new(x).digest() |
| 168 |
z = key_agreement( |
| 169 |
kdf=kdf, |
| 170 |
static_pub=self.static_pub, |
| 171 |
static_priv=self.static_priv, |
| 172 |
eph_pub=self.eph_pub, |
| 173 |
eph_priv=self.eph_priv) |
| 174 |
self.assertEqual(hexlify(z), |
| 175 |
b"7447b733d40c8fab2c633b3dc61e4a8c742f3a6af7e16fb0cc486f5bdb5d6ba2") |
| 176 |
|
| 177 |
def test_3(self): |
| 178 |
|
| 179 |
kdf = lambda x: SHA256.new(x).digest() |
| 180 |
z = key_agreement( |
| 181 |
kdf=kdf, |
| 182 |
static_pub=self.static_pub, |
| 183 |
static_priv=self.static_priv, |
| 184 |
eph_priv=self.eph_priv) |
| 185 |
self.assertEqual(hexlify(z), |
| 186 |
b"9e977ae45f33bf67f285d064d83e6632bcafe3a7d33fe571233bab4794ace759") |
| 187 |
|
| 188 |
def test_4(self): |
| 189 |
|
| 190 |
kdf = lambda x: SHA256.new(x).digest() |
| 191 |
z = key_agreement( |
| 192 |
kdf=kdf, |
| 193 |
static_pub=self.static_pub, |
| 194 |
static_priv=self.static_priv, |
| 195 |
eph_pub=self.eph_pub) |
| 196 |
self.assertEqual(hexlify(z), |
| 197 |
b"c9532df6aa7e9dbe5fe85da31ee25ff19c179c88691ec4b8328cc2036dcdadf2") |
| 198 |
|
| 199 |
def test_5(self): |
| 200 |
|
| 201 |
kdf = lambda x: SHA256.new(x).digest() |
| 202 |
self.assertRaises(ValueError, |
| 203 |
key_agreement, |
| 204 |
kdf=kdf, |
| 205 |
static_priv=self.static_priv, |
| 206 |
eph_pub=self.eph_pub, |
| 207 |
eph_priv=self.eph_priv) |
| 208 |
|
| 209 |
def test_6(self): |
| 210 |
|
| 211 |
kdf = lambda x: SHA256.new(x).digest() |
| 212 |
self.assertRaises(ValueError, |
| 213 |
key_agreement, |
| 214 |
kdf=kdf, |
| 215 |
static_pub=self.static_pub, |
| 216 |
eph_pub=self.eph_pub, |
| 217 |
eph_priv=self.eph_priv) |
| 218 |
|
| 219 |
def test_7(self): |
| 220 |
|
| 221 |
kdf = lambda x: SHA256.new(x).digest() |
| 222 |
z = key_agreement( |
| 223 |
kdf=kdf, |
| 224 |
eph_pub=self.eph_pub, |
| 225 |
eph_priv=self.eph_priv) |
| 226 |
self.assertEqual(hexlify(z), |
| 227 |
b"feb257ebe063078b1391aac07913283d7b642ad7df61b46dfc9cd6f420bb896a") |
| 228 |
|
| 229 |
def test_8(self): |
| 230 |
|
| 231 |
kdf = lambda x: SHA256.new(x).digest() |
| 232 |
z = key_agreement( |
| 233 |
kdf=kdf, |
| 234 |
static_priv=self.static_priv, |
| 235 |
eph_pub=self.eph_pub) |
| 236 |
self.assertEqual(hexlify(z), |
| 237 |
b"ee4dc995117476ed57fd17ff0ed44e9f0466d46b929443bc0db9380317583b04") |
| 238 |
|
| 239 |
def test_9(self): |
| 240 |
|
| 241 |
kdf = lambda x: SHA256.new(x).digest() |
| 242 |
z = key_agreement( |
| 243 |
kdf=kdf, |
| 244 |
static_pub=self.static_pub, |
| 245 |
eph_priv=self.eph_priv) |
| 246 |
self.assertEqual(hexlify(z), |
| 247 |
b"2351cc2014f7c40468fa072b5d30f706eeaeef7507311cd8e59bab3b43f03c51") |
| 248 |
|
| 249 |
def test_10(self): |
| 250 |
|
| 251 |
kdf = lambda x: SHA256.new(x).digest() |
| 252 |
self.assertRaises(ValueError, |
| 253 |
key_agreement, |
| 254 |
kdf=kdf, |
| 255 |
static_pub=self.static_pub, |
| 256 |
eph_pub=self.eph_pub) |
| 257 |
|
| 258 |
def test_11(self): |
| 259 |
|
| 260 |
kdf = lambda x: SHA256.new(x).digest() |
| 261 |
self.assertRaises(ValueError, |
| 262 |
key_agreement, |
| 263 |
kdf=kdf, |
| 264 |
static_priv=self.static_priv, |
| 265 |
eph_priv=self.eph_priv) |
| 266 |
|
| 267 |
def test_12(self): |
| 268 |
|
| 269 |
self.assertRaises(ValueError, |
| 270 |
key_agreement, |
| 271 |
static_pub=self.static_pub, |
| 272 |
static_priv=self.static_priv) |
| 273 |
|
| 274 |
|
| 275 |
def get_tests(config={}): |
| 276 |
|
| 277 |
tests = [] |
| 278 |
tests += list_test_cases(FIPS_ECDH_Tests_KAT) |
| 279 |
tests += [TestVectorsECDHWycheproof()] |
| 280 |
tests += list_test_cases(ECDH_Tests) |
| 281 |
|
| 282 |
slow_tests = config.get('slow_tests') |
| 283 |
if slow_tests: |
| 284 |
pass |
| 285 |
|
| 286 |
return tests |
| 287 |
|
| 288 |
|
| 289 |
if __name__ == '__main__': |
| 290 |
def suite(): |
| 291 |
return unittest.TestSuite(get_tests()) |
| 292 |
unittest.main(defaultTest='suite') |