In SSH's transport protocol, how do client and server pick each algorithm from their SSH_MSG_KEXINIT lists, and whose preference order wins?
answer
- both sides speak at once
- ordered name-lists per category
- walk one side's list
- first H becomes the session identifier
basics
~20 sBoth sides send SSH_MSG_KEXINIT with ordered name-lists per category. For each category SSH takes the first algorithm on the client's list that the server also supports, so the client's order wins and the server's list only limits the choice.
solid answer
~50 sAfter the version banners, each side sends `SSH_MSG_KEXINIT`: a random cookie and name-lists for key exchange, server host-key algorithms, and ciphers, MACs and compression in each direction. RFC 4253 section 7.1 says to pick, per category, the **first algorithm on the client's list that the server also supports**, with an extra check that the key exchange and host-key choices fit together, and to disconnect if a category has no match. So the client's order decides; the server's list only says what is allowed, and OpenSSH's sshd_config(5) states that the server's ordering is not important. The chosen method yields a shared secret K and an **exchange hash H** over both banners, both KEXINIT payloads, the host key and the exchange values. The server signs H with its host key, and the first H becomes the **session identifier** that user authentication later signs.
go deeper
Remember that SSH negotiates its algorithms at the start of every connection and that the client's order of preference is the one that counts.
Walk through KEXINIT's lists, the first-client-match rule per category, the exchange hash inputs, and how the first hash becomes the session identifier.
Use the rule in practice: read a negotiation failure by category, explain why tuning server order does nothing, and show how hashing the offers blocks downgrades.
Weigh where algorithm policy should live, client defaults versus server allow-lists, and how a fleet moves to new algorithms without stranding older peers.
## The version exchange and SSH_MSG_KEXINIT An SSH connection starts in clear text. Each side sends an identification line beginning `SSH-2.0-` followed by its software name and version, and then both send `SSH_MSG_KEXINIT` without waiting for the other (RFC 4253 section 7.1). The message carries: - a 16-byte random **cookie**, so neither side alone can determine the keys or the session identifier; - `kex_algorithms`, the key exchange methods; - `server_host_key_algorithms`, the signature algorithms for the server's host key; - `encryption_algorithms`, `mac_algorithms` and `compression_algorithms`, each **twice**, client-to-server and server-to-client; - language lists, normally empty; - `first_kex_packet_follows`, which says whether the sender has already sent a guessed first key exchange packet. Every list is ordered from most to least preferred. Because the messages travel before any encryption, an observer can read every offer, which is why the outcome is later bound into the exchange hash. ## The selection rule RFC 4253 fixes one rule, applied category by category: 1. Walk the **client's** list in order. 2. Take the first algorithm the **server** also lists. 3. For key exchange, also require a compatible host-key algorithm: a method that needs a signature-capable host key is chosen only if the server lists one the client also accepts. 4. If nothing qualifies, both sides disconnect. The guess flag is a shortcut on top of this: if both sides put the same key exchange method first, that method is used and a guessed packet can be kept. The result is the same as the main rule, because that method is then also the client's first choice. ```pseudocode for each category in [kex, host_key, cipher_c2s, cipher_s2c, mac_c2s, mac_s2c, comp_c2s, comp_s2c]: chosen = none for alg in client_list[category]: # client's order if alg in server_list[category] and fits_other_choices(alg): chosen = alg break if chosen is none: disconnect("no matching " + category) ``` ## A worked example | Client offers (in order) | Server offers | Chosen | |---|---|---| | `mlkem768x25519-sha256`, `curve25519-sha256` | `curve25519-sha256`, `mlkem768x25519-sha256` | `mlkem768x25519-sha256` | | `mlkem768x25519-sha256`, `curve25519-sha256` | `curve25519-sha256`, `diffie-hellman-group16-sha512` | `curve25519-sha256` | | `curve25519-sha256`, `diffie-hellman-group14-sha256` | `diffie-hellman-group1-sha1` | none, disconnect | The first row is the point interviewers probe: the server put `curve25519-sha256` first, and it did not matter. ## Markers that ride in the list The `kex_algorithms` list in the *first* KEXINIT also carries names that are not algorithms at all. RFC 8308 has the client add `ext-info-c` and the server `ext-info-s` to say they accept an `SSH_MSG_EXT_INFO` extension message; the two names differ precisely so they can never match and be "chosen". If one ever ended up negotiated, both sides must disconnect. OpenSSH's strict key exchange markers work the same way, one name for the client and another for the server. So when you read a captured offer, not every entry is a candidate method. ## What comes out of the exchange The negotiated method produces two values: - a **shared secret K**, from Diffie-Hellman, elliptic-curve Diffie-Hellman or a hybrid post-quantum method; - an **exchange hash H**, computed over the client's and server's identification strings, both `SSH_MSG_KEXINIT` payloads, the server's public host key and the exchange values. The server signs H with its host key. By verifying that signature against the key it trusts for that host, the client learns that the real server took part in *this* exchange. Encryption keys, IVs and MAC keys are then derived from K, H and the session identifier with distinct letters, for example `HASH(K || H || "C" || session_id)` for the client-to-server encryption key, and `SSH_MSG_NEWKEYS` switches them on. The **session identifier** is H from the *first* key exchange. Re-keying later produces new keys, but the identifier never changes. User authentication signs it, which ties a login proof to this one connection. ## Why the offers are hashed Because the KEXINIT payloads go into H, an attacker who edits an offer in transit, say to strip strong algorithms, makes the client and server compute different hashes. The server's signature then fails to verify on the client, and the downgrade is caught. The hash covers only selected values, not every packet, and that gap is what the Terrapin attack later exploited. ## What it means for the person running SSH - To change which algorithm is *preferred*, change the **client's** list; reordering the server's list has no effect. - A server can only *forbid* an algorithm by leaving it out. Which lines to trim in a server's configuration is part of hardening sshd. - A negotiation failure names the category that found no match, which tells you whether to look at key exchange, host keys, ciphers or MACs. - `ssh -Q kex`, `ssh -Q cipher` and `ssh -Q mac` list what the local OpenSSH implements, which is wider than what it offers by default.
- An administrator moves curve25519-sha256 to the front of sshd's KexAlgorithms. Will clients now use it first?No. The server's order does not change the outcome; the client's first algorithm that the server also supports is chosen, so a client preferring `mlkem768x25519-sha256` still gets it if the server lists it anywhere. A server can only stop an algorithm by removing it from its list.
- Why does the exchange hash include both SSH_MSG_KEXINIT payloads?It makes negotiation tamper-evident. Each side hashes what it sent and what it received; if an attacker altered an offer, the two hashes differ and the server's host-key signature over its hash fails to verify on the client, so a stripped-down offer cannot be slipped in silently.
- What happens to the session identifier when an SSH connection re-keys?Nothing. RFC 4253 keeps the exchange hash of the first key exchange as the session identifier for the connection's lifetime; re-keying derives new encryption and MAC keys but the identifier, which authentication signatures cover, stays the same.
- The server's preference order decides which algorithm is used.
- Both sides automatically pick the strongest algorithm they have in common.
- The session identifier changes every time the connection re-keys.
- KEXINIT is encrypted, so an observer cannot see the offered algorithms.
- Reordering sshd's algorithm list makes clients prefer a different algorithm.