sfinx

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

12-tweak.c (4059B)


      1 #include <string.h>
      2 
      3 #include "_tap.c"
      4 #include "src/sfinx.c"
      5 
      6 #define MSG     "sfinx tweak vector"
      7 #define MSG_LEN (sizeof(MSG) - 1)
      8 
      9 static void step(sfinx_hash hash, const uint8_t *kid, uint8_t index, uint8_t from, uint8_t *out) {
     10   uint8_t node[SFINX_HASH_LEN_MAX];
     11   for (size_t i = 0; i < sfinx_hash_len(hash); i++) {
     12     node[i] = (uint8_t)(i + 1);
     13   }
     14   _sfinx_wots_chain(hash, node, kid, index, from, 1);
     15   memcpy(out, node, sfinx_hash_len(hash));
     16 }
     17 
     18 static void kid_for(sfinx_hash hash, size_t path_len, const uint8_t *path, size_t k, uint8_t *out) {
     19   uint8_t suffix[SFINX_HASH_LEN_MAX];
     20   _sfinx_e4m4_encode((uint32_t)path_len, suffix);
     21   for (size_t i = 0; i < k; i++) {
     22     suffix[1 + i] = path[i];
     23   }
     24   _sfinx_kid(hash, suffix, k + 1, out);
     25 }
     26 
     27 static void roundtrip(sfinx_hash hash, size_t path_len, const char *label) {
     28   static const uint8_t seed[] = "sfinx tweak seed";
     29   uint8_t              path[32];
     30   uint8_t              pub[SFINX_HASH_LEN_MAX];
     31   uint8_t             *sig;
     32   size_t               seed_len = strlen((const char *)seed);
     33   size_t               n        = sfinx_signature_len(hash, path_len);
     34   size_t               got      = 0;
     35   char                 name[96];
     36 
     37   for (size_t i = 0; i < sizeof(path); i++) {
     38     path[i] = (uint8_t)(i * 3 + 1);
     39   }
     40   sig = malloc(n);
     41   if (!sig) {
     42     tap(0, label, "out of memory");
     43     return;
     44   }
     45 
     46   if (path_len == 0) {
     47     sfinx_single_public_key(hash, seed, seed_len, pub, sizeof(pub), &got);
     48     sfinx_single_sign(hash, seed, seed_len, (const uint8_t *)MSG, MSG_LEN, sig, n, NULL);
     49   } else {
     50     sfinx_tree_public_key(hash, seed, seed_len, path_len, pub, sizeof(pub), &got);
     51     sfinx_tree_sign(hash, seed, seed_len, path, path_len, (const uint8_t *)MSG, MSG_LEN, sig, n, NULL);
     52   }
     53 
     54   snprintf(name, sizeof(name), "%s verifies", label);
     55   tap(sfinx_verify(pub, got, (const uint8_t *)MSG, MSG_LEN, sig, n) == SFINX_OK, name, "verify failed");
     56 
     57   sig[n / 2] ^= 0x01;
     58   snprintf(name, sizeof(name), "%s rejects a tampered signature", label);
     59   tap(sfinx_verify(pub, got, (const uint8_t *)MSG, MSG_LEN, sig, n) != SFINX_OK, name, "verify passed");
     60 
     61   free(sig);
     62 }
     63 
     64 int main(void) {
     65   static const sfinx_hash hashes[] = {SFINX_HASH_224, SFINX_HASH_256, SFINX_HASH_384, SFINX_HASH_512};
     66   static const char      *sizes[]  = {"224", "256", "384", "512"};
     67   uint8_t                 kid[SFINX_HASH_LEN_MAX];
     68   uint8_t                 other[SFINX_HASH_LEN_MAX];
     69   uint8_t                 a[SFINX_HASH_LEN_MAX];
     70   uint8_t                 b[SFINX_HASH_LEN_MAX];
     71   uint8_t                 path1[] = {0x0a, 0x1b};
     72   uint8_t                 path2[] = {0x0a, 0x2c};
     73   size_t                  len     = sfinx_hash_len(SFINX_HASH_256);
     74   size_t                  k       = _sfinx_kid_len(SFINX_HASH_256);
     75 
     76   tap_begin("sfinx chain tweak");
     77 
     78   _sfinx_kid(SFINX_HASH_256, (const uint8_t *)"\x01\x0a", 2, kid);
     79 
     80   memcpy(other, kid, k);
     81   other[0] ^= 0x01;
     82   step(SFINX_HASH_256, kid, 3, 0, a);
     83   step(SFINX_HASH_256, other, 3, 0, b);
     84   tap(memcmp(a, b, len) != 0, "a different key id changes the step", "outputs match");
     85 
     86   step(SFINX_HASH_256, kid, 3, 0, a);
     87   step(SFINX_HASH_256, kid, 4, 0, b);
     88   tap(memcmp(a, b, len) != 0, "a different chain index changes the step", "outputs match");
     89 
     90   step(SFINX_HASH_256, kid, 3, 0, a);
     91   step(SFINX_HASH_256, kid, 3, 1, b);
     92   tap(memcmp(a, b, len) != 0, "a different step index changes the step", "outputs match");
     93 
     94   kid_for(SFINX_HASH_256, 2, path1, 2, a);
     95   kid_for(SFINX_HASH_256, 2, path2, 2, b);
     96   tap(memcmp(a, b, k) != 0, "different paths give different key ids", "key ids match");
     97 
     98   kid_for(SFINX_HASH_256, 2, path1, 1, a);
     99   kid_for(SFINX_HASH_256, 3, path1, 1, b);
    100   tap(memcmp(a, b, k) != 0, "the same path at a different N gives a different key id", "key ids match");
    101 
    102   for (size_t h = 0; h < 4; h++) {
    103     char label[64];
    104     for (size_t p = 0; p < 3; p++) {
    105       snprintf(label, sizeof(label), "L%s N%zu", sizes[h], p);
    106       roundtrip(hashes[h], p, label);
    107     }
    108   }
    109   roundtrip(SFINX_HASH_256, 32, "L256 N32");
    110 
    111   return tap_plan();
    112 }