sfinx

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

07-key.c (4603B)


      1 #include <string.h>
      2 
      3 #include "_tap.c"
      4 #include "src/sfinx.c"
      5 
      6 int main(void) {
      7   static const uint8_t seed[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
      8                                    0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
      9                                    0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
     10   static const uint8_t badhdr[] = "public-key: zzzz\n";
     11   uint8_t              hdr[4096];
     12   size_t               hdr_len = 0;
     13   size_t               need    = 0;
     14   sfinx_key            key;
     15   sfinx_key            other;
     16   sfinx_key            badkey;
     17   sfinx_key            pubkey;
     18   sfinx_status         status;
     19 
     20   tap_begin("sfinx key");
     21 
     22   sfinx_key_init(&key);
     23   tap(key.hash == SFINX_HASH_DEFAULT, "init sets the default hash", "got %d", (int)key.hash);
     24   tap(key.path_len == -1, "init leaves the path length unrecorded", "got %zd", key.path_len);
     25   tap(key.seed == NULL && key.pub == NULL, "init zeroes both halves", "not zeroed");
     26 
     27   status = sfinx_key_decode(seed, sizeof(seed), "raw", &key);
     28   tap(status == SFINX_OK, "decode raw", "%s", sfinx_strerror(status));
     29   tap(key.seed_len == sizeof(seed), "raw seed length", "got %zu", key.seed_len);
     30   tap(key.pub == NULL, "raw has no pub yet", "pub present");
     31 
     32   key.path_len = 3;
     33   status       = sfinx_key_derive(&key);
     34   tap(status == SFINX_OK, "derive", "%s", sfinx_strerror(status));
     35   tap(key.pub_len == 32, "derived pub length", "got %zu", key.pub_len);
     36 
     37   need = sfinx_key_encode_len(&key, "hdr");
     38   tap(need > 0, "hdr encode_len", "got %zu", need);
     39   status = sfinx_key_encode(&key, "hdr", hdr, sizeof(hdr), &hdr_len);
     40   tap(status == SFINX_OK, "encode hdr", "%s", sfinx_strerror(status));
     41   tap(hdr_len == need, "hdr length matches encode_len", "got %zu want %zu", hdr_len, need);
     42 
     43   status = sfinx_key_decode(hdr, hdr_len, NULL, &other);
     44   tap(status == SFINX_OK, "decode hdr auto", "%s", sfinx_strerror(status));
     45   tap(other.pub_len == key.pub_len && memcmp(other.pub, key.pub, key.pub_len) == 0, "hdr pub matches", "mismatch");
     46   tap(other.seed_len == key.seed_len && memcmp(other.seed, key.seed, key.seed_len) == 0, "hdr seed matches",
     47       "mismatch");
     48   tap(other.path_len == key.path_len, "hdr path-len matches", "got %zd want %zd", other.path_len, key.path_len);
     49 
     50   free(other.seed);
     51   other.seed     = NULL;
     52   other.seed_len = 0;
     53   need           = sfinx_key_encode_len(&other, "raw");
     54   tap(need == other.pub_len, "raw pub encode_len", "got %zu", need);
     55 
     56   status = sfinx_key_encode(&key, "hdr", hdr, 1, &hdr_len);
     57   tap(status == SFINX_ERR_NOSPACE, "hdr encode rejects a small cap", "got %s", sfinx_strerror(status));
     58 
     59   sfinx_key_init(&badkey);
     60   status = sfinx_key_decode(seed, sizeof(seed), "nope", &badkey);
     61   tap(status == SFINX_ERR_FORMAT, "decode rejects an unknown format", "got %s", sfinx_strerror(status));
     62   status = sfinx_key_decode(badhdr, sizeof(badhdr) - 1, NULL, &badkey);
     63   tap(status == SFINX_ERR_FORMAT, "decode rejects bad hex", "got %s", sfinx_strerror(status));
     64 
     65   sfinx_key_init(&pubkey);
     66   need = sfinx_key_encode_len(&key, "rawpub");
     67   tap(need == key.pub_len, "rawpub encode_len", "got %zu", need);
     68   status = sfinx_key_encode(&key, "rawpub", hdr, sizeof(hdr), &hdr_len);
     69   tap(status == SFINX_OK, "rawpub encode", "%s", sfinx_strerror(status));
     70   status = sfinx_key_decode(hdr, hdr_len, "rawpub", &pubkey);
     71   tap(status == SFINX_OK && pubkey.pub_len == key.pub_len && memcmp(pubkey.pub, key.pub, key.pub_len) == 0,
     72       "rawpub round trip", "mismatch");
     73   sfinx_key_free(&pubkey);
     74 
     75   sfinx_key_init(&pubkey);
     76   need = sfinx_key_encode_len(&key, "hexpub");
     77   tap(need == key.pub_len * 2, "hexpub encode_len", "got %zu", need);
     78   status = sfinx_key_encode(&key, "hexpub", hdr, sizeof(hdr), &hdr_len);
     79   tap(status == SFINX_OK, "hexpub encode", "%s", sfinx_strerror(status));
     80   status = sfinx_key_decode(hdr, hdr_len, "hexpub", &pubkey);
     81   tap(status == SFINX_OK && pubkey.pub_len == key.pub_len && memcmp(pubkey.pub, key.pub, key.pub_len) == 0,
     82       "hexpub round trip", "mismatch");
     83   tap(pubkey.path_len == -1, "hexpub records no path length", "got %zd", pubkey.path_len);
     84   sfinx_key_free(&pubkey);
     85 
     86   sfinx_key_init(&pubkey);
     87   status = sfinx_key_decode(seed, sizeof(seed), "raw", &pubkey);
     88   tap(status == SFINX_OK && sfinx_key_derive(&pubkey) == SFINX_OK && pubkey.path_len == SFINX_PATH_LEN_DEFAULT,
     89       "an unrecorded path length derives at the default", "got %zd", pubkey.path_len);
     90   sfinx_key_free(&pubkey);
     91 
     92   sfinx_key_free(&key);
     93   sfinx_key_free(&other);
     94   sfinx_key_free(&badkey);
     95   return tap_plan();
     96 }