Skip to content

Commit

Permalink
Drop tiny_sha3 directory, cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Mar 26, 2022
1 parent 6cb6954 commit bf0dafe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
3 changes: 1 addition & 2 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ coverage-lcov:
'*/Modules/_blake2/impl/*' \
'*/Modules/_ctypes/libffi*/*' \
'*/Modules/_decimal/libmpdec/*' \
'*/Modules/_sha3/kcp/*' \
'*/Modules/expat/*' \
'*/Modules/zlib/*' \
'*/Include/*' \
Expand Down Expand Up @@ -2491,7 +2490,7 @@ MODULE__IO_DEPS=$(srcdir)/Modules/_io/_iomodule.h
MODULE__MD5_DEPS=$(srcdir)/Modules/hashlib.h
MODULE__SHA1_DEPS=$(srcdir)/Modules/hashlib.h
MODULE__SHA256_DEPS=$(srcdir)/Modules/hashlib.h
MODULE__SHA3_DEPS=$(srcdir)/Modules/_sha3/kcp/KeccakHash.c $(srcdir)/Modules/_sha3/kcp/KeccakHash.h $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-64.macros $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-SnP-opt32.h $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-SnP-opt64.h $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-SnP.h $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-inplace32BI.c $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-opt64-config.h $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-opt64.c $(srcdir)/Modules/_sha3/kcp/KeccakP-1600-unrolling.macros $(srcdir)/Modules/_sha3/kcp/KeccakSponge.c $(srcdir)/Modules/_sha3/kcp/KeccakSponge.h $(srcdir)/Modules/_sha3/kcp/KeccakSponge.inc $(srcdir)/Modules/_sha3/kcp/PlSnP-Fallback.inc $(srcdir)/Modules/_sha3/kcp/SnP-Relaned.h $(srcdir)/Modules/_sha3/kcp/align.h $(srcdir)/Modules/hashlib.h
MODULE__SHA3_DEPS=$(srcdir)/Modules/_sha3/sha3.c $(srcdir)/Modules/_sha3/sha3.h $(srcdir)/Modules/hashlib.h
MODULE__SHA512_DEPS=$(srcdir)/Modules/hashlib.h
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h
MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data.h $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions Modules/_sha3/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ tiny_sha3

https://github.com/mjosaarinen/tiny_sha3
commit dcbb3192047c2a721f5f851db591871d428036a9

- All functions have been converted to static functions.
- sha3() function is commented out.
14 changes: 8 additions & 6 deletions Modules/_sha3/tiny_sha3/sha3.c → Modules/_sha3/sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// update the state with given number of rounds

void sha3_keccakf(uint64_t st[25])
static void sha3_keccakf(uint64_t st[25])
{
// constants
const uint64_t keccakf_rndc[24] = {
Expand Down Expand Up @@ -100,7 +100,7 @@ void sha3_keccakf(uint64_t st[25])

// Initialize the context for SHA3

int sha3_init(sha3_ctx_t *c, int mdlen)
static int sha3_init(sha3_ctx_t *c, int mdlen)
{
int i;

Expand All @@ -115,7 +115,7 @@ int sha3_init(sha3_ctx_t *c, int mdlen)

// update state with more data

int sha3_update(sha3_ctx_t *c, const void *data, size_t len)
static int sha3_update(sha3_ctx_t *c, const void *data, size_t len)
{
size_t i;
int j;
Expand All @@ -135,7 +135,7 @@ int sha3_update(sha3_ctx_t *c, const void *data, size_t len)

// finalize and output a hash

int sha3_final(void *md, sha3_ctx_t *c)
static int sha3_final(void *md, sha3_ctx_t *c)
{
int i;

Expand All @@ -150,6 +150,7 @@ int sha3_final(void *md, sha3_ctx_t *c)
return 1;
}

#if 0
// compute a SHA-3 hash (md) of given byte length from "in"

void *sha3(const void *in, size_t inlen, void *md, int mdlen)
Expand All @@ -162,18 +163,19 @@ void *sha3(const void *in, size_t inlen, void *md, int mdlen)

return md;
}
#endif

// SHAKE128 and SHAKE256 extensible-output functionality

void shake_xof(sha3_ctx_t *c)
static void shake_xof(sha3_ctx_t *c)
{
c->st.b[c->pt] ^= 0x1F;
c->st.b[c->rsiz - 1] ^= 0x80;
sha3_keccakf(c->st.q);
c->pt = 0;
}

void shake_out(sha3_ctx_t *c, void *out, size_t len)
static void shake_out(sha3_ctx_t *c, void *out, size_t len)
{
size_t i;
int j;
Expand Down
16 changes: 9 additions & 7 deletions Modules/_sha3/tiny_sha3/sha3.h → Modules/_sha3/sha3.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ typedef struct {
} sha3_ctx_t;

// Compression function.
void sha3_keccakf(uint64_t st[25]);
static void sha3_keccakf(uint64_t st[25]);

// OpenSSL - like interfece
int sha3_init(sha3_ctx_t *c, int mdlen); // mdlen = hash output in bytes
int sha3_update(sha3_ctx_t *c, const void *data, size_t len);
int sha3_final(void *md, sha3_ctx_t *c); // digest goes to md
static int sha3_init(sha3_ctx_t *c, int mdlen); // mdlen = hash output in bytes
static int sha3_update(sha3_ctx_t *c, const void *data, size_t len);
static int sha3_final(void *md, sha3_ctx_t *c); // digest goes to md

// compute a sha3 hash (md) of given byte length from "in"
void *sha3(const void *in, size_t inlen, void *md, int mdlen);
#if 0
static void *sha3(const void *in, size_t inlen, void *md, int mdlen);
#endif

// SHAKE128 and SHAKE256 extensible-output functions
#define shake128_init(c) sha3_init(c, 16)
#define shake256_init(c) sha3_init(c, 32)
#define shake_update sha3_update

void shake_xof(sha3_ctx_t *c);
void shake_out(sha3_ctx_t *c, void *out, size_t len);
static void shake_xof(sha3_ctx_t *c);
static void shake_out(sha3_ctx_t *c, void *out, size_t len);

#endif

45 changes: 18 additions & 27 deletions Modules/_sha3/sha3module.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Trevor Perrin ([email protected])
* Gregory P. Smith ([email protected])
*
* Copyright (C) 2012-2016 Christian Heimes ([email protected])
* Copyright (C) 2012-2022 Christian Heimes ([email protected])
* Licensed to PSF under a Contributor Agreement.
*
*/
Expand All @@ -23,7 +23,7 @@
#include "pycore_strhex.h" // _Py_strhex()
#include "../hashlib.h"

#include "tiny_sha3/sha3.c"
#include "sha3.c"

#define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
#define SHA3_LANESIZE 0
Expand All @@ -34,16 +34,8 @@
#define SHA3_squeeze(state, out, len) shake_xof(state), shake_out(state, out, len)
#define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state))

#define Keccak_HashInitialize_SHA3_224(state) sha3_init(state, 28)
#define Keccak_HashInitialize_SHA3_256(state) sha3_init(state, 32)
#define Keccak_HashInitialize_SHA3_384(state) sha3_init(state, 48)
#define Keccak_HashInitialize_SHA3_512(state) sha3_init(state, 64)
#define Keccak_HashInitialize_SHAKE128(state) shake128_init(state)
#define Keccak_HashInitialize_SHAKE256(state) shake256_init(state)

// no optimization
#define KeccakOpt 0
#define KeccakP1600_implementation "tiny_sha3"

typedef enum { SUCCESS = 1, FAIL = 0, BAD_HASHLEN = 2 } HashReturn;

Expand Down Expand Up @@ -123,17 +115,17 @@ py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity)
assert(state != NULL);

if (type == state->sha3_224_type) {
res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
res = sha3_init(&self->hash_state, 28);
} else if (type == state->sha3_256_type) {
res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
res = sha3_init(&self->hash_state, 32);
} else if (type == state->sha3_384_type) {
res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
res = sha3_init(&self->hash_state, 48);
} else if (type == state->sha3_512_type) {
res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
res = sha3_init(&self->hash_state, 64);
} else if (type == state->shake_128_type) {
res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
res = sha3_init(&self->hash_state, 16);
} else if (type == state->shake_256_type) {
res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
res = sha3_init(&self->hash_state, 32);
} else {
PyErr_BadInternalCall();
goto error;
Expand Down Expand Up @@ -392,15 +384,7 @@ SHA3_get_rate_bits(SHA3object *self, void *closure)
static PyObject *
SHA3_get_suffix(SHA3object *self, void *closure)
{
PyTypeObject *type = Py_TYPE(self);
SHA3State *state = PyType_GetModuleState(type);
unsigned char suffix[2];
if ((type == state->shake_128_type) || (type == state->shake_256_type)) {
suffix[0] = 0x1f;
} else {
suffix[0] = 0x06;
}
suffix[1] = 0;
unsigned char suffix[2] = {0x06, 0};
return PyBytes_FromStringAndSize((const char *)suffix, 1);
}

Expand Down Expand Up @@ -542,14 +526,21 @@ SHAKE_get_digest_size(SHA3object *self, void *closure)
return PyLong_FromLong(0);
}

static PyObject *
SHAKE_get_suffix(SHA3object *self, void *closure)
{
unsigned char suffix[2] = {0x1f, 0};
return PyBytes_FromStringAndSize((const char *)suffix, 1);
}


static PyGetSetDef SHAKE_getseters[] = {
{"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
{"name", (getter)SHA3_get_name, NULL, NULL, NULL},
{"digest_size", (getter)SHAKE_get_digest_size, NULL, NULL, NULL},
{"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
{"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
{"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL},
{"_suffix", (getter)SHAKE_get_suffix, NULL, NULL, NULL},
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -640,7 +631,7 @@ _sha3_exec(PyObject *m)
return -1;
}
if (PyModule_AddStringConstant(m, "implementation",
KeccakP1600_implementation) < 0) {
"tiny_sha3") < 0) {
return -1;
}

Expand Down

0 comments on commit bf0dafe

Please sign in to comment.