lotherk 577052decd
ci / build (push) Successful in 30s
fix the remaining review findings in key handling
- Parse public key lines field by field instead of searching for the type name
  anywhere. A commented-out key line is now ignored (it used to be accepted,
  so a secret could be encrypted to a key the user had disabled), and an
  authorized_keys option that contains the word ssh-rsa no longer aborts the
  parse. A candidate field only counts if its base64 decodes to that type.
- Bound the line loop by the buffer length, not by *p: the buffer is not NUL
  terminated, so a 64 KiB file ending in a newline made the loop look at
  uninitialised heap (valgrind). Reproduced on the old build, clean now.
- Ask for the passphrase before creating the temp file, and register an
  atexit cleanup, so a scr_die between mkstemp and the unlink cannot leave the
  key behind (no tty and no SSHCRYPT_PASSPHRASE used to leak one per run).
- Install the cleanup handler before the prompt again. A background job in a
  non-interactive shell inherits SIG_IGN for SIGINT, and ignored dispositions
  survive exec, so killing at the prompt is a no-op without a handler.

The suite is 31 cases and now covers each of these; five of them fail on the
previous build.
2026-11-21 19:02:08 +00:00

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)
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.

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%