diff --git a/src/common/common.c b/src/common/common.c index 1cdcd2baa..1101c9843 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -298,38 +298,38 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - OQS_MEM_free(ptr); // IGNORE free-check + OQS_MEM_free(ptr); // IGNORE free-check } } OQS_API void OQS_MEM_insecure_free(void *ptr) { - OQS_MEM_free(ptr); // IGNORE free-check + OQS_MEM_free(ptr); // IGNORE free-check } void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { #if defined(OQS_USE_OPENSSL) - // Use OpenSSL's memory allocation functions - if (!size) { - return NULL; - } - const size_t offset = alignment - 1 + sizeof(uint8_t); - uint8_t *buffer = OPENSSL_malloc(size + offset); - if (!buffer) { - return NULL; - } - uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1)); - ptrdiff_t diff = ptr - buffer; - if (diff > UINT8_MAX) { - // Free and return NULL if alignment is too large - OPENSSL_free(buffer); - errno = EINVAL; - return NULL; - } - // Store the difference so that the free function can use it - ptr[-1] = diff; - return ptr; + // Use OpenSSL's memory allocation functions + if (!size) { + return NULL; + } + const size_t offset = alignment - 1 + sizeof(uint8_t); + uint8_t *buffer = OPENSSL_malloc(size + offset); + if (!buffer) { + return NULL; + } + uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1)); + ptrdiff_t diff = ptr - buffer; + if (diff > UINT8_MAX) { + // Free and return NULL if alignment is too large + OPENSSL_free(buffer); + errno = EINVAL; + return NULL; + } + // Store the difference so that the free function can use it + ptr[-1] = diff; + return ptr; #elif defined(OQS_HAVE_ALIGNED_ALLOC) // glibc and other implementations providing aligned_alloc - return aligned_alloc(alignment, size); + return aligned_alloc(alignment, size); #else // Check alignment (power of 2, and >= sizeof(void*)) and size (multiple of alignment) if (alignment & (alignment - 1) || size & (alignment - 1) || alignment < sizeof(void *)) { @@ -391,13 +391,13 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { void OQS_MEM_aligned_free(void *ptr) { #if defined(OQS_USE_OPENSSL) - // Use OpenSSL's free function - if (ptr) { - uint8_t *u8ptr = ptr; - OPENSSL_free(u8ptr - u8ptr[-1]); - } + // Use OpenSSL's free function + if (ptr) { + uint8_t *u8ptr = ptr; + OPENSSL_free(u8ptr - u8ptr[-1]); + } #elif defined(OQS_HAVE_ALIGNED_ALLOC) || defined(OQS_HAVE_POSIX_MEMALIGN) || defined(OQS_HAVE_MEMALIGN) - free(ptr); // IGNORE free-check + free(ptr); // IGNORE free-check #elif defined(__MINGW32__) || defined(__MINGW64__) __mingw_aligned_free(ptr); #elif defined(_MSC_VER) diff --git a/src/common/common.h b/src/common/common.h index 147dcdb84..82e9e5bfc 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -21,39 +21,39 @@ extern "C" { #endif #if defined(OQS_USE_OPENSSL) - #ifndef OPENSSL_malloc - # define OPENSSL_malloc(num) \ +#ifndef OPENSSL_malloc +# define OPENSSL_malloc(num) \ CRYPTO_malloc(num, __FILE__, __LINE__) - #endif +#endif - #ifndef OPENSSL_zalloc - # define OPENSSL_zalloc(num) \ +#ifndef OPENSSL_zalloc +# define OPENSSL_zalloc(num) \ CRYPTO_zalloc(num, __FILE__, __LINE__) - #endif +#endif - #ifndef OPENSSL_free - # define OPENSSL_free(addr) \ +#ifndef OPENSSL_free +# define OPENSSL_free(addr) \ CRYPTO_free(addr, __FILE__, __LINE__) - #endif +#endif - #ifndef OPENSSL_strdup - # define OPENSSL_strdup(str) \ +#ifndef OPENSSL_strdup +# define OPENSSL_strdup(str) \ CRYPTO_strdup(str, __FILE__, __LINE__) - #endif +#endif - extern void *CRYPTO_malloc(size_t num, const char *file, int line); - extern void *CRYPTO_zalloc(size_t num, const char *file, int line); - extern void CRYPTO_free(void *str, const char *file, int line); - extern char *CRYPTO_strdup(const char *str, const char* file, int line); - #define OQS_MEM_malloc(size) OPENSSL_malloc(size) - #define OQS_MEM_free(ptr) OPENSSL_free(ptr) - #define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size)) - #define OQS_MEM_strdup(str) OPENSSL_strdup(str) +extern void *CRYPTO_malloc(size_t num, const char *file, int line); +extern void *CRYPTO_zalloc(size_t num, const char *file, int line); +extern void CRYPTO_free(void *str, const char *file, int line); +extern char *CRYPTO_strdup(const char *str, const char *file, int line); +#define OQS_MEM_malloc(size) OPENSSL_malloc(size) +#define OQS_MEM_free(ptr) OPENSSL_free(ptr) +#define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size)) +#define OQS_MEM_strdup(str) OPENSSL_strdup(str) #else - #define OQS_MEM_malloc(size) malloc(size) - #define OQS_MEM_free(ptr) free(ptr) - #define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) - #define OQS_MEM_strdup(str) strdup(str) +#define OQS_MEM_malloc(size) malloc(size) +#define OQS_MEM_free(ptr) free(ptr) +#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) +#define OQS_MEM_strdup(str) strdup(str) #endif /** diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index 544e0af8d..5ead4c1ee 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -588,22 +588,22 @@ void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *state /* Destroy the hash state. */ void oqs_sha2_sha224_inc_ctx_release_c(sha224ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha256_inc_ctx_release_c(sha256ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha384_inc_ctx_release_c(sha384ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) {