Skip to content

Commit

Permalink
use function pointer instead
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 27, 2024
1 parent e136b83 commit fbde971
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ static SEXP hash_to_char(const unsigned char *buf, const size_t sz) {

}

static void clear_buffer(void *buf, size_t sz) {

void *(*volatile secure_memset)(void *, int, size_t) = memset;
secure_memset(buf, 0, sz);

}

static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits,
const SEXP convert, const int file) {

Expand Down Expand Up @@ -261,11 +268,10 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits,

if ((fp = fopen(filepath, "rb")) == NULL)
Rf_error("file not found or no read permission at '%s'", filepath);
while ((cur = fread(buf, 1, sizeof(buf), fp))) {
while ((cur = fread(buf, sizeof(char), SB_BUF_SIZE, fp))) {
mbedtls_sha3_update(&ctx, buf, cur);
}
memset(&buf, 0, SB_BUF_SIZE);
CHECK_MEMORY_INTEGRITY(&buf);
clear_buffer(&buf, SB_BUF_SIZE);
if (ferror(fp)) {
fclose(fp);
Rf_error("file read error at '%s'", filepath);
Expand Down Expand Up @@ -311,8 +317,7 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits,

finish:
mbedtls_sha3_finish(&ctx, output, outlen);
memset(&ctx, 0, sizeof(mbedtls_sha3_context));
CHECK_MEMORY_INTEGRITY(&ctx);
clear_buffer(&ctx, sizeof(mbedtls_sha3_context));

switch (conv) {
case 0:
Expand Down
1 change: 0 additions & 1 deletion src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#define SB_R_SERIAL_VER 3
#define SB_SERIAL_HEADERS 6
#define SB_BUF_SIZE 4096
#define CHECK_MEMORY_INTEGRITY(x) if (*(int *) x) Rf_error("memory corruption")

typedef enum {
MBEDTLS_SHA3_SHAKE256 = 0,
Expand Down

0 comments on commit fbde971

Please sign in to comment.