···11+---
22+title: How to move away from RSA for SSH keys
33+date: 2023-01-04
44+tags:
55+ - OpenSSH
66+ - RSA
77+ - ed25519
88+ - security
99+ - sre
1010+ - NixOS
1111+---
1212+1313+<xeblog-hero ai="Stable Diffusion v1.5" file="volcano-bliss" prompt="a rolling green landscape by makoto shinkai, breath of the wild, active volcano, windows xp bliss, manga style, ((thick outlines))"></xeblog-hero>
1414+1515+[RSA](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) is one of the most
1616+widely deployed encryption algorithms in the world. Notably, when you generate
1717+an SSH key without any extra flags, `ssh-keygen` will default to using RSA:
1818+1919+```
2020+root@hiro:~# ssh-keygen
2121+Generating public/private rsa key pair.
2222+Enter file in which to save the key (/root/.ssh/id_rsa):
2323+```
2424+2525+For a while cryptographers have feared that RSA is vulnerable to a quantum
2626+computing algorithm known as [Shor's
2727+Algorithm](https://en.wikipedia.org/wiki/Shor%27s_algorithm). I won't pretend to
2828+understand it in this article, but the main reason why it's not deployed is that
2929+the hardware required to attack RSA keys in the wild literally doesn't exist
3030+yet (think literally tens of generations more advanced than current quantum
3131+computers).
3232+3333+A group of researchers have just published [a
3434+paper](https://arxiv.org/pdf/2212.12372.pdf) that posits that it's likely you
3535+can break 2048-bit RSA (the most widely deployed keysize) with a quantum
3636+computer that only uses 372 qubits of computational power. The [IBM
3737+Osprey](https://newsroom.ibm.com/2022-11-09-IBM-Unveils-400-Qubit-Plus-Quantum-Processor-and-Next-Generation-IBM-Quantum-System-Two)
3838+has 433 qubits.
3939+4040+<xeblog-conv name="Cadey" mood="coffee">Note that quantum computers are
4141+effectively unobtainable (unless you're a research institution or you have a few
4242+small loans of billions of dollars laying around), require a team of highly
4343+specialized experts to monitor them 24/7, and aren't really usable to the
4444+general public. I highly doubt that quantum computers are going to be rolling
4545+into store shelves any time soon. I also have no idea what I'm talking about
4646+with quantum computers. Please temper your interpretations of my statements
4747+appropriately.</xeblog-conv>
4848+4949+It may be a good time to move away from RSA keys when and where you can. Today
5050+I'm going to cover how to make SSH keys using
5151+[ed25519](https://en.wikipedia.org/wiki/EdDSA#Ed25519) keys instead of RSA.
5252+5353+<xeblog-conv name="Mara" mood="hacker">It's worth noting that RSA has not been
5454+broken yet, the paper in question
5555+5656+## Generating new keys
5757+5858+To generate a new keypair, use the `ssh-keygen` command:
5959+6060+```
6161+ssh-keygen -t ed25519
6262+```
6363+6464+Make sure to set a password on that key and then you can add it to your SSH
6565+agent with `ssh-add`. Copy the public key to your clipboard (print it to the
6666+screen with `cat ~/.ssh/id_ed25519.pub`) and then you can add it to GitHub or
6767+other services you use.
6868+6969+<xeblog-conv name="Mara" mood="hacker">Pro tip: you can get a list of machines
7070+you've SSHed into by reading your `~/.ssh/known_hosts` file. You could use a
7171+command like this:</xeblog-conv>
7272+7373+```
7474+cat ~/.ssh/known_hosts | cut -d' ' -f1 | sort | uniq
7575+```
7676+7777+<xeblog-conv name="Mara" mood="happy">This will get you a list of machines that
7878+you may need to update your SSH key in! Remember that your new key should go to
7979+the end of `~/.ssh/authorized_keys`!</xeblog-conv>
8080+8181+## Disabling RSA host keys
8282+8383+The OpenSSH server will create a keypair for each machine it runs on. By default
8484+this creates an RSA key as well as an ed25519 key. You can disable this by
8585+adding the following line to `/etc/ssh/sshd_config`:
8686+8787+```
8888+HostKey /etc/ssh/ssh_host_ed25519_key
8989+```
9090+9191+<xeblog-conv name="Mara" mood="hacker">In my testing, this was the case for both
9292+NixOS and Ubuntu. If you want to be sure you're setting the right key, check the
9393+file for commented-out HostKey instructions. Uncomment whichever one contains
9494+`ed25519` in it.</xeblog-conv>
9595+9696+If your SSH configuration file has a `Ciphers`, `HostKeyAlgorithms`,
9797+`PubkeyAcceptedAlgorithms`, or `CASignatureAlgorithms` setting in it, you may
9898+want to make sure that any `rsa` cipher or algorithm isn't present in any of
9999+them. If your distro has an option to change this system wide (such as in [Red
100100+Hat and
101101+derivatives](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/security_hardening/using-the-system-wide-cryptographic-policies_security-hardening)),
102102+you may want to use that.
103103+104104+<xeblog-conv name="Mara" mood="happy">You may want to have some kind of
105105+transition period for shared machines before you start rejecting RSA keys
106106+willy-nilly. This can break people's workflows and SSH-key-in-GPG setups. Talk
107107+with your users and work on compromises. Something something shill for the
108108+company supplying the author of this post with the money needed for food
109109+something something.</xeblog-conv>
110110+111111+If you want to do this on NixOS, add the following configuration to either your
112112+`configuration.nix` or something that is imported by your `configuration.nix`:
113113+114114+```nix
115115+services.openssh.hostKeys = [{
116116+ path = "/etc/ssh/ssh_host_ed25519_key";
117117+ type = "ed25519";
118118+}];
119119+```
120120+121121+<xeblog-conv name="Mara" mood="hacker">This tells SSH to use only an ed25519
122122+host key. By default it will also create an RSA key.</xeblog-conv>
123123+124124+---
125125+126126+I hope this helps! Systems administration is full of annyoing migrations and
127127+compromises like this. Good luck out there!
128128+129129+<xeblog-conv name="Mara" mood="hacker">Also check out [this
130130+article](https://xeiaso.net/blog/yubikey-ssh-key-storage) on how you can store
131131+an SSH key on a Yubikey or any other compliant FIDO2 key!</xeblog-conv>