As we saw in the architecture lesson, the Transport Layer begins with the Key Exchange (Kex). The goal of Kex is for the client and server to derive a shared symmetric key without ever sending that key across the wire. This is where the 'math' of SSH happens.
In standard DH, the server provides a prime number $p$ and a generator $g$. Both parties pick secret numbers and exchange public values. The resulting shared secret is then used to derive the encryption keys. However, if the server uses a 'small' or 'common' prime, an attacker can pre-compute the logarithms (the Logjam attack).
๐ก To prevent this, modern SSH uses 'Group Exchange', where the client can request a specific range of prime sizes, ensuring the prime is large enough (e.g., 2048 or 4096 bits) to be secure.
The output above shows the server proposing `g14`, which is a standardized 2048-bit MODP group. The client will accept this if it is in its allowed list of algorithms.
Elliptic Curve Diffie-Hellman (ECDH) is significantly faster and more secure than traditional DH. The current gold standard in SSH is `curve25519-sha256@libssh.org`. It uses a specific elliptic curve that is designed to be immune to many side-channel attacks and offers high security with very small keys (256-bit).
โ ๏ธ Using traditional RSA or DH for key exchange is slower and requires much larger keys to maintain the same security level as Curve25519.
| Algorithm | Basis | Key Size | Security Level |
|---|---|---|---|
| DH Group 14 | Modular Exponentiation | 2048 bit | Medium |
| DH Group 16 | Modular Exponentiation | 4096 bit | High |
| ECDH (NIST P-256) | Elliptic Curve | 256 bit | High |
| Curve25519 | Montgomery Curve | 256 bit | Very High |
Once the Kex is complete, the shared secret is not used directly. Instead, it is passed through a Hash function (like SHA-256) to generate: 1. The symmetric encryption key. 2. The MAC key for integrity. 3. The IV (Initialization Vector). This ensures that even if one key is compromised, the others remain secure.
If you use a generic, default DH group found in many old tutorials, you are susceptible to 'pre-computation' attacks by well-funded adversaries.
Verify exercises to earn โ 150 XP and unlock next lab level.