Files
sshcrypt/tests/run.sh
T
lotherk 0afb114993 sshcrypt: encrypt to an SSH public key, decrypt with the private key
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.
2026-11-21 10:56:55 +00:00

118 lines
4.5 KiB
Bash
Executable File

#!/bin/sh
# End-to-end tests. No network, no fixtures: keys are made in a temp dir.
set -eu
cd "$(dirname "$0")/.."
BIN=$PWD/sshcrypt
T=$(mktemp -d "${TMPDIR:-/tmp}/sshcrypt-test.XXXXXX")
trap 'rm -rf "$T"' EXIT INT TERM
pass=0
fail=0
ok() { pass=$((pass + 1)); }
bad() { fail=$((fail + 1)); printf 'FAIL: %s\n' "$*" >&2; }
check() { if eval "$2"; then ok; else bad "$1"; fi; }
ssh-keygen -q -t ed25519 -N '' -C t -f "$T/ed" >/dev/null
ssh-keygen -q -t rsa -b 3072 -N '' -C t -f "$T/rsa" >/dev/null
ssh-keygen -q -t ed25519 -N '' -C other -f "$T/other" >/dev/null
ssh-keygen -q -t ed25519 -N 'hunter2' -C pw -f "$T/pw" >/dev/null
# --- fingerprints agree with OpenSSH -----------------------------------------
check "fingerprint ed25519 matches ssh-keygen" \
'[ "$("$BIN" -F "$T/ed.pub")" = "$(ssh-keygen -lf "$T/ed.pub" | cut -d" " -f2)" ]'
check "fingerprint RSA matches ssh-keygen" \
'[ "$("$BIN" -F "$T/rsa.pub")" = "$(ssh-keygen -lf "$T/rsa.pub" | cut -d" " -f2)" ]'
# --- round trips --------------------------------------------------------------
printf 'hello, world\n' >"$T/msg"
"$BIN" -r "$T/ed.pub" -i "$T/msg" -o "$T/msg.asc"
check "armored output is text" 'grep -q "BEGIN SSHCRYPT MESSAGE" "$T/msg.asc"'
"$BIN" -d -k "$T/ed" -i "$T/msg.asc" -o "$T/msg.out"
check "ed25519 round trip" 'cmp -s "$T/msg" "$T/msg.out"'
"$BIN" -d -k "$T/ed" -i "$T/msg.asc" -o "$T/msg.same" -q
check "round trip is byte exact" 'cmp -s "$T/msg" "$T/msg.same"'
head -c 65536 /dev/urandom >"$T/blob"
"$BIN" -r "$T/ed.pub" -b -i "$T/blob" -o "$T/blob.bin"
check "binary envelope has the magic" 'head -c 4 "$T/blob.bin" | grep -q SSHC'
"$BIN" -d -k "$T/ed" -i "$T/blob.bin" -o "$T/blob.out"
check "64 KiB random binary round trip" 'cmp -s "$T/blob" "$T/blob.out"'
: >"$T/empty"
"$BIN" -r "$T/ed.pub" -i "$T/empty" -o "$T/empty.asc"
"$BIN" -d -k "$T/ed" -i "$T/empty.asc" -o "$T/empty.out"
check "empty input round trip" '[ ! -s "$T/empty.out" ]'
printf 'no trailing newline\000and a nul' >"$T/odd"
"$BIN" -r "$T/ed.pub" -i "$T/odd" -o "$T/odd.asc"
"$BIN" -d -k "$T/ed" -i "$T/odd.asc" -o "$T/odd.out"
check "NUL bytes and no trailing newline" 'cmp -s "$T/odd" "$T/odd.out"'
# --- pipes -------------------------------------------------------------------
cat "$T/msg" | "$BIN" -r "$T/ed.pub" | "$BIN" -d -k "$T/ed" >"$T/pipe.out"
check "stdin to stdout pipeline" 'cmp -s "$T/msg" "$T/pipe.out"'
# --- RSA ---------------------------------------------------------------------
"$BIN" -r "$T/rsa.pub" -i "$T/msg" -o "$T/rsa.asc"
"$BIN" -d -k "$T/rsa" -i "$T/rsa.asc" -o "$T/rsa.out"
check "RSA round trip" 'cmp -s "$T/msg" "$T/rsa.out"'
# --- armor survives being pasted into a mail ---------------------------------
{
printf 'Hi, here is the thing:\n\n'
cat "$T/msg.asc"
printf '\n-- \nsig\n'
} >"$T/mail.txt"
"$BIN" -d -k "$T/ed" -i "$T/mail.txt" -o "$T/mail.out"
check "armor inside surrounding text" 'cmp -s "$T/msg" "$T/mail.out"'
# --- errors ------------------------------------------------------------------
if "$BIN" -d -k "$T/other" -i "$T/msg.asc" -o "$T/nope" 2>"$T/err"; then
bad "decrypting with the wrong key must fail"
else
ok
fi
check "wrong key names both fingerprints" \
'grep -q "but your key is SHA256:" "$T/err"'
check "wrong key writes nothing" '[ ! -e "$T/nope" ]'
cp "$T/blob.bin" "$T/tampered.bin"
python3 - "$T/tampered.bin" <<'EOF'
import sys
p = sys.argv[1]
b = bytearray(open(p, 'rb').read())
b[-1] ^= 0x01
open(p, 'wb').write(b)
EOF
if "$BIN" -d -k "$T/ed" -i "$T/tampered.bin" -o "$T/nope2" 2>"$T/err2"; then
bad "a modified message must fail"
else
ok
fi
if "$BIN" -r "$T/ed" -d -k "$T/ed" -i "$T/msg" -o "$T/nope3" 2>/dev/null; then
bad "garbage input must fail"
else
ok
fi
# --- passphrase-protected OpenSSH key, and -r with a private key --------------
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -r "$T/pw.pub" -i "$T/msg" -o "$T/pw.asc"
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -d -k "$T/pw" -i "$T/pw.asc" -o "$T/pw.out"
check "passphrase-protected key round trip" 'cmp -s "$T/msg" "$T/pw.out"'
"$BIN" -r "$T/ed" -i "$T/msg" -o "$T/viapriv.asc"
"$BIN" -d -k "$T/ed" -i "$T/viapriv.asc" -o "$T/viapriv.out"
check "encrypting to a private-key path works" 'cmp -s "$T/msg" "$T/viapriv.out"'
# --- 1 MiB -------------------------------------------------------------------
head -c 1048576 /dev/urandom >"$T/big"
"$BIN" -r "$T/ed.pub" -i "$T/big" -o "$T/big.asc"
"$BIN" -d -k "$T/ed" -i "$T/big.asc" -o "$T/big.out"
check "1 MiB round trip" 'cmp -s "$T/big" "$T/big.out"'
printf '%d passed, %d failed\n' "$pass" "$fail"
[ "$fail" -eq 0 ]