gestumblinde

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

test_slh.c (2900B)


      1 #include "test_slh.h"
      2 
      3 void test_slh_keygen() {
      4   // just run it, enough not to crash
      5   uint8_t sk[4 * ENN];
      6   uint8_t pk[2 * ENN];
      7 
      8   slh_keygen(sk, pk);
      9 }
     10 
     11 void test_slh_sign() {
     12   json_t *tv = json_load_file(TEST_FILENAME_JSON, 0, NULL);
     13 
     14   if (tv == NULL) {
     15     fprintf(stderr, "Could not open JSON test file\n");
     16     exit(1);
     17   }
     18 
     19   uint8_t sk[4 * ENN];
     20   if (read_key_aoa(sk, 4 * ENN, KEY_SLH_SK, tv)) {
     21     fprintf(stderr, "Could not read the secret key from JSON!\n");
     22     exit(1);
     23   }
     24 
     25   uint8_t csig[SLH_SIGNATURE_LEN];
     26   if (read_key_array(csig, SLH_SIGNATURE_LEN, KEY_SLH_SIGNATURE, tv)) {
     27     fprintf(stderr, "Could not read the signature from JSON!\n");
     28     exit(1);
     29   }
     30 
     31   uint8_t msg[32];
     32   if (read_key_array(msg, 32, KEY_SLH_MSG, tv)) {
     33     fprintf(stderr, "Could not read the message from JSON!\n");
     34     exit(1);
     35   }
     36 
     37   uint8_t sig[SLH_SIGNATURE_LEN];
     38   slh_sign(sig, msg, 32, sk, 0);
     39 
     40   size_t j;
     41   for (j = 0; j < SLH_SIGNATURE_LEN; j++) {
     42     CU_ASSERT_EQUAL(sig[j], csig[j]);
     43   }
     44 }
     45 
     46 void test_slh_verify() {
     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   uint8_t pk[2 * ENN];
     55   if (read_key_aoa(pk, 2 * ENN, KEY_SLH_PK, tv)) {
     56     fprintf(stderr, "Could not read the public key from JSON!\n");
     57     exit(1);
     58   }
     59 
     60   uint8_t sig[SLH_SIGNATURE_LEN];
     61   if (read_key_array(sig, SLH_SIGNATURE_LEN, KEY_SLH_SIGNATURE, tv)) {
     62     fprintf(stderr, "Could not read the signature from JSON!\n");
     63     exit(1);
     64   }
     65 
     66   uint8_t msg[32];
     67   if (read_key_array(msg, 32, KEY_SLH_MSG, tv)) {
     68     fprintf(stderr, "Could not read the message from JSON!\n");
     69     exit(1);
     70   }
     71 
     72   CU_ASSERT_EQUAL(slh_verify(msg, 32, sig, SLH_SIGNATURE_LEN, pk), 1);
     73 
     74   msg[3] ^= 0x40;
     75   CU_ASSERT_EQUAL(slh_verify(msg, 32, sig, SLH_SIGNATURE_LEN, pk), 0);
     76   msg[3] ^= 0x40;
     77 
     78   sig[1] ^= 0x10;
     79   CU_ASSERT_EQUAL(slh_verify(msg, 32, sig, SLH_SIGNATURE_LEN, pk), 0);
     80   sig[1] ^= 0x10;
     81 
     82   CU_ASSERT_EQUAL(slh_verify(msg, 32, sig, SLH_SIGNATURE_LEN - 1, pk), 0);
     83 }
     84 
     85 void test_slh_ref_sign() {
     86   json_t *tv = json_load_file(TEST_FILENAME_REF_JSON, 0, NULL);
     87 
     88   if (tv == NULL) {
     89     fprintf(stderr, "Could not open JSON test file\n");
     90     exit(1);
     91   }
     92 
     93   uint8_t sk[4 * ENN];
     94   if (read_key_aoa(sk, 4 * ENN, KEY_SLH_SK, tv)) {
     95     fprintf(stderr, "Could not read the secret key from JSON!\n");
     96     exit(1);
     97   }
     98 
     99   uint8_t csig[SLH_SIGNATURE_LEN];
    100   if (read_key_array(csig, SLH_SIGNATURE_LEN, KEY_SLH_SIGNATURE, tv)) {
    101     fprintf(stderr, "Could not read the signature from JSON!\n");
    102     exit(1);
    103   }
    104 
    105   uint8_t msg[32];
    106   if (read_key_array(msg, 32, KEY_SLH_MSG, tv)) {
    107     fprintf(stderr, "Could not read the message from JSON!\n");
    108     exit(1);
    109   }
    110 
    111   uint8_t sig[SLH_SIGNATURE_LEN];
    112   slh_sign(sig, msg, 32, sk, 0);
    113 
    114   size_t j;
    115   for (j = 0; j < SLH_SIGNATURE_LEN; j++)
    116     CU_ASSERT_EQUAL(sig[j], csig[j]);
    117 }