sign.c (6667B)
1 #include <cofyc/argparse.h> 2 #include <stddef.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "common.h" 8 #include "registry.h" 9 #include "sfinx.h" 10 #include "util/file.h" 11 12 static int cmd_sign(int argc, const char **argv) { 13 const char *key_file = NULL; 14 const char *key_format = NULL; 15 const char *out_file = NULL; 16 const char *format = NULL; 17 const char *path_hex = NULL; 18 const char *message = NULL; 19 const char *msg_file = NULL; 20 int single = 0; 21 int hash = 0; 22 int path_len = -1; 23 int force = 0; 24 uint8_t *key_data = NULL; 25 uint8_t *msg_data = NULL; 26 uint8_t *path = NULL; 27 uint8_t *sig = NULL; 28 uint8_t *encoded = NULL; 29 size_t key_len = 0; 30 size_t msg_len = 0; 31 size_t path_bytes = 0; 32 size_t sig_len = 0; 33 size_t enc_len = 0; 34 sfinx_key key; 35 sfinx_status status; 36 int rc = 1; 37 38 static const char *const usages[] = { 39 "sfinx sign -k keyfile [-L hash] [--single | -p path-len | -P path] [-m message | -M message-file] [--key-fmt " 40 "fmt] " 41 "[--sig-fmt fmt] [-o file] [-F]", 42 NULL, 43 }; 44 struct argparse_option options[] = { 45 OPT_HELP(), 46 OPT_STRING('k', "key-file", &key_file, "secret key file", NULL, 0, 0), 47 OPT_STRING(0, "key-fmt", &key_format, "key format, auto by default", NULL, 0, 0), 48 OPT_INTEGER('L', "hash", &hash, "hash size 224/256/384/512", NULL, 0, 0), 49 OPT_INTEGER('p', "path-len", &path_len, "random path length", NULL, 0, 0), 50 OPT_STRING('P', "path", &path_hex, "explicit path as hex", NULL, 0, 0), 51 OPT_BOOLEAN(0, "single", &single, "single-key mode, one WOTS key over H(message)", NULL, 0, 0), 52 OPT_STRING('m', "message", &message, "message string", NULL, 0, 0), 53 OPT_STRING('M', "message-file", &msg_file, "message file, - for stdin", NULL, 0, 0), 54 OPT_STRING(0, "sig-fmt", &format, "signature format, hex on stdout and raw to a file by default", NULL, 0, 0), 55 OPT_STRING('o', "out", &out_file, "signature file, - for stdout", NULL, 0, 0), 56 OPT_BOOLEAN('F', "force", &force, "overwrite an existing output file", NULL, 0, 0), 57 OPT_END(), 58 }; 59 struct argparse argparse; 60 argparse_init(&argparse, options, usages, 0); 61 argparse_describe(&argparse, "\nSign a message", NULL); 62 argc = argparse_parse(&argparse, argc, argv); 63 64 sfinx_key_init(&key); 65 66 if (argc > 0) { 67 fprintf(stderr, "sfinx: sign: unexpected argument: %s\n", argv[0]); 68 return 1; 69 } 70 if (!key_file) { 71 fprintf(stderr, "sfinx: sign: missing --key-file\n"); 72 return 1; 73 } 74 if (message && msg_file) { 75 fprintf(stderr, "sfinx: sign: --message and --message-file are exclusive\n"); 76 return 1; 77 } 78 if (single && (path_hex || path_len != -1)) { 79 fprintf(stderr, "sfinx: sign: --single cannot be combined with --path-len or --path\n"); 80 return 1; 81 } 82 if (!single && path_len == -1) { 83 path_len = SFINX_PATH_LEN_DEFAULT; 84 } 85 86 if (util_file_read(key_file, &key_data, &key_len) != 0) { 87 fprintf(stderr, "sfinx: sign: cannot read %s\n", key_file); 88 goto done; 89 } 90 status = sfinx_key_decode(key_data, key_len, key_format, &key); 91 if (status != SFINX_OK && !key_format) { 92 status = sfinx_key_decode(key_data, key_len, "raw", &key); 93 } 94 if (status != SFINX_OK) { 95 fprintf(stderr, "sfinx: sign: cannot decode %s: %s\n", key_file, sfinx_strerror(status)); 96 goto done; 97 } 98 if (!key.seed) { 99 fprintf(stderr, "sfinx: sign: key has no seed\n"); 100 goto done; 101 } 102 if (hash != 0) { 103 key.hash = (sfinx_hash)hash; 104 } 105 if (sfinx_hash_len(key.hash) == 0) { 106 fprintf(stderr, "sfinx: sign: invalid hash size: %d\n", (int)key.hash); 107 goto done; 108 } 109 110 if (cli_read_message(message, msg_file, &msg_data, &msg_len) != 0) { 111 fprintf(stderr, "sfinx: sign: cannot read the message\n"); 112 goto done; 113 } 114 115 if (single) { 116 path_bytes = 0; 117 } else if (path_hex) { 118 if (cli_hex_decode(path_hex, &path, &path_bytes) != 0 || !sfinx_path_len_valid(path_bytes)) { 119 fprintf(stderr, "sfinx: sign: invalid path\n"); 120 goto done; 121 } 122 } else { 123 if (path_len <= 0 || !sfinx_path_len_valid((size_t)path_len)) { 124 fprintf(stderr, "sfinx: sign: invalid path length: %d (use --single for single-key mode)\n", path_len); 125 goto done; 126 } 127 path_bytes = (size_t)path_len; 128 path = malloc(path_bytes); 129 if (!path) { 130 fprintf(stderr, "sfinx: sign: out of memory\n"); 131 goto done; 132 } 133 if (sfinx_random_bytes(path, path_bytes) != SFINX_OK) { 134 fprintf(stderr, "sfinx: sign: %s\n", sfinx_strerror(SFINX_ERR_RANDOM)); 135 goto done; 136 } 137 } 138 139 sig_len = sfinx_signature_len(key.hash, path_bytes); 140 sig = malloc(sig_len); 141 if (!sig) { 142 fprintf(stderr, "sfinx: sign: out of memory\n"); 143 goto done; 144 } 145 status = single ? sfinx_single_sign(key.hash, key.seed, key.seed_len, msg_data, msg_len, sig, sig_len, NULL) 146 : sfinx_tree_sign(key.hash, key.seed, key.seed_len, path, path_bytes, msg_data, msg_len, sig, sig_len, 147 NULL); 148 if (status != SFINX_OK) { 149 fprintf(stderr, "sfinx: sign: %s\n", sfinx_strerror(status)); 150 goto done; 151 } 152 153 if (!format) { 154 format = (out_file && strcmp(out_file, "-") != 0) ? "raw" : "hex"; 155 } 156 enc_len = sfinx_sig_encode_len(sig, sig_len, format); 157 if (enc_len == 0) { 158 fprintf(stderr, "sfinx: sign: unknown signature format: %s\n", format); 159 goto done; 160 } 161 encoded = malloc(enc_len); 162 if (!encoded) { 163 fprintf(stderr, "sfinx: sign: out of memory\n"); 164 goto done; 165 } 166 status = sfinx_sig_encode(sig, sig_len, format, encoded, enc_len, &enc_len); 167 if (status != SFINX_OK) { 168 fprintf(stderr, "sfinx: sign: cannot encode: %s\n", sfinx_strerror(status)); 169 goto done; 170 } 171 if (util_file_write(out_file, encoded, enc_len, 0644, force) != 0) { 172 fprintf(stderr, "sfinx: sign: cannot write %s\n", out_file ? out_file : "-"); 173 goto done; 174 } 175 rc = 0; 176 177 done: 178 free(key_data); 179 free(msg_data); 180 free(path); 181 free(sig); 182 free(encoded); 183 sfinx_key_free(&key); 184 return rc; 185 } 186 187 static struct cli_command sign_command = { 188 .names = (const char *const[]){"sign", NULL}, 189 .display = "sign", 190 .description = "Sign a message", 191 .help = 192 "sfinx sign -k keyfile [-L hash] [--single | -p path-len | -P path] [-m message | -M message-file] [--key-fmt " 193 "fmt] " 194 "[--sig-fmt fmt] [-o file] [-F]", 195 .fn = cmd_sign, 196 }; 197 198 __attribute__((constructor)) static void sign_register(void) { 199 cli_command_register(&sign_command); 200 }