C99/POSIX tool, one job: -r PUBKEY encrypts, -d -k PRIVKEY decrypts. Ed25519 goes through X25519 + HKDF-SHA256 + ChaCha20-Poly1305, RSA through RSA-OAEP-SHA256. Text armor by default so the message survives mail and chat. OpenSSH private keys are converted through ssh-keygen; no other subprocess is ever started.
3.6 KiB
sshcrypt — notes for agents
One job: encrypt bytes for whoever holds an SSH keypair. -r PUBKEY encrypts,
-d -k PRIVKEY decrypts. Everything else is a non-goal on purpose: no signing,
no key discovery, no config file, no daemon, no chunked streaming. Adding one of
those is a design decision, not a commit.
Build, test, install
make # -std=c99 -D_POSIX_C_SOURCE=200809L, links libcrypto
make check # tests/run.sh, generates keys in a tmpdir, no network
make install PREFIX=$HOME/.local # installs bin/sshcrypt
make check is the gate: 20 end-to-end cases. It needs ssh-keygen, cmp and
python3 (one tamper test). After touching parsing or the envelope, also run
make clean && make CFLAGS="-O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address,undefined" && make check
The Makefile uses override CFLAGS +=, so the language flags survive any CFLAGS
you pass; do not remove that override.
Layout
src/main.c— CLI and mode dispatch, no crypto.src/keys.c— SSH wire parsing, public/private key loading, the ssh-keygen conversion, Ed25519→X25519.src/envelope.c— KEM, AEAD, the wire format.src/armor.c— base64 armor.src/util.c— buffers, base64, zeroing.tests/run.sh— everything is tested through the binary, not unit tests.
Traps that already cost time (all verified)
EVP_PKEY_decryptneeds an output buffer at least as large as the RSA modulus, even when the plaintext is 32 bytes. A 32-byte buffer fails withbad length(providers/implementations/asymciphers/rsa_enc.c).ssh-keygen -p -m PKCS8only writes PKCS#8 when the new passphrase is empty (-N ''). With a non-empty-Nit silently keeps the OpenSSH container. That is why the conversion always writes an unencrypted key into the 0600 tmpfs file and unlinks it at once.- libcrypto cannot read
-----BEGIN OPENSSH PRIVATE KEY-----at all: no openssh-key-v1, no bcrypt_pbkdf. Do not feed those bytes toPEM_read_PrivateKey. $SSH_ASKPASSis exec'd with the prompt asargv[1], so askpass mode cannot be a command-line flag. It travels asSSHCRYPT_ASKPASS_MODEin the child's environment, with the passphrase inSSHCRYPT_ASKPASS— never in argv.- ssh-keygen only uses askpass when there is no controlling terminal, so the
child calls
setsid()beforeexecvp. - OpenSSL has no Ed25519→X25519 helper.
scr_ed25519_pk_to_x25519()inkeys.cis the standardu = (1+y)/(1-y)map, cross-checked against libcrypto X25519 intests/run.sh. Do not replace it with an assumption. memmemis GNU, not POSIX. Usefind_bytes()inkeys.c.- Every
.cincludes its own headers; missing<stdio.h>is a hard error under-std=c99 -Wpedantic.
Changing the format
The 40-byte header is AEAD additional data. Any layout change bumps
SCR_VERSION in src/sshcrypt.h and updates the table in README.md. Old
readers must fail closed, never guess. Keep the recipient fingerprint check
before key derivation: wrong keys must exit 3 and name both fingerprints.
Invariants
- Exit codes are interface: 0 ok, 1 usage/I-O, 2 malformed input, 3 wrong key or tampered message.
- Never put plaintext, key material or passphrases in argv, logs or temp files.
Prompt on
/dev/ttywith echo off. Zero secrets withscr_secure_zero. - Encryption must stay subprocess-free; only OpenSSH private keys may call
ssh-keygen. - Input is buffered whole, capped at 1 GiB. Do not silently raise the cap.
Git
Remote https://git.kropa.tech/hiddenbox/sshcrypt (Gitea), default branch
main.