liboqs/tests/test_kem.c
Christian Paquin dc9617f81f Refactored sig api following nist-branch (issue380) (#391)
* Fixed a typo in a comment

* Refactored sig API following nist-branch (also fixes issue 380)

* Fixed Windows compilation error in sig.c.

* Added Picnic to Windows' config, and changed defaul alg to Picnic (since qTesla is not yet supported on Windows)

* Moved sig_picnic and sig_qtesla under sig directory, to harmonize with kem api

* Use different default sig alg on Windows to fix Travis back-compat tests and platform gap.

* Further changes required for OQS to be properly used by applications

* Compare OQS functions's return values to OQS error codes in sig.c.

* Fixed typos in comments.

* Replaced minimal_oqs_sig with example_sig.

* Ensure travis tests fail on error

* Add try-catch block in all-tests.sh

* Ignore example_sig

* Point global-namespace-check to .libs/liboqs.a

* More precise error handling in global-namespace-check

* Warning colours in travis tests and error handling in free-check

* Error handling in style-check

* Clean up style-check

* Removed leftover minimal_sig_oqs ref and VS projects.

* Prettyprint

* Revert clang-format version check

* Re-revert clang-format style check

* Prettyprint

* Added speed_sig to master.

* Removed superfluous extern from sig schemes .h
2018-09-26 10:01:53 -04:00

101 lines
2.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oqs/oqs.h>
static OQS_STATUS kem_test_correctness(const char *method_name) {
OQS_KEM *kem = NULL;
uint8_t *public_key = NULL;
uint8_t *secret_key = NULL;
uint8_t *ciphertext = NULL;
uint8_t *shared_secret_e = NULL;
uint8_t *shared_secret_d = NULL;
OQS_STATUS rc, ret = OQS_ERROR;
int rv;
kem = OQS_KEM_new(method_name);
if (kem == NULL) {
return OQS_SUCCESS;
}
printf("================================================================================\n");
printf("Sample computation for KEM %s\n", kem->method_name);
printf("================================================================================\n");
public_key = malloc(kem->length_public_key);
secret_key = malloc(kem->length_secret_key);
ciphertext = malloc(kem->length_ciphertext);
shared_secret_e = malloc(kem->length_shared_secret);
shared_secret_d = malloc(kem->length_shared_secret);
if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) {
fprintf(stderr, "ERROR: malloc failed\n");
goto err;
}
rc = OQS_KEM_keypair(kem, public_key, secret_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_KEM_keypair failed\n");
goto err;
}
rc = OQS_KEM_encaps(kem, ciphertext, shared_secret_e, public_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_KEM_encaps failed\n");
goto err;
}
rc = OQS_KEM_decaps(kem, shared_secret_d, ciphertext, secret_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_KEM_decaps failed\n");
goto err;
}
rv = memcmp(shared_secret_e, shared_secret_d, kem->length_shared_secret);
if (rv != 0) {
fprintf(stderr, "ERROR: shared secrets are not equal\n");
OQS_print_hex_string("shared_secret_e", shared_secret_e, kem->length_shared_secret);
OQS_print_hex_string("shared_secret_d", shared_secret_d, kem->length_shared_secret);
goto err;
} else {
printf("shared secrets are equal\n");
}
ret = OQS_SUCCESS;
goto cleanup;
err:
ret = OQS_ERROR;
cleanup:
if (kem != NULL) {
OQS_MEM_secure_free(secret_key, kem->length_secret_key);
OQS_MEM_secure_free(shared_secret_e, kem->length_shared_secret);
OQS_MEM_secure_free(shared_secret_d, kem->length_shared_secret);
}
OQS_MEM_insecure_free(public_key);
OQS_MEM_insecure_free(ciphertext);
OQS_KEM_free(kem);
return ret;
}
int main() {
int ret = EXIT_SUCCESS;
OQS_STATUS rc;
// Use system RNG in this program
OQS_randombytes_switch_algorithm(OQS_RAND_alg_system);
for (size_t i = 0; i < OQS_KEM_algs_length; i++) {
rc = kem_test_correctness(OQS_KEM_alg_identifier(i));
if (rc != OQS_SUCCESS) {
ret = EXIT_FAILURE;
}
}
return ret;
}