Skip to content

Commit

Permalink
Error Handling
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 22, 2024
1 parent a90603f commit 40586f8
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/common/aes/aes128_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/common/aes/aes128_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/common/aes/aes256_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/common/aes/aes256_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/common/aes/aes_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 */
}
}

Expand Down Expand Up @@ -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 */
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/common/aes/aes_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 */ \
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/common/ossl_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 */
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/common/rand/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
6 changes: 3 additions & 3 deletions tests/speed_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}

}
Expand Down
6 changes: 3 additions & 3 deletions tests/speed_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/vectors_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/vectors_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down

0 comments on commit 40586f8

Please sign in to comment.