MCPcopy
hub / github.com/pyca/cryptography / test_ecdh

Function test_ecdh

tests/wycheproof/test_ecdh.py:50–82  ·  view source on GitHub ↗
(backend, wycheproof)

Source from the content-addressed store, hash-verified

48 "ecdh_secp521r1_test.json",
49)
50def test_ecdh(backend, wycheproof):
51 curve = _CURVES[wycheproof.testgroup["curve"]]
52 if curve is None:
53 pytest.skip(
54 "Unsupported curve ({})".format(wycheproof.testgroup["curve"])
55 )
56 _skip_exchange_algorithm_unsupported(backend, ec.ECDH(), curve)
57 private_key = wycheproof.cache_value_to_group(
58 f"private_key_{wycheproof.testcase['private']}",
59 lambda: ec.derive_private_key(
60 int(wycheproof.testcase["private"], 16), curve
61 ),
62 )
63
64 try:
65 # caching these values shows no performance improvement
66 public_key = serialization.load_der_public_key(
67 binascii.unhexlify(wycheproof.testcase["public"]), backend
68 )
69 assert isinstance(public_key, ec.EllipticCurvePublicKey)
70 except ValueError:
71 assert wycheproof.invalid or wycheproof.acceptable
72 return
73 except UnsupportedAlgorithm:
74 return
75
76 if wycheproof.valid or wycheproof.acceptable:
77 computed_shared = private_key.exchange(ec.ECDH(), public_key)
78 expected_shared = binascii.unhexlify(wycheproof.testcase["shared"])
79 assert computed_shared == expected_shared
80 else:
81 with pytest.raises(ValueError):
82 private_key.exchange(ec.ECDH(), public_key)
83
84
85@wycheproof_tests(

Callers

nothing calls this directly

Calls 4

cache_value_to_groupMethod · 0.80
unhexlifyMethod · 0.80
exchangeMethod · 0.45

Tested by

no test coverage detected