Skip to content

Commit

Permalink
remove OQS_MEM_free
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 23, 2024
1 parent dd1f819 commit b268445
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 38 deletions.
8 changes: 6 additions & 2 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 2 additions & 10 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/common/sha2/sha2_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/common/sha3/ossl_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/common/sha3/ossl_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/sig_stfl/lms/external/hss_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,15 @@ void hss_free_working_key(struct hss_working_key *w) {
unsigned j, k;
for (j=0; j<MAX_SUBLEVELS; j++)
for (k=0; k<3; k++)
OQS_MEM_free(tree->subtree[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; i<MAX_HSS_LEVELS-1; i++) {
OQS_MEM_free(w->signed_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
}
4 changes: 2 additions & 2 deletions src/sig_stfl/lms/external/hss_generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/sig_stfl/lms/external/hss_keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/sig_stfl/lms/external/hss_thread_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b268445

Please sign in to comment.