Understanding EC Diffie-Hellman
By: Pierre Philip Du Preez
Tags: Security, Programming, Elliptic Curve, Cryptography, Encryption

If you’ve worked with web servers, the chances are that you’ve come across the Elliptic-curve Diffie–Hellman (ECDH) or Elliptic-curve Diffie–Hellman Ephemeral (ECDHE) cipher suites. You might have wondered what these suites implement and how they work. That is what we will discuss today.

To understand ECDH, we first have to dive into the standard Diffie-Hellman key exchange protocol. It was one of the first public-key protocols to be designed, back in 1976, and is still widely used today. It was named after Whitfield Diffie and Martin Hellman — both outstanding cryptographers that left their mark on the community, they were both part of the three-person team that invented public-key cryptography.

Traditionally, when one implemented encryption (symmetrical encryption), you would need to exchange a secret via a secure channel (could be of any form of transmission) — between two parties. The inherent flaw with this methodology is that a party can intercept the secret if they are privy to the communication channel. This secret can then be used to decrypt the encrypted data between the two parties, rendering it useless.

Locked — but useless

Cryptographic Keys

In cryptography, a key is a string of characters used within an encryption algorithm for altering data so that it appears random. Like a physical key, it locks (encrypts) data so that only someone with the right key can unlock (decrypt) it. It is important to note that every time you use the same key on the same data the result will be the same.

A key usually conforms to a certain size in bits. The use with symmetric encryption such as AES 256 the key sizes vary from 128 to 256 bits. In DH the key size is recommended to be upwards of 2000 bits where the same level of security can be achieved in ECDH with 250 bits. One can have a look at the Keylength website to get a better understanding of which key size is relevant to their implementation.

Diffie-Hellman

Diffie-Hellman allows the two parties, mentioned above, to exchange their secret without the need for a secure channel to transfer the secret. In fact, this can be used across any unsecured channel. The secret generated by the exchange can then be used to encrypt subsequent communications utilizing symmetrical encryption with the generated secret.

As mentioned in my previous article, I brushed across the basics of this, but we will go into more detail here. An analogy illustrates the concept of public key exchange by using colours instead of very large numbers which are actually used during the process.

Key exchange

Suppose a third party were to inspect the exchange on the unsecured communication. They would only see the common colour (yellow in this case) and the first set of mixed colours (light orange and light blue). It would be extremely difficult for the third part to compute the final colour. Instead of colours, extremely large numbers are used; this determination is computationally expensive. It is infeasible to try and calculate the final colour, even for modern-day supercomputers.

Breakdown

1 - I generate a prime number (called p) and a number (called g) which is coprime to p-1. I then tell you both those numbers. 2 - You pick a secret (called a). You then compute (gᵃ modulo p)(called A) and send that back to me. 3 - I do the same and pick a secret (called b). (gᵇ modulo p) (called B) and send that to you. 4 - Use the number I sent you to do the same operation. (Bᵃ modulo p) 5 - I do the same operation with A (Aᵇ modulo p) The numbers we get at 4 and 5 are the same.

So that we have the same numbers at step 4 and 5, we can then use that value as our secret for symmetrical encryption. The math above really comes down to properties of modulo exponents.