Skip to content

Commit

Permalink
[open-quantum-safe#1823] replace malloc/calloc/strdup/free with opens…
Browse files Browse the repository at this point in the history
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
  • Loading branch information
songlingatpan committed Sep 21, 2024
1 parent 3b2cd83 commit e4d0560
Show file tree
Hide file tree
Showing 38 changed files with 375 additions and 312 deletions.
2 changes: 1 addition & 1 deletion src/common/aes/aes128_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/common/aes/aes256_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/common/aes/aes_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/common/aes/aes_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
41 changes: 34 additions & 7 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 *)) {
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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) {
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) {
Expand Down
12 changes: 6 additions & 6 deletions src/common/sha3/ossl_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
Expand Down 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);
free(tmp); // IGNORE free-check
OQS_MEM_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);
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) {
Expand Down Expand Up @@ -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)();
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);
free(tmp); // IGNORE free-check
OQS_MEM_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);
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) {
Expand Down
12 changes: 6 additions & 6 deletions src/common/sha3/ossl_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
Expand Down 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);
free(tmp); // IGNORE free-check
OQS_MEM_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);
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) {
Expand Down Expand Up @@ -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)();
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);
free(tmp); // IGNORE free-check
OQS_MEM_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);
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) {
Expand Down
Loading

0 comments on commit e4d0560

Please sign in to comment.