Skip to content

Commit

Permalink
Add NULL check or SHA2
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 22, 2024
1 parent 0c2345b commit 9fecaa8
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 72 deletions.
81 changes: 60 additions & 21 deletions src/common/sha2/sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,121 @@
static struct OQS_SHA2_callbacks *callbacks = &sha2_default_callbacks;

OQS_API void OQS_SHA2_set_callbacks(struct OQS_SHA2_callbacks *new_callbacks) {
callbacks = new_callbacks;
if (new_callbacks != NULL) {
callbacks = new_callbacks;
}
}

void OQS_SHA2_sha256_inc_init(OQS_SHA2_sha256_ctx *state) {
callbacks->SHA2_sha256_inc_init(state);
if (state != NULL && callbacks != NULL && callbacks->SHA2_sha256_inc_init != NULL) {
callbacks->SHA2_sha256_inc_init(state);
}
}

void OQS_SHA2_sha256_inc_ctx_clone(OQS_SHA2_sha256_ctx *dest, const OQS_SHA2_sha256_ctx *src) {
callbacks->SHA2_sha256_inc_ctx_clone(dest, src);
if (dest != NULL && src != NULL && callbacks != NULL && callbacks->SHA2_sha256_inc_ctx_clone != NULL) {
callbacks->SHA2_sha256_inc_ctx_clone(dest, src);
}
}

void OQS_SHA2_sha256_inc_blocks(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inblocks) {
callbacks->SHA2_sha256_inc_blocks(state, in, inblocks);
if (state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha256_inc_blocks != NULL) {
callbacks->SHA2_sha256_inc_blocks(state, in, inblocks);
}
}

void OQS_SHA2_sha256_inc(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t len) {
callbacks->SHA2_sha256_inc(state, in, len);
if (state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha256_inc != NULL) {
callbacks->SHA2_sha256_inc(state, in, len);
}
}

void OQS_SHA2_sha256_inc_finalize(uint8_t *out, OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inlen) {
callbacks->SHA2_sha256_inc_finalize(out, state, in, inlen);
if (out != NULL && state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha256_inc_finalize != NULL) {
callbacks->SHA2_sha256_inc_finalize(out, state, in, inlen);
}
}

void OQS_SHA2_sha256_inc_ctx_release(OQS_SHA2_sha256_ctx *state) {
callbacks->SHA2_sha256_inc_ctx_release(state);
if (state != NULL && callbacks != NULL && callbacks->SHA2_sha256_inc_ctx_release != NULL) {
callbacks->SHA2_sha256_inc_ctx_release(state);
}
}

void OQS_SHA2_sha384_inc_init(OQS_SHA2_sha384_ctx *state) {
callbacks->SHA2_sha384_inc_init(state);
if (state != NULL && callbacks != NULL && callbacks->SHA2_sha384_inc_init != NULL) {
callbacks->SHA2_sha384_inc_init(state);
}
}

void OQS_SHA2_sha384_inc_ctx_clone(OQS_SHA2_sha384_ctx *dest, const OQS_SHA2_sha384_ctx *src) {
callbacks->SHA2_sha384_inc_ctx_clone(dest, src);
if (dest != NULL && src != NULL && callbacks != NULL && callbacks->SHA2_sha384_inc_ctx_clone != NULL) {
callbacks->SHA2_sha384_inc_ctx_clone(dest, src);
}
}

void OQS_SHA2_sha384_inc_blocks(OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inblocks) {
callbacks->SHA2_sha384_inc_blocks(state, in, inblocks);
if (state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha384_inc_blocks != NULL) {
callbacks->SHA2_sha384_inc_blocks(state, in, inblocks);
}
}

void OQS_SHA2_sha384_inc_finalize(uint8_t *out, OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inlen) {
callbacks->SHA2_sha384_inc_finalize(out, state, in, inlen);
if (out != NULL && state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha384_inc_finalize != NULL) {
callbacks->SHA2_sha384_inc_finalize(out, state, in, inlen);
}
}

void OQS_SHA2_sha384_inc_ctx_release(OQS_SHA2_sha384_ctx *state) {
callbacks->SHA2_sha384_inc_ctx_release(state);
if (state != NULL && callbacks != NULL && callbacks->SHA2_sha384_inc_ctx_release != NULL) {
callbacks->SHA2_sha384_inc_ctx_release(state);
}
}

void OQS_SHA2_sha512_inc_init(OQS_SHA2_sha512_ctx *state) {
callbacks->SHA2_sha512_inc_init(state);
if (state != NULL && callbacks != NULL && callbacks->SHA2_sha512_inc_init != NULL) {
callbacks->SHA2_sha512_inc_init(state);
}
}

void OQS_SHA2_sha512_inc_ctx_clone(OQS_SHA2_sha512_ctx *dest, const OQS_SHA2_sha512_ctx *src) {
callbacks->SHA2_sha512_inc_ctx_clone(dest, src);
if (dest != NULL && src != NULL && callbacks != NULL && callbacks->SHA2_sha512_inc_ctx_clone != NULL) {
callbacks->SHA2_sha512_inc_ctx_clone(dest, src);
}
}

void OQS_SHA2_sha512_inc_blocks(OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inblocks) {
callbacks->SHA2_sha512_inc_blocks(state, in, inblocks);
if (state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha512_inc_blocks != NULL) {
callbacks->SHA2_sha512_inc_blocks(state, in, inblocks);
}
}

void OQS_SHA2_sha512_inc_finalize(uint8_t *out, OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inlen) {
callbacks->SHA2_sha512_inc_finalize(out, state, in, inlen);
if (out != NULL && state != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha512_inc_finalize != NULL) {
callbacks->SHA2_sha512_inc_finalize(out, state, in, inlen);
}
}

void OQS_SHA2_sha512_inc_ctx_release(OQS_SHA2_sha512_ctx *state) {
callbacks->SHA2_sha512_inc_ctx_release(state);
if (state != NULL && callbacks != NULL && callbacks->SHA2_sha512_inc_ctx_release != NULL) {
callbacks->SHA2_sha512_inc_ctx_release(state);
}
}

void OQS_SHA2_sha256(uint8_t *out, const uint8_t *in, size_t inlen) {
callbacks->SHA2_sha256(out, in, inlen);
if (out != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha256 != NULL) {
callbacks->SHA2_sha256(out, in, inlen);
}
}

void OQS_SHA2_sha384(uint8_t *out, const uint8_t *in, size_t inlen) {
callbacks->SHA2_sha384(out, in, inlen);
if (out != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha384 != NULL) {
callbacks->SHA2_sha384(out, in, inlen);
}
}

void OQS_SHA2_sha512(uint8_t *out, const uint8_t *in, size_t inlen) {
callbacks->SHA2_sha512(out, in, inlen);
if (out != NULL && in != NULL && callbacks != NULL && callbacks->SHA2_sha512 != NULL) {
callbacks->SHA2_sha512(out, in, inlen);
}
}

33 changes: 33 additions & 0 deletions src/common/sha2/sha2_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ static size_t crypto_hashblocks_sha256_armv8(uint8_t *statebytes,
void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const uint8_t *in, size_t inlen) {
uint8_t padded[128];

if (state == NULL || out == NULL) {
return;
}

size_t new_inlen = state->data_len + inlen;
size_t tmp_len = new_inlen;
const uint8_t *new_in;
Expand All @@ -181,6 +185,9 @@ 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_checked_malloc(tmp_len);
if (tmp_in == NULL) {
return;
}

memcpy(tmp_in, state->data, state->data_len);
if (in && inlen) {
Expand Down Expand Up @@ -238,6 +245,10 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui
}

void oqs_sha2_sha224_inc_finalize_armv8(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen) {
if (out == NULL || state == NULL) {
return;
}

uint8_t tmp[32];
oqs_sha2_sha256_inc_finalize_armv8(tmp, (sha256ctx *)state, in, inlen);

Expand All @@ -247,6 +258,10 @@ void oqs_sha2_sha224_inc_finalize_armv8(uint8_t *out, sha224ctx *state, const ui
}

void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_t inblocks) {
if (state == NULL || in == NULL) {
return;
}

uint64_t bytes = load_bigendian_64(state->ctx + 32);
const uint8_t *new_in;
size_t buf_len = 64 * inblocks;
Expand All @@ -255,6 +270,9 @@ 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_checked_malloc(buf_len);
if (tmp_in == NULL) {
return;
}

memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, buf_len - state->data_len);
Expand All @@ -274,6 +292,10 @@ void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_
}

void oqs_sha2_sha256_inc_armv8(sha256ctx *state, const uint8_t *in, size_t len) {
if (state == NULL || in == NULL) {
return;
}

while (len) {
size_t incr = 64 - state->data_len;
if (incr > len) {
Expand Down Expand Up @@ -304,17 +326,28 @@ void oqs_sha2_sha256_inc_armv8(sha256ctx *state, const uint8_t *in, size_t len)
}

void oqs_sha2_sha224_inc_blocks_armv8(sha224ctx *state, const uint8_t *in, size_t inblocks) {
if (state == NULL || in == NULL) {
return;
}
oqs_sha2_sha256_inc_blocks_armv8((sha256ctx *)state, in, inblocks);
}

void oqs_sha2_sha256_armv8(uint8_t *out, const uint8_t *in, size_t inlen) {
if (out == NULL || in == NULL) {
return;
}

sha256ctx state;

oqs_sha2_sha256_inc_init_c(&state);
oqs_sha2_sha256_inc_finalize_armv8(out, &state, in, inlen);
}

void oqs_sha2_sha224_armv8(uint8_t *out, const uint8_t *in, size_t inlen) {
if (out == NULL || in == NULL) {
return;
}

sha224ctx state;

oqs_sha2_sha224_inc_init_c(&state);
Expand Down
Loading

0 comments on commit 9fecaa8

Please sign in to comment.