sfinx

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

verify.c (6066B)


      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_verify(int argc, const char **argv) {
     13   const char  *key_file     = NULL;
     14   const char  *key_format   = NULL;
     15   const char  *format       = NULL;
     16   const char  *signature    = NULL;
     17   const char  *sig_file     = NULL;
     18   const char  *message      = NULL;
     19   const char  *msg_file     = NULL;
     20   int          hash         = 0;
     21   int          path_len     = -1;
     22   uint8_t     *key_data     = NULL;
     23   uint8_t     *msg_data     = NULL;
     24   uint8_t     *sig_data     = NULL;
     25   uint8_t     *raw_sig      = NULL;
     26   size_t       key_len      = 0;
     27   size_t       msg_len      = 0;
     28   size_t       sig_data_len = 0;
     29   size_t       raw_len      = 0;
     30   sfinx_hash   sig_hash;
     31   size_t       sig_path;
     32   sfinx_key    key;
     33   sfinx_status status;
     34   int          rc = 1;
     35 
     36   static const char *const usages[] = {
     37       "sfinx verify -k keyfile [-L hash] [-p len] [-s signature | -S signature-file] [-m message | -M message-file] "
     38       "[--key-fmt fmt] [--sig-fmt fmt]",
     39       NULL,
     40   };
     41   struct argparse_option options[] = {
     42       OPT_HELP(),
     43       OPT_STRING('k', "key-file", &key_file, "public key file", NULL, 0, 0),
     44       OPT_STRING(0, "key-fmt", &key_format, "key format, auto by default", NULL, 0, 0),
     45       OPT_STRING(0, "sig-fmt", &format, "signature format, auto by default", NULL, 0, 0),
     46       OPT_INTEGER('L', "hash", &hash, "expected hash size", NULL, 0, 0),
     47       OPT_INTEGER('p', "path-len", &path_len, "path length the public key is for", NULL, 0, 0),
     48       OPT_STRING('s', "signature", &signature, "signature as hex", NULL, 0, 0),
     49       OPT_STRING('S', "signature-file", &sig_file, "signature file, - for stdin", NULL, 0, 0),
     50       OPT_STRING('m', "message", &message, "message string", NULL, 0, 0),
     51       OPT_STRING('M', "message-file", &msg_file, "message file, - for stdin", NULL, 0, 0),
     52       OPT_END(),
     53   };
     54   struct argparse argparse;
     55   argparse_init(&argparse, options, usages, 0);
     56   argparse_describe(&argparse, "\nVerify a message signature", NULL);
     57   argc = argparse_parse(&argparse, argc, argv);
     58 
     59   sfinx_key_init(&key);
     60 
     61   if (argc > 0) {
     62     fprintf(stderr, "sfinx: verify: unexpected argument: %s\n", argv[0]);
     63     return 1;
     64   }
     65   if (!key_file) {
     66     fprintf(stderr, "sfinx: verify: missing --key-file\n");
     67     return 1;
     68   }
     69   if (signature && sig_file) {
     70     fprintf(stderr, "sfinx: verify: --signature and --signature-file are exclusive\n");
     71     return 1;
     72   }
     73   if (message && msg_file) {
     74     fprintf(stderr, "sfinx: verify: --message and --message-file are exclusive\n");
     75     return 1;
     76   }
     77 
     78   int msg_stdin = !message && (!msg_file || strcmp(msg_file, "-") == 0);
     79   int sig_stdin = !signature && (!sig_file || strcmp(sig_file, "-") == 0);
     80   if (msg_stdin && sig_stdin) {
     81     fprintf(stderr, "sfinx: verify: message and signature cannot both come from stdin\n");
     82     return 1;
     83   }
     84 
     85   if (util_file_read(key_file, &key_data, &key_len) != 0) {
     86     fprintf(stderr, "sfinx: verify: cannot read %s\n", key_file);
     87     goto done;
     88   }
     89   status = sfinx_key_decode(key_data, key_len, key_format, &key);
     90   if (status != SFINX_OK && !key_format) {
     91     status = sfinx_key_decode(key_data, key_len, "rawpub", &key);
     92   }
     93   if (status != SFINX_OK) {
     94     fprintf(stderr, "sfinx: verify: cannot decode %s: %s\n", key_file, sfinx_strerror(status));
     95     goto done;
     96   }
     97   if (hash != 0) {
     98     key.hash = (sfinx_hash)hash;
     99   }
    100 
    101   if (cli_read_message(message, msg_file, &msg_data, &msg_len) != 0) {
    102     fprintf(stderr, "sfinx: verify: cannot read the message\n");
    103     goto done;
    104   }
    105 
    106   if (signature) {
    107     if (cli_hex_decode(signature, &raw_sig, &raw_len) != 0) {
    108       fprintf(stderr, "sfinx: verify: invalid signature hex\n");
    109       goto done;
    110     }
    111   } else {
    112     if (util_file_read(sig_file, &sig_data, &sig_data_len) != 0) {
    113       fprintf(stderr, "sfinx: verify: cannot read the signature\n");
    114       goto done;
    115     }
    116     raw_sig = malloc(sig_data_len + 1);
    117     if (!raw_sig) {
    118       fprintf(stderr, "sfinx: verify: out of memory\n");
    119       goto done;
    120     }
    121     status = sfinx_sig_decode(sig_data, sig_data_len, format, raw_sig, sig_data_len, &raw_len);
    122     if (status != SFINX_OK) {
    123       fprintf(stderr, "sfinx: verify: cannot decode the signature: %s\n", sfinx_strerror(status));
    124       goto done;
    125     }
    126   }
    127 
    128   if (sfinx_signature_info(raw_sig, raw_len, &sig_hash, &sig_path) != SFINX_OK) {
    129     fprintf(stderr, "sfinx: verify: invalid signature\n");
    130     goto done;
    131   }
    132   if (hash != 0 && (int)sig_hash != hash) {
    133     fprintf(stderr, "sfinx: verify: signature is not %d-bit\n", hash);
    134     goto done;
    135   }
    136   if (path_len != -1) {
    137     key.path_len = (ssize_t)path_len;
    138   }
    139   if (key.seed) {
    140     if (cli_key_derive(&key, sig_path == 0, (ssize_t)sig_path) != SFINX_OK) {
    141       fprintf(stderr, "sfinx: verify: cannot derive the public key\n");
    142       goto done;
    143     }
    144   } else if (key.path_len >= 0 && (size_t)key.path_len != sig_path) {
    145     fprintf(stderr, "sfinx: verify: public key is for path length %zd, signature uses %zu\n", key.path_len, sig_path);
    146     goto done;
    147   }
    148   if (!key.pub) {
    149     fprintf(stderr, "sfinx: verify: key has no public key\n");
    150     goto done;
    151   }
    152 
    153   if (sfinx_verify(key.pub, key.pub_len, msg_data, msg_len, raw_sig, raw_len) == SFINX_OK) {
    154     fprintf(stdout, "OK\n");
    155     rc = 0;
    156   } else {
    157     fprintf(stdout, "FAIL\n");
    158     rc = 1;
    159   }
    160 
    161 done:
    162   free(key_data);
    163   free(msg_data);
    164   free(sig_data);
    165   free(raw_sig);
    166   sfinx_key_free(&key);
    167   return rc;
    168 }
    169 
    170 static struct cli_command verify_command = {
    171     .names       = (const char *const[]){"verify", NULL},
    172     .display     = "verify",
    173     .description = "Verify a message signature",
    174     .help =
    175         "sfinx verify -k keyfile [-L hash] [-s signature | -S signature-file] [-m message | -M message-file] "
    176         "[--key-fmt fmt] [--sig-fmt fmt]",
    177     .fn = cmd_verify,
    178 };
    179 
    180 __attribute__((constructor)) static void verify_register(void) {
    181   cli_command_register(&verify_command);
    182 }