Files
lotherk 154d25be91
ci / build (push) Successful in 28s
accept input of exactly the documented size limit
Review found the cap used as an output bound as well as an input bound: a file
of exactly 1 GiB, the documented maximum, was rejected twice. scr_read_fd could
not tell a full buffer from a larger input, and scr_encrypt refused anything
over SCR_INPUT_MAX minus the envelope overhead. The reader now probes a single
byte past a full buffer to distinguish the two, and the encrypt guard is a
plain size_t overflow check.

The suite builds a second binary with -DSCR_INPUT_MAX=65536 so both sides of the
boundary are tested without a 1 GiB file; the previous code fails the exact-cap
case. Also adds CRLF armor and 'a failed decrypt leaves the output file alone'
cases, and documents that -r on a protected key asks for its passphrase.
2026-11-21 20:07:04 +00:00

121 lines
4.8 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.
A passphrase-protected key asks for its passphrase first, because the public
half is read out of the key.
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`.