1 Diffie-Hellman (DH) Key Exchange Protocol
1 Diffie-Hellman (DH) Key Exchange Protocol
Exchange Protocol:
Objective
The objective of this section is to:
1. Gain a deep understanding of the discrete logarithm problem (DLP) and its
role in the security of the DH protocol.
Let pbe a prime number, and g be a primitive root (or generator) of the
multiplicative group of integers modulo p. This means that g generates all
integers from 1 to p − 1when raised to powers modulo p.
For a given integer bsuch that 1 ≤ b < p , the discrete logarithm of b with
respect to g is the integer xsuch that: b = g
x
mod p
The DH protocol allows two parties (Alice and Bob) to establish a shared secret
key over an insecure channel. The steps are as follows:
Alice and Bob publicly agree on a large prime number pand a primitive
root g modulo p.
Alice selects a private key a(a random integer such that 1 < a < p − 1
Bob selects a private key b(a random integer such that 1 < b < p − 1
mod p = g
a.b
mod p
g
a.b
mod p
Both Alice and Bob now share the same secret S, which can be used as
a symmetric key for encryption.
3. Forward Secrecy:
If private keys aand bare ephemeral (used only once), the protocol
provides forward secrecy, it means that even if long-term keys are
compromised, the past communications remain secure.
2. Power Consumption:
Parameter DH ECDH
Assets:
Implementation of Diffie-Hellman key exchange in python:
dh.py
def get_strong_prime(bits):
def get_primitive_root(p):
# Returns a primitive root modulo p, i.e. a number g such that for all 0 < i < p-1
# This is useful for generating the public keys in the DH key exchange
for g in range(2, p):
if all(pow(g, i, p) != 1 for i in [2,(p-1)//2]):
return g
def generate_DH_params(bits):
# Generates a DH keypair of size bits
# This is useful for the DH key exchange
p = get_strong_prime(bits)
g = get_primitive_root(p)
return p, g
def key_exchange():
# Generates a DH keypair, and simulates the key exchange
p, g = generate_DH_params(512)
a = random.randint(1, p-1) # Alice's private key
b = random.randint(1, p-1) # Bob's private key
A = pow(g, a, p) # Alice's public key
B = pow(g, b, p) # Bob's public key
s1 = pow(B, a, p) # Alice's shared secret
s2 = pow(A, b, p) # Bob's shared secret
print(f"p: {p}")
print(f"g: {g}")
print(f"Alice's private key: {a}")
print(f"Bob's private key: {b}")
print('#'*30)
print("Key exchange successful!")
print(f"Alice's shared secret: {s1}")
print(f"Bob's shared secret: {s2}")
if __name__ == "__main__":
key_exchange()
References
1. Diffie, W., & Hellman, M. E. (1976). "New Directions in Cryptography." IEEE
Transactions on Information Theory.