sfinx

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

common.c (2531B)


      1 #include "common.h"
      2 
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include "sfinx.h"
      7 #include "util/file.h"
      8 
      9 int cli_read_message(const char *message, const char *file, uint8_t **out, size_t *len_out) {
     10   uint8_t *buf;
     11   size_t   len;
     12   if (message) {
     13     len = strlen(message);
     14     buf = malloc(len + 1);
     15     if (!buf) return -1;
     16     memcpy(buf, message, len + 1);
     17     *out     = buf;
     18     *len_out = len;
     19     return 0;
     20   }
     21   return util_file_read(file, out, len_out);
     22 }
     23 
     24 int cli_seed_length(const char *text, size_t *len_out) {
     25   char         *end = NULL;
     26   unsigned long v;
     27   if (!text) {
     28     *len_out = CLI_SEED_LEN_DEFAULT;
     29     return 0;
     30   }
     31   v = strtoul(text, &end, 10);
     32   if (!end || *end != '\0' || v == 0 || v > SFINX_SEED_LEN_MAX) {
     33     return -1;
     34   }
     35   *len_out = (size_t)v;
     36   return 0;
     37 }
     38 
     39 sfinx_status cli_seed_random(size_t len, uint8_t **out) {
     40   uint8_t     *seed = malloc(len);
     41   sfinx_status status;
     42   if (!seed) {
     43     return SFINX_ERR_NOSPACE;
     44   }
     45   status = sfinx_random_bytes(seed, len);
     46   if (status != SFINX_OK) {
     47     free(seed);
     48     return status;
     49   }
     50   *out = seed;
     51   return SFINX_OK;
     52 }
     53 
     54 sfinx_status cli_key_derive(sfinx_key *key, int single, ssize_t path_len) {
     55   size_t       len;
     56   uint8_t     *pub;
     57   sfinx_status status;
     58 
     59   if (!key->seed) {
     60     return sfinx_key_derive(key);
     61   }
     62   len = sfinx_hash_len(key->hash);
     63   if (len == 0) {
     64     return SFINX_ERR_ARGS;
     65   }
     66   free(key->pub);
     67   key->pub     = NULL;
     68   key->pub_len = 0;
     69   if (!single) {
     70     key->path_len = path_len;
     71     return sfinx_key_derive(key);
     72   }
     73   key->path_len = 0;
     74   pub           = malloc(len);
     75   if (!pub) {
     76     return SFINX_ERR_NOSPACE;
     77   }
     78   status = sfinx_single_public_key(key->hash, key->seed, key->seed_len, pub, len, NULL);
     79   if (status != SFINX_OK) {
     80     free(pub);
     81     return status;
     82   }
     83   key->pub     = pub;
     84   key->pub_len = len;
     85   return SFINX_OK;
     86 }
     87 
     88 static int hexval(int c) {
     89   if (c >= '0' && c <= '9') return c - '0';
     90   if (c >= 'a' && c <= 'f') return c - 'a' + 10;
     91   if (c >= 'A' && c <= 'F') return c - 'A' + 10;
     92   return -1;
     93 }
     94 
     95 int cli_hex_decode(const char *hex, uint8_t **out, size_t *len_out) {
     96   size_t   len = strlen(hex);
     97   size_t   n;
     98   uint8_t *buf;
     99   if (len % 2 != 0) return -1;
    100   n   = len / 2;
    101   buf = malloc(n + 1);
    102   if (!buf) return -1;
    103   for (size_t i = 0; i < n; i++) {
    104     int hi = hexval(hex[i * 2]);
    105     int lo = hexval(hex[i * 2 + 1]);
    106     if (hi < 0 || lo < 0) {
    107       free(buf);
    108       return -1;
    109     }
    110     buf[i] = (uint8_t)((hi << 4) | lo);
    111   }
    112   *out     = buf;
    113   *len_out = n;
    114   return 0;
    115 }