gestumblinde

Gestumblinde - reference implementation of SLH-DSA
git clone git://www.tkruger.se/gestumblinde.git
Log | Files | Refs | README

test_ht.c (3231B)


      1 #include "test_ht.h"
      2 
      3 void test_ht_sign() {
      4   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
      5 
      6   if (tv == NULL) {
      7     fprintf(stderr, "Could not open JSON test file\n");
      8     exit(1);
      9   }
     10 
     11   uint8_t sk_seed[ENN];
     12   if (read_key_array(sk_seed, ENN, KEY_HT_SK_SEED, tv)) {
     13     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
     14     exit(1);
     15   }
     16 
     17   uint8_t pk_seed[ENN];
     18   if (read_key_array(pk_seed, ENN, KEY_HT_PK_SEED, tv)) {
     19     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
     20     exit(1);
     21   }
     22 
     23   idx_tree_t idx_tree = read_key_idx_tree(KEY_HT_IDX_TREE, tv);
     24   uint64_t idx_leaf = read_key_uint64(KEY_HT_IDX_LEAF, tv);
     25 
     26   uint8_t msg[ENN];
     27   if (read_key_array(msg, ENN, KEY_HT_MSG, tv)) {
     28     fprintf(stderr, "Could not read the node from JSON!\n");
     29     exit(1);
     30   }
     31 
     32   uint8_t csig[(H + D * WOTSP_LEN) * ENN];
     33   if (read_key_array(csig, (H + D * WOTSP_LEN) * ENN, KEY_HT_SIGNATURE, tv)) {
     34     fprintf(stderr, "Could not read the node from JSON!\n");
     35     exit(1);
     36   }
     37 
     38   uint8_t sig[(H + D * WOTSP_LEN) * ENN];
     39   ht_sign(sig, msg, sk_seed, pk_seed, idx_tree, idx_leaf);
     40 
     41   size_t j;
     42   for (j = 0; j < (H + D * WOTSP_LEN) * ENN; j++) {
     43     CU_ASSERT_EQUAL(sig[j], csig[j]);
     44   }
     45 }
     46 
     47 void test_ht_verify() {
     48   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
     49 
     50   if (tv == NULL) {
     51     fprintf(stderr, "Could not open JSON test file\n");
     52     exit(1);
     53   }
     54 
     55   uint8_t sk_seed[ENN];
     56   if (read_key_array(sk_seed, ENN, KEY_HT_SK_SEED, tv)) {
     57     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
     58     exit(1);
     59   }
     60 
     61   uint8_t pk_seed[ENN];
     62   if (read_key_array(pk_seed, ENN, KEY_HT_PK_SEED, tv)) {
     63     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
     64     exit(1);
     65   }
     66 
     67   idx_tree_t idx_tree = read_key_idx_tree(KEY_HT_IDX_TREE, tv);
     68   uint64_t idx_leaf = read_key_uint64(KEY_HT_IDX_LEAF, tv);
     69 
     70   uint8_t msg[ENN];
     71   if (read_key_array(msg, ENN, KEY_HT_MSG, tv)) {
     72     fprintf(stderr, "Could not read the node from JSON!\n");
     73     exit(1);
     74   }
     75 
     76   uint8_t sig[(H + D * WOTSP_LEN) * ENN];
     77   if (read_key_array(sig, (H + D * WOTSP_LEN) * ENN, KEY_HT_SIGNATURE, tv)) {
     78     fprintf(stderr, "Could not read the node from JSON!\n");
     79     exit(1);
     80   }
     81 
     82   uint8_t pk_root[ENN];
     83   uint32_t adrs[ADRS_LEN] = {0};
     84   adrs[ADRS_LAYER_ADDRESS_IDX] = htobe32(D - 1);
     85   xmss_node(pk_root, sk_seed, 0, HP, pk_seed, adrs);
     86 
     87   // try valid (sig,msg) pair
     88   int res = ht_verify(msg, sig, pk_seed, idx_tree, idx_leaf, pk_root);
     89   CU_ASSERT_EQUAL(res, 1);
     90 
     91   // flip a bit in the signature
     92   uint8_t sigf[(H + D * WOTSP_LEN) * ENN];
     93   memcpy(sigf, sig, (H + D * WOTSP_LEN) * ENN);
     94   sigf[3] ^= 0x40;
     95   res = ht_verify(msg, sigf, pk_seed, idx_tree, idx_leaf, pk_root);
     96   CU_ASSERT_EQUAL(res, 0);
     97 
     98   // flip a bit in the message
     99   uint8_t msgf[ENN];
    100   memcpy(msgf, msg, ENN);
    101   msgf[4] ^= 0x02;
    102   res = ht_verify(msgf, sig, pk_seed, idx_tree, idx_leaf, pk_root);
    103   CU_ASSERT_EQUAL(res, 0);
    104 
    105   // wrong tree index
    106   idx_tree_t iff = idx_tree;
    107   iff.vallb ^= 0x10;
    108   res = ht_verify(msg, sig, pk_seed, iff, idx_leaf, pk_root);
    109   CU_ASSERT_EQUAL(res, 0);
    110 
    111   // wrong leaf index
    112   uint64_t lff = idx_leaf;
    113   lff ^= 0x80;
    114   res = ht_verify(msg, sig, pk_seed, idx_tree, lff, pk_root);
    115   CU_ASSERT_EQUAL(res, 0);
    116 }