Diffie-Hellman (DH) is a mechanism that allows two parties to create a shared secret over a public, insecure channel also known as a key exchange. Since all message exchanges are public the eavesdropper will not be able to determine the shared secret.
Both parties agree on a shared public base, which is known to everyone, including potential eavesdroppers. Each party then generates its own public key, which can also be shared openly. In addition, each party has a private key that is kept strictly secret and never transmitted.
A cornerstone of this mechanism is that deriving the private key from the public key is considered computationally infeasible, meaning it would take an impractically long time to compute.
Charlie and Brianna want to establish a shared secret they need the following elements:
Public Base Value that everyone (including an eavesdropper) can see
Private Key Value is private (to only one party) often a randomly generated number.
Public key Value is calcualted from the private key and the base
Charlie and Brianna to exchange their public keys (if they havent already in advance) keeping the public key secret adds a layer of obscuration. An eavesdropper now knows the public base and the public keys—but they still don't know the private keys.
Charlie Public Key C-Pub
Charlie Private Key C-Priv
Public Base Base
Brianna Public key B-Pub
Brianna Private key B-Priv
Public Base Base
Q(x,y)
d
G(x,y)
Expresison I: Charlie mixes his Private Key (C-Priv) with Briana’s Public key (B-Pub)
Expresison II: Briana’s mixes her Private Key (B-Priv) with Charlie's Public key (C-Pub)
The result is always the same identical secret and is a product of 3 keys.
The defacto standard for performing a Diffie-Hellman (DH) key exchange is Elliptic Curve Cryptography (ECC). The original DH protocol was built on the mathematics of finite fields, the industry has since migrated to elliptic curves because its is currently the most effiicent in key sizes and computation.
Generator Point
The generator point G(x,y) base point on the eliptic curve is a constant value chosen by curve designer insitution NIST. Both parties and the public has this value its not a secret. A public key is produced my multiplying the privatekey with generator. Q = d x G
Charlie’s Keys
Charlie has generated a P256 public key from his private key using the generator point G(x,y) and the elliptic curve. He can now safely send his public key to Briana.
Briana’s Keys
Briana has generated a P256 public key from her private key using the generator point G(x,y) and the elliptic curve. She can now safely send his public key to Charlie.
Shared Secret
Once Charlie and Briana mix their keys (see Expression I & II) the resulting shared secret is identical. They have never shared sensitive information to the public or an eavesdropper. They can now use this secret generate a key and use it to encrypt their communication using a cryptographic cipher.
ECDH is not used to encrypt information it sole purpose is to establish a shared secret between two parties withouth eavesdropping because no sensitive information is ever transmitted. The shared key is just an x coordinate of a point P(x,y) on the eliptic curve. For an eliptic curve there are two possible y values both parties generate P(x,y) point. For common usecases the P(y) value is disregarded.
The size of the shared secret will depend on the exact curve used. P256 (secp256r1) generates a 256 bit shared secret, P521 (secp521r1) respectively generates a 521 bits key. Using AES256 technically would technically allow to use the 256 shared secret directly as a key for AES Encryption but since it reveals some information you would want an Key Derrivation Function KDF to transform the secret into an random looking cryptograhic key.