In the previous parts of this blog series I introduced the general principle of asymmetric cryptography and explained how one can use the Diffie-Hellman Key Exchange to establish a shared secret among multiple parties over an unsecure channel. In this blog I will introduce you to Elliptic Curve Cryptography (ECC), which allows using shorter keys than, for example, the DH key exchange or the RSA cryptosystem.
An elliptic curve is a collection of points space that satisfy the equation y2 = x3 + ax2 + bx + c1,2. For most practical uses a depressed cubic curve is sufficient, which means that a = 0 and therefore y2 = x3 + ax + b (using reassigned coefficients). If we choose a = -1 and b = 1, the curve will look like this:
To use the elliptic curves for asymmetric cryptography in a way similar to the DH key exchange, we will need to define an Abelian group that is based on the curve. To do this as a first step there needs to be a group operation *. For elliptic curves this operation is called addition and behaves as follows:
- To add two distinct points P1 = (x1,y2) and P2 = (x2,y2), connect the points in a straight line and draw that line further until it intersects the curve again in a point P3. Mirror this point on the x-axis and you get the result of P1+P2. Thus R = (xr,yr) = (δ – x1 – x2, δ(x1 – xr) – y1) while δ = (y2 – y1)/(x2 – x1).
- To add a point to itself one cannot connect the two addends. Instead you just use the tangent on the curve in that point and thus get to P3. The rest works the same as in 1, therefore R = (xr,yr) = (δ – x1 – x2, δ(x1 – xr) – y1) while δ = (3x12 + b)/2y1.
- Connecting two points where x1 = x2 will only intersect the curve a third time at infinity. Therefore we introduce the point (∞,∞) and add it to the group as the identity element.
It is easy to see, that this leads to the satisfaction of four out of five requirements of an Abelian group3.
For the same reason as in the “normal” Diffie-Hellman algorithm, we do not want to have an infinite amount of points so that we take all the points on the curve modulo a certain number. Geometrically this is quite counter-intuitive but algebraically it works well. Say we take the curve from figure 1 and use modulo 7, which will lead to the following points:
- x=0; y2=1 → y=1 or y=6
- x=1; y2=1 → y=1 or y=6
- x=2; y2=0 → y=0
- x=3; y2=4 → y=2 or y=5
- x=4; y2=5 → no integer square root
- x=5; y2=2 → no integer square root
- x=6; y2=1 → y=1 or y=6
- x=∞; → y=∞
This Abelian group over a discrete elliptic curve can be used for cryptography similarly to the previous blog post, which will be explained in the following section.
Elliptic Curve Diffie-Hellman (ECDH)
Like exponentiation on integers, multiplication4 on elliptic curves is a one-way function and therefore can be used for the Diffie-Hellman key exchange in a similar way. Alice and Bob agree on a discrete elliptic curve and a specific generator point, i.e. the following parameters:
- p: the prime number that is the order of the group.
- a & b: the coefficients for the elliptic curve.
- xg & yg: the coordinates of the generator point G
- q: the order of the sub-group generated by G
- h: the cofactor of the curve, which is the ratio between the number of points on the curve and the number of points generated by G
First of all they both secretly create their private keys α and β, which are both integers while 0 < α,β < p holds. They then create their public keys A = αG and B = βG and share them with their counterparts. All parties can now calculate the shared secret S = αB = αβG = βαG = βA and use it for symmetric encryption.
Gaining authenticity is quite similar to ECDH and works as follows. Say Alice wants to send an encrypted message to Bob using his public key B. What she must do first is to create a random number r (0 < r < q). She then generates a point R on the curve by multiplying r with the generator point G (R = rG). Additionally she creates the (symmetric) encryption key S which is Bob’s public key B multiplied with her random number r (S = rB) and uses this to encrypt the message. She then sends R along with the ciphertext to Bob.
Upon reception of the point-ciphertext pair, Bob multiplies R with his private key β, which leads to βR = βrG = rβG = rB = S to restore the shared secret which he uses to decrypt Alices’ message.
To sign her message Alice will first create a random number k (0 < k < q). Similarly to encryption she generates a point R using that number (R = kG) and uses its x-coordinate as r (mod q). Note that r must not be zero. She now computes s as in s = (h + αr)/k mod q, h being the hashed message to be signed, and sends the pair (r,s) along with the message to Bob.
To verify Alice’s signature he calculates a point P as follows: P = (h/s)G + (r/s)A mod q. He then compares xp (i.e. the x-coordinate of point P) and r. If they are equal, the signature is valid5.
A note on the random number k
It is very important that Alice chooses a different k for each signature she creates. Refraining from doing so will lead to a situation where a third-party can easily recover Alice’s private key α. This can be seen as follows: Using k for two different signatures will lead to r1 = kG = r2 and therefore s1 = (h1 + αr)/k mod q and s2 = (h2 + αr)/k mod q which is a system of two linear equations with two unknowns (k and α), which is trivial to solve.
1. Elliptic curves do not represent ellipses! The name stems from the calculation of arc lengths of ellipses.
2. Additionally the curve must be non-singular, meaning the discriminant must not be zero.
3. Except for associativity which can be proven as shown here.
4. As in simple arithmetics, multiplication of a point on an elliptic curve with a scalar n is the same as adding that point n times to itself.
5. The proof of this is left as an exercise for the reader.