| 1 |
from Crypto.Util.number import long_to_bytes |
| 2 |
from Crypto.PublicKey.ECC import EccKey |
| 3 |
|
| 4 |
|
| 5 |
def _compute_ecdh(key_priv, key_pub): |
| 6 |
|
| 7 |
pointP = key_pub.pointQ * key_priv.d |
| 8 |
if pointP.is_point_at_infinity(): |
| 9 |
raise ValueError("Invalid ECDH point") |
| 10 |
z = long_to_bytes(pointP.x, pointP.size_in_bytes()) |
| 11 |
return z |
| 12 |
|
| 13 |
|
| 14 |
def key_agreement(**kwargs): |
| 15 |
"""Perform a Diffie-Hellman key agreement. |
| 16 |
|
| 17 |
Keywords: |
| 18 |
kdf (callable): |
| 19 |
A key derivation function that accepts ``bytes`` as input and returns |
| 20 |
``bytes``. |
| 21 |
static_priv (EccKey): |
| 22 |
The local static private key. Optional. |
| 23 |
static_pub (EccKey): |
| 24 |
The static public key that belongs to the peer. Optional. |
| 25 |
eph_priv (EccKey): |
| 26 |
The local ephemeral private key, generated for this session. Optional. |
| 27 |
eph_pub (EccKey): |
| 28 |
The ephemeral public key, received from the peer for this session. Optional. |
| 29 |
|
| 30 |
At least two keys must be passed, of which one is a private key and one |
| 31 |
a public key. |
| 32 |
|
| 33 |
Returns (bytes): |
| 34 |
The derived secret key material. |
| 35 |
""" |
| 36 |
|
| 37 |
static_priv = kwargs.get('static_priv', None) |
| 38 |
static_pub = kwargs.get('static_pub', None) |
| 39 |
eph_priv = kwargs.get('eph_priv', None) |
| 40 |
eph_pub = kwargs.get('eph_pub', None) |
| 41 |
kdf = kwargs.get('kdf', None) |
| 42 |
|
| 43 |
if kdf is None: |
| 44 |
raise ValueError("'kdf' is mandatory") |
| 45 |
|
| 46 |
count_priv = 0 |
| 47 |
count_pub = 0 |
| 48 |
curve = None |
| 49 |
|
| 50 |
def check_curve(curve, key, name, private): |
| 51 |
if not isinstance(key, EccKey): |
| 52 |
raise TypeError("'%s' must be an ECC key" % name) |
| 53 |
if private and not key.has_private(): |
| 54 |
raise TypeError("'%s' must be a private ECC key" % name) |
| 55 |
if curve is None: |
| 56 |
curve = key.curve |
| 57 |
elif curve != key.curve: |
| 58 |
raise TypeError("'%s' is defined on an incompatible curve" % name) |
| 59 |
return curve |
| 60 |
|
| 61 |
if static_priv is not None: |
| 62 |
curve = check_curve(curve, static_priv, 'static_priv', True) |
| 63 |
count_priv += 1 |
| 64 |
|
| 65 |
if static_pub is not None: |
| 66 |
curve = check_curve(curve, static_pub, 'static_pub', False) |
| 67 |
count_pub += 1 |
| 68 |
|
| 69 |
if eph_priv is not None: |
| 70 |
curve = check_curve(curve, eph_priv, 'eph_priv', True) |
| 71 |
count_priv += 1 |
| 72 |
|
| 73 |
if eph_pub is not None: |
| 74 |
curve = check_curve(curve, eph_pub, 'eph_pub', False) |
| 75 |
count_pub += 1 |
| 76 |
|
| 77 |
if (count_priv + count_pub) < 2 or count_priv == 0 or count_pub == 0: |
| 78 |
raise ValueError("Too few keys for the ECDH key agreement") |
| 79 |
|
| 80 |
Zs = b'' |
| 81 |
Ze = b'' |
| 82 |
|
| 83 |
if static_priv and static_pub: |
| 84 |
|
| 85 |
Zs = _compute_ecdh(static_priv, static_pub) |
| 86 |
|
| 87 |
if eph_priv and eph_pub: |
| 88 |
|
| 89 |
if bool(static_priv) != bool(static_pub): |
| 90 |
raise ValueError("DH mode C(2e, 1s) is not supported") |
| 91 |
Ze = _compute_ecdh(eph_priv, eph_pub) |
| 92 |
elif eph_priv and static_pub: |
| 93 |
|
| 94 |
Ze = _compute_ecdh(eph_priv, static_pub) |
| 95 |
elif eph_pub and static_priv: |
| 96 |
|
| 97 |
Ze = _compute_ecdh(static_priv, eph_pub) |
| 98 |
|
| 99 |
Z = Ze + Zs |
| 100 |
|
| 101 |
return kdf(Z) |