Skip to content

Commit

Permalink
some improvements in test code
Browse files Browse the repository at this point in the history
  • Loading branch information
bhess committed Jan 25, 2024
1 parent b45a0e6 commit 6ca8117
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
1 change: 1 addition & 0 deletions tests/test_kem_vectors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ encaps_K=$(grep "encaps_K: " "$file")

output=$($build_dir/tests/vectors_kem $1 "$keygen_z$keygen_d$encaps_m" "$encaps_ek" "$encaps_k" "$decaps_dk" "$decaps_c" "$decaps_kprime")
if [ $? != 0 ]; then
echo "$output"
exit 1
fi

Expand Down
40 changes: 24 additions & 16 deletions tests/vectors_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

#include "system_info.c"

typedef struct {
struct {
const uint8_t *pos;
} fixed_prng_state;

fixed_prng_state prng_state = { .pos = 0 };
} prng_state = {
.pos = 0
};

/* Displays hexadecimal strings */
static void OQS_print_hex_string(const char *label, const uint8_t *str, size_t len) {
Expand Down Expand Up @@ -228,22 +228,28 @@ int main(int argc, char **argv) {
char *decaps_ciphertext = argv[6];
char *decaps_kprime = argv[7];

OQS_KEM *kem = OQS_KEM_new(alg_name);
if (kem == NULL) {
printf("[vectors_kem] %s was not enabled at compile-time.\n", alg_name);
goto err;
}

if (strlen(prng_output_stream) % 2 != 0 ||
strlen(encaps_pk) % 2 != 0 ||
strlen(encaps_K) % 2 != 0 ||
strlen(decaps_sk) % 2 != 0 ||
strlen(decaps_ciphertext) % 2 != 0 ||
strlen(decaps_kprime) % 2 != 0) {
return EXIT_FAILURE;
strlen(encaps_pk) != 2 * kem->length_public_key ||
strlen(encaps_K) != 2 * kem->length_shared_secret ||
strlen(decaps_sk) != 2 * kem->length_secret_key ||
strlen(decaps_ciphertext) != 2 * kem->length_ciphertext ||
strlen(decaps_kprime) != 2 * kem->length_shared_secret ) {
rc = OQS_ERROR;
goto err;
}

uint8_t *prng_output_stream_bytes = malloc(strlen(prng_output_stream) / 2); // TODO: allocate real sizes and check before to real sizes!
uint8_t *encaps_pk_bytes = malloc(strlen(encaps_pk) / 2);
uint8_t *encaps_K_bytes = malloc(strlen(encaps_K) / 2);
uint8_t *decaps_sk_bytes = malloc(strlen(decaps_sk) / 2);
uint8_t *decaps_ciphertext_bytes = malloc(strlen(decaps_ciphertext) / 2);
uint8_t *decaps_kprime_bytes = malloc(strlen(decaps_kprime) / 2);
uint8_t *prng_output_stream_bytes = malloc(strlen(prng_output_stream) / 2);
uint8_t *encaps_pk_bytes = malloc(kem->length_public_key);
uint8_t *encaps_K_bytes = malloc(kem->length_shared_secret);
uint8_t *decaps_sk_bytes = malloc(kem->length_secret_key);
uint8_t *decaps_ciphertext_bytes = malloc(kem->length_ciphertext);
uint8_t *decaps_kprime_bytes = malloc(kem->length_shared_secret);

if ((prng_output_stream_bytes == NULL) || (encaps_pk_bytes == NULL) || (encaps_K_bytes == NULL) || (decaps_sk_bytes == NULL) || (decaps_ciphertext_bytes == NULL) || (decaps_kprime_bytes == NULL)) {
fprintf(stderr, "[vectors_kem] ERROR: malloc failed!\n");
Expand All @@ -268,6 +274,8 @@ int main(int argc, char **argv) {
OQS_MEM_insecure_free(decaps_ciphertext_bytes);
OQS_MEM_insecure_free(decaps_kprime_bytes);

OQS_KEM_free(kem);

OQS_destroy();

if (rc != OQS_SUCCESS) {
Expand Down
48 changes: 34 additions & 14 deletions tests/vectors_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

#include "system_info.c"

typedef struct {
struct {
const uint8_t *pos;
} fixed_prng_state;

fixed_prng_state prng_state = { .pos = 0 };
} prng_state = {
.pos = 0
};

static void fprintBstr(FILE *fp, const char *S, const uint8_t *A, size_t L) {
size_t i;
Expand Down Expand Up @@ -175,6 +175,8 @@ OQS_STATUS sig_vector(const char *method_name,
}

int main(int argc, char **argv) {
OQS_STATUS rc;

OQS_init();

if (argc != 8) {
Expand Down Expand Up @@ -203,37 +205,55 @@ int main(int argc, char **argv) {
char *verif_msg = argv[7];
size_t verif_msg_len = strlen(verif_msg) / 2;

OQS_SIG *sig = OQS_SIG_new(alg_name);
if (sig == NULL) {
printf("[vectors_sig] %s was not enabled at compile-time.\n", alg_name);
goto err;
}

if (strlen(prng_output_stream) % 2 != 0 ||
strlen(sig_msg) % 2 != 0 ||
strlen(sig_sk) % 2 != 0 ||
strlen(verif_sig) % 2 != 0 ||
strlen(verif_pk) % 2 != 0 ||
strlen(verif_msg) % 2 != 0) {
return EXIT_FAILURE;
strlen(sig_msg) % 2 != 0 || // variable length
strlen(sig_sk) != 2 * sig->length_secret_key ||
strlen(verif_sig) != 2 * sig->length_signature ||
strlen(verif_pk) != 2 * sig->length_public_key ||
strlen(verif_msg) % 2 != 0) { // variable length
rc = OQS_ERROR;
goto err;
}

uint8_t *prng_output_stream_bytes = malloc(strlen(prng_output_stream) / 2);
uint8_t *sig_msg_bytes = malloc(strlen(sig_msg) / 2);
uint8_t *sig_sk_bytes = malloc(strlen(sig_sk) / 2);
uint8_t *verif_sig_bytes = malloc(strlen(verif_sig) / 2);
uint8_t *verif_pk_bytes = malloc(strlen(verif_pk) / 2);
uint8_t *sig_sk_bytes = malloc(sig->length_secret_key);
uint8_t *verif_sig_bytes = malloc(sig->length_signature);
uint8_t *verif_pk_bytes = malloc(sig->length_public_key);
uint8_t *verif_msg_bytes = malloc(strlen(verif_msg) / 2);

if ((prng_output_stream_bytes == NULL) || (sig_msg_bytes == NULL) || (sig_sk_bytes == NULL) || (verif_sig_bytes == NULL) || (verif_pk_bytes == NULL) || (verif_msg_bytes == NULL)) {
fprintf(stderr, "[vectors_sig] ERROR: malloc failed!\n");
rc = OQS_ERROR;
goto err;
}


hexStringToByteArray(prng_output_stream, prng_output_stream_bytes);
hexStringToByteArray(sig_msg, sig_msg_bytes);
hexStringToByteArray(sig_sk, sig_sk_bytes);
hexStringToByteArray(verif_sig, verif_sig_bytes);
hexStringToByteArray(verif_pk, verif_pk_bytes);
hexStringToByteArray(verif_msg, verif_msg_bytes);

OQS_STATUS rc = sig_vector(alg_name, prng_output_stream_bytes, sig_msg_bytes, sig_msg_len, sig_sk_bytes, verif_sig_bytes, verif_pk_bytes, verif_msg_bytes, verif_msg_len);
rc = sig_vector(alg_name, prng_output_stream_bytes, sig_msg_bytes, sig_msg_len, sig_sk_bytes, verif_sig_bytes, verif_pk_bytes, verif_msg_bytes, verif_msg_len);

err:
OQS_MEM_insecure_free(prng_output_stream_bytes);
OQS_MEM_insecure_free(sig_msg_bytes);
OQS_MEM_insecure_free(sig_sk_bytes);
OQS_MEM_insecure_free(verif_sig_bytes);
OQS_MEM_insecure_free(verif_pk_bytes);
OQS_MEM_insecure_free(verif_msg_bytes);

OQS_SIG_free(sig);

OQS_destroy();

if (rc != OQS_SUCCESS) {
Expand Down

0 comments on commit 6ca8117

Please sign in to comment.