ssh
"Load key: invalid format" and "error in libcrypto": Causes, Tested
Both SSH errors mean the key file did not parse. We broke throwaway keys 18 ways on two OpenSSH builds: CRLF, missing newline, BOM, CI secrets. Each fix is one line.
By Kyle Hurst ·
You ran ssh, ssh-add or git push and got one of these:
Load key "/home/you/.ssh/id_ed25519": invalid format
Load key "/home/you/.ssh/id_ed25519": error in libcrypto
Error loading key "/home/you/.ssh/id_rsa": error in libcrypto
They are the same failure: the bytes in the file are not a key OpenSSH can parse. Which wording you get depends on how your ssh was built (its OpenSSH version and crypto library), not on what’s wrong. The usual causes are Windows line endings, a missing newline at the very end, or a key that got mangled on its way through a clipboard or a CI secret. Run this first. It reads the private key directly, needs no server, and prints the public key if the file is fine:
ssh-keygen -y -f ~/.ssh/id_ed25519
If that fails, this repairs the five most common kinds of damage (CRLF, missing final newline, a byte-order mark, stray indentation, junk above or below the key). It needs bash or zsh, and it backs the key up first:
k=~/.ssh/id_ed25519
cp "$k" "$k.bak"
LC_ALL=C sed $'1s/^\xef\xbb\xbf//; s/\r$//; s/^[ \t]*//; s/[ \t]*$//; /^-----BEGIN /,$!d; /^-----END /q' "$k.bak" > "$k"
[ -n "$(tail -c 1 "$k")" ] && echo >> "$k"
chmod 600 "$k"
ssh-keygen -y -f "$k"
We ran that on the CRLF, missing-newline, BOM, indented, trailing-space and junk-above-BEGIN files from the table below, for all three key types, on macOS and on Ubuntu. ssh-keygen -y accepted every one afterwards. If it still fails, the key is truncated, in the wrong format, or not a private key at all. The table tells you which.
Same file, two messages
We tested two builds side by side:
- macOS: the bundled
OpenSSH_10.3p1, LibreSSL 3.3.6 - Ubuntu 26.04:
OpenSSH_10.2p1 Ubuntu-2ubuntu3.8, OpenSSL 3.5.5
Given the same broken file, macOS said invalid format where Ubuntu almost always said error in libcrypto. OpenSSH’s sshkey.c explains it. It tries its own key format first, and on a format error it falls back to the crypto library’s PEM parser. When that also fails, translate_libcrypto_error() maps the library’s error to an OpenSSH one, and anything it doesn’t recognise becomes “error in libcrypto”.
The message is not fixed even among OpenSSL builds. From OpenSSH 10.3, ssh can print the crypto library’s own reason in place of the bare “error in libcrypto” (commit 9c4949c, February 2026). As a third data point we ran ssh-keygen -y on every file in Fedora’s OpenSSH_10.5p1, OpenSSL 4.0.3: the same files failed as on macOS, and every one said invalid format.
So “error in libcrypto” on a file that starts -----BEGIN OPENSSH PRIVATE KEY----- almost never means OpenSSL is broken. Neither parser could read the file. OpenSSL issue #13443 began as a real OpenSSL 3.0 alpha bug (the reporter confirmed it fixed in alpha16). Several later commenters turned out to have a missing final newline, CRLF endings, blank lines in a pasted key, or an IdentityFile pointing at the .pub. All of those are rows in the table below.
The table: 18 ways to break a key, measured
We generated throwaway keys for this post with ssh-keygen (Ed25519 and RSA in the default OpenSSH format, plus an RSA key with -m PEM), broke a copy of each in one way, and ran three commands on every file: ssh-keygen -y -f, ssh-add, and ssh -i ... -o BatchMode=yes against a local test server that trusted the key. All three commands always agreed.
| What happened to the file | OpenSSH format (BEGIN OPENSSH PRIVATE KEY) | Old PEM RSA (BEGIN RSA PRIVATE KEY) |
|---|---|---|
| CRLF (Windows) line endings | Fails | Loads |
| No newline after the END line | Fails | Loads |
| END line missing | Fails | Fails |
| END line cut short (dashes missing) | Fails | Fails |
| Half the base64 lines missing | Fails | Fails ¹ |
| Every line indented, BEGIN included | Fails | Fails |
| Only the base64 lines indented | Loads | Loads |
| Trailing spaces on every line | Fails | Loads |
Whole key on one line (echo $KEY) | Fails | Fails |
| Base64 re-wrapped to 76 columns | Loads | Loads |
| Base64 joined into one long line | Loads | Loads |
| Blank line inside the base64 | Loads | Fails ¹ |
| Text or a blank line above BEGIN | Fails | Loads |
Literal \n instead of line breaks | Fails | Fails |
| UTF-8 byte-order mark (BOM) | Fails | macOS fails, Ubuntu loads |
| UTF-16 with BOM | Fails | Fails |
| Public key saved as the private key | Fails | Fails |
PuTTY .ppk file | Fails (Ed25519 and RSA) | n/a |
How to read the messages:
- On macOS every “Fails” printed
invalid format. - On Ubuntu every “Fails” printed
error in libcrypto, except the two marked ¹, which printedinvalid formaton both builds. - Ed25519 and RSA keys in OpenSSH format behaved identically in every row.
ssh-keygen -yandsshprintLoad key "file": <reason>.ssh-addprintsError loading key "file": <reason>.sshthen addsPermission denied (publickey).
The OpenSSH format is strict about its first and last lines and relaxed about the middle. PEM is relaxed about line endings and strict about the base64. In the source, private2_uudecode() requires the file to begin with exactly the 36 bytes -----BEGIN OPENSSH PRIVATE KEY-----\n, newline included. A \r, a leading space, a BOM, a blank first line or trailing spaces all break that match. It then needs -----END OPENSSH PRIVATE KEY-----\n, newline again included, so a missing final newline is fatal. In between it drops line breaks and decodes base64 that skips whitespace, so re-wrapped or indented base64 lines are harmless.
PEM keys go to OpenSSL or LibreSSL, which mostly follow RFC 7468: parsers “MUST handle different newline conventions” and “MUST NOT malfunction” on data before the BEGIN line.
When ssh doesn’t show the error at all
ssh only reads the private key when it needs it. If id_ed25519.pub sits next to the key, ssh offers that public key first and opens the private file only once the server accepts it. Against a server that didn’t trust the key, a broken private key with a good .pub beside it gave only Permission denied (publickey), with no Load key line. Remove the .pub and the Load key line comes back. That’s why ssh-keygen -y -f is the test to use: it always reads the private file.
A broken .pub was harmless. A wrong .pub was not. An empty .pub, a CRLF .pub, a junk .pub, or a copy of the private key in the .pub path: both builds ignored it and logged in. But a .pub from a different key that the server trusts gave this:
identity_sign: private key id_ed25519 contents do not match public
you@host: Permission denied (publickey).
In that state ssh-keygen -l -f id_ed25519 printed the other key’s fingerprint, because it reads the .pub first. It even reported the wrong key type. Regenerate the public half from the private one:
ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub
Each cause: how to check, how to fix
CRLF line endings. file -k key reports with CRLF line terminators, and tail -c 2 key | od -c ends in \r \n instead of - \n. Fix with dos2unix key or tr -d '\r' < key.bak > key. On Windows this hits Git for Windows users. In git-for-windows issue #4705, still open, a CRLF Vagrant key fails with error in libcrypto in Git’s bundled ssh, while the reporter says Windows’ own OpenSSH reads it. A maintainer points to a CRLF patch in Microsoft’s fork that Git’s build lacks. A November 2026 comment adds that SSH commit signing fails too, with No private key found, which we reproduced with ssh-keygen -Y sign on a CRLF key. If the key lives in a repository, stop Git converting it. Unsetting text “tells Git not to attempt any end-of-line conversion” (gitattributes):
# .gitattributes
*.pem -text
id_* -text
No final newline. tail -c 2 key | od -c shows - - instead of - \n. Fix: echo >> key. This happens when you paste a key into an editor that doesn’t add a trailing newline, or write it with printf '%s'. It only matters for the OpenSSH format. PEM keys loaded fine without it.
Truncated, or the END line is missing. tail -1 key should be the full -----END ... PRIVATE KEY----- line. If the END line alone was lost, you can paste it back. If base64 lines are gone, the key can’t be repaired. Copy it again from the source, or make a new pair.
A PuTTY .ppk file. head -1 key shows PuTTY-User-Key-File-3: ssh-ed25519 (or -2:), and file says PuTTY Private Key File. OpenSSH can’t read PuTTY’s format at all. Convert it with puttygen (we used release 0.83):
puttygen key.ppk -O private-openssh-new -o ~/.ssh/id_ed25519
-O private-openssh also works. For Ed25519 it writes the OpenSSH format. For RSA it writes the old BEGIN RSA PRIVATE KEY PEM file. In the PuTTYgen window, the same choice is under Conversions, “Export OpenSSH key” or “Export OpenSSH key (force new file format)”, as the PuTTY manual describes.
Reformatted by a chat app, wiki or email. Indented, trailing spaces, everything on one line. Indented and re-wrapped base64 lines are fine. The BEGIN and END lines must start in column 0 and end right after the last dash. The repair script at the top strips leading and trailing whitespace. A key flattened onto one line with spaces, -----BEGIN OPENSSH PRIVATE KEY----- b3Bl... -----END OPENSSH PRIVATE KEY-----, only needs its two marker lines back. The spaces left inside the base64 are skipped. This fixed flattened Ed25519, RSA and PEM keys on both builds:
perl -pe 's/----- /-----\n/; s/ -----END/\n-----END/' key.bak > key
The public key in the private key’s place. head -c 12 key shows ssh-ed25519 or ssh-rsa , and file says OpenSSH ED25519 public key. Usually ssh -i or an IdentityFile line in ~/.ssh/config points at the .pub. Point it at the file without .pub.
A UTF-8 or UTF-16 BOM. head -c 3 key | od -c shows 357 273 277 for a UTF-8 BOM, or 377 376 for UTF-16. file says Unicode text, UTF-8 (with BOM). On Windows the usual source is PowerShell. Per Microsoft’s encoding docs, in Windows PowerShell 5.1 “Out-File and the redirection operators > and >> create UTF-16LE”, and its UTF8 encoding writes a BOM. dos2unix fixed both kinds in our tests. So did iconv -f UTF-16 -t UTF-8 for UTF-16 and the sed script above for UTF-8.
Wrong passphrase (a different message). An encrypted key with the wrong passphrase never says invalid format. ssh-keygen -y prints Load key "id_ed25519": incorrect passphrase supplied to decrypt private key, and ssh-add prints Bad passphrase, try again for id_ed25519:. Plain ssh just asks again, up to three times by default (NumberOfPasswordPrompts), then gives up with Permission denied (publickey). With BatchMode=yes it can’t ask, so you get only that last line. Those messages were identical for OpenSSH-format and PEM keys on both builds. A related tell: if ssh-keygen -y asks you for a passphrase, the file parsed. None of the broken files above got as far as a prompt.
CI secrets: GitHub Actions and friends
A key stored as a CI secret usually reaches your script as an environment variable. Shell handling then decides whether it survives. On Ubuntu with bash 5.3:
| How the script writes the key | Result |
|---|---|
echo $SSH_KEY > key (unquoted) | Fails: all on one line |
printf '%s' "$SSH_KEY" > key | Fails if the secret doesn’t end in a newline |
echo "$SSH_KEY" > key | Loads |
printf '%s\n' "$SSH_KEY" > key | Loads |
Secret stored with literal \n, written with printf '%s\n' | Fails |
Same secret, written with printf '%b\n' | Loads |
GitHub’s secrets guide says to quote secrets in run steps, and the unquoted row shows why. This step worked with a secret with or without a trailing newline, and with CRLF. The last line makes the job fail right there with the load error, not later with a confusing git one:
- name: Install deploy key
env:
SSH_KEY: ${{ secrets.DEPLOY_KEY }}
run: |
mkdir -p ~/.ssh && chmod 700 ~/.ssh
printf '%s\n' "$SSH_KEY" | tr -d '\r' > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keygen -y -f ~/.ssh/id_ed25519 > /dev/null
If the secret holds literal \n sequences (say, a key copied out of a JSON string), use printf '%b\n' instead. Base64 has no backslashes, so %b only touches the \ns. To avoid newline problems entirely, use the approach GitHub documents for binary files: store the key base64-encoded (base64 -w 0 key on Linux, base64 -i key on macOS) and decode it in the job with echo "$KEY_B64" | base64 --decode > ~/.ssh/id_ed25519. Our round trip came out byte-identical.
The three formats, briefly
- OpenSSH format,
-----BEGIN OPENSSH PRIVATE KEY-----. A binary structure described in PROTOCOL.key. It starts with the magic stringopenssh-key-v1, followed by the cipher, the KDF, the public keys and an optionally encrypted private section. Since OpenSSH 7.8 (August 2018),ssh-keygenwrites this format “by default instead of using OpenSSL’s PEM format”. This is the file with the strict first and last lines. - PEM / PKCS#1,
-----BEGIN RSA PRIVATE KEY-----. The RSA-onlyRSAPrivateKeystructure from RFC 8017 (appendix A.1.2), parsed by OpenSSL or LibreSSL. Encrypted ones carryProc-Type: 4,ENCRYPTEDandDEK-Info:header lines. You get this fromssh-keygen -m PEMor older tools. - PKCS#8,
-----BEGIN PRIVATE KEY-----(orENCRYPTED PRIVATE KEY). The algorithm-neutral wrapper from RFC 7468 section 10. OpenSSH reads it through the same PEM path.
To convert, ssh-keygen -p rewrites a key in place, so keep a copy:
ssh-keygen -p -f key # PEM or PKCS#8 → OpenSSH format
ssh-keygen -p -m PEM -f key # OpenSSH-format RSA → BEGIN RSA PRIVATE KEY
ssh-keygen -p -m PKCS8 -f key # OpenSSH-format RSA → BEGIN PRIVATE KEY
We ran all three and each produced the header shown. One catch: -m PEM on an Ed25519 key quietly left it in OpenSSH format. The -p flag means “change passphrase”, so it prompts for a new one (and for the old one, if the key has one). Press Enter for none.
What our generators produce
Our SSH key generator writes Ed25519 private keys in OpenSSH format with LF line endings, base64 wrapped at 64 columns (ssh-keygen uses 70; both load). The download adds the final newline and saves id_ed25519 and id_ed25519.pub. RSA private keys come as PKCS#8 (BEGIN PRIVATE KEY) in id_rsa.pem, next to id_rsa.pub. We ran the generator’s encoding code under Node, and ssh-keygen -y accepted both key types and printed public keys matching the generated .pub files.
Testing for this post caught a bug of our own: the Copy button used to copy the private key without its trailing newline, so pbpaste > ~/.ssh/id_ed25519 produced the missing-newline row of the table. Copy now includes the newline, the same as the download. If you paste a key into an editor instead, check that the editor kept the final newline, or run echo >> ~/.ssh/id_ed25519 afterwards. The PKCS#8 keys from our RSA / EC key-pair generator are copy only and have no trailing newline, and they loaded fine, because PEM doesn’t need one.
Downloads land with your default file mode, so run chmod 600 before first use, or you’ll trade this error for the “permissions 0644 are too open” one. If you’re making a fresh key anyway, Ed25519 vs RSA covers which to pick.