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>

[open-quantum-safe#1823] update memory allocator for copy_from_upstream

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] format code

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] Use OpenSSL Memory Allocator for BIKE, FrodoKEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] Add Comments for Doxygen

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
  • Loading branch information
songlingatpan committed Sep 18, 2024
1 parent 28e7f4b commit bdbb1de
Show file tree
Hide file tree
Showing 112 changed files with 504 additions and 394 deletions.
4 changes: 2 additions & 2 deletions scripts/copy_from_upstream/src/kem/family/kem_scheme.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% endif %}
OQS_KEM *OQS_KEM_{{ family }}_{{ scheme['scheme'] }}_new(void) {

OQS_KEM *kem = malloc(sizeof(OQS_KEM));
OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM));
if (kem == NULL) {
return NULL;
}
Expand Down Expand Up @@ -42,7 +42,7 @@ OQS_KEM *OQS_KEM_{{ family }}_{{ scheme['scheme'] }}_new(void) {
/** Alias */
OQS_KEM *OQS_KEM_{{ family }}_{{ scheme['alias_scheme'] }}_new(void) {

OQS_KEM *kem = malloc(sizeof(OQS_KEM));
OQS_KEM *kem = OQS_MEM_malloc(sizeof(OQS_KEM));
if (kem == NULL) {
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/copy_from_upstream/src/sig/family/sig_scheme.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% endif %}
OQS_SIG *OQS_SIG_{{ family }}_{{ scheme['scheme'] }}_new(void) {

OQS_SIG *sig = malloc(sizeof(OQS_SIG));
OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG));
if (sig == NULL) {
return NULL;
}
Expand Down Expand Up @@ -41,7 +41,7 @@ OQS_SIG *OQS_SIG_{{ family }}_{{ scheme['scheme'] }}_new(void) {
/** Alias */
OQS_SIG *OQS_SIG_{{ family }}_{{ scheme['alias_scheme'] }}_new(void) {

OQS_SIG *sig = malloc(sizeof(OQS_SIG));
OQS_SIG *sig = OQS_MEM_malloc(sizeof(OQS_SIG));
if (sig == NULL) {
return NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{%- if scheme['signed_msg_order'] == 'sig_then_msg' %}
// signed_msg = signature || msg
*signed_msg_len = signature_len + msg_len;
*signed_msg = malloc(*signed_msg_len);
*signed_msg = OQS_MEM_malloc(*signed_msg_len);
if (*signed_msg == NULL) {
return OQS_ERROR;
}
Expand All @@ -13,7 +13,7 @@
{%- elif scheme['signed_msg_order'] == 'msg_then_sig' %}
// signed_msg = msg || signature
*signed_msg_len = msg_len + signature_len;
*signed_msg = malloc(*signed_msg_len);
*signed_msg = OQS_MEM_malloc(*signed_msg_len);
if (*signed_msg == NULL) {
return OQS_ERROR;
}
Expand All @@ -24,7 +24,7 @@
// signed_msg = sig_len (2 bytes, big endian) || nonce (40 bytes) || msg || 0x29 || sig
const uint16_t signature_len_uint16 = (uint16_t)signature_len;
*signed_msg_len = 2 + signature_len_uint16 + msg_len;
*signed_msg = malloc(*signed_msg_len);
*signed_msg = OQS_MEM_malloc(*signed_msg_len);
if (*signed_msg == NULL) {
return OQS_ERROR;
}
Expand All @@ -44,7 +44,7 @@
// signed_msg = sig_len (2 bytes, big endian) || nonce (40 bytes) || msg || 0x2A || sig
const uint16_t signature_len_uint16 = (uint16_t)signature_len;
*signed_msg_len = 2 + signature_len + msg_len;
*signed_msg = malloc(*signed_msg_len);
*signed_msg = OQS_MEM_malloc(*signed_msg_len);
if (*signed_msg == NULL) {
return OQS_ERROR;
}
Expand Down
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
37 changes: 32 additions & 5 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,16 +298,37 @@ 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
#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)
Expand Down Expand Up @@ -369,7 +390,13 @@ 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)
#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);
Expand Down
83 changes: 83 additions & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,89 @@
extern "C" {
#endif

/**
* @brief Memory allocation and deallocation functions.
*
* These macros provide a unified interface for memory operations,
* using OpenSSL functions when OQS_USE_OPENSSL is defined, and
* standard C library functions otherwise.
*/
#if defined(OQS_USE_OPENSSL)
#ifndef OPENSSL_malloc
# define OPENSSL_malloc(num) \
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);
/**
* Allocates memory of a given size.
* @param size The size of the memory to be allocated in bytes.
* @return A pointer to the allocated memory.
*/
#define OQS_MEM_malloc(size) OPENSSL_malloc(size)
/**
* Frees the allocated memory.
* @param ptr The pointer to the memory to be freed.
*/
#define OQS_MEM_free(ptr) OPENSSL_free(ptr)
/**
* Allocates memory for an array of elements of a given size.
* @param num_elements The number of elements to allocate.
* @param element_size The size of each element in bytes.
* @return A pointer to the allocated memory.
*/
#define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size))
/**
* Duplicates a string.
* @param str The string to be duplicated.
* @return A pointer to the newly allocated string.
*/
#define OQS_MEM_strdup(str) OPENSSL_strdup(str)
#else
/**
* Allocates memory of a given size.
* @param size The size of the memory to be allocated in bytes.
* @return A pointer to the allocated memory.
*/
#define OQS_MEM_malloc(size) malloc(size)
/**
* Frees the allocated memory.
* @param ptr The pointer to the memory to be freed.
*/
#define OQS_MEM_free(ptr) free(ptr)
/**
* Allocates memory for an array of elements of a given size.
* @param num_elements The number of elements to allocate.
* @param element_size The size of each element in bytes.
* @return A pointer to the allocated memory.
*/
#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size)
/**
* Duplicates a string.
* @param str The string to be duplicated.
* @return A pointer to the newly allocated string.
*/
#define OQS_MEM_strdup(str) strdup(str)
#endif

/**
* 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
Loading

0 comments on commit bdbb1de

Please sign in to comment.