Changing My SSH Keys for the Second Time This Year
Early December brought a pile of cryptography news:
- The NSA advises moving to post-quantum cryptography urgently and, for now, replacing P-256 and Ed25519 with P-384;
- Bruce Schneier (a well-known cryptographer, author of "Applied Cryptography") writes that, according to a reliable source, P-256 is broken;
- Chrome will stop trusting certificate authorities on P-256 from March 2027, and Firefox is moving away from it too;
- OpenSSH's
ssh-keygennow creates a P-384 key by default (not in a release yet).
Earlier, in October, Anthropic showed where the seeds of the NIST curves came from (P-256 and P-384 are among them). All this made a lot of noise, so I need to react somehow.
This year I already changed my SSH keys when I moved to the Secure Enclave (an isolated block inside the Mac's processor that the private key never leaves). I moved because of the spread of coding agents, so that none of them could accidentally read or leak the key.
This time the Secure Enclave won't do, because ssh on the Mac only takes P-256 from it (details at the end). So I'm going back to a regular key in a file.
Two weak spots
When connecting over SSH, the client and the server first agree on the key that encrypts the traffic (key exchange, KexAlgorithms). Then the server checks who is connecting with a pair of public and private keys (the public one is on the server, the private one is in our ~/.ssh). Both steps are potentially vulnerable.
Weak spot 1: key exchange
Let's assume the traffic is being recorded. If a weakness is found in the key exchange algorithm, all the traffic recorded so far can be decrypted. A hybrid exchange protects against this: the session key is built from two exchanges at once, a classic one and a post-quantum one, and both have to be broken to decrypt the traffic.
Although OpenSSH switched to such an exchange by default back in 2025 (mlkem768x25519-sha256), the system config in macOS 27.2.1 still puts the exchange on the P-256 curve first (ecdh-sha2-nistp256 in /etc/ssh/crypto.conf). Judging by the comment in the file, Apple favors fast FIPS algorithms (US standards for government systems), so let's fix it ourselves. Maybe Apple will change it on its own, as it did with Keychain.
The fix itself is one line at the top of ~/.ssh/config:
KexAlgorithms ^mlkem768x25519-sha256,sntrup761x25519-sha512,sntr******************@openssh.com
The ^ puts these algorithms at the front of the list (sntrup761… is an older post-quantum hybrid for servers that don't know ML-KEM). The other algorithms stay as fallbacks for really old servers.
To check what was negotiated:
ssh -v g**@github.com 2>&1 | grep 'kex: algorithm'
# debug1: kex: algorithm: mlkem768x25519-sha256
A quick word on ML-KEM and sntrup761. These are post-quantum algorithms, built not on curves but on lattices. The security of a curve rests on a problem that a quantum computer solves with Shor's algorithm. In both post-quantum algorithms, the secret is hidden in a system of linear equations with a little noise (in sntrup761, rounding plays the role of noise). Without the noise the system is easy to solve, but with it no fast algorithms are known yet.
Weak spot 2: the login key
A public key isn't a secret, and it's assumed that anyone may know it (on GitHub, for example, anyone's keys are public at github.com/<username>.keys). If the key's algorithm is broken, the private key can be recovered from the public one. One line won't fix this, it takes a new key. GitHub doesn't accept post-quantum keys (ML-DSA) yet, so I took P-384 (P-521 or RSA of 3072 bits and up would also do).
Creating the new key:
ssh-keygen -t ecdsa -b 384 -f ~/.ssh/id_ecdsa_p384
The key sits in a file, so it must have a passphrase. There's no need to type it every time: on the Mac, UseKeychain takes care of that. With it, ssh asks for the passphrase on the first login, saves it in Keychain and takes it from there afterwards. AddKeysToAgent 1h keeps the decrypted key in ssh-agent for an hour.
Host *
IdentityFile ~/.ssh/id_ecdsa_p384
IdentitiesOnly yes
UseKeychain yes
AddKeysToAgent 1h
IdentitiesOnly yes makes ssh try only this key, not everything available. If the server doesn't accept it, ssh fails with an error. Another key can always be passed by hand with -i.
The passphrase doesn't have to be written down anywhere (I've never even seen mine). Instead of the ssh-keygen above, this command for bash or zsh generates a long random passphrase, creates the key and puts the passphrase straight into Keychain:
k=~/.ssh/id_ecdsa_p384 a=$(mktemp) && \
export PASS=$(openssl rand -base64 36) && \
printf '#!/bin/sh\necho "$PASS"\n' > $a && chmod 700 $a && \
ssh-keygen -q -t ecdsa -b 384 -N "$PASS" -f $k && \
SSH_ASKPASS=$a SSH_ASKPASS_REQUIRE=force ssh-add --apple-use-keychain -t 1h $k ; \
rm $a ; unset PASS
Only ssh itself can get the passphrase out of Keychain. So even if a coding agent copies the key file, the file is useless without the passphrase. The agent can still use the key (by running ssh), but that was true with the Secure Enclave too.
What's left is to add the public key to GitHub and the servers and remove the old ones there.
Updating the key on all machines
I have several machines, and ssh-copy-id is a handy way to put the new key on each of them:
ssh-copy-id -i ~/.ssh/id_ecdsa_p384.pub -o IdentitiesOnly=no mini
IdentitiesOnly=no is needed here to log in with the old key from ssh-agent, since the new one isn't on the machine yet. I do the same for the other machines. I call them by short names, and that's how I wrote them in ~/.ssh/config:
Host mini
HostName mac-mini.local
HostKeyAlias mini
User user
Host nas
HostName nas.lan
HostKeyAlias nas
User user
Host router
HostName 192.168.1.1
HostKeyAlias router
User root
Host vps
HostName 203.0.113.10
HostKeyAlias vps
User root
Host is the short name I connect by (ssh mini). HostName is the real address. For the Mac (mini) it's a Bonjour name (.local, a name the Mac announces on the local network by itself), so an IP change doesn't matter.
The most interesting part here is HostKeyAlias (I suspect few people know about it). ssh stores the server's key in known_hosts (the file where ssh remembers which key belongs to which server) under this name. So if a machine's address changes, ssh doesn't complain about an unknown key.
Why I left the Secure Enclave
The Secure Enclave itself can do P-384 (the built-in sc_auth tool creates such a key). But ssh reaches it through Apple's library /usr/lib/ssh-keychain.dylib, which has two modes. In FIDO key mode (the way a YubiKey works with ssh), OpenSSH only knows P-256 and Ed25519 (P-384 was added on December 5, but it isn't in a release yet). In smart card mode (PKCS#11), the library only hands out RSA, which the Secure Enclave can't do. Apple says so plainly in man 8 ssh-keychain.
This can be worked around with a third-party SSH agent that talks to the Secure Enclave itself. The ready-made agent Secretive sees keys from sc_auth, but signs with them incorrectly for now (secretive#888). I could also write a similar agent myself (someone on HN says theirs came out at about 200 lines of Swift), but I don't want to spend time on that.
There's also the YubiKey, which can do P-384 (as a smart card, not through FIDO). But I don't like such solutions. First, there's the risk of a supply chain attack (the device is swapped or modified on its way to the buyer). This has already happened with hardware crypto wallets, where the key also lives in a separate chip: recently someone found Ledger wallets with a spy chip that a reseller had been selling. Second, the cryptography itself can be bad. Coldcard wallets generated keys with weak randomness because of a firmware bug. A key can be extracted from a YubiKey 5 with firmware older than 5.7 through a side channel (the chip's electromagnetic emissions, if the YubiKey falls into the hands of someone with expensive equipment). And in general, when you try too hard to protect yourself, you only draw more attention.
Comments
No comments yet. Start the discussion.