Skip to content

Commit

Permalink
Add more description to testcases and test failure results
Browse files Browse the repository at this point in the history
  • Loading branch information
ashman-p committed Oct 22, 2023
1 parent 074e4f5 commit 35e9fab
Showing 1 changed file with 88 additions and 34 deletions.
122 changes: 88 additions & 34 deletions tests/test_sig_stfl.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ OQS_STATUS sig_stfl_keypair_from_KATs(OQS_SIG_STFL *sig, uint8_t *public_key, OQ
*/
OQS_STATUS sig_stfl_KATs_keygen(OQS_SIG_STFL *sig, uint8_t *public_key, OQS_SIG_STFL_SECRET_KEY *secret_key, const char *katfile) {

printf("%s ", sig->method_name);
printf("%s \n\n", sig->method_name);
if (0) {

#ifdef OQS_ENABLE_SIG_STFL_xmss_sha256_h16
Expand Down Expand Up @@ -528,56 +528,108 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char
OQS_randombytes(message, message_len);
OQS_TEST_CT_DECLASSIFY(message, message_len);

printf("================================================================================\n");
printf("1. Generate Key-pair %s\n", sig->method_name);
printf("================================================================================\n");
/*
* Some keypair generation is fast, so we only read keypair from KATs for slow XMSS parameters
*/
rc = sig_stfl_KATs_keygen(sig, public_key, secret_key, katfile);
sk = secret_key;
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_keypair failed\n");
fprintf(stderr, "ERROR: OQS_SIG_STFL_keypair failed. :%s\n", method_name);
goto err;
}

/*
* Get max num signature and the amount remaining
*/
unsigned long long num_sig_left = 0, max_num_sigs = 0;
rc = OQS_SIG_STFL_sigs_total((const OQS_SIG_STFL *)sig, &max_num_sigs, (const OQS_SIG_STFL_SECRET_KEY *)sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get max number of sig from %s.\n", method_name);
goto err;
}
printf("%s Total number of sign operations = %llu\n\n", method_name, max_num_sigs);

printf("================================================================================\n");
printf("2. Serialize and store secret key %s\n", sig->method_name);
printf("================================================================================\n");
rc = OQS_SECRET_KEY_STFL_serialize_key(sk, &sk_buf_len, &sk_buf);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "Failed to serialize_key secret key %s.\n", method_name);
goto err;
}

/* write key pair to disk */
if (oqs_fstore("sk", file_store, sk_buf, sk_buf_len) != OQS_SUCCESS) {
fprintf(stderr, "Failed to store secret key file for %s.\n", method_name);
goto err;
}

if (oqs_fstore("pk", file_store, public_key, sig->length_public_key) != OQS_SUCCESS) {
fprintf(stderr, "Failed to store public key file for %s.\n", method_name);
goto err;
}

printf("================================================================================\n");
printf("3. Generate signature with secret key %s\n", sig->method_name);
printf("================================================================================\n");
rc = OQS_SIG_STFL_sign(sig, signature, &signature_len, message, message_len, secret_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_sign failed\n");
fprintf(stderr, "ERROR: OQS_SIG_STFL_sign failed. :%s\n", method_name);
goto err;
}

printf("Signature len is %zu \n\n", signature_len);
if (oqs_fstore("sig", file_store, signature, signature_len) != OQS_SUCCESS) {
fprintf(stderr, "Failed to store signature file for %s.\n", method_name);
goto err;
}

if (oqs_fstore("msg", file_store, message, message_len) != OQS_SUCCESS) {
fprintf(stderr, "Failed to store signature file for %s.\n", method_name);
goto err;
}

rc = OQS_SIG_STFL_sigs_remaining((const OQS_SIG_STFL *)sig, &num_sig_left, (const OQS_SIG_STFL_SECRET_KEY *)sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get the remaining number of sig from %s.\n", method_name);
goto err;
}
printf("%s Remaining number of sign operations = %llu\n\n", method_name, num_sig_left);

printf("================================================================================\n");
printf("4. Verify signature %s\n", sig->method_name);
printf("================================================================================\n");
OQS_TEST_CT_DECLASSIFY(public_key, sig->length_public_key);
OQS_TEST_CT_DECLASSIFY(signature, signature_len);
rc = OQS_SIG_STFL_verify(sig, message, message_len, signature, signature_len, public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_verify failed\n");
fprintf(stderr, "ERROR: OQS_SIG_STFL_verify failed. :%s\n", method_name);
goto err;
}

printf("================================================================================\n");
printf("5. Read Public key %s\n", sig->method_name);
printf("================================================================================\n");
/* Read public key and re-test verify.*/
read_pk_buf = malloc(sig->length_public_key);
if (oqs_fload("pk", file_store, read_pk_buf, sig->length_public_key, &read_pk_len) != OQS_SUCCESS) {
fprintf(stderr, "ERROR: Failed to load public key. :%s\n", method_name);
goto err;
}

printf("================================================================================\n");
printf("6. Verify signature with the retrieved public from step 5. %s\n", sig->method_name);
printf("================================================================================\n");
rc = OQS_SIG_STFL_verify(sig, message, message_len, signature, signature_len, read_pk_buf);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: 2nd Verify with restored public key OQS_SIG_STFL_verify failed\n");
fprintf(stderr, "ERROR: 2nd Verify with restored public key OQS_SIG_STFL_verify failed. :%s\n", method_name);
}

/* modify the signature to invalidate it */
Expand All @@ -586,7 +638,7 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char
rc = OQS_SIG_STFL_verify(sig, message, message_len, signature, signature_len, public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_ERROR) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_verify should have failed!\n");
fprintf(stderr, "ERROR: OQS_SIG_STFL_verify should have failed! :%s\n", method_name);
goto err;
}

Expand All @@ -599,12 +651,12 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char
rv |= memcmp(message - sizeof(magic_t), magic.val, sizeof(magic_t));
rv |= memcmp(signature - sizeof(magic_t), magic.val, sizeof(magic_t));
if (rv) {
fprintf(stderr, "ERROR: Magic numbers do not match\n");
fprintf(stderr, "ERROR: Magic numbers do not match. :%s\n", method_name);
goto err;
}
#endif

printf("verification passes as expected\n");
printf("verification passes as expected %s.\n", method_name);
ret = OQS_SUCCESS;
goto cleanup;

Expand Down Expand Up @@ -724,35 +776,36 @@ static OQS_STATUS sig_stfl_test_secret_key(const char *method_name) {
keep_going:

printf("================================================================================\n");
printf("Create stateful Signature %s\n", method_name);
printf("Create stateful Signature structure for %s\n", method_name);
printf("================================================================================\n");

sig_obj = OQS_SIG_STFL_new(method_name);
if (sig_obj == NULL) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed\n");
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed. :%s\n", method_name);
goto err;
}

public_key = malloc(sig_obj->length_public_key * sizeof(uint8_t));

printf("================================================================================\n");
printf("Create stateful Secret Key %s\n", method_name);
printf("Create stateful Secret Key structure for %s\n", method_name);
printf("================================================================================\n");

sk = OQS_SIG_STFL_SECRET_KEY_new(method_name);
if (sk == NULL) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new failed\n");
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new failed. :%s\n", method_name);
goto err;
}

printf("\n\n\n");
printf("================================================================================\n");
printf("Generate keypair %s\n", method_name);
printf("Step 1: Generate keypair %s\n", method_name);
printf("================================================================================\n");

rc = OQS_SIG_STFL_keypair(sig_obj, public_key, sk);

if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key gen failed.\n");
fprintf(stderr, "OQS STFL key gen failed. :%s\n", method_name);
goto err;
}

Expand All @@ -776,38 +829,39 @@ static OQS_STATUS sig_stfl_test_secret_key(const char *method_name) {
/* write sk key to disk */
rc = OQS_SECRET_KEY_STFL_serialize_key(sk, &to_file_sk_len, &to_file_sk_buf);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "Failed to serialize secret key. %s.\n", method_name);
goto err;
}

file_store_name = convert_method_name_to_file_name(sig_obj->method_name);
if (oqs_fstore("sk", file_store_name, to_file_sk_buf, to_file_sk_len) != OQS_SUCCESS) {
fprintf(stderr, "Failed to store secret key. %s.\n", method_name);
goto err;
}

if (!sk->secret_key_data) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new incomplete.\n");
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new incomplete. :%s\n", method_name);
goto err;
}

/* set context and secure store callback */
if (sk->set_scrt_key_store_cb) {
context = strdup(file_store_name);
sk->set_scrt_key_store_cb(sk, test_save_secret_key, (void *)context);
}
context = strdup(file_store_name);
OQS_SIG_STFL_SECRET_KEY_SET_store_cb(sk, test_save_secret_key, context);

/* read secret key from disk */
frm_file_sk_buf = malloc(to_file_sk_len);
if (oqs_fload("sk", file_store_name, frm_file_sk_buf, to_file_sk_len, &frm_file_sk_len) != OQS_SUCCESS) {
fprintf(stderr, "Failed to load secret key. %s.\n", method_name);
goto err;
}
if (to_file_sk_len != frm_file_sk_len) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new stored length not equal read length\n");
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new stored length not equal read length. :%s\n", method_name);
goto err;
}

sk_frm_file = OQS_SIG_STFL_SECRET_KEY_new(method_name);
if (sk_frm_file == NULL) {
fprintf(stderr, "ERROR: 2nd OQS_SECRET_KEY_new failed\n");
fprintf(stderr, "ERROR: 2nd OQS_SECRET_KEY_new failed. :%s\n", method_name);
goto err;
}

Expand All @@ -819,7 +873,7 @@ static OQS_STATUS sig_stfl_test_secret_key(const char *method_name) {
goto err;
}

printf("Secret Key created as expected.\n");
printf("PASSED: Secret Key created as expected. :%s\n", method_name);
goto end_it;

err:
Expand Down Expand Up @@ -914,7 +968,7 @@ static OQS_STATUS sig_stfl_test_query_key(const char *method_name) {
keep_going:

printf("================================================================================\n");
printf("Testing stateful Signature Verification %s\n", method_name);
printf("Step 3: Testing stateful Signature Verification %s\n", method_name);
printf("================================================================================\n");

if ( lock_test_sk == NULL || lock_test_sig_obj == NULL || signature_1 == NULL
Expand All @@ -929,7 +983,7 @@ static OQS_STATUS sig_stfl_test_query_key(const char *method_name) {
rc = OQS_SIG_STFL_verify(lock_test_sig_obj, message_1, message_len_1, signature_1, signature_len_1, lock_test_public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_verify failed\n");
fprintf(stderr, "ERROR: lock thread test Sig Verify 1 failed. :%s\n", method_name);
goto err;
}

Expand All @@ -940,7 +994,7 @@ static OQS_STATUS sig_stfl_test_query_key(const char *method_name) {
rc = OQS_SIG_STFL_verify(lock_test_sig_obj, message_2, message_len_2, signature_2, signature_len_2, lock_test_public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_verify failed\n");
fprintf(stderr, "ERROR: lock thread test Sig Verify 2 failed. :%s\n", method_name);
goto err;
}
rc = OQS_SUCCESS;
Expand Down Expand Up @@ -1033,7 +1087,7 @@ static OQS_STATUS sig_stfl_test_sig_gen(const char *method_name) {
keep_going:

printf("================================================================================\n");
printf("Testing stateful Signature Generation %s\n", method_name);
printf("Step 2: Testing stateful Signature Generation %s\n", method_name);
printf("================================================================================\n");

if ( lock_test_sk == NULL || lock_test_sig_obj == NULL) {
Expand Down Expand Up @@ -1064,15 +1118,15 @@ static OQS_STATUS sig_stfl_test_sig_gen(const char *method_name) {
printf("%s Remaining number of sign operations = %llu\n", method_name, num_sig_left);

printf("================================================================================\n");
printf("Sig Gen 1 %s\n", method_name);
printf("Sig Gen 1st %s\n", method_name);
printf("================================================================================\n");

signature_1 = malloc(lock_test_sig_obj->length_signature);

rc = OQS_SIG_STFL_sign(lock_test_sig_obj, signature_1, &signature_len_1, message_1, message_len_1, lock_test_sk);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_sign failed\n");
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_sign failed. :%s\n", method_name);
goto err;
}

Expand All @@ -1095,15 +1149,15 @@ static OQS_STATUS sig_stfl_test_sig_gen(const char *method_name) {
printf("%s Remaining number of sign operations = %llu\n", method_name, num_sig_left);

printf("================================================================================\n");
printf("Sig Gen 2 %s\n", method_name);
printf("Sig Gen 2nd %s\n", method_name);
printf("================================================================================\n");

signature_2 = malloc(lock_test_sig_obj->length_signature);

rc = OQS_SIG_STFL_sign(lock_test_sig_obj, signature_2, &signature_len_2, message_2, message_len_2, lock_test_sk);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_sign failed\n");
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_sign failed. :%s\n", method_name);
goto err;
}

Expand Down Expand Up @@ -1221,7 +1275,7 @@ static OQS_STATUS sig_stfl_test_secret_key_lock(const char *method_name) {

lock_test_sig_obj = OQS_SIG_STFL_new(method_name);
if (lock_test_sig_obj == NULL) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed\n");
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed. :%s\n", method_name);
goto err;
}

Expand All @@ -1233,7 +1287,7 @@ static OQS_STATUS sig_stfl_test_secret_key_lock(const char *method_name) {

lock_test_sk = OQS_SIG_STFL_SECRET_KEY_new(method_name);
if (lock_test_sk == NULL) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new failed\n");
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new failed. :%s\n", method_name);
goto err;
}

Expand All @@ -1260,12 +1314,12 @@ static OQS_STATUS sig_stfl_test_secret_key_lock(const char *method_name) {
rc = OQS_SIG_STFL_keypair(lock_test_sig_obj, lock_test_public_key, lock_test_sk);

if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key gen failed.\n");
fprintf(stderr, "OQS STFL key gen failed. :%s\n", method_name);
goto err;
}

if (!lock_test_sk->secret_key_data) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new incomplete.\n");
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new incomplete. :%s\n", method_name);
goto err;
}

Expand Down

0 comments on commit 35e9fab

Please sign in to comment.