Skip to content

Commit

Permalink
use offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Apr 25, 2024
1 parent c95c001 commit f4029f8
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ typedef enum {
MBEDTLS_SHA3_224,
MBEDTLS_SHA3_256,
MBEDTLS_SHA3_384,
MBEDTLS_SHA3_512,
MBEDTLS_KECCAK_224,
MBEDTLS_KECCAK_256,
MBEDTLS_KECCAK_384,
MBEDTLS_KECCAK_512,
MBEDTLS_SHA3_512
} mbedtls_sha3_id;

typedef struct mbedtls_sha3_family_functions {
Expand All @@ -57,10 +53,10 @@ static mbedtls_sha3_family_functions sha3_families[] = {
{ MBEDTLS_SHA3_256, 1088, 256, 0x06 },
{ MBEDTLS_SHA3_384, 832, 384, 0x06 },
{ MBEDTLS_SHA3_512, 576, 512, 0x06 },
{ MBEDTLS_KECCAK_224, 1152, 224, 0x01 },
{ MBEDTLS_KECCAK_256, 1088, 256, 0x01 },
{ MBEDTLS_KECCAK_384, 832, 384, 0x01 },
{ MBEDTLS_KECCAK_512, 576, 512, 0x01 }
{ MBEDTLS_SHA3_224, 1152, 224, 0x01 },
{ MBEDTLS_SHA3_256, 1088, 256, 0x01 },
{ MBEDTLS_SHA3_384, 832, 384, 0x01 },
{ MBEDTLS_SHA3_512, 576, 512, 0x01 }
};

static const uint64_t rc[24] = {
Expand Down Expand Up @@ -321,28 +317,28 @@ SEXP hash_to_sexp(unsigned char *buf, size_t sz, int conv) {

static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits, const SEXP convert,
void (*const hash_func)(mbedtls_sha3_context *, SEXP),
const int type) {
const int offset) {

const int conv = LOGICAL(convert)[0];
const int bt = Rf_asInteger(bits);
mbedtls_sha3_id id;

if (type) {
if (offset < 0) {
id = MBEDTLS_SHA3_SHAKE256;
} else {
switch(bt) {
case 256:
id = type > 0 ? MBEDTLS_SHA3_256 : MBEDTLS_KECCAK_256; break;
id = MBEDTLS_SHA3_256 + offset; break;
case 512:
id = type > 0 ? MBEDTLS_SHA3_512 : MBEDTLS_KECCAK_512; break;
id = MBEDTLS_SHA3_512 + offset; break;
case 224:
id = type > 0 ? MBEDTLS_SHA3_224 : MBEDTLS_KECCAK_224; break;
id = MBEDTLS_SHA3_224 + offset; break;
case 384:
id = type > 0 ? MBEDTLS_SHA3_384 : MBEDTLS_KECCAK_384; break;
id = MBEDTLS_SHA3_384 + offset; break;
default:
if (type < 0) Rf_error("'bits' must be 224, 256, 384 or 512");
if (offset) Rf_error("'bits' must be 224, 256, 384 or 512");
id = MBEDTLS_SHA3_SHAKE256;
}
} else {
id = MBEDTLS_SHA3_SHAKE256;
}

if (bt < 8 || bt > (1 << 24))
Expand All @@ -365,36 +361,36 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits, const SEXP conve

SEXP secretbase_sha3(SEXP x, SEXP bits, SEXP convert) {

return secretbase_sha3_impl(x, bits, convert, hash_object, 1);
return secretbase_sha3_impl(x, bits, convert, hash_object, 0);

}

SEXP secretbase_sha3_file(SEXP x, SEXP bits, SEXP convert) {

return secretbase_sha3_impl(x, bits, convert, hash_file, 1);
return secretbase_sha3_impl(x, bits, convert, hash_file, 0);

}

SEXP secretbase_shake256(SEXP x, SEXP bits, SEXP convert) {

return secretbase_sha3_impl(x, bits, convert, hash_object, 0);
return secretbase_sha3_impl(x, bits, convert, hash_object, -1);

}

SEXP secretbase_shake256_file(SEXP x, SEXP bits, SEXP convert) {

return secretbase_sha3_impl(x, bits, convert, hash_file, 0);
return secretbase_sha3_impl(x, bits, convert, hash_file, -1);

}

SEXP secretbase_keccak(SEXP x, SEXP bits, SEXP convert) {

return secretbase_sha3_impl(x, bits, convert, hash_object, -1);
return secretbase_sha3_impl(x, bits, convert, hash_object, 4);

}

SEXP secretbase_keccak_file(SEXP x, SEXP bits, SEXP convert) {

return secretbase_sha3_impl(x, bits, convert, hash_file, -1);
return secretbase_sha3_impl(x, bits, convert, hash_file, 4);

}

0 comments on commit f4029f8

Please sign in to comment.