generate.c (7032B)
1 #include <cofyc/argparse.h> 2 #include <ctype.h> 3 #include <stddef.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "common.h" 9 #include "registry.h" 10 #include "sfinx.h" 11 #include "util/file.h" 12 13 // Pub path {{{ 14 // 15 // The public half goes next to the key file 16 // a `.key` suffix of any case becomes `.pub` in the same case, anything else has `.pub` appended 17 18 static int path_ends_in_key(const char *path, size_t len) { 19 static const char suffix[] = ".key"; 20 if (len < 4) { 21 return 0; 22 } 23 for (size_t i = 0; i < 4; i++) { 24 if (tolower((unsigned char)path[len - 4 + i]) != suffix[i]) { 25 return 0; 26 } 27 } 28 return 1; 29 } 30 31 static char pub_char(char source, char lower) { 32 return isupper((unsigned char)source) ? (char)toupper((unsigned char)lower) : lower; 33 } 34 35 static int cli_pub_path(const char *path, char **out) { 36 size_t len = strlen(path); 37 char *buf; 38 if (path_ends_in_key(path, len)) { 39 buf = malloc(len + 1); 40 if (!buf) { 41 return -1; 42 } 43 memcpy(buf, path, len - 4); 44 buf[len - 4] = '.'; 45 buf[len - 3] = pub_char(path[len - 3], 'p'); 46 buf[len - 2] = pub_char(path[len - 2], 'u'); 47 buf[len - 1] = pub_char(path[len - 1], 'b'); 48 buf[len] = '\0'; 49 *out = buf; 50 return 0; 51 } 52 buf = malloc(len + 5); 53 if (!buf) { 54 return -1; 55 } 56 memcpy(buf, path, len); 57 memcpy(buf + len, ".pub", 5); 58 *out = buf; 59 return 0; 60 } 61 // }}} 62 63 static int cmd_generate(int argc, const char **argv) { 64 const char *out_file = NULL; 65 const char *format = "hdr"; 66 const char *pub_format; 67 int hash = 0; 68 int single = 0; 69 int force = 0; 70 int path_len = -1; 71 size_t seed_len = CLI_SEED_LEN_DEFAULT; 72 uint8_t *seed = NULL; 73 uint8_t *key_buf = NULL; 74 uint8_t *pub_buf = NULL; 75 char *pub_file = NULL; 76 size_t key_len = 0; 77 size_t pub_len = 0; 78 sfinx_key key; 79 sfinx_status status; 80 int to_stdout; 81 int rc = 1; 82 83 static const char *const usages[] = { 84 "sfinx generate [length] [-L hash] [-p len] [--out-fmt fmt] [--single] [-o file] [-F]", 85 NULL, 86 }; 87 struct argparse_option options[] = { 88 OPT_HELP(), 89 OPT_STRING(0, "out-fmt", &format, "hdr writes one file, raw and hex write a key file and a .pub file", NULL, 0, 90 0), 91 OPT_INTEGER('L', "hash", &hash, "hash size 224/256/384/512", NULL, 0, 0), 92 OPT_INTEGER('p', "path-len", &path_len, "path length the public key is derived for", NULL, 0, 0), 93 OPT_STRING('o', "out", &out_file, "key file, - for stdout", NULL, 0, 0), 94 OPT_BOOLEAN(0, "single", &single, "single-key mode, derive the N = 0 public key", NULL, 0, 0), 95 OPT_BOOLEAN('F', "force", &force, "overwrite existing files", NULL, 0, 0), 96 OPT_END(), 97 }; 98 struct argparse argparse; 99 argparse_init(&argparse, options, usages, 0); 100 argparse_describe(&argparse, "\nGenerate a seed and its public key", NULL); 101 argc = argparse_parse(&argparse, argc, argv); 102 103 sfinx_key_init(&key); 104 105 if (argc > 1) { 106 fprintf(stderr, "sfinx: generate: too many arguments\n"); 107 return 1; 108 } 109 if (single && path_len != -1) { 110 fprintf(stderr, "sfinx: generate: --single cannot be combined with --path-len\n"); 111 return 1; 112 } 113 if (!single) { 114 if (path_len == -1) { 115 path_len = SFINX_PATH_LEN_DEFAULT; 116 } 117 if (path_len <= 0 || !sfinx_path_len_valid((size_t)path_len)) { 118 fprintf(stderr, "sfinx: generate: invalid path length: %d\n", path_len); 119 return 1; 120 } 121 } 122 if (strcmp(format, "hdr") == 0) { 123 pub_format = NULL; 124 } else if (strcmp(format, "raw") == 0) { 125 pub_format = "rawpub"; 126 } else if (strcmp(format, "hex") == 0) { 127 pub_format = "hexpub"; 128 } else { 129 fprintf(stderr, "sfinx: generate: unknown format: %s\n", format); 130 return 1; 131 } 132 if (cli_seed_length(argc == 1 ? argv[0] : NULL, &seed_len) != 0) { 133 fprintf(stderr, "sfinx: generate: invalid length: %s\n", argv[0]); 134 return 1; 135 } 136 137 status = cli_seed_random(seed_len, &seed); 138 if (status != SFINX_OK) { 139 fprintf(stderr, "sfinx: generate: %s\n", sfinx_strerror(status)); 140 return 1; 141 } 142 key.seed = seed; 143 key.seed_len = seed_len; 144 seed = NULL; 145 key.hash = hash != 0 ? (sfinx_hash)hash : SFINX_HASH_DEFAULT; 146 if (sfinx_hash_len(key.hash) == 0) { 147 fprintf(stderr, "sfinx: generate: invalid hash size: %d\n", hash); 148 goto done; 149 } 150 status = cli_key_derive(&key, single, (ssize_t)path_len); 151 if (status != SFINX_OK) { 152 fprintf(stderr, "sfinx: generate: cannot derive the public key: %s\n", sfinx_strerror(status)); 153 goto done; 154 } 155 156 to_stdout = !out_file || strcmp(out_file, "-") == 0; 157 if (pub_format && !to_stdout) { 158 if (cli_pub_path(out_file, &pub_file) != 0) { 159 fprintf(stderr, "sfinx: generate: out of memory\n"); 160 goto done; 161 } 162 } 163 if (!to_stdout && !force) { 164 if (util_file_exists(out_file)) { 165 fprintf(stderr, "sfinx: generate: %s exists\n", out_file); 166 goto done; 167 } 168 if (pub_file && util_file_exists(pub_file)) { 169 fprintf(stderr, "sfinx: generate: %s exists\n", pub_file); 170 goto done; 171 } 172 } 173 174 key_len = sfinx_key_encode_len(&key, format); 175 if (key_len == 0) { 176 fprintf(stderr, "sfinx: generate: cannot encode a key as %s\n", format); 177 goto done; 178 } 179 key_buf = malloc(key_len); 180 if (!key_buf) { 181 fprintf(stderr, "sfinx: generate: out of memory\n"); 182 goto done; 183 } 184 status = sfinx_key_encode(&key, format, key_buf, key_len, &key_len); 185 if (status != SFINX_OK) { 186 fprintf(stderr, "sfinx: generate: cannot encode: %s\n", sfinx_strerror(status)); 187 goto done; 188 } 189 190 if (pub_file) { 191 pub_len = sfinx_key_encode_len(&key, pub_format); 192 pub_buf = malloc(pub_len); 193 if (!pub_buf) { 194 fprintf(stderr, "sfinx: generate: out of memory\n"); 195 goto done; 196 } 197 status = sfinx_key_encode(&key, pub_format, pub_buf, pub_len, &pub_len); 198 if (status != SFINX_OK) { 199 fprintf(stderr, "sfinx: generate: cannot encode the public key: %s\n", sfinx_strerror(status)); 200 goto done; 201 } 202 } 203 204 if (util_file_write(out_file, key_buf, key_len, 0600, force) != 0) { 205 fprintf(stderr, "sfinx: generate: cannot write %s\n", out_file ? out_file : "-"); 206 goto done; 207 } 208 if (pub_buf) { 209 if (util_file_write(pub_file, pub_buf, pub_len, 0644, force) != 0) { 210 fprintf(stderr, "sfinx: generate: cannot write %s\n", pub_file); 211 goto done; 212 } 213 } 214 rc = 0; 215 216 done: 217 free(seed); 218 free(key_buf); 219 free(pub_buf); 220 free(pub_file); 221 sfinx_key_free(&key); 222 return rc; 223 } 224 225 static struct cli_command generate_command = { 226 .names = (const char *const[]){"generate", NULL}, 227 .display = "generate", 228 .description = "Generate a seed and its public key", 229 .help = "sfinx generate [length] [-L hash] [--out-fmt fmt] [--single] [-o file] [-F]", 230 .fn = cmd_generate, 231 }; 232 233 __attribute__((constructor)) static void generate_register(void) { 234 cli_command_register(&generate_command); 235 }