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

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

# verify which key a message or a key file belongs to
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 directly. An OpenSSH-format private key (-----BEGIN OPENSSH PRIVATE KEY-----) is converted through ssh-keygen, which therefore has to be installed. The conversion happens in a 0600 file under /dev/shm (or $TMPDIR), and the file is unlinked immediately.
  • 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)
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 OpenSSH-format private keys.

Tested on Debian with OpenSSL 3.6.3 and OpenSSH 10.5p1.

License

CNCL 1.1, the Could Not Care Less License. See LICENSE.

S
Description
sshcrypt (from kitt, ~/src/sshcrypt)
Readme
193 KiB
0 Stars 3 Watchers 0 Forks
Languages
C 82.1%
Shell 16.3%
Makefile 1.6%