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; }