Skip to content

Commit

Permalink
Use OQS_MEM_secure_free instead of free
Browse files Browse the repository at this point in the history
  • Loading branch information
ashman-p committed Nov 26, 2023
1 parent ce1db1b commit 5377388
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/common/sha2/sha2_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui
uint8_t padded[128];

size_t new_inlen = state->data_len + inlen;
size_t tmp_len = new_inlen;
const uint8_t *new_in;
uint8_t *tmp_in = NULL;

if (new_inlen == inlen) {
new_in = in;
} else { //Combine incremental data with final input
tmp_in = malloc(new_inlen);
tmp_in = malloc(tmp_len);
size_t i, j;
for (i = 0; i < state->data_len; ++i) {
tmp_in[i] = state->data[i];
Expand Down Expand Up @@ -234,7 +235,7 @@ void oqs_sha2_sha256_inc_finalize_armv8(uint8_t *out, sha256ctx *state, const ui
out[i] = state->ctx[i];
}
oqs_sha2_sha256_inc_ctx_release_c(state);
free(tmp_in); // IGNORE free-check
OQS_MEM_secure_free(tmp_in, tmp_len);
}

void oqs_sha2_sha224_inc_finalize_armv8(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen) {
Expand All @@ -249,11 +250,11 @@ 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) {
uint64_t bytes = load_bigendian_64(state->ctx + 32);
const uint8_t *new_in;
size_t buf_len = 64 * inblocks;
uint8_t *tmp_in = NULL;

/* Process any existing incremental data first */
if (state->data_len) {
size_t buf_len = 64 * inblocks;
tmp_in = malloc(buf_len);
memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, buf_len - state->data_len);
Expand All @@ -269,7 +270,7 @@ void oqs_sha2_sha256_inc_blocks_armv8(sha256ctx *state, const uint8_t *in, size_
bytes += 64 * inblocks;

store_bigendian_64(state->ctx + 32, bytes);
free(tmp_in); // IGNORE free-check
OQS_MEM_secure_free(tmp_in, buf_len);
}

void oqs_sha2_sha256_inc_armv8(sha256ctx *state, const uint8_t *in, size_t len) {
Expand Down
9 changes: 5 additions & 4 deletions src/common/sha2/sha2_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,12 @@ void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) {

void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) {
uint64_t bytes = load_bigendian_64(state->ctx + 32);
size_t tmp_buflen = 64 * inblocks;
const uint8_t *new_in;
uint8_t *tmp_in = NULL;

/* Process any existing incremental data first */
if (state->data_len) {
size_t tmp_buflen = 64 * inblocks;
tmp_in = malloc(tmp_buflen);
memcpy(tmp_in, state->data, state->data_len);
memcpy(tmp_in + state->data_len, in, tmp_buflen - state->data_len);
Expand All @@ -653,7 +653,7 @@ void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t in
bytes += 64 * inblocks;

store_bigendian_64(state->ctx + 32, bytes);
free(tmp_in); // IGNORE free-check
OQS_MEM_secure_free(tmp_in, tmp_buflen);
}

void oqs_sha2_sha256_inc_c(sha256ctx *state, const uint8_t *in, size_t len) {
Expand Down Expand Up @@ -709,13 +709,14 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_
uint8_t padded[128];

size_t new_inlen = state->data_len + inlen;
size_t tmp_len = new_inlen;
const uint8_t *new_in;
uint8_t *tmp_in = NULL;

if (new_inlen == inlen) {
new_in = in;
} else { //Combine incremental data with final input
tmp_in = malloc(new_inlen);
tmp_in = malloc(tmp_len);
size_t i, j;
for (i = 0; i < state->data_len; ++i) {
tmp_in[i] = state->data[i];
Expand Down Expand Up @@ -770,7 +771,7 @@ void oqs_sha2_sha256_inc_finalize_c(uint8_t *out, sha256ctx *state, const uint8_
out[i] = state->ctx[i];
}
oqs_sha2_sha256_inc_ctx_release_c(state);
free(tmp_in); // IGNORE free-check
OQS_MEM_secure_free(tmp_in, tmp_len);
}

void oqs_sha2_sha224_inc_finalize_c(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen) {
Expand Down

0 comments on commit 5377388

Please sign in to comment.