Prerequisites:
Suppose Alice and Bob want to talk to each other secretly, so they decide that they will encrypt messages and send it over the insecure channel for communication. They will have to decide upon a key to encrypt the messages, and this key cannot pass through the same insecure channel because if an attacker knows the key, plaintext can be easily be recovered from the ciphertext.
To overcome this problem, there is an algorithm- Diffie Hellman Key Exchange created by Diffie and Hellman for secure exchange of keys over the insecure channel. The algorithm is based on various Number Theoretic concepts, as we will see in the next few sections.
This write-up is primarily for Algebraic Diffie Hellman Key Exchange, there is another section in the same directory for Elliptic Curve based Diffie Hellman Key Exchange (ECDH and ECDHE).
Please note that the security of Diffie-Hellman is primarily based on the security of Discrete Logarithm Problem(DLP), so it is strongly recommended that you read and get familiar with DLP before moving on further.
This directory also contains various complications involved while implementing Diffie Hellman Key Exchange:
- DH groups with smooth order
- Small subgroup attacks on DH
- Ensuring Forward Secrecy: Ephemeral Diffie-Hellman - TBA
Case Scenario: Suppose Alice wants to send a message to Bob. Consider Alice as the first person who will generate public key and share it with Bob publicly.
There are a number of steps involved in Diffie Hellman Key Exchange. This is just to give you an idea of how the protocol works, we will discuss public key generation and shared key calculation in detail in the upcoming sections:
- Group Selection Agreement
- Public Key Generation- Alice's Side
- Public Key Generation- Bob's Side
- Shared Key Calculation- Bob's Side
- Shared Key Calculation- Alice's Side
Variable Notation:
G
: Algebraic group over which DH is definedg
: Base point generator of the algebraic groupG
p
: Modulus of the groupG
q
: Number of elements in the groupG
x_a
: Alice's secret keyx_b
: Bob's secret keyA
: Alice's public keyB
: Bob's public keyk
: Shared key
In this step, Both Alice (Sender) and Bob (Receiver) agree on an algebraic group G
= and compute it's generator g
that they will use for calculating respective private and public keys.
For security purposes, the cardinality of the group should be large, otherwise the DLP can be solved in polynomial time. Also, it is essential to keep modulus p
of the form 2*r + 1
, where r
another prime number.
To find out the multiplicative generator of an algebraic Cyclic Group, you can use sagemath:
p = (1 << 1024) - 1093337
G = IntegerModRing(p)
G.multiplicative_generator()
> 7 # Generator for the Cyclic Group Z_p*
In this step, Alice and Bob calculate their respective private-public keys
To generate the public key, Alice does the following computation:
- Generates
x_a
using a cryptographically secure pseudo random number generator - She computes
- Lastly, she sends
A
to Bob over the insecure channel
To generate the public key, Bob does the following computation:
- Generates
x_b
using a cryptographically secure pseudo random number generator - He computes
- Lastly, he sends
B
to Alice over the insecure channel
After Alice receives Bob's public key B
, she calculates the following:
After Bob receives Alice's public key A
, he calculates the following:
Notice that _key_a
and _key_b
have the same values:
Hence both of them have calculated the same value, securely. Question- How "securely"? Let us see how.
Consider an attacker who was somehow able to intercept the communication established between Alice and Bob. In such cases, the attacker will only have information of A
, B
, g
and G
, since only this information is sent via the insecure channel. Remember that secret keys are not exchanged, only Alice knows her secret key x
and only Bob knows his secret key y
.
Hence, having information of A
, B
, g
and G
is not sufficient to calculate the shared secret!
Another Question: What if the attacker is able to solve any of the Discrete Logarithm Problems: or ?
Clearly, he/she will then be able to calculate the shared secret, breaking the security of Diffie Hellman Key Exchange.
I have written a simple implementation of Diffie Hellman Key Exchange here: example.py
If you want to read more details about DHK, I strongly recommend you to read Page 401-406 of the textbook: "A graduate course on Applied Cryptography": version-0.4