commit 14f714af6cede62ced558fe0bd29132667a11a06
parent 379773ad7e8302c7d5d59aee50632845e7e2cb5d
Author: finwo <finwo@pm.me>
Date: Fri, 9 Oct 2026 22:48:34 +0200
Add 'generate' subcommand
Diffstat:
9 files changed, 385 insertions(+), 21 deletions(-)
diff --git a/README.md b/README.md
@@ -193,6 +193,7 @@ a universal keyspace.
`sfinx` is one binary that dispatches on its first argument.
- `sfinx seed [length] [-o file]`: write `length` random seed bytes, default 32, to stdout or a file.
+- `sfinx generate [length] [-L hash] [--out-fmt fmt] [-o file] [-F]`: generate a seed and derive its public key. `--out-fmt hdr` writes one combined file, `raw` and `hex` write a seed file and a `.pub` file beside it. Stdout carries the seed alone.
- `sfinx printkey -k file [-L hash] [--in-fmt fmt] [--out-fmt fmt] [-o file] [--public-only]`: read a key file, derive the public key from the seed, and re-encode it. `--in-fmt` defaults to auto-detect, `--out-fmt` to `hdr`. `--public-only` drops the seed.
- `sfinx sign -k keyfile [-L hash] [-p path-len | -P path] [-m message | -M message-file] [--key-fmt fmt] [--sig-fmt fmt] [-o file] [-F]`: sign a message and write the signature. `--key-fmt` defaults to auto-detect with a `raw` seed fallback, `--sig-fmt` to `hex` on stdout and `raw` to a file.
- `sfinx verify -k keyfile [-L bits] [-s signature | -S signature-file] [-m message | -M message-file] [--key-fmt fmt] [--sig-fmt fmt]`: verify a signature and print `OK` or `FAIL`. `--key-fmt` defaults to auto-detect with a `rawpub` fallback, `--sig-fmt` to auto-detect with a `raw` fallback.
diff --git a/src/cli/common.c b/src/cli/common.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
+#include "sfinx.h"
#include "util/file.h"
int cli_read_message(const char *message, const char *file, uint8_t **out, size_t *len_out) {
@@ -20,6 +21,36 @@ int cli_read_message(const char *message, const char *file, uint8_t **out, size_
return util_file_read(file, out, len_out);
}
+int cli_seed_length(const char *text, size_t *len_out) {
+ char *end = NULL;
+ unsigned long v;
+ if (!text) {
+ *len_out = CLI_SEED_LEN_DEFAULT;
+ return 0;
+ }
+ v = strtoul(text, &end, 10);
+ if (!end || *end != '\0' || v == 0 || v > SFINX_SEED_LEN_MAX) {
+ return -1;
+ }
+ *len_out = (size_t)v;
+ return 0;
+}
+
+sfinx_status cli_seed_random(size_t len, uint8_t **out) {
+ uint8_t *seed = malloc(len);
+ sfinx_status status;
+ if (!seed) {
+ return SFINX_ERR_NOSPACE;
+ }
+ status = sfinx_random_bytes(seed, len);
+ if (status != SFINX_OK) {
+ free(seed);
+ return status;
+ }
+ *out = seed;
+ return SFINX_OK;
+}
+
static int hexval(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
diff --git a/src/cli/common.h b/src/cli/common.h
@@ -4,6 +4,8 @@
#include <stddef.h>
#include <stdint.h>
+#include "sfinx.h"
+
// Common {{{
//
// Message reading and hex helpers shared by the commands
@@ -12,4 +14,14 @@ int cli_read_message(const char *message, const char *file, uint8_t **out, size_
int cli_hex_decode(const char *hex, uint8_t **out, size_t *len_out);
// }}}
+// Seed {{{
+//
+// The optional length argument and the random bytes it describes
+
+#define CLI_SEED_LEN_DEFAULT 32
+
+int cli_seed_length(const char *text, size_t *len_out);
+sfinx_status cli_seed_random(size_t len, uint8_t **out);
+// }}}
+
#endif // __SFINX_CLI_COMMON_H__
diff --git a/src/cli/generate.c b/src/cli/generate.c
@@ -0,0 +1,218 @@
+#include <cofyc/argparse.h>
+#include <ctype.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common.h"
+#include "registry.h"
+#include "sfinx.h"
+#include "util/file.h"
+
+// Pub path {{{
+//
+// The public half goes next to the key file
+// a `.key` suffix of any case becomes `.pub` in the same case, anything else has `.pub` appended
+
+static int path_ends_in_key(const char *path, size_t len) {
+ static const char suffix[] = ".key";
+ if (len < 4) {
+ return 0;
+ }
+ for (size_t i = 0; i < 4; i++) {
+ if (tolower((unsigned char)path[len - 4 + i]) != suffix[i]) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+static char pub_char(char source, char lower) {
+ return isupper((unsigned char)source) ? (char)toupper((unsigned char)lower) : lower;
+}
+
+static int cli_pub_path(const char *path, char **out) {
+ size_t len = strlen(path);
+ char *buf;
+ if (path_ends_in_key(path, len)) {
+ buf = malloc(len + 1);
+ if (!buf) {
+ return -1;
+ }
+ memcpy(buf, path, len - 4);
+ buf[len - 4] = '.';
+ buf[len - 3] = pub_char(path[len - 3], 'p');
+ buf[len - 2] = pub_char(path[len - 2], 'u');
+ buf[len - 1] = pub_char(path[len - 1], 'b');
+ buf[len] = '\0';
+ *out = buf;
+ return 0;
+ }
+ buf = malloc(len + 5);
+ if (!buf) {
+ return -1;
+ }
+ memcpy(buf, path, len);
+ memcpy(buf + len, ".pub", 5);
+ *out = buf;
+ return 0;
+}
+// }}}
+
+static int cmd_generate(int argc, const char **argv) {
+ const char *out_file = NULL;
+ const char *format = "hdr";
+ const char *pub_format;
+ int hash = 0;
+ int force = 0;
+ size_t seed_len = CLI_SEED_LEN_DEFAULT;
+ uint8_t *seed = NULL;
+ uint8_t *key_buf = NULL;
+ uint8_t *pub_buf = NULL;
+ char *pub_file = NULL;
+ size_t key_len = 0;
+ size_t pub_len = 0;
+ sfinx_key key;
+ sfinx_status status;
+ int to_stdout;
+ int rc = 1;
+
+ static const char *const usages[] = {
+ "sfinx generate [length] [-L hash] [--out-fmt fmt] [-o file] [-F]",
+ NULL,
+ };
+ struct argparse_option options[] = {
+ OPT_HELP(),
+ OPT_STRING(0, "out-fmt", &format, "hdr writes one file, raw and hex write a key file and a .pub file", NULL, 0,
+ 0),
+ OPT_INTEGER('L', "hash", &hash, "hash size 224/256/384/512", NULL, 0, 0),
+ OPT_STRING('o', "out", &out_file, "key file, - for stdout", NULL, 0, 0),
+ OPT_BOOLEAN('F', "force", &force, "overwrite existing files", NULL, 0, 0),
+ OPT_END(),
+ };
+ struct argparse argparse;
+ argparse_init(&argparse, options, usages, 0);
+ argparse_describe(&argparse, "\nGenerate a seed and its public key", NULL);
+ argc = argparse_parse(&argparse, argc, argv);
+
+ sfinx_key_init(&key);
+
+ if (argc > 1) {
+ fprintf(stderr, "sfinx: generate: too many arguments\n");
+ return 1;
+ }
+ if (strcmp(format, "hdr") == 0) {
+ pub_format = NULL;
+ } else if (strcmp(format, "raw") == 0) {
+ pub_format = "rawpub";
+ } else if (strcmp(format, "hex") == 0) {
+ pub_format = "hexpub";
+ } else {
+ fprintf(stderr, "sfinx: generate: unknown format: %s\n", format);
+ return 1;
+ }
+ if (cli_seed_length(argc == 1 ? argv[0] : NULL, &seed_len) != 0) {
+ fprintf(stderr, "sfinx: generate: invalid length: %s\n", argv[0]);
+ return 1;
+ }
+
+ status = cli_seed_random(seed_len, &seed);
+ if (status != SFINX_OK) {
+ fprintf(stderr, "sfinx: generate: %s\n", sfinx_strerror(status));
+ return 1;
+ }
+ key.seed = seed;
+ key.seed_len = seed_len;
+ seed = NULL;
+ key.hash = hash != 0 ? (sfinx_hash)hash : SFINX_HASH_DEFAULT;
+ if (sfinx_hash_len(key.hash) == 0) {
+ fprintf(stderr, "sfinx: generate: invalid hash size: %d\n", hash);
+ goto done;
+ }
+ status = sfinx_key_derive(&key);
+ if (status != SFINX_OK) {
+ fprintf(stderr, "sfinx: generate: cannot derive the public key: %s\n", sfinx_strerror(status));
+ goto done;
+ }
+
+ to_stdout = !out_file || strcmp(out_file, "-") == 0;
+ if (pub_format && !to_stdout) {
+ if (cli_pub_path(out_file, &pub_file) != 0) {
+ fprintf(stderr, "sfinx: generate: out of memory\n");
+ goto done;
+ }
+ }
+ if (!to_stdout && !force) {
+ if (util_file_exists(out_file)) {
+ fprintf(stderr, "sfinx: generate: %s exists\n", out_file);
+ goto done;
+ }
+ if (pub_file && util_file_exists(pub_file)) {
+ fprintf(stderr, "sfinx: generate: %s exists\n", pub_file);
+ goto done;
+ }
+ }
+
+ key_len = sfinx_key_encode_len(&key, format);
+ if (key_len == 0) {
+ fprintf(stderr, "sfinx: generate: cannot encode a key as %s\n", format);
+ goto done;
+ }
+ key_buf = malloc(key_len);
+ if (!key_buf) {
+ fprintf(stderr, "sfinx: generate: out of memory\n");
+ goto done;
+ }
+ status = sfinx_key_encode(&key, format, key_buf, key_len, &key_len);
+ if (status != SFINX_OK) {
+ fprintf(stderr, "sfinx: generate: cannot encode: %s\n", sfinx_strerror(status));
+ goto done;
+ }
+
+ if (pub_file) {
+ pub_len = sfinx_key_encode_len(&key, pub_format);
+ pub_buf = malloc(pub_len);
+ if (!pub_buf) {
+ fprintf(stderr, "sfinx: generate: out of memory\n");
+ goto done;
+ }
+ status = sfinx_key_encode(&key, pub_format, pub_buf, pub_len, &pub_len);
+ if (status != SFINX_OK) {
+ fprintf(stderr, "sfinx: generate: cannot encode the public key: %s\n", sfinx_strerror(status));
+ goto done;
+ }
+ }
+
+ if (util_file_write(out_file, key_buf, key_len, 0600, force) != 0) {
+ fprintf(stderr, "sfinx: generate: cannot write %s\n", out_file ? out_file : "-");
+ goto done;
+ }
+ if (pub_buf) {
+ if (util_file_write(pub_file, pub_buf, pub_len, 0644, force) != 0) {
+ fprintf(stderr, "sfinx: generate: cannot write %s\n", pub_file);
+ goto done;
+ }
+ }
+ rc = 0;
+
+done:
+ free(seed);
+ free(key_buf);
+ free(pub_buf);
+ free(pub_file);
+ sfinx_key_free(&key);
+ return rc;
+}
+
+static struct cli_command generate_command = {
+ .names = (const char *const[]){"generate", NULL},
+ .display = "generate",
+ .description = "Generate a seed and its public key",
+ .help = "sfinx generate [length] [-L hash] [--out-fmt fmt] [-o file] [-F]",
+ .fn = cmd_generate,
+};
+
+__attribute__((constructor)) static void generate_register(void) {
+ cli_command_register(&generate_command);
+}
diff --git a/src/cli/seed.c b/src/cli/seed.c
@@ -2,16 +2,18 @@
#include <stdio.h>
#include <stdlib.h>
+#include "common.h"
#include "registry.h"
#include "sfinx.h"
#include "util/file.h"
static int cmd_seed(int argc, const char **argv) {
- const char *out_file = NULL;
- int force = 0;
- size_t length = 32;
- uint8_t *seed;
- int rc = 0;
+ const char *out_file = NULL;
+ int force = 0;
+ size_t length = CLI_SEED_LEN_DEFAULT;
+ uint8_t *seed = NULL;
+ sfinx_status status;
+ int rc = 0;
static const char *const usages[] = {
"sfinx seed [length] [-o file]",
@@ -32,24 +34,14 @@ static int cmd_seed(int argc, const char **argv) {
fprintf(stderr, "sfinx: seed: too many arguments\n");
return 1;
}
- if (argc == 1) {
- char *end = NULL;
- unsigned long v = strtoul(argv[0], &end, 10);
- if (!end || *end != '\0' || v == 0 || v > SFINX_SEED_LEN_MAX) {
- fprintf(stderr, "sfinx: seed: invalid length: %s\n", argv[0]);
- return 1;
- }
- length = (size_t)v;
- }
-
- seed = malloc(length);
- if (!seed) {
- fprintf(stderr, "sfinx: seed: out of memory\n");
+ if (cli_seed_length(argc == 1 ? argv[0] : NULL, &length) != 0) {
+ fprintf(stderr, "sfinx: seed: invalid length: %s\n", argv[0]);
return 1;
}
- if (sfinx_random_bytes(seed, length) != SFINX_OK) {
- fprintf(stderr, "sfinx: seed: %s\n", sfinx_strerror(SFINX_ERR_RANDOM));
- free(seed);
+
+ status = cli_seed_random(length, &seed);
+ if (status != SFINX_OK) {
+ fprintf(stderr, "sfinx: seed: %s\n", sfinx_strerror(status));
return 1;
}
diff --git a/src/util/file.c b/src/util/file.c
@@ -74,3 +74,8 @@ int util_file_write(const char *path, const uint8_t *data, size_t len, unsigned
close(fd);
return rc;
}
+
+int util_file_exists(const char *path) {
+ if (!path || strcmp(path, "-") == 0) return 0;
+ return access(path, F_OK) == 0;
+}
diff --git a/src/util/file.h b/src/util/file.h
@@ -8,9 +8,11 @@
//
// Whole-file IO for the CLI, "-" or NULL means stdin or stdout
// read mallocs len + 1 and NUL terminates, write sets the mode and O_EXCL
+// exists answers whether a path is already taken
int util_file_read(const char *path, uint8_t **out, size_t *len_out);
int util_file_write(const char *path, const uint8_t *data, size_t len, unsigned mode, int force);
+int util_file_exists(const char *path);
// }}}
#endif // __SFINX_UTIL_FILE_H__
diff --git a/test/test_generate.sh b/test/test_generate.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+set -eu
+HERE=$(cd "$(dirname "$0")" && pwd)
+ROOT=$(cd "${HERE}/.." && pwd)
+. "${HERE}/_tap.sh"
+
+BIN=$(ensure_bin "${ROOT}")
+TMP=$(mktemp -d)
+trap 'rm -rf "${TMP}"' EXIT
+
+mode() {
+ stat -c '%a' "$1" 2>/dev/null || stat -f '%Lp' "$1"
+}
+
+bytes() {
+ wc -c <"$1" | tr -d '[:space:]'
+}
+
+tap_begin "cli: generate"
+
+"${BIN}" generate -o "${TMP}/a.hdr"
+grep -q '^seed: ' "${TMP}/a.hdr" && grep -q '^public-key: ' "${TMP}/a.hdr" && ok=1 || ok=0
+tap "${ok}" "hdr writes one combined file" "$(cat "${TMP}/a.hdr")"
+tap "$([ ! -e "${TMP}/a.hdr.pub" ] && echo 1 || echo 0)" "hdr creates no pub file" "a.hdr.pub exists"
+tap "$([ "$(mode "${TMP}/a.hdr")" = 600 ] && echo 1 || echo 0)" "hdr key file is 0600" "got $(mode "${TMP}/a.hdr")"
+
+"${BIN}" generate >"${TMP}/stdout.hdr"
+grep -q '^seed: ' "${TMP}/stdout.hdr" && grep -q '^public-key: ' "${TMP}/stdout.hdr" && ok=1 || ok=0
+tap "${ok}" "hdr to stdout is the combined file" "$(cat "${TMP}/stdout.hdr")"
+
+"${BIN}" generate --out-fmt raw -o "${TMP}/b.key"
+tap "$([ "$(bytes "${TMP}/b.key")" = 32 ] && echo 1 || echo 0)" "raw key holds a 32-byte seed" "got $(bytes "${TMP}/b.key")"
+tap "$([ "$(bytes "${TMP}/b.pub")" = 32 ] && echo 1 || echo 0)" "raw pub holds a 32-byte public key" "got $(bytes "${TMP}/b.pub")"
+tap "$([ ! -e "${TMP}/b.key.pub" ] && echo 1 || echo 0)" "the .key suffix is replaced, not appended to" "b.key.pub exists"
+tap "$([ "$(mode "${TMP}/b.key")" = 600 ] && [ "$(mode "${TMP}/b.pub")" = 644 ] && echo 1 || echo 0)" \
+ "raw pair is 0600 and 0644" "$(mode "${TMP}/b.key") $(mode "${TMP}/b.pub")"
+
+"${BIN}" generate --out-fmt raw -o "${TMP}/c.KEY"
+tap "$([ -e "${TMP}/c.PUB" ] && echo 1 || echo 0)" ".KEY becomes .PUB" "$(ls -1 "${TMP}")"
+"${BIN}" generate --out-fmt raw -o "${TMP}/d.Key"
+tap "$([ -e "${TMP}/d.Pub" ] && echo 1 || echo 0)" ".Key becomes .Pub" "$(ls -1 "${TMP}")"
+"${BIN}" generate --out-fmt hex -o "${TMP}/e.hex"
+tap "$([ -e "${TMP}/e.hex.pub" ] && echo 1 || echo 0)" "a suffix that is not .key has .pub appended" "$(ls -1 "${TMP}")"
+
+grep -qE '^[0-9a-f]{64}$' "${TMP}/e.hex" && ok=1 || ok=0
+tap "${ok}" "hex key file is 64 hex characters" "$(cat "${TMP}/e.hex")"
+grep -qE '^[0-9a-f]{64}$' "${TMP}/e.hex.pub" && ok=1 || ok=0
+tap "${ok}" "hex pub file is 64 hex characters" "$(cat "${TMP}/e.hex.pub")"
+
+"${BIN}" generate --out-fmt raw >"${TMP}/out.raw"
+tap "$([ "$(bytes "${TMP}/out.raw")" = 32 ] && echo 1 || echo 0)" "raw stdout is seed only" "got $(bytes "${TMP}/out.raw")"
+
+"${BIN}" generate --out-fmt hex >"${TMP}/out.hex"
+tap "$([ "$(bytes "${TMP}/out.hex")" = 64 ] && echo 1 || echo 0)" "hex stdout is seed only" "got $(bytes "${TMP}/out.hex")"
+
+"${BIN}" generate 16 --out-fmt raw -o "${TMP}/s.key"
+tap "$([ "$(bytes "${TMP}/s.key")" = 16 ] && echo 1 || echo 0)" "a length argument sizes the seed" "got $(bytes "${TMP}/s.key")"
+
+"${BIN}" generate -L 512 --out-fmt raw -o "${TMP}/h.key"
+tap "$([ "$(bytes "${TMP}/h.pub")" = 64 ] && echo 1 || echo 0)" "--hash 512 gives a 64-byte pub" "got $(bytes "${TMP}/h.pub")"
+
+"${BIN}" printkey --in-fmt raw -k "${TMP}/b.key" --public-only --out-fmt rawpub >"${TMP}/b.expect"
+cmp -s "${TMP}/b.pub" "${TMP}/b.expect" && ok=1 || ok=0
+tap "${ok}" "the pub matches printkey for the same seed" "differs"
+
+rc=0
+"${BIN}" generate -o "${TMP}/a.hdr" >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "refuses an existing key file" "exit ${rc}"
+
+rc=0
+"${BIN}" generate -F -o "${TMP}/a.hdr" >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" = 0 ] && echo 1 || echo 0)" "--force overwrites" "exit ${rc}"
+
+rm "${TMP}/s.key"
+rc=0
+"${BIN}" generate --out-fmt raw -o "${TMP}/s.key" >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "refuses when only the pub is taken" "exit ${rc}"
+tap "$([ ! -e "${TMP}/s.key" ] && echo 1 || echo 0)" "a refused pair writes no key file" "s.key exists"
+
+rc=0
+"${BIN}" generate --out-fmt rawpub -o "${TMP}/no.key" >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects a pub-only output format" "exit ${rc}"
+
+rc=0
+"${BIN}" generate --out-fmt bogus >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects an unknown format" "exit ${rc}"
+
+rc=0
+"${BIN}" generate 0 >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects a zero length" "exit ${rc}"
+
+rc=0
+"${BIN}" generate abc >/dev/null 2>&1 || rc=$?
+tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects a non-numeric length" "exit ${rc}"
+
+"${BIN}" generate --out-fmt raw -o "${TMP}/v.key"
+"${BIN}" sign -k "${TMP}/v.key" -m hello -P 0011 -o "${TMP}/v.sig"
+"${BIN}" verify -k "${TMP}/v.pub" -m hello -S "${TMP}/v.sig" >/dev/null && ok=1 || ok=0
+tap "${ok}" "sign with the key, verify with the pub" "failed"
+
+tap_plan
diff --git a/test/test_subcommands.sh b/test/test_subcommands.sh
@@ -15,6 +15,8 @@ tap "${ok}" "--help shows usage" "${out}"
out=$("${BIN}" help 2>&1 || true)
case "${out}" in *seed*) ok=1 ;; *) ok=0 ;; esac
tap "${ok}" "help lists seed" "${out}"
+case "${out}" in *generate*) ok=1 ;; *) ok=0 ;; esac
+tap "${ok}" "help lists generate" "${out}"
case "${out}" in *printkey*) ok=1 ;; *) ok=0 ;; esac
tap "${ok}" "help lists printkey" "${out}"
case "${out}" in *sign*) ok=1 ;; *) ok=0 ;; esac