From 1d92135e808fb2f9f847e080ec8ff3124ee91ac7 Mon Sep 17 00:00:00 2001 From: songlingatpan <62972259+songlingatpan@users.noreply.github.com> Date: Fri, 18 Oct 2024 22:21:51 -0700 Subject: [PATCH] [#1823] replace malloc/calloc/strdup/free with openssl allocator (#1926) * [#1823] replace malloc/calloc/strdup/free with openssl allocator Signed-off-by: Songling Han * [#1823] update memory allocator for copy_from_upstream Signed-off-by: Songling Han * [#1823] Use OpenSSL Memory Allocator for BIKE, FrodoKEM, and NTRUPrime Signed-off-by: Songling Han * [#1823] Add Comments for Doxygen Signed-off-by: Songling Han * include openssl/crypto.h and resolve conflict varible for ntru Signed-off-by: Songling Han * Add openssl version check to fix build error Signed-off-by: Songling Han * Fix build for OQS_DLOPEN_OPENSSL Signed-off-by: Songling Han * remove OQS_MEM_free Signed-off-by: Songling Han * Add allocator check in tests/test_code_conventions.py Signed-off-by: Songling Han * Add IGNORE memory-check Signed-off-by: Songling Han * Delect checked allocation functions Signed-off-by: Songling Han * Revert back p_param to p for sntrup Signed-off-by: Songling Han * Add allocator check for '.c', '.h', '.fragment' Signed-off-by: Songling Han * Add NULL for previous checked allocation Signed-off-by: Songling Han * Add fprintf error for abort cases Signed-off-by: Songling Han * use OQS_EXIT_IF_NULLPTR for checked malloc cases Signed-off-by: Songling Han --------- Signed-off-by: Songling Han --- .../src/kem/family/kem_scheme.c | 4 +- .../src/sig/family/sig_scheme.c | 4 +- .../combine_message_signature.fragment | 8 +- src/common/aes/aes128_ni.c | 2 +- src/common/aes/aes256_ni.c | 2 +- src/common/aes/aes_c.c | 8 +- src/common/aes/aes_ossl.c | 8 +- src/common/common.c | 80 +++++++------ src/common/common.h | 106 +++++++++--------- src/common/sha2/sha2_armv8.c | 8 +- src/common/sha2/sha2_c.c | 48 ++++---- src/common/sha3/ossl_sha3.c | 18 +-- src/common/sha3/ossl_sha3x4.c | 18 +-- src/common/sha3/xkcp_sha3.c | 17 ++- src/common/sha3/xkcp_sha3x4.c | 7 +- src/kem/bike/kem_bike.c | 6 +- .../kem_classic_mceliece_348864.c | 2 +- .../kem_classic_mceliece_348864f.c | 2 +- .../kem_classic_mceliece_460896.c | 2 +- .../kem_classic_mceliece_460896f.c | 2 +- .../kem_classic_mceliece_6688128.c | 2 +- .../kem_classic_mceliece_6688128f.c | 2 +- .../kem_classic_mceliece_6960119.c | 2 +- .../kem_classic_mceliece_6960119f.c | 2 +- .../kem_classic_mceliece_8192128.c | 2 +- .../kem_classic_mceliece_8192128f.c | 2 +- src/kem/frodokem/kem_frodokem1344aes.c | 2 +- src/kem/frodokem/kem_frodokem1344shake.c | 2 +- src/kem/frodokem/kem_frodokem640aes.c | 2 +- src/kem/frodokem/kem_frodokem640shake.c | 2 +- src/kem/frodokem/kem_frodokem976aes.c | 2 +- src/kem/frodokem/kem_frodokem976shake.c | 2 +- src/kem/hqc/kem_hqc_128.c | 2 +- src/kem/hqc/kem_hqc_192.c | 2 +- src/kem/hqc/kem_hqc_256.c | 2 +- src/kem/kyber/kem_kyber_1024.c | 2 +- src/kem/kyber/kem_kyber_512.c | 2 +- src/kem/kyber/kem_kyber_768.c | 2 +- src/kem/ml_kem/kem_ml_kem_1024.c | 2 +- src/kem/ml_kem/kem_ml_kem_512.c | 2 +- src/kem/ml_kem/kem_ml_kem_768.c | 2 +- src/kem/ntruprime/kem_ntruprime_sntrup761.c | 2 +- src/sig/cross/sig_cross_rsdp_128_balanced.c | 2 +- src/sig/cross/sig_cross_rsdp_128_fast.c | 2 +- src/sig/cross/sig_cross_rsdp_128_small.c | 2 +- src/sig/cross/sig_cross_rsdp_192_balanced.c | 2 +- src/sig/cross/sig_cross_rsdp_192_fast.c | 2 +- src/sig/cross/sig_cross_rsdp_192_small.c | 2 +- src/sig/cross/sig_cross_rsdp_256_balanced.c | 2 +- src/sig/cross/sig_cross_rsdp_256_fast.c | 2 +- src/sig/cross/sig_cross_rsdp_256_small.c | 2 +- src/sig/cross/sig_cross_rsdpg_128_balanced.c | 2 +- src/sig/cross/sig_cross_rsdpg_128_fast.c | 2 +- src/sig/cross/sig_cross_rsdpg_128_small.c | 2 +- src/sig/cross/sig_cross_rsdpg_192_balanced.c | 2 +- src/sig/cross/sig_cross_rsdpg_192_fast.c | 2 +- src/sig/cross/sig_cross_rsdpg_192_small.c | 2 +- src/sig/cross/sig_cross_rsdpg_256_balanced.c | 2 +- src/sig/cross/sig_cross_rsdpg_256_fast.c | 2 +- src/sig/cross/sig_cross_rsdpg_256_small.c | 2 +- src/sig/dilithium/sig_dilithium_2.c | 2 +- src/sig/dilithium/sig_dilithium_3.c | 2 +- src/sig/dilithium/sig_dilithium_5.c | 2 +- src/sig/falcon/sig_falcon_1024.c | 2 +- src/sig/falcon/sig_falcon_512.c | 2 +- src/sig/falcon/sig_falcon_padded_1024.c | 2 +- src/sig/falcon/sig_falcon_padded_512.c | 2 +- src/sig/mayo/sig_mayo_1.c | 2 +- src/sig/mayo/sig_mayo_2.c | 2 +- src/sig/mayo/sig_mayo_3.c | 2 +- src/sig/mayo/sig_mayo_5.c | 2 +- src/sig/ml_dsa/sig_ml_dsa_44_ipd.c | 4 +- src/sig/ml_dsa/sig_ml_dsa_65_ipd.c | 4 +- src/sig/ml_dsa/sig_ml_dsa_87_ipd.c | 4 +- .../sphincs/sig_sphincs_sha2_128f_simple.c | 2 +- .../sphincs/sig_sphincs_sha2_128s_simple.c | 2 +- .../sphincs/sig_sphincs_sha2_192f_simple.c | 2 +- .../sphincs/sig_sphincs_sha2_192s_simple.c | 2 +- .../sphincs/sig_sphincs_sha2_256f_simple.c | 2 +- .../sphincs/sig_sphincs_sha2_256s_simple.c | 2 +- .../sphincs/sig_sphincs_shake_128f_simple.c | 2 +- .../sphincs/sig_sphincs_shake_128s_simple.c | 2 +- .../sphincs/sig_sphincs_shake_192f_simple.c | 2 +- .../sphincs/sig_sphincs_shake_192s_simple.c | 2 +- .../sphincs/sig_sphincs_shake_256f_simple.c | 2 +- .../sphincs/sig_sphincs_shake_256s_simple.c | 2 +- src/sig_stfl/lms/CMakeLists.txt | 1 + src/sig_stfl/lms/external/common_defs.h | 2 +- src/sig_stfl/lms/external/hss_alloc.c | 20 ++-- src/sig_stfl/lms/external/hss_generate.c | 6 +- src/sig_stfl/lms/external/hss_keygen.c | 6 +- .../lms/external/hss_thread_pthread.c | 14 +-- src/sig_stfl/lms/sig_stfl_lms.c | 4 +- src/sig_stfl/lms/sig_stfl_lms_functions.c | 16 +-- src/sig_stfl/xmss/external/wots.c | 12 +- src/sig_stfl/xmss/external/xmss_commons.c | 6 +- src/sig_stfl/xmss/external/xmss_core_fast.c | 34 +++--- .../xmss/sig_stfl_xmss_secret_key_functions.c | 8 +- src/sig_stfl/xmss/sig_stfl_xmss_xmssmt.c | 2 +- tests/CMakeLists.txt | 8 +- tests/example_kem.c | 12 +- tests/example_sig.c | 10 +- tests/example_sig_stfl.c | 10 +- tests/kat_kem.c | 12 +- tests/kat_sig.c | 98 ++++++++-------- tests/kat_sig_stfl.c | 18 +-- tests/speed_common.c | 48 ++++---- tests/speed_kem.c | 12 +- tests/speed_sig.c | 10 +- tests/test_code_conventions.py | 48 +++++--- tests/test_hash.c | 34 +++--- tests/test_helpers.c | 2 +- tests/test_kem.c | 12 +- tests/test_kem_mem.c | 28 ++--- tests/test_sig.c | 10 +- tests/test_sig_mem.c | 26 ++--- tests/test_sig_stfl.c | 24 ++-- tests/vectors_kem.c | 42 +++---- tests/vectors_sig.c | 22 ++-- 119 files changed, 579 insertions(+), 528 deletions(-) diff --git a/scripts/copy_from_upstream/src/kem/family/kem_scheme.c b/scripts/copy_from_upstream/src/kem/family/kem_scheme.c index 2560d17942..108078ffcd 100644 --- a/scripts/copy_from_upstream/src/kem/family/kem_scheme.c +++ b/scripts/copy_from_upstream/src/kem/family/kem_scheme.c @@ -11,7 +11,7 @@ {% endif %} OQS_KEM *OQS_KEM_{{ family }}_{{ scheme['scheme'] }}_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } @@ -42,7 +42,7 @@ OQS_KEM *OQS_KEM_{{ family }}_{{ scheme['scheme'] }}_new(void) { /** Alias */ OQS_KEM *OQS_KEM_{{ family }}_{{ scheme['alias_scheme'] }}_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/scripts/copy_from_upstream/src/sig/family/sig_scheme.c b/scripts/copy_from_upstream/src/sig/family/sig_scheme.c index fcb364be71..235c857301 100644 --- a/scripts/copy_from_upstream/src/sig/family/sig_scheme.c +++ b/scripts/copy_from_upstream/src/sig/family/sig_scheme.c @@ -11,7 +11,7 @@ {% endif %} OQS_SIG *OQS_SIG_{{ family }}_{{ scheme['scheme'] }}_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } @@ -41,7 +41,7 @@ OQS_SIG *OQS_SIG_{{ family }}_{{ scheme['scheme'] }}_new(void) { /** Alias */ OQS_SIG *OQS_SIG_{{ family }}_{{ scheme['alias_scheme'] }}_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/scripts/copy_from_upstream/tests/kat_sig.c/combine_message_signature.fragment b/scripts/copy_from_upstream/tests/kat_sig.c/combine_message_signature.fragment index 25609f254e..2e6c215056 100644 --- a/scripts/copy_from_upstream/tests/kat_sig.c/combine_message_signature.fragment +++ b/scripts/copy_from_upstream/tests/kat_sig.c/combine_message_signature.fragment @@ -3,7 +3,7 @@ {%- if scheme['signed_msg_order'] == 'sig_then_msg' %} // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -13,7 +13,7 @@ {%- elif scheme['signed_msg_order'] == 'msg_then_sig' %} // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -24,7 +24,7 @@ // signed_msg = sig_len (2 bytes, big endian) || nonce (40 bytes) || msg || 0x29 || sig const uint16_t signature_len_uint16 = (uint16_t)signature_len; *signed_msg_len = 2 + signature_len_uint16 + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -44,7 +44,7 @@ // signed_msg = sig_len (2 bytes, big endian) || nonce (40 bytes) || msg || 0x2A || sig const uint16_t signature_len_uint16 = (uint16_t)signature_len; *signed_msg_len = 2 + signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } diff --git a/src/common/aes/aes128_ni.c b/src/common/aes/aes128_ni.c index b08a3041a4..eb2f354813 100644 --- a/src/common/aes/aes128_ni.c +++ b/src/common/aes/aes128_ni.c @@ -49,7 +49,7 @@ static inline void aes128ni_setkey_encrypt(const unsigned char *key, __m128i rke } void oqs_aes128_load_schedule_ni(const uint8_t *key, void **_schedule) { - *_schedule = malloc(sizeof(aes128ctx)); + *_schedule = OQS_MEM_malloc(sizeof(aes128ctx)); OQS_EXIT_IF_NULLPTR(*_schedule, "AES"); assert(*_schedule != NULL); __m128i *schedule = ((aes128ctx *) *_schedule)->sk_exp; diff --git a/src/common/aes/aes256_ni.c b/src/common/aes/aes256_ni.c index 33e0f35be0..eb28bc585d 100644 --- a/src/common/aes/aes256_ni.c +++ b/src/common/aes/aes256_ni.c @@ -76,7 +76,7 @@ static inline void aes256ni_setkey_encrypt(const unsigned char *key, __m128i rke } void oqs_aes256_load_schedule_ni(const uint8_t *key, void **_schedule) { - *_schedule = malloc(sizeof(aes256ctx)); + *_schedule = OQS_MEM_malloc(sizeof(aes256ctx)); OQS_EXIT_IF_NULLPTR(*_schedule, "AES"); assert(*_schedule != NULL); __m128i *schedule = ((aes256ctx *) *_schedule)->sk_exp; diff --git a/src/common/aes/aes_c.c b/src/common/aes/aes_c.c index f2ec57a500..dfbf0abc13 100644 --- a/src/common/aes/aes_c.c +++ b/src/common/aes/aes_c.c @@ -676,7 +676,7 @@ static void aes_ctr(unsigned char *out, size_t outlen, const unsigned char *iv, } void oqs_aes128_load_schedule_c(const uint8_t *key, void **_schedule) { - *_schedule = malloc(sizeof(aes128ctx)); + *_schedule = OQS_MEM_malloc(sizeof(aes128ctx)); OQS_EXIT_IF_NULLPTR(*_schedule, "AES"); aes128ctx *ctx = (aes128ctx *) *_schedule; uint64_t skey[22]; @@ -685,7 +685,7 @@ void oqs_aes128_load_schedule_c(const uint8_t *key, void **_schedule) { } void oqs_aes256_load_schedule_c(const uint8_t *key, void **_schedule) { - *_schedule = malloc(sizeof(aes256ctx)); + *_schedule = OQS_MEM_malloc(sizeof(aes256ctx)); OQS_EXIT_IF_NULLPTR(*_schedule, "AES"); aes256ctx *ctx = (aes256ctx *) *_schedule; uint64_t skey[30]; @@ -719,7 +719,7 @@ static void aes_keysched_no_bitslice(uint32_t *skey, const unsigned char *key, u } void oqs_aes256_load_schedule_no_bitslice(const uint8_t *key, void **_schedule) { - *_schedule = malloc(sizeof(aes256ctx_nobitslice)); + *_schedule = OQS_MEM_malloc(sizeof(aes256ctx_nobitslice)); assert(*_schedule != NULL); uint32_t *schedule = ((aes256ctx_nobitslice *) *_schedule)->sk_exp; aes_keysched_no_bitslice(schedule, (const unsigned char *) key, 32); @@ -752,7 +752,7 @@ void oqs_aes256_load_iv_u64_c(uint64_t iv, void *schedule) { } void oqs_aes128_load_schedule_no_bitslice(const uint8_t *key, void **_schedule) { - *_schedule = malloc(44 * sizeof(int)); + *_schedule = OQS_MEM_malloc(44 * sizeof(int)); assert(*_schedule != NULL); uint32_t *schedule = (uint32_t *) *_schedule; aes_keysched_no_bitslice(schedule, (const unsigned char *) key, 16); diff --git a/src/common/aes/aes_ossl.c b/src/common/aes/aes_ossl.c index c7dc5b9445..ff971c874b 100644 --- a/src/common/aes/aes_ossl.c +++ b/src/common/aes/aes_ossl.c @@ -28,7 +28,7 @@ static inline void br_enc64be(unsigned char *dst, uint64_t x) { } static void AES128_ECB_load_schedule(const uint8_t *key, void **schedule) { - *schedule = malloc(sizeof(struct key_schedule)); + *schedule = OQS_MEM_malloc(sizeof(struct key_schedule)); OQS_EXIT_IF_NULLPTR(*schedule, "OpenSSL"); struct key_schedule *ks = (struct key_schedule *) *schedule; ks->for_ECB = 1; @@ -93,7 +93,7 @@ static void AES128_CTR_inc_stream_iv(const uint8_t *iv, size_t iv_len, const voi } static void AES128_CTR_inc_init(const uint8_t *key, void **schedule) { - *schedule = malloc(sizeof(struct key_schedule)); + *schedule = OQS_MEM_malloc(sizeof(struct key_schedule)); OQS_EXIT_IF_NULLPTR(*schedule, "OpenSSL"); struct key_schedule *ks = (struct key_schedule *) *schedule; @@ -128,7 +128,7 @@ static void AES128_CTR_inc_ivu64(uint64_t iv, void *schedule) { } static void AES256_ECB_load_schedule(const uint8_t *key, void **schedule) { - *schedule = malloc(sizeof(struct key_schedule)); + *schedule = OQS_MEM_malloc(sizeof(struct key_schedule)); OQS_EXIT_IF_NULLPTR(*schedule, "OpenSSL"); struct key_schedule *ks = (struct key_schedule *) *schedule; ks->for_ECB = 1; @@ -139,7 +139,7 @@ static void AES256_ECB_load_schedule(const uint8_t *key, void **schedule) { } static void AES256_CTR_inc_init(const uint8_t *key, void **schedule) { - *schedule = malloc(sizeof(struct key_schedule)); + *schedule = OQS_MEM_malloc(sizeof(struct key_schedule)); OQS_EXIT_IF_NULLPTR(*schedule, "OpenSSL"); struct key_schedule *ks = (struct key_schedule *) *schedule; diff --git a/src/common/common.c b/src/common/common.c index 35df0e8be3..78d0dcb247 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -13,6 +13,7 @@ #include #include #include +#include #if defined(OQS_DIST_BUILD) && defined(OQS_USE_PTHREADS) #include @@ -257,6 +258,9 @@ OQS_API int OQS_MEM_secure_bcmp(const void *a, const void *b, size_t len) { } OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { + if (ptr == NULL) { + return; + } #if defined(OQS_USE_OPENSSL) OSSL_FUNC(OPENSSL_cleanse)(ptr, len); #elif defined(_WIN32) @@ -276,39 +280,44 @@ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { #endif } -void *OQS_MEM_checked_malloc(size_t len) { - void *ptr = malloc(len); - if (ptr == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } - - return ptr; -} - -void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { - void *ptr = OQS_MEM_aligned_alloc(alignment, size); - if (ptr == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } - - return ptr; -} - OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - free(ptr); // IGNORE free-check + OQS_MEM_insecure_free(ptr); } } OQS_API void OQS_MEM_insecure_free(void *ptr) { - free(ptr); // IGNORE free-check +#if (defined(OQS_USE_OPENSSL) || defined(OQS_DLOPEN_OPENSSL)) && defined(OPENSSL_VERSION_NUMBER) + OPENSSL_free(ptr); +#else + free(ptr); // IGNORE memory-check +#endif } void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { -#if defined(OQS_HAVE_ALIGNED_ALLOC) // glibc and other implementations providing aligned_alloc +#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] = (uint8_t)diff; + return ptr; +#elif defined(OQS_HAVE_ALIGNED_ALLOC) // glibc and other implementations providing aligned_alloc return aligned_alloc(alignment, size); #else // Check alignment (power of 2, and >= sizeof(void*)) and size (multiple of alignment) @@ -347,7 +356,7 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { // | // diff = ptr - buffer const size_t offset = alignment - 1 + sizeof(uint8_t); - uint8_t *buffer = malloc(size + offset); + uint8_t *buffer = malloc(size + offset); // IGNORE memory-check if (!buffer) { return NULL; } @@ -357,7 +366,7 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { ptrdiff_t diff = ptr - buffer; if (diff > UINT8_MAX) { // This should never happen in our code, but just to be safe - free(buffer); // IGNORE free-check + free(buffer); // IGNORE memory-check errno = EINVAL; return NULL; } @@ -370,18 +379,23 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { } void OQS_MEM_aligned_free(void *ptr) { -#if defined(OQS_HAVE_ALIGNED_ALLOC) || defined(OQS_HAVE_POSIX_MEMALIGN) || defined(OQS_HAVE_MEMALIGN) - free(ptr); // IGNORE free-check + if (ptr == NULL) { + return; + } +#if defined(OQS_USE_OPENSSL) + // Use OpenSSL's free function + 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 memory-check #elif defined(__MINGW32__) || defined(__MINGW64__) __mingw_aligned_free(ptr); #elif defined(_MSC_VER) _aligned_free(ptr); #else - if (ptr) { - // Reconstruct the pointer returned from malloc using the difference - // stored one byte ahead of ptr. - uint8_t *u8ptr = ptr; - free(u8ptr - u8ptr[-1]); // IGNORE free-check - } + // Reconstruct the pointer returned from malloc using the difference + // stored one byte ahead of ptr. + uint8_t *u8ptr = ptr; + free(u8ptr - u8ptr[-1]); // IGNORE memory-check #endif } diff --git a/src/common/common.h b/src/common/common.h index 18993d0a5f..b15e244a39 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -20,6 +20,59 @@ extern "C" { #endif +/** + * @brief Memory allocation and deallocation functions. + * + * These macros provide a unified interface for memory operations, + * using OpenSSL functions when OQS_USE_OPENSSL is defined, and + * standard C library functions otherwise. + */ +#if (defined(OQS_USE_OPENSSL) || defined(OQS_DLOPEN_OPENSSL)) && defined(OPENSSL_VERSION_NUMBER) +#include + +/** +* Allocates memory of a given size. +* @param size The size of the memory to be allocated in bytes. +* @return A pointer to the allocated memory. +*/ +#define OQS_MEM_malloc(size) OPENSSL_malloc(size) + +/** + * Allocates memory for an array of elements of a given size. + * @param num_elements The number of elements to allocate. + * @param element_size The size of each element in bytes. + * @return A pointer to the allocated memory. + */ +#define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size)) +/** + * Duplicates a string. + * @param str The string to be duplicated. + * @return A pointer to the newly allocated string. + */ +#define OQS_MEM_strdup(str) OPENSSL_strdup(str) +#else +/** +* Allocates memory of a given size. +* @param size The size of the memory to be allocated in bytes. +* @return A pointer to the allocated memory. +*/ +#define OQS_MEM_malloc(size) malloc(size) // IGNORE memory-check + +/** + * Allocates memory for an array of elements of a given size. + * @param num_elements The number of elements to allocate. + * @param element_size The size of each element in bytes. + * @return A pointer to the allocated memory. + */ +#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) // IGNORE memory-check +/** + * Duplicates a string. + * @param str The string to be duplicated. + * @return A pointer to the newly allocated string. + */ +#define OQS_MEM_strdup(str) strdup(str) // IGNORE memory-check +#endif + /** * Macro for terminating the program if x is * a null pointer. @@ -193,59 +246,6 @@ OQS_API int OQS_MEM_secure_bcmp(const void *a, const void *b, size_t len); */ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len); -/** - * Allocates memory of a specified size and checks for successful allocation. - * - * This function attempts to allocate a block of memory of the specified size. - * If the allocation is successful, it returns a pointer to the beginning of the - * memory block. If the allocation fails, it prints an error message to stderr - * and terminates the program. - * - * @param[in] len The size of the memory block to allocate, in bytes. - * - * @return A pointer to the allocated memory block if the allocation is successful. - * - * @note This function is intended to be used when the allocation must succeed, - * and failure to allocate memory is considered a fatal error. As such, - * it does not return if the allocation fails, but instead terminates the - * program with an exit status indicating failure. - * - * @note The memory block returned by this function is not initialized. The caller - * is responsible for initializing the memory if required. - * - * @note The allocated memory should be freed using the standard `free` function - * when it is no longer needed. - */ -void *OQS_MEM_checked_malloc(size_t len); - -/** - * Allocates memory of a specified size and alignment and checks for successful allocation. - * - * This function attempts to allocate a block of memory with the specified size - * and alignment. If the allocation is successful, it returns a pointer to the - * memory block. If the allocation fails, it prints an error message to stderr - * and terminates the program. - * - * Alignment must be a power of two and a multiple of sizeof(void *). - * - * @param[in] alignment The alignment of the memory block to allocate. - * @param[in] size The size of the memory block to allocate, in bytes. - * - * @return A pointer to the allocated memory block if the allocation is successful. - * - * @note This function is intended to be used when the allocation must succeed, - * and failure to allocate memory is considered a fatal error. As such, - * it does not return if the allocation fails, but instead terminates the - * program with an exit status indicating failure. - * - * @note The memory block returned by this function is not initialized. The caller - * is responsible for initializing the memory if required. - * - * @note The allocated memory should be freed with `OQS_MEM_aligned_free` when it - * is no longer needed. - */ -void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size); - /** * Zeros out `len` bytes of memory starting at `ptr`, then frees `ptr`. * diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index 65ea6750c3..2ca7325cb8 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -180,8 +180,8 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui new_in = in; } else { // Combine incremental data with final input - tmp_in = OQS_MEM_checked_malloc(tmp_len); - + tmp_in = OQS_MEM_malloc(tmp_len); + OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2"); memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { memcpy(tmp_in + state->data_len, in, inlen); @@ -254,8 +254,8 @@ void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_ /* Process any existing incremental data first */ if (state->data_len) { - tmp_in = OQS_MEM_checked_malloc(buf_len); - + tmp_in = OQS_MEM_malloc(buf_len); + OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2"); memcpy(tmp_in, state->data, state->data_len); memcpy(tmp_in + state->data_len, in, buf_len - state->data_len); diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index e5bd350889..ba71abab23 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -502,8 +502,8 @@ static const uint8_t iv_512[64] = { }; void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { - state->ctx = OQS_MEM_checked_malloc(PQC_SHA256CTX_BYTES); - + state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_224[i]; } @@ -516,8 +516,8 @@ void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { state->data_len = 0; - state->ctx = OQS_MEM_checked_malloc(PQC_SHA256CTX_BYTES); - + state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_256[i]; } @@ -529,8 +529,8 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { } void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { - state->ctx = OQS_MEM_checked_malloc(PQC_SHA512CTX_BYTES); - + state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_384[i]; } @@ -542,8 +542,8 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { } void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { - state->ctx = OQS_MEM_checked_malloc(PQC_SHA512CTX_BYTES); - + state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_512[i]; } @@ -555,32 +555,32 @@ void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { } void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *statein) { - stateout->ctx = OQS_MEM_checked_malloc(PQC_SHA256CTX_BYTES); - + stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); + OQS_EXIT_IF_NULLPTR(stateout->ctx, "SHA2"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); } void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) { - stateout->ctx = OQS_MEM_checked_malloc(PQC_SHA256CTX_BYTES); - + stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); + OQS_EXIT_IF_NULLPTR(stateout->ctx, "SHA2"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); } void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { - stateout->ctx = OQS_MEM_checked_malloc(PQC_SHA512CTX_BYTES); - + stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); + OQS_EXIT_IF_NULLPTR(stateout->ctx, "SHA2"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); } void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) { - stateout->ctx = OQS_MEM_checked_malloc(PQC_SHA512CTX_BYTES); - + stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); + OQS_EXIT_IF_NULLPTR(stateout->ctx, "SHA2"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); @@ -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) { - free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); } /* Destroy the hash state. */ void oqs_sha2_sha256_inc_ctx_release_c(sha256ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); } /* Destroy the hash state. */ void oqs_sha2_sha384_inc_ctx_release_c(sha384ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); } /* Destroy the hash state. */ void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); } void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) { @@ -614,8 +614,8 @@ void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t in /* Process any existing incremental data first */ if (state->data_len) { - tmp_in = OQS_MEM_checked_malloc(tmp_buflen); - + tmp_in = OQS_MEM_malloc(tmp_buflen); + OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2"); memcpy(tmp_in, state->data, state->data_len); memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len); @@ -691,8 +691,8 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_ if (new_inlen == inlen) { new_in = in; } else { //Combine incremental data with final input - tmp_in = OQS_MEM_checked_malloc(tmp_len); - + tmp_in = OQS_MEM_malloc(tmp_len); + OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2"); memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { memcpy(tmp_in + state->data_len, in, inlen); diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index 5d36f2280c..de8919bb8c 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -168,7 +168,7 @@ typedef struct { } intrn_shake128_inc_ctx; static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) { - state->ctx = malloc(sizeof(intrn_shake128_inc_ctx)); + state->ctx = OQS_MEM_malloc(sizeof(intrn_shake128_inc_ctx)); intrn_shake128_inc_ctx *s = (intrn_shake128_inc_ctx *)state->ctx; s->mdctx = OSSL_FUNC(EVP_MD_CTX_new)(); @@ -198,10 +198,11 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s if (s->n_out == 0) { OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { - uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); + uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + OQS_EXIT_IF_NULLPTR(tmp, "SHA3"); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); - free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -211,7 +212,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s static void SHA3_shake128_inc_ctx_release(OQS_SHA3_shake128_inc_ctx *state) { intrn_shake128_inc_ctx *s = (intrn_shake128_inc_ctx *)state->ctx; OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx); - free(s); // IGNORE free-check + OQS_MEM_insecure_free(s); // IGNORE free-check } static void SHA3_shake128_inc_ctx_clone(OQS_SHA3_shake128_inc_ctx *dest, const OQS_SHA3_shake128_inc_ctx *src) { @@ -242,7 +243,7 @@ typedef struct { } intrn_shake256_inc_ctx; static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) { - state->ctx = malloc(sizeof(intrn_shake256_inc_ctx)); + state->ctx = OQS_MEM_malloc(sizeof(intrn_shake256_inc_ctx)); intrn_shake256_inc_ctx *s = (intrn_shake256_inc_ctx *)state->ctx; s->mdctx = OSSL_FUNC(EVP_MD_CTX_new)(); @@ -272,10 +273,11 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s if (s->n_out == 0) { OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { - uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); + uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + OQS_EXIT_IF_NULLPTR(tmp, "SHA3"); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); - free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -285,7 +287,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s static void SHA3_shake256_inc_ctx_release(OQS_SHA3_shake256_inc_ctx *state) { intrn_shake256_inc_ctx *s = (intrn_shake256_inc_ctx *)state->ctx; OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx); - free(s); // IGNORE free-check + OQS_MEM_insecure_free(s); // IGNORE free-check } static void SHA3_shake256_inc_ctx_clone(OQS_SHA3_shake256_inc_ctx *dest, const OQS_SHA3_shake256_inc_ctx *src) { diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index 1f6a03c615..4bdee4d2be 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -33,7 +33,7 @@ typedef struct { } intrn_shake128_x4_inc_ctx; static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) { - state->ctx = malloc(sizeof(intrn_shake128_x4_inc_ctx)); + state->ctx = OQS_MEM_malloc(sizeof(intrn_shake128_x4_inc_ctx)); intrn_shake128_x4_inc_ctx *s = (intrn_shake128_x4_inc_ctx *)state->ctx; s->mdctx0 = OSSL_FUNC(EVP_MD_CTX_new)(); @@ -81,7 +81,8 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx3); OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { - uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); + uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + OQS_EXIT_IF_NULLPTR(tmp, "SHA3x4"); OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(out0, tmp + s->n_out, outlen); @@ -94,7 +95,7 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx3); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(out3, tmp + s->n_out, outlen); - free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -117,7 +118,7 @@ static void SHA3_shake128_x4_inc_ctx_release(OQS_SHA3_shake128_x4_inc_ctx *state OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx1); OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx2); OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx3); - free(s); // IGNORE free-check + OQS_MEM_insecure_free(s); } static void SHA3_shake128_x4_inc_ctx_reset(OQS_SHA3_shake128_x4_inc_ctx *state) { @@ -154,7 +155,7 @@ typedef struct { } intrn_shake256_x4_inc_ctx; static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) { - state->ctx = malloc(sizeof(intrn_shake256_x4_inc_ctx)); + state->ctx = OQS_MEM_malloc(sizeof(intrn_shake256_x4_inc_ctx)); intrn_shake256_x4_inc_ctx *s = (intrn_shake256_x4_inc_ctx *)state->ctx; s->mdctx0 = OSSL_FUNC(EVP_MD_CTX_new)(); @@ -202,7 +203,8 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx3); OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { - uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); + uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + OQS_EXIT_IF_NULLPTR(tmp, "SHA3x4"); OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(out0, tmp + s->n_out, outlen); @@ -215,7 +217,7 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx3); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(out3, tmp + s->n_out, outlen); - free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -238,7 +240,7 @@ static void SHA3_shake256_x4_inc_ctx_release(OQS_SHA3_shake256_x4_inc_ctx *state OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx1); OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx2); OSSL_FUNC(EVP_MD_CTX_free)(s->mdctx3); - free(s); // IGNORE free-check + OQS_MEM_insecure_free(s); } static void SHA3_shake256_x4_inc_ctx_reset(OQS_SHA3_shake256_x4_inc_ctx *state) { diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 196652d85d..32b0db6a8c 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -199,7 +199,9 @@ static void SHA3_sha3_256(uint8_t *output, const uint8_t *input, size_t inlen) { } static void SHA3_sha3_256_inc_init(OQS_SHA3_sha3_256_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -235,10 +237,10 @@ static void SHA3_sha3_384(uint8_t *output, const uint8_t *input, size_t inlen) { } static void SHA3_sha3_384_inc_init(OQS_SHA3_sha3_384_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } - static void SHA3_sha3_384_inc_absorb(OQS_SHA3_sha3_384_inc_ctx *state, const uint8_t *input, size_t inlen) { keccak_inc_absorb((uint64_t *)state->ctx, OQS_SHA3_SHA3_384_RATE, input, inlen); } @@ -271,7 +273,8 @@ static void SHA3_sha3_512(uint8_t *output, const uint8_t *input, size_t inlen) { } static void SHA3_sha3_512_inc_init(OQS_SHA3_sha3_512_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -310,7 +313,8 @@ static void SHA3_shake128(uint8_t *output, size_t outlen, const uint8_t *input, /* SHAKE128 incremental */ static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -352,7 +356,8 @@ static void SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input, /* SHAKE256 incremental */ static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index bd441a01ff..893744def8 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -167,10 +167,10 @@ static void SHA3_shake128_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ /* SHAKE128 incremental */ static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3x4"); keccak_x4_inc_reset((uint64_t *)state->ctx); } - static void SHA3_shake128_x4_inc_absorb(OQS_SHA3_shake128_x4_inc_ctx *state, const uint8_t *in0, const uint8_t *in1, const uint8_t *in2, const uint8_t *in3, size_t inlen) { keccak_x4_inc_absorb((uint64_t *)state->ctx, OQS_SHA3_SHAKE128_RATE, in0, in1, in2, in3, inlen); } @@ -209,7 +209,8 @@ static void SHA3_shake256_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ /* SHAKE256 incremental */ static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) { - state->ctx = OQS_MEM_checked_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3x4"); keccak_x4_inc_reset((uint64_t *)state->ctx); } diff --git a/src/kem/bike/kem_bike.c b/src/kem/bike/kem_bike.c index def6358e3d..52c4f3d049 100644 --- a/src/kem/bike/kem_bike.c +++ b/src/kem/bike/kem_bike.c @@ -6,7 +6,7 @@ #ifdef OQS_ENABLE_KEM_bike_l1 OQS_KEM *OQS_KEM_bike_l1_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } @@ -31,7 +31,7 @@ OQS_KEM *OQS_KEM_bike_l1_new(void) { #ifdef OQS_ENABLE_KEM_bike_l3 OQS_KEM *OQS_KEM_bike_l3_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } @@ -56,7 +56,7 @@ OQS_KEM *OQS_KEM_bike_l3_new(void) { #ifdef OQS_ENABLE_KEM_bike_l5 OQS_KEM *OQS_KEM_bike_l5_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_348864.c b/src/kem/classic_mceliece/kem_classic_mceliece_348864.c index 87b74a1b98..b935b8c6ff 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_348864.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_348864.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_348864_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_348864f.c b/src/kem/classic_mceliece/kem_classic_mceliece_348864f.c index a3c347af8f..e54ad855cf 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_348864f.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_348864f.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_348864f_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_460896.c b/src/kem/classic_mceliece/kem_classic_mceliece_460896.c index 839f8d4535..458d8a95c7 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_460896.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_460896.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_460896_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_460896f.c b/src/kem/classic_mceliece/kem_classic_mceliece_460896f.c index 0629166487..f1cbc1ee88 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_460896f.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_460896f.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_460896f_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_6688128.c b/src/kem/classic_mceliece/kem_classic_mceliece_6688128.c index 91a70105dc..9b302f1975 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_6688128.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_6688128.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_6688128_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_6688128f.c b/src/kem/classic_mceliece/kem_classic_mceliece_6688128f.c index d97e5c3501..2ccf2e2f79 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_6688128f.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_6688128f.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_6688128f_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_6960119.c b/src/kem/classic_mceliece/kem_classic_mceliece_6960119.c index 70d828fcdd..31ebbe0532 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_6960119.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_6960119.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_6960119_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_6960119f.c b/src/kem/classic_mceliece/kem_classic_mceliece_6960119f.c index 5c4bfa71a7..45f2e624e4 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_6960119f.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_6960119f.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_6960119f_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_8192128.c b/src/kem/classic_mceliece/kem_classic_mceliece_8192128.c index 4dc8fe263b..df39ea18d6 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_8192128.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_8192128.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_8192128_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/classic_mceliece/kem_classic_mceliece_8192128f.c b/src/kem/classic_mceliece/kem_classic_mceliece_8192128f.c index 81981c1fad..c1253440ce 100644 --- a/src/kem/classic_mceliece/kem_classic_mceliece_8192128f.c +++ b/src/kem/classic_mceliece/kem_classic_mceliece_8192128f.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_classic_mceliece_8192128f_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/frodokem/kem_frodokem1344aes.c b/src/kem/frodokem/kem_frodokem1344aes.c index 607061545b..199db1dace 100644 --- a/src/kem/frodokem/kem_frodokem1344aes.c +++ b/src/kem/frodokem/kem_frodokem1344aes.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_frodokem_1344_aes_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/frodokem/kem_frodokem1344shake.c b/src/kem/frodokem/kem_frodokem1344shake.c index c4a20c727d..9ab6d186b0 100644 --- a/src/kem/frodokem/kem_frodokem1344shake.c +++ b/src/kem/frodokem/kem_frodokem1344shake.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_frodokem_1344_shake_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/frodokem/kem_frodokem640aes.c b/src/kem/frodokem/kem_frodokem640aes.c index 2d9b38bf9c..c582013065 100644 --- a/src/kem/frodokem/kem_frodokem640aes.c +++ b/src/kem/frodokem/kem_frodokem640aes.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_frodokem_640_aes_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/frodokem/kem_frodokem640shake.c b/src/kem/frodokem/kem_frodokem640shake.c index 29db4de20f..1ff37c30a3 100644 --- a/src/kem/frodokem/kem_frodokem640shake.c +++ b/src/kem/frodokem/kem_frodokem640shake.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_frodokem_640_shake_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/frodokem/kem_frodokem976aes.c b/src/kem/frodokem/kem_frodokem976aes.c index 3d6b07a07c..fadf28373d 100644 --- a/src/kem/frodokem/kem_frodokem976aes.c +++ b/src/kem/frodokem/kem_frodokem976aes.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_frodokem_976_aes_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/frodokem/kem_frodokem976shake.c b/src/kem/frodokem/kem_frodokem976shake.c index 6cda90384f..cdc4d2964f 100644 --- a/src/kem/frodokem/kem_frodokem976shake.c +++ b/src/kem/frodokem/kem_frodokem976shake.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_frodokem_976_shake_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/hqc/kem_hqc_128.c b/src/kem/hqc/kem_hqc_128.c index edabb8c2f2..300ff93e02 100644 --- a/src/kem/hqc/kem_hqc_128.c +++ b/src/kem/hqc/kem_hqc_128.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_hqc_128_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/hqc/kem_hqc_192.c b/src/kem/hqc/kem_hqc_192.c index 3da83c5e9e..e0e6d676aa 100644 --- a/src/kem/hqc/kem_hqc_192.c +++ b/src/kem/hqc/kem_hqc_192.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_hqc_192_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/hqc/kem_hqc_256.c b/src/kem/hqc/kem_hqc_256.c index a80197c228..6cd66b634b 100644 --- a/src/kem/hqc/kem_hqc_256.c +++ b/src/kem/hqc/kem_hqc_256.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_hqc_256_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/kyber/kem_kyber_1024.c b/src/kem/kyber/kem_kyber_1024.c index db72b23cd5..44c8879b1a 100644 --- a/src/kem/kyber/kem_kyber_1024.c +++ b/src/kem/kyber/kem_kyber_1024.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_kyber_1024_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/kyber/kem_kyber_512.c b/src/kem/kyber/kem_kyber_512.c index e2b870713d..db6618fe45 100644 --- a/src/kem/kyber/kem_kyber_512.c +++ b/src/kem/kyber/kem_kyber_512.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_kyber_512_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/kyber/kem_kyber_768.c b/src/kem/kyber/kem_kyber_768.c index d1b792b5ce..263f8a081d 100644 --- a/src/kem/kyber/kem_kyber_768.c +++ b/src/kem/kyber/kem_kyber_768.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_kyber_768_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/ml_kem/kem_ml_kem_1024.c b/src/kem/ml_kem/kem_ml_kem_1024.c index 51297a61f0..8fc125999e 100644 --- a/src/kem/ml_kem/kem_ml_kem_1024.c +++ b/src/kem/ml_kem/kem_ml_kem_1024.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_ml_kem_1024_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/ml_kem/kem_ml_kem_512.c b/src/kem/ml_kem/kem_ml_kem_512.c index ec1e147c5c..fffc647dcb 100644 --- a/src/kem/ml_kem/kem_ml_kem_512.c +++ b/src/kem/ml_kem/kem_ml_kem_512.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_ml_kem_512_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/ml_kem/kem_ml_kem_768.c b/src/kem/ml_kem/kem_ml_kem_768.c index 789e3ffd76..a8d0e8a3ff 100644 --- a/src/kem/ml_kem/kem_ml_kem_768.c +++ b/src/kem/ml_kem/kem_ml_kem_768.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_ml_kem_768_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/kem/ntruprime/kem_ntruprime_sntrup761.c b/src/kem/ntruprime/kem_ntruprime_sntrup761.c index bc69b36209..f8c4567641 100644 --- a/src/kem/ntruprime/kem_ntruprime_sntrup761.c +++ b/src/kem/ntruprime/kem_ntruprime_sntrup761.c @@ -8,7 +8,7 @@ OQS_KEM *OQS_KEM_ntruprime_sntrup761_new(void) { - OQS_KEM *kem = malloc(sizeof(OQS_KEM)); + OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM)); if (kem == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_128_balanced.c b/src/sig/cross/sig_cross_rsdp_128_balanced.c index 0460004239..0bf311bf51 100644 --- a/src/sig/cross/sig_cross_rsdp_128_balanced.c +++ b/src/sig/cross/sig_cross_rsdp_128_balanced.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_128_balanced_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_128_fast.c b/src/sig/cross/sig_cross_rsdp_128_fast.c index 5ad9f0ae31..f981c6feda 100644 --- a/src/sig/cross/sig_cross_rsdp_128_fast.c +++ b/src/sig/cross/sig_cross_rsdp_128_fast.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_128_fast_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_128_small.c b/src/sig/cross/sig_cross_rsdp_128_small.c index 24fb6f6cb7..6cd10ea3a9 100644 --- a/src/sig/cross/sig_cross_rsdp_128_small.c +++ b/src/sig/cross/sig_cross_rsdp_128_small.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_128_small_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_192_balanced.c b/src/sig/cross/sig_cross_rsdp_192_balanced.c index f4cc950cb9..c698b268a5 100644 --- a/src/sig/cross/sig_cross_rsdp_192_balanced.c +++ b/src/sig/cross/sig_cross_rsdp_192_balanced.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_192_balanced_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_192_fast.c b/src/sig/cross/sig_cross_rsdp_192_fast.c index 8bed778f65..64556713fa 100644 --- a/src/sig/cross/sig_cross_rsdp_192_fast.c +++ b/src/sig/cross/sig_cross_rsdp_192_fast.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_192_fast_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_192_small.c b/src/sig/cross/sig_cross_rsdp_192_small.c index 53864a8661..df57669b54 100644 --- a/src/sig/cross/sig_cross_rsdp_192_small.c +++ b/src/sig/cross/sig_cross_rsdp_192_small.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_192_small_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_256_balanced.c b/src/sig/cross/sig_cross_rsdp_256_balanced.c index 3cd07a60bb..da1e03494a 100644 --- a/src/sig/cross/sig_cross_rsdp_256_balanced.c +++ b/src/sig/cross/sig_cross_rsdp_256_balanced.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_256_balanced_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_256_fast.c b/src/sig/cross/sig_cross_rsdp_256_fast.c index 3c954f9a28..e11218a3d8 100644 --- a/src/sig/cross/sig_cross_rsdp_256_fast.c +++ b/src/sig/cross/sig_cross_rsdp_256_fast.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_256_fast_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdp_256_small.c b/src/sig/cross/sig_cross_rsdp_256_small.c index f20039a911..8c2a2512df 100644 --- a/src/sig/cross/sig_cross_rsdp_256_small.c +++ b/src/sig/cross/sig_cross_rsdp_256_small.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdp_256_small_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_128_balanced.c b/src/sig/cross/sig_cross_rsdpg_128_balanced.c index 2877847715..5e6ecd898c 100644 --- a/src/sig/cross/sig_cross_rsdpg_128_balanced.c +++ b/src/sig/cross/sig_cross_rsdpg_128_balanced.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_128_balanced_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_128_fast.c b/src/sig/cross/sig_cross_rsdpg_128_fast.c index cf10d6298d..cfc4393e5a 100644 --- a/src/sig/cross/sig_cross_rsdpg_128_fast.c +++ b/src/sig/cross/sig_cross_rsdpg_128_fast.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_128_fast_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_128_small.c b/src/sig/cross/sig_cross_rsdpg_128_small.c index e2cab5d433..455b82238c 100644 --- a/src/sig/cross/sig_cross_rsdpg_128_small.c +++ b/src/sig/cross/sig_cross_rsdpg_128_small.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_128_small_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_192_balanced.c b/src/sig/cross/sig_cross_rsdpg_192_balanced.c index e85be184be..ead0d35b43 100644 --- a/src/sig/cross/sig_cross_rsdpg_192_balanced.c +++ b/src/sig/cross/sig_cross_rsdpg_192_balanced.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_192_balanced_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_192_fast.c b/src/sig/cross/sig_cross_rsdpg_192_fast.c index 4c600ae14f..1b421b767b 100644 --- a/src/sig/cross/sig_cross_rsdpg_192_fast.c +++ b/src/sig/cross/sig_cross_rsdpg_192_fast.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_192_fast_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_192_small.c b/src/sig/cross/sig_cross_rsdpg_192_small.c index 624f02e458..8b3c091c11 100644 --- a/src/sig/cross/sig_cross_rsdpg_192_small.c +++ b/src/sig/cross/sig_cross_rsdpg_192_small.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_192_small_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_256_balanced.c b/src/sig/cross/sig_cross_rsdpg_256_balanced.c index 73f12fafbf..0e7ce61e84 100644 --- a/src/sig/cross/sig_cross_rsdpg_256_balanced.c +++ b/src/sig/cross/sig_cross_rsdpg_256_balanced.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_256_balanced_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_256_fast.c b/src/sig/cross/sig_cross_rsdpg_256_fast.c index 1ebfa2c224..83105f1c8d 100644 --- a/src/sig/cross/sig_cross_rsdpg_256_fast.c +++ b/src/sig/cross/sig_cross_rsdpg_256_fast.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_256_fast_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/cross/sig_cross_rsdpg_256_small.c b/src/sig/cross/sig_cross_rsdpg_256_small.c index 6f105167e2..72d8546aab 100644 --- a/src/sig/cross/sig_cross_rsdpg_256_small.c +++ b/src/sig/cross/sig_cross_rsdpg_256_small.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_cross_rsdpg_256_small_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/dilithium/sig_dilithium_2.c b/src/sig/dilithium/sig_dilithium_2.c index de1b2ad1fa..89372111b3 100644 --- a/src/sig/dilithium/sig_dilithium_2.c +++ b/src/sig/dilithium/sig_dilithium_2.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_dilithium_2_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/dilithium/sig_dilithium_3.c b/src/sig/dilithium/sig_dilithium_3.c index f1a8311a20..20cef1bf5d 100644 --- a/src/sig/dilithium/sig_dilithium_3.c +++ b/src/sig/dilithium/sig_dilithium_3.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_dilithium_3_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/dilithium/sig_dilithium_5.c b/src/sig/dilithium/sig_dilithium_5.c index 06871be6e6..2e7a985682 100644 --- a/src/sig/dilithium/sig_dilithium_5.c +++ b/src/sig/dilithium/sig_dilithium_5.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_dilithium_5_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/falcon/sig_falcon_1024.c b/src/sig/falcon/sig_falcon_1024.c index 06562cdea7..b306f0c4b8 100644 --- a/src/sig/falcon/sig_falcon_1024.c +++ b/src/sig/falcon/sig_falcon_1024.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_falcon_1024_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/falcon/sig_falcon_512.c b/src/sig/falcon/sig_falcon_512.c index d77e3eb735..d66f51d266 100644 --- a/src/sig/falcon/sig_falcon_512.c +++ b/src/sig/falcon/sig_falcon_512.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_falcon_512_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/falcon/sig_falcon_padded_1024.c b/src/sig/falcon/sig_falcon_padded_1024.c index 53b8c3926d..0389fc97e6 100644 --- a/src/sig/falcon/sig_falcon_padded_1024.c +++ b/src/sig/falcon/sig_falcon_padded_1024.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_falcon_padded_1024_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/falcon/sig_falcon_padded_512.c b/src/sig/falcon/sig_falcon_padded_512.c index 9521187b80..781e7bf75a 100644 --- a/src/sig/falcon/sig_falcon_padded_512.c +++ b/src/sig/falcon/sig_falcon_padded_512.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_falcon_padded_512_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/mayo/sig_mayo_1.c b/src/sig/mayo/sig_mayo_1.c index 93034eb5ef..9d78196278 100644 --- a/src/sig/mayo/sig_mayo_1.c +++ b/src/sig/mayo/sig_mayo_1.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_mayo_1_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/mayo/sig_mayo_2.c b/src/sig/mayo/sig_mayo_2.c index 25719f0aee..1dbdadddad 100644 --- a/src/sig/mayo/sig_mayo_2.c +++ b/src/sig/mayo/sig_mayo_2.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_mayo_2_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/mayo/sig_mayo_3.c b/src/sig/mayo/sig_mayo_3.c index 7a68024880..ba8ee36dc6 100644 --- a/src/sig/mayo/sig_mayo_3.c +++ b/src/sig/mayo/sig_mayo_3.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_mayo_3_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/mayo/sig_mayo_5.c b/src/sig/mayo/sig_mayo_5.c index ca19cfbf91..633328dcbb 100644 --- a/src/sig/mayo/sig_mayo_5.c +++ b/src/sig/mayo/sig_mayo_5.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_mayo_5_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/ml_dsa/sig_ml_dsa_44_ipd.c b/src/sig/ml_dsa/sig_ml_dsa_44_ipd.c index 6d3f1e2cc2..5856a4c531 100644 --- a/src/sig/ml_dsa/sig_ml_dsa_44_ipd.c +++ b/src/sig/ml_dsa/sig_ml_dsa_44_ipd.c @@ -10,7 +10,7 @@ OQS_SIG *OQS_SIG_ml_dsa_44_ipd_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } @@ -36,7 +36,7 @@ OQS_SIG *OQS_SIG_ml_dsa_44_ipd_new(void) { /** Alias */ OQS_SIG *OQS_SIG_ml_dsa_44_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/ml_dsa/sig_ml_dsa_65_ipd.c b/src/sig/ml_dsa/sig_ml_dsa_65_ipd.c index 4c175d680c..4fc828966f 100644 --- a/src/sig/ml_dsa/sig_ml_dsa_65_ipd.c +++ b/src/sig/ml_dsa/sig_ml_dsa_65_ipd.c @@ -10,7 +10,7 @@ OQS_SIG *OQS_SIG_ml_dsa_65_ipd_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } @@ -36,7 +36,7 @@ OQS_SIG *OQS_SIG_ml_dsa_65_ipd_new(void) { /** Alias */ OQS_SIG *OQS_SIG_ml_dsa_65_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/ml_dsa/sig_ml_dsa_87_ipd.c b/src/sig/ml_dsa/sig_ml_dsa_87_ipd.c index 2ba18d57c5..5b362a2c08 100644 --- a/src/sig/ml_dsa/sig_ml_dsa_87_ipd.c +++ b/src/sig/ml_dsa/sig_ml_dsa_87_ipd.c @@ -10,7 +10,7 @@ OQS_SIG *OQS_SIG_ml_dsa_87_ipd_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } @@ -36,7 +36,7 @@ OQS_SIG *OQS_SIG_ml_dsa_87_ipd_new(void) { /** Alias */ OQS_SIG *OQS_SIG_ml_dsa_87_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_sha2_128f_simple.c b/src/sig/sphincs/sig_sphincs_sha2_128f_simple.c index d3872e9c3c..1b92d96a8a 100644 --- a/src/sig/sphincs/sig_sphincs_sha2_128f_simple.c +++ b/src/sig/sphincs/sig_sphincs_sha2_128f_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_sha2_128f_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_sha2_128s_simple.c b/src/sig/sphincs/sig_sphincs_sha2_128s_simple.c index d9dab73c1e..fabd013849 100644 --- a/src/sig/sphincs/sig_sphincs_sha2_128s_simple.c +++ b/src/sig/sphincs/sig_sphincs_sha2_128s_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_sha2_128s_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_sha2_192f_simple.c b/src/sig/sphincs/sig_sphincs_sha2_192f_simple.c index b7e2cf4372..74e280f963 100644 --- a/src/sig/sphincs/sig_sphincs_sha2_192f_simple.c +++ b/src/sig/sphincs/sig_sphincs_sha2_192f_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_sha2_192f_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_sha2_192s_simple.c b/src/sig/sphincs/sig_sphincs_sha2_192s_simple.c index 5104a7e5be..6368233999 100644 --- a/src/sig/sphincs/sig_sphincs_sha2_192s_simple.c +++ b/src/sig/sphincs/sig_sphincs_sha2_192s_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_sha2_192s_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_sha2_256f_simple.c b/src/sig/sphincs/sig_sphincs_sha2_256f_simple.c index 3e372e359d..57f569a2ad 100644 --- a/src/sig/sphincs/sig_sphincs_sha2_256f_simple.c +++ b/src/sig/sphincs/sig_sphincs_sha2_256f_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_sha2_256f_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_sha2_256s_simple.c b/src/sig/sphincs/sig_sphincs_sha2_256s_simple.c index c2e468ccde..6088aada72 100644 --- a/src/sig/sphincs/sig_sphincs_sha2_256s_simple.c +++ b/src/sig/sphincs/sig_sphincs_sha2_256s_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_sha2_256s_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_shake_128f_simple.c b/src/sig/sphincs/sig_sphincs_shake_128f_simple.c index f3539ddb74..11fb1091b5 100644 --- a/src/sig/sphincs/sig_sphincs_shake_128f_simple.c +++ b/src/sig/sphincs/sig_sphincs_shake_128f_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_shake_128f_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_shake_128s_simple.c b/src/sig/sphincs/sig_sphincs_shake_128s_simple.c index c46327547a..8578bd4d30 100644 --- a/src/sig/sphincs/sig_sphincs_shake_128s_simple.c +++ b/src/sig/sphincs/sig_sphincs_shake_128s_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_shake_128s_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_shake_192f_simple.c b/src/sig/sphincs/sig_sphincs_shake_192f_simple.c index 9d48934cfb..8e1b152977 100644 --- a/src/sig/sphincs/sig_sphincs_shake_192f_simple.c +++ b/src/sig/sphincs/sig_sphincs_shake_192f_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_shake_192f_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_shake_192s_simple.c b/src/sig/sphincs/sig_sphincs_shake_192s_simple.c index add522d162..6ccb2bd5f9 100644 --- a/src/sig/sphincs/sig_sphincs_shake_192s_simple.c +++ b/src/sig/sphincs/sig_sphincs_shake_192s_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_shake_192s_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_shake_256f_simple.c b/src/sig/sphincs/sig_sphincs_shake_256f_simple.c index 73143d77d0..29671454bc 100644 --- a/src/sig/sphincs/sig_sphincs_shake_256f_simple.c +++ b/src/sig/sphincs/sig_sphincs_shake_256f_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_shake_256f_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig/sphincs/sig_sphincs_shake_256s_simple.c b/src/sig/sphincs/sig_sphincs_shake_256s_simple.c index 832292dc3d..0cbd5476bc 100644 --- a/src/sig/sphincs/sig_sphincs_shake_256s_simple.c +++ b/src/sig/sphincs/sig_sphincs_shake_256s_simple.c @@ -8,7 +8,7 @@ OQS_SIG *OQS_SIG_sphincs_shake_256s_simple_new(void) { - OQS_SIG *sig = malloc(sizeof(OQS_SIG)); + OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG)); if (sig == NULL) { return NULL; } diff --git a/src/sig_stfl/lms/CMakeLists.txt b/src/sig_stfl/lms/CMakeLists.txt index e47452eb50..b04030e834 100644 --- a/src/sig_stfl/lms/CMakeLists.txt +++ b/src/sig_stfl/lms/CMakeLists.txt @@ -37,6 +37,7 @@ set(SRCS add_library(lms OBJECT ${SRCS}) +target_include_directories(lms PRIVATE ${LIBOQS_ROOT_DIR}/include) set(_LMS_OBJS ${_LMS_OBJS} $) set(LMS_OBJS ${_LMS_OBJS} PARENT_SCOPE) diff --git a/src/sig_stfl/lms/external/common_defs.h b/src/sig_stfl/lms/external/common_defs.h index 1c7c85d382..2a64738102 100644 --- a/src/sig_stfl/lms/external/common_defs.h +++ b/src/sig_stfl/lms/external/common_defs.h @@ -9,7 +9,7 @@ #include #include - +#include #define MAX_HASH 32 /* Length of the largest hash we support */ /* The I (Merkle tree identifier) value is 16 bytes long */ diff --git a/src/sig_stfl/lms/external/hss_alloc.c b/src/sig_stfl/lms/external/hss_alloc.c index 53eaa762e2..00c0d628b5 100644 --- a/src/sig_stfl/lms/external/hss_alloc.c +++ b/src/sig_stfl/lms/external/hss_alloc.c @@ -174,7 +174,7 @@ struct hss_working_key *allocate_working_key( signed long initial_mem_target = mem_target; /* DEBUG HACK */ #endif - struct hss_working_key *w = malloc( sizeof *w ); + struct hss_working_key *w = OQS_MEM_malloc( sizeof *w ); if (!w) { info->error_code = hss_error_out_of_memory; return NULL; @@ -215,7 +215,7 @@ signed long initial_mem_target = mem_target; /* DEBUG HACK */ w->signed_pk_len[i] = w->siglen[i-1] + pklen; - w->signed_pk[i] = malloc( w->signed_pk_len[i] ); + w->signed_pk[i] = OQS_MEM_malloc( w->signed_pk_len[i] ); if (!w->signed_pk[i]) { hss_free_working_key(w); info->error_code = hss_error_out_of_memory; @@ -430,7 +430,7 @@ printf( "Allocation = %ld\n", initial_mem_target - mem_target + best_mem ); /* D stack = NULL; /* Hey! No stack required */ /* Avoid the malloc, as malloc(0) is allowed to fail */ } else { - stack = malloc(stack_usage); + stack = OQS_MEM_malloc(stack_usage); if (!stack) { hss_free_working_key(w); info->error_code = hss_error_out_of_memory; @@ -445,7 +445,7 @@ printf( "Allocation = %ld\n", initial_mem_target - mem_target + best_mem ); /* D * allocations */ for (i = 0; ierror_code = hss_error_out_of_memory; @@ -484,7 +484,7 @@ printf( "Allocation = %ld\n", initial_mem_target - mem_target + best_mem ); /* D /* 'next subtree' */ if (k == NEXT_TREE && i == 0) continue; - struct subtree *s = malloc( sizeof *s + hash_size[i] * + struct subtree *s = OQS_MEM_malloc( sizeof *s + hash_size[i] * (((size_t)2<subtree[j][k]); // IGNORE free-check + OQS_MEM_insecure_free(tree->subtree[j][k]); hss_zeroize( tree, sizeof *tree ); /* We have seeds here */ } - free(tree); // IGNORE free-check + OQS_MEM_insecure_free(tree); } for (i=0; isigned_pk[i]); // IGNORE free-check + OQS_MEM_insecure_free(w->signed_pk[i]); } - free(w->stack); // IGNORE free-check + OQS_MEM_insecure_free(w->stack); hss_zeroize( w, sizeof *w ); /* We have secret information here */ - free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); } diff --git a/src/sig_stfl/lms/external/hss_generate.c b/src/sig_stfl/lms/external/hss_generate.c index 28fcc9eaee..359706ad6f 100644 --- a/src/sig_stfl/lms/external/hss_generate.c +++ b/src/sig_stfl/lms/external/hss_generate.c @@ -657,7 +657,7 @@ bool hss_generate_working_key( size_t total_hash = (hash_len * count_nodes) << subdiv; unsigned h_subtree = (subtree->level == 0) ? tree->top_subtree_size : tree->subtree_size; - struct sub_order *sub = malloc( sizeof *sub + total_hash ); + struct sub_order *sub = OQS_MEM_malloc( sizeof *sub + total_hash ); if (!sub) continue; /* On malloc failure, don't bother trying */ /* to subdivide */ @@ -796,7 +796,7 @@ bool hss_generate_working_key( #if DO_FLOATING_POINT /* Don't leak suborders on an intermediate error */ for (i=0; i<(sequence_t)count_order; i++) { - free( order[i].sub ); // IGNORE free-check + OQS_MEM_insecure_free( order[i].sub ); } #endif info->error_code = got_error; @@ -831,7 +831,7 @@ bool hss_generate_working_key( hash_size, tree->h, I); } - free( sub ); // IGNORE free-check + OQS_MEM_insecure_free( sub ); p_order->sub = 0; } #endif diff --git a/src/sig_stfl/lms/external/hss_keygen.c b/src/sig_stfl/lms/external/hss_keygen.c index d85d9626c7..6dc0d02b78 100644 --- a/src/sig_stfl/lms/external/hss_keygen.c +++ b/src/sig_stfl/lms/external/hss_keygen.c @@ -188,7 +188,7 @@ bool hss_generate_private_key( if (((unsigned)1< MAX_THREAD) num_thread = MAX_THREAD; - struct thread_collection *col = malloc( sizeof *col ); + struct thread_collection *col = OQS_MEM_malloc( sizeof *col ); if (!col) return 0; /* On malloc failure, run single threaded */ col->num_thread = num_thread; if (0 != pthread_mutex_init( &col->lock, 0 )) { - free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); return 0; } if (0 != pthread_mutex_init( &col->write_lock, 0 )) { pthread_mutex_destroy( &col->lock ); - free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); return 0; } @@ -126,7 +126,7 @@ static void *worker_thread( void *arg ) { (w->function)(w->x.detail, col); /* Ok, we did that */ - free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); /* Check if there's anything else to do */ pthread_mutex_lock( &col->lock ); @@ -172,7 +172,7 @@ void hss_thread_issue_work(struct thread_collection *col, size_t extra_space; if (size_detail_structure < MIN_DETAIL) extra_space = 0; else extra_space = size_detail_structure - MIN_DETAIL; - struct work_item *w = malloc(sizeof *w + extra_space); + struct work_item *w = OQS_MEM_malloc(sizeof *w + extra_space); if (!w) { /* Can't allocate the work structure; fall back to single-threaded */ @@ -219,7 +219,7 @@ void hss_thread_issue_work(struct thread_collection *col, /* Hmmm, couldn't spawn it; fall back */ default: /* On error condition */ pthread_mutex_unlock( &col->lock ); - free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); function( detail, col ); return; } @@ -277,7 +277,7 @@ void hss_thread_done(struct thread_collection *col) { pthread_mutex_destroy( &col->lock ); pthread_mutex_destroy( &col->write_lock ); - free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); } void hss_thread_before_write(struct thread_collection *col) { diff --git a/src/sig_stfl/lms/sig_stfl_lms.c b/src/sig_stfl/lms/sig_stfl_lms.c index acc218a6ba..7be57c2e97 100644 --- a/src/sig_stfl/lms/sig_stfl_lms.c +++ b/src/sig_stfl/lms/sig_stfl_lms.c @@ -70,7 +70,7 @@ static void OQS_SECRET_KEY_LMS_set_store_cb(OQS_SIG_STFL_SECRET_KEY *sk, secure_ #define LMS_ALG(lms_variant, LMS_VARIANT) \ OQS_SIG_STFL *OQS_SIG_STFL_alg_lms_##lms_variant##_new(void) { \ \ - OQS_SIG_STFL *sig = (OQS_SIG_STFL *)malloc(sizeof(OQS_SIG_STFL)); \ + OQS_SIG_STFL *sig = (OQS_SIG_STFL *)OQS_MEM_malloc(sizeof(OQS_SIG_STFL)); \ if (sig == NULL) { \ return NULL; \ } \ @@ -103,7 +103,7 @@ OQS_STATUS OQS_SIG_STFL_alg_lms_##lms_variant##_keypair(uint8_t *public_key, OQS \ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_##LMS_VARIANT##_new(void) {\ \ - OQS_SIG_STFL_SECRET_KEY *sk = malloc(sizeof(OQS_SIG_STFL_SECRET_KEY));\ + OQS_SIG_STFL_SECRET_KEY *sk = OQS_MEM_malloc(sizeof(OQS_SIG_STFL_SECRET_KEY));\ if (sk == NULL) {\ return NULL;\ }\ diff --git a/src/sig_stfl/lms/sig_stfl_lms_functions.c b/src/sig_stfl/lms/sig_stfl_lms_functions.c index 60d1d0c60b..25ee7b6cbe 100644 --- a/src/sig_stfl/lms/sig_stfl_lms_functions.c +++ b/src/sig_stfl/lms/sig_stfl_lms_functions.c @@ -255,7 +255,7 @@ int oqs_sig_stfl_lms_keypair(uint8_t *pk, OQS_SIG_STFL_SECRET_KEY *sk, const uin return -1; } - oqs_key_data = malloc(sizeof(oqs_lms_key_data)); + oqs_key_data = OQS_MEM_malloc(sizeof(oqs_lms_key_data)); if (oqs_key_data == NULL) { return -1; } @@ -269,7 +269,7 @@ int oqs_sig_stfl_lms_keypair(uint8_t *pk, OQS_SIG_STFL_SECRET_KEY *sk, const uin oqs_key_data->levels = 1; oqs_key_data->len_sec_key = sk->length_secret_key; - oqs_key_data->sec_key = (uint8_t *)malloc(sk->length_secret_key * sizeof(uint8_t)); + oqs_key_data->sec_key = (uint8_t *)OQS_MEM_malloc(sk->length_secret_key * sizeof(uint8_t)); if (oqs_key_data->sec_key == NULL) { OQS_MEM_insecure_free(oqs_key_data); oqs_key_data = NULL; @@ -280,7 +280,7 @@ int oqs_sig_stfl_lms_keypair(uint8_t *pk, OQS_SIG_STFL_SECRET_KEY *sk, const uin //Aux Data size_t len_aux_data = DEFAULT_AUX_DATA; - uint8_t *aux_data = calloc(len_aux_data, sizeof(uint8_t)); + uint8_t *aux_data = OQS_MEM_calloc(len_aux_data, sizeof(uint8_t)); if (aux_data == NULL) { OQS_MEM_insecure_free( oqs_key_data->sec_key); OQS_MEM_insecure_free(oqs_key_data); @@ -582,7 +582,7 @@ int oqs_sig_stfl_lms_sign(OQS_SIG_STFL_SECRET_KEY *sk, return 0; } - sig = malloc(sig_len); + sig = OQS_MEM_malloc(sig_len); if (!sig) { hss_free_working_key(w); return -1; @@ -694,7 +694,7 @@ OQS_STATUS oqs_serialize_lms_key(uint8_t **sk_key, size_t *sk_len, const OQS_SIG return OQS_ERROR; } - uint8_t *sk_key_buf = malloc(key_len * sizeof(uint8_t)); + uint8_t *sk_key_buf = OQS_MEM_malloc(key_len * sizeof(uint8_t)); if (sk_key_buf == NULL) { return OQS_ERROR; @@ -760,8 +760,8 @@ OQS_STATUS oqs_deserialize_lms_key(OQS_SIG_STFL_SECRET_KEY *sk, const uint8_t *s return OQS_ERROR; } - lms_key_data = malloc(sizeof(oqs_lms_key_data)); - lms_sk = malloc(lms_sk_len * sizeof(uint8_t)); + lms_key_data = OQS_MEM_malloc(sizeof(oqs_lms_key_data)); + lms_sk = OQS_MEM_malloc(lms_sk_len * sizeof(uint8_t)); if (lms_key_data == NULL || lms_sk == NULL) { goto err; @@ -773,7 +773,7 @@ OQS_STATUS oqs_deserialize_lms_key(OQS_SIG_STFL_SECRET_KEY *sk, const uint8_t *s lms_key_data->context = context; if (aux_buf_len) { - lms_aux = malloc(aux_buf_len * sizeof(uint8_t)); + lms_aux = OQS_MEM_malloc(aux_buf_len * sizeof(uint8_t)); if (lms_aux == NULL) { goto err; diff --git a/src/sig_stfl/xmss/external/wots.c b/src/sig_stfl/xmss/external/wots.c index 0d5b57fd57..3d87c206b2 100644 --- a/src/sig_stfl/xmss/external/wots.c +++ b/src/sig_stfl/xmss/external/wots.c @@ -87,7 +87,7 @@ static void wots_checksum(const xmss_params *params, { int csum = 0; unsigned int csum_bytes_length = (params->wots_len2 * params->wots_log_w + 7) / 8; - unsigned char *csum_bytes = malloc(csum_bytes_length); + unsigned char *csum_bytes = OQS_MEM_malloc(csum_bytes_length); if (csum_bytes == NULL) { return; } @@ -129,7 +129,7 @@ void wots_pkgen(const xmss_params *params, { unsigned int i; const size_t buf_size = 2 * params->padding_len + 4 * params->n + 64; - unsigned char *buf = malloc(buf_size); + unsigned char *buf = OQS_MEM_malloc(buf_size); if (buf == NULL) { return; } @@ -156,8 +156,8 @@ void wots_sign(const xmss_params *params, uint32_t addr[8]) { const size_t buf_size = 2 * params->padding_len + 4 * params->n + 64; - unsigned int *lengths = calloc(params->wots_len, sizeof(unsigned int)); - unsigned char *buf = malloc(buf_size); + unsigned int *lengths = OQS_MEM_calloc(params->wots_len, sizeof(unsigned int)); + unsigned char *buf = OQS_MEM_malloc(buf_size); unsigned int i; if (lengths == NULL || buf == NULL) { return; @@ -187,9 +187,9 @@ void wots_pk_from_sig(const xmss_params *params, unsigned char *pk, const unsigned char *sig, const unsigned char *msg, const unsigned char *pub_seed, uint32_t addr[8]) { - unsigned int *lengths = calloc(params->wots_len, sizeof(unsigned int )); + unsigned int *lengths = OQS_MEM_calloc(params->wots_len, sizeof(unsigned int )); const size_t thash_buf_len = 2 * params->padding_len + 4 * params->n + 32; - unsigned char *thash_buf = malloc(thash_buf_len); + unsigned char *thash_buf = OQS_MEM_malloc(thash_buf_len); unsigned int i; if (lengths == NULL || thash_buf == NULL) { return; diff --git a/src/sig_stfl/xmss/external/xmss_commons.c b/src/sig_stfl/xmss/external/xmss_commons.c index 3d7e469a4d..58f2173efb 100644 --- a/src/sig_stfl/xmss/external/xmss_commons.c +++ b/src/sig_stfl/xmss/external/xmss_commons.c @@ -109,7 +109,7 @@ void gen_leaf_wots(const xmss_params *params, unsigned char *leaf, const unsigned char *sk_seed, const unsigned char *pub_seed, uint32_t ltree_addr[8], uint32_t ots_addr[8]) { - unsigned char *pk = malloc(params->wots_sig_bytes + 2 * params->padding_len + 6 * params->n + 32); + unsigned char *pk = OQS_MEM_malloc(params->wots_sig_bytes + 2 * params->padding_len + 6 * params->n + 32); if (pk == NULL) { return; } @@ -151,7 +151,7 @@ int xmssmt_core_sign_open(const xmss_params *params, const unsigned char *pub_root = pk; const unsigned char *pub_seed = pk + params->n; - unsigned char *tmp = malloc(params->wots_sig_bytes + params->n + params->n + + unsigned char *tmp = OQS_MEM_malloc(params->wots_sig_bytes + params->n + params->n + + 2 *params->n + 2 * params->padding_len + 6 * params->n + 32); if (tmp == NULL) { return -1; @@ -181,7 +181,7 @@ int xmssmt_core_sign_open(const xmss_params *params, // Unused since smlen is a constant (void) smlen; - if ((m_with_prefix_len == 0) || (m_with_prefix = malloc(m_with_prefix_len)) == NULL){ + if ((m_with_prefix_len == 0) || (m_with_prefix = OQS_MEM_malloc(m_with_prefix_len)) == NULL){ ret = -1; goto fail; } diff --git a/src/sig_stfl/xmss/external/xmss_core_fast.c b/src/sig_stfl/xmss/external/xmss_core_fast.c index 9ad19e3908..5417b42f23 100644 --- a/src/sig_stfl/xmss/external/xmss_core_fast.c +++ b/src/sig_stfl/xmss/external/xmss_core_fast.c @@ -175,7 +175,7 @@ static void deep_state_swap(const xmss_params *params, ? (params->tree_height + 1) : ((1 << params->bds_k) - params->bds_k - 1)) * params->n; - unsigned char *t = malloc(t_size); + unsigned char *t = OQS_MEM_malloc(t_size); if (t == NULL) { return; } @@ -243,9 +243,9 @@ static void treehash_init(const xmss_params *params, uint32_t lastnode = index +(1<padding_len + 6 * params->n + 32; const size_t stack_size = ((height+1)*params->n)* sizeof(unsigned char); - unsigned char *stack = calloc((height+1)*params->n, sizeof(unsigned char)); - unsigned int *stacklevels = malloc((height + 1)*sizeof(unsigned int)); - unsigned char *thash_buf = malloc(thash_buf_size); + unsigned char *stack = OQS_MEM_calloc((height+1)*params->n, sizeof(unsigned char)); + unsigned int *stacklevels = OQS_MEM_malloc((height + 1)*sizeof(unsigned int)); + unsigned char *thash_buf = OQS_MEM_malloc(thash_buf_size); if (stack == NULL || stacklevels == NULL || thash_buf == NULL) { return; @@ -321,7 +321,7 @@ static void treehash_update(const xmss_params *params, set_ots_addr(ots_addr, treehash->next_idx); const size_t buf_size = 2 * params->n + 2 * params->padding_len + 6 * params->n + 32; - unsigned char *buf = malloc(buf_size); + unsigned char *buf = OQS_MEM_malloc(buf_size); if (buf == NULL) { return; } @@ -419,7 +419,7 @@ static char bds_state_update(const xmss_params *params, uint32_t node_addr[8] = {0}; uint32_t ots_addr[8] = {0}; const size_t thash_buf_size = 2 * params->padding_len + 6 * params->n + 32; - unsigned char *thash_buf = malloc(thash_buf_size); + unsigned char *thash_buf = OQS_MEM_malloc(thash_buf_size); if (thash_buf == NULL) { return -1; @@ -485,7 +485,7 @@ static void bds_round(const xmss_params *params, unsigned int startidx; unsigned int offset, rowidx; const size_t buf_size = 2 * params->n + 2 * params->padding_len + 6 * params->n + 32; - unsigned char *buf = malloc(buf_size); + unsigned char *buf = OQS_MEM_malloc(buf_size); if (buf == NULL) { return; } @@ -589,7 +589,7 @@ int xmss_core_keypair(const xmss_params *params, // TODO (from upstream) refactor BDS state not to need separate treehash instances bds_state state; const size_t treehash_size = (params->tree_height - params->bds_k)*sizeof(treehash_inst); - treehash_inst *treehash = calloc(params->tree_height - params->bds_k, sizeof(treehash_inst)); + treehash_inst *treehash = OQS_MEM_calloc(params->tree_height - params->bds_k, sizeof(treehash_inst)); if (treehash == NULL) { return -1; } @@ -652,8 +652,8 @@ int xmss_core_sign(const xmss_params *params, bds_state state; const size_t treehash_size = (params->tree_height - params->bds_k) * sizeof(treehash_inst); const size_t tmp_size = 5 * params->n + params->padding_len + params->n + 32; - treehash_inst *treehash = calloc(params->tree_height - params->bds_k, sizeof(treehash_inst)); - unsigned char *tmp = malloc(tmp_size); + treehash_inst *treehash = OQS_MEM_calloc(params->tree_height - params->bds_k, sizeof(treehash_inst)); + unsigned char *tmp = OQS_MEM_malloc(tmp_size); if (treehash == NULL || tmp == NULL) { return -1; } @@ -725,7 +725,7 @@ int xmss_core_sign(const xmss_params *params, /* Already put the message in the right place, to make it easier to prepend * things when computing the hash over the message. */ unsigned long long prefix_length = params->padding_len + 3*params->n; - unsigned char *m_with_prefix = malloc((size_t)(mlen + prefix_length)); + unsigned char *m_with_prefix = OQS_MEM_malloc((size_t)(mlen + prefix_length)); if (m_with_prefix == NULL) { ret = -1; goto cleanup; @@ -811,8 +811,8 @@ int xmssmt_core_keypair(const xmss_params *params, // TODO (from upstream) refactor BDS state not to need separate treehash instances const size_t states_size = (2*params->d - 1)* sizeof(bds_state); const size_t treehash_size = ((2*params->d - 1) * (params->tree_height - params->bds_k))* sizeof(treehash_inst); - bds_state *states = calloc(2*params->d - 1, sizeof(bds_state)); - treehash_inst *treehash = calloc((2*params->d - 1) * (params->tree_height - params->bds_k), sizeof(treehash_inst)); + bds_state *states = OQS_MEM_calloc(2*params->d - 1, sizeof(bds_state)); + treehash_inst *treehash = OQS_MEM_calloc((2*params->d - 1) * (params->tree_height - params->bds_k), sizeof(treehash_inst)); if (states == NULL || treehash == NULL) { return -1; } @@ -890,9 +890,9 @@ int xmssmt_core_sign(const xmss_params *params, const size_t treehash_size = (2*params->d - 1) * (params->tree_height - params->bds_k) * sizeof(treehash_inst); const size_t tmp_size = 5 * params->n + params->padding_len + params->n + 32; - bds_state *states = calloc(2*params->d - 1, sizeof(bds_state)); - treehash_inst *treehash = calloc((2*params->d - 1) * (params->tree_height - params->bds_k), sizeof(treehash_inst)); - unsigned char *tmp = malloc(5 * params->n + + bds_state *states = OQS_MEM_calloc(2*params->d - 1, sizeof(bds_state)); + treehash_inst *treehash = OQS_MEM_calloc((2*params->d - 1) * (params->tree_height - params->bds_k), sizeof(treehash_inst)); + unsigned char *tmp = OQS_MEM_malloc(5 * params->n + params->padding_len + params->n + 32); if (states == NULL || treehash == NULL || tmp == NULL) { return -1; @@ -925,7 +925,7 @@ int xmssmt_core_sign(const xmss_params *params, states[i].next_leaf = 0; } - if ((m_with_prefix_len == 0) || (m_with_prefix = malloc(m_with_prefix_len)) == NULL) { + if ((m_with_prefix_len == 0) || (m_with_prefix = OQS_MEM_malloc(m_with_prefix_len)) == NULL) { ret = -1; goto cleanup; } diff --git a/src/sig_stfl/xmss/sig_stfl_xmss_secret_key_functions.c b/src/sig_stfl/xmss/sig_stfl_xmss_secret_key_functions.c index 6903135cb0..af1e74efdb 100644 --- a/src/sig_stfl/xmss/sig_stfl_xmss_secret_key_functions.c +++ b/src/sig_stfl/xmss/sig_stfl_xmss_secret_key_functions.c @@ -14,7 +14,7 @@ extern inline OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_XMSS_new(size_t length_secret_key) { // Initialize the secret key in the heap with adequate memory - OQS_SIG_STFL_SECRET_KEY *sk = malloc(sizeof(OQS_SIG_STFL_SECRET_KEY)); + OQS_SIG_STFL_SECRET_KEY *sk = OQS_MEM_malloc(sizeof(OQS_SIG_STFL_SECRET_KEY)); if (sk == NULL) { return NULL; } @@ -27,7 +27,7 @@ extern inline OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_XMSS_new(size_t length_sec sk->deserialize_key = OQS_SECRET_KEY_XMSS_deserialize_key; // Initialize the key with length_secret_key amount of bytes. - sk->secret_key_data = (uint8_t *)malloc(sk->length_secret_key * sizeof(uint8_t)); + sk->secret_key_data = (uint8_t *)OQS_MEM_malloc(sk->length_secret_key * sizeof(uint8_t)); if (sk->secret_key_data == NULL) { OQS_MEM_insecure_free(sk); @@ -71,7 +71,7 @@ OQS_STATUS OQS_SECRET_KEY_XMSS_serialize_key(uint8_t **sk_buf_ptr, size_t *sk_le return OQS_ERROR; } - uint8_t *sk_buf = malloc(sk->length_secret_key * sizeof(uint8_t)); + uint8_t *sk_buf = OQS_MEM_malloc(sk->length_secret_key * sizeof(uint8_t)); if (sk_buf == NULL) { return OQS_ERROR; } @@ -96,7 +96,7 @@ OQS_STATUS OQS_SECRET_KEY_XMSS_inner_serialize_key(uint8_t **sk_buf_ptr, size_t return OQS_ERROR; } - uint8_t *sk_buf = malloc(sk->length_secret_key * sizeof(uint8_t)); + uint8_t *sk_buf = OQS_MEM_malloc(sk->length_secret_key * sizeof(uint8_t)); if (sk_buf == NULL) { return OQS_ERROR; } diff --git a/src/sig_stfl/xmss/sig_stfl_xmss_xmssmt.c b/src/sig_stfl/xmss/sig_stfl_xmss_xmssmt.c index ed25233be1..a3d096c71c 100644 --- a/src/sig_stfl/xmss/sig_stfl_xmss_xmssmt.c +++ b/src/sig_stfl/xmss/sig_stfl_xmss_xmssmt.c @@ -30,7 +30,7 @@ #define XMSS_ALG(mt, xmss_v, XMSS_V) \ OQS_SIG_STFL *OQS_SIG_STFL_alg_xmss##xmss_v##_new(void) { \ \ - OQS_SIG_STFL *sig = (OQS_SIG_STFL *)malloc(sizeof(OQS_SIG_STFL)); \ + OQS_SIG_STFL *sig = (OQS_SIG_STFL *)OQS_MEM_malloc(sizeof(OQS_SIG_STFL)); \ if (sig == NULL) { \ return NULL; \ } \ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5d45ecb6f8..a03e784d36 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,7 +30,13 @@ if(NOT WIN32) endif() # List oqs-internal after oqs so that oqs has linking precedence. -set(TEST_DEPS oqs oqs-internal ${LIBM}) +if(${OQS_USE_OPENSSL}) + find_package(OpenSSL 1.1.1 REQUIRED) + set(TEST_DEPS oqs oqs-internal ${LIBM} OpenSSL::Crypto) +else() + set(TEST_DEPS oqs oqs-internal ${LIBM}) +endif() + if(OQS_USE_PTHREADS) set(TEST_DEPS ${TEST_DEPS} Threads::Threads) endif() diff --git a/tests/example_kem.c b/tests/example_kem.c index 7d7c47d327..020bbd601c 100644 --- a/tests/example_kem.c +++ b/tests/example_kem.c @@ -107,14 +107,14 @@ static OQS_STATUS example_heap(void) { return OQS_SUCCESS; } - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); - ciphertext = malloc(kem->length_ciphertext); - shared_secret_e = malloc(kem->length_shared_secret); - shared_secret_d = malloc(kem->length_shared_secret); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); + ciphertext = OQS_MEM_malloc(kem->length_ciphertext); + shared_secret_e = OQS_MEM_malloc(kem->length_shared_secret); + shared_secret_d = OQS_MEM_malloc(kem->length_shared_secret); if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) { - fprintf(stderr, "ERROR: malloc failed!\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed!\n"); cleanup_heap(secret_key, shared_secret_e, shared_secret_d, public_key, ciphertext, kem); diff --git a/tests/example_sig.c b/tests/example_sig.c index 62cc30fae5..25edcdf62b 100644 --- a/tests/example_sig.c +++ b/tests/example_sig.c @@ -109,12 +109,12 @@ static OQS_STATUS example_heap(void) { return OQS_ERROR; } - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); - message = malloc(message_len); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); + message = OQS_MEM_malloc(message_len); + signature = OQS_MEM_malloc(sig->length_signature); if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) { - fprintf(stderr, "ERROR: malloc failed!\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed!\n"); cleanup_heap(public_key, secret_key, message, signature, sig); return OQS_ERROR; } diff --git a/tests/example_sig_stfl.c b/tests/example_sig_stfl.c index ca21015f48..80bf75351b 100644 --- a/tests/example_sig_stfl.c +++ b/tests/example_sig_stfl.c @@ -66,12 +66,12 @@ static OQS_STATUS stfl_example(char *method_name) { /* * Allocate storage for public key, secret key filename, message and signature */ - public_key = malloc(sig->length_public_key); - sk_fname = malloc(strlen(method_name) + strlen(".sk") + 1); - message = malloc(message_len); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + sk_fname = OQS_MEM_malloc(strlen(method_name) + strlen(".sk") + 1); + message = OQS_MEM_malloc(message_len); + signature = OQS_MEM_malloc(sig->length_signature); if ((public_key == NULL) || (message == NULL) || (signature == NULL) || (sk_fname == NULL)) { - fprintf(stderr, "ERROR: malloc failed!\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed!\n"); goto err; } diff --git a/tests/kat_kem.c b/tests/kat_kem.c index 314b20b510..ed136189aa 100644 --- a/tests/kat_kem.c +++ b/tests/kat_kem.c @@ -55,13 +55,13 @@ static OQS_STATUS kem_kat(const char *method_name, bool all) { fh = stdout; - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); - ciphertext = malloc(kem->length_ciphertext); - shared_secret_e = malloc(kem->length_shared_secret); - shared_secret_d = malloc(kem->length_shared_secret); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); + ciphertext = OQS_MEM_malloc(kem->length_ciphertext); + shared_secret_e = OQS_MEM_malloc(kem->length_shared_secret); + shared_secret_d = OQS_MEM_malloc(kem->length_shared_secret); if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) { - fprintf(stderr, "[kat_kem] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[kat_kem] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } diff --git a/tests/kat_sig.c b/tests/kat_sig.c index 0c873afc97..5326d3c335 100644 --- a/tests/kat_sig.c +++ b/tests/kat_sig.c @@ -35,7 +35,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "Dilithium2")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -45,7 +45,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "Dilithium3")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -55,7 +55,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "Dilithium5")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -65,7 +65,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "ML-DSA-44-ipd") || 0 == strcmp(sig->method_name, "ML-DSA-44")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -75,7 +75,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "ML-DSA-65-ipd") || 0 == strcmp(sig->method_name, "ML-DSA-65")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -85,7 +85,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "ML-DSA-87-ipd") || 0 == strcmp(sig->method_name, "ML-DSA-87")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -96,7 +96,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le // signed_msg = sig_len (2 bytes, big endian) || nonce (40 bytes) || msg || 0x29 || sig const uint16_t signature_len_uint16 = (uint16_t)signature_len; *signed_msg_len = 2 + signature_len_uint16 + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -116,7 +116,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le // signed_msg = sig_len (2 bytes, big endian) || nonce (40 bytes) || msg || 0x2A || sig const uint16_t signature_len_uint16 = (uint16_t)signature_len; *signed_msg_len = 2 + signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -135,7 +135,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "Falcon-padded-512")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -145,7 +145,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "Falcon-padded-1024")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -155,7 +155,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHA2-128f-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -165,7 +165,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHA2-128s-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -175,7 +175,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHA2-192f-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -185,7 +185,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHA2-192s-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -195,7 +195,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHA2-256f-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -205,7 +205,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHA2-256s-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -215,7 +215,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHAKE-128f-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -225,7 +225,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHAKE-128s-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -235,7 +235,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHAKE-192f-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -245,7 +245,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHAKE-192s-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -255,7 +255,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHAKE-256f-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -265,7 +265,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "SPHINCS+-SHAKE-256s-simple")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -275,7 +275,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "MAYO-1")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -285,7 +285,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "MAYO-2")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -295,7 +295,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "MAYO-3")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -305,7 +305,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "MAYO-5")) { // signed_msg = signature || msg *signed_msg_len = signature_len + msg_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -315,7 +315,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-128-balanced")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -325,7 +325,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-128-fast")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -335,7 +335,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-128-small")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -345,7 +345,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-192-balanced")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -355,7 +355,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-192-fast")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -365,7 +365,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-192-small")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -375,7 +375,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-256-balanced")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -385,7 +385,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-256-fast")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -395,7 +395,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdp-256-small")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -405,7 +405,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-128-balanced")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -415,7 +415,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-128-fast")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -425,7 +425,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-128-small")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -435,7 +435,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-192-balanced")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -445,7 +445,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-192-fast")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -455,7 +455,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-192-small")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -465,7 +465,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-256-balanced")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -475,7 +475,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-256-fast")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -485,7 +485,7 @@ OQS_STATUS combine_message_signature(uint8_t **signed_msg, size_t *signed_msg_le } else if (0 == strcmp(sig->method_name, "cross-rsdpg-256-small")) { // signed_msg = msg || signature *signed_msg_len = msg_len + signature_len; - *signed_msg = malloc(*signed_msg_len); + *signed_msg = OQS_MEM_malloc(*signed_msg_len); if (*signed_msg == NULL) { return OQS_ERROR; } @@ -537,13 +537,13 @@ OQS_STATUS sig_kat(const char *method_name, bool all) { max_count = all ? prng->max_kats : 1; - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); + signature = OQS_MEM_malloc(sig->length_signature); // allocate maximum length for msg - msg = malloc(33 * max_count); + msg = OQS_MEM_malloc(33 * max_count); if ((public_key == NULL) || (secret_key == NULL) || (signature == NULL) || (msg == NULL)) { - fprintf(stderr, "[kat_sig] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[kat_sig] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } diff --git a/tests/kat_sig_stfl.c b/tests/kat_sig_stfl.c index 85b0896154..26eebaa962 100644 --- a/tests/kat_sig_stfl.c +++ b/tests/kat_sig_stfl.c @@ -184,15 +184,15 @@ OQS_STATUS sig_stfl_kat(const char *method_name, const char *katfile) { } // Grab the pk and sk from KAT file - public_key = malloc(sig->length_public_key); + public_key = OQS_MEM_malloc(sig->length_public_key); secret_key = OQS_SIG_STFL_SECRET_KEY_new(sig->method_name); OQS_SIG_STFL_SECRET_KEY_SET_store_cb(secret_key, do_nothing_save, NULL); - signature = calloc(sig->length_signature, sizeof(uint8_t)); - signature_kat = calloc(sig->length_signature, sizeof(uint8_t)); + signature = OQS_MEM_calloc(sig->length_signature, sizeof(uint8_t)); + signature_kat = OQS_MEM_calloc(sig->length_signature, sizeof(uint8_t)); if ((public_key == NULL) || (secret_key == NULL) || (signature == NULL)) { - fprintf(stderr, "[kat_stfl_sig] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[kat_stfl_sig] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } @@ -223,8 +223,8 @@ OQS_STATUS sig_stfl_kat(const char *method_name, const char *katfile) { msg_len = 33 * (0 + 1); fprintf(fh, "mlen = %zu\n", msg_len); - msg = malloc(msg_len); - msg_rand = malloc(msg_len); + msg = OQS_MEM_malloc(msg_len); + msg_rand = OQS_MEM_malloc(msg_len); if (!ReadHex(fp_rsp, msg, msg_len, "msg = ")) { fprintf(stderr, "ERROR: unable to read 'msg' from <%s>\n", katfile); @@ -403,9 +403,9 @@ static OQS_STATUS test_lms_kat(const char *method_name, const char *katfile) { goto err; } - public_key = malloc(sig->length_public_key); - sm = malloc(sig->length_signature); - msg = malloc((unsigned long)msg_len); + public_key = OQS_MEM_malloc(sig->length_public_key); + sm = OQS_MEM_malloc(sig->length_signature); + msg = OQS_MEM_malloc((unsigned long)msg_len); if ((!msg || !sm || !public_key)) { fprintf(stderr, "ERROR: unable to allocate memory.\n"); diff --git a/tests/speed_common.c b/tests/speed_common.c index 17f459146b..7fb7ad39cb 100644 --- a/tests/speed_common.c +++ b/tests/speed_common.c @@ -28,15 +28,15 @@ static OQS_STATUS speed_aes128(uint64_t duration, size_t message_len) { uint8_t *ciphertext = NULL; void *schedule = NULL; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } - ciphertext = malloc(message_len); + ciphertext = OQS_MEM_malloc(message_len); if (ciphertext == NULL) { OQS_MEM_insecure_free(message); - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -61,15 +61,15 @@ static OQS_STATUS speed_aes256(uint64_t duration, size_t message_len) { uint8_t nonce[12] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; void *schedule = NULL; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } - ciphertext = malloc(message_len); + ciphertext = OQS_MEM_malloc(message_len); if (ciphertext == NULL) { OQS_MEM_insecure_free(message); - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -104,9 +104,9 @@ static OQS_STATUS speed_sha256(uint64_t duration, size_t message_len) { uint8_t *message = NULL; uint8_t output[32]; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -124,9 +124,9 @@ static OQS_STATUS speed_sha384(uint64_t duration, size_t message_len) { uint8_t *message = NULL; uint8_t output[48]; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -144,9 +144,9 @@ static OQS_STATUS speed_sha512(uint64_t duration, size_t message_len) { uint8_t *message = NULL; uint8_t output[64]; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -163,9 +163,9 @@ static OQS_STATUS speed_sha512(uint64_t duration, size_t message_len) { static OQS_STATUS speed_sha3(uint64_t duration, size_t message_len) { uint8_t *message = NULL; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -193,15 +193,15 @@ static OQS_STATUS speed_shake128(uint64_t duration, size_t message_len, size_t o uint8_t *message = NULL; uint8_t *output = NULL; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } - output = malloc(output_len); + output = OQS_MEM_malloc(output_len); if (output == NULL) { OQS_MEM_insecure_free(message); - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } @@ -220,15 +220,15 @@ static OQS_STATUS speed_shake256(uint64_t duration, size_t message_len, size_t o uint8_t *message = NULL; uint8_t *output = NULL; - message = malloc(message_len); + message = OQS_MEM_malloc(message_len); if (message == NULL) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } - output = malloc(output_len); + output = OQS_MEM_malloc(output_len); if (output == NULL) { OQS_MEM_insecure_free(message); - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); return OQS_ERROR; } diff --git a/tests/speed_kem.c b/tests/speed_kem.c index 3a29a55c5e..9ce63534db 100644 --- a/tests/speed_kem.c +++ b/tests/speed_kem.c @@ -48,14 +48,14 @@ static OQS_STATUS kem_speed_wrapper(const char *method_name, uint64_t duration, return OQS_SUCCESS; } - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); - ciphertext = malloc(kem->length_ciphertext); - shared_secret_e = malloc(kem->length_shared_secret); - shared_secret_d = malloc(kem->length_shared_secret); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); + ciphertext = OQS_MEM_malloc(kem->length_ciphertext); + shared_secret_e = OQS_MEM_malloc(kem->length_shared_secret); + shared_secret_d = OQS_MEM_malloc(kem->length_shared_secret); if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } diff --git a/tests/speed_sig.c b/tests/speed_sig.c index 9669069293..cc911beedf 100644 --- a/tests/speed_sig.c +++ b/tests/speed_sig.c @@ -48,13 +48,13 @@ static OQS_STATUS sig_speed_wrapper(const char *method_name, uint64_t duration, return OQS_SUCCESS; } - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); - message = malloc(message_len); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); + message = OQS_MEM_malloc(message_len); + signature = OQS_MEM_malloc(sig->length_signature); if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } diff --git a/tests/test_code_conventions.py b/tests/test_code_conventions.py index ed88f483ab..2ed437f119 100644 --- a/tests/test_code_conventions.py +++ b/tests/test_code_conventions.py @@ -48,26 +48,46 @@ def test_spdx(): print(result) assert False -# Ensure "free" is not used unprotected in the main OQS code. -@helpers.filtered_test -@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not needed on Windows") -def test_free(): - c_files = [] +def test_memory_functions(): + c_h_files = [] for path, _, files in os.walk('src'): - c_files += [os.path.join(path,f) for f in files if f[-2:] == '.c'] + c_h_files += [os.path.join(path, f) for f in files if f.endswith(('.c', '.h', '.fragment'))] + + memory_functions = ['free', 'malloc', 'calloc', 'realloc', 'strdup'] okay = True - for fn in c_files: + + for fn in c_h_files: with open(fn) as f: - # Find all lines that contain 'free(' but not '_free(' - for no, line in enumerate(f,1): - if not re.match(r'^.*[^_]free\(.*$', line): + content = f.read() + lines = content.splitlines() + in_multiline_comment = False + for no, line in enumerate(lines, 1): + # Skip single-line comments + if line.strip().startswith('//'): + continue + # Check for start of multi-line comment + if '/*' in line and not in_multiline_comment: + in_multiline_comment = True + # Check for end of multi-line comment + if '*/' in line and in_multiline_comment: + in_multiline_comment = False continue - if 'IGNORE free-check' in line: + # Skip lines inside multi-line comments + if in_multiline_comment: continue - okay = False - print("Suspicious `free` in {}:{}:{}".format(fn,no,line)) - assert okay, "'free' is used in some files. These should be changed to 'OQS_MEM_secure_free' or 'OQS_MEM_insecure_free' as appropriate. If you are sure you want to use 'free' in a particular spot, add the comment '// IGNORE free-check' on the line where 'free' occurs." + for func in memory_functions: + if re.search(r'\b{}\('.format(func), line) and not re.search(r'\b_{}\('.format(func), line): + if 'IGNORE memory-check' in line: + continue + okay = False + print(f"Suspicious `{func}` in {fn}:{no}:{line.strip()}") + + assert okay, ("Standard memory functions are used in some files. " + "These should be changed to OQS_MEM_* equivalents as appropriate. " + "If you are sure you want to use these functions in a particular spot, " + "add the comment '// IGNORE memory-check' on the line where the function occurs.") if __name__ == "__main__": + test_memory_functions() import sys pytest.main(sys.argv) diff --git a/tests/test_hash.c b/tests/test_hash.c index 788f41ffb2..49d40e1dc9 100644 --- a/tests/test_hash.c +++ b/tests/test_hash.c @@ -17,7 +17,7 @@ static bool sha2_callback_called = false; static int read_stdin(uint8_t **msg, size_t *msg_len) { - *msg = malloc(BUFFER_SIZE); + *msg = OQS_MEM_malloc(BUFFER_SIZE); if (*msg == NULL) { return -1; } @@ -79,7 +79,7 @@ static int do_sha256(void) { if (memcmp(output, output_inc, 32) != 0) { fprintf(stderr, "ERROR: Incremental API does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -2; } // hash with second state @@ -91,7 +91,7 @@ static int do_sha256(void) { } if (memcmp(output, output_inc, 32) != 0) { fprintf(stderr, "ERROR: Incremental API with cloned state does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -3; } @@ -103,7 +103,7 @@ static int do_sha256(void) { OQS_SHA2_sha256_inc_finalize(output_inc_2, &state3, &msg[i], 0); if (memcmp(output, output_inc_2, 32) != 0) { fprintf(stderr, "ERROR: Non-block Incremental API with cloned state does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -4; } @@ -112,7 +112,7 @@ static int do_sha256(void) { OQS_SHA2_sha256_inc_finalize(output_inc, &state6, NULL, 0); if (memcmp(output, output_inc, 32) != 0) { fprintf(stderr, "ERROR: Incremental API with the entire msg.\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -3; } @@ -128,7 +128,7 @@ static int do_sha256(void) { } if (memcmp(output, output_inc_2, 32) != 0) { fprintf(stderr, "ERROR: Combined block increments with non-block size failed to match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -5; } @@ -142,12 +142,12 @@ static int do_sha256(void) { } if (memcmp(output, output_inc_2, 32) != 0) { fprintf(stderr, "ERROR: Combined non-block size and block increments failed to match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -5; } //Test inc API print_hex(output, 32); - free(msg); + OQS_MEM_insecure_free(msg); return 0; } @@ -178,7 +178,7 @@ static int do_sha384(void) { } if (memcmp(output, output_inc, 48) != 0) { fprintf(stderr, "ERROR: Incremental API does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -2; } // hash with second state @@ -190,11 +190,11 @@ static int do_sha384(void) { } if (memcmp(output, output_inc, 48) != 0) { fprintf(stderr, "ERROR: Incremental API with cloned state does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -3; } print_hex(output, 48); - free(msg); + OQS_MEM_insecure_free(msg); return 0; } @@ -225,7 +225,7 @@ static int do_sha512(void) { } if (memcmp(output, output_inc, 64) != 0) { fprintf(stderr, "ERROR: Incremental API does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -2; } // hash with second state @@ -237,11 +237,11 @@ static int do_sha512(void) { } if (memcmp(output, output_inc, 64) != 0) { fprintf(stderr, "ERROR: Incremental API with cloned state does not match main API\n"); - free(msg); + OQS_MEM_insecure_free(msg); return -3; } print_hex(output, 64); - free(msg); + OQS_MEM_insecure_free(msg); return 0; } @@ -254,11 +254,11 @@ static int do_arbitrary_hash(void (*hash)(uint8_t *, const uint8_t *, size_t), s return -1; } // run main SHA-256 API - uint8_t *output = malloc(hash_len); + uint8_t *output = OQS_MEM_malloc(hash_len); hash(output, msg, msg_len); print_hex(output, hash_len); - free(output); - free(msg); + OQS_MEM_insecure_free(output); + OQS_MEM_insecure_free(msg); return 0; } diff --git a/tests/test_helpers.c b/tests/test_helpers.c index 260dc41321..84bd31937b 100644 --- a/tests/test_helpers.c +++ b/tests/test_helpers.c @@ -79,7 +79,7 @@ static int is_hqc(const char *method_name) { /* OQS_KAT_PRNG interface implementation */ OQS_KAT_PRNG *OQS_KAT_PRNG_new(const char *method_name) { - OQS_KAT_PRNG *prng = malloc(sizeof(OQS_KAT_PRNG)); + OQS_KAT_PRNG *prng = OQS_MEM_malloc(sizeof(OQS_KAT_PRNG)); if (prng != NULL) { prng->max_kats = is_mceliece(method_name) ? 10 : 100; if (is_hqc(method_name)) { diff --git a/tests/test_kem.c b/tests/test_kem.c index b7c7baad85..3c6c70b70f 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -61,14 +61,14 @@ static OQS_STATUS kem_test_correctness(const char *method_name) { printf("Sample computation for KEM %s\n", kem->method_name); printf("================================================================================\n"); - public_key = malloc(kem->length_public_key + 2 * sizeof(magic_t)); - secret_key = malloc(kem->length_secret_key + 2 * sizeof(magic_t)); - ciphertext = malloc(kem->length_ciphertext + 2 * sizeof(magic_t)); - shared_secret_e = malloc(kem->length_shared_secret + 2 * sizeof(magic_t)); - shared_secret_d = malloc(kem->length_shared_secret + 2 * sizeof(magic_t)); + public_key = OQS_MEM_malloc(kem->length_public_key + 2 * sizeof(magic_t)); + secret_key = OQS_MEM_malloc(kem->length_secret_key + 2 * sizeof(magic_t)); + ciphertext = OQS_MEM_malloc(kem->length_ciphertext + 2 * sizeof(magic_t)); + shared_secret_e = OQS_MEM_malloc(kem->length_shared_secret + 2 * sizeof(magic_t)); + shared_secret_d = OQS_MEM_malloc(kem->length_shared_secret + 2 * sizeof(magic_t)); if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } diff --git a/tests/test_kem_mem.c b/tests/test_kem_mem.c index 7f2c98428b..4c2cbd92d7 100644 --- a/tests/test_kem_mem.c +++ b/tests/test_kem_mem.c @@ -53,11 +53,11 @@ static OQS_STATUS kem_test_correctness(const char *method_name, KEM_OPS op) { printf("Executing keygen for KEM %s\n", kem->method_name); printf("================================================================================\n"); - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); if ((public_key == NULL) || (secret_key == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } @@ -80,13 +80,13 @@ static OQS_STATUS kem_test_correctness(const char *method_name, KEM_OPS op) { printf("Executing encaps for KEM %s\n", kem->method_name); printf("================================================================================\n"); - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); - ciphertext = malloc(kem->length_ciphertext); - shared_secret_e = malloc(kem->length_shared_secret); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); + ciphertext = OQS_MEM_malloc(kem->length_ciphertext); + shared_secret_e = OQS_MEM_malloc(kem->length_shared_secret); if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } @@ -115,14 +115,14 @@ static OQS_STATUS kem_test_correctness(const char *method_name, KEM_OPS op) { printf("Executing decaps for KEM %s\n", kem->method_name); printf("================================================================================\n"); - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); - ciphertext = malloc(kem->length_ciphertext); - shared_secret_e = malloc(kem->length_shared_secret); - shared_secret_d = malloc(kem->length_shared_secret); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); + ciphertext = OQS_MEM_malloc(kem->length_ciphertext); + shared_secret_e = OQS_MEM_malloc(kem->length_shared_secret); + shared_secret_d = OQS_MEM_malloc(kem->length_shared_secret); if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } if (oqs_fload("pk", method_name, public_key, kem->length_public_key, &retlen) != OQS_SUCCESS) { diff --git a/tests/test_sig.c b/tests/test_sig.c index e94d3034c4..a5246cc9df 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -55,13 +55,13 @@ static OQS_STATUS sig_test_correctness(const char *method_name) { printf("Sample computation for signature %s\n", sig->method_name); printf("================================================================================\n"); - public_key = malloc(sig->length_public_key + 2 * sizeof(magic_t)); - secret_key = malloc(sig->length_secret_key + 2 * sizeof(magic_t)); - message = malloc(message_len + 2 * sizeof(magic_t)); - signature = malloc(sig->length_signature + 2 * sizeof(magic_t)); + public_key = OQS_MEM_malloc(sig->length_public_key + 2 * sizeof(magic_t)); + secret_key = OQS_MEM_malloc(sig->length_secret_key + 2 * sizeof(magic_t)); + message = OQS_MEM_malloc(message_len + 2 * sizeof(magic_t)); + signature = OQS_MEM_malloc(sig->length_signature + 2 * sizeof(magic_t)); if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } diff --git a/tests/test_sig_mem.c b/tests/test_sig_mem.c index 38d63cc2af..bda9bbfc5c 100644 --- a/tests/test_sig_mem.c +++ b/tests/test_sig_mem.c @@ -42,10 +42,10 @@ static OQS_STATUS sig_test_correctness(const char *method_name, SIG_OPS op) { printf("Executing keygen for SIGALG %s\n", sig->method_name); printf("================================================================================\n"); - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); if ((public_key == NULL) || (secret_key == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } rc = OQS_SIG_keypair(sig, public_key, secret_key); @@ -67,13 +67,13 @@ static OQS_STATUS sig_test_correctness(const char *method_name, SIG_OPS op) { printf("Executing sign for SIGALG %s\n", sig->method_name); printf("================================================================================\n"); - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); - message = malloc(message_len); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); + message = OQS_MEM_malloc(message_len); + signature = OQS_MEM_malloc(sig->length_signature); if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } if (oqs_fload("pk", method_name, public_key, sig->length_public_key, &signature_len) != OQS_SUCCESS) { @@ -104,13 +104,13 @@ static OQS_STATUS sig_test_correctness(const char *method_name, SIG_OPS op) { printf("Executing verify for SIGALG %s\n", sig->method_name); printf("================================================================================\n"); - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); - message = malloc(message_len); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); + message = OQS_MEM_malloc(message_len); + signature = OQS_MEM_malloc(sig->length_signature); if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } if (oqs_fload("pk", method_name, public_key, sig->length_public_key, &signature_len) != OQS_SUCCESS) { diff --git a/tests/test_sig_stfl.c b/tests/test_sig_stfl.c index 9867dbf60a..f95e613691 100644 --- a/tests/test_sig_stfl.c +++ b/tests/test_sig_stfl.c @@ -463,12 +463,12 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char #if OQS_USE_PTHREADS_IN_TESTS OQS_SIG_STFL_SECRET_KEY_SET_mutex(secret_key, sk_lock); #endif - public_key = malloc(sig->length_public_key + 2 * sizeof(magic_t)); - message = malloc(message_len + 2 * sizeof(magic_t)); - signature = malloc(sig->length_signature + 2 * sizeof(magic_t)); + public_key = OQS_MEM_malloc(sig->length_public_key + 2 * sizeof(magic_t)); + message = OQS_MEM_malloc(message_len + 2 * sizeof(magic_t)); + signature = OQS_MEM_malloc(sig->length_signature + 2 * sizeof(magic_t)); if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) { - fprintf(stderr, "ERROR: malloc failed\n"); + fprintf(stderr, "ERROR: OQS_MEM_malloc failed\n"); goto err; } @@ -530,7 +530,7 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char } /* Read public key and re-test verify.*/ - read_pk_buf = malloc(sig->length_public_key); + read_pk_buf = OQS_MEM_malloc(sig->length_public_key); if (oqs_fload("pk", file_store, read_pk_buf, sig->length_public_key, &read_pk_len) != OQS_SUCCESS) { goto err; } @@ -621,7 +621,7 @@ static OQS_STATUS sig_stfl_test_secret_key(const char *method_name, const char * goto err; } - public_key = malloc(sig_obj->length_public_key * sizeof(uint8_t)); + public_key = OQS_MEM_malloc(sig_obj->length_public_key * sizeof(uint8_t)); printf("================================================================================\n"); printf("Create stateful Secret Key %s\n", method_name); @@ -682,7 +682,7 @@ static OQS_STATUS sig_stfl_test_secret_key(const char *method_name, const char * } /* read secret key from disk */ - from_file_sk_buf = malloc(to_file_sk_len); + from_file_sk_buf = OQS_MEM_malloc(to_file_sk_len); if (oqs_fload("sk", file_store_name, from_file_sk_buf, to_file_sk_len, &from_file_sk_len) != OQS_SUCCESS) { goto err; } @@ -837,7 +837,7 @@ static OQS_STATUS sig_stfl_test_sig_gen(const char *method_name) { printf("Sig Gen 1 %s\n", method_name); printf("================================================================================\n"); - signature_1 = malloc(lock_test_sig_obj->length_signature); + signature_1 = OQS_MEM_malloc(lock_test_sig_obj->length_signature); rc = OQS_SIG_STFL_sign(lock_test_sig_obj, signature_1, &signature_len_1, message_1, message_len_1, lock_test_sk); OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc); @@ -866,7 +866,7 @@ static OQS_STATUS sig_stfl_test_sig_gen(const char *method_name) { printf("Sig Gen 2 %s\n", method_name); printf("================================================================================\n"); - signature_2 = malloc(lock_test_sig_obj->length_signature); + signature_2 = OQS_MEM_malloc(lock_test_sig_obj->length_signature); rc = OQS_SIG_STFL_sign(lock_test_sig_obj, signature_2, &signature_len_2, message_2, message_len_2, lock_test_sk); OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc); @@ -927,7 +927,7 @@ static OQS_STATUS sig_stfl_test_secret_key_lock(const char *method_name, const c goto err; } - lock_test_public_key = malloc(lock_test_sig_obj->length_public_key * sizeof(uint8_t)); + lock_test_public_key = OQS_MEM_malloc(lock_test_sig_obj->length_public_key * sizeof(uint8_t)); printf("================================================================================\n"); printf("Create stateful Secret Key %s\n", method_name); @@ -1132,11 +1132,11 @@ int main(int argc, char **argv) { lock_test_data_t td_sign = {.alg_name = alg_name, .katfile = katfile, .rc = OQS_ERROR}; lock_test_data_t td_query = {.alg_name = alg_name, .katfile = katfile, .rc = OQS_ERROR}; - test_sk_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + test_sk_lock = (pthread_mutex_t *)OQS_MEM_malloc(sizeof(pthread_mutex_t)); if (test_sk_lock == NULL) { goto err; } - sk_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + sk_lock = (pthread_mutex_t *)OQS_MEM_malloc(sizeof(pthread_mutex_t)); if (sk_lock == NULL) { goto err; } diff --git a/tests/vectors_kem.c b/tests/vectors_kem.c index d7e5b42b85..4dc8ae63bc 100644 --- a/tests/vectors_kem.c +++ b/tests/vectors_kem.c @@ -113,11 +113,11 @@ static OQS_STATUS kem_kg_vector(const char *method_name, fh = stdout; - public_key = malloc(kem->length_public_key); - secret_key = malloc(kem->length_secret_key); + public_key = OQS_MEM_malloc(kem->length_public_key); + secret_key = OQS_MEM_malloc(kem->length_secret_key); if ((public_key == NULL) || (secret_key == NULL)) { - fprintf(stderr, "[vectors_kem] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[vectors_kem] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } @@ -196,10 +196,10 @@ static OQS_STATUS kem_vector_encdec_aft(const char *method_name, fh = stdout; - ss_encaps = malloc(kem->length_shared_secret); - ct_encaps = malloc(kem->length_ciphertext); + ss_encaps = OQS_MEM_malloc(kem->length_shared_secret); + ct_encaps = OQS_MEM_malloc(kem->length_ciphertext); if ((ss_encaps == NULL) || (ct_encaps == NULL)) { - fprintf(stderr, "[vectors_kem] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[vectors_kem] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } @@ -261,10 +261,10 @@ static OQS_STATUS kem_vector_encdec_val(const char *method_name, fh = stdout; - ss_decaps = malloc(kem->length_shared_secret); + ss_decaps = OQS_MEM_malloc(kem->length_shared_secret); if (ss_decaps == NULL) { - fprintf(stderr, "[vectors_kem] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[vectors_kem] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } @@ -369,12 +369,12 @@ int main(int argc, char **argv) { goto err; } - prng_output_stream_bytes = malloc(strlen(prng_output_stream) / 2); - kg_pk_bytes = malloc(kem->length_public_key); - kg_sk_bytes = malloc(kem->length_secret_key); + prng_output_stream_bytes = OQS_MEM_malloc(strlen(prng_output_stream) / 2); + kg_pk_bytes = OQS_MEM_malloc(kem->length_public_key); + kg_sk_bytes = OQS_MEM_malloc(kem->length_secret_key); if ((prng_output_stream_bytes == NULL) || (kg_pk_bytes == NULL) || (kg_sk_bytes == NULL)) { - fprintf(stderr, "[vectors_kem] ERROR: malloc failed!\n"); + fprintf(stderr, "[vectors_kem] ERROR: OQS_MEM_malloc failed!\n"); rc = OQS_ERROR; goto err; } @@ -399,13 +399,13 @@ int main(int argc, char **argv) { goto err; } - prng_output_stream_bytes = malloc(strlen(prng_output_stream) / 2); - encdec_aft_pk_bytes = malloc(kem->length_public_key); - encdec_aft_k_bytes = malloc(kem->length_shared_secret); - encdec_aft_c_bytes = malloc(kem->length_ciphertext); + prng_output_stream_bytes = OQS_MEM_malloc(strlen(prng_output_stream) / 2); + encdec_aft_pk_bytes = OQS_MEM_malloc(kem->length_public_key); + encdec_aft_k_bytes = OQS_MEM_malloc(kem->length_shared_secret); + encdec_aft_c_bytes = OQS_MEM_malloc(kem->length_ciphertext); if ((prng_output_stream_bytes == NULL) || (encdec_aft_pk_bytes == NULL) || (encdec_aft_k_bytes == NULL) || (encdec_aft_c_bytes == NULL)) { - fprintf(stderr, "[vectors_kem] ERROR: malloc failed!\n"); + fprintf(stderr, "[vectors_kem] ERROR: OQS_MEM_malloc failed!\n"); rc = OQS_ERROR; goto err; } @@ -428,12 +428,12 @@ int main(int argc, char **argv) { goto err; } - encdec_val_sk_bytes = malloc(kem->length_secret_key); - encdec_val_k_bytes = malloc(kem->length_shared_secret); - encdec_val_c_bytes = malloc(kem->length_ciphertext); + encdec_val_sk_bytes = OQS_MEM_malloc(kem->length_secret_key); + encdec_val_k_bytes = OQS_MEM_malloc(kem->length_shared_secret); + encdec_val_c_bytes = OQS_MEM_malloc(kem->length_ciphertext); if ((encdec_val_sk_bytes == NULL) || (encdec_val_k_bytes == NULL) || (encdec_val_c_bytes == NULL)) { - fprintf(stderr, "[vectors_kem] ERROR: malloc failed!\n"); + fprintf(stderr, "[vectors_kem] ERROR: OQS_MEM_malloc failed!\n"); rc = OQS_ERROR; goto err; } diff --git a/tests/vectors_sig.c b/tests/vectors_sig.c index 04652498a9..bfccfb23c2 100644 --- a/tests/vectors_sig.c +++ b/tests/vectors_sig.c @@ -122,11 +122,11 @@ OQS_STATUS sig_vector(const char *method_name, fh = stdout; - public_key = malloc(sig->length_public_key); - secret_key = malloc(sig->length_secret_key); - signature = malloc(sig->length_signature); + public_key = OQS_MEM_malloc(sig->length_public_key); + secret_key = OQS_MEM_malloc(sig->length_secret_key); + signature = OQS_MEM_malloc(sig->length_signature); if ((public_key == NULL) || (secret_key == NULL) || (signature == NULL)) { - fprintf(stderr, "[vectors_sig] %s ERROR: malloc failed!\n", method_name); + fprintf(stderr, "[vectors_sig] %s ERROR: OQS_MEM_malloc failed!\n", method_name); goto err; } @@ -232,15 +232,15 @@ int main(int argc, char **argv) { goto err; } - prng_output_stream_bytes = malloc(strlen(prng_output_stream) / 2); - sig_msg_bytes = malloc(strlen(sig_msg) / 2); - sig_sk_bytes = malloc(sig->length_secret_key); - verif_sig_bytes = malloc(sig->length_signature); - verif_pk_bytes = malloc(sig->length_public_key); - verif_msg_bytes = malloc(strlen(verif_msg) / 2); + prng_output_stream_bytes = OQS_MEM_malloc(strlen(prng_output_stream) / 2); + sig_msg_bytes = OQS_MEM_malloc(strlen(sig_msg) / 2); + sig_sk_bytes = OQS_MEM_malloc(sig->length_secret_key); + verif_sig_bytes = OQS_MEM_malloc(sig->length_signature); + verif_pk_bytes = OQS_MEM_malloc(sig->length_public_key); + verif_msg_bytes = OQS_MEM_malloc(strlen(verif_msg) / 2); if ((prng_output_stream_bytes == NULL) || (sig_msg_bytes == NULL) || (sig_sk_bytes == NULL) || (verif_sig_bytes == NULL) || (verif_pk_bytes == NULL) || (verif_msg_bytes == NULL)) { - fprintf(stderr, "[vectors_sig] ERROR: malloc failed!\n"); + fprintf(stderr, "[vectors_sig] ERROR: OQS_MEM_malloc failed!\n"); rc = OQS_ERROR; goto err; }