sfinx

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

commit 404586d47b2386c60ecd86ecadc2688fbaa0e5fd
parent 550caa0a08e017e895474dfc6ee69df3d3a94e3a
Author: finwo <finwo@pm.me>
Date:   Fri,  9 Oct 2026 21:11:56 +0200

printkey and seed subcommands

Diffstat:
A.dep.export | 1+
MREADME.md | 10++++++++++
Aexport.mk | 3+++
Msrc/cli/help.c | 19++++++++++++++-----
Asrc/cli/printkey.c | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/cli/registry.c | 43++++++++++++++-----------------------------
Msrc/cli/registry.h | 22++++++++++++++++++----
Asrc/cli/seed.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/cli/usage.c | 13+++++++++++++
Asrc/cli/usage.h | 6++++++
Asrc/cli/version.c | 24++++++++++++++++++++++++
Asrc/fmt/hdr.c | 199+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/fmt/raw.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/keypair.c | 139+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.c | 45+++++++++++++++++++++++----------------------
Msrc/sfinx.h | 40++++++++++++++++++++++++++++++++++++++++
Asrc/util/file.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/util/file.h | 16++++++++++++++++
Atest/07-key.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/Makefile | 7++++---
Mtest/_tap.sh | 13++++++++++++-
Mtest/test_embed.sh | 91++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Atest/test_printkey.sh | 48++++++++++++++++++++++++++++++++++++++++++++++++
Atest/test_seed.sh | 44++++++++++++++++++++++++++++++++++++++++++++
Atest/test_subcommands.sh | 35+++++++++++++++++++++++++++++++++++
25 files changed, 1128 insertions(+), 90 deletions(-)

diff --git a/.dep.export b/.dep.export @@ -0,0 +1 @@ +include/finwo/sfinx.h src/sfinx.h diff --git a/README.md b/README.md @@ -188,6 +188,16 @@ sfinx is stateless. There is no counter and no state file. A signature is a proof that you can produce the path to a given root, and the virtual tree gives a universal keyspace. +## Command line + +`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 printkey -k file [-L hash] [-f format] [--in format] [-o file] [--public-only]`: read a key file, derive the public key from the seed, and re-encode it. `--public-only` drops the seed. +- `sfinx help` and `sfinx version`. + +A key file carries a seed, a public key, or both. `hdr` is the text form (`hash:`, `seed:`, `public-key:`), `raw` is a bare seed. The encode and decode code lives in the library, so any program using sfinx reads and writes the same files. + ## Pseudocode A sketch, not reference code. diff --git a/export.mk b/export.mk @@ -0,0 +1,3 @@ +SRC+={{module.dirname}}/src/sfinx.c +SRC+={{module.dirname}}/src/keypair.c +SRC+=$(wildcard {{module.dirname}}/src/fmt/*.c) diff --git a/src/cli/help.c b/src/cli/help.c @@ -1,14 +1,23 @@ -#include <stdio.h> +#include <stddef.h> #include "registry.h" +#include "usage.h" -int main_help(int argc, char *argv[]) { +static int cmd_help(int argc, const char **argv) { (void)argc; (void)argv; - fprintf(stdout, "hi there\n"); + cli_print_global_usage(); return 0; } -__attribute__((constructor)) static void register_help(void) { - cli_register("help", main_help); +static struct cli_command help_command = { + .names = (const char *const[]){"help", NULL}, + .display = "help", + .description = "Show this help", + .help = "sfinx help", + .fn = cmd_help, +}; + +__attribute__((constructor)) static void help_register(void) { + cli_command_register(&help_command); } diff --git a/src/cli/printkey.c b/src/cli/printkey.c @@ -0,0 +1,128 @@ +#include <cofyc/argparse.h> +#include <stdio.h> +#include <stdlib.h> + +#include "registry.h" +#include "sfinx.h" +#include "util/file.h" + +static int cmd_printkey(int argc, const char **argv) { + const char *key_file = NULL; + const char *out_file = NULL; + const char *format = "hdr"; + const char *in_format = NULL; + int hash = 0; + int public_only = 0; + int force = 0; + uint8_t *data = NULL; + uint8_t *out = NULL; + size_t data_len = 0; + size_t out_len = 0; + sfinx_key key; + sfinx_status status; + int rc = 0; + + static const char *const usages[] = { + "sfinx printkey -k file [-L hash] [-f format] [--in format] [-o file] [--public-only]", + NULL, + }; + struct argparse_option options[] = { + OPT_HELP(), + OPT_STRING('k', "key-file", &key_file, "key file to read", NULL, 0, 0), + OPT_STRING('f', "format", &format, "output format (default hdr)", NULL, 0, 0), + OPT_STRING(0, "in", &in_format, "input format (default auto)", NULL, 0, 0), + OPT_INTEGER('L', "hash", &hash, "hash size (224/256/384/512)", NULL, 0, 0), + OPT_STRING('o', "out", &out_file, "write to a file instead of stdout", NULL, 0, 0), + OPT_BOOLEAN(0, "public-only", &public_only, "output only the public key", NULL, 0, 0), + OPT_BOOLEAN(0, "force", &force, "overwrite an existing file", NULL, 0, 0), + OPT_END(), + }; + struct argparse argparse; + argparse_init(&argparse, options, usages, 0); + argparse_describe(&argparse, "\nPrint or convert a key file", NULL); + argc = argparse_parse(&argparse, argc, argv); + + if (argc > 0) { + fprintf(stderr, "sfinx: printkey: unexpected argument: %s\n", argv[0]); + return 1; + } + if (!key_file) { + fprintf(stderr, "sfinx: printkey: missing --key-file\n"); + return 1; + } + if (util_file_read(key_file, &data, &data_len) != 0) { + fprintf(stderr, "sfinx: printkey: cannot read %s\n", key_file); + return 1; + } + + status = sfinx_key_decode(data, data_len, in_format, &key); + free(data); + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: printkey: cannot decode %s: %s\n", key_file, sfinx_strerror(status)); + return 1; + } + if (hash != 0) { + key.hash = (sfinx_hash)hash; + } + if (sfinx_hash_len(key.hash) == 0) { + fprintf(stderr, "sfinx: printkey: invalid hash size: %d\n", (int)key.hash); + sfinx_key_free(&key); + return 1; + } + status = sfinx_key_derive(&key); + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: printkey: cannot derive public key: %s\n", sfinx_strerror(status)); + sfinx_key_free(&key); + return 1; + } + if (public_only) { + free(key.seed); + key.seed = NULL; + key.seed_len = 0; + } + + if (!sfinx_format_find(format)) { + fprintf(stderr, "sfinx: printkey: unknown format: %s\n", format); + sfinx_key_free(&key); + return 1; + } + out_len = sfinx_key_encode_len(&key, format); + if (out_len == 0) { + fprintf(stderr, "sfinx: printkey: cannot encode key as %s\n", format); + sfinx_key_free(&key); + return 1; + } + out = malloc(out_len); + if (!out) { + fprintf(stderr, "sfinx: printkey: out of memory\n"); + sfinx_key_free(&key); + return 1; + } + status = sfinx_key_encode(&key, format, out, out_len, &out_len); + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: printkey: cannot encode: %s\n", sfinx_strerror(status)); + rc = 1; + } else if (out_file) { + if (util_file_write(out_file, out, out_len, key.seed ? 0600 : 0644, force) != 0) { + fprintf(stderr, "sfinx: printkey: cannot write %s\n", out_file); + rc = 1; + } + } else if (fwrite(out, 1, out_len, stdout) != out_len) { + rc = 1; + } + free(out); + sfinx_key_free(&key); + return rc; +} + +static struct cli_command printkey_command = { + .names = (const char *const[]){"printkey", NULL}, + .display = "printkey", + .description = "Print or convert a key file", + .help = "sfinx printkey -k file [-L hash] [-f format] [--in format] [-o file] [--public-only]", + .fn = cmd_printkey, +}; + +__attribute__((constructor)) static void printkey_register(void) { + cli_command_register(&printkey_command); +} diff --git a/src/cli/registry.c b/src/cli/registry.c @@ -1,39 +1,24 @@ #include "registry.h" -#include <stdlib.h> #include <string.h> -#define CLI_MAX 32 +struct cli_command *cli_commands = NULL; -static const char *cli_names[CLI_MAX]; -static cli_main_fn cli_fns[CLI_MAX]; -static int cli_count = 0; - -void cli_register(const char *name, cli_main_fn fn) { - if (cli_count >= CLI_MAX) return; - cli_names[cli_count] = name; - cli_fns[cli_count] = fn; - cli_count++; +void cli_command_register(struct cli_command *command) { + if (!command) return; + command->next = cli_commands; + cli_commands = command; } -cli_main_fn cli_find(const char *name) { - for (int i = 0; i < cli_count; i++) { - if (!strcmp(cli_names[i], name)) return cli_fns[i]; +struct cli_command *cli_command_find(const char *name) { + struct cli_command *command; + if (!name) return NULL; + for (command = cli_commands; command; command = command->next) { + for (const char *const *alias = command->names; alias && *alias; alias++) { + if (!strcmp(*alias, name)) { + return command; + } + } } return NULL; } - -char *cli_registered() { - char *out; - int len = 1; - for (int i = 0; i < cli_count; i++) { - if (i) len += 2; - len += strlen(cli_names[i]); - } - out = calloc(1, len); - for (int i = 0; i < cli_count; i++) { - if (i) strcat(out, ", "); - strcat(out, cli_names[i]); - } - return out; -} diff --git a/src/cli/registry.h b/src/cli/registry.h @@ -1,10 +1,24 @@ #ifndef __SFINX_CLI_REGISTRY_H__ #define __SFINX_CLI_REGISTRY_H__ -typedef int (*cli_main_fn)(int argc, char **argv); +// Registry {{{ +// +// Commands prepend themselves on load +// names is a NULL terminated alias list, argv[0] is the command name -void cli_register(const char *name, cli_main_fn fn); -cli_main_fn cli_find(const char *name); -char *cli_registered(); +struct cli_command { + struct cli_command *next; + const char *const *names; + const char *display; + const char *description; + const char *help; + int (*fn)(int argc, const char **argv); +}; + +extern struct cli_command *cli_commands; + +void cli_command_register(struct cli_command *command); +struct cli_command *cli_command_find(const char *name); +// }}} #endif // __SFINX_CLI_REGISTRY_H__ diff --git a/src/cli/seed.c b/src/cli/seed.c @@ -0,0 +1,78 @@ +#include <cofyc/argparse.h> +#include <stdio.h> +#include <stdlib.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; + + static const char *const usages[] = { + "sfinx seed [length] [-o file]", + NULL, + }; + struct argparse_option options[] = { + OPT_HELP(), + OPT_STRING('o', "out", &out_file, "write the seed to a file", NULL, 0, 0), + OPT_BOOLEAN(0, "force", &force, "overwrite an existing file", NULL, 0, 0), + OPT_END(), + }; + struct argparse argparse; + argparse_init(&argparse, options, usages, 0); + argparse_describe(&argparse, "\nGenerate a random seed", NULL); + argc = argparse_parse(&argparse, argc, argv); + + if (argc > 1) { + 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"); + return 1; + } + if (sfinx_random_bytes(seed, length) != SFINX_OK) { + fprintf(stderr, "sfinx: seed: %s\n", sfinx_strerror(SFINX_ERR_RANDOM)); + free(seed); + return 1; + } + + if (out_file) { + if (util_file_write(out_file, seed, length, 0600, force) != 0) { + fprintf(stderr, "sfinx: seed: cannot write %s\n", out_file); + rc = 1; + } + } else if (fwrite(seed, 1, length, stdout) != length) { + rc = 1; + } + free(seed); + return rc; +} + +static struct cli_command seed_command = { + .names = (const char *const[]){"seed", NULL}, + .display = "seed", + .description = "Generate a random seed", + .help = "sfinx seed [length] [-o file]", + .fn = cmd_seed, +}; + +__attribute__((constructor)) static void seed_register(void) { + cli_command_register(&seed_command); +} diff --git a/src/cli/usage.c b/src/cli/usage.c @@ -0,0 +1,13 @@ +#include "usage.h" + +#include <stdio.h> + +#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); + } +} diff --git a/src/cli/usage.h b/src/cli/usage.h @@ -0,0 +1,6 @@ +#ifndef __SFINX_CLI_USAGE_H__ +#define __SFINX_CLI_USAGE_H__ + +void cli_print_global_usage(void); + +#endif // __SFINX_CLI_USAGE_H__ diff --git a/src/cli/version.c b/src/cli/version.c @@ -0,0 +1,24 @@ +#include <stddef.h> +#include <stdio.h> + +#include "registry.h" +#include "sfinx.h" + +static int cmd_version(int argc, const char **argv) { + (void)argc; + (void)argv; + fprintf(stdout, "%s\n", sfinx_version()); + return 0; +} + +static struct cli_command version_command = { + .names = (const char *const[]){"version", NULL}, + .display = "version", + .description = "Show the version", + .help = "sfinx version", + .fn = cmd_version, +}; + +__attribute__((constructor)) static void version_register(void) { + cli_command_register(&version_command); +} diff --git a/src/fmt/hdr.c b/src/fmt/hdr.c @@ -0,0 +1,199 @@ +#include <stdlib.h> +#include <string.h> + +#include "../sfinx.h" + +// hdr format {{{ +// +// Text key file, self-describing by label, unknown labels are ignored +// hash: <bits> +// seed: <hex | (no seed)> +// public-key: <hex> +// labels are case-insensitive, first valid occurrence wins, a lone seed derives its public half + +static int hdr_hexval(uint8_t c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +static uint8_t *hdr_hex_decode(const uint8_t *in, size_t in_len, size_t *out_len) { + uint8_t *out; + if (in_len % 2 != 0) { + return NULL; + } + out = malloc(in_len / 2 + 1); + if (!out) { + return NULL; + } + for (size_t i = 0; i < in_len / 2; i++) { + int hi = hdr_hexval(in[i * 2]); + int lo = hdr_hexval(in[i * 2 + 1]); + if (hi < 0 || lo < 0) { + free(out); + return NULL; + } + out[i] = (uint8_t)((hi << 4) | lo); + } + *out_len = in_len / 2; + return out; +} + +static void hdr_hex_encode(uint8_t *out, const uint8_t *in, size_t in_len) { + static const char digits[] = "0123456789abcdef"; + for (size_t i = 0; i < in_len; i++) { + out[i * 2] = (uint8_t)digits[(in[i] >> 4) & 0x0f]; + out[i * 2 + 1] = (uint8_t)digits[in[i] & 0x0f]; + } +} + +static int hdr_match(const uint8_t *line, size_t len, const char *label, const uint8_t **val, size_t *val_len) { + size_t at = 0; + size_t label_len = strlen(label); + while (at < len && (line[at] == ' ' || line[at] == '\t')) at++; + if (at + label_len > len) return 0; + for (size_t i = 0; i < label_len; i++) { + uint8_t c = line[at + i]; + if (c >= 'A' && c <= 'Z') c = (uint8_t)(c - 'A' + 'a'); + if (c != (uint8_t)label[i]) return 0; + } + at += label_len; + while (at < len && (line[at] == ' ' || line[at] == '\t')) at++; + if (at >= len || line[at] != ':') return 0; + at++; + while (at < len && (line[at] == ' ' || line[at] == '\t')) at++; + size_t end = len; + while (end > at && (line[end - 1] == ' ' || line[end - 1] == '\t' || line[end - 1] == '\r')) end--; + *val = line + at; + *val_len = end - at; + return 1; +} + +static int hdr_detect(const uint8_t *data, size_t len) { + const uint8_t *val; + size_t val_len; + for (size_t pos = 0; pos < len;) { + size_t end = pos; + while (end < len && data[end] != '\n') end++; + if (hdr_match(data + pos, end - pos, "seed", &val, &val_len) || + hdr_match(data + pos, end - pos, "public-key", &val, &val_len)) { + return 1; + } + pos = end + 1; + } + return 0; +} + +static sfinx_status hdr_decode(const uint8_t *data, size_t len, sfinx_key *out) { + int seen_hash = 0; + int seen_seed = 0; + int seen_pub = 0; + + for (size_t pos = 0; pos < len;) { + const uint8_t *val; + size_t val_len; + size_t end = pos; + while (end < len && data[end] != '\n') end++; + + if (!seen_hash && hdr_match(data + pos, end - pos, "hash", &val, &val_len)) { + seen_hash = 1; + uint32_t bits = 0; + int ok = val_len > 0; + for (size_t i = 0; i < val_len && ok; i++) { + if (val[i] < '0' || val[i] > '9') { + ok = 0; + break; + } + bits = bits * 10 + (uint32_t)(val[i] - '0'); + } + if (ok && sfinx_hash_len((sfinx_hash)bits) != 0) { + out->hash = (sfinx_hash)bits; + } + } else if (!seen_seed && hdr_match(data + pos, end - pos, "seed", &val, &val_len)) { + seen_seed = 1; + if (val_len > 0 && val[0] != '(') { + out->seed = hdr_hex_decode(val, val_len, &out->seed_len); + if (!out->seed) return SFINX_ERR_FORMAT; + } + } else if (!seen_pub && hdr_match(data + pos, end - pos, "public-key", &val, &val_len)) { + seen_pub = 1; + if (val_len > 0 && val[0] != '(') { + out->pub = hdr_hex_decode(val, val_len, &out->pub_len); + if (!out->pub) return SFINX_ERR_FORMAT; + } + } + pos = end + 1; + } + + if (!seen_seed && !seen_pub) return SFINX_ERR_FORMAT; + if (!out->seed && !out->pub) return SFINX_ERR_FORMAT; + if (out->seed && !out->pub && sfinx_key_derive(out) != SFINX_OK) { + return SFINX_ERR_FORMAT; + } + return SFINX_OK; +} + +static size_t hdr_encode_len(const sfinx_key *key) { + size_t need = strlen("hash: 000\n"); + need += strlen("seed: \n") + (key->seed ? key->seed_len * 2 : strlen("(no seed)")); + need += strlen("public-key: \n") + (key->pub ? key->pub_len * 2 : strlen("(no public key)")); + return need; +} + +static sfinx_status hdr_encode(const sfinx_key *key, uint8_t *out, size_t cap, size_t *len_out) { + uint32_t bits = (uint32_t)key->hash; + size_t at = 0; + if (cap < hdr_encode_len(key)) { + return SFINX_ERR_NOSPACE; + } + + memcpy(out + at, "hash: ", 6); + at += 6; + out[at++] = (uint8_t)('0' + (bits / 100) % 10); + out[at++] = (uint8_t)('0' + (bits / 10) % 10); + out[at++] = (uint8_t)('0' + bits % 10); + out[at++] = '\n'; + + memcpy(out + at, "seed: ", 6); + at += 6; + if (key->seed) { + hdr_hex_encode(out + at, key->seed, key->seed_len); + at += key->seed_len * 2; + } else { + memcpy(out + at, "(no seed)", 9); + at += 9; + } + out[at++] = '\n'; + + memcpy(out + at, "public-key: ", 12); + at += 12; + if (key->pub) { + hdr_hex_encode(out + at, key->pub, key->pub_len); + at += key->pub_len * 2; + } else { + memcpy(out + at, "(no public key)", 15); + at += 15; + } + out[at++] = '\n'; + + if (len_out) { + *len_out = at; + } + return SFINX_OK; +} + +static sfinx_format hdr_format = { + .name = "hdr", + .detect = hdr_detect, + .decode = hdr_decode, + .encode_len = hdr_encode_len, + .encode = hdr_encode, +}; + +__attribute__((constructor)) static void hdr_register(void) { + sfinx_format_register(&hdr_format); +} +// }}} + +// vim:fdm=marker:fdl=0 diff --git a/src/fmt/raw.c b/src/fmt/raw.c @@ -0,0 +1,67 @@ +#include <stdlib.h> +#include <string.h> + +#include "../sfinx.h" + +// raw format {{{ +// +// The whole file is raw key material, no marker, so it is never auto-detected +// decode reads a seed, encode writes the seed if present else the public key + +static sfinx_status raw_decode(const uint8_t *data, size_t len, sfinx_key *out) { + uint8_t *seed; + if (len == 0) { + return SFINX_ERR_FORMAT; + } + seed = malloc(len); + if (!seed) { + return SFINX_ERR_NOSPACE; + } + memcpy(seed, data, len); + out->seed = seed; + out->seed_len = len; + return SFINX_OK; +} + +static size_t raw_encode_len(const sfinx_key *key) { + if (key->seed) return key->seed_len; + if (key->pub) return key->pub_len; + return 0; +} + +static sfinx_status raw_encode(const sfinx_key *key, uint8_t *out, size_t cap, size_t *len_out) { + const uint8_t *src; + size_t len; + if (key->seed) { + src = key->seed; + len = key->seed_len; + } else if (key->pub) { + src = key->pub; + len = key->pub_len; + } else { + return SFINX_ERR_ARGS; + } + if (cap < len) { + return SFINX_ERR_NOSPACE; + } + memcpy(out, src, len); + if (len_out) { + *len_out = len; + } + return SFINX_OK; +} + +static sfinx_format raw_format = { + .name = "raw", + .detect = NULL, + .decode = raw_decode, + .encode_len = raw_encode_len, + .encode = raw_encode, +}; + +__attribute__((constructor)) static void raw_register(void) { + sfinx_format_register(&raw_format); +} +// }}} + +// vim:fdm=marker:fdl=0 diff --git a/src/keypair.c b/src/keypair.c @@ -0,0 +1,139 @@ +#include <stdlib.h> +#include <string.h> + +#include "sfinx.h" + +// Keypair {{{ +// +// The secret is the seed, the public key is derived on demand +// init sets the default hash, free releases both halves + +void sfinx_key_init(sfinx_key *key) { + if (!key) return; + memset(key, 0, sizeof(*key)); + key->hash = SFINX_HASH_DEFAULT; +} + +void sfinx_key_free(sfinx_key *key) { + if (!key) return; + free(key->seed); + free(key->pub); + memset(key, 0, sizeof(*key)); +} + +sfinx_status sfinx_key_derive(sfinx_key *key) { + size_t len; + uint8_t *pub; + sfinx_status status; + + if (!key) { + return SFINX_ERR_ARGS; + } + if (key->pub) { + return SFINX_OK; + } + if (!key->seed) { + return SFINX_ERR_ARGS; + } + len = sfinx_hash_len(key->hash); + if (len == 0) { + return SFINX_ERR_ARGS; + } + pub = malloc(len); + if (!pub) { + return SFINX_ERR_NOSPACE; + } + status = sfinx_tree_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; +} +// }}} + +// Registry {{{ +// +// Formats prepend themselves on load, so the newest wins ties in search + +static sfinx_format *sfinx_formats = NULL; + +void sfinx_format_register(sfinx_format *format) { + if (!format) return; + format->next = sfinx_formats; + sfinx_formats = format; +} + +const sfinx_format *sfinx_format_find(const char *name) { + const sfinx_format *fmt; + if (!name) return NULL; + for (fmt = sfinx_formats; fmt; fmt = fmt->next) { + if (fmt->name && strcmp(fmt->name, name) == 0) { + return fmt; + } + } + return NULL; +} + +static const sfinx_format *sfinx_format_detect(const uint8_t *data, size_t len) { + const sfinx_format *fmt; + for (fmt = sfinx_formats; fmt; fmt = fmt->next) { + if (fmt->detect && fmt->detect(data, len)) { + return fmt; + } + } + return NULL; +} +// }}} + +// Dispatch {{{ +// +// Decode mallocs into the key, encode writes into the caller buffer + +sfinx_status sfinx_key_decode(const uint8_t *data, size_t len, const char *format, sfinx_key *out) { + const sfinx_format *fmt; + sfinx_status status; + + if (!data || !out) { + return SFINX_ERR_ARGS; + } + fmt = format ? sfinx_format_find(format) : sfinx_format_detect(data, len); + if (!fmt || !fmt->decode) { + return SFINX_ERR_FORMAT; + } + sfinx_key_init(out); + status = fmt->decode(data, len, out); + if (status != SFINX_OK) { + sfinx_key_free(out); + } + return status; +} + +size_t sfinx_key_encode_len(const sfinx_key *key, const char *format) { + const sfinx_format *fmt; + if (!key || !format) { + return 0; + } + fmt = sfinx_format_find(format); + if (!fmt || !fmt->encode_len) { + return 0; + } + return fmt->encode_len(key); +} + +sfinx_status sfinx_key_encode(const sfinx_key *key, const char *format, uint8_t *out, size_t cap, size_t *len_out) { + const sfinx_format *fmt; + if (!key || !format || !out) { + return SFINX_ERR_ARGS; + } + fmt = sfinx_format_find(format); + if (!fmt || !fmt->encode) { + return SFINX_ERR_ARGS; + } + return fmt->encode(key, out, cap, len_out); +} +// }}} + +// vim:fdm=marker:fdl=0 diff --git a/src/main.c b/src/main.c @@ -1,30 +1,31 @@ -#include <libgen.h> #include <stdio.h> -#include <stdlib.h> +#include <string.h> #include "cli/registry.h" +#include "cli/usage.h" +#include "sfinx.h" -int main(int argc, char *argv[]) { - const char *prog = basename(argv[0]); +int main(int argc, char **argv) { + struct cli_command *command; - cli_main_fn fn = cli_find(prog); - if (fn) return fn(argc, argv); - - // Not argv[0], see if it's given as subcommand - if (argc > 1 && (fn = cli_find(argv[1]))) { - char **shifted = malloc(sizeof(char *) * (size_t)argc); - if (!shifted) { - fprintf(stderr, "out of memory\n"); - return 1; - } - shifted[0] = argv[1]; - for (int i = 2; i < argc; i++) shifted[i - 1] = argv[i]; - int rc = fn(argc - 1, shifted); - free(shifted); - return rc; + 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")) { + fprintf(stdout, "%s\n", sfinx_version()); + return 0; } - fprintf(stderr, "%s: unknown command: %s\n", argv[0], prog); - fprintf(stderr, "available commands: %s\n", cli_registered()); - return 1; + command = cli_command_find(argv[1]); + if (!command) { + fprintf(stderr, "sfinx: unknown command: %s\n", argv[1]); + cli_print_global_usage(); + return 1; + } + return command->fn(argc - 1, (const char **)(argv + 1)); } diff --git a/src/sfinx.h b/src/sfinx.h @@ -80,6 +80,46 @@ sfinx_status sfinx_verify(const uint8_t *pubkey, size_t pubkey_len, const uint8_ sfinx_status sfinx_random_bytes(uint8_t *out, size_t len); // }}} +// Key {{{ +// +// Seed and public key pair, either half may be absent +// seed NULL means public-only, decode mallocs, sfinx_key_free releases + +typedef struct { + uint8_t *seed; + size_t seed_len; + uint8_t *pub; + size_t pub_len; + sfinx_hash hash; +} sfinx_key; + +void sfinx_key_init(sfinx_key *key); +void sfinx_key_free(sfinx_key *key); +sfinx_status sfinx_key_derive(sfinx_key *key); +// }}} + +// Formats {{{ +// +// Pluggable key encodings, registered by constructors and detected by content +// decode with a NULL name auto-detects, encode_len sizes before encode + +typedef struct sfinx_format { + struct sfinx_format *next; + const char *name; + int (*detect)(const uint8_t *data, size_t len); + sfinx_status (*decode)(const uint8_t *data, size_t len, sfinx_key *out); + size_t (*encode_len)(const sfinx_key *key); + sfinx_status (*encode)(const sfinx_key *key, uint8_t *out, size_t cap, size_t *len_out); +} sfinx_format; + +void sfinx_format_register(sfinx_format *format); +const sfinx_format *sfinx_format_find(const char *name); + +sfinx_status sfinx_key_decode(const uint8_t *data, size_t len, const char *format, sfinx_key *out); +size_t sfinx_key_encode_len(const sfinx_key *key, const char *format); +sfinx_status sfinx_key_encode(const sfinx_key *key, const char *format, uint8_t *out, size_t cap, size_t *len_out); +// }}} + #endif // __SFINX_H__ // vim:fdm=marker:fdl=0 diff --git a/src/util/file.c b/src/util/file.c @@ -0,0 +1,62 @@ +#include "file.h" + +#include <fcntl.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <unistd.h> + +int util_file_read(const char *path, uint8_t **out, size_t *len_out) { + struct stat st; + uint8_t *buf; + size_t len; + size_t got = 0; + int fd; + + if (!path || !out || !len_out) return -1; + fd = open(path, O_RDONLY); + if (fd < 0) return -1; + if (fstat(fd, &st) != 0) { + close(fd); + return -1; + } + len = (size_t)st.st_size; + buf = malloc(len + 1); + if (!buf) { + close(fd); + return -1; + } + while (got < len) { + ssize_t n = read(fd, buf + got, len - got); + if (n <= 0) { + free(buf); + close(fd); + return -1; + } + got += (size_t)n; + } + close(fd); + buf[len] = 0; + *out = buf; + *len_out = len; + return 0; +} + +int util_file_write(const char *path, const uint8_t *data, size_t len, unsigned mode, int force) { + size_t put = 0; + int fd; + int flags = O_WRONLY | O_CREAT | O_TRUNC; + + if (!path || (!data && len)) return -1; + if (!force) flags |= O_EXCL; + fd = open(path, flags, mode); + if (fd < 0) return -1; + while (put < len) { + ssize_t n = write(fd, data + put, len - put); + if (n <= 0) { + close(fd); + return -1; + } + put += (size_t)n; + } + return close(fd); +} diff --git a/src/util/file.h b/src/util/file.h @@ -0,0 +1,16 @@ +#ifndef __SFINX_UTIL_FILE_H__ +#define __SFINX_UTIL_FILE_H__ + +#include <stddef.h> +#include <stdint.h> + +// File {{{ +// +// Whole-file IO for the CLI +// read mallocs len + 1 and NUL terminates, write sets the mode and O_EXCL + +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); +// }}} + +#endif // __SFINX_UTIL_FILE_H__ diff --git a/test/07-key.c b/test/07-key.c @@ -0,0 +1,65 @@ +#include <string.h> + +#include "_tap.c" +#include "src/sfinx.c" + +int main(void) { + static const uint8_t seed[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}; + static const uint8_t badhdr[] = "public-key: zzzz\n"; + uint8_t hdr[4096]; + size_t hdr_len = 0; + size_t need = 0; + sfinx_key key; + sfinx_key other; + sfinx_key badkey; + sfinx_status status; + + tap_begin("sfinx key"); + + sfinx_key_init(&key); + tap(key.hash == SFINX_HASH_DEFAULT, "init sets the default hash", "got %d", (int)key.hash); + tap(key.seed == NULL && key.pub == NULL, "init zeroes both halves", "not zeroed"); + + status = sfinx_key_decode(seed, sizeof(seed), "raw", &key); + tap(status == SFINX_OK, "decode raw", "%s", sfinx_strerror(status)); + tap(key.seed_len == sizeof(seed), "raw seed length", "got %zu", key.seed_len); + tap(key.pub == NULL, "raw has no pub yet", "pub present"); + + status = sfinx_key_derive(&key); + tap(status == SFINX_OK, "derive", "%s", sfinx_strerror(status)); + tap(key.pub_len == 32, "derived pub length", "got %zu", key.pub_len); + + need = sfinx_key_encode_len(&key, "hdr"); + tap(need > 0, "hdr encode_len", "got %zu", need); + status = sfinx_key_encode(&key, "hdr", hdr, sizeof(hdr), &hdr_len); + tap(status == SFINX_OK, "encode hdr", "%s", sfinx_strerror(status)); + tap(hdr_len == need, "hdr length matches encode_len", "got %zu want %zu", hdr_len, need); + + status = sfinx_key_decode(hdr, hdr_len, NULL, &other); + tap(status == SFINX_OK, "decode hdr auto", "%s", sfinx_strerror(status)); + tap(other.pub_len == key.pub_len && memcmp(other.pub, key.pub, key.pub_len) == 0, "hdr pub matches", "mismatch"); + tap(other.seed_len == key.seed_len && memcmp(other.seed, key.seed, key.seed_len) == 0, "hdr seed matches", + "mismatch"); + + free(other.seed); + other.seed = NULL; + other.seed_len = 0; + need = sfinx_key_encode_len(&other, "raw"); + tap(need == other.pub_len, "raw pub encode_len", "got %zu", need); + + status = sfinx_key_encode(&key, "hdr", hdr, 1, &hdr_len); + tap(status == SFINX_ERR_NOSPACE, "hdr encode rejects a small cap", "got %s", sfinx_strerror(status)); + + sfinx_key_init(&badkey); + status = sfinx_key_decode(seed, sizeof(seed), "nope", &badkey); + tap(status == SFINX_ERR_FORMAT, "decode rejects an unknown format", "got %s", sfinx_strerror(status)); + status = sfinx_key_decode(badhdr, sizeof(badhdr) - 1, NULL, &badkey); + tap(status == SFINX_ERR_FORMAT, "decode rejects bad hex", "got %s", sfinx_strerror(status)); + + sfinx_key_free(&key); + sfinx_key_free(&other); + sfinx_key_free(&badkey); + return tap_plan(); +} diff --git a/test/Makefile b/test/Makefile @@ -1,6 +1,6 @@ CC ?= cc CFLAGS ?= -Wall -Wextra -O2 -CFLAGS += -I.. -I../lib/.dep/include +CFLAGS += -I.. -I../src -I../lib/.dep/include OUT := ../build/test SRCS := $(filter-out _tap.c,$(wildcard *.c)) @@ -9,6 +9,7 @@ BINS := $(addprefix $(OUT)/,$(TESTS)) SH := $(filter-out _tap.sh,$(wildcard *.sh)) KECCAK := ../lib/coruus/keccak-tiny/keccak-tiny.c +LIB := ../src/keypair.c $(wildcard ../src/fmt/*.c) .PHONY: all all: $(BINS) @@ -19,8 +20,8 @@ $(OUT): $(KECCAK): @cd .. && dep install -$(OUT)/%: %.c ../src/sfinx.c _tap.c $(KECCAK) | $(OUT) - @$(CC) $(CFLAGS) $< $(KECCAK) -o $@ +$(OUT)/%: %.c ../src/sfinx.c _tap.c $(LIB) $(KECCAK) | $(OUT) + @$(CC) $(CFLAGS) $< $(LIB) $(KECCAK) -o $@ .PHONY: run run: all diff --git a/test/_tap.sh b/test/_tap.sh @@ -1,4 +1,4 @@ -# test/_tap.sh - TAP helpers for the shell tests, source with `. test/_tap.sh` +# TAP helpers, source with `. test/_tap.sh` # Mirrors test/_tap.c: tap_begin, tap, tap_check, tap_plan. TAP_NO=0 @@ -43,3 +43,14 @@ tap_plan() { fi return 0 } + +# Path to the built sfinx binary, building it once on demand +ensure_bin() { + _root=$1 + _target=$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m) + _bin="${_root}/build/${_target}/sfinx" + if [ ! -x "${_bin}" ]; then + (cd "${_root}" && make) >/dev/null 2>&1 || true + fi + printf '%s\n' "${_bin}" +} diff --git a/test/test_embed.sh b/test/test_embed.sh @@ -1,10 +1,7 @@ #!/bin/sh -# test/test_embed.sh - prove sfinx stands alone as a library. -# -# Compiles a program that includes only sfinx.h and links only sfinx.c plus -# keccak-tiny, then runs an N=2 tree round trip with tamper checks. No CLI, no -# util, no argparse, no rxi/log path is reachable, so a leaked dependency -# breaks the build instead of passing silently. +# 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. set -eu HERE=$(cd "$(dirname "$0")" && pwd) ROOT=$(cd "${HERE}/.." && pwd) @@ -29,14 +26,6 @@ else tap 0 "sfinx.h includes only stddef and stdint" "${bad}" fi -bad=$(grep -E '^[[:space:]]*#include' "${ROOT}/src/sfinx.c" | - grep -E 'cli/|util/|argparse|log\.h' || true) -if [ -z "${bad}" ]; then - tap 1 "sfinx.c has no util includes" "" -else - tap 0 "sfinx.c has no util includes" "${bad}" -fi - if [ ! -f "${KECCAK}" ]; then (cd "${ROOT}" && dep install) >/dev/null 2>&1 || true fi @@ -49,6 +38,37 @@ fi TMP=$(mktemp -d) trap 'rm -rf "${TMP}"' EXIT +# export.mk, templated the way dep does it, expanded the way make does it +sed "s|{{module.dirname}}|${ROOT}|g" "${ROOT}/export.mk" >"${TMP}/export.mk" +printf 'include %s\nprobe:\n\t@echo $(SRC)\n' "${TMP}/export.mk" >"${TMP}/probe.mk" +LIB_SRCS=$(make -s -f "${TMP}/probe.mk" probe 2>/dev/null || true) +if [ -n "${LIB_SRCS}" ]; then + tap 1 "export.mk yields library sources" "" +else + tap 0 "export.mk yields library sources" "probe produced nothing" +fi + +missing="" +for f in ${LIB_SRCS}; do + [ -f "$f" ] || missing="${missing} ${f}" +done +if [ -z "${missing}" ]; then + tap 1 "library sources exist" "" +else + tap 0 "library sources exist" "missing:${missing}" +fi + +bad="" +for f in ${LIB_SRCS}; do + hit=$(grep -E '^[[:space:]]*#include' "$f" | grep -E 'cli/|util/|argparse|log\.h' || true) + [ -z "${hit}" ] || bad="${bad}${f} ${hit}; " +done +if [ -z "${bad}" ]; then + tap 1 "library sources have no util includes" "" +else + tap 0 "library sources have no util includes" "${bad}" +fi + cat >"${TMP}/embed.c" <<'EOF' #include "sfinx.h" @@ -69,9 +89,14 @@ int main(void) { uint8_t pub[SFINX_HASH_LEN_MAX]; uint8_t sig[4096]; uint8_t tamper[4096]; + uint8_t hdr[4096]; size_t pub_len = 0; size_t sig_len = 0; + size_t hdr_len = 0; size_t msg_len = sizeof(msg) - 1; + sfinx_key key; + sfinx_key reread; + sfinx_status status; if (sfinx_tree_public_key(SFINX_HASH_256, seed, sizeof(seed), pub, sizeof(pub), &pub_len) != SFINX_OK) { return fail("tree_public_key"); @@ -90,22 +115,36 @@ int main(void) { return fail("tampered path accepted"); } - memcpy(tamper, sig, sig_len); - tamper[sig_len - 1] ^= 0x01; - if (sfinx_verify(pub, pub_len, msg, msg_len, tamper, sig_len) == SFINX_OK) { - return fail("tampered proof accepted"); + status = sfinx_key_decode(seed, sizeof(seed), "raw", &key); + if (status != SFINX_OK) { + return fail("decode raw"); } - - pub[0] ^= 0x01; - if (sfinx_verify(pub, pub_len, msg, msg_len, sig, sig_len) == SFINX_OK) { - return fail("tampered pubkey accepted"); + if (sfinx_key_derive(&key) != SFINX_OK) { + return fail("derive"); } - + if (key.pub_len != pub_len || memcmp(key.pub, pub, pub_len) != 0) { + return fail("derived pub mismatch"); + } + if (sfinx_key_encode_len(&key, "hdr") == 0) { + return fail("encode_len hdr"); + } + if (sfinx_key_encode(&key, "hdr", hdr, sizeof(hdr), &hdr_len) != SFINX_OK) { + return fail("encode hdr"); + } + status = sfinx_key_decode(hdr, hdr_len, NULL, &reread); + if (status != SFINX_OK) { + return fail("decode hdr auto"); + } + if (reread.pub_len != pub_len || memcmp(reread.pub, pub, pub_len) != 0) { + return fail("hdr pub mismatch"); + } + sfinx_key_free(&key); + sfinx_key_free(&reread); return 0; } EOF -if "${CC}" -Wall -Wextra -O2 -I"${ROOT}/src" -I"${ROOT}/lib/.dep/include" "${TMP}/embed.c" "${ROOT}/src/sfinx.c" \ +if "${CC}" -Wall -Wextra -O2 -I"${ROOT}/src" -I"${ROOT}/lib/.dep/include" "${TMP}/embed.c" ${LIB_SRCS} \ "${KECCAK}" -o "${TMP}/embed" >"${TMP}/build.log" 2>&1; then tap 1 "embed program builds against the library only" "" else @@ -119,9 +158,9 @@ else rc=127 fi if [ "${rc}" = 0 ]; then - tap 1 "embed N=2 round trip and tamper checks" "" + tap 1 "embed crypto and format round trip" "" else - tap 0 "embed N=2 round trip and tamper checks" "exit ${rc}: $(cat "${TMP}/run.log" 2>/dev/null || true)" + tap 0 "embed crypto and format round trip" "exit ${rc}: $(cat "${TMP}/run.log" 2>/dev/null || true)" fi tap_plan diff --git a/test/test_printkey.sh b/test/test_printkey.sh @@ -0,0 +1,48 @@ +#!/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 + +tap_begin "cli: printkey" + +"${BIN}" seed -o "${TMP}/seed.bin" + +"${BIN}" printkey --in raw -k "${TMP}/seed.bin" >"${TMP}/key.hdr" +grep -q '^public-key: ' "${TMP}/key.hdr" && ok=1 || ok=0 +tap "${ok}" "raw seed converts to hdr" "$(cat "${TMP}/key.hdr")" + +"${BIN}" printkey --in raw -k "${TMP}/seed.bin" --public-only >"${TMP}/a.hdr" +"${BIN}" printkey --in raw -k "${TMP}/seed.bin" --public-only >"${TMP}/b.hdr" +cmp -s "${TMP}/a.hdr" "${TMP}/b.hdr" && ok=1 || ok=0 +tap "${ok}" "same seed gives the same key" "differs" + +"${BIN}" printkey -k "${TMP}/key.hdr" --public-only -f raw >"${TMP}/pub.bin" +n=$(wc -c <"${TMP}/pub.bin" | tr -d '[:space:]') +tap "$([ "${n}" = 32 ] && echo 1 || echo 0)" "hdr auto-detect gives a 32-byte pub" "got ${n}" + +"${BIN}" printkey --in raw -k "${TMP}/seed.bin" -L 512 --public-only -f raw >"${TMP}/pub512.bin" +n=$(wc -c <"${TMP}/pub512.bin" | tr -d '[:space:]') +tap "$([ "${n}" = 64 ] && echo 1 || echo 0)" "--hash 512 gives a 64-byte pub" "got ${n}" + +"${BIN}" printkey --in raw -k "${TMP}/seed.bin" -o "${TMP}/full.hdr" +mode=$(stat -c '%a' "${TMP}/full.hdr" 2>/dev/null || stat -f '%Lp' "${TMP}/full.hdr") +tap "$([ "${mode}" = 600 ] && echo 1 || echo 0)" "full key file is 0600" "got ${mode}" + +"${BIN}" printkey --in raw -k "${TMP}/seed.bin" --public-only -o "${TMP}/pub.hdr" +mode=$(stat -c '%a' "${TMP}/pub.hdr" 2>/dev/null || stat -f '%Lp' "${TMP}/pub.hdr") +tap "$([ "${mode}" = 644 ] && echo 1 || echo 0)" "public-only file is 0644" "got ${mode}" + +rc=0 +"${BIN}" printkey -k "${TMP}/key.hdr" -f bogus >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects an unknown format" "exit ${rc}" + +rc=0 +"${BIN}" printkey >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "requires a key file" "exit ${rc}" + +tap_plan diff --git a/test/test_seed.sh b/test/test_seed.sh @@ -0,0 +1,44 @@ +#!/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 + +tap_begin "cli: seed" + +if [ ! -x "${BIN}" ]; then + tap 0 "sfinx binary present" "missing ${BIN}" + tap_plan + exit 1 +fi +tap 1 "sfinx binary present" "" + +"${BIN}" seed >"${TMP}/default.bin" +n=$(wc -c <"${TMP}/default.bin" | tr -d '[:space:]') +tap "$([ "${n}" = 32 ] && echo 1 || echo 0)" "default is 32 bytes" "got ${n}" + +"${BIN}" seed 16 >"${TMP}/sixteen.bin" +n=$(wc -c <"${TMP}/sixteen.bin" | tr -d '[:space:]') +tap "$([ "${n}" = 16 ] && echo 1 || echo 0)" "explicit length is honored" "got ${n}" + +"${BIN}" seed 32 >"${TMP}/a.bin" +"${BIN}" seed 32 >"${TMP}/b.bin" +if cmp -s "${TMP}/a.bin" "${TMP}/b.bin"; then + tap 0 "two seeds differ" "identical" +else + tap 1 "two seeds differ" "" +fi + +"${BIN}" seed -o "${TMP}/out.seed" +mode=$(stat -c '%a' "${TMP}/out.seed" 2>/dev/null || stat -f '%Lp' "${TMP}/out.seed") +tap "$([ "${mode}" = 600 ] && echo 1 || echo 0)" "-o writes mode 0600" "got ${mode}" + +rc=0 +"${BIN}" seed 0 >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects length 0" "exit ${rc}" + +tap_plan diff --git a/test/test_subcommands.sh b/test/test_subcommands.sh @@ -0,0 +1,35 @@ +#!/bin/sh +set -eu +HERE=$(cd "$(dirname "$0")" && pwd) +ROOT=$(cd "${HERE}/.." && pwd) +. "${HERE}/_tap.sh" + +BIN=$(ensure_bin "${ROOT}") + +tap_begin "cli: subcommands" + +out=$("${BIN}" --help 2>&1 || true) +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 *seed*) ok=1 ;; *) ok=0 ;; esac +tap "${ok}" "help lists seed" "${out}" +case "${out}" in *printkey*) ok=1 ;; *) ok=0 ;; esac +tap "${ok}" "help lists printkey" "${out}" + +out=$("${BIN}" --version 2>&1 || true) +tap "$([ -n "${out}" ] && echo 1 || echo 0)" "--version prints" "empty" + +out=$("${BIN}" version 2>&1 || true) +tap "$([ -n "${out}" ] && echo 1 || echo 0)" "version command prints" "empty" + +rc=0 +"${BIN}" nope >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "unknown command fails" "exit ${rc}" + +rc=0 +"${BIN}" >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "no args fails" "exit ${rc}" + +tap_plan