From a30af4ae893fc171d7a844551383a399b44e8037 Mon Sep 17 00:00:00 2001 From: Songling Han Date: Thu, 17 Oct 2024 06:21:25 +0000 Subject: [PATCH] Add LIBOQS_die Signed-off-by: Songling Han --- src/common/common.h | 12 +++++++++ src/common/sha2/sha2_armv8.c | 10 ++----- src/common/sha2/sha2_c.c | 50 +++++++---------------------------- src/common/sha3/ossl_sha3.c | 10 ++----- src/common/sha3/ossl_sha3x4.c | 10 ++----- src/common/sha3/xkcp_sha3.c | 25 ++++-------------- src/common/sha3/xkcp_sha3x4.c | 10 ++----- 7 files changed, 35 insertions(+), 92 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index b15e244a3..95734aa9e 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -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. diff --git a/src/common/sha2/sha2_armv8.c b/src/common/sha2/sha2_armv8.c index ba7e1fded..c8a7cee78 100644 --- a/src/common/sha2/sha2_armv8.c +++ b/src/common/sha2/sha2_armv8.c @@ -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); @@ -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); diff --git a/src/common/sha2/sha2_c.c b/src/common/sha2/sha2_c.c index fc5d90086..091b29376 100644 --- a/src/common/sha2/sha2_c.c +++ b/src/common/sha2/sha2_c.c @@ -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]; } @@ -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]; } @@ -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]; } @@ -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]; } @@ -568,10 +556,7 @@ 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); @@ -579,10 +564,7 @@ void oqs_sha2_sha224_inc_ctx_clone_c(sha224ctx *stateout, const sha224ctx *state 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); @@ -590,10 +572,7 @@ void oqs_sha2_sha256_inc_ctx_clone_c(sha256ctx *stateout, const sha256ctx *state 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); @@ -601,10 +580,7 @@ void oqs_sha2_sha384_inc_ctx_clone_c(sha384ctx *stateout, const sha384ctx *state 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); @@ -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); @@ -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); diff --git a/src/common/sha3/ossl_sha3.c b/src/common/sha3/ossl_sha3.c index e50d65303..632ea48fa 100644 --- a/src/common/sha3/ossl_sha3.c +++ b/src/common/sha3/ossl_sha3.c @@ -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 @@ -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 diff --git a/src/common/sha3/ossl_sha3x4.c b/src/common/sha3/ossl_sha3x4.c index 857607d99..df11e86e9 100644 --- a/src/common/sha3/ossl_sha3x4.c +++ b/src/common/sha3/ossl_sha3x4.c @@ -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); @@ -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); diff --git a/src/common/sha3/xkcp_sha3.c b/src/common/sha3/xkcp_sha3.c index 8549ed23a..346f12de0 100644 --- a/src/common/sha3/xkcp_sha3.c +++ b/src/common/sha3/xkcp_sha3.c @@ -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); } @@ -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) { @@ -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); } @@ -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); } @@ -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); } diff --git a/src/common/sha3/xkcp_sha3x4.c b/src/common/sha3/xkcp_sha3x4.c index 48aba2927..9622474dc 100644 --- a/src/common/sha3/xkcp_sha3x4.c +++ b/src/common/sha3/xkcp_sha3x4.c @@ -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) { @@ -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); }