Skip to content

Commit

Permalink
Add LIBOQS_die
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 d502cac commit 8a788db
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 92 deletions.
12 changes: 12 additions & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ extern "C" {
} \
} while (0)

/**
* Macro for terminating the program if a given pointer is NULL.
* @param ptr The pointer to check.
* @param msg The error message to display if the pointer is NULL.
*/
#define LIBOQS_die(ptr, msg) do { \
if (!(ptr)) { \
fprintf(stderr, "%s\n", msg); \
abort(); \
} \
} while (0)

/**
* This macro is intended to replace those assert()s
* involving side-effecting statements in aes/aes_ossl.c.
Expand Down
10 changes: 2 additions & 8 deletions src/common/sha2/sha2_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +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);
if (!tmp_in) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp_in, "Memory allocation failed");
memcpy(tmp_in, state->data, state->data_len);
if (in && inlen) {
memcpy(tmp_in + state->data_len, in, inlen);
Expand Down Expand Up @@ -258,10 +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);
if (!tmp_in) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp_in, "Memory allocation failed");
memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, buf_len - state->data_len);

Expand Down
50 changes: 10 additions & 40 deletions src/common/sha2/sha2_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +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);
if (!state->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
for (size_t i = 0; i < 32; ++i) {
state->ctx[i] = iv_224[i];
}
Expand All @@ -520,10 +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);
if (!state->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
for (size_t i = 0; i < 32; ++i) {
state->ctx[i] = iv_256[i];
}
Expand All @@ -536,10 +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);
if (!state->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
for (size_t i = 0; i < 64; ++i) {
state->ctx[i] = iv_384[i];
}
Expand All @@ -552,10 +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);
if (!state->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
for (size_t i = 0; i < 64; ++i) {
state->ctx[i] = iv_512[i];
}
Expand All @@ -568,43 +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);
if (!stateout->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
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);
if (!stateout->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
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);
if (!stateout->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
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);
if (!stateout->ctx) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
stateout->data_len = statein->data_len;
memcpy(stateout->data, statein->data, 128);
memcpy(stateout->ctx, statein->ctx, PQC_SHA512CTX_BYTES);
Expand Down Expand Up @@ -639,10 +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);
if (!tmp_in) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp_in, "Memory allocation failed");
memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len);

Expand Down Expand Up @@ -719,10 +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);
if (!tmp_in) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp_in, "Memory allocation failed");
memcpy(tmp_in, state->data, state->data_len);
if (in && inlen) {
memcpy(tmp_in + state->data_len, in, inlen);
Expand Down
10 changes: 2 additions & 8 deletions src/common/sha3/ossl_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +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);
if (!tmp) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp, "Memory allocation failed");
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 @@ -277,10 +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);
if (!tmp) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp, "Memory allocation failed");
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
10 changes: 2 additions & 8 deletions src/common/sha3/ossl_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +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);
if (!tmp) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp, "Memory allocation failed");
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 @@ -207,10 +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);
if (!tmp) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(tmp, "Memory allocation failed");
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
25 changes: 5 additions & 20 deletions src/common/sha3/xkcp_sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +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);

if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down Expand Up @@ -241,10 +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);
if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
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 @@ -280,10 +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);
if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down Expand Up @@ -326,10 +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);
if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down Expand Up @@ -372,10 +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);
if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
keccak_inc_reset((uint64_t *)state->ctx);
}

Expand Down
10 changes: 2 additions & 8 deletions src/common/sha3/xkcp_sha3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +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);
if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
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 @@ -213,10 +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);
if (state->ctx == NULL) {
fprintf(stderr, "Memory allocation failed\n");
abort();
}
LIBOQS_die(state->ctx, "Memory allocation failed");
keccak_x4_inc_reset((uint64_t *)state->ctx);
}

Expand Down

0 comments on commit 8a788db

Please sign in to comment.