sfinx

sphincs+ inspired signing utility
git clone git://git.finwo.net/app/sfinx
Log | Files | Refs | README

commit 3dab62dfc4f36ebbbfeac016da1ed24866cac826
parent 14f714af6cede62ced558fe0bd29132667a11a06
Author: finwo <finwo@pm.me>
Date:   Sat, 10 Oct 2026 00:05:38 +0200

Drop unused dep; use argparse foor root as well

Diffstat:
Msrc/cli/common.c | 32++++++++++++++++++++++++++++++++
Msrc/cli/common.h | 9+++++++++
Msrc/cli/registry.c | 36++++++++++++++++++++++++++++++++++++
Msrc/cli/registry.h | 1+
Msrc/cli/usage.c | 6+-----
Msrc/main.c | 43++++++++++++++++++++++++++++---------------
Mtarget/common/.dep | 1-
Mtest/test_embed.sh | 2+-
Mtest/test_subcommands.sh | 4+++-
9 files changed, 111 insertions(+), 23 deletions(-)

diff --git a/src/cli/common.c b/src/cli/common.c @@ -51,6 +51,38 @@ sfinx_status cli_seed_random(size_t len, uint8_t **out) { return SFINX_OK; } +sfinx_status cli_key_derive(sfinx_key *key, size_t path_len) { + size_t len; + uint8_t *pub; + sfinx_status status; + + if (!key->seed) { + return sfinx_key_derive(key); + } + len = sfinx_hash_len(key->hash); + if (len == 0) { + return SFINX_ERR_ARGS; + } + free(key->pub); + key->pub = NULL; + key->pub_len = 0; + if (path_len != 0) { + return sfinx_key_derive(key); + } + pub = malloc(len); + if (!pub) { + return SFINX_ERR_NOSPACE; + } + status = sfinx_single_public_key(key->hash, key->seed, key->seed_len, pub, len, NULL); + if (status != SFINX_OK) { + free(pub); + return status; + } + key->pub = pub; + key->pub_len = len; + 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 @@ -24,4 +24,13 @@ int cli_seed_length(const char *text, size_t *len_out); sfinx_status cli_seed_random(size_t len, uint8_t **out); // }}} +// Key derive {{{ +// +// Derive the public half of a key for one key mode +// path_len 0 selects the single-key mode, any other value the stacked subtrees +// a seed in the key wins over a stored public key, so --hash and --path-len cannot disagree with the file + +sfinx_status cli_key_derive(sfinx_key *key, size_t path_len); +// }}} + #endif // __SFINX_CLI_COMMON_H__ diff --git a/src/cli/registry.c b/src/cli/registry.c @@ -1,13 +1,48 @@ #include "registry.h" +#include <stdio.h> +#include <stdlib.h> #include <string.h> struct cli_command *cli_commands = NULL; +char *command_list = NULL; + +// Command list {{{ +// +// The rendered command table, refreshed on every registration +// commands register from constructors, so the list is final before main runs +// the format is what the global usage has always printed + +static void command_list_render(void) { + struct cli_command *command; + char *text; + size_t len = sizeof("commands:\n"); + size_t at = 0; + + for (command = cli_commands; command; command = command->next) { + len += (size_t)snprintf(NULL, 0, " %-12s %s\n", command->display, command->description); + } + text = malloc(len); + if (!text) return; + at += (size_t)snprintf(text + at, len - at, "commands:\n"); + for (command = cli_commands; command; command = command->next) { + at += (size_t)snprintf(text + at, len - at, " %-12s %s\n", command->display, command->description); + } + free(command_list); + command_list = text; +} +// }}} + +// Registry {{{ +// +// Commands prepend themselves on load +// names is a NULL terminated alias list, argv[0] is the command name void cli_command_register(struct cli_command *command) { if (!command) return; command->next = cli_commands; cli_commands = command; + command_list_render(); } struct cli_command *cli_command_find(const char *name) { @@ -22,3 +57,4 @@ struct cli_command *cli_command_find(const char *name) { } return NULL; } +// }}} diff --git a/src/cli/registry.h b/src/cli/registry.h @@ -16,6 +16,7 @@ struct cli_command { }; extern struct cli_command *cli_commands; +extern char *command_list; void cli_command_register(struct cli_command *command); struct cli_command *cli_command_find(const char *name); diff --git a/src/cli/usage.c b/src/cli/usage.c @@ -5,9 +5,5 @@ #include "registry.h" void cli_print_global_usage(void) { - struct cli_command *command; - fprintf(stdout, "usage: sfinx <command> [options]\n\ncommands:\n"); - for (command = cli_commands; command; command = command->next) { - fprintf(stdout, " %-12s %s\n", command->display, command->description); - } + fprintf(stdout, "usage: sfinx <command> [options]\n\n%s", command_list ? command_list : ""); } diff --git a/src/main.c b/src/main.c @@ -1,31 +1,44 @@ +#include <cofyc/argparse.h> #include <stdio.h> -#include <string.h> #include "cli/registry.h" -#include "cli/usage.h" #include "sfinx.h" +static const char *const usages[] = { + "sfinx [global] command [local]", + "sfinx help", + "sfinx --version", + NULL, +}; + int main(int argc, char **argv) { struct cli_command *command; + struct argparse argparse; + int version = 0; - if (argc < 2) { - cli_print_global_usage(); - return 1; - } - if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { - cli_print_global_usage(); - return 0; - } - if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) { + struct argparse_option options[] = { + OPT_HELP(), + OPT_BOOLEAN('V', "version", &version, "show version and exit", NULL, 0, 0), + OPT_END(), + }; + + argparse_init(&argparse, options, usages, ARGPARSE_STOP_AT_NON_OPTION); + argparse_describe(&argparse, NULL, command_list); + argc = argparse_parse(&argparse, argc, (const char **)argv); + + if (version) { fprintf(stdout, "%s\n", sfinx_version()); return 0; } + if (argc < 1) { + argparse_usage(&argparse); + return 1; + } - command = cli_command_find(argv[1]); + command = cli_command_find(argv[0]); if (!command) { - fprintf(stderr, "sfinx: unknown command: %s\n", argv[1]); - cli_print_global_usage(); + fprintf(stderr, "sfinx: unknown command: %s\n", argv[0]); return 1; } - return command->fn(argc - 1, (const char **)(argv + 1)); + return command->fn(argc, (const char **)argv); } diff --git a/target/common/.dep b/target/common/.dep @@ -1,3 +1,2 @@ cofyc/argparse https://git.finwo.net/misc/dep-repository/archives/heads/pkg/cofyc/argparse.tar.gz coruus/keccak-tiny https://git.finwo.net/misc/dep-repository/archives/heads/pkg/coruus/keccak-tiny.tar.gz -rxi/log https://git.finwo.net/misc/dep-repository/archives/heads/pkg/rxi/log.tar.gz diff --git a/test/test_embed.sh b/test/test_embed.sh @@ -1,7 +1,7 @@ #!/bin/sh # Reads export.mk for the library source list, compiles a program that includes # only sfinx.h against that list plus keccak-tiny, then exercises the crypto and -# the key format API. No CLI, no util, no argparse, no rxi/log is reachable. +# the key format API. No CLI, no util, no argparse is reachable. set -eu HERE=$(cd "$(dirname "$0")" && pwd) ROOT=$(cd "${HERE}/.." && pwd) diff --git a/test/test_subcommands.sh b/test/test_subcommands.sh @@ -9,10 +9,12 @@ BIN=$(ensure_bin "${ROOT}") tap_begin "cli: subcommands" out=$("${BIN}" --help 2>&1 || true) -case "${out}" in *"usage: sfinx"*) ok=1 ;; *) ok=0 ;; esac +case "${out}" in *"Usage: sfinx"*) ok=1 ;; *) ok=0 ;; esac tap "${ok}" "--help shows usage" "${out}" out=$("${BIN}" help 2>&1 || true) +case "${out}" in *"usage: sfinx"*) ok=1 ;; *) ok=0 ;; esac +tap "${ok}" "help shows usage" "${out}" case "${out}" in *seed*) ok=1 ;; *) ok=0 ;; esac tap "${ok}" "help lists seed" "${out}" case "${out}" in *generate*) ok=1 ;; *) ok=0 ;; esac