119 lines
4.7 KiB
Markdown
119 lines
4.7 KiB
Markdown
# sshcrypt
|
|
|
|
Encrypt a file, a message, or whatever comes down the pipe, for someone whose
|
|
SSH public key you already have. They decrypt it with the private key they
|
|
already use. No new identity system, no key server, no config file.
|
|
|
|
sshcrypt -r alice.pub -i report.pdf -o report.pdf.asc
|
|
# ...mail it...
|
|
sshcrypt -d -k ~/.ssh/id_ed25519 -i report.pdf.asc -o report.pdf
|
|
|
|
The output is ASCII armor, so it survives mail, chat, and copy-paste. `-b`
|
|
writes the raw binary envelope instead.
|
|
|
|
Code: https://git.kropa.tech/hiddenbox/sshcrypt
|
|
|
|
make # the binary, needs libcrypto headers
|
|
make check # end-to-end tests, no network
|
|
make install PREFIX=$HOME/.local # ~/.local/bin/sshcrypt and the man page
|
|
make lint-man # mandoc -Tlint, if mandoc is installed
|
|
|
|
man sshcrypt
|
|
|
|
## The usual job
|
|
|
|
# encrypting needs only their public key
|
|
sshcrypt -r bob.pub < notes.txt > notes.txt.asc
|
|
|
|
# decrypting needs the matching private key
|
|
sshcrypt -d -k ~/.ssh/id_ed25519 < notes.txt.asc
|
|
|
|
# print a key's fingerprint, the same value ssh-keygen -l shows
|
|
sshcrypt -F bob.pub
|
|
sshcrypt -F ~/.ssh/id_ed25519
|
|
|
|
`-r` also accepts a private key path: the public half is derived from it, so
|
|
`sshcrypt -r ~/.ssh/id_ed25519` works when you want to encrypt to yourself.
|
|
|
|
Nothing is written to stdout except the payload. The one-line
|
|
`encrypted for SHA256:...` note goes to stderr, and only when stderr is a
|
|
terminal.
|
|
|
|
## Keys
|
|
|
|
* `ssh-ed25519` and `ssh-rsa` are accepted. ECDSA keys are not.
|
|
* A file with more than one supported key is rejected: put the one recipient's
|
|
key in its own file.
|
|
* Encryption never runs another program.
|
|
* Decryption reads PEM/PKCS#8 with libcrypto, and the OpenSSH container
|
|
(`-----BEGIN OPENSSH PRIVATE KEY-----`) with its own parser. A
|
|
passphrase-protected OpenSSH key is first rewritten without its passphrase by
|
|
`ssh-keygen`, so that program is needed only for those keys. The rewrite
|
|
happens in a `0600` file under `/dev/shm` (or `$TMPDIR`) that is unlinked as
|
|
soon as it has been read.
|
|
* A passphrase is asked for on `/dev/tty` with echo off, never on stdin (that
|
|
is the payload). `SSHCRYPT_PASSPHRASE` supplies it non-interactively.
|
|
|
|
## What this is not
|
|
|
|
* No signatures and no sender authentication. Anyone holding your public key
|
|
can encrypt to you; the message does not prove who sent it. Verify the
|
|
fingerprint out of band if that matters.
|
|
* No key discovery, no trust store, no revocation, no config file, no daemon.
|
|
* No forward secrecy. The envelope holds an ephemeral public key per message,
|
|
but whoever later obtains the recipient's private key can decrypt the
|
|
messages they kept.
|
|
* Not post-quantum.
|
|
* The whole input is held in memory, up to 1 GiB. There is no chunked or
|
|
streaming mode yet.
|
|
|
|
## Format
|
|
|
|
Binary envelope, version 1:
|
|
|
|
| bytes | field |
|
|
|---|---|
|
|
| 4 | `SSHC` |
|
|
| 1 | version, currently 1 |
|
|
| 1 | suite: 1 = ed25519/X25519, 2 = RSA |
|
|
| 2 | reserved, zero |
|
|
| 32 | SHA-256 of the recipient's SSH public key blob |
|
|
| 2 | KEM length, big endian |
|
|
| n | suite 1: ephemeral X25519 public key (32)<br>suite 2: 32 random bytes wrapped with RSA-OAEP-SHA256 |
|
|
| 12 | nonce |
|
|
| ... | ChaCha20-Poly1305 ciphertext, 16-byte tag at the end |
|
|
|
|
Suite 1 derives the message key with HKDF-SHA256 over the X25519 shared secret,
|
|
with the recipient fingerprint and the ephemeral public key as salt. Suite 2
|
|
derives it from the RSA-wrapped random key. The header, the KEM blob and the
|
|
nonce are authenticated as AEAD additional data, so a modified version, suite,
|
|
recipient, or ephemeral key fails the tag check.
|
|
|
|
An Ed25519 key is mapped to X25519 with the standard `u = (1+y)/(1-y)` birational
|
|
map; the private scalar is `clamp(SHA-512(seed)[0..31])`. This is the same
|
|
mapping libsodium uses, checked in `make check` against libcrypto's X25519
|
|
implementation.
|
|
|
|
Armor is base64 in 64-column lines between
|
|
`-----BEGIN SSHCRYPT MESSAGE-----` and `-----END SSHCRYPT MESSAGE-----`.
|
|
The decoder finds those markers inside surrounding text.
|
|
|
|
The fingerprint in the header is checked before any key derivation, so the
|
|
wrong key reports both fingerprints instead of a padding error:
|
|
|
|
sshcrypt: this message is for SHA256:9xQ... but your key is SHA256:2fA...
|
|
|
|
## Requirements
|
|
|
|
* C99 + POSIX.1-2008, `cc`, `make`.
|
|
* OpenSSL 3.x `libcrypto` (development headers). `pkg-config` is used when
|
|
present, `-lcrypto` otherwise.
|
|
* `ssh-keygen` at runtime, only for passphrase-protected OpenSSH keys.
|
|
|
|
Tested on Debian bookworm (OpenSSH 9.2, OpenSSL 3.0), Ubuntu 26.04 (OpenSSH
|
|
10.2, OpenSSL 3.5) and Debian testing (OpenSSH 10.5, OpenSSL 3.6).
|
|
|
|
## License
|
|
|
|
CNCL 1.1, the Could Not Care Less License. See `LICENSE`.
|