Skip to content

Commit

Permalink
use OQS_EXIT_IF_NULLPTR for checked malloc cases
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Oct 18, 2024
1 parent 8a788db commit 2077298
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/common/sha2/sha2_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui
} else {
// Combine incremental data with final input
tmp_in = OQS_MEM_malloc(tmp_len);
LIBOQS_die(tmp_in, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2");
memcpy(tmp_in, state->data, state->data_len);
if (in && inlen) {
memcpy(tmp_in + state->data_len, in, inlen);
Expand Down Expand Up @@ -255,7 +255,7 @@ void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_
/* Process any existing incremental data first */
if (state->data_len) {
tmp_in = OQS_MEM_malloc(buf_len);
LIBOQS_die(tmp_in, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2");
memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, buf_len - state->data_len);

Expand Down
20 changes: 10 additions & 10 deletions src/common/sha2/sha2_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ static const uint8_t iv_512[64] = {

void oqs_sha2_sha224_inc_init_c(sha224ctx *state) {
state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2");
for (size_t i = 0; i < 32; ++i) {
state->ctx[i] = iv_224[i];
}
Expand All @@ -517,7 +517,7 @@ void oqs_sha2_sha224_inc_init_c(sha224ctx *state) {
void oqs_sha2_sha256_inc_init_c(sha256ctx *state) {
state->data_len = 0;
state->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2");
for (size_t i = 0; i < 32; ++i) {
state->ctx[i] = iv_256[i];
}
Expand All @@ -530,7 +530,7 @@ void oqs_sha2_sha256_inc_init_c(sha256ctx *state) {

void oqs_sha2_sha384_inc_init_c(sha384ctx *state) {
state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2");
for (size_t i = 0; i < 64; ++i) {
state->ctx[i] = iv_384[i];
}
Expand All @@ -543,7 +543,7 @@ void oqs_sha2_sha384_inc_init_c(sha384ctx *state) {

void oqs_sha2_sha512_inc_init_c(sha512ctx *state) {
state->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2");
for (size_t i = 0; i < 64; ++i) {
state->ctx[i] = iv_512[i];
}
Expand All @@ -556,31 +556,31 @@ void oqs_sha2_sha512_inc_init_c(sha512ctx *state) {

void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *statein) {
stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2");
stateout->data_len = statein->data_len;
memcpy(stateout->data, statein->data, 128);
memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES);
}

void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *statein) {
stateout->ctx = OQS_MEM_malloc(PQC_SHA256CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(stateout->ctx, "SHA2");
stateout->data_len = statein->data_len;
memcpy(stateout->data, statein->data, 128);
memcpy(stateout->ctx, statein->ctx, PQC_SHA256CTX_BYTES);
}

void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *statein) {
stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA2");
stateout->data_len = statein->data_len;
memcpy(stateout->data, statein->data, 128);
memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES);
}

void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *statein) {
stateout->ctx = OQS_MEM_malloc(PQC_SHA512CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(stateout->ctx, "SHA2");
stateout->data_len = statein->data_len;
memcpy(stateout->data, statein->data, 128);
memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES);
Expand Down Expand Up @@ -615,7 +615,7 @@ void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t in
/* Process any existing incremental data first */
if (state->data_len) {
tmp_in = OQS_MEM_malloc(tmp_buflen);
LIBOQS_die(tmp_in, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2");
memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len);

Expand Down Expand Up @@ -692,7 +692,7 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_
new_in = in;
} else { //Combine incremental data with final input
tmp_in = OQS_MEM_malloc(tmp_len);
LIBOQS_die(tmp_in, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp_in, "SHA2");
memcpy(tmp_in, state->data, state->data_len);
if (in && inlen) {
memcpy(tmp_in + state->data_len, in, inlen);
Expand Down
4 changes: 2 additions & 2 deletions src/common/sha3/ossl_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static void SHA3_shake128_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s
OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen);
} else {
uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen);
LIBOQS_die(tmp, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp, "SHA3");
OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen);
memcpy(output, tmp + s->n_out, outlen);
OQS_MEM_insecure_free(tmp); // IGNORE free-check
Expand Down Expand Up @@ -274,7 +274,7 @@ static void SHA3_shake256_inc_squeeze(uint8_t *output, size_t outlen, OQS_SHA3_s
OSSL_FUNC(EVP_DigestFinalXOF)(clone, output, outlen);
} else {
uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen);
LIBOQS_die(tmp, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp, "SHA3");
OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen);
memcpy(output, tmp + s->n_out, outlen);
OQS_MEM_insecure_free(tmp); // IGNORE free-check
Expand Down
4 changes: 2 additions & 2 deletions src/common/sha3/ossl_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static void SHA3_shake128_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t *
OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen);
} else {
uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen);
LIBOQS_die(tmp, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp, "SHA3x4");
OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0);
OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen);
memcpy(out0, tmp + s->n_out, outlen);
Expand Down Expand Up @@ -204,7 +204,7 @@ static void SHA3_shake256_x4_inc_squeeze(uint8_t *out0, uint8_t *out1, uint8_t *
OSSL_FUNC(EVP_DigestFinalXOF)(clone, out3, outlen);
} else {
uint8_t *tmp = OQS_MEM_malloc(s->n_out + outlen);
LIBOQS_die(tmp, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(tmp, "SHA3x4");
OSSL_FUNC(EVP_MD_CTX_copy_ex)(clone, s->mdctx0);
OSSL_FUNC(EVP_DigestFinalXOF)(clone, tmp, s->n_out + outlen);
memcpy(out0, tmp + s->n_out, outlen);
Expand Down
10 changes: 5 additions & 5 deletions src/common/sha3/xkcp_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static void SHA3_sha3_256(uint8_t *output, const uint8_t *input, size_t inlen) {
static void SHA3_sha3_256_inc_init(OQS_SHA3_sha3_256_inc_ctx *state) {
state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES);

LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down Expand Up @@ -238,7 +238,7 @@ static void SHA3_sha3_384(uint8_t *output, const uint8_t *input, size_t inlen) {

static void SHA3_sha3_384_inc_init(OQS_SHA3_sha3_384_inc_ctx *state) {
state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3");
keccak_inc_reset((uint64_t *)state->ctx);
}
static void SHA3_sha3_384_inc_absorb(OQS_SHA3_sha3_384_inc_ctx *state, const uint8_t *input, size_t inlen) {
Expand Down Expand Up @@ -274,7 +274,7 @@ static void SHA3_sha3_512(uint8_t *output, const uint8_t *input, size_t inlen) {

static void SHA3_sha3_512_inc_init(OQS_SHA3_sha3_512_inc_ctx *state) {
state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down Expand Up @@ -317,7 +317,7 @@ static void SHA3_shake128_inc_init(OQS_SHA3_shake128_inc_ctx *state) {
return;
}
state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down Expand Up @@ -360,7 +360,7 @@ static void SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input,

static void SHA3_shake256_inc_init(OQS_SHA3_shake256_inc_ctx *state) {
state->ctx = OQS_MEM_aligned_alloc(KECCAK_CTX_ALIGNMENT, KECCAK_CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/sha3/xkcp_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static void SHA3_shake128_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_

static void SHA3_shake128_x4_inc_init(OQS_SHA3_shake128_x4_inc_ctx *state) {
state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3x4");
keccak_x4_inc_reset((uint64_t *)state->ctx);
}
static void SHA3_shake128_x4_inc_absorb(OQS_SHA3_shake128_x4_inc_ctx *state, const uint8_t *in0, const uint8_t *in1, const uint8_t *in2, const uint8_t *in3, size_t inlen) {
Expand Down Expand Up @@ -210,7 +210,7 @@ static void SHA3_shake256_x4(uint8_t *out0, uint8_t *out1, uint8_t *out2, uint8_

static void SHA3_shake256_x4_inc_init(OQS_SHA3_shake256_x4_inc_ctx *state) {
state->ctx = OQS_MEM_aligned_alloc(KECCAK_X4_CTX_ALIGNMENT, KECCAK_X4_CTX_BYTES);
LIBOQS_die(state->ctx, "Memory allocation failed");
OQS_EXIT_IF_NULLPTR(state->ctx, "SHA3x4");
keccak_x4_inc_reset((uint64_t *)state->ctx);
}

Expand Down

0 comments on commit 2077298

Please sign in to comment.