From 40586f8d8bc0e887be3b510642d9503783a8ff5a Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 17 Sep 2024 23:37:25 +0000 Subject: [PATCH] Error Handling Signed-off-by: Songling Han --- src/common/aes/aes128_armv8.c | 4 ++-- src/common/aes/aes128_ni.c | 4 ++-- src/common/aes/aes256_armv8.c | 4 ++-- src/common/aes/aes256_ni.c | 4 ++-- src/common/aes/aes_c.c | 6 +++--- src/common/aes/aes_ossl.c | 8 ++++---- src/common/common.h | 8 ++++---- src/common/ossl_helpers.c | 4 ++-- src/common/rand/rand.c | 14 +++++++------- tests/speed_kem.c | 6 +++--- tests/speed_sig.c | 6 +++--- tests/vectors_kem.c | 2 +- tests/vectors_sig.c | 2 +- 13 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/common/aes/aes128_armv8.c b/src/common/aes/aes128_armv8.c index 292539fefa..d506e9d606 100644 --- a/src/common/aes/aes128_armv8.c +++ b/src/common/aes/aes128_armv8.c @@ -21,7 +21,7 @@ void oqs_aes128_load_iv_armv8(const uint8_t *iv, size_t iv_len, void *_schedule) } else if (iv_len == 16) { memcpy(ctx->iv, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } @@ -118,7 +118,7 @@ void oqs_aes128_ctr_enc_sch_armv8(const uint8_t *iv, const size_t iv_len, const memcpy(&ctr_be, &iv[12], 4); ctr = BE_TO_UINT32(ctr_be); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } while (out_len >= 16) { ctr_be = UINT32_TO_BE(ctr); diff --git a/src/common/aes/aes128_ni.c b/src/common/aes/aes128_ni.c index eb2f354813..5b7398a52d 100644 --- a/src/common/aes/aes128_ni.c +++ b/src/common/aes/aes128_ni.c @@ -65,7 +65,7 @@ void oqs_aes128_load_iv_ni(const uint8_t *iv, size_t iv_len, void *_schedule) { } else if (iv_len == 16) { ctx->iv = _mm_shuffle_epi8(_mm_loadu_si128((const __m128i *)iv), idx); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } @@ -176,7 +176,7 @@ void oqs_aes128_ctr_enc_sch_ni(const uint8_t *iv, const size_t iv_len, const voi } else if (iv_len == 16) { block = _mm_loadu_si128((const __m128i *)iv); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } while (out_len >= 64) { diff --git a/src/common/aes/aes256_armv8.c b/src/common/aes/aes256_armv8.c index 24dbe0b58a..e7c34baffa 100644 --- a/src/common/aes/aes256_armv8.c +++ b/src/common/aes/aes256_armv8.c @@ -21,7 +21,7 @@ void oqs_aes256_load_iv_armv8(const uint8_t *iv, size_t iv_len, void *_schedule) } else if (iv_len == 16) { memcpy(ctx->iv, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } @@ -122,7 +122,7 @@ void oqs_aes256_ctr_enc_sch_armv8(const uint8_t *iv, const size_t iv_len, const memcpy(&ctr_be, &iv[12], 4); ctr = BE_TO_UINT32(ctr_be); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } while (out_len >= 16) { ctr_be = UINT32_TO_BE(ctr); diff --git a/src/common/aes/aes256_ni.c b/src/common/aes/aes256_ni.c index eb28bc585d..d5ae56e2a9 100644 --- a/src/common/aes/aes256_ni.c +++ b/src/common/aes/aes256_ni.c @@ -92,7 +92,7 @@ void oqs_aes256_load_iv_ni(const uint8_t *iv, size_t iv_len, void *_schedule) { } else if (iv_len == 16) { ctx->iv = _mm_shuffle_epi8(_mm_loadu_si128((const __m128i *)iv), idx); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } @@ -210,7 +210,7 @@ void oqs_aes256_ctr_enc_sch_ni(const uint8_t *iv, const size_t iv_len, const voi } else if (iv_len == 16) { block = _mm_loadu_si128((const __m128i *)iv); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } while (out_len >= 64) { diff --git a/src/common/aes/aes_c.c b/src/common/aes/aes_c.c index dfbf0abc13..df75b6322b 100644 --- a/src/common/aes/aes_c.c +++ b/src/common/aes/aes_c.c @@ -651,7 +651,7 @@ static void aes_ctr(unsigned char *out, size_t outlen, const unsigned char *iv, } else if (iv_len == 16) { br_range_dec32le(ivw, 4, iv); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } memcpy(ivw + 4, ivw, 3 * sizeof(uint32_t)); memcpy(ivw + 8, ivw, 3 * sizeof(uint32_t)); @@ -733,7 +733,7 @@ void oqs_aes256_load_iv_c(const uint8_t *iv, size_t iv_len, void *_schedule) { } else if (iv_len == 16) { memcpy(ctx->iv, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } @@ -766,7 +766,7 @@ void oqs_aes128_load_iv_c(const uint8_t *iv, size_t iv_len, void *_schedule) { } else if (iv_len == 16) { memcpy(ctx->iv, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } diff --git a/src/common/aes/aes_ossl.c b/src/common/aes/aes_ossl.c index ff971c874b..1e48124a3b 100644 --- a/src/common/aes/aes_ossl.c +++ b/src/common/aes/aes_ossl.c @@ -79,7 +79,7 @@ static void AES128_CTR_inc_stream_iv(const uint8_t *iv, size_t iv_len, const voi } else if (iv_len == 16) { memcpy(iv_ctr, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } const struct key_schedule *ks = (const struct key_schedule *) schedule; OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_EncryptInit_ex)(ctr_ctx, oqs_aes_128_ctr(), NULL, ks->key, iv_ctr)); @@ -114,7 +114,7 @@ static void AES128_CTR_inc_iv(const uint8_t *iv, size_t iv_len, void *schedule) } else if (iv_len == 16) { memcpy(ks->iv, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_EncryptInit_ex)(ks->ctx, oqs_aes_128_ctr(), NULL, ks->key, ks->iv)); } @@ -160,7 +160,7 @@ static void AES256_CTR_inc_iv(const uint8_t *iv, size_t iv_len, void *schedule) } else if (iv_len == 16) { memcpy(ks->iv, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_EncryptInit_ex)(ks->ctx, oqs_aes_256_ctr(), NULL, ks->key, ks->iv)); } @@ -203,7 +203,7 @@ static void AES256_CTR_inc_stream_iv(const uint8_t *iv, size_t iv_len, const voi } else if (iv_len == 16) { memcpy(iv_ctr, iv, 16); } else { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } const struct key_schedule *ks = (const struct key_schedule *) schedule; OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_EncryptInit_ex)(ctr_ctx, oqs_aes_256_ctr(), NULL, ks->key, iv_ctr)); diff --git a/src/common/common.h b/src/common/common.h index 65d24e8b50..947b65b030 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -89,7 +89,7 @@ extern "C" { do { \ if ( (x) == (void*)0 ) { \ fprintf(stderr, "Unexpected NULL returned from %s API. Exiting.\n", loc); \ - exit(EXIT_FAILURE); \ + return; /* TODO: better error handling */ \ } \ } while (0) @@ -110,7 +110,7 @@ extern "C" { do { \ if( 1 != (x) ) { \ fprintf(stderr, "Error return value from OpenSSL API: %d. Exiting.\n", x); \ - exit(EXIT_FAILURE); \ + return; /* TODO: better error handling */ \ } \ } while (0) #else // OPENSSL_NO_STDIO @@ -119,7 +119,7 @@ extern "C" { if( 1 != (x) ) { \ fprintf(stderr, "Error return value from OpenSSL API: %d. Exiting.\n", x); \ OSSL_FUNC(ERR_print_errors_fp)(stderr); \ - exit(EXIT_FAILURE); \ + return; /* TODO: better error handling */ \ } \ } while (0) #endif // OPENSSL_NO_STDIO @@ -136,7 +136,7 @@ extern "C" { if (size_t_var_name <= INT_MAX) { \ int_var_name = (int)size_t_var_name; \ } else { \ - exit(EXIT_FAILURE); \ + return; /* TODO: better error handling */ \ } /** diff --git a/src/common/ossl_helpers.c b/src/common/ossl_helpers.c index 76dccb0ef4..2eaf4f5861 100644 --- a/src/common/ossl_helpers.c +++ b/src/common/ossl_helpers.c @@ -343,7 +343,7 @@ static void ensure_symbol(const char *name, void **symp) { if (!*symp) { void *sym = dlsym(libcrypto_dlhandle, name); if (!sym) { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } *symp = sym; } @@ -354,7 +354,7 @@ static void ensure_library(void) { libcrypto_dlhandle = dlopen(OQS_OPENSSL_CRYPTO_SONAME, RTLD_LAZY | RTLD_LOCAL); if (!libcrypto_dlhandle) { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } diff --git a/src/common/rand/rand.c b/src/common/rand/rand.c index 5ff7efe0a6..b479e91472 100644 --- a/src/common/rand/rand.c +++ b/src/common/rand/rand.c @@ -59,7 +59,7 @@ void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { HCRYPTPROV hCryptProv; if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) || !CryptGenRandom(hCryptProv, (DWORD) bytes_to_read, random_array)) { - exit(EXIT_FAILURE); // better to fail than to return bad random data + return; /* TODO: better error handling */ // better to fail than to return bad random data } CryptReleaseContext(hCryptProv, 0); } @@ -71,19 +71,19 @@ void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { fprintf(stderr, "OQS_randombytes_system is not available in an embedded build.\n"); fprintf(stderr, "Call OQS_randombytes_custom_algorithm() to set a custom method for your system.\n"); - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } #elif defined(OQS_HAVE_GETENTROPY) void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { while (bytes_to_read > 256) { if (getentropy(random_array, 256)) { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } random_array += 256; bytes_to_read -= 256; } if (getentropy(random_array, bytes_to_read)) { - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } #else @@ -94,13 +94,13 @@ void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { handle = fopen("/dev/urandom", "rb"); if (!handle) { perror("OQS_randombytes"); - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } bytes_read = fread(random_array, 1, bytes_to_read, handle); if (bytes_read < bytes_to_read || ferror(handle)) { perror("OQS_randombytes"); - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } fclose(handle); @@ -122,7 +122,7 @@ void OQS_randombytes_openssl(uint8_t *random_array, size_t bytes_to_read) { fprintf(stderr, "No OpenSSL randomness retrieved. DRBG available?\n"); // because of void signature we have no other way to signal the problem // we cannot possibly return without randomness - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } } #endif diff --git a/tests/speed_kem.c b/tests/speed_kem.c index 9ce63534db..48c53aa977 100644 --- a/tests/speed_kem.c +++ b/tests/speed_kem.c @@ -20,15 +20,15 @@ static void fullcycletest(OQS_KEM *kem, uint8_t *public_key, uint8_t *secret_key, uint8_t *ciphertext, uint8_t *shared_secret_e, uint8_t *shared_secret_d) { if (OQS_KEM_keypair(kem, public_key, secret_key) != OQS_SUCCESS) { printf("Error creating KEM key. Exiting.\n"); - exit(-1); + return; /* TODO: better error handling */ } if (OQS_KEM_encaps(kem, ciphertext, shared_secret_e, public_key) != OQS_SUCCESS) { printf("Error during KEM encaps. Exiting.\n"); - exit(-1); + return; /* TODO: better error handling */ } if (OQS_KEM_decaps(kem, shared_secret_d, ciphertext, secret_key) != OQS_SUCCESS) { printf("Error during KEM decaps. Exiting.\n"); - exit(-1); + return; /* TODO: better error handling */ } } diff --git a/tests/speed_sig.c b/tests/speed_sig.c index cc911beedf..747fe99610 100644 --- a/tests/speed_sig.c +++ b/tests/speed_sig.c @@ -20,15 +20,15 @@ static void fullcycle(OQS_SIG *sig, uint8_t *public_key, uint8_t *secret_key, uint8_t *signature, size_t signature_len, uint8_t *message, size_t message_len) { if (OQS_SIG_keypair(sig, public_key, secret_key) != OQS_SUCCESS) { printf("keygen error. Exiting.\n"); - exit(-1); + return; /* TODO: better error handling */ } if (OQS_SIG_sign(sig, signature, &signature_len, message, message_len, secret_key) != OQS_SUCCESS) { printf("sign error. Exiting.\n"); - exit(-1); + return; /* TODO: better error handling */ } if (OQS_SIG_verify(sig, message, message_len, signature, signature_len, public_key) != OQS_SUCCESS) { printf("verify error. Exiting.\n"); - exit(-1); + return; /* TODO: better error handling */ } } diff --git a/tests/vectors_kem.c b/tests/vectors_kem.c index 4dc8ae63bc..abd6878e23 100644 --- a/tests/vectors_kem.c +++ b/tests/vectors_kem.c @@ -50,7 +50,7 @@ static void hexStringToByteArray(const char *hexString, uint8_t *byteArray) { if (len % 2 != 0) { fprintf(stderr, "Hex string must have an even number of characters\n"); - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } for (size_t i = 0, j = 0; i < len; i += 2, j++) { diff --git a/tests/vectors_sig.c b/tests/vectors_sig.c index bfccfb23c2..b84bd90f5a 100644 --- a/tests/vectors_sig.c +++ b/tests/vectors_sig.c @@ -50,7 +50,7 @@ static void hexStringToByteArray(const char *hexString, uint8_t *byteArray) { if (len % 2 != 0) { fprintf(stderr, "Hex string must have an even number of characters\n"); - exit(EXIT_FAILURE); + return; /* TODO: better error handling */ } for (size_t i = 0, j = 0; i < len; i += 2, j++) {