gestumblinde

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

test_fors.c (5249B)


      1 #include "test_fors.h"
      2 
      3 void test_fors_skgen() {
      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_FORS_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_FORS_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_FORS_PK_SEED, tv)) {
     25     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
     26     exit(1);
     27   }
     28 
     29   uint64_t idx = read_key_uint64(KEY_FORS_IDX, tv);
     30 
     31   uint8_t csk[ENN];
     32   if (read_key_array(csk, ENN, KEY_FORS_SK, tv)) {
     33     fprintf(stderr, "Could not read the node from JSON!\n");
     34     exit(1);
     35   }
     36 
     37   uint8_t sk[ENN];
     38   fors_skgen(sk, sk_seed, pk_seed, adrs, idx);
     39 
     40   size_t j;
     41   for (j = 0; j < ENN; j++) {
     42     CU_ASSERT_EQUAL(sk[j], csk[j]);
     43   }
     44 }
     45 
     46 void test_fors_node() {
     47   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
     48 
     49   if (tv == NULL) {
     50     fprintf(stderr, "Could not open JSON test file\n");
     51     exit(1);
     52   }
     53 
     54   uint32_t adrs[ADRS_LEN];
     55   if (read_key_array(adrs, ADRS_LEN * sizeof(*adrs), KEY_FORS_ADDRESS, tv)) {
     56     fprintf(stderr, "Could not read address from JSON!\n");
     57     exit(1);
     58   }
     59 
     60   uint8_t sk_seed[ENN];
     61   if (read_key_array(sk_seed, ENN, KEY_FORS_SK_SEED, tv)) {
     62     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
     63     exit(1);
     64   }
     65 
     66   uint8_t pk_seed[ENN];
     67   if (read_key_array(pk_seed, ENN, KEY_FORS_PK_SEED, tv)) {
     68     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
     69     exit(1);
     70   }
     71 
     72   uint64_t i = read_key_uint64(KEY_FORS_NODEI, tv);
     73   uint64_t z = read_key_uint64(KEY_FORS_NODEZ, tv);
     74 
     75   uint8_t cnode[ENN];
     76   if (read_key_array(cnode, ENN, KEY_FORS_NODE, tv)) {
     77     fprintf(stderr, "Could not read the node from JSON!\n");
     78     exit(1);
     79   }
     80 
     81   uint8_t node[ENN];
     82   fors_node(node, sk_seed, i, z, pk_seed, adrs);
     83 
     84   size_t j;
     85   for (j = 0; j < ENN; j++) {
     86     CU_ASSERT_EQUAL(node[j], cnode[j]);
     87   }
     88 }
     89 
     90 void test_fors_sign() {
     91   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
     92 
     93   if (tv == NULL) {
     94     fprintf(stderr, "Could not open JSON test file\n");
     95     exit(1);
     96   }
     97 
     98   uint32_t adrs[ADRS_LEN];
     99   if (read_key_array(adrs, ADRS_LEN * sizeof(*adrs), KEY_FORS_ADDRESS, tv)) {
    100     fprintf(stderr, "Could not read address from JSON!\n");
    101     exit(1);
    102   }
    103 
    104   uint8_t sk_seed[ENN];
    105   if (read_key_array(sk_seed, ENN, KEY_FORS_SK_SEED, tv)) {
    106     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
    107     exit(1);
    108   }
    109 
    110   uint8_t pk_seed[ENN];
    111   if (read_key_array(pk_seed, ENN, KEY_FORS_PK_SEED, tv)) {
    112     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
    113     exit(1);
    114   }
    115 
    116   uint8_t csig[FORS_SIG_LEN];
    117   if (read_key_array(csig, FORS_SIG_LEN, KEY_FORS_SIGNATURE, tv)) {
    118     fprintf(stderr, "Could not read the signature from JSON!\n");
    119     exit(1);
    120   }
    121 
    122   uint8_t md[FORS_MD_LEN];
    123   if (read_key_array(md, FORS_MD_LEN, KEY_FORS_MD, tv)) {
    124     fprintf(stderr, "Could not read the message digest from JSON!\n");
    125     exit(1);
    126   }
    127 
    128   uint8_t sig[FORS_SIG_LEN];
    129   fors_sign(sig, md, sk_seed, pk_seed, adrs);
    130 
    131   size_t j;
    132   for (j = 0; j < ENN; j++) {
    133     CU_ASSERT_EQUAL(sig[j], csig[j]);
    134   }
    135 }
    136 
    137 void test_fors_pk_from_sig() {
    138   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
    139 
    140   if (tv == NULL) {
    141     fprintf(stderr, "Could not open JSON test file\n");
    142     exit(1);
    143   }
    144 
    145   uint32_t adrs[ADRS_LEN];
    146   if (read_key_array(adrs, ADRS_LEN * sizeof(*adrs), KEY_FORS_ADDRESS, tv)) {
    147     fprintf(stderr, "Could not read address from JSON!\n");
    148     exit(1);
    149   }
    150 
    151   uint8_t sk_seed[ENN];
    152   if (read_key_array(sk_seed, ENN, KEY_FORS_SK_SEED, tv)) {
    153     fprintf(stderr, "Could not read the sk_seed from JSON!\n");
    154     exit(1);
    155   }
    156 
    157   uint8_t pk_seed[ENN];
    158   if (read_key_array(pk_seed, ENN, KEY_FORS_PK_SEED, tv)) {
    159     fprintf(stderr, "Could not read the pk_seed from JSON!\n");
    160     exit(1);
    161   }
    162 
    163   uint8_t sig[FORS_SIG_LEN];
    164   if (read_key_array(sig, FORS_SIG_LEN, KEY_FORS_SIGNATURE, tv)) {
    165     fprintf(stderr, "Could not read the signature from JSON!\n");
    166     exit(1);
    167   }
    168 
    169   uint8_t md[FORS_MD_LEN];
    170   if (read_key_array(md, FORS_MD_LEN, KEY_FORS_MD, tv)) {
    171     fprintf(stderr, "Could not read the message digest from JSON!\n");
    172     exit(1);
    173   }
    174 
    175   uint8_t node[ENN];
    176   if (read_key_array(node, ENN, KEY_FORS_NODE, tv)) {
    177     fprintf(stderr, "Could not read the node from JSON!\n");
    178     exit(1);
    179   }
    180 
    181   // generate the pk from roots from sk_seed
    182   uint8_t roots[K * ENN];
    183   uint8_t *cr = roots;
    184   size_t j;
    185   for (j = 0; j < K; j++) {
    186     fors_node(cr, sk_seed, j, A, pk_seed, adrs);
    187     cr += ENN;
    188   }
    189   uint32_t forspkadrs[ADRS_LEN];
    190   memcpy(forspkadrs, adrs, ADRS_LEN * sizeof(*adrs));
    191   forspkadrs[ADRS_TYPE_IDX] = FORS_ROOTS;
    192   forspkadrs[ADRS_KEYPAIR_IDX] = adrs[ADRS_KEYPAIR_IDX];
    193   forspkadrs[6] = 0;
    194   forspkadrs[7] = 0;
    195 
    196   uint8_t cpk[ENN];
    197   hash_t(cpk, K, pk_seed, forspkadrs, roots);
    198 
    199   uint8_t pk[ENN];
    200   fors_pk_from_sig(pk, sig, md, pk_seed, adrs);
    201 
    202   for (j = 0; j < ENN; j++) {
    203     CU_ASSERT_EQUAL(pk[j], cpk[j]);
    204   }
    205 }