From b61754c56b72357ab07333941dcd95be6af50422 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Mon, 16 Sep 2024 17:30:36 -0700 Subject: [PATCH 01/34] [#1823] replace malloc/calloc/strdup/free with openssl allocator Signed-off-by: Songling Han --- 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 | 41 ++++++-- src/common/common.h | 36 +++++++ src/common/sha2/sha2_c.c | 8 +- src/common/sha3/ossl_sha3.c | 12 +-- src/common/sha3/ossl_sha3x4.c | 12 +-- 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/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_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 ++--- 38 files changed, 375 insertions(+), 312 deletions(-) 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 ee7b19f703..1cdcd2baa4 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -276,7 +276,7 @@ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { } void *OQS_MEM_checked_malloc(size_t len) { - void *ptr = malloc(len); + void *ptr = OQS_MEM_malloc(len); if (ptr == NULL) { fprintf(stderr, "Memory allocation failed\n"); abort(); @@ -298,17 +298,38 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - free(ptr); // IGNORE free-check + OQS_MEM_free(ptr); // IGNORE free-check } } OQS_API void OQS_MEM_insecure_free(void *ptr) { - free(ptr); // IGNORE free-check + OQS_MEM_free(ptr); // IGNORE free-check } void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { -#if defined(OQS_HAVE_ALIGNED_ALLOC) // glibc and other implementations providing aligned_alloc - return aligned_alloc(alignment, size); +#if defined(OQS_USE_OPENSSL) + // Use OpenSSL's memory allocation functions + if (!size) { + return NULL; + } + const size_t offset = alignment - 1 + sizeof(uint8_t); + uint8_t *buffer = OPENSSL_malloc(size + offset); + if (!buffer) { + return NULL; + } + uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1)); + ptrdiff_t diff = ptr - buffer; + if (diff > UINT8_MAX) { + // Free and return NULL if alignment is too large + OPENSSL_free(buffer); + errno = EINVAL; + return NULL; + } + // Store the difference so that the free function can use it + ptr[-1] = diff; + return ptr; +#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) if (alignment & (alignment - 1) || size & (alignment - 1) || alignment < sizeof(void *)) { @@ -369,8 +390,14 @@ 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 defined(OQS_USE_OPENSSL) + // Use OpenSSL's free function + if (ptr) { + uint8_t *u8ptr = ptr; + OPENSSL_free(u8ptr - u8ptr[-1]); + } +#elif defined(OQS_HAVE_ALIGNED_ALLOC) || defined(OQS_HAVE_POSIX_MEMALIGN) || defined(OQS_HAVE_MEMALIGN) + free(ptr); // IGNORE free-check #elif defined(__MINGW32__) || defined(__MINGW64__) __mingw_aligned_free(ptr); #elif defined(_MSC_VER) diff --git a/src/common/common.h b/src/common/common.h index 18993d0a5f..147dcdb84e 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -20,6 +20,42 @@ extern "C" { #endif +#if defined(OQS_USE_OPENSSL) + #ifndef OPENSSL_malloc + # define OPENSSL_malloc(num) \ + CRYPTO_malloc(num, __FILE__, __LINE__) + #endif + + #ifndef OPENSSL_zalloc + # define OPENSSL_zalloc(num) \ + CRYPTO_zalloc(num, __FILE__, __LINE__) + #endif + + #ifndef OPENSSL_free + # define OPENSSL_free(addr) \ + CRYPTO_free(addr, __FILE__, __LINE__) + #endif + + #ifndef OPENSSL_strdup + # define OPENSSL_strdup(str) \ + CRYPTO_strdup(str, __FILE__, __LINE__) + #endif + + extern void *CRYPTO_malloc(size_t num, const char *file, int line); + extern void *CRYPTO_zalloc(size_t num, const char *file, int line); + extern void CRYPTO_free(void *str, const char *file, int line); + extern char *CRYPTO_strdup(const char *str, const char* file, int line); + #define OQS_MEM_malloc(size) OPENSSL_malloc(size) + #define OQS_MEM_free(ptr) OPENSSL_free(ptr) + #define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size)) + #define OQS_MEM_strdup(str) OPENSSL_strdup(str) +#else + #define OQS_MEM_malloc(size) malloc(size) + #define OQS_MEM_free(ptr) free(ptr) + #define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) + #define OQS_MEM_strdup(str) strdup(str) +#endif + /** * Macro for terminating the program if x is * a null pointer. diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index e5bd350889..544e0af8da 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -588,22 +588,22 @@ void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *state /* Destroy the hash state. */ void oqs_sha2_sha224_inc_ctx_release_c(sha224ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha256_inc_ctx_release_c(sha256ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha384_inc_ctx_release_c(sha384ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) { - free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) { diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index 5d36f2280c..2ac3e98cb5 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)(); @@ -201,7 +201,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); - free(tmp); // IGNORE free-check + OQS_MEM_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -211,7 +211,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_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 +242,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)(); @@ -275,7 +275,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); - free(tmp); // IGNORE free-check + OQS_MEM_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -285,7 +285,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_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..eb14a9f1fc 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)(); @@ -94,7 +94,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_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -117,7 +117,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_free(s); // IGNORE free-check } static void SHA3_shake128_x4_inc_ctx_reset(OQS_SHA3_shake128_x4_inc_ctx *state) { @@ -154,7 +154,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)(); @@ -215,7 +215,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_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -238,7 +238,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_free(s); // IGNORE free-check } static void SHA3_shake256_x4_inc_ctx_reset(OQS_SHA3_shake256_x4_inc_ctx *state) { diff --git a/src/sig_stfl/lms/external/hss_alloc.c b/src/sig_stfl/lms/external/hss_alloc.c index 53eaa762e2..c5043f8a8e 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_free(tree->subtree[j][k]); // IGNORE free-check hss_zeroize( tree, sizeof *tree ); /* We have seeds here */ } - free(tree); // IGNORE free-check + OQS_MEM_free(tree); // IGNORE free-check } for (i=0; isigned_pk[i]); // IGNORE free-check + OQS_MEM_free(w->signed_pk[i]); // IGNORE free-check } - free(w->stack); // IGNORE free-check + OQS_MEM_free(w->stack); // IGNORE free-check hss_zeroize( w, sizeof *w ); /* We have secret information here */ - free(w); // IGNORE free-check + OQS_MEM_free(w); // IGNORE free-check } diff --git a/src/sig_stfl/lms/external/hss_generate.c b/src/sig_stfl/lms/external/hss_generate.c index 28fcc9eaee..f3d3f0212d 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_free( order[i].sub ); // IGNORE free-check } #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_free( sub ); // IGNORE free-check 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..5d1eca28da 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_free(col); // IGNORE free-check return 0; } if (0 != pthread_mutex_init( &col->write_lock, 0 )) { pthread_mutex_destroy( &col->lock ); - free(col); // IGNORE free-check + OQS_MEM_free(col); // IGNORE free-check 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_free(w); // IGNORE free-check /* 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_free(w); // IGNORE free-check 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_free(col); // IGNORE free-check } 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..6ba8dd9f9e 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_malloc(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/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_hash.c b/tests/test_hash.c index 788f41ffb2..d374a9caa1 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_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_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_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_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_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_free(msg); return -5; } //Test inc API print_hex(output, 32); - free(msg); + OQS_MEM_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_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_free(msg); return -3; } print_hex(output, 48); - free(msg); + OQS_MEM_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_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_free(msg); return -3; } print_hex(output, 64); - free(msg); + OQS_MEM_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_free(output); + OQS_MEM_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; } From 3dc12841379a474ca037d95413b864922dbe7a36 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 17 Sep 2024 04:57:11 +0000 Subject: [PATCH 02/34] [#1823] update memory allocator for copy_from_upstream Signed-off-by: Songling Han --- scripts/copy_from_upstream/src/kem/family/kem_scheme.c | 4 ++-- scripts/copy_from_upstream/src/sig/family/sig_scheme.c | 4 ++-- .../tests/kat_sig.c/combine_message_signature.fragment | 8 ++++---- src/kem/classic_mceliece/kem_classic_mceliece_348864.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_348864f.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_460896.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_460896f.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_6688128.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_6688128f.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_6960119.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_6960119f.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_8192128.c | 2 +- src/kem/classic_mceliece/kem_classic_mceliece_8192128f.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/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 ++-- src/sig/sphincs/sig_sphincs_sha2_128f_simple.c | 2 +- src/sig/sphincs/sig_sphincs_sha2_128s_simple.c | 2 +- src/sig/sphincs/sig_sphincs_sha2_192f_simple.c | 2 +- src/sig/sphincs/sig_sphincs_sha2_192s_simple.c | 2 +- src/sig/sphincs/sig_sphincs_sha2_256f_simple.c | 2 +- src/sig/sphincs/sig_sphincs_sha2_256s_simple.c | 2 +- src/sig/sphincs/sig_sphincs_shake_128f_simple.c | 2 +- src/sig/sphincs/sig_sphincs_shake_128s_simple.c | 2 +- src/sig/sphincs/sig_sphincs_shake_192f_simple.c | 2 +- src/sig/sphincs/sig_sphincs_shake_192s_simple.c | 2 +- src/sig/sphincs/sig_sphincs_shake_256f_simple.c | 2 +- src/sig/sphincs/sig_sphincs_shake_256s_simple.c | 2 +- 66 files changed, 74 insertions(+), 74 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/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/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/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; } From 04a9a736f33241975a730a003c68a45161ecc46a Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 17 Sep 2024 07:41:21 +0000 Subject: [PATCH 03/34] [#1823] format code Signed-off-by: Songling Han --- src/common/common.c | 58 ++++++++++++++++++++-------------------- src/common/common.h | 48 ++++++++++++++++----------------- src/common/sha2/sha2_c.c | 8 +++--- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/common/common.c b/src/common/common.c index 1cdcd2baa4..1101c98434 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -298,38 +298,38 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - OQS_MEM_free(ptr); // IGNORE free-check + OQS_MEM_free(ptr); // IGNORE free-check } } OQS_API void OQS_MEM_insecure_free(void *ptr) { - OQS_MEM_free(ptr); // IGNORE free-check + OQS_MEM_free(ptr); // IGNORE free-check } void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { #if defined(OQS_USE_OPENSSL) - // Use OpenSSL's memory allocation functions - if (!size) { - return NULL; - } - const size_t offset = alignment - 1 + sizeof(uint8_t); - uint8_t *buffer = OPENSSL_malloc(size + offset); - if (!buffer) { - return NULL; - } - uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1)); - ptrdiff_t diff = ptr - buffer; - if (diff > UINT8_MAX) { - // Free and return NULL if alignment is too large - OPENSSL_free(buffer); - errno = EINVAL; - return NULL; - } - // Store the difference so that the free function can use it - ptr[-1] = diff; - return ptr; + // Use OpenSSL's memory allocation functions + if (!size) { + return NULL; + } + const size_t offset = alignment - 1 + sizeof(uint8_t); + uint8_t *buffer = OPENSSL_malloc(size + offset); + if (!buffer) { + return NULL; + } + uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1)); + ptrdiff_t diff = ptr - buffer; + if (diff > UINT8_MAX) { + // Free and return NULL if alignment is too large + OPENSSL_free(buffer); + errno = EINVAL; + return NULL; + } + // Store the difference so that the free function can use it + ptr[-1] = diff; + return ptr; #elif defined(OQS_HAVE_ALIGNED_ALLOC) // glibc and other implementations providing aligned_alloc - return aligned_alloc(alignment, size); + return aligned_alloc(alignment, size); #else // Check alignment (power of 2, and >= sizeof(void*)) and size (multiple of alignment) if (alignment & (alignment - 1) || size & (alignment - 1) || alignment < sizeof(void *)) { @@ -391,13 +391,13 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { void OQS_MEM_aligned_free(void *ptr) { #if defined(OQS_USE_OPENSSL) - // Use OpenSSL's free function - if (ptr) { - uint8_t *u8ptr = ptr; - OPENSSL_free(u8ptr - u8ptr[-1]); - } + // Use OpenSSL's free function + if (ptr) { + uint8_t *u8ptr = ptr; + OPENSSL_free(u8ptr - u8ptr[-1]); + } #elif defined(OQS_HAVE_ALIGNED_ALLOC) || defined(OQS_HAVE_POSIX_MEMALIGN) || defined(OQS_HAVE_MEMALIGN) - free(ptr); // IGNORE free-check + free(ptr); // IGNORE free-check #elif defined(__MINGW32__) || defined(__MINGW64__) __mingw_aligned_free(ptr); #elif defined(_MSC_VER) diff --git a/src/common/common.h b/src/common/common.h index 147dcdb84e..82e9e5bfcc 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -21,39 +21,39 @@ extern "C" { #endif #if defined(OQS_USE_OPENSSL) - #ifndef OPENSSL_malloc - # define OPENSSL_malloc(num) \ +#ifndef OPENSSL_malloc +# define OPENSSL_malloc(num) \ CRYPTO_malloc(num, __FILE__, __LINE__) - #endif +#endif - #ifndef OPENSSL_zalloc - # define OPENSSL_zalloc(num) \ +#ifndef OPENSSL_zalloc +# define OPENSSL_zalloc(num) \ CRYPTO_zalloc(num, __FILE__, __LINE__) - #endif +#endif - #ifndef OPENSSL_free - # define OPENSSL_free(addr) \ +#ifndef OPENSSL_free +# define OPENSSL_free(addr) \ CRYPTO_free(addr, __FILE__, __LINE__) - #endif +#endif - #ifndef OPENSSL_strdup - # define OPENSSL_strdup(str) \ +#ifndef OPENSSL_strdup +# define OPENSSL_strdup(str) \ CRYPTO_strdup(str, __FILE__, __LINE__) - #endif +#endif - extern void *CRYPTO_malloc(size_t num, const char *file, int line); - extern void *CRYPTO_zalloc(size_t num, const char *file, int line); - extern void CRYPTO_free(void *str, const char *file, int line); - extern char *CRYPTO_strdup(const char *str, const char* file, int line); - #define OQS_MEM_malloc(size) OPENSSL_malloc(size) - #define OQS_MEM_free(ptr) OPENSSL_free(ptr) - #define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size)) - #define OQS_MEM_strdup(str) OPENSSL_strdup(str) +extern void *CRYPTO_malloc(size_t num, const char *file, int line); +extern void *CRYPTO_zalloc(size_t num, const char *file, int line); +extern void CRYPTO_free(void *str, const char *file, int line); +extern char *CRYPTO_strdup(const char *str, const char *file, int line); +#define OQS_MEM_malloc(size) OPENSSL_malloc(size) +#define OQS_MEM_free(ptr) OPENSSL_free(ptr) +#define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size)) +#define OQS_MEM_strdup(str) OPENSSL_strdup(str) #else - #define OQS_MEM_malloc(size) malloc(size) - #define OQS_MEM_free(ptr) free(ptr) - #define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) - #define OQS_MEM_strdup(str) strdup(str) +#define OQS_MEM_malloc(size) malloc(size) +#define OQS_MEM_free(ptr) free(ptr) +#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size) +#define OQS_MEM_strdup(str) strdup(str) #endif /** diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index 544e0af8da..5ead4c1ee7 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -588,22 +588,22 @@ void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *state /* Destroy the hash state. */ void oqs_sha2_sha224_inc_ctx_release_c(sha224ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha256_inc_ctx_release_c(sha256ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha384_inc_ctx_release_c(sha384ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_free(state->ctx); // IGNORE free-check } void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) { From 201771c8784f5766d748c11e6afd5e639990f262 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 17 Sep 2024 08:26:09 +0000 Subject: [PATCH 04/34] [#1823] Use OpenSSL Memory Allocator for BIKE, FrodoKEM, and NTRUPrime Signed-off-by: Songling Han --- src/kem/bike/kem_bike.c | 6 +++--- 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/ntruprime/kem_ntruprime_sntrup761.c | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-) 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/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/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; } From a7aef7ddb3bc4c87569989d1d5151ff360900dd9 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 17 Sep 2024 20:37:08 +0000 Subject: [PATCH 05/34] [#1823] Add Comments for Doxygen Signed-off-by: Songling Han --- src/common/common.h | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/common/common.h b/src/common/common.h index 82e9e5bfcc..58f9575f2a 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -20,6 +20,13 @@ 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) #ifndef OPENSSL_malloc # define OPENSSL_malloc(num) \ @@ -45,14 +52,54 @@ extern void *CRYPTO_malloc(size_t num, const char *file, int line); extern void *CRYPTO_zalloc(size_t num, const char *file, int line); extern void CRYPTO_free(void *str, const char *file, int line); extern char *CRYPTO_strdup(const char *str, const char *file, int line); +/** +* 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) +/** + * Frees the allocated memory. + * @param ptr The pointer to the memory to be freed. + */ #define OQS_MEM_free(ptr) OPENSSL_free(ptr) +/** + * 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) +/** + * Frees the allocated memory. + * @param ptr The pointer to the memory to be freed. + */ #define OQS_MEM_free(ptr) free(ptr) +/** + * 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) +/** + * 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) #endif From ac5c941b137aa7989d0db814035df9d8a4d03774 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 07:20:40 +0000 Subject: [PATCH 06/34] sig_stfl build fix Signed-off-by: Songling Han --- src/sig_stfl/lms/CMakeLists.txt | 1 + src/sig_stfl/lms/external/common_defs.h | 2 +- src/sig_stfl/xmss/external/wots.c | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) 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/xmss/external/wots.c b/src/sig_stfl/xmss/external/wots.c index 6ba8dd9f9e..3d87c206b2 100644 --- a/src/sig_stfl/xmss/external/wots.c +++ b/src/sig_stfl/xmss/external/wots.c @@ -187,7 +187,7 @@ 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 = OQS_MEM_malloc(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 = OQS_MEM_malloc(thash_buf_len); unsigned int i; From 74c4dbaa840ef28b904e1e6b1be9095b03904133 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 07:38:09 +0000 Subject: [PATCH 07/34] fix ptrdiff_t failure Signed-off-by: Songling Han --- src/common/common.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/common.c b/src/common/common.c index 1101c98434..81f1e80cd9 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -12,6 +12,7 @@ #include #include #include +#include #if defined(OQS_DIST_BUILD) && defined(OQS_USE_PTHREADS) #include From f511ba026fc864b51bbd8f338a51c138f047d268 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 08:46:12 +0000 Subject: [PATCH 08/34] include openssl/crypto.h and resolve conflict varible for ntru Signed-off-by: Songling Han --- src/common/common.h | 24 +------------ .../crypto_core_invsntrup761.c | 26 +++++++------- .../crypto_core_weightsntrup761.c | 4 +-- .../crypto_core_wforcesntrup761.c | 2 +- .../ntruprime/pqclean_sntrup761_avx2/kem.c | 34 +++++++++--------- .../ntruprime/pqclean_sntrup761_avx2/params.h | 2 +- .../crypto_core_inv3sntrup761.c | 36 +++++++++---------- .../crypto_core_invsntrup761.c | 36 +++++++++---------- .../crypto_core_mult3sntrup761.c | 24 ++++++------- .../crypto_core_multsntrup761.c | 22 ++++++------ .../crypto_core_weightsntrup761.c | 2 +- .../crypto_core_wforcesntrup761.c | 4 +-- .../ntruprime/pqclean_sntrup761_clean/kem.c | 34 +++++++++--------- .../pqclean_sntrup761_clean/params.h | 2 +- 14 files changed, 115 insertions(+), 137 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index 58f9575f2a..36c8752599 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -28,30 +28,8 @@ extern "C" { * standard C library functions otherwise. */ #if defined(OQS_USE_OPENSSL) -#ifndef OPENSSL_malloc -# define OPENSSL_malloc(num) \ - CRYPTO_malloc(num, __FILE__, __LINE__) -#endif - -#ifndef OPENSSL_zalloc -# define OPENSSL_zalloc(num) \ - CRYPTO_zalloc(num, __FILE__, __LINE__) -#endif - -#ifndef OPENSSL_free -# define OPENSSL_free(addr) \ - CRYPTO_free(addr, __FILE__, __LINE__) -#endif - -#ifndef OPENSSL_strdup -# define OPENSSL_strdup(str) \ - CRYPTO_strdup(str, __FILE__, __LINE__) -#endif +#include -extern void *CRYPTO_malloc(size_t num, const char *file, int line); -extern void *CRYPTO_zalloc(size_t num, const char *file, int line); -extern void CRYPTO_free(void *str, const char *file, int line); -extern char *CRYPTO_strdup(const char *str, const char *file, int line); /** * Allocates memory of a given size. * @param size The size of the memory to be allocated in bytes. diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c index e27bedfb7a..712fb77564 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c @@ -129,7 +129,7 @@ static inline void vectormodq_xswapeliminate(Fq *f, Fq *g, int len, const Fq f0, int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, const unsigned char *inbytes) { small *in = (void *) inbytes; int loop; - Fq out[p], f[ppad], g[ppad], v[ppad], r[ppad]; + Fq out[p_param], f[ppad], g[ppad], v[ppad], r[ppad]; Fq f0, g0; Fq scale; int i; @@ -142,14 +142,14 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con f[i] = 0; } f[0] = 1; - f[p - 1] = -1; - f[p] = -1; + f[p_param - 1] = -1; + f[p_param] = -1; /* generalization: initialize f to reversal of any deg-p polynomial m */ - for (i = 0; i < p; ++i) { - g[i] = in[p - 1 - i]; + for (i = 0; i < p_param; ++i) { + g[i] = in[p_param - 1 - i]; } - for (i = p; i < ppad; ++i) { + for (i = p_param; i < ppad; ++i) { g[i] = 0; } @@ -162,7 +162,7 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con v[i] = 0; } - for (loop = 0; loop < p; ++loop) { + for (loop = 0; loop < p_param; ++loop) { g0 = Fq_freeze(g[0]); f0 = f[0]; if (q > 5167) { @@ -180,11 +180,11 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con f[0] = f0; - vectormodq_swapeliminate(f + 1, g + 1, p, f0, g0, swap); + vectormodq_swapeliminate(f + 1, g + 1, p_param, f0, g0, swap); vectormodq_xswapeliminate(v, r, loop + 1, f0, g0, swap); } - for (loop = p - 1; loop > 0; --loop) { + for (loop = p_param - 1; loop > 0; --loop) { g0 = Fq_freeze(g[0]); f0 = f[0]; if (q > 5167) { @@ -203,15 +203,15 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con f[0] = f0; vectormodq_swapeliminate(f + 1, g + 1, loop, f0, g0, swap); - vectormodq_xswapeliminate(v, r, p, f0, g0, swap); + vectormodq_xswapeliminate(v, r, p_param, f0, g0, swap); } scale = Fq_recip(Fq_freeze(f[0])); - for (i = 0; i < p; ++i) { - out[i] = Fq_bigfreeze(scale * (int32)Fq_freeze(v[p - i])); + for (i = 0; i < p_param; ++i) { + out[i] = Fq_bigfreeze(scale * (int32)Fq_freeze(v[p_param - i])); } crypto_encode_pxint16(outbytes, out); - outbytes[2 * p] = (unsigned char) int16_nonzero_mask((int16) delta); + outbytes[2 * p_param] = (unsigned char) int16_nonzero_mask((int16) delta); return 0; } diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c index 5d1b85e37a..1d44f4cceb 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c @@ -14,10 +14,10 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_weightsntrup761(unsigned char *outbytes, __m256i sum, sumhi; int16 weight; - sum = _mm256_loadu_si256((__m256i *) (in + p - 32)); + sum = _mm256_loadu_si256((__m256i *) (in + p_param - 32)); sum &= endingmask; - for (i = p - 32; i >= 0; i -= 32) { + for (i = p_param - 32; i >= 0; i -= 32) { __m256i bits = _mm256_loadu_si256((__m256i *) in); bits &= _mm256_set1_epi8(1); sum = _mm256_add_epi8(sum, bits); diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c index 0dedf0af99..a43eb9f821 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c @@ -40,7 +40,7 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_wforcesntrup761(unsigned char *out, const out += i; } - i = p - w - 32; + i = p_param - w - 32; for (;;) { do { __m256i x = _mm256_loadu_si256((__m256i *) in); diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c index d0d3ffae97..a7ff6e25bd 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c @@ -78,37 +78,37 @@ static void Short_random(small *out) { uint32 L[ppadsort]; int i; - randombytes((unsigned char *) L, 4 * p); + randombytes((unsigned char *) L, 4 * p_param); crypto_decode_pxint32(L, (unsigned char *) L); for (i = 0; i < w; ++i) { L[i] = L[i] & (uint32) - 2; } - for (i = w; i < p; ++i) { + for (i = w; i < p_param; ++i) { L[i] = (L[i] & (uint32) - 3) | 1; } - for (i = p; i < ppadsort; ++i) { + for (i = p_param; i < ppadsort; ++i) { L[i] = 0xffffffff; } PQCLEAN_SNTRUP761_AVX2_crypto_sort_uint32(L, ppadsort); - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { out[i] = (small) ((L[i] & 3) - 1); } } static void Small_random(small *out) { - uint32 L[p]; + uint32 L[p_param]; int i; randombytes((unsigned char *) L, sizeof L); crypto_decode_pxint32(L, (unsigned char *) L); - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { out[i] = (small) ((((L[i] & 0x3fffffff) * 3) >> 30) - 1); } } /* ----- Streamlined NTRU Prime */ -typedef small Inputs[p]; /* passed by reference */ +typedef small Inputs[p_param]; /* passed by reference */ #define Ciphertexts_bytes Rounded_bytes #define SecretKeys_bytes (2*Small_bytes) #define PublicKeys_bytes Rq_bytes @@ -119,7 +119,7 @@ typedef small Inputs[p]; /* passed by reference */ /* also set x[0]=2, and x[1:1+Hash_bytes] = Hash3(r_enc) */ /* also overwrite x[1+Hash_bytes:1+2*Hash_bytes] */ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) { - Fq h[p]; + Fq h[p_param]; int i; Small_encode(r_enc + 1, r); @@ -137,14 +137,14 @@ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const int PQCLEAN_SNTRUP761_AVX2_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { - small g[p]; + small g[p_param]; for (;;) { Small_random(g); { - small v[p + 1]; + small v[p_param + 1]; small vp; crypto_core_inv3((unsigned char *) v, (const unsigned char *) g); - vp = v[p]; + vp = v[p_param]; crypto_declassify(&vp, sizeof vp); if (vp == 0) { Small_encode(sk + Small_bytes, v); @@ -153,11 +153,11 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { } } { - small f[p]; + small f[p_param]; Short_random(f); Small_encode(sk, f); { - Fq h[p + 1]; + Fq h[p_param + 1]; Rq_recip3(h, f); /* always works */ Rq_mult_small(h, g); Rq_encode(pk, h); @@ -212,17 +212,17 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_kem_dec(uint8_t *k, const uint8_t *c, const ui int mask, i; Inputs r; { - Fq d[p]; + Fq d[p_param]; Rounded_decode(d, c); { - small f[p]; + small f[p_param]; Small_decode(f, sk); Rq_mult_small(d, f); Rq_mult3(d, d); } { - small e[p]; - small v[p]; + small e[p_param]; + small v[p_param]; R3_fromRq(e, d); Small_decode(v, sk + Small_bytes); R3_mult(r, e, v); diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h b/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h index 0224bbef70..f924b8e422 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h @@ -32,7 +32,7 @@ #define ppad 769 #define endingmask _mm256_set_epi8(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0) #define crypto_core_weight PQCLEAN_SNTRUP761_AVX2_crypto_core_weightsntrup761 -#define p 761 +#define p_param 761 #define q 4591 #define w 286 diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c index 87f3750a4c..208332315a 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c @@ -43,32 +43,32 @@ static small F3_freeze(int16 x) { int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, const unsigned char *inbytes) { small *out = (void *) outbytes; small *in = (void *) inbytes; - small f[p + 1], g[p + 1], v[p + 1], r[p + 1]; + small f[p_param + 1], g[p_param + 1], v[p_param + 1], r[p_param + 1]; int i, loop, delta; int sign, swap, t; - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { v[i] = 0; } - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { r[i] = 0; } r[0] = 1; - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { f[i] = 0; } f[0] = 1; - f[p - 1] = f[p] = -1; - for (i = 0; i < p; ++i) { + f[p_param - 1] = f[p_param] = -1; + for (i = 0; i < p_param; ++i) { small i1 = in[i] & 1; - g[p - 1 - i] = (small) (i1 - (in[i] & (i1 << 1))); + g[p_param - 1 - i] = (small) (i1 - (in[i] & (i1 << 1))); } - g[p] = 0; + g[p_param] = 0; delta = 1; - for (loop = 0; loop < 2 * p - 1; ++loop) { - for (i = p; i > 0; --i) { + for (loop = 0; loop < 2 * p_param - 1; ++loop) { + for (i = p_param; i > 0; --i) { v[i] = v[i - 1]; } v[0] = 0; @@ -78,7 +78,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, c delta ^= swap & (delta ^ -delta); delta += 1; - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { t = swap & (f[i] ^ g[i]); f[i] ^= (small) t; g[i] ^= (small) t; @@ -87,24 +87,24 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, c r[i] ^= (small) t; } - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { g[i] = F3_freeze((int16) (g[i] + sign * f[i])); } - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { r[i] = F3_freeze((int16) (r[i] + sign * v[i])); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { g[i] = g[i + 1]; } - g[p] = (int16) 0; + g[p_param] = (int16) 0; } sign = (int) f[0]; - for (i = 0; i < p; ++i) { - out[i] = (small) (sign * v[p - 1 - i]); + for (i = 0; i < p_param; ++i) { + out[i] = (small) (sign * v[p_param - 1 - i]); } - out[p] = (small) int16_nonzero_mask((int16) delta); + out[p_param] = (small) int16_nonzero_mask((int16) delta); return 0; } diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c index f9c8a09572..e86ab7ddac 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c @@ -62,33 +62,33 @@ static Fq Fq_recip(Fq a1) { /* outbytes[2*p] is 0 if recip succeeded; else -1 */ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_invsntrup761(unsigned char *outbytes, const unsigned char *inbytes) { small *in = (void *) inbytes; - Fq out[p], f[p + 1], g[p + 1], v[p + 1], r[p + 1]; + Fq out[p_param], f[p_param + 1], g[p_param + 1], v[p_param + 1], r[p_param + 1]; int i, loop, delta; int swap, t; int32 f0, g0; Fq scale; - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { v[i] = 0; } - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { r[i] = 0; } r[0] = Fq_recip(3); - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { f[i] = 0; } f[0] = 1; - f[p - 1] = f[p] = -1; - for (i = 0; i < p; ++i) { - g[p - 1 - i] = (Fq) in[i]; + f[p_param - 1] = f[p_param] = -1; + for (i = 0; i < p_param; ++i) { + g[p_param - 1 - i] = (Fq) in[i]; } - g[p] = 0; + g[p_param] = 0; delta = 1; - for (loop = 0; loop < 2 * p - 1; ++loop) { - for (i = p; i > 0; --i) { + for (loop = 0; loop < 2 * p_param - 1; ++loop) { + for (i = p_param; i > 0; --i) { v[i] = v[i - 1]; } v[0] = 0; @@ -97,7 +97,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_invsntrup761(unsigned char *outbytes, co delta ^= swap & (delta ^ -delta); delta += 1; - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { t = swap & (f[i] ^ g[i]); f[i] ^= (Fq) t; g[i] ^= (Fq) t; @@ -108,25 +108,25 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_invsntrup761(unsigned char *outbytes, co f0 = f[0]; g0 = g[0]; - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { g[i] = Fq_bigfreeze(f0 * g[i] - g0 * f[i]); } - for (i = 0; i < p + 1; ++i) { + for (i = 0; i < p_param + 1; ++i) { r[i] = Fq_bigfreeze(f0 * r[i] - g0 * v[i]); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { g[i] = g[i + 1]; } - g[p] = 0; + g[p_param] = 0; } scale = Fq_recip(f[0]); - for (i = 0; i < p; ++i) { - out[i] = Fq_bigfreeze(scale * (int32)v[p - 1 - i]); + for (i = 0; i < p_param; ++i) { + out[i] = Fq_bigfreeze(scale * (int32)v[p_param - 1 - i]); } crypto_encode_pxint16(outbytes, out); - outbytes[2 * p] = (unsigned char) int16_nonzero_mask((int16) delta); + outbytes[2 * p_param] = (unsigned char) int16_nonzero_mask((int16) delta); return 0; } diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c index b25892b41e..5ca7620d95 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c @@ -13,44 +13,44 @@ static small F3_freeze(int16 x) { int PQCLEAN_SNTRUP761_CLEAN_crypto_core_mult3sntrup761(unsigned char *outbytes, const unsigned char *inbytes, const unsigned char *kbytes) { small *h = (void *) outbytes; - small f[p]; - small g[p]; - small fg[p + p - 1]; + small f[p_param]; + small g[p_param]; + small fg[p_param + p_param - 1]; int16 result; int i, j; - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { small fi = (small) inbytes[i]; small fi0 = fi & 1; f[i] = (small) (fi0 - (fi & (fi0 << 1))); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { small gi = (small) kbytes[i]; small gi0 = gi & 1; g[i] = (small) (gi0 - (gi & (gi0 << 1))); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { result = 0; for (j = 0; j <= i; ++j) { result += (small) (f[j] * g[i - j]); } fg[i] = F3_freeze(result); } - for (i = p; i < p + p - 1; ++i) { + for (i = p_param; i < p_param + p_param - 1; ++i) { result = 0; - for (j = i - p + 1; j < p; ++j) { + for (j = i - p_param + 1; j < p_param; ++j) { result += (small) (f[j] * g[i - j]); } fg[i] = F3_freeze(result); } - for (i = p + p - 2; i >= p; --i) { - fg[i - p] = F3_freeze(fg[i - p] + fg[i]); - fg[i - p + 1] = F3_freeze(fg[i - p + 1] + fg[i]); + for (i = p_param + p_param - 2; i >= p_param; --i) { + fg[i - p_param] = F3_freeze(fg[i - p_param] + fg[i]); + fg[i - p_param + 1] = F3_freeze(fg[i - p_param + 1] + fg[i]); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { h[i] = fg[i]; } return 0; diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c index 54d3503a00..19ab1ddd3f 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c @@ -18,41 +18,41 @@ static Fq Fq_freeze(int32 x) { } int PQCLEAN_SNTRUP761_CLEAN_crypto_core_multsntrup761(unsigned char *outbytes, const unsigned char *inbytes, const unsigned char *kbytes) { - Fq f[p]; - small g[p]; - Fq fg[p + p - 1]; + Fq f[p_param]; + small g[p_param]; + Fq fg[p_param + p_param - 1]; int32 result; int i, j; crypto_decode_pxint16(f, inbytes); - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { f[i] = Fq_freeze(f[i]); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { small gi = (small) kbytes[i]; small gi0 = gi & 1; g[i] = (small) (gi0 - (gi & (gi0 << 1))); } - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { result = 0; for (j = 0; j <= i; ++j) { result += f[j] * (int32)g[i - j]; } fg[i] = Fq_freeze(result); } - for (i = p; i < p + p - 1; ++i) { + for (i = p_param; i < p_param + p_param - 1; ++i) { result = 0; - for (j = i - p + 1; j < p; ++j) { + for (j = i - p_param + 1; j < p_param; ++j) { result += f[j] * (int32)g[i - j]; } fg[i] = Fq_freeze(result); } - for (i = p + p - 2; i >= p; --i) { - fg[i - p] = Fq_freeze(fg[i - p] + fg[i]); - fg[i - p + 1] = Fq_freeze(fg[i - p + 1] + fg[i]); + for (i = p_param + p_param - 2; i >= p_param; --i) { + fg[i - p_param] = Fq_freeze(fg[i - p_param] + fg[i]); + fg[i - p_param + 1] = Fq_freeze(fg[i - p_param + 1] + fg[i]); } crypto_encode_pxint16(outbytes, fg); diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c index 3809abf054..a2e43b1194 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c @@ -13,7 +13,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_weightsntrup761(unsigned char *outbytes, int16 weight = 0; int i; - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { weight += in[i] & 1; } PQCLEAN_SNTRUP761_CLEAN_crypto_encode_int16(outbytes, &weight); diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c index 01dc9ddd62..68db2dd71f 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c @@ -24,7 +24,7 @@ static int Weightw_mask(const small *r) { int weight = 0; int i; - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { weight += r[i] & 1; } return int16_nonzero_mask((int16) (weight - w)); @@ -41,7 +41,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_wforcesntrup761(unsigned char *outbytes, for (i = 0; i < w; ++i) { out[i] = (small) (((in[i] ^ 1) & ~mask) ^ 1); } - for (i = w; i < p; ++i) { + for (i = w; i < p_param; ++i) { out[i] = (small) (in[i] & ~mask); } return 0; diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c b/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c index 4c1995c7b3..371597427e 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c @@ -78,37 +78,37 @@ static void Short_random(small *out) { uint32 L[ppadsort]; int i; - randombytes((unsigned char *) L, 4 * p); + randombytes((unsigned char *) L, 4 * p_param); crypto_decode_pxint32(L, (unsigned char *) L); for (i = 0; i < w; ++i) { L[i] = L[i] & (uint32) - 2; } - for (i = w; i < p; ++i) { + for (i = w; i < p_param; ++i) { L[i] = (L[i] & (uint32) - 3) | 1; } - for (i = p; i < ppadsort; ++i) { + for (i = p_param; i < ppadsort; ++i) { L[i] = 0xffffffff; } PQCLEAN_SNTRUP761_CLEAN_crypto_sort_uint32(L, ppadsort); - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { out[i] = (small) ((L[i] & 3) - 1); } } static void Small_random(small *out) { - uint32 L[p]; + uint32 L[p_param]; int i; randombytes((unsigned char *) L, sizeof L); crypto_decode_pxint32(L, (unsigned char *) L); - for (i = 0; i < p; ++i) { + for (i = 0; i < p_param; ++i) { out[i] = (small) ((((L[i] & 0x3fffffff) * 3) >> 30) - 1); } } /* ----- Streamlined NTRU Prime */ -typedef small Inputs[p]; /* passed by reference */ +typedef small Inputs[p_param]; /* passed by reference */ #define Ciphertexts_bytes Rounded_bytes #define SecretKeys_bytes (2*Small_bytes) #define PublicKeys_bytes Rq_bytes @@ -119,7 +119,7 @@ typedef small Inputs[p]; /* passed by reference */ /* also set x[0]=2, and x[1:1+Hash_bytes] = Hash3(r_enc) */ /* also overwrite x[1+Hash_bytes:1+2*Hash_bytes] */ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) { - Fq h[p]; + Fq h[p_param]; int i; Small_encode(r_enc + 1, r); @@ -137,14 +137,14 @@ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const int PQCLEAN_SNTRUP761_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { - small g[p]; + small g[p_param]; for (;;) { Small_random(g); { - small v[p + 1]; + small v[p_param + 1]; small vp; crypto_core_inv3((unsigned char *) v, (const unsigned char *) g); - vp = v[p]; + vp = v[p_param]; crypto_declassify(&vp, sizeof vp); if (vp == 0) { Small_encode(sk + Small_bytes, v); @@ -153,11 +153,11 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { } } { - small f[p]; + small f[p_param]; Short_random(f); Small_encode(sk, f); { - Fq h[p + 1]; + Fq h[p_param + 1]; Rq_recip3(h, f); /* always works */ Rq_mult_small(h, g); Rq_encode(pk, h); @@ -212,17 +212,17 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_kem_dec(uint8_t *k, const uint8_t *c, const u int mask, i; Inputs r; { - Fq d[p]; + Fq d[p_param]; Rounded_decode(d, c); { - small f[p]; + small f[p_param]; Small_decode(f, sk); Rq_mult_small(d, f); Rq_mult3(d, d); } { - small e[p]; - small v[p]; + small e[p_param]; + small v[p_param]; R3_fromRq(e, d); Small_decode(v, sk + Small_bytes); R3_mult(r, e, v); diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/params.h b/src/kem/ntruprime/pqclean_sntrup761_clean/params.h index 4b8adb499c..f8125e52e8 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/params.h +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/params.h @@ -28,7 +28,7 @@ #define q27 29235 /* closest integer to 2^27/q */ #define q18 57 /* closest integer to 2^18/q */ #define q14 4 /* closest integer to 2^14/q */ -#define p 761 +#define p_param 761 #define q 4591 #define w 286 From 01de31f0344d9c764355ed876714644a6c7c554e Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 08:59:14 +0000 Subject: [PATCH 09/34] Add openssl version check to fix build error Signed-off-by: Songling Han --- src/common/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/common.h b/src/common/common.h index 36c8752599..7698d5c3a8 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -27,7 +27,7 @@ extern "C" { * using OpenSSL functions when OQS_USE_OPENSSL is defined, and * standard C library functions otherwise. */ -#if defined(OQS_USE_OPENSSL) +#if defined(OQS_USE_OPENSSL) && defined(OPENSSL_VERSION_NUMBER) #include /** From a955fe58ad37b97131e01026137a9dc684d82d54 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 09:06:47 +0000 Subject: [PATCH 10/34] fix implicit conversion for diff Signed-off-by: Songling Han --- src/common/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/common.c b/src/common/common.c index 81f1e80cd9..44280c1416 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -327,7 +327,7 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { return NULL; } // Store the difference so that the free function can use it - ptr[-1] = diff; + 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); From 7dfc5578531e651271e5ff306f290b01cabcc57a Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 09:36:15 +0000 Subject: [PATCH 11/34] fix build for tests Signed-off-by: Songling Han --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index eb297a8047..8b6851c474 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,7 +30,7 @@ if(NOT WIN32) endif() # List oqs-internal after oqs so that oqs has linking precedence. -set(TEST_DEPS oqs oqs-internal ${LIBM}) +set(TEST_DEPS oqs oqs-internal ${LIBM} OpenSSL::Crypto) if(OQS_USE_PTHREADS) set(TEST_DEPS ${TEST_DEPS} Threads::Threads) endif() From 30ca6bd5a133c11ef3380323b9648919c97c6ae2 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 09:49:56 +0000 Subject: [PATCH 12/34] Fix build for tests Signed-off-by: Songling Han --- src/common/common.h | 2 +- tests/CMakeLists.txt | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index 7698d5c3a8..813e0a13a9 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -28,7 +28,7 @@ extern "C" { * standard C library functions otherwise. */ #if defined(OQS_USE_OPENSSL) && defined(OPENSSL_VERSION_NUMBER) -#include +#include /** * Allocates memory of a given size. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8b6851c474..97761b9d83 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,7 +30,14 @@ if(NOT WIN32) endif() # List oqs-internal after oqs so that oqs has linking precedence. -set(TEST_DEPS oqs oqs-internal ${LIBM} OpenSSL::Crypto) +find_package(OpenSSL REQUIRED) + +if(NOT OpenSSL_FOUND) + set(TEST_DEPS oqs oqs-internal ${LIBM}) +else() + set(TEST_DEPS oqs oqs-internal ${LIBM} OpenSSL::Crypto) +endif() + if(OQS_USE_PTHREADS) set(TEST_DEPS ${TEST_DEPS} Threads::Threads) endif() From 9951317430261dd9ba859dd3395962a6e4a12a11 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 11:05:34 +0000 Subject: [PATCH 13/34] Fix build for OQS_DLOPEN_OPENSSL Signed-off-by: Songling Han --- src/common/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/common.h b/src/common/common.h index 813e0a13a9..65d24e8b50 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -27,7 +27,7 @@ extern "C" { * using OpenSSL functions when OQS_USE_OPENSSL is defined, and * standard C library functions otherwise. */ -#if defined(OQS_USE_OPENSSL) && defined(OPENSSL_VERSION_NUMBER) +#if (defined(OQS_USE_OPENSSL) || defined(OQS_DLOPEN_OPENSSL)) && defined(OPENSSL_VERSION_NUMBER) #include /** From 51f263bc1dc80c0f4843a3fb9642ddd988f2d076 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 21 Sep 2024 16:23:48 +0000 Subject: [PATCH 14/34] Fix build failure [full tests] Signed-off-by: Songling Han --- tests/CMakeLists.txt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 97761b9d83..cc5f373b5f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,12 +30,11 @@ if(NOT WIN32) endif() # List oqs-internal after oqs so that oqs has linking precedence. -find_package(OpenSSL REQUIRED) - -if(NOT OpenSSL_FOUND) - set(TEST_DEPS oqs oqs-internal ${LIBM}) -else() +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) From b2684457481ec0f4ad2420e6d25b558d930015e3 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Mon, 23 Sep 2024 14:39:32 -0700 Subject: [PATCH 15/34] remove OQS_MEM_free Signed-off-by: Songling Han --- src/common/common.c | 8 ++++++-- src/common/common.h | 12 ++---------- src/common/sha2/sha2_c.c | 8 ++++---- src/common/sha3/ossl_sha3.c | 8 ++++---- src/common/sha3/ossl_sha3x4.c | 8 ++++---- src/sig_stfl/lms/external/hss_alloc.c | 10 +++++----- src/sig_stfl/lms/external/hss_generate.c | 4 ++-- src/sig_stfl/lms/external/hss_keygen.c | 4 ++-- src/sig_stfl/lms/external/hss_thread_pthread.c | 10 +++++----- 9 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/common/common.c b/src/common/common.c index 44280c1416..8557f9bda1 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -299,12 +299,16 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - OQS_MEM_free(ptr); // IGNORE free-check + OQS_MEM_insecure_free(ptr); // IGNORE free-check } } OQS_API void OQS_MEM_insecure_free(void *ptr) { - OQS_MEM_free(ptr); // IGNORE free-check +#if (defined(OQS_USE_OPENSSL) || defined(OQS_DLOPEN_OPENSSL)) && defined(OPENSSL_VERSION_NUMBER) + OPENSSL_free(ptr); +#else + free(ptr); +#endif } void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { diff --git a/src/common/common.h b/src/common/common.h index 65d24e8b50..5bc25ee7f1 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -36,11 +36,7 @@ extern "C" { * @return A pointer to the allocated memory. */ #define OQS_MEM_malloc(size) OPENSSL_malloc(size) -/** - * Frees the allocated memory. - * @param ptr The pointer to the memory to be freed. - */ -#define OQS_MEM_free(ptr) OPENSSL_free(ptr) + /** * Allocates memory for an array of elements of a given size. * @param num_elements The number of elements to allocate. @@ -61,11 +57,7 @@ extern "C" { * @return A pointer to the allocated memory. */ #define OQS_MEM_malloc(size) malloc(size) -/** - * Frees the allocated memory. - * @param ptr The pointer to the memory to be freed. - */ -#define OQS_MEM_free(ptr) free(ptr) + /** * Allocates memory for an array of elements of a given size. * @param num_elements The number of elements to allocate. diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index 5ead4c1ee7..d35d3e6496 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -588,22 +588,22 @@ void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *state /* Destroy the hash state. */ void oqs_sha2_sha224_inc_ctx_release_c(sha224ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha256_inc_ctx_release_c(sha256ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha384_inc_ctx_release_c(sha384ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); // IGNORE free-check } /* Destroy the hash state. */ void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) { - OQS_MEM_free(state->ctx); // IGNORE free-check + OQS_MEM_insecure_free(state->ctx); // IGNORE free-check } void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) { diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index 2ac3e98cb5..99d6d76ba3 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -201,7 +201,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); - OQS_MEM_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 +211,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); - OQS_MEM_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) { @@ -275,7 +275,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); - OQS_MEM_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 +285,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); - OQS_MEM_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 eb14a9f1fc..a1a69949a7 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -94,7 +94,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); - OQS_MEM_free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -117,7 +117,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); - OQS_MEM_free(s); // IGNORE free-check + OQS_MEM_insecure_free(s); // IGNORE free-check } static void SHA3_shake128_x4_inc_ctx_reset(OQS_SHA3_shake128_x4_inc_ctx *state) { @@ -215,7 +215,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); - OQS_MEM_free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); // IGNORE free-check } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -238,7 +238,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); - OQS_MEM_free(s); // IGNORE free-check + OQS_MEM_insecure_free(s); // IGNORE free-check } static void SHA3_shake256_x4_inc_ctx_reset(OQS_SHA3_shake256_x4_inc_ctx *state) { diff --git a/src/sig_stfl/lms/external/hss_alloc.c b/src/sig_stfl/lms/external/hss_alloc.c index c5043f8a8e..70b5ca0a68 100644 --- a/src/sig_stfl/lms/external/hss_alloc.c +++ b/src/sig_stfl/lms/external/hss_alloc.c @@ -542,15 +542,15 @@ void hss_free_working_key(struct hss_working_key *w) { unsigned j, k; for (j=0; jsubtree[j][k]); // IGNORE free-check + OQS_MEM_insecure_free(tree->subtree[j][k]); // IGNORE free-check hss_zeroize( tree, sizeof *tree ); /* We have seeds here */ } - OQS_MEM_free(tree); // IGNORE free-check + OQS_MEM_insecure_free(tree); // IGNORE free-check } for (i=0; isigned_pk[i]); // IGNORE free-check + OQS_MEM_insecure_free(w->signed_pk[i]); // IGNORE free-check } - OQS_MEM_free(w->stack); // IGNORE free-check + OQS_MEM_insecure_free(w->stack); // IGNORE free-check hss_zeroize( w, sizeof *w ); /* We have secret information here */ - OQS_MEM_free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); // IGNORE free-check } diff --git a/src/sig_stfl/lms/external/hss_generate.c b/src/sig_stfl/lms/external/hss_generate.c index f3d3f0212d..44171abdc4 100644 --- a/src/sig_stfl/lms/external/hss_generate.c +++ b/src/sig_stfl/lms/external/hss_generate.c @@ -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++) { - OQS_MEM_free( order[i].sub ); // IGNORE free-check + OQS_MEM_insecure_free( order[i].sub ); // IGNORE free-check } #endif info->error_code = got_error; @@ -831,7 +831,7 @@ bool hss_generate_working_key( hash_size, tree->h, I); } - OQS_MEM_free( sub ); // IGNORE free-check + OQS_MEM_insecure_free( sub ); // IGNORE free-check 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 5d1eca28da..2f1482a298 100644 --- a/src/sig_stfl/lms/external/hss_keygen.c +++ b/src/sig_stfl/lms/external/hss_keygen.c @@ -278,7 +278,7 @@ bool hss_generate_private_key( } else { hss_zeroize( context, PRIVATE_KEY_LEN ); } - OQS_MEM_free(temp_buffer); // IGNORE free-check + OQS_MEM_insecure_free(temp_buffer); // IGNORE free-check return false; } @@ -355,7 +355,7 @@ bool hss_generate_private_key( /* Hey, what do you know -- it all worked! */ hss_zeroize( private_key, sizeof private_key ); /* Zeroize local copy of */ /* the private key */ - OQS_MEM_free(temp_buffer); // IGNORE free-check + OQS_MEM_insecure_free(temp_buffer); // IGNORE free-check return true; } #endif diff --git a/src/sig_stfl/lms/external/hss_thread_pthread.c b/src/sig_stfl/lms/external/hss_thread_pthread.c index 7ffbd2a40f..741bae0c36 100644 --- a/src/sig_stfl/lms/external/hss_thread_pthread.c +++ b/src/sig_stfl/lms/external/hss_thread_pthread.c @@ -91,13 +91,13 @@ struct thread_collection *hss_thread_init(int num_thread) { col->num_thread = num_thread; if (0 != pthread_mutex_init( &col->lock, 0 )) { - OQS_MEM_free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); // IGNORE free-check return 0; } if (0 != pthread_mutex_init( &col->write_lock, 0 )) { pthread_mutex_destroy( &col->lock ); - OQS_MEM_free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); // IGNORE free-check return 0; } @@ -126,7 +126,7 @@ static void *worker_thread( void *arg ) { (w->function)(w->x.detail, col); /* Ok, we did that */ - OQS_MEM_free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); // IGNORE free-check /* Check if there's anything else to do */ pthread_mutex_lock( &col->lock ); @@ -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 ); - OQS_MEM_free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); // IGNORE free-check 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 ); - OQS_MEM_free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); // IGNORE free-check } void hss_thread_before_write(struct thread_collection *col) { From c4b647ed5ac38f3c1223096366ad84ea4db9b56e Mon Sep 17 00:00:00 2001 From: Songling Han Date: Mon, 23 Sep 2024 15:05:07 -0700 Subject: [PATCH 16/34] remove OQS_MEM_free Signed-off-by: Songling Han --- tests/test_hash.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/test_hash.c b/tests/test_hash.c index d374a9caa1..49d40e1dc9 100644 --- a/tests/test_hash.c +++ b/tests/test_hash.c @@ -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"); - OQS_MEM_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"); - OQS_MEM_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"); - OQS_MEM_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"); - OQS_MEM_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"); - OQS_MEM_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"); - OQS_MEM_free(msg); + OQS_MEM_insecure_free(msg); return -5; } //Test inc API print_hex(output, 32); - OQS_MEM_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"); - OQS_MEM_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"); - OQS_MEM_free(msg); + OQS_MEM_insecure_free(msg); return -3; } print_hex(output, 48); - OQS_MEM_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"); - OQS_MEM_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"); - OQS_MEM_free(msg); + OQS_MEM_insecure_free(msg); return -3; } print_hex(output, 64); - OQS_MEM_free(msg); + OQS_MEM_insecure_free(msg); return 0; } @@ -257,8 +257,8 @@ static int do_arbitrary_hash(void (*hash)(uint8_t *, const uint8_t *, size_t), s uint8_t *output = OQS_MEM_malloc(hash_len); hash(output, msg, msg_len); print_hex(output, hash_len); - OQS_MEM_free(output); - OQS_MEM_free(msg); + OQS_MEM_insecure_free(output); + OQS_MEM_insecure_free(msg); return 0; } From f47e341bc5dc557608d08440f64b00012e70a1c3 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 24 Sep 2024 03:45:10 +0000 Subject: [PATCH 17/34] Add allocator check in tests/test_code_conventions.py Signed-off-by: Songling Han --- src/common/common.c | 37 ++++++++++--------- src/common/sha2/sha2_c.c | 8 ++-- src/common/sha3/ossl_sha3x4.c | 8 ++-- src/sig_stfl/lms/external/hss_alloc.c | 10 ++--- src/sig_stfl/lms/external/hss_generate.c | 4 +- src/sig_stfl/lms/external/hss_keygen.c | 4 +- .../lms/external/hss_thread_pthread.c | 10 ++--- tests/test_code_conventions.py | 36 ++++++++++++------ 8 files changed, 65 insertions(+), 52 deletions(-) diff --git a/src/common/common.c b/src/common/common.c index 8557f9bda1..0ea95fb95b 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -257,6 +257,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) @@ -267,7 +270,7 @@ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { explicit_memset(ptr, 0, len); #elif defined(__STDC_LIB_EXT1__) || defined(OQS_HAVE_MEMSET_S) if (0U < len && memset_s(ptr, (rsize_t)len, 0, (rsize_t)len) != 0) { - abort(); + return; //abort(); } #else typedef void *(*memset_t)(void *, int, size_t); @@ -275,12 +278,11 @@ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { memset_func(ptr, 0, len); #endif } - void *OQS_MEM_checked_malloc(size_t len) { void *ptr = OQS_MEM_malloc(len); if (ptr == NULL) { fprintf(stderr, "Memory allocation failed\n"); - abort(); + return NULL; //abort(); } return ptr; @@ -290,7 +292,7 @@ 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 NULL; //abort(); } return ptr; @@ -299,7 +301,7 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - OQS_MEM_insecure_free(ptr); // IGNORE free-check + OQS_MEM_insecure_free(ptr); } } @@ -372,7 +374,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; } @@ -382,7 +384,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; } @@ -395,24 +397,23 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) { } void OQS_MEM_aligned_free(void *ptr) { + if (ptr == NULL) { + return; + } #if defined(OQS_USE_OPENSSL) // Use OpenSSL's free function - if (ptr) { - uint8_t *u8ptr = ptr; - OPENSSL_free(u8ptr - u8ptr[-1]); - } + uint8_t *u8ptr = ptr; + OPENSSL_free(u8ptr - u8ptr[-1]); #elif defined(OQS_HAVE_ALIGNED_ALLOC) || defined(OQS_HAVE_POSIX_MEMALIGN) || defined(OQS_HAVE_MEMALIGN) - free(ptr); // IGNORE free-check + free(ptr); // IGNORE 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/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index d35d3e6496..a8751a375f 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -588,22 +588,22 @@ void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *state /* Destroy the hash state. */ void oqs_sha2_sha224_inc_ctx_release_c(sha224ctx *state) { - OQS_MEM_insecure_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) { - OQS_MEM_insecure_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) { - OQS_MEM_insecure_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) { - OQS_MEM_insecure_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) { diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index a1a69949a7..a5cfeb5242 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -94,7 +94,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); - OQS_MEM_insecure_free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -117,7 +117,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); - OQS_MEM_insecure_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) { @@ -215,7 +215,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); - OQS_MEM_insecure_free(tmp); // IGNORE free-check + OQS_MEM_insecure_free(tmp); } OSSL_FUNC(EVP_MD_CTX_free)(clone); s->n_out += outlen; @@ -238,7 +238,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); - OQS_MEM_insecure_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/sig_stfl/lms/external/hss_alloc.c b/src/sig_stfl/lms/external/hss_alloc.c index 70b5ca0a68..00c0d628b5 100644 --- a/src/sig_stfl/lms/external/hss_alloc.c +++ b/src/sig_stfl/lms/external/hss_alloc.c @@ -542,15 +542,15 @@ void hss_free_working_key(struct hss_working_key *w) { unsigned j, k; for (j=0; jsubtree[j][k]); // IGNORE free-check + OQS_MEM_insecure_free(tree->subtree[j][k]); hss_zeroize( tree, sizeof *tree ); /* We have seeds here */ } - OQS_MEM_insecure_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]); } - OQS_MEM_insecure_free(w->stack); // IGNORE free-check + OQS_MEM_insecure_free(w->stack); hss_zeroize( w, sizeof *w ); /* We have secret information here */ - OQS_MEM_insecure_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 44171abdc4..359706ad6f 100644 --- a/src/sig_stfl/lms/external/hss_generate.c +++ b/src/sig_stfl/lms/external/hss_generate.c @@ -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++) { - OQS_MEM_insecure_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); } - OQS_MEM_insecure_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 2f1482a298..6dc0d02b78 100644 --- a/src/sig_stfl/lms/external/hss_keygen.c +++ b/src/sig_stfl/lms/external/hss_keygen.c @@ -278,7 +278,7 @@ bool hss_generate_private_key( } else { hss_zeroize( context, PRIVATE_KEY_LEN ); } - OQS_MEM_insecure_free(temp_buffer); // IGNORE free-check + OQS_MEM_insecure_free(temp_buffer); return false; } @@ -355,7 +355,7 @@ bool hss_generate_private_key( /* Hey, what do you know -- it all worked! */ hss_zeroize( private_key, sizeof private_key ); /* Zeroize local copy of */ /* the private key */ - OQS_MEM_insecure_free(temp_buffer); // IGNORE free-check + OQS_MEM_insecure_free(temp_buffer); return true; } #endif diff --git a/src/sig_stfl/lms/external/hss_thread_pthread.c b/src/sig_stfl/lms/external/hss_thread_pthread.c index 741bae0c36..b5df4a6054 100644 --- a/src/sig_stfl/lms/external/hss_thread_pthread.c +++ b/src/sig_stfl/lms/external/hss_thread_pthread.c @@ -91,13 +91,13 @@ struct thread_collection *hss_thread_init(int num_thread) { col->num_thread = num_thread; if (0 != pthread_mutex_init( &col->lock, 0 )) { - OQS_MEM_insecure_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 ); - OQS_MEM_insecure_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 */ - OQS_MEM_insecure_free(w); // IGNORE free-check + OQS_MEM_insecure_free(w); /* Check if there's anything else to do */ pthread_mutex_lock( &col->lock ); @@ -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 ); - OQS_MEM_insecure_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 ); - OQS_MEM_insecure_free(col); // IGNORE free-check + OQS_MEM_insecure_free(col); } void hss_thread_before_write(struct thread_collection *col) { diff --git a/tests/test_code_conventions.py b/tests/test_code_conventions.py index ed88f483ab..081bf8dd9c 100644 --- a/tests/test_code_conventions.py +++ b/tests/test_code_conventions.py @@ -48,26 +48,38 @@ 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(): +def test_memory_functions(): c_files = [] for path, _, files in os.walk('src'): - c_files += [os.path.join(path,f) for f in files if f[-2:] == '.c'] + c_files += [os.path.join(path, f) for f in files if f.endswith('.c')] + + memory_functions = ['free', 'malloc', 'calloc', 'realloc', 'strdup'] okay = True + for fn in c_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() + for no, line in enumerate(lines, 1): + # Skip comments + if line.strip().startswith('//') or line.strip().startswith('/*'): continue - if 'IGNORE free-check' in line: + # Check if we're inside a multi-line comment + if '/*' in content[:content.find(line)] and '*/' not in content[:content.find(line)]: 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) From 99c198a98a782fe3ceb93c405d99cd4406288be9 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 24 Sep 2024 05:18:41 +0000 Subject: [PATCH 18/34] format code Signed-off-by: Songling Han --- src/common/common.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/common.c b/src/common/common.c index 0ea95fb95b..6521fd6799 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -301,15 +301,15 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) { OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { OQS_MEM_cleanse(ptr, len); - OQS_MEM_insecure_free(ptr); + OQS_MEM_insecure_free(ptr); } } OQS_API void OQS_MEM_insecure_free(void *ptr) { #if (defined(OQS_USE_OPENSSL) || defined(OQS_DLOPEN_OPENSSL)) && defined(OPENSSL_VERSION_NUMBER) - OPENSSL_free(ptr); + OPENSSL_free(ptr); #else - free(ptr); + free(ptr); #endif } From 374c1d454d50a78b1ba1cf1c0d251af3fc31d396 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Tue, 24 Sep 2024 07:42:28 +0000 Subject: [PATCH 19/34] Add IGNORE memory-check Signed-off-by: Songling Han --- src/common/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/common.c b/src/common/common.c index 6521fd6799..30cf674815 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -309,7 +309,7 @@ OQS_API void OQS_MEM_insecure_free(void *ptr) { #if (defined(OQS_USE_OPENSSL) || defined(OQS_DLOPEN_OPENSSL)) && defined(OPENSSL_VERSION_NUMBER) OPENSSL_free(ptr); #else - free(ptr); + free(ptr); // IGNORE memory-check #endif } From 34f9b1bb9b40d107a1534181eff9b303a55f93cd Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 27 Sep 2024 22:17:56 +0000 Subject: [PATCH 20/34] revert back to abort() in OQS_MEM_cleanse Signed-off-by: Songling Han --- src/common/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/common.c b/src/common/common.c index 30cf674815..2101ca3e8d 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -270,7 +270,7 @@ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { explicit_memset(ptr, 0, len); #elif defined(__STDC_LIB_EXT1__) || defined(OQS_HAVE_MEMSET_S) if (0U < len && memset_s(ptr, (rsize_t)len, 0, (rsize_t)len) != 0) { - return; //abort(); + abort(); } #else typedef void *(*memset_t)(void *, int, size_t); From c01c37634f8ee8e27170ddba95dfc7f4f322df93 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 3 Oct 2024 02:03:10 +0000 Subject: [PATCH 21/34] Delect checked allocation functions Signed-off-by: Songling Han --- src/common/common.c | 19 ------------- src/common/common.h | 53 ----------------------------------- src/common/sha2/sha2_armv8.c | 4 +-- src/common/sha2/sha2_c.c | 20 ++++++------- src/common/sha3/ossl_sha3.c | 4 +-- src/common/sha3/ossl_sha3x4.c | 4 +-- src/common/sha3/xkcp_sha3.c | 33 +++++++++++++++------- src/common/sha3/xkcp_sha3x4.c | 11 ++++++-- 8 files changed, 47 insertions(+), 101 deletions(-) diff --git a/src/common/common.c b/src/common/common.c index 2101ca3e8d..7f89a188c9 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -278,25 +278,6 @@ OQS_API void OQS_MEM_cleanse(void *ptr, size_t len) { memset_func(ptr, 0, len); #endif } -void *OQS_MEM_checked_malloc(size_t len) { - void *ptr = OQS_MEM_malloc(len); - if (ptr == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - return NULL; //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"); - return NULL; //abort(); - } - - return ptr; -} OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) { if (ptr != NULL) { diff --git a/src/common/common.h b/src/common/common.h index 5bc25ee7f1..887698f818 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -246,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..e98434d08e 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -180,7 +180,7 @@ 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); memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { @@ -254,7 +254,7 @@ 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); 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 a8751a375f..3df7a1c2a9 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -502,7 +502,7 @@ 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); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_224[i]; @@ -516,7 +516,7 @@ 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); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_256[i]; @@ -529,7 +529,7 @@ 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); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_384[i]; @@ -542,7 +542,7 @@ 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); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_512[i]; @@ -555,7 +555,7 @@ 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); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -563,7 +563,7 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state } 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); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -571,7 +571,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state } 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); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -579,7 +579,7 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state } 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); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -614,7 +614,7 @@ 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); memcpy(tmp_in, state->data, state->data_len); memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len); @@ -691,7 +691,7 @@ 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); memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index 99d6d76ba3..9a64acf1c5 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -198,7 +198,7 @@ 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); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check @@ -272,7 +272,7 @@ 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); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index a5cfeb5242..a6d0cc8cdf 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -81,7 +81,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, out3, outlen); } else { - uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); + uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); 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); @@ -202,7 +202,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, out3, outlen); } else { - uint8_t *tmp = OQS_MEM_checked_malloc(s->n_out + outlen); + uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); 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); diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 196652d85d..400f4191a5 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -199,8 +199,10 @@ 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); - keccak_inc_reset((uint64_t *)state->ctx); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + if (state->ctx != NULL) { + keccak_inc_reset((uint64_t *)state->ctx); + } } static void SHA3_sha3_256_inc_absorb(OQS_SHA3_sha3_256_inc_ctx *state, const uint8_t *input, size_t inlen) { @@ -235,10 +237,11 @@ 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); - keccak_inc_reset((uint64_t *)state->ctx); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + if (state->ctx != NULL) { + 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,8 +274,10 @@ 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); - keccak_inc_reset((uint64_t *)state->ctx); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + if (state->ctx != NULL) { + keccak_inc_reset((uint64_t *)state->ctx); + } } static void SHA3_sha3_512_inc_absorb(OQS_SHA3_sha3_512_inc_ctx *state, const uint8_t *input, size_t inlen) { @@ -310,7 +315,13 @@ 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); + if (state == NULL) { + return; + } + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + if (state->ctx == NULL) { + return; + } keccak_inc_reset((uint64_t *)state->ctx); } @@ -352,8 +363,10 @@ 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); - keccak_inc_reset((uint64_t *)state->ctx); + state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); + if (state->ctx != NULL) { + keccak_inc_reset((uint64_t *)state->ctx); + } } static void SHA3_shake256_inc_absorb(OQS_SHA3_shake256_inc_ctx *state, const uint8_t *input, size_t inlen) { diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index bd441a01ff..e493248067 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -167,10 +167,12 @@ 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); + if (state->ctx == NULL) { + return; + } 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 +211,10 @@ 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); + if (state->ctx == NULL) { + return; + } keccak_x4_inc_reset((uint64_t *)state->ctx); } From 320c6e11d61f4c5c481b687f20f3999df411d3bb Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 3 Oct 2024 02:21:14 +0000 Subject: [PATCH 22/34] Revert back p_param to p for sntrup Signed-off-by: Songling Han --- .../crypto_core_invsntrup761.c | 26 +++++++------- .../crypto_core_weightsntrup761.c | 4 +-- .../crypto_core_wforcesntrup761.c | 2 +- .../ntruprime/pqclean_sntrup761_avx2/kem.c | 34 +++++++++--------- .../ntruprime/pqclean_sntrup761_avx2/params.h | 2 +- .../crypto_core_inv3sntrup761.c | 36 +++++++++---------- .../crypto_core_invsntrup761.c | 36 +++++++++---------- .../crypto_core_mult3sntrup761.c | 24 ++++++------- .../crypto_core_multsntrup761.c | 22 ++++++------ .../crypto_core_weightsntrup761.c | 2 +- .../crypto_core_wforcesntrup761.c | 4 +-- .../ntruprime/pqclean_sntrup761_clean/kem.c | 34 +++++++++--------- .../pqclean_sntrup761_clean/params.h | 2 +- 13 files changed, 114 insertions(+), 114 deletions(-) diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c index 712fb77564..e27bedfb7a 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_invsntrup761.c @@ -129,7 +129,7 @@ static inline void vectormodq_xswapeliminate(Fq *f, Fq *g, int len, const Fq f0, int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, const unsigned char *inbytes) { small *in = (void *) inbytes; int loop; - Fq out[p_param], f[ppad], g[ppad], v[ppad], r[ppad]; + Fq out[p], f[ppad], g[ppad], v[ppad], r[ppad]; Fq f0, g0; Fq scale; int i; @@ -142,14 +142,14 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con f[i] = 0; } f[0] = 1; - f[p_param - 1] = -1; - f[p_param] = -1; + f[p - 1] = -1; + f[p] = -1; /* generalization: initialize f to reversal of any deg-p polynomial m */ - for (i = 0; i < p_param; ++i) { - g[i] = in[p_param - 1 - i]; + for (i = 0; i < p; ++i) { + g[i] = in[p - 1 - i]; } - for (i = p_param; i < ppad; ++i) { + for (i = p; i < ppad; ++i) { g[i] = 0; } @@ -162,7 +162,7 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con v[i] = 0; } - for (loop = 0; loop < p_param; ++loop) { + for (loop = 0; loop < p; ++loop) { g0 = Fq_freeze(g[0]); f0 = f[0]; if (q > 5167) { @@ -180,11 +180,11 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con f[0] = f0; - vectormodq_swapeliminate(f + 1, g + 1, p_param, f0, g0, swap); + vectormodq_swapeliminate(f + 1, g + 1, p, f0, g0, swap); vectormodq_xswapeliminate(v, r, loop + 1, f0, g0, swap); } - for (loop = p_param - 1; loop > 0; --loop) { + for (loop = p - 1; loop > 0; --loop) { g0 = Fq_freeze(g[0]); f0 = f[0]; if (q > 5167) { @@ -203,15 +203,15 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_invsntrup761(unsigned char *outbytes, con f[0] = f0; vectormodq_swapeliminate(f + 1, g + 1, loop, f0, g0, swap); - vectormodq_xswapeliminate(v, r, p_param, f0, g0, swap); + vectormodq_xswapeliminate(v, r, p, f0, g0, swap); } scale = Fq_recip(Fq_freeze(f[0])); - for (i = 0; i < p_param; ++i) { - out[i] = Fq_bigfreeze(scale * (int32)Fq_freeze(v[p_param - i])); + for (i = 0; i < p; ++i) { + out[i] = Fq_bigfreeze(scale * (int32)Fq_freeze(v[p - i])); } crypto_encode_pxint16(outbytes, out); - outbytes[2 * p_param] = (unsigned char) int16_nonzero_mask((int16) delta); + outbytes[2 * p] = (unsigned char) int16_nonzero_mask((int16) delta); return 0; } diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c index 1d44f4cceb..5d1b85e37a 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_weightsntrup761.c @@ -14,10 +14,10 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_weightsntrup761(unsigned char *outbytes, __m256i sum, sumhi; int16 weight; - sum = _mm256_loadu_si256((__m256i *) (in + p_param - 32)); + sum = _mm256_loadu_si256((__m256i *) (in + p - 32)); sum &= endingmask; - for (i = p_param - 32; i >= 0; i -= 32) { + for (i = p - 32; i >= 0; i -= 32) { __m256i bits = _mm256_loadu_si256((__m256i *) in); bits &= _mm256_set1_epi8(1); sum = _mm256_add_epi8(sum, bits); diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c index a43eb9f821..0dedf0af99 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/crypto_core_wforcesntrup761.c @@ -40,7 +40,7 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_core_wforcesntrup761(unsigned char *out, const out += i; } - i = p_param - w - 32; + i = p - w - 32; for (;;) { do { __m256i x = _mm256_loadu_si256((__m256i *) in); diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c b/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c index a7ff6e25bd..d0d3ffae97 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/kem.c @@ -78,37 +78,37 @@ static void Short_random(small *out) { uint32 L[ppadsort]; int i; - randombytes((unsigned char *) L, 4 * p_param); + randombytes((unsigned char *) L, 4 * p); crypto_decode_pxint32(L, (unsigned char *) L); for (i = 0; i < w; ++i) { L[i] = L[i] & (uint32) - 2; } - for (i = w; i < p_param; ++i) { + for (i = w; i < p; ++i) { L[i] = (L[i] & (uint32) - 3) | 1; } - for (i = p_param; i < ppadsort; ++i) { + for (i = p; i < ppadsort; ++i) { L[i] = 0xffffffff; } PQCLEAN_SNTRUP761_AVX2_crypto_sort_uint32(L, ppadsort); - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { out[i] = (small) ((L[i] & 3) - 1); } } static void Small_random(small *out) { - uint32 L[p_param]; + uint32 L[p]; int i; randombytes((unsigned char *) L, sizeof L); crypto_decode_pxint32(L, (unsigned char *) L); - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { out[i] = (small) ((((L[i] & 0x3fffffff) * 3) >> 30) - 1); } } /* ----- Streamlined NTRU Prime */ -typedef small Inputs[p_param]; /* passed by reference */ +typedef small Inputs[p]; /* passed by reference */ #define Ciphertexts_bytes Rounded_bytes #define SecretKeys_bytes (2*Small_bytes) #define PublicKeys_bytes Rq_bytes @@ -119,7 +119,7 @@ typedef small Inputs[p_param]; /* passed by reference */ /* also set x[0]=2, and x[1:1+Hash_bytes] = Hash3(r_enc) */ /* also overwrite x[1+Hash_bytes:1+2*Hash_bytes] */ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) { - Fq h[p_param]; + Fq h[p]; int i; Small_encode(r_enc + 1, r); @@ -137,14 +137,14 @@ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const int PQCLEAN_SNTRUP761_AVX2_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { - small g[p_param]; + small g[p]; for (;;) { Small_random(g); { - small v[p_param + 1]; + small v[p + 1]; small vp; crypto_core_inv3((unsigned char *) v, (const unsigned char *) g); - vp = v[p_param]; + vp = v[p]; crypto_declassify(&vp, sizeof vp); if (vp == 0) { Small_encode(sk + Small_bytes, v); @@ -153,11 +153,11 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { } } { - small f[p_param]; + small f[p]; Short_random(f); Small_encode(sk, f); { - Fq h[p_param + 1]; + Fq h[p + 1]; Rq_recip3(h, f); /* always works */ Rq_mult_small(h, g); Rq_encode(pk, h); @@ -212,17 +212,17 @@ int PQCLEAN_SNTRUP761_AVX2_crypto_kem_dec(uint8_t *k, const uint8_t *c, const ui int mask, i; Inputs r; { - Fq d[p_param]; + Fq d[p]; Rounded_decode(d, c); { - small f[p_param]; + small f[p]; Small_decode(f, sk); Rq_mult_small(d, f); Rq_mult3(d, d); } { - small e[p_param]; - small v[p_param]; + small e[p]; + small v[p]; R3_fromRq(e, d); Small_decode(v, sk + Small_bytes); R3_mult(r, e, v); diff --git a/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h b/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h index f924b8e422..0224bbef70 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h +++ b/src/kem/ntruprime/pqclean_sntrup761_avx2/params.h @@ -32,7 +32,7 @@ #define ppad 769 #define endingmask _mm256_set_epi8(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0) #define crypto_core_weight PQCLEAN_SNTRUP761_AVX2_crypto_core_weightsntrup761 -#define p_param 761 +#define p 761 #define q 4591 #define w 286 diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c index 208332315a..87f3750a4c 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_inv3sntrup761.c @@ -43,32 +43,32 @@ static small F3_freeze(int16 x) { int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, const unsigned char *inbytes) { small *out = (void *) outbytes; small *in = (void *) inbytes; - small f[p_param + 1], g[p_param + 1], v[p_param + 1], r[p_param + 1]; + small f[p + 1], g[p + 1], v[p + 1], r[p + 1]; int i, loop, delta; int sign, swap, t; - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { v[i] = 0; } - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { r[i] = 0; } r[0] = 1; - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { f[i] = 0; } f[0] = 1; - f[p_param - 1] = f[p_param] = -1; - for (i = 0; i < p_param; ++i) { + f[p - 1] = f[p] = -1; + for (i = 0; i < p; ++i) { small i1 = in[i] & 1; - g[p_param - 1 - i] = (small) (i1 - (in[i] & (i1 << 1))); + g[p - 1 - i] = (small) (i1 - (in[i] & (i1 << 1))); } - g[p_param] = 0; + g[p] = 0; delta = 1; - for (loop = 0; loop < 2 * p_param - 1; ++loop) { - for (i = p_param; i > 0; --i) { + for (loop = 0; loop < 2 * p - 1; ++loop) { + for (i = p; i > 0; --i) { v[i] = v[i - 1]; } v[0] = 0; @@ -78,7 +78,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, c delta ^= swap & (delta ^ -delta); delta += 1; - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { t = swap & (f[i] ^ g[i]); f[i] ^= (small) t; g[i] ^= (small) t; @@ -87,24 +87,24 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_inv3sntrup761(unsigned char *outbytes, c r[i] ^= (small) t; } - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { g[i] = F3_freeze((int16) (g[i] + sign * f[i])); } - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { r[i] = F3_freeze((int16) (r[i] + sign * v[i])); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { g[i] = g[i + 1]; } - g[p_param] = (int16) 0; + g[p] = (int16) 0; } sign = (int) f[0]; - for (i = 0; i < p_param; ++i) { - out[i] = (small) (sign * v[p_param - 1 - i]); + for (i = 0; i < p; ++i) { + out[i] = (small) (sign * v[p - 1 - i]); } - out[p_param] = (small) int16_nonzero_mask((int16) delta); + out[p] = (small) int16_nonzero_mask((int16) delta); return 0; } diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c index e86ab7ddac..f9c8a09572 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_invsntrup761.c @@ -62,33 +62,33 @@ static Fq Fq_recip(Fq a1) { /* outbytes[2*p] is 0 if recip succeeded; else -1 */ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_invsntrup761(unsigned char *outbytes, const unsigned char *inbytes) { small *in = (void *) inbytes; - Fq out[p_param], f[p_param + 1], g[p_param + 1], v[p_param + 1], r[p_param + 1]; + Fq out[p], f[p + 1], g[p + 1], v[p + 1], r[p + 1]; int i, loop, delta; int swap, t; int32 f0, g0; Fq scale; - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { v[i] = 0; } - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { r[i] = 0; } r[0] = Fq_recip(3); - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { f[i] = 0; } f[0] = 1; - f[p_param - 1] = f[p_param] = -1; - for (i = 0; i < p_param; ++i) { - g[p_param - 1 - i] = (Fq) in[i]; + f[p - 1] = f[p] = -1; + for (i = 0; i < p; ++i) { + g[p - 1 - i] = (Fq) in[i]; } - g[p_param] = 0; + g[p] = 0; delta = 1; - for (loop = 0; loop < 2 * p_param - 1; ++loop) { - for (i = p_param; i > 0; --i) { + for (loop = 0; loop < 2 * p - 1; ++loop) { + for (i = p; i > 0; --i) { v[i] = v[i - 1]; } v[0] = 0; @@ -97,7 +97,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_invsntrup761(unsigned char *outbytes, co delta ^= swap & (delta ^ -delta); delta += 1; - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { t = swap & (f[i] ^ g[i]); f[i] ^= (Fq) t; g[i] ^= (Fq) t; @@ -108,25 +108,25 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_invsntrup761(unsigned char *outbytes, co f0 = f[0]; g0 = g[0]; - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { g[i] = Fq_bigfreeze(f0 * g[i] - g0 * f[i]); } - for (i = 0; i < p_param + 1; ++i) { + for (i = 0; i < p + 1; ++i) { r[i] = Fq_bigfreeze(f0 * r[i] - g0 * v[i]); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { g[i] = g[i + 1]; } - g[p_param] = 0; + g[p] = 0; } scale = Fq_recip(f[0]); - for (i = 0; i < p_param; ++i) { - out[i] = Fq_bigfreeze(scale * (int32)v[p_param - 1 - i]); + for (i = 0; i < p; ++i) { + out[i] = Fq_bigfreeze(scale * (int32)v[p - 1 - i]); } crypto_encode_pxint16(outbytes, out); - outbytes[2 * p_param] = (unsigned char) int16_nonzero_mask((int16) delta); + outbytes[2 * p] = (unsigned char) int16_nonzero_mask((int16) delta); return 0; } diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c index 5ca7620d95..b25892b41e 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_mult3sntrup761.c @@ -13,44 +13,44 @@ static small F3_freeze(int16 x) { int PQCLEAN_SNTRUP761_CLEAN_crypto_core_mult3sntrup761(unsigned char *outbytes, const unsigned char *inbytes, const unsigned char *kbytes) { small *h = (void *) outbytes; - small f[p_param]; - small g[p_param]; - small fg[p_param + p_param - 1]; + small f[p]; + small g[p]; + small fg[p + p - 1]; int16 result; int i, j; - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { small fi = (small) inbytes[i]; small fi0 = fi & 1; f[i] = (small) (fi0 - (fi & (fi0 << 1))); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { small gi = (small) kbytes[i]; small gi0 = gi & 1; g[i] = (small) (gi0 - (gi & (gi0 << 1))); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { result = 0; for (j = 0; j <= i; ++j) { result += (small) (f[j] * g[i - j]); } fg[i] = F3_freeze(result); } - for (i = p_param; i < p_param + p_param - 1; ++i) { + for (i = p; i < p + p - 1; ++i) { result = 0; - for (j = i - p_param + 1; j < p_param; ++j) { + for (j = i - p + 1; j < p; ++j) { result += (small) (f[j] * g[i - j]); } fg[i] = F3_freeze(result); } - for (i = p_param + p_param - 2; i >= p_param; --i) { - fg[i - p_param] = F3_freeze(fg[i - p_param] + fg[i]); - fg[i - p_param + 1] = F3_freeze(fg[i - p_param + 1] + fg[i]); + for (i = p + p - 2; i >= p; --i) { + fg[i - p] = F3_freeze(fg[i - p] + fg[i]); + fg[i - p + 1] = F3_freeze(fg[i - p + 1] + fg[i]); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { h[i] = fg[i]; } return 0; diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c index 19ab1ddd3f..54d3503a00 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_multsntrup761.c @@ -18,41 +18,41 @@ static Fq Fq_freeze(int32 x) { } int PQCLEAN_SNTRUP761_CLEAN_crypto_core_multsntrup761(unsigned char *outbytes, const unsigned char *inbytes, const unsigned char *kbytes) { - Fq f[p_param]; - small g[p_param]; - Fq fg[p_param + p_param - 1]; + Fq f[p]; + small g[p]; + Fq fg[p + p - 1]; int32 result; int i, j; crypto_decode_pxint16(f, inbytes); - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { f[i] = Fq_freeze(f[i]); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { small gi = (small) kbytes[i]; small gi0 = gi & 1; g[i] = (small) (gi0 - (gi & (gi0 << 1))); } - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { result = 0; for (j = 0; j <= i; ++j) { result += f[j] * (int32)g[i - j]; } fg[i] = Fq_freeze(result); } - for (i = p_param; i < p_param + p_param - 1; ++i) { + for (i = p; i < p + p - 1; ++i) { result = 0; - for (j = i - p_param + 1; j < p_param; ++j) { + for (j = i - p + 1; j < p; ++j) { result += f[j] * (int32)g[i - j]; } fg[i] = Fq_freeze(result); } - for (i = p_param + p_param - 2; i >= p_param; --i) { - fg[i - p_param] = Fq_freeze(fg[i - p_param] + fg[i]); - fg[i - p_param + 1] = Fq_freeze(fg[i - p_param + 1] + fg[i]); + for (i = p + p - 2; i >= p; --i) { + fg[i - p] = Fq_freeze(fg[i - p] + fg[i]); + fg[i - p + 1] = Fq_freeze(fg[i - p + 1] + fg[i]); } crypto_encode_pxint16(outbytes, fg); diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c index a2e43b1194..3809abf054 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_weightsntrup761.c @@ -13,7 +13,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_weightsntrup761(unsigned char *outbytes, int16 weight = 0; int i; - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { weight += in[i] & 1; } PQCLEAN_SNTRUP761_CLEAN_crypto_encode_int16(outbytes, &weight); diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c index 68db2dd71f..01dc9ddd62 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/crypto_core_wforcesntrup761.c @@ -24,7 +24,7 @@ static int Weightw_mask(const small *r) { int weight = 0; int i; - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { weight += r[i] & 1; } return int16_nonzero_mask((int16) (weight - w)); @@ -41,7 +41,7 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_core_wforcesntrup761(unsigned char *outbytes, for (i = 0; i < w; ++i) { out[i] = (small) (((in[i] ^ 1) & ~mask) ^ 1); } - for (i = w; i < p_param; ++i) { + for (i = w; i < p; ++i) { out[i] = (small) (in[i] & ~mask); } return 0; diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c b/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c index 371597427e..4c1995c7b3 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/kem.c @@ -78,37 +78,37 @@ static void Short_random(small *out) { uint32 L[ppadsort]; int i; - randombytes((unsigned char *) L, 4 * p_param); + randombytes((unsigned char *) L, 4 * p); crypto_decode_pxint32(L, (unsigned char *) L); for (i = 0; i < w; ++i) { L[i] = L[i] & (uint32) - 2; } - for (i = w; i < p_param; ++i) { + for (i = w; i < p; ++i) { L[i] = (L[i] & (uint32) - 3) | 1; } - for (i = p_param; i < ppadsort; ++i) { + for (i = p; i < ppadsort; ++i) { L[i] = 0xffffffff; } PQCLEAN_SNTRUP761_CLEAN_crypto_sort_uint32(L, ppadsort); - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { out[i] = (small) ((L[i] & 3) - 1); } } static void Small_random(small *out) { - uint32 L[p_param]; + uint32 L[p]; int i; randombytes((unsigned char *) L, sizeof L); crypto_decode_pxint32(L, (unsigned char *) L); - for (i = 0; i < p_param; ++i) { + for (i = 0; i < p; ++i) { out[i] = (small) ((((L[i] & 0x3fffffff) * 3) >> 30) - 1); } } /* ----- Streamlined NTRU Prime */ -typedef small Inputs[p_param]; /* passed by reference */ +typedef small Inputs[p]; /* passed by reference */ #define Ciphertexts_bytes Rounded_bytes #define SecretKeys_bytes (2*Small_bytes) #define PublicKeys_bytes Rq_bytes @@ -119,7 +119,7 @@ typedef small Inputs[p_param]; /* passed by reference */ /* also set x[0]=2, and x[1:1+Hash_bytes] = Hash3(r_enc) */ /* also overwrite x[1+Hash_bytes:1+2*Hash_bytes] */ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const Inputs r, const unsigned char *pk, const unsigned char *cache) { - Fq h[p_param]; + Fq h[p]; int i; Small_encode(r_enc + 1, r); @@ -137,14 +137,14 @@ static void Hide(unsigned char *x, unsigned char *c, unsigned char *r_enc, const int PQCLEAN_SNTRUP761_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { - small g[p_param]; + small g[p]; for (;;) { Small_random(g); { - small v[p_param + 1]; + small v[p + 1]; small vp; crypto_core_inv3((unsigned char *) v, (const unsigned char *) g); - vp = v[p_param]; + vp = v[p]; crypto_declassify(&vp, sizeof vp); if (vp == 0) { Small_encode(sk + Small_bytes, v); @@ -153,11 +153,11 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { } } { - small f[p_param]; + small f[p]; Short_random(f); Small_encode(sk, f); { - Fq h[p_param + 1]; + Fq h[p + 1]; Rq_recip3(h, f); /* always works */ Rq_mult_small(h, g); Rq_encode(pk, h); @@ -212,17 +212,17 @@ int PQCLEAN_SNTRUP761_CLEAN_crypto_kem_dec(uint8_t *k, const uint8_t *c, const u int mask, i; Inputs r; { - Fq d[p_param]; + Fq d[p]; Rounded_decode(d, c); { - small f[p_param]; + small f[p]; Small_decode(f, sk); Rq_mult_small(d, f); Rq_mult3(d, d); } { - small e[p_param]; - small v[p_param]; + small e[p]; + small v[p]; R3_fromRq(e, d); Small_decode(v, sk + Small_bytes); R3_mult(r, e, v); diff --git a/src/kem/ntruprime/pqclean_sntrup761_clean/params.h b/src/kem/ntruprime/pqclean_sntrup761_clean/params.h index f8125e52e8..4b8adb499c 100644 --- a/src/kem/ntruprime/pqclean_sntrup761_clean/params.h +++ b/src/kem/ntruprime/pqclean_sntrup761_clean/params.h @@ -28,7 +28,7 @@ #define q27 29235 /* closest integer to 2^27/q */ #define q18 57 /* closest integer to 2^18/q */ #define q14 4 /* closest integer to 2^14/q */ -#define p_param 761 +#define p 761 #define q 4591 #define w 286 From 3374a39e8ab2f676f8c2c28731e613272b13cb6e Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 4 Oct 2024 22:39:33 +0000 Subject: [PATCH 23/34] Address multiple line comments case Signed-off-by: Songling Han --- src/common/common.h | 6 +++--- tests/test_code_conventions.py | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index 887698f818..b15e244a39 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -56,7 +56,7 @@ extern "C" { * @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) +#define OQS_MEM_malloc(size) malloc(size) // IGNORE memory-check /** * Allocates memory for an array of elements of a given size. @@ -64,13 +64,13 @@ extern "C" { * @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) +#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) +#define OQS_MEM_strdup(str) strdup(str) // IGNORE memory-check #endif /** diff --git a/tests/test_code_conventions.py b/tests/test_code_conventions.py index 081bf8dd9c..ed59fe14c9 100644 --- a/tests/test_code_conventions.py +++ b/tests/test_code_conventions.py @@ -49,23 +49,31 @@ def test_spdx(): assert False def test_memory_functions(): - c_files = [] + c_h_files = [] for path, _, files in os.walk('src'): - c_files += [os.path.join(path, f) for f in files if f.endswith('.c')] + c_h_files += [os.path.join(path, f) for f in files if f.endswith(('.c', '.h'))] 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: content = f.read() lines = content.splitlines() + in_multiline_comment = False for no, line in enumerate(lines, 1): - # Skip comments - if line.strip().startswith('//') or line.strip().startswith('/*'): + # Skip single-line comments + if line.strip().startswith('//'): continue - # Check if we're inside a multi-line comment - if '/*' in content[:content.find(line)] and '*/' not in content[:content.find(line)]: + # 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 + # Skip lines inside multi-line comments + if in_multiline_comment: continue for func in memory_functions: if re.search(r'\b{}\('.format(func), line) and not re.search(r'\b_{}\('.format(func), line): From 284275affc5c1f3c02bceac6011090ff4c7b3723 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 4 Oct 2024 22:52:59 +0000 Subject: [PATCH 24/34] Add allocator check for '.c', '.h', '.fragment' Signed-off-by: Songling Han --- tests/test_code_conventions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_code_conventions.py b/tests/test_code_conventions.py index ed59fe14c9..2ed437f119 100644 --- a/tests/test_code_conventions.py +++ b/tests/test_code_conventions.py @@ -51,7 +51,7 @@ def test_spdx(): def test_memory_functions(): c_h_files = [] for path, _, files in os.walk('src'): - c_h_files += [os.path.join(path, f) for f in files if f.endswith(('.c', '.h'))] + 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 From f04570c93014628127b242e0c246d74f3f807e30 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 10 Oct 2024 20:21:15 +0000 Subject: [PATCH 25/34] Add NULL for previous checked allocation Signed-off-by: Songling Han --- src/common/sha2/sha2_armv8.c | 8 +++++-- src/common/sha2/sha2_c.c | 40 ++++++++++++++++++++++++++--------- src/common/sha3/ossl_sha3.c | 6 ++++++ src/common/sha3/ossl_sha3x4.c | 6 ++++++ 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index e98434d08e..9bebdb8c21 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -181,7 +181,9 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui } else { // Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); - + if (!tmp_in) { + return; + } memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { memcpy(tmp_in + state->data_len, in, inlen); @@ -255,7 +257,9 @@ 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_malloc(buf_len); - + if (!tmp_in) { + return; + } 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 3df7a1c2a9..80f5f73618 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -503,7 +503,9 @@ static const uint8_t iv_512[64] = { void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - + if (!stateout->ctx) { + return; + } for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_224[i]; } @@ -517,7 +519,9 @@ 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_malloc(PQC_SHA256CTX_BYTES); - + if (!stateout->ctx) { + return; + } for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_256[i]; } @@ -530,7 +534,9 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - + if (!stateout->ctx) { + return; + } for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_384[i]; } @@ -543,7 +549,9 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - + if (!stateout->ctx) { + return; + } for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_512[i]; } @@ -556,7 +564,9 @@ 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_malloc(PQC_SHA256CTX_BYTES); - + if (!stateout->ctx) { + return; + } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); @@ -564,7 +574,9 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - + if (!stateout->ctx) { + return; + } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); @@ -572,7 +584,9 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - + if (!stateout->ctx) { + return; + } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); @@ -580,7 +594,9 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - + if (!stateout->ctx) { + return; + } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); @@ -615,7 +631,9 @@ 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_malloc(tmp_buflen); - + if (!tmp_in) { + return; + } memcpy(tmp_in, state->data, state->data_len); memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len); @@ -692,7 +710,9 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_ new_in = in; } else { //Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); - + if (!tmp_in) { + return; + } 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 9a64acf1c5..82b00431df 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -199,6 +199,9 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + if (!tmp) { + return; + } OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check @@ -273,6 +276,9 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + if (!tmp) { + return; + } OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index a6d0cc8cdf..5d8e45ff7b 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -82,6 +82,9 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + if (!tmp) { + return; + } 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); @@ -203,6 +206,9 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); + if (!tmp) { + return; + } 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); From d9e0c964dd5827578f3aa08973d8a9bb75e18c54 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 11 Oct 2024 19:26:24 +0000 Subject: [PATCH 26/34] Fix build failure Signed-off-by: Songling Han --- src/common/sha2/sha2_c.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index 80f5f73618..6e54f1225d 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -503,7 +503,7 @@ static const uint8_t iv_512[64] = { void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - if (!stateout->ctx) { + if (!state->ctx) { return; } for (size_t i = 0; i < 32; ++i) { @@ -519,7 +519,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - if (!stateout->ctx) { + if (!stat->ctx) { return; } for (size_t i = 0; i < 32; ++i) { @@ -549,7 +549,7 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - if (!stateout->ctx) { + if (!state->ctx) { return; } for (size_t i = 0; i < 64; ++i) { From 6032349554999d76c11393315ab379eaf0f32dbe Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 11 Oct 2024 19:30:04 +0000 Subject: [PATCH 27/34] Fix build failure Signed-off-by: Songling Han --- src/common/sha2/sha2_c.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index 6e54f1225d..20cf23badc 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -519,7 +519,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - if (!stat->ctx) { + if (!state->ctx) { return; } for (size_t i = 0; i < 32; ++i) { @@ -534,7 +534,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - if (!stateout->ctx) { + if (!state->ctx) { return; } for (size_t i = 0; i < 64; ++i) { From 1e4201bc280c454a5f6501eef682554356c3c21f Mon Sep 17 00:00:00 2001 From: Songling Han Date: Mon, 14 Oct 2024 21:35:36 +0000 Subject: [PATCH 28/34] revert back to abort() for checked cases Signed-off-by: Songling Han --- src/common/sha2/sha2_armv8.c | 4 ++-- src/common/sha2/sha2_c.c | 20 ++++++++++---------- src/common/sha3/ossl_sha3.c | 4 ++-- src/common/sha3/ossl_sha3x4.c | 4 ++-- src/common/sha3/xkcp_sha3.c | 23 ++++++++++++++--------- src/common/sha3/xkcp_sha3x4.c | 4 ++-- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index 9bebdb8c21..5e8a6c6c2a 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -182,7 +182,7 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui // Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); if (!tmp_in) { - return; + abort(); } memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { @@ -258,7 +258,7 @@ void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_ if (state->data_len) { tmp_in = OQS_MEM_malloc(buf_len); if (!tmp_in) { - return; + abort(); } 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 20cf23badc..09277a6c4d 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -504,7 +504,7 @@ static const uint8_t iv_512[64] = { void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); if (!state->ctx) { - return; + abort(); } for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_224[i]; @@ -520,7 +520,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { state->data_len = 0; state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); if (!state->ctx) { - return; + abort(); } for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_256[i]; @@ -535,7 +535,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!state->ctx) { - return; + abort(); } for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_384[i]; @@ -550,7 +550,7 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!state->ctx) { - return; + abort(); } for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_512[i]; @@ -565,7 +565,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); if (!stateout->ctx) { - return; + abort(); } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -575,7 +575,7 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); if (!stateout->ctx) { - return; + abort(); } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -585,7 +585,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!stateout->ctx) { - return; + abort(); } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -595,7 +595,7 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!stateout->ctx) { - return; + abort(); } stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); @@ -632,7 +632,7 @@ void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t in if (state->data_len) { tmp_in = OQS_MEM_malloc(tmp_buflen); if (!tmp_in) { - return; + abort(); } memcpy(tmp_in, state->data, state->data_len); memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len); @@ -711,7 +711,7 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_ } else { //Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); if (!tmp_in) { - return; + abort(); } memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index 82b00431df..274219c437 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -200,7 +200,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { - return; + abort(); } OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); @@ -277,7 +277,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { - return; + abort(); } OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index 5d8e45ff7b..e8dbd4939c 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -83,7 +83,7 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { - return; + abort(); } OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); @@ -207,7 +207,7 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { - return; + abort(); } OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 400f4191a5..4fe0de2678 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -200,9 +200,11 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx != NULL) { - keccak_inc_reset((uint64_t *)state->ctx); + + if (state->ctx == NULL) { + abort(); } + keccak_inc_reset((uint64_t *)state->ctx); } static void SHA3_sha3_256_inc_absorb(OQS_SHA3_sha3_256_inc_ctx *state, const uint8_t *input, size_t inlen) { @@ -238,9 +240,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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx != NULL) { - keccak_inc_reset((uint64_t *)state->ctx); + if (state->ctx == NULL) { + abort(); } + 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); @@ -275,9 +278,10 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx != NULL) { - keccak_inc_reset((uint64_t *)state->ctx); + if (state->ctx == NULL) { + abort(); } + keccak_inc_reset((uint64_t *)state->ctx); } static void SHA3_sha3_512_inc_absorb(OQS_SHA3_sha3_512_inc_ctx *state, const uint8_t *input, size_t inlen) { @@ -320,7 +324,7 @@ static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) { } state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); if (state->ctx == NULL) { - return; + abort(); } keccak_inc_reset((uint64_t *)state->ctx); } @@ -364,9 +368,10 @@ static void SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input, static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx != NULL) { - keccak_inc_reset((uint64_t *)state->ctx); + if (state->ctx == NULL) { + abort(); } + keccak_inc_reset((uint64_t *)state->ctx); } static void SHA3_shake256_inc_absorb(OQS_SHA3_shake256_inc_ctx *state, const uint8_t *input, size_t inlen) { diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index e493248067..6b03f0baad 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -169,7 +169,7 @@ static void SHA3_shake128_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); if (state->ctx == NULL) { - return; + abort(); } keccak_x4_inc_reset((uint64_t *)state->ctx); } @@ -213,7 +213,7 @@ static void SHA3_shake256_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); if (state->ctx == NULL) { - return; + abort(); } keccak_x4_inc_reset((uint64_t *)state->ctx); } From d502cac9c5bde067293df973aa5fa389ffda809a Mon Sep 17 00:00:00 2001 From: Songling Han Date: Wed, 16 Oct 2024 08:03:36 +0000 Subject: [PATCH 29/34] Add fprintf error for abort cases Signed-off-by: Songling Han --- src/common/sha2/sha2_armv8.c | 2 ++ src/common/sha2/sha2_c.c | 10 ++++++++++ src/common/sha3/ossl_sha3.c | 2 ++ src/common/sha3/ossl_sha3x4.c | 2 ++ src/common/sha3/xkcp_sha3.c | 5 +++++ src/common/sha3/xkcp_sha3x4.c | 2 ++ 6 files changed, 23 insertions(+) diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index 5e8a6c6c2a..ba7e1fded5 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -182,6 +182,7 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui // Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); if (!tmp_in) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } memcpy(tmp_in, state->data, state->data_len); @@ -258,6 +259,7 @@ void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_ if (state->data_len) { tmp_in = OQS_MEM_malloc(buf_len); if (!tmp_in) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } memcpy(tmp_in, state->data, state->data_len); diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index 09277a6c4d..fc5d900867 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -504,6 +504,7 @@ static const uint8_t iv_512[64] = { void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); if (!state->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } for (size_t i = 0; i < 32; ++i) { @@ -520,6 +521,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { state->data_len = 0; state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); if (!state->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } for (size_t i = 0; i < 32; ++i) { @@ -535,6 +537,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!state->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } for (size_t i = 0; i < 64; ++i) { @@ -550,6 +553,7 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!state->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } for (size_t i = 0; i < 64; ++i) { @@ -565,6 +569,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); if (!stateout->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } stateout->data_len = statein->data_len; @@ -575,6 +580,7 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); if (!stateout->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } stateout->data_len = statein->data_len; @@ -585,6 +591,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!stateout->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } stateout->data_len = statein->data_len; @@ -595,6 +602,7 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); if (!stateout->ctx) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } stateout->data_len = statein->data_len; @@ -632,6 +640,7 @@ void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t in if (state->data_len) { tmp_in = OQS_MEM_malloc(tmp_buflen); if (!tmp_in) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } memcpy(tmp_in, state->data, state->data_len); @@ -711,6 +720,7 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_ } else { //Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); if (!tmp_in) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } memcpy(tmp_in, state->data, state->data_len); diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index 274219c437..e50d65303d 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -200,6 +200,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); @@ -277,6 +278,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index e8dbd4939c..857607d99d 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -83,6 +83,7 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0); @@ -207,6 +208,7 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); if (!tmp) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0); diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 4fe0de2678..8549ed23ab 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -202,6 +202,7 @@ static void SHA3_sha3_256_inc_init(OQS_SHA3_sha3_256_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_inc_reset((uint64_t *)state->ctx); @@ -241,6 +242,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_inc_reset((uint64_t *)state->ctx); @@ -279,6 +281,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_inc_reset((uint64_t *)state->ctx); @@ -324,6 +327,7 @@ static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) { } state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_inc_reset((uint64_t *)state->ctx); @@ -369,6 +373,7 @@ static void SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input, static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_inc_reset((uint64_t *)state->ctx); diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index 6b03f0baad..48aba29279 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -169,6 +169,7 @@ static void SHA3_shake128_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_x4_inc_reset((uint64_t *)state->ctx); @@ -213,6 +214,7 @@ static void SHA3_shake256_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); if (state->ctx == NULL) { + fprintf(stderr, "Memory allocation failed\n"); abort(); } keccak_x4_inc_reset((uint64_t *)state->ctx); From 8a788dbe81c5e533be76a16ac24afd4786ceb701 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 17 Oct 2024 06:21:25 +0000 Subject: [PATCH 30/34] Add LIBOQS_die Signed-off-by: Songling Han --- src/common/common.h | 12 +++++++++ src/common/sha2/sha2_armv8.c | 10 ++----- src/common/sha2/sha2_c.c | 50 +++++++---------------------------- src/common/sha3/ossl_sha3.c | 10 ++----- src/common/sha3/ossl_sha3x4.c | 10 ++----- src/common/sha3/xkcp_sha3.c | 25 ++++-------------- src/common/sha3/xkcp_sha3x4.c | 10 ++----- 7 files changed, 35 insertions(+), 92 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index b15e244a39..95734aa9e2 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -85,6 +85,18 @@ extern "C" { } \ } while (0) +/** + * Macro for terminating the program if a given pointer is NULL. + * @param ptr The pointer to check. + * @param msg The error message to display if the pointer is NULL. + */ +#define LIBOQS_die(ptr, msg) do { \ + if (!(ptr)) { \ + fprintf(stderr, "%s\n", msg); \ + abort(); \ + } \ +} while (0) + /** * This macro is intended to replace those assert()s * involving side-effecting statements in aes/aes_ossl.c. diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index ba7e1fded5..c8a7cee786 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -181,10 +181,7 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui } else { // Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); - if (!tmp_in) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp_in, "Memory allocation failed"); memcpy(tmp_in, state->data, state->data_len); if (in && inlen) { memcpy(tmp_in + state->data_len, in, inlen); @@ -258,10 +255,7 @@ 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_malloc(buf_len); - if (!tmp_in) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp_in, "Memory allocation failed"); 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 fc5d900867..091b293765 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -503,10 +503,7 @@ static const uint8_t iv_512[64] = { void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - if (!state->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_224[i]; } @@ -520,10 +517,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - if (!state->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_256[i]; } @@ -536,10 +530,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - if (!state->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_384[i]; } @@ -552,10 +543,7 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - if (!state->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_512[i]; } @@ -568,10 +556,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - if (!stateout->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); @@ -579,10 +564,7 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - if (!stateout->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); @@ -590,10 +572,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - if (!stateout->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); @@ -601,10 +580,7 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - if (!stateout->ctx) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); @@ -639,10 +615,7 @@ 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_malloc(tmp_buflen); - if (!tmp_in) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp_in, "Memory allocation failed"); memcpy(tmp_in, state->data, state->data_len); memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len); @@ -719,10 +692,7 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_ new_in = in; } else { //Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); - if (!tmp_in) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp_in, "Memory allocation failed"); 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 e50d65303d..632ea48fab 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -199,10 +199,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - if (!tmp) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp, "Memory allocation failed"); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check @@ -277,10 +274,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - if (!tmp) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp, "Memory allocation failed"); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index 857607d99d..df11e86e97 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -82,10 +82,7 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - if (!tmp) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp, "Memory allocation failed"); 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); @@ -207,10 +204,7 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - if (!tmp) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(tmp, "Memory allocation failed"); 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); diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 8549ed23ab..346f12de02 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -201,10 +201,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -241,10 +238,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); 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) { @@ -280,10 +274,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -326,10 +317,7 @@ static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) { return; } state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -372,10 +360,7 @@ static void SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input, static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); keccak_inc_reset((uint64_t *)state->ctx); } diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index 48aba29279..9622474dce 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -168,10 +168,7 @@ static void SHA3_shake128_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); 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) { @@ -213,10 +210,7 @@ static void SHA3_shake256_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); - if (state->ctx == NULL) { - fprintf(stderr, "Memory allocation failed\n"); - abort(); - } + LIBOQS_die(state->ctx, "Memory allocation failed"); keccak_x4_inc_reset((uint64_t *)state->ctx); } From 2077298241ab87fc546faccf593505a2debb3f0e Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 17 Oct 2024 07:06:18 +0000 Subject: [PATCH 31/34] use OQS_EXIT_IF_NULLPTR for checked malloc cases Signed-off-by: Songling Han --- src/common/sha2/sha2_armv8.c | 4 ++-- src/common/sha2/sha2_c.c | 20 ++++++++++---------- src/common/sha3/ossl_sha3.c | 4 ++-- src/common/sha3/ossl_sha3x4.c | 4 ++-- src/common/sha3/xkcp_sha3.c | 10 +++++----- src/common/sha3/xkcp_sha3x4.c | 4 ++-- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index c8a7cee786..2ca7325cb8 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -181,7 +181,7 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui } else { // Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); - LIBOQS_die(tmp_in, "Memory allocation failed"); + 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); @@ -255,7 +255,7 @@ 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_malloc(buf_len); - LIBOQS_die(tmp_in, "Memory allocation failed"); + 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 091b293765..c660374ff8 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -503,7 +503,7 @@ static const uint8_t iv_512[64] = { void oqs_sha2_sha224_inc_init_c(sha224ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_224[i]; } @@ -517,7 +517,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 32; ++i) { state->ctx[i] = iv_256[i]; } @@ -530,7 +530,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) { void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_384[i]; } @@ -543,7 +543,7 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) { void oqs_sha2_sha512_inc_init_c(sha512ctx *state) { state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); for (size_t i = 0; i < 64; ++i) { state->ctx[i] = iv_512[i]; } @@ -556,7 +556,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES); @@ -564,7 +564,7 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + 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); @@ -572,7 +572,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); stateout->data_len = statein->data_len; memcpy(stateout->data, statein->data, 128); memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES); @@ -580,7 +580,7 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + 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); @@ -615,7 +615,7 @@ 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_malloc(tmp_buflen); - LIBOQS_die(tmp_in, "Memory allocation failed"); + 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); @@ -692,7 +692,7 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_ new_in = in; } else { //Combine incremental data with final input tmp_in = OQS_MEM_malloc(tmp_len); - LIBOQS_die(tmp_in, "Memory allocation failed"); + 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 632ea48fab..de8919bb8c 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -199,7 +199,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - LIBOQS_die(tmp, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(tmp, "SHA3"); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check @@ -274,7 +274,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - LIBOQS_die(tmp, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(tmp, "SHA3"); OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen); memcpy(output, tmp + s->n_out, outlen); OQS_MEM_insecure_free(tmp); // IGNORE free-check diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index df11e86e97..4bdee4d2be 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -82,7 +82,7 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - LIBOQS_die(tmp, "Memory allocation failed"); + 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); @@ -204,7 +204,7 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t * OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen); } else { uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen); - LIBOQS_die(tmp, "Memory allocation failed"); + 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); diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 346f12de02..333690071f 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -201,7 +201,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -238,7 +238,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + 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) { @@ -274,7 +274,7 @@ 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_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -317,7 +317,7 @@ static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) { return; } state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3"); keccak_inc_reset((uint64_t *)state->ctx); } @@ -360,7 +360,7 @@ static void SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input, static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + 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 9622474dce..893744def8 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -168,7 +168,7 @@ static void SHA3_shake128_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + 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) { @@ -210,7 +210,7 @@ static void SHA3_shake256_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_ static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) { state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES); - LIBOQS_die(state->ctx, "Memory allocation failed"); + OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3x4"); keccak_x4_inc_reset((uint64_t *)state->ctx); } From 7afe1a31bde243077fc75c511f5ac34b95e1fff9 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 17 Oct 2024 07:11:44 +0000 Subject: [PATCH 32/34] Fix build failure Signed-off-by: Songling Han --- src/common/sha2/sha2_c.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index c660374ff8..ba71abab23 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -556,7 +556,7 @@ 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_malloc(PQC_SHA256CTX_BYTES); - OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); + 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); @@ -572,7 +572,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) { stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES); - OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2"); + 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); From 6b825e1f73937ab98e3f7e052f254c55b54e9001 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Fri, 18 Oct 2024 23:08:05 +0000 Subject: [PATCH 33/34] remove NULL check to keep code consistent Signed-off-by: Songling Han --- src/common/sha3/xkcp_sha3.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 333690071f..32b0db6a8c 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -313,9 +313,6 @@ 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) { - if (state == NULL) { - return; - } 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); From 405b6ac3bd4a0e9140d854b3f8af06ba6993e329 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Sat, 19 Oct 2024 01:52:22 +0000 Subject: [PATCH 34/34] remove LIBOQS_die Signed-off-by: Songling Han --- src/common/common.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index 95734aa9e2..b15e244a39 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -85,18 +85,6 @@ extern "C" { } \ } while (0) -/** - * Macro for terminating the program if a given pointer is NULL. - * @param ptr The pointer to check. - * @param msg The error message to display if the pointer is NULL. - */ -#define LIBOQS_die(ptr, msg) do { \ - if (!(ptr)) { \ - fprintf(stderr, "%s\n", msg); \ - abort(); \ - } \ -} while (0) - /** * This macro is intended to replace those assert()s * involving side-effecting statements in aes/aes_ossl.c.