Skip to content

Commit

Permalink
Merge pull request #2 from BytesLogik/asan-check-tests
Browse files Browse the repository at this point in the history
Asan check tests
  • Loading branch information
trigpolynom authored Dec 2, 2023
2 parents d64f691 + a588655 commit 675fbfb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ liboqs
.local
# build directory
_build
build
# generated from openssl src:
test/ssltestlib.c
test/ssltestlib.h
Expand Down
6 changes: 2 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,17 @@ add_test(
# openssl under MSVC seems to have a bug registering NIDs:
# It only works when setting OPENSSL_CONF, not when loading the same cnf file:
if (MSVC)
set_tests_properties(oqs_signatures
set_tests_properties(oqs_test_hashnsign
PROPERTIES ENVIRONMENT "OPENSSL_MODULES=${OQS_PROV_BINARY_DIR};OPENSSL_CONF=${CMAKE_CURRENT_SOURCE_DIR}/openssl-ca.cnf"
)
else()
set_tests_properties(oqs_signatures
set_tests_properties(oqs_test_hashnsign
PROPERTIES ENVIRONMENT "OPENSSL_MODULES=${OQS_PROV_BINARY_DIR}"
)
endif()

add_executable(oqs_test_hashnsign oqs_test_hashnsign.c test_common.c)
target_link_libraries(oqs_test_hashnsign PRIVATE ${OPENSSL_CRYPTO_LIBRARY} ${OQS_ADDL_SOCKET_LIBS})


add_test(
NAME oqs_tlssig
COMMAND oqs_test_tlssig
Expand Down
75 changes: 42 additions & 33 deletions test/oqs_test_hashnsign.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ static const EVP_MD *get_digest_for_algorithm(const char *alg_name)
return md;
}

static int test_hash_n_sign(const char *sigalg_name)
{
static int test_hash_n_sign(const char *sigalg_name) {
EVP_MD_CTX *mdctx = NULL;
EVP_PKEY_CTX *pkey_ctx = NULL;
EVP_PKEY *key = NULL;
Expand All @@ -72,67 +71,75 @@ static int test_hash_n_sign(const char *sigalg_name)

const EVP_MD *md_type = get_digest_for_algorithm(sigalg_name);
if (!md_type) {
printf(
"Unsupported digest type for algorithm %s.\n Not failing over unsupported hash algs.",
sigalg_name);
printf("Unsupported digest type for algorithm %s.\n Not failing over unsupported hash algs.", sigalg_name);
return 1;
}

pkey_ctx = EVP_PKEY_CTX_new_from_name(libctx, sigalg_name, NULL);
if (!pkey_ctx) {
printf("EVP_PKEY_CTX_new_from_name failed for %s.\n", sigalg_name);
return 0;
goto cleanup;
}

if (EVP_PKEY_keygen_init(pkey_ctx) <= 0) {
printf("EVP_PKEY_keygen_init failed for %s.\n", sigalg_name);
EVP_PKEY_CTX_free(pkey_ctx);
return 0;
goto cleanup;
}

if (EVP_PKEY_generate(pkey_ctx, &key) <= 0) {
printf("EVP_PKEY_generate failed for %s.\n", sigalg_name);
EVP_PKEY_CTX_free(pkey_ctx);
return 0;
goto cleanup;
}

mdctx = EVP_MD_CTX_new();
if (mdctx == NULL) {
printf("EVP_MD_CTX_new failed for %s.\n", sigalg_name);
return 0;
goto cleanup;
}

if (EVP_DigestSignInit(mdctx, NULL, md_type, NULL, key) <= 0) {
printf("EVP_DigestSignInit failed for %s.\n", sigalg_name);
testresult = 0;
} else {
if (EVP_DigestSignUpdate(mdctx, msg, strlen(msg)) <= 0) {
printf("EVP_DigestSignUpdate failed for %s.\n", sigalg_name);
testresult = 0;
} else if (EVP_DigestSignFinal(mdctx, NULL, &siglen) <= 0) {
printf("EVP_DigestSignFinal (get length) failed for %s.\n",
sigalg_name);
testresult = 0;
} else {
sig = OPENSSL_malloc(siglen);
if (sig == NULL) {
printf("OPENSSL_malloc failed for %s.\n", sigalg_name);
testresult = 0;
} else if (EVP_DigestSignFinal(mdctx, sig, &siglen) <= 0) {
printf("EVP_DigestSignFinal (get signature) failed for %s.\n",
sigalg_name);
testresult = 0;
} else {
printf("Signature operation successful for %s.\n", sigalg_name);
testresult = 1;
}
}
goto cleanup;
}

if (EVP_DigestSignUpdate(mdctx, msg, strlen(msg)) <= 0) {
printf("EVP_DigestSignUpdate failed for %s.\n", sigalg_name);
testresult = 0;
goto cleanup;
}

if (EVP_DigestSignFinal(mdctx, NULL, &siglen) <= 0) {
printf("EVP_DigestSignFinal (get length) failed for %s.\n", sigalg_name);
testresult = 0;
goto cleanup;
}

sig = OPENSSL_malloc(siglen);
if (sig == NULL) {
printf("OPENSSL_malloc failed for %s.\n", sigalg_name);
testresult = 0;
goto cleanup;
}

if (EVP_DigestSignFinal(mdctx, sig, &siglen) <= 0) {
printf("EVP_DigestSignFinal (get signature) failed for %s.\n", sigalg_name);
testresult = 0;
goto cleanup;
}

printf("Signature operation successful for %s.\n", sigalg_name);
testresult = 1;

cleanup:
if (sig)
OPENSSL_free(sig);
if (mdctx)
EVP_MD_CTX_free(mdctx);
if (key)
EVP_PKEY_free(key);
if (pkey_ctx)
EVP_PKEY_CTX_free(pkey_ctx);

return testresult;
}
Expand All @@ -151,6 +158,8 @@ int main(int argc, char *argv[])
modulename = argv[1];
configfile = argv[2];

printf("Config file: %s\n", configfile);

load_oqs_provider(libctx, modulename, configfile);

oqsprov = OSSL_PROVIDER_load(libctx, modulename);
Expand Down

0 comments on commit 675fbfb

Please sign in to comment.