Files
sshcrypt/AGENTS.md
T
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

5.7 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: 31 end-to-end cases. It needs ssh-keygen, cmp, python3 (tamper and crafted-key cases), script(1) (pty cases), pgrep (SIGINT case) and setsid(1) (no-tty case); those skip themselves if the tool is missing. No network. 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 passphrase strip, Ed25519→X25519.
  • src/openssh.c — reads the unencrypted openssh-key-v1 container, so Ed25519 keys work on ssh-keygen versions that will not export PKCS#8.
  • 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.
  • docs/sshcrypt.1 — the man page. Any flag change updates it too; make lint-man (what CI runs) fails on bad markup.
  • .gitea/workflows/ci.yml — build, make check, make lint-man, then the same tests again under ASan+UBSan.

Traps that already cost time (all verified)

  • EVP_PKEY_decrypt needs an output buffer at least as large as the RSA modulus, even when the plaintext is 32 bytes. A 32-byte buffer fails with bad length (providers/implementations/asymciphers/rsa_enc.c).
  • ssh-keygen -p -m PKCS8 is not a conversion path: on OpenSSH 10.2 (Ubuntu 26.04 LTS) it silently ignores -m for ed25519 keys, exits 0, and leaves the OpenSSH container. RSA still converts, which is what made this look fine. src/openssh.c therefore parses the container itself. ssh-keygen is only used to strip a passphrase (-p -N ''), which every version does.
  • libcrypto cannot read -----BEGIN OPENSSH PRIVATE KEY----- at all: no openssh-key-v1, no bcrypt_pbkdf. Do not feed those bytes to PEM_read_PrivateKey.
  • $SSH_ASKPASS is exec'd with the prompt as argv[1], so askpass mode cannot be a command-line flag. It travels as SSHCRYPT_ASKPASS_MODE in the child's environment, with the passphrase in SSHCRYPT_ASKPASS — never in argv.
  • ssh-keygen only uses askpass when there is no controlling terminal, so the child calls setsid() before execvp.
  • OpenSSL has no Ed25519→X25519 helper. scr_ed25519_pk_to_x25519() in keys.c is the standard u = (1+y)/(1-y) map, cross-checked against libcrypto X25519 in tests/run.sh. Do not replace it with an assumption.
  • Lengths inside a key or message file are attacker-controlled. Every copy into a fixed field must be bounded: the public blob from openssh-key-v1 is memcpy'd into struct scr_priv.blob[4096], and it used to be copied at whatever length the file claimed (stack overflow, caught in review). The guard is the publ > sizeof out->blob check in scr_openssh_load.
  • memmem is GNU, not POSIX. Use find_bytes() in keys.c.
  • Terminal echo: save termios before clearing ECHO and restore that saved copy. Re-reading with tcgetattr after disabling restores no-echo, and termios outlives the fd, so the user's shell is left mute. Use TCSANOW, not TCSAFLUSH: the flush discards a passphrase that is already queued in the tty. Both are covered by the script(1) cases in make check.
  • A background job started by a non-interactive shell inherits SIGINT and SIGQUIT set to SIG_IGN, and ignored dispositions survive exec. Installing the cleanup handler is what makes Ctrl-C work in that case; do not move it after the passphrase prompt (the pty test kills at the prompt).
  • Public key lines are parsed field by field: a commented line is ignored, and a field that merely contains a type name is skipped unless its base64 decodes to that same type. A substring search accepts # disabled: ssh-ed25519 ... and breaks on command="echo ssh-rsa hi".
  • Every .c includes 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 or logs. The one temp file, the ssh-keygen conversion, is 0600 on tmpfs, is unlinked as soon as it has been read, and SIGINT/SIGTERM/SIGHUP unlink it too. Any new path that writes key material must do the same.
  • Prompt on /dev/tty with echo off (see the termios trap). Zero secrets with scr_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.