sfinx

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

09-ref.c (4015B)


      1 #include <string.h>
      2 
      3 #include "_tap.c"
      4 #include "ref/sfinx_ref.c"
      5 #include "src/sfinx.c"
      6 
      7 #define MSG     "sfinx reference vector"
      8 #define MSG_LEN (sizeof(MSG) - 1)
      9 
     10 static const uint8_t SEED[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
     11                                  0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
     12                                  0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
     13 static const uint8_t PATH1[1] = {0x0a};
     14 static const uint8_t PATH2[2] = {0x0a, 0x1b};
     15 
     16 static void run_tree(int bits, sfinx_hash hash, size_t len, const uint8_t *path, size_t path_len) {
     17   uint8_t lib_pub[SFINX_HASH_LEN_MAX];
     18   uint8_t ref_pub[SFINX_HASH_LEN_MAX];
     19   uint8_t lib_sig[16384];
     20   uint8_t ref_sig[16384];
     21   uint8_t tamper[16384];
     22   size_t  n = sfinx_signature_len(hash, path_len);
     23   char    label[96];
     24 
     25   sfinx_tree_public_key(hash, SEED, sizeof(SEED), path_len, lib_pub, sizeof(lib_pub), NULL);
     26   ref_tree_pub(bits, SEED, sizeof(SEED), path_len, ref_pub);
     27   snprintf(label, sizeof(label), "L%zu N%zu tree pub matches reference", len, path_len);
     28   tap(memcmp(lib_pub, ref_pub, len) == 0, label, "differs");
     29 
     30   sfinx_tree_sign(hash, SEED, sizeof(SEED), path, path_len, (const uint8_t *)MSG, MSG_LEN, lib_sig, sizeof(lib_sig),
     31                   NULL);
     32   ref_tree_sign(bits, SEED, sizeof(SEED), path, path_len, (const uint8_t *)MSG, MSG_LEN, ref_sig);
     33 
     34   snprintf(label, sizeof(label), "L%zu N%zu signature matches reference", len, path_len);
     35   tap(memcmp(lib_sig, ref_sig, n) == 0, label, "differs");
     36 
     37   snprintf(label, sizeof(label), "L%zu N%zu library verifies the reference", len, path_len);
     38   tap(sfinx_verify(lib_pub, len, (const uint8_t *)MSG, MSG_LEN, ref_sig, n) == SFINX_OK, label, "rejected");
     39 
     40   snprintf(label, sizeof(label), "L%zu N%zu reference verifies the library", len, path_len);
     41   tap(ref_verify(lib_pub, len, (const uint8_t *)MSG, MSG_LEN, lib_sig, n) != 0, label, "rejected");
     42 
     43   memcpy(tamper, ref_sig, n);
     44   tamper[2 + path_len] ^= 0x01;
     45   snprintf(label, sizeof(label), "L%zu N%zu tampered reference fails", len, path_len);
     46   tap(sfinx_verify(lib_pub, len, (const uint8_t *)MSG, MSG_LEN, tamper, n) != SFINX_OK, label, "accepted");
     47 
     48   memcpy(tamper, lib_sig, n);
     49   tamper[n - 1] ^= 0x01;
     50   snprintf(label, sizeof(label), "L%zu N%zu tampered library fails", len, path_len);
     51   tap(ref_verify(lib_pub, len, (const uint8_t *)MSG, MSG_LEN, tamper, n) == 0, label, "accepted");
     52 
     53   memcpy(tamper, lib_sig, n);
     54   ref_e4m4_encode((uint32_t)(len == 32 ? 64 : 32), tamper + 1 + path_len);
     55   snprintf(label, sizeof(label), "L%zu N%zu reference rejects a wrong E4M4(L)", len, path_len);
     56   tap(ref_verify(lib_pub, len, (const uint8_t *)MSG, MSG_LEN, tamper, n) == 0, label, "accepted");
     57 }
     58 
     59 static void run_bits(int bits) {
     60   sfinx_hash hash = (sfinx_hash)bits;
     61   size_t     len  = sfinx_hash_len(hash);
     62   uint8_t    lib_pub[SFINX_HASH_LEN_MAX];
     63   uint8_t    ref_pub[SFINX_HASH_LEN_MAX];
     64   uint8_t    lib_sig[16384];
     65   uint8_t    ref_sig[16384];
     66   size_t     n;
     67   char       label[96];
     68 
     69   sfinx_single_public_key(hash, SEED, sizeof(SEED), lib_pub, sizeof(lib_pub), NULL);
     70   ref_single_pub(bits, SEED, sizeof(SEED), ref_pub);
     71   snprintf(label, sizeof(label), "L%zu single pub matches reference", len);
     72   tap(memcmp(lib_pub, ref_pub, len) == 0, label, "differs");
     73 
     74   n = sfinx_signature_len(hash, 0);
     75   sfinx_single_sign(hash, SEED, sizeof(SEED), (const uint8_t *)MSG, MSG_LEN, lib_sig, sizeof(lib_sig), NULL);
     76   ref_single_sign(bits, SEED, sizeof(SEED), (const uint8_t *)MSG, MSG_LEN, ref_sig);
     77   snprintf(label, sizeof(label), "L%zu single signature matches reference", len);
     78   tap(memcmp(lib_sig, ref_sig, n) == 0, label, "differs");
     79 
     80   run_tree(bits, hash, len, PATH1, sizeof(PATH1));
     81   if (bits == 224 || bits == 256) {
     82     run_tree(bits, hash, len, PATH2, sizeof(PATH2));
     83   }
     84 }
     85 
     86 int main(void) {
     87   tap_begin("sfinx reference differential");
     88 
     89   run_bits(224);
     90   run_bits(256);
     91   run_bits(384);
     92   run_bits(512);
     93 
     94   return tap_plan();
     95 }