From 4b23d10a57ff0db2e16535f35c9a1f55f0108fa6 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Wed, 18 Sep 2024 06:09:53 +0000 Subject: [PATCH] Add NULL check for rand Signed-off-by: Songling Han --- src/common/rand/rand.c | 14 ++++++++++---- src/common/rand/rand_nist.c | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/common/rand/rand.c b/src/common/rand/rand.c index b479e9147..5802efbc0 100644 --- a/src/common/rand/rand.c +++ b/src/common/rand/rand.c @@ -50,16 +50,21 @@ OQS_API void OQS_randombytes_custom_algorithm(void (*algorithm_ptr)(uint8_t *, s } OQS_API void OQS_randombytes(uint8_t *random_array, size_t bytes_to_read) { - oqs_randombytes_algorithm(random_array, bytes_to_read); + if (random_array != NULL && bytes_to_read > 0) { + oqs_randombytes_algorithm(random_array, bytes_to_read); + } } // Select the implementation for OQS_randombytes_system #if defined(_WIN32) 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)) { - return; /* TODO: better error handling */ // better to fail than to return bad random data + if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { + return; /* TODO: better error handling */ + } + if (!CryptGenRandom(hCryptProv, (DWORD) bytes_to_read, random_array)) { + CryptReleaseContext(hCryptProv, 0); + return; /* TODO: better error handling */ } CryptReleaseContext(hCryptProv, 0); } @@ -100,6 +105,7 @@ void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { bytes_read = fread(random_array, 1, bytes_to_read, handle); if (bytes_read < bytes_to_read || ferror(handle)) { perror("OQS_randombytes"); + fclose(handle); return; /* TODO: better error handling */ } diff --git a/src/common/rand/rand_nist.c b/src/common/rand/rand_nist.c index 12407a08d..c44db18d5 100644 --- a/src/common/rand/rand_nist.c +++ b/src/common/rand/rand_nist.c @@ -38,7 +38,7 @@ static void AES256_CTR_DRBG_Update(unsigned char *provided_data, unsigned char * // buffer - a 128-bit ciphertext value static void AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer) { #ifdef OQS_USE_OPENSSL - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = NULL; int len; @@ -46,16 +46,23 @@ static void AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *bu ctx = OSSL_FUNC(EVP_CIPHER_CTX_new)(); OQS_EXIT_IF_NULLPTR(ctx, "OpenSSL"); - OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_EncryptInit_ex)(ctx, oqs_aes_256_ecb(), NULL, key, NULL)); - OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_EncryptUpdate)(ctx, buffer, &len, ctr, 16)); + if (OSSL_FUNC(EVP_EncryptInit_ex)(ctx, oqs_aes_256_ecb(), NULL, key, NULL) != 1 || + OSSL_FUNC(EVP_EncryptUpdate)(ctx, buffer, &len, ctr, 16) != 1) { + OSSL_FUNC(EVP_CIPHER_CTX_free)(ctx); + OQS_EXIT("AES256_ECB"); + } /* Clean up */ OSSL_FUNC(EVP_CIPHER_CTX_free)(ctx); #else void *schedule = NULL; OQS_AES256_ECB_load_schedule(key, &schedule); - OQS_AES256_ECB_enc(ctr, 16, key, buffer); - OQS_AES256_free_schedule(schedule); + if (schedule != NULL) { + OQS_AES256_ECB_enc(ctr, 16, key, buffer); + OQS_AES256_free_schedule(schedule); + } else { + OQS_EXIT("AES256_ECB"); + } #endif }