sfinx

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

commit 379773ad7e8302c7d5d59aee50632845e7e2cb5d
parent 404586d47b2386c60ecd86ecadc2688fbaa0e5fd
Author: finwo <finwo@pm.me>
Date:   Fri,  9 Oct 2026 22:22:28 +0200

Basic cli functionality

Diffstat:
MREADME.md | 6++++--
Asrc/cli/common.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/cli/common.h | 15+++++++++++++++
Msrc/cli/printkey.c | 8++++----
Asrc/cli/sign.c | 185+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/cli/verify.c | 163+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/fmt/hdr.c | 173+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
Asrc/fmt/hex.c | 198+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/fmt/raw.c | 103++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Msrc/keypair.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Msrc/sfinx.h | 23++++++++++++++++++-----
Msrc/util/file.c | 94+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Msrc/util/file.h | 2+-
Mtest/07-key.c | 21+++++++++++++++++++++
Atest/08-sig.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/test_embed.sh | 25+++++++++++++++++++++----
Mtest/test_printkey.sh | 25+++++++++++++++++--------
Atest/test_sign_verify.sh | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/test_subcommands.sh | 4++++
19 files changed, 1139 insertions(+), 145 deletions(-)

diff --git a/README.md b/README.md @@ -193,10 +193,12 @@ 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 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 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. - `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. +A key file carries a seed, a public key, or both. `hdr` is the text form (`hash:`, `seed:`, `public-key:`, `signature:`), and `raw`, `hex`, `rawpub`, and `hexpub` are the bare forms. The encode and decode code lives in the library, so any program using sfinx reads and writes the same files. ## Pseudocode diff --git a/src/cli/common.c b/src/cli/common.c @@ -0,0 +1,50 @@ +#include "common.h" + +#include <stdlib.h> +#include <string.h> + +#include "util/file.h" + +int cli_read_message(const char *message, const char *file, uint8_t **out, size_t *len_out) { + uint8_t *buf; + size_t len; + if (message) { + len = strlen(message); + buf = malloc(len + 1); + if (!buf) return -1; + memcpy(buf, message, len + 1); + *out = buf; + *len_out = len; + return 0; + } + return util_file_read(file, out, len_out); +} + +static int hexval(int 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; +} + +int cli_hex_decode(const char *hex, uint8_t **out, size_t *len_out) { + size_t len = strlen(hex); + size_t n; + uint8_t *buf; + if (len % 2 != 0) return -1; + n = len / 2; + buf = malloc(n + 1); + if (!buf) return -1; + for (size_t i = 0; i < n; i++) { + int hi = hexval(hex[i * 2]); + int lo = hexval(hex[i * 2 + 1]); + if (hi < 0 || lo < 0) { + free(buf); + return -1; + } + buf[i] = (uint8_t)((hi << 4) | lo); + } + *out = buf; + *len_out = n; + return 0; +} diff --git a/src/cli/common.h b/src/cli/common.h @@ -0,0 +1,15 @@ +#ifndef __SFINX_CLI_COMMON_H__ +#define __SFINX_CLI_COMMON_H__ + +#include <stddef.h> +#include <stdint.h> + +// Common {{{ +// +// Message reading and hex helpers shared by the commands + +int cli_read_message(const char *message, const char *file, uint8_t **out, size_t *len_out); +int cli_hex_decode(const char *hex, uint8_t **out, size_t *len_out); +// }}} + +#endif // __SFINX_CLI_COMMON_H__ diff --git a/src/cli/printkey.c b/src/cli/printkey.c @@ -23,14 +23,14 @@ static int cmd_printkey(int argc, const char **argv) { int rc = 0; static const char *const usages[] = { - "sfinx printkey -k file [-L hash] [-f format] [--in format] [-o file] [--public-only]", + "sfinx printkey -k file [-L hash] [--in-fmt fmt] [--out-fmt fmt] [-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_STRING(0, "out-fmt", &format, "output format (default hdr)", NULL, 0, 0), + OPT_STRING(0, "in-fmt", &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), @@ -119,7 +119,7 @@ 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]", + .help = "sfinx printkey -k file [-L hash] [--in-fmt fmt] [--out-fmt fmt] [-o file] [--public-only]", .fn = cmd_printkey, }; diff --git a/src/cli/sign.c b/src/cli/sign.c @@ -0,0 +1,185 @@ +#include <cofyc/argparse.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" + +static int cmd_sign(int argc, const char **argv) { + const char *key_file = NULL; + const char *key_format = NULL; + const char *out_file = NULL; + const char *format = NULL; + const char *path_hex = NULL; + const char *message = NULL; + const char *msg_file = NULL; + int hash = 0; + int path_len = SFINX_PATH_LEN_DEFAULT; + int force = 0; + uint8_t *key_data = NULL; + uint8_t *msg_data = NULL; + uint8_t *path = NULL; + uint8_t *sig = NULL; + uint8_t *encoded = NULL; + size_t key_len = 0; + size_t msg_len = 0; + size_t path_bytes = 0; + size_t sig_len = 0; + size_t enc_len = 0; + sfinx_key key; + sfinx_status status; + int rc = 1; + + static const char *const usages[] = { + "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]", + NULL, + }; + struct argparse_option options[] = { + OPT_HELP(), + OPT_STRING('k', "key-file", &key_file, "secret key file", NULL, 0, 0), + OPT_STRING(0, "key-fmt", &key_format, "key format, auto by default", NULL, 0, 0), + OPT_INTEGER('L', "hash", &hash, "hash size 224/256/384/512", NULL, 0, 0), + OPT_INTEGER('p', "path-len", &path_len, "random path length", NULL, 0, 0), + OPT_STRING('P', "path", &path_hex, "explicit path as hex", NULL, 0, 0), + OPT_STRING('m', "message", &message, "message string", NULL, 0, 0), + OPT_STRING('M', "message-file", &msg_file, "message file, - for stdin", NULL, 0, 0), + OPT_STRING(0, "sig-fmt", &format, "signature format, hex on stdout and raw to a file by default", NULL, 0, 0), + OPT_STRING('o', "out", &out_file, "signature file, - for stdout", NULL, 0, 0), + OPT_BOOLEAN('F', "force", &force, "overwrite an existing output file", NULL, 0, 0), + OPT_END(), + }; + struct argparse argparse; + argparse_init(&argparse, options, usages, 0); + argparse_describe(&argparse, "\nSign a message", NULL); + argc = argparse_parse(&argparse, argc, argv); + + sfinx_key_init(&key); + + if (argc > 0) { + fprintf(stderr, "sfinx: sign: unexpected argument: %s\n", argv[0]); + return 1; + } + if (!key_file) { + fprintf(stderr, "sfinx: sign: missing --key-file\n"); + return 1; + } + if (message && msg_file) { + fprintf(stderr, "sfinx: sign: --message and --message-file are exclusive\n"); + return 1; + } + + if (util_file_read(key_file, &key_data, &key_len) != 0) { + fprintf(stderr, "sfinx: sign: cannot read %s\n", key_file); + goto done; + } + status = sfinx_key_decode(key_data, key_len, key_format, &key); + if (status != SFINX_OK && !key_format) { + status = sfinx_key_decode(key_data, key_len, "raw", &key); + } + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: sign: cannot decode %s: %s\n", key_file, sfinx_strerror(status)); + goto done; + } + if (!key.seed) { + fprintf(stderr, "sfinx: sign: key has no seed\n"); + goto done; + } + if (hash != 0) { + key.hash = (sfinx_hash)hash; + } + if (sfinx_hash_len(key.hash) == 0) { + fprintf(stderr, "sfinx: sign: invalid hash size: %d\n", (int)key.hash); + goto done; + } + + if (cli_read_message(message, msg_file, &msg_data, &msg_len) != 0) { + fprintf(stderr, "sfinx: sign: cannot read the message\n"); + goto done; + } + + if (path_hex) { + if (cli_hex_decode(path_hex, &path, &path_bytes) != 0 || !sfinx_path_len_valid(path_bytes)) { + fprintf(stderr, "sfinx: sign: invalid path\n"); + goto done; + } + } else { + if (path_len <= 0 || !sfinx_path_len_valid((size_t)path_len)) { + fprintf(stderr, "sfinx: sign: invalid path length: %d\n", path_len); + goto done; + } + path_bytes = (size_t)path_len; + path = malloc(path_bytes); + if (!path) { + fprintf(stderr, "sfinx: sign: out of memory\n"); + goto done; + } + if (sfinx_random_bytes(path, path_bytes) != SFINX_OK) { + fprintf(stderr, "sfinx: sign: %s\n", sfinx_strerror(SFINX_ERR_RANDOM)); + goto done; + } + } + + sig_len = sfinx_signature_len(key.hash, path_bytes); + sig = malloc(sig_len); + if (!sig) { + fprintf(stderr, "sfinx: sign: out of memory\n"); + goto done; + } + status = sfinx_tree_sign(key.hash, key.seed, key.seed_len, path, path_bytes, msg_data, msg_len, sig, sig_len, NULL); + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: sign: %s\n", sfinx_strerror(status)); + goto done; + } + + if (!format) { + format = (out_file && strcmp(out_file, "-") != 0) ? "raw" : "hex"; + } + enc_len = sfinx_sig_encode_len(sig, sig_len, format); + if (enc_len == 0) { + fprintf(stderr, "sfinx: sign: unknown signature format: %s\n", format); + goto done; + } + encoded = malloc(enc_len); + if (!encoded) { + fprintf(stderr, "sfinx: sign: out of memory\n"); + goto done; + } + status = sfinx_sig_encode(sig, sig_len, format, encoded, enc_len, &enc_len); + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: sign: cannot encode: %s\n", sfinx_strerror(status)); + goto done; + } + if (util_file_write(out_file, encoded, enc_len, 0644, force) != 0) { + fprintf(stderr, "sfinx: sign: cannot write %s\n", out_file ? out_file : "-"); + goto done; + } + rc = 0; + +done: + free(key_data); + free(msg_data); + free(path); + free(sig); + free(encoded); + sfinx_key_free(&key); + return rc; +} + +static struct cli_command sign_command = { + .names = (const char *const[]){"sign", NULL}, + .display = "sign", + .description = "Sign a message", + .help = + "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]", + .fn = cmd_sign, +}; + +__attribute__((constructor)) static void sign_register(void) { + cli_command_register(&sign_command); +} diff --git a/src/cli/verify.c b/src/cli/verify.c @@ -0,0 +1,163 @@ +#include <cofyc/argparse.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" + +static int cmd_verify(int argc, const char **argv) { + const char *key_file = NULL; + const char *key_format = NULL; + const char *format = NULL; + const char *signature = NULL; + const char *sig_file = NULL; + const char *message = NULL; + const char *msg_file = NULL; + int hash = 0; + uint8_t *key_data = NULL; + uint8_t *msg_data = NULL; + uint8_t *sig_data = NULL; + uint8_t *raw_sig = NULL; + size_t key_len = 0; + size_t msg_len = 0; + size_t sig_data_len = 0; + size_t raw_len = 0; + sfinx_key key; + sfinx_status status; + int rc = 1; + + static const char *const usages[] = { + "sfinx verify -k keyfile [-L bits] [-s signature | -S signature-file] [-m message | -M message-file] " + "[--key-fmt fmt] [--sig-fmt fmt]", + NULL, + }; + struct argparse_option options[] = { + OPT_HELP(), + OPT_STRING('k', "key-file", &key_file, "public key file", NULL, 0, 0), + OPT_STRING(0, "key-fmt", &key_format, "key format, auto by default", NULL, 0, 0), + OPT_STRING(0, "sig-fmt", &format, "signature format, auto by default", NULL, 0, 0), + OPT_INTEGER('L', "hash", &hash, "expected hash size", NULL, 0, 0), + OPT_STRING('s', "signature", &signature, "signature as hex", NULL, 0, 0), + OPT_STRING('S', "signature-file", &sig_file, "signature file, - for stdin", NULL, 0, 0), + OPT_STRING('m', "message", &message, "message string", NULL, 0, 0), + OPT_STRING('M', "message-file", &msg_file, "message file, - for stdin", NULL, 0, 0), + OPT_END(), + }; + struct argparse argparse; + argparse_init(&argparse, options, usages, 0); + argparse_describe(&argparse, "\nVerify a message signature", NULL); + argc = argparse_parse(&argparse, argc, argv); + + sfinx_key_init(&key); + + if (argc > 0) { + fprintf(stderr, "sfinx: verify: unexpected argument: %s\n", argv[0]); + return 1; + } + if (!key_file) { + fprintf(stderr, "sfinx: verify: missing --key-file\n"); + return 1; + } + if (signature && sig_file) { + fprintf(stderr, "sfinx: verify: --signature and --signature-file are exclusive\n"); + return 1; + } + if (message && msg_file) { + fprintf(stderr, "sfinx: verify: --message and --message-file are exclusive\n"); + return 1; + } + + int msg_stdin = !message && (!msg_file || strcmp(msg_file, "-") == 0); + int sig_stdin = !signature && (!sig_file || strcmp(sig_file, "-") == 0); + if (msg_stdin && sig_stdin) { + fprintf(stderr, "sfinx: verify: message and signature cannot both come from stdin\n"); + return 1; + } + + if (util_file_read(key_file, &key_data, &key_len) != 0) { + fprintf(stderr, "sfinx: verify: cannot read %s\n", key_file); + goto done; + } + status = sfinx_key_decode(key_data, key_len, key_format, &key); + if (status != SFINX_OK && !key_format) { + status = sfinx_key_decode(key_data, key_len, "rawpub", &key); + } + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: verify: cannot decode %s: %s\n", key_file, sfinx_strerror(status)); + goto done; + } + if (sfinx_key_derive(&key) != SFINX_OK || !key.pub) { + fprintf(stderr, "sfinx: verify: key has no public key\n"); + goto done; + } + + if (cli_read_message(message, msg_file, &msg_data, &msg_len) != 0) { + fprintf(stderr, "sfinx: verify: cannot read the message\n"); + goto done; + } + + if (signature) { + if (cli_hex_decode(signature, &raw_sig, &raw_len) != 0) { + fprintf(stderr, "sfinx: verify: invalid signature hex\n"); + goto done; + } + } else { + if (util_file_read(sig_file, &sig_data, &sig_data_len) != 0) { + fprintf(stderr, "sfinx: verify: cannot read the signature\n"); + goto done; + } + raw_sig = malloc(sig_data_len + 1); + if (!raw_sig) { + fprintf(stderr, "sfinx: verify: out of memory\n"); + goto done; + } + status = sfinx_sig_decode(sig_data, sig_data_len, format, raw_sig, sig_data_len, &raw_len); + if (status != SFINX_OK) { + fprintf(stderr, "sfinx: verify: cannot decode the signature: %s\n", sfinx_strerror(status)); + goto done; + } + } + + if (hash != 0) { + sfinx_hash sig_hash; + size_t sig_path; + if (sfinx_signature_info(raw_sig, raw_len, &sig_hash, &sig_path) != SFINX_OK || (int)sig_hash != hash) { + fprintf(stderr, "sfinx: verify: signature is not %d-bit\n", hash); + goto done; + } + } + + if (sfinx_verify(key.pub, key.pub_len, msg_data, msg_len, raw_sig, raw_len) == SFINX_OK) { + fprintf(stdout, "OK\n"); + rc = 0; + } else { + fprintf(stdout, "FAIL\n"); + rc = 1; + } + +done: + free(key_data); + free(msg_data); + free(sig_data); + free(raw_sig); + sfinx_key_free(&key); + return rc; +} + +static struct cli_command verify_command = { + .names = (const char *const[]){"verify", NULL}, + .display = "verify", + .description = "Verify a message signature", + .help = + "sfinx verify -k keyfile [-L bits] [-s signature | -S signature-file] [-m message | -M message-file] " + "[--key-fmt fmt] [--sig-fmt fmt]", + .fn = cmd_verify, +}; + +__attribute__((constructor)) static void verify_register(void) { + cli_command_register(&verify_command); +} diff --git a/src/fmt/hdr.c b/src/fmt/hdr.c @@ -5,10 +5,11 @@ // hdr format {{{ // -// Text key file, self-describing by label, unknown labels are ignored +// Text key or signature file, self-describing by label, unknown labels ignored // hash: <bits> // seed: <hex | (no seed)> // public-key: <hex> +// signature: <hex> // labels are case-insensitive, first valid occurrence wins, a lone seed derives its public half static int hdr_hexval(uint8_t c) { @@ -18,26 +19,27 @@ static int hdr_hexval(uint8_t c) { return -1; } -static uint8_t *hdr_hex_decode(const uint8_t *in, size_t in_len, size_t *out_len) { - uint8_t *out; +static sfinx_status hdr_hex_decode(const uint8_t *in, size_t in_len, uint8_t *out, size_t cap, size_t *out_len) { + size_t n; if (in_len % 2 != 0) { - return NULL; + return SFINX_ERR_FORMAT; } - out = malloc(in_len / 2 + 1); - if (!out) { - return NULL; + n = in_len / 2; + if (cap < n) { + return SFINX_ERR_NOSPACE; } - for (size_t i = 0; i < in_len / 2; i++) { + for (size_t i = 0; i < n; 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; + return SFINX_ERR_FORMAT; } out[i] = (uint8_t)((hi << 4) | lo); } - *out_len = in_len / 2; - return out; + if (out_len) { + *out_len = n; + } + return SFINX_OK; } static void hdr_hex_encode(uint8_t *out, const uint8_t *in, size_t in_len) { @@ -70,14 +72,11 @@ static int hdr_match(const uint8_t *line, size_t len, const char *label, const u return 1; } -static int hdr_detect(const uint8_t *data, size_t len) { - const uint8_t *val; - size_t val_len; +static int hdr_scan(const uint8_t *data, size_t len, const char *label, 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)) { + if (hdr_match(data + pos, end - pos, label, val, val_len)) { return 1; } pos = end + 1; @@ -85,48 +84,62 @@ static int hdr_detect(const uint8_t *data, size_t len) { 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++; +static int hdr_key_detect(const uint8_t *data, size_t len) { + const uint8_t *val; + size_t val_len; + return hdr_scan(data, len, "seed", &val, &val_len) || hdr_scan(data, len, "public-key", &val, &val_len); +} - 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; +static sfinx_status hdr_key_decode(const uint8_t *data, size_t len, sfinx_key *out) { + const uint8_t *val; + size_t val_len; + int have_seed = 0; + int have_pub = 0; + sfinx_status status; + + if (hdr_scan(data, len, "hash", &val, &val_len)) { + 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; } - } 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; + bits = bits * 10 + (uint32_t)(val[i] - '0'); + } + if (ok && sfinx_hash_len((sfinx_hash)bits) != 0) { + out->hash = (sfinx_hash)bits; + } + } + if (hdr_scan(data, len, "seed", &val, &val_len)) { + have_seed = 1; + if (val_len > 0 && val[0] != '(') { + if (val_len / 2 > SFINX_SEED_LEN_MAX) return SFINX_ERR_FORMAT; + out->seed = malloc(val_len / 2 + 1); + if (!out->seed) return SFINX_ERR_NOSPACE; + status = hdr_hex_decode(val, val_len, out->seed, val_len / 2, &out->seed_len); + if (status != SFINX_OK) { + free(out->seed); + out->seed = NULL; + return status; } - } 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; + } + } + if (hdr_scan(data, len, "public-key", &val, &val_len)) { + have_pub = 1; + if (val_len > 0 && val[0] != '(') { + out->pub = malloc(val_len / 2 + 1); + if (!out->pub) return SFINX_ERR_NOSPACE; + status = hdr_hex_decode(val, val_len, out->pub, val_len / 2, &out->pub_len); + if (status != SFINX_OK) { + free(out->pub); + out->pub = NULL; + return status; } } - pos = end + 1; } - if (!seen_seed && !seen_pub) return SFINX_ERR_FORMAT; + if (!have_seed && !have_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; @@ -134,17 +147,17 @@ static sfinx_status hdr_decode(const uint8_t *data, size_t len, sfinx_key *out) return SFINX_OK; } -static size_t hdr_encode_len(const sfinx_key *key) { +static size_t hdr_key_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) { +static sfinx_status hdr_key_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)) { + if (cap < hdr_key_encode_len(key)) { return SFINX_ERR_NOSPACE; } @@ -183,12 +196,52 @@ static sfinx_status hdr_encode(const sfinx_key *key, uint8_t *out, size_t cap, s return SFINX_OK; } +static int hdr_sig_detect(const uint8_t *data, size_t len) { + const uint8_t *val; + size_t val_len; + return hdr_scan(data, len, "signature", &val, &val_len); +} + +static sfinx_status hdr_sig_decode(const uint8_t *data, size_t len, uint8_t *out, size_t cap, size_t *len_out) { + const uint8_t *val; + size_t val_len; + if (!hdr_scan(data, len, "signature", &val, &val_len)) { + return SFINX_ERR_FORMAT; + } + return hdr_hex_decode(val, val_len, out, cap, len_out); +} + +static size_t hdr_sig_encode_len(const uint8_t *sig, size_t sig_len) { + (void)sig; + return strlen("signature: \n") + sig_len * 2; +} + +static sfinx_status hdr_sig_encode(const uint8_t *sig, size_t sig_len, uint8_t *out, size_t cap, size_t *len_out) { + size_t at = 0; + if (cap < hdr_sig_encode_len(sig, sig_len)) { + return SFINX_ERR_NOSPACE; + } + memcpy(out + at, "signature: ", 11); + at += 11; + hdr_hex_encode(out + at, sig, sig_len); + at += sig_len * 2; + 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, + .name = "hdr", + .key_detect = hdr_key_detect, + .key_decode = hdr_key_decode, + .key_encode_len = hdr_key_encode_len, + .key_encode = hdr_key_encode, + .sig_detect = hdr_sig_detect, + .sig_decode = hdr_sig_decode, + .sig_encode_len = hdr_sig_encode_len, + .sig_encode = hdr_sig_encode, }; __attribute__((constructor)) static void hdr_register(void) { diff --git a/src/fmt/hex.c b/src/fmt/hex.c @@ -0,0 +1,198 @@ +#include <stdlib.h> + +#include "../sfinx.h" + +// hex format {{{ +// +// Lowercase hex text, no marker, so it is never auto-detected +// key decode reads a seed, key encode writes the seed if present else the public key +// signature decode parses hex, encode emits hex + +static int hex_val(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 void hex_put(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 sfinx_status hex_get(const uint8_t *in, size_t in_len, uint8_t *out, size_t cap, size_t *out_len) { + size_t n; + if (in_len % 2 != 0) { + return SFINX_ERR_FORMAT; + } + n = in_len / 2; + if (cap < n) { + return SFINX_ERR_NOSPACE; + } + for (size_t i = 0; i < n; i++) { + int hi = hex_val(in[i * 2]); + int lo = hex_val(in[i * 2 + 1]); + if (hi < 0 || lo < 0) { + return SFINX_ERR_FORMAT; + } + out[i] = (uint8_t)((hi << 4) | lo); + } + if (out_len) { + *out_len = n; + } + return SFINX_OK; +} + +static sfinx_status hex_key_decode(const uint8_t *data, size_t len, sfinx_key *out) { + sfinx_status status; + if (len == 0 || len / 2 > SFINX_SEED_LEN_MAX) { + return SFINX_ERR_FORMAT; + } + out->seed = malloc(len / 2 + 1); + if (!out->seed) { + return SFINX_ERR_NOSPACE; + } + status = hex_get(data, len, out->seed, len / 2, &out->seed_len); + if (status != SFINX_OK) { + free(out->seed); + out->seed = NULL; + } + return status; +} + +static size_t hex_key_encode_len(const sfinx_key *key) { + if (key->seed) return key->seed_len * 2; + if (key->pub) return key->pub_len * 2; + return 0; +} + +static sfinx_status hex_key_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 * 2) { + return SFINX_ERR_NOSPACE; + } + hex_put(out, src, len); + if (len_out) { + *len_out = len * 2; + } + return SFINX_OK; +} + +static sfinx_status hex_sig_decode(const uint8_t *data, size_t len, uint8_t *out, size_t cap, size_t *len_out) { + return hex_get(data, len, out, cap, len_out); +} + +static size_t hex_sig_encode_len(const uint8_t *sig, size_t sig_len) { + (void)sig; + return sig_len * 2; +} + +static sfinx_status hex_sig_encode(const uint8_t *sig, size_t sig_len, uint8_t *out, size_t cap, size_t *len_out) { + if (cap < sig_len * 2) { + return SFINX_ERR_NOSPACE; + } + hex_put(out, sig, sig_len); + if (len_out) { + *len_out = sig_len * 2; + } + return SFINX_OK; +} + +static sfinx_format hex_format = { + .name = "hex", + .key_decode = hex_key_decode, + .key_encode_len = hex_key_encode_len, + .key_encode = hex_key_encode, + .sig_decode = hex_sig_decode, + .sig_encode_len = hex_sig_encode_len, + .sig_encode = hex_sig_encode, +}; + +__attribute__((constructor)) static void hex_register(void) { + sfinx_format_register(&hex_format); +} +// }}} + +// hexpub format {{{ +// +// A public key as hex text, no marker, so it is never auto-detected +// decode infers the hash from the length, encode emits hex + +static sfinx_hash hexpub_hash_for_len(size_t len) { + static const sfinx_hash hashes[] = {SFINX_HASH_224, SFINX_HASH_256, SFINX_HASH_384, SFINX_HASH_512}; + for (size_t i = 0; i < sizeof(hashes) / sizeof(hashes[0]); i++) { + if (sfinx_hash_len(hashes[i]) == len) return hashes[i]; + } + return 0; +} + +static sfinx_status hexpub_key_decode(const uint8_t *data, size_t len, sfinx_key *out) { + sfinx_status status; + size_t n; + sfinx_hash hash; + if (len % 2 != 0) { + return SFINX_ERR_FORMAT; + } + n = len / 2; + hash = hexpub_hash_for_len(n); + if (hash == 0) { + return SFINX_ERR_FORMAT; + } + out->pub = malloc(n); + if (!out->pub) { + return SFINX_ERR_NOSPACE; + } + status = hex_get(data, len, out->pub, n, &out->pub_len); + if (status != SFINX_OK) { + free(out->pub); + out->pub = NULL; + return status; + } + out->hash = hash; + return SFINX_OK; +} + +static size_t hexpub_key_encode_len(const sfinx_key *key) { + return key->pub ? key->pub_len * 2 : 0; +} + +static sfinx_status hexpub_key_encode(const sfinx_key *key, uint8_t *out, size_t cap, size_t *len_out) { + if (!key->pub) { + return SFINX_ERR_ARGS; + } + if (cap < key->pub_len * 2) { + return SFINX_ERR_NOSPACE; + } + hex_put(out, key->pub, key->pub_len); + if (len_out) { + *len_out = key->pub_len * 2; + } + return SFINX_OK; +} + +static sfinx_format hexpub_format = { + .name = "hexpub", + .key_decode = hexpub_key_decode, + .key_encode_len = hexpub_key_encode_len, + .key_encode = hexpub_key_encode, +}; + +__attribute__((constructor)) static void hexpub_register(void) { + sfinx_format_register(&hexpub_format); +} +// }}} + +// vim:fdm=marker:fdl=0 diff --git a/src/fmt/raw.c b/src/fmt/raw.c @@ -5,12 +5,13 @@ // 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 +// The whole input is raw bytes, no marker, so it is never auto-detected +// key decode reads a seed, key encode writes the seed if present else the public key +// signature decode and encode are the identity -static sfinx_status raw_decode(const uint8_t *data, size_t len, sfinx_key *out) { +static sfinx_status raw_key_decode(const uint8_t *data, size_t len, sfinx_key *out) { uint8_t *seed; - if (len == 0) { + if (len == 0 || len > SFINX_SEED_LEN_MAX) { return SFINX_ERR_FORMAT; } seed = malloc(len); @@ -23,13 +24,13 @@ static sfinx_status raw_decode(const uint8_t *data, size_t len, sfinx_key *out) return SFINX_OK; } -static size_t raw_encode_len(const sfinx_key *key) { +static size_t raw_key_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) { +static sfinx_status raw_key_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) { @@ -51,12 +52,34 @@ static sfinx_status raw_encode(const sfinx_key *key, uint8_t *out, size_t cap, s return SFINX_OK; } +static sfinx_status raw_sig_decode(const uint8_t *data, size_t len, uint8_t *out, size_t cap, size_t *len_out) { + if (cap < len) { + return SFINX_ERR_NOSPACE; + } + memcpy(out, data, len); + if (len_out) { + *len_out = len; + } + return SFINX_OK; +} + +static size_t raw_sig_encode_len(const uint8_t *sig, size_t sig_len) { + (void)sig; + return sig_len; +} + +static sfinx_status raw_sig_encode(const uint8_t *sig, size_t sig_len, uint8_t *out, size_t cap, size_t *len_out) { + return raw_sig_decode(sig, sig_len, out, cap, len_out); +} + static sfinx_format raw_format = { - .name = "raw", - .detect = NULL, - .decode = raw_decode, - .encode_len = raw_encode_len, - .encode = raw_encode, + .name = "raw", + .key_decode = raw_key_decode, + .key_encode_len = raw_key_encode_len, + .key_encode = raw_key_encode, + .sig_decode = raw_sig_decode, + .sig_encode_len = raw_sig_encode_len, + .sig_encode = raw_sig_encode, }; __attribute__((constructor)) static void raw_register(void) { @@ -64,4 +87,62 @@ __attribute__((constructor)) static void raw_register(void) { } // }}} +// rawpub format {{{ +// +// A bare public key of L bytes, no marker, so it is never auto-detected +// decode infers the hash from the length, encode writes the public half + +static sfinx_hash rawpub_hash_for_len(size_t len) { + static const sfinx_hash hashes[] = {SFINX_HASH_224, SFINX_HASH_256, SFINX_HASH_384, SFINX_HASH_512}; + for (size_t i = 0; i < sizeof(hashes) / sizeof(hashes[0]); i++) { + if (sfinx_hash_len(hashes[i]) == len) return hashes[i]; + } + return 0; +} + +static sfinx_status rawpub_key_decode(const uint8_t *data, size_t len, sfinx_key *out) { + sfinx_hash hash = rawpub_hash_for_len(len); + if (hash == 0) { + return SFINX_ERR_FORMAT; + } + out->pub = malloc(len); + if (!out->pub) { + return SFINX_ERR_NOSPACE; + } + memcpy(out->pub, data, len); + out->pub_len = len; + out->hash = hash; + return SFINX_OK; +} + +static size_t rawpub_key_encode_len(const sfinx_key *key) { + return key->pub ? key->pub_len : 0; +} + +static sfinx_status rawpub_key_encode(const sfinx_key *key, uint8_t *out, size_t cap, size_t *len_out) { + if (!key->pub) { + return SFINX_ERR_ARGS; + } + if (cap < key->pub_len) { + return SFINX_ERR_NOSPACE; + } + memcpy(out, key->pub, key->pub_len); + if (len_out) { + *len_out = key->pub_len; + } + return SFINX_OK; +} + +static sfinx_format rawpub_format = { + .name = "rawpub", + .key_decode = rawpub_key_decode, + .key_encode_len = rawpub_key_encode_len, + .key_encode = rawpub_key_encode, +}; + +__attribute__((constructor)) static void rawpub_register(void) { + sfinx_format_register(&rawpub_format); +} +// }}} + // vim:fdm=marker:fdl=0 diff --git a/src/keypair.c b/src/keypair.c @@ -77,10 +77,20 @@ const sfinx_format *sfinx_format_find(const char *name) { return NULL; } -static const sfinx_format *sfinx_format_detect(const uint8_t *data, size_t len) { +static const sfinx_format *sfinx_format_detect_key(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)) { + if (fmt->key_detect && fmt->key_detect(data, len)) { + return fmt; + } + } + return NULL; +} + +static const sfinx_format *sfinx_format_detect_sig(const uint8_t *data, size_t len) { + const sfinx_format *fmt; + for (fmt = sfinx_formats; fmt; fmt = fmt->next) { + if (fmt->sig_detect && fmt->sig_detect(data, len)) { return fmt; } } @@ -90,7 +100,7 @@ static const sfinx_format *sfinx_format_detect(const uint8_t *data, size_t len) // Dispatch {{{ // -// Decode mallocs into the key, encode writes into the caller buffer +// Key decode mallocs into the key, every 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; @@ -99,12 +109,12 @@ sfinx_status sfinx_key_decode(const uint8_t *data, size_t len, const char *forma if (!data || !out) { return SFINX_ERR_ARGS; } - fmt = format ? sfinx_format_find(format) : sfinx_format_detect(data, len); - if (!fmt || !fmt->decode) { + fmt = format ? sfinx_format_find(format) : sfinx_format_detect_key(data, len); + if (!fmt || !fmt->key_decode) { return SFINX_ERR_FORMAT; } sfinx_key_init(out); - status = fmt->decode(data, len, out); + status = fmt->key_decode(data, len, out); if (status != SFINX_OK) { sfinx_key_free(out); } @@ -117,10 +127,10 @@ size_t sfinx_key_encode_len(const sfinx_key *key, const char *format) { return 0; } fmt = sfinx_format_find(format); - if (!fmt || !fmt->encode_len) { + if (!fmt || !fmt->key_encode_len) { return 0; } - return fmt->encode_len(key); + return fmt->key_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) { @@ -129,10 +139,53 @@ sfinx_status sfinx_key_encode(const sfinx_key *key, const char *format, uint8_t return SFINX_ERR_ARGS; } fmt = sfinx_format_find(format); - if (!fmt || !fmt->encode) { + if (!fmt || !fmt->key_encode) { + return SFINX_ERR_ARGS; + } + return fmt->key_encode(key, out, cap, len_out); +} + +sfinx_status sfinx_sig_decode(const uint8_t *data, size_t len, const char *format, uint8_t *out, size_t cap, + size_t *len_out) { + const sfinx_format *fmt; + if (!data || !out) { + return SFINX_ERR_ARGS; + } + if (format) { + fmt = sfinx_format_find(format); + } else { + fmt = sfinx_format_detect_sig(data, len); + if (!fmt) fmt = sfinx_format_find("raw"); + } + if (!fmt || !fmt->sig_decode) { + return SFINX_ERR_FORMAT; + } + return fmt->sig_decode(data, len, out, cap, len_out); +} + +size_t sfinx_sig_encode_len(const uint8_t *sig, size_t sig_len, const char *format) { + const sfinx_format *fmt; + if (!sig || !format) { + return 0; + } + fmt = sfinx_format_find(format); + if (!fmt || !fmt->sig_encode_len) { + return 0; + } + return fmt->sig_encode_len(sig, sig_len); +} + +sfinx_status sfinx_sig_encode(const uint8_t *sig, size_t sig_len, const char *format, uint8_t *out, size_t cap, + size_t *len_out) { + const sfinx_format *fmt; + if (!sig || !format || !out) { + return SFINX_ERR_ARGS; + } + fmt = sfinx_format_find(format); + if (!fmt || !fmt->sig_encode) { return SFINX_ERR_ARGS; } - return fmt->encode(key, out, cap, len_out); + return fmt->sig_encode(sig, sig_len, out, cap, len_out); } // }}} diff --git a/src/sfinx.h b/src/sfinx.h @@ -100,16 +100,23 @@ sfinx_status sfinx_key_derive(sfinx_key *key); // Formats {{{ // -// Pluggable key encodings, registered by constructors and detected by content +// Pluggable key and signature encodings, registered by constructors // decode with a NULL name auto-detects, encode_len sizes before encode +// signature auto-detect falls back to raw when nothing matches 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); + + int (*key_detect)(const uint8_t *data, size_t len); + sfinx_status (*key_decode)(const uint8_t *data, size_t len, sfinx_key *out); + size_t (*key_encode_len)(const sfinx_key *key); + sfinx_status (*key_encode)(const sfinx_key *key, uint8_t *out, size_t cap, size_t *len_out); + + int (*sig_detect)(const uint8_t *data, size_t len); + sfinx_status (*sig_decode)(const uint8_t *data, size_t len, uint8_t *out, size_t cap, size_t *len_out); + size_t (*sig_encode_len)(const uint8_t *sig, size_t sig_len); + sfinx_status (*sig_encode)(const uint8_t *sig, size_t sig_len, uint8_t *out, size_t cap, size_t *len_out); } sfinx_format; void sfinx_format_register(sfinx_format *format); @@ -118,6 +125,12 @@ 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); + +sfinx_status sfinx_sig_decode(const uint8_t *data, size_t len, const char *format, uint8_t *out, size_t cap, + size_t *len_out); +size_t sfinx_sig_encode_len(const uint8_t *sig, size_t sig_len, const char *format); +sfinx_status sfinx_sig_encode(const uint8_t *sig, size_t sig_len, const char *format, uint8_t *out, size_t cap, + size_t *len_out); // }}} #endif // __SFINX_H__ diff --git a/src/util/file.c b/src/util/file.c @@ -2,61 +2,75 @@ #include <fcntl.h> #include <stdlib.h> -#include <sys/stat.h> +#include <string.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) { +static int read_fd(int fd, uint8_t **out, size_t *len_out) { + size_t cap = 65536; + size_t len = 0; + uint8_t *buf = malloc(cap + 1); + if (!buf) return -1; + for (;;) { + if (len == cap) { + size_t ncap = cap * 2; + uint8_t *nbuf = realloc(buf, ncap + 1); + if (!nbuf) { + free(buf); + return -1; + } + buf = nbuf; + cap = ncap; + } + ssize_t n = read(fd, buf + len, cap - len); + if (n < 0) { free(buf); - close(fd); return -1; } - got += (size_t)n; + if (n == 0) break; + len += (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) { +static int write_fd(int fd, const uint8_t *data, size_t len) { 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; - } + if (n <= 0) return -1; put += (size_t)n; } - return close(fd); + return 0; +} + +int util_file_read(const char *path, uint8_t **out, size_t *len_out) { + int fd; + int rc; + if (!out || !len_out) return -1; + if (!path || strcmp(path, "-") == 0) { + return read_fd(STDIN_FILENO, out, len_out); + } + fd = open(path, O_RDONLY); + if (fd < 0) return -1; + rc = read_fd(fd, out, len_out); + close(fd); + return rc; +} + +int util_file_write(const char *path, const uint8_t *data, size_t len, unsigned mode, int force) { + int fd; + int rc; + int flags = O_WRONLY | O_CREAT | O_TRUNC; + if (!data && len) return -1; + if (!path || strcmp(path, "-") == 0) { + return write_fd(STDOUT_FILENO, data, len); + } + if (!force) flags |= O_EXCL; + fd = open(path, flags, mode); + if (fd < 0) return -1; + rc = write_fd(fd, data, len); + close(fd); + return rc; } diff --git a/src/util/file.h b/src/util/file.h @@ -6,7 +6,7 @@ // File {{{ // -// Whole-file IO for the CLI +// 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 int util_file_read(const char *path, uint8_t **out, size_t *len_out); diff --git a/test/07-key.c b/test/07-key.c @@ -14,6 +14,7 @@ int main(void) { sfinx_key key; sfinx_key other; sfinx_key badkey; + sfinx_key pubkey; sfinx_status status; tap_begin("sfinx key"); @@ -58,6 +59,26 @@ int main(void) { 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_init(&pubkey); + need = sfinx_key_encode_len(&key, "rawpub"); + tap(need == key.pub_len, "rawpub encode_len", "got %zu", need); + status = sfinx_key_encode(&key, "rawpub", hdr, sizeof(hdr), &hdr_len); + tap(status == SFINX_OK, "rawpub encode", "%s", sfinx_strerror(status)); + status = sfinx_key_decode(hdr, hdr_len, "rawpub", &pubkey); + tap(status == SFINX_OK && pubkey.pub_len == key.pub_len && memcmp(pubkey.pub, key.pub, key.pub_len) == 0, + "rawpub round trip", "mismatch"); + sfinx_key_free(&pubkey); + + sfinx_key_init(&pubkey); + need = sfinx_key_encode_len(&key, "hexpub"); + tap(need == key.pub_len * 2, "hexpub encode_len", "got %zu", need); + status = sfinx_key_encode(&key, "hexpub", hdr, sizeof(hdr), &hdr_len); + tap(status == SFINX_OK, "hexpub encode", "%s", sfinx_strerror(status)); + status = sfinx_key_decode(hdr, hdr_len, "hexpub", &pubkey); + tap(status == SFINX_OK && pubkey.pub_len == key.pub_len && memcmp(pubkey.pub, key.pub, key.pub_len) == 0, + "hexpub round trip", "mismatch"); + sfinx_key_free(&pubkey); + sfinx_key_free(&key); sfinx_key_free(&other); sfinx_key_free(&badkey); diff --git a/test/08-sig.c b/test/08-sig.c @@ -0,0 +1,54 @@ +#include <string.h> + +#include "_tap.c" +#include "src/sfinx.c" + +int main(void) { + static const uint8_t raw[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; + static const uint8_t badhex[] = "zz"; + uint8_t out[64]; + uint8_t back[64]; + size_t out_len = 0; + size_t back_len = 0; + size_t need = 0; + sfinx_status status; + + tap_begin("sfinx signature formats"); + + status = sfinx_sig_decode(raw, sizeof(raw), "raw", out, sizeof(out), &out_len); + tap(status == SFINX_OK, "raw decode", "%s", sfinx_strerror(status)); + tap(out_len == sizeof(raw) && memcmp(out, raw, sizeof(raw)) == 0, "raw is the identity", "mismatch"); + + need = sfinx_sig_encode_len(raw, sizeof(raw), "hex"); + tap(need == sizeof(raw) * 2, "hex encode_len", "got %zu", need); + status = sfinx_sig_encode(raw, sizeof(raw), "hex", out, sizeof(out), &out_len); + tap(status == SFINX_OK, "hex encode", "%s", sfinx_strerror(status)); + tap(out_len == sizeof(raw) * 2 && memcmp(out, "0123456789abcdef", 16) == 0, "hex text", "got %.*s", (int)out_len, + out); + + status = sfinx_sig_decode(out, out_len, "hex", back, sizeof(back), &back_len); + tap(status == SFINX_OK, "hex decode", "%s", sfinx_strerror(status)); + tap(back_len == sizeof(raw) && memcmp(back, raw, sizeof(raw)) == 0, "hex round trip", "mismatch"); + + need = sfinx_sig_encode_len(raw, sizeof(raw), "hdr"); + status = sfinx_sig_encode(raw, sizeof(raw), "hdr", out, sizeof(out), &out_len); + tap(status == SFINX_OK, "hdr encode", "%s", sfinx_strerror(status)); + tap(need == out_len, "hdr encode_len", "got %zu want %zu", need, out_len); + status = sfinx_sig_decode(out, out_len, NULL, back, sizeof(back), &back_len); + tap(status == SFINX_OK, "hdr auto-detect", "%s", sfinx_strerror(status)); + tap(back_len == sizeof(raw) && memcmp(back, raw, sizeof(raw)) == 0, "hdr round trip", "mismatch"); + + status = sfinx_sig_decode(raw, sizeof(raw), NULL, back, sizeof(back), &back_len); + tap(status == SFINX_OK && back_len == sizeof(raw), "auto falls back to raw", "%s", sfinx_strerror(status)); + + status = sfinx_sig_decode(badhex, sizeof(badhex) - 1, "hex", back, sizeof(back), &back_len); + tap(status == SFINX_ERR_FORMAT, "hex rejects bad input", "got %s", sfinx_strerror(status)); + + status = sfinx_sig_decode(raw, sizeof(raw), "nope", back, sizeof(back), &back_len); + tap(status == SFINX_ERR_FORMAT, "rejects an unknown format", "got %s", sfinx_strerror(status)); + + status = sfinx_sig_encode(raw, sizeof(raw), "hex", out, 4, &out_len); + tap(status == SFINX_ERR_NOSPACE, "hex encode rejects a small cap", "got %s", sfinx_strerror(status)); + + return tap_plan(); +} diff --git a/test/test_embed.sh b/test/test_embed.sh @@ -90,10 +90,14 @@ int main(void) { 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; + uint8_t sigtext[8192]; + uint8_t sigback[4096]; + size_t pub_len = 0; + size_t sig_len = 0; + size_t hdr_len = 0; + size_t sigtext_len = 0; + size_t sigback_len = 0; + size_t msg_len = sizeof(msg) - 1; sfinx_key key; sfinx_key reread; sfinx_status status; @@ -138,6 +142,19 @@ int main(void) { if (reread.pub_len != pub_len || memcmp(reread.pub, pub, pub_len) != 0) { return fail("hdr pub mismatch"); } + sigtext_len = sfinx_sig_encode_len(sig, sig_len, "hdr"); + if (sigtext_len == 0 || sigtext_len > sizeof(sigtext)) { + return fail("sig encode_len"); + } + if (sfinx_sig_encode(sig, sig_len, "hdr", sigtext, sizeof(sigtext), &sigtext_len) != SFINX_OK) { + return fail("sig encode hdr"); + } + if (sfinx_sig_decode(sigtext, sigtext_len, NULL, sigback, sizeof(sigback), &sigback_len) != SFINX_OK) { + return fail("sig decode auto"); + } + if (sigback_len != sig_len || memcmp(sigback, sig, sig_len) != 0) { + return fail("sig round trip"); + } sfinx_key_free(&key); sfinx_key_free(&reread); return 0; diff --git a/test/test_printkey.sh b/test/test_printkey.sh @@ -12,33 +12,42 @@ tap_begin "cli: printkey" "${BIN}" seed -o "${TMP}/seed.bin" -"${BIN}" printkey --in raw -k "${TMP}/seed.bin" >"${TMP}/key.hdr" +"${BIN}" printkey --in-fmt 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" +"${BIN}" printkey --in-fmt raw -k "${TMP}/seed.bin" --public-only >"${TMP}/a.hdr" +"${BIN}" printkey --in-fmt 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" +"${BIN}" printkey -k "${TMP}/key.hdr" --public-only --out-fmt 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" +"${BIN}" printkey --in-fmt raw -k "${TMP}/seed.bin" -L 512 --public-only --out-fmt 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" +"${BIN}" printkey -k "${TMP}/key.hdr" --public-only --out-fmt rawpub >"${TMP}/rawpub.bin" +n=$(wc -c <"${TMP}/rawpub.bin" | tr -d '[:space:]') +tap "$([ "${n}" = 32 ] && echo 1 || echo 0)" "out-fmt rawpub gives a 32-byte pub" "got ${n}" + +"${BIN}" printkey --in-fmt rawpub -k "${TMP}/rawpub.bin" --public-only --out-fmt hexpub >"${TMP}/hexpub.hex" +bytes=$(tr -d '\n' <"${TMP}/hexpub.hex" | wc -c | tr -d '[:space:]') +grep -qE '^[0-9a-f]+$' "${TMP}/hexpub.hex" && shape=1 || shape=0 +tap "$([ "${bytes}" = 64 ] && [ "${shape}" = 1 ] && echo 1 || echo 0)" "rawpub reads back and hexpub writes hex" "got ${bytes}" + +"${BIN}" printkey --in-fmt 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" +"${BIN}" printkey --in-fmt 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=$? +"${BIN}" printkey -k "${TMP}/key.hdr" --out-fmt bogus >/dev/null 2>&1 || rc=$? tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "rejects an unknown format" "exit ${rc}" rc=0 diff --git a/test/test_sign_verify.sh b/test/test_sign_verify.sh @@ -0,0 +1,62 @@ +#!/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: sign and verify" + +"${BIN}" seed -o "${TMP}/seed.bin" +"${BIN}" printkey --in-fmt raw -k "${TMP}/seed.bin" -o "${TMP}/key.hdr" +"${BIN}" printkey -k "${TMP}/key.hdr" --public-only -o "${TMP}/pub.hdr" + +"${BIN}" sign -k "${TMP}/key.hdr" -m hello -P 0011 -o "${TMP}/sig.bin" +"${BIN}" verify -k "${TMP}/pub.hdr" -m hello -S "${TMP}/sig.bin" >/dev/null && ok=1 || ok=0 +tap "${ok}" "raw signature file round trip" "failed" + +"${BIN}" sign -k "${TMP}/key.hdr" -m hello -P 0011 -o "${TMP}/a.bin" +"${BIN}" sign -k "${TMP}/key.hdr" -m hello -P 0011 -o "${TMP}/b.bin" +cmp -s "${TMP}/a.bin" "${TMP}/b.bin" && ok=1 || ok=0 +tap "${ok}" "fixed path is deterministic" "differs" + +HEX=$("${BIN}" sign -k "${TMP}/key.hdr" -m hello -P 0011) +"${BIN}" verify -k "${TMP}/pub.hdr" -m hello -s "${HEX}" >/dev/null && ok=1 || ok=0 +tap "${ok}" "hex signature with -s" "failed" + +"${BIN}" sign -k "${TMP}/key.hdr" -m hello -P 0011 --sig-fmt hdr | + "${BIN}" verify -k "${TMP}/pub.hdr" -m hello >/dev/null && ok=1 || ok=0 +tap "${ok}" "hdr signature auto-detected from stdin" "failed" + +"${BIN}" printkey -k "${TMP}/key.hdr" --public-only --out-fmt rawpub >"${TMP}/pub.bin" +"${BIN}" sign --key-fmt raw -k "${TMP}/seed.bin" -m hello -P 0011 --sig-fmt hex -o "${TMP}/sig.hex" +"${BIN}" verify --key-fmt rawpub -k "${TMP}/pub.bin" --sig-fmt hex -m hello -S "${TMP}/sig.hex" >/dev/null && ok=1 || ok=0 +tap "${ok}" "explicit key-fmt and sig-fmt round trip" "failed" + +"${BIN}" printkey -k "${TMP}/key.hdr" --public-only --out-fmt hexpub >"${TMP}/pub.hex" +"${BIN}" verify --key-fmt hexpub -k "${TMP}/pub.hex" -m hello -S "${TMP}/sig.bin" >/dev/null && ok=1 || ok=0 +tap "${ok}" "hexpub key reads back" "failed" + +printf 'hello' >"${TMP}/msg" +"${BIN}" verify -k "${TMP}/pub.hdr" -M "${TMP}/msg" -S "${TMP}/sig.bin" >/dev/null && ok=1 || ok=0 +tap "${ok}" "message from a file" "failed" + +printf 'hello' | "${BIN}" verify -k "${TMP}/pub.hdr" -S "${TMP}/sig.bin" >/dev/null && ok=1 || ok=0 +tap "${ok}" "message from stdin" "failed" + +rc=0 +"${BIN}" verify -k "${TMP}/pub.hdr" -m nope -S "${TMP}/sig.bin" >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "tampered message fails" "exit ${rc}" + +rc=0 +"${BIN}" verify -k "${TMP}/pub.hdr" -m hello -L 512 -S "${TMP}/sig.bin" >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "hash mismatch fails" "exit ${rc}" + +rc=0 +"${BIN}" verify -k "${TMP}/pub.hdr" >/dev/null 2>&1 || rc=$? +tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "both inputs on stdin fails" "exit ${rc}" + +tap_plan diff --git a/test/test_subcommands.sh b/test/test_subcommands.sh @@ -17,6 +17,10 @@ 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}" +case "${out}" in *sign*) ok=1 ;; *) ok=0 ;; esac +tap "${ok}" "help lists sign" "${out}" +case "${out}" in *verify*) ok=1 ;; *) ok=0 ;; esac +tap "${ok}" "help lists verify" "${out}" out=$("${BIN}" --version 2>&1 || true) tap "$([ -n "${out}" ] && echo 1 || echo 0)" "--version prints" "empty"