gestumblinde

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

test_wotsp.c (4184B)


      1 #include "test_wotsp.h"
      2 
      3 void test_wotsp_pkgen() {
      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   uint32_t adrs[ADRS_LEN];
     12   if (read_key_array(adrs, ADRS_LEN * sizeof(*adrs), KEY_WOTSP_ADDRESS, tv)) {
     13     fprintf(stderr, "Could not read address from JSON!\n");
     14     exit(1);
     15   }
     16 
     17   uint8_t sk_seed[ENN];
     18   if (read_key_array(sk_seed, ENN, KEY_WOTSP_SK_SEED, tv)) {
     19     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
     20     exit(1);
     21   }
     22 
     23   uint8_t pk_seed[ENN];
     24   if (read_key_array(pk_seed, ENN, KEY_WOTSP_PK_SEED, tv)) {
     25     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
     26     exit(1);
     27   }
     28 
     29   uint8_t cpk[ENN];
     30   if (read_key_array(cpk, ENN, KEY_WOTSP_PUBLIC_KEY, tv)) {
     31     fprintf(stderr, "Could not read the public key from JSON!\n");
     32     exit(1);
     33   }
     34 
     35   uint8_t pk[ENN];
     36   wotsp_pkgen(pk, sk_seed, pk_seed, adrs);
     37 
     38   size_t i;
     39   for (i = 0; i < ENN; i++) {
     40     CU_ASSERT_EQUAL(pk[i], cpk[i]);
     41   }
     42 }
     43 
     44 void test_wotsp_sign() {
     45   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
     46 
     47   if (tv == NULL) {
     48     fprintf(stderr, "Could not open JSON test file\n");
     49     exit(1);
     50   }
     51 
     52   uint32_t adrs[ADRS_LEN];
     53   if (read_key_array(adrs, ADRS_LEN * sizeof(*adrs), KEY_WOTSP_ADDRESS, tv)) {
     54     fprintf(stderr, "Could not read address from JSON!\n");
     55     exit(1);
     56   }
     57 
     58   uint8_t sk_seed[ENN];
     59   if (read_key_array(sk_seed, ENN, KEY_WOTSP_SK_SEED, tv)) {
     60     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
     61     exit(1);
     62   }
     63 
     64   uint8_t pk_seed[ENN];
     65   if (read_key_array(pk_seed, ENN, KEY_WOTSP_PK_SEED, tv)) {
     66     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
     67     exit(1);
     68   }
     69 
     70   uint8_t m[ENN];
     71   if (read_key_array(m, ENN, KEY_WOTSP_MSG, tv)) {
     72     fprintf(stderr, "Could not read the message from JSON!\n");
     73     exit(1);
     74   }
     75 
     76   uint8_t csig[ENN * WOTSP_LEN];
     77   if (read_key_array(csig, ENN * WOTSP_LEN, KEY_WOTSP_SIGNATURE, tv)) {
     78     fprintf(stderr, "Could not read the signature from JSON!\n");
     79     exit(1);
     80   }
     81 
     82   uint8_t sig[ENN * WOTSP_LEN];
     83   wotsp_sign(sig, m, sk_seed, pk_seed, adrs);
     84 
     85   size_t i;
     86   for (i = 0; i < ENN * WOTSP_LEN; i++) {
     87     CU_ASSERT_EQUAL(sig[i], csig[i]);
     88   }
     89 }
     90 
     91 void test_wotsp_verify() {
     92   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
     93 
     94   if (tv == NULL) {
     95     fprintf(stderr, "Could not open JSON test file\n");
     96     exit(1);
     97   }
     98 
     99   uint32_t adrs[ADRS_LEN];
    100   if (read_key_array(adrs, ADRS_LEN * sizeof(*adrs), KEY_WOTSP_ADDRESS, tv)) {
    101     fprintf(stderr, "Could not read address from JSON!\n");
    102     exit(1);
    103   }
    104 
    105   uint8_t sk_seed[ENN];
    106   if (read_key_array(sk_seed, ENN, KEY_WOTSP_SK_SEED, tv)) {
    107     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
    108     exit(1);
    109   }
    110 
    111   uint8_t pk_seed[ENN];
    112   if (read_key_array(pk_seed, ENN, KEY_WOTSP_PK_SEED, tv)) {
    113     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
    114     exit(1);
    115   }
    116 
    117   uint8_t m[ENN];
    118   if (read_key_array(m, ENN, KEY_WOTSP_MSG, tv)) {
    119     fprintf(stderr, "Could not read the message from JSON!\n");
    120     exit(1);
    121   }
    122 
    123   uint8_t sig[ENN * WOTSP_LEN];
    124   if (read_key_array(sig, ENN * WOTSP_LEN, KEY_WOTSP_SIGNATURE, tv)) {
    125     fprintf(stderr, "Could not read the signature from JSON!\n");
    126     exit(1);
    127   }
    128 
    129   uint8_t pk[ENN];
    130   if (read_key_array(pk, ENN, KEY_WOTSP_PUBLIC_KEY, tv)) {
    131     fprintf(stderr, "Could not read the public key from JSON!\n");
    132     exit(1);
    133   }
    134 
    135   uint8_t computed_pk[ENN];
    136 
    137   // compute correct signature
    138   wotsp_pk_from_sig(computed_pk, sig, m, ENN, pk_seed, adrs);
    139   CU_ASSERT_EQUAL(memcmp(pk, computed_pk, ENN), 0);
    140 
    141   // verify fails with bitflipped signature
    142   uint8_t corrupted_sig[ENN * WOTSP_LEN];
    143   memcpy(corrupted_sig, sig, ENN * WOTSP_LEN);
    144   corrupted_sig[3] ^= 0x10;
    145   wotsp_pk_from_sig(computed_pk, corrupted_sig, m, ENN, pk_seed, adrs);
    146   CU_ASSERT_NOT_EQUAL(memcmp(pk, computed_pk, ENN), 0);
    147 
    148   // verify fails with bitflipped message
    149   uint8_t corrupted_msg[ENN];
    150   memcpy(corrupted_msg, m, ENN);
    151   corrupted_msg[3] ^= 0x04;
    152   wotsp_pk_from_sig(computed_pk, sig, corrupted_msg, ENN, pk_seed, adrs);
    153   CU_ASSERT_NOT_EQUAL(memcmp(pk, computed_pk, ENN), 0);
    154 }