Skip to content

Commit

Permalink
integrate further and remove redundant checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 21, 2024
1 parent 6092cae commit 337e597
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 44 deletions.
50 changes: 7 additions & 43 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,45 +151,26 @@ static void keccak_f1600(mbedtls_sha3_context *ctx) {
}

static void mbedtls_sha3_init(mbedtls_sha3_context *ctx) {

if (ctx == NULL)
return;


memset(ctx, 0, sizeof(mbedtls_sha3_context));

}

static void mbedtls_sha3_free(mbedtls_sha3_context *ctx) {
static void mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id) {

if (ctx == NULL)
return;
mbedtls_sha3_family_functions *p;

memset(ctx, 0, sizeof(mbedtls_sha3_context));

}

static int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id) {

mbedtls_sha3_family_functions *p = NULL;
if (ctx == NULL)
return -1;

for (p = sha3_families; p->id != MBEDTLS_SHA3_NONE; p++) {
if (p->id == id)
break;
}

if (p == NULL)
return -1;

ctx->id = id;
ctx->r = p->r;
ctx->olen = p->olen / 8;
ctx->xor_byte = p->xor_byte;
ctx->max_block_size = ctx->r / 8;

return 0;

}

static void mbedtls_sha3_update(mbedtls_sha3_context *ctx,
Expand All @@ -204,16 +185,11 @@ static void mbedtls_sha3_update(mbedtls_sha3_context *ctx,
keccak_f1600(ctx);
}

return;

}

static int mbedtls_sha3_finish(mbedtls_sha3_context *ctx,
uint8_t *output, size_t olen) {
static void mbedtls_sha3_finish(mbedtls_sha3_context *ctx,
uint8_t *output, size_t olen) {

if (ctx->olen > 0 && ctx->olen != olen)
return -1;

ABSORB(ctx, ctx->index, ctx->xor_byte);
ABSORB(ctx, ctx->max_block_size - 1, 0x80);
keccak_f1600(ctx);
Expand All @@ -225,8 +201,6 @@ static int mbedtls_sha3_finish(mbedtls_sha3_context *ctx,
keccak_f1600(ctx);
}

return 0;

}

// secretbase - internal auxiliary functions -----------------------------------
Expand Down Expand Up @@ -272,7 +246,6 @@ SEXP secretbase_sha3(SEXP x, SEXP size, SEXP convert) {
unsigned char output[outlen];
mbedtls_sha3_id id;
SEXP out;
int xc;

switch (bits) {
case 224:
Expand All @@ -289,9 +262,7 @@ SEXP secretbase_sha3(SEXP x, SEXP size, SEXP convert) {

mbedtls_sha3_context ctx;
mbedtls_sha3_init(&ctx);

if ((xc = mbedtls_sha3_starts(&ctx, id)))
goto exit;
mbedtls_sha3_starts(&ctx, id);

switch (TYPEOF(x)) {
case STRSXP:
Expand Down Expand Up @@ -328,8 +299,7 @@ SEXP secretbase_sha3(SEXP x, SEXP size, SEXP convert) {

finish:

if ((xc = mbedtls_sha3_finish(&ctx, output, outlen)))
goto exit;
mbedtls_sha3_finish(&ctx, output, outlen);

switch (conv) {
case 0:
Expand All @@ -343,13 +313,7 @@ SEXP secretbase_sha3(SEXP x, SEXP size, SEXP convert) {
out = Rf_allocVector(INTSXP, outlen / sizeof(int));
memcpy(STDVEC_DATAPTR(out), output, outlen);
}

mbedtls_sha3_free(&ctx);

return out;

exit:
mbedtls_sha3_free(&ctx);
Rf_error("hashing encountered error");

}
3 changes: 2 additions & 1 deletion tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ test_equal(sha3(sha3("secret base", size = 32, convert = FALSE), size = 32), "4d
test_that(sha3(rnorm(1e5), size = 8196), is.character)
test_equal(sha3("secret base", size = 32, convert = NA), -1044750695L)

# SHA-3 streaming serialization tests:
# Streaming serialization tests:
test_equal(sha3(data.frame(a = 1, b = 2)), "05d4308e79d029b4af5604739ecc6c4efa1f602a23add0ed2d247b7407d4832f")
test_equal(sha3(c("secret", "base")), "d906024c71828a10e28865a80f5e81d2cb5cd74067d44852d7039813ba62b0b6")
test_equal(sha3(NULL), "b3e37e4c5def1bfb2841b79ef8503b83d1fed46836b5b913d7c16de92966dcee")
test_equal(sha3(substitute()), "9d31eb41cfb721b8040c52d574df1aacfc381d371c2b933f90792beba5160a57")
test_equal(sha3(`class<-`(sha3(character(), size = 192, convert = FALSE), "hash"), size = "32", convert = NA), -111175135L)

# Error handling tests:
test_error(sha3("secret base", size = 0), "'size' must be between 8 and 2^24")
Expand Down

0 comments on commit 337e597

Please sign in to comment.