diff --git a/main.c b/main.c index 6d45a16..674ff2e 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include +#include #include "sha3.h" // read a hex string, return byte length or -1 on error. @@ -153,11 +154,42 @@ int test_shake() return fails; } +// test speed of the comp + +void test_speed() +{ + int i; + uint64_t st[25], x, n; + clock_t bg, us; + + for (i = 0; i < 25; i++) + st[i] = i; + + bg = clock(); + n = 0; + do { + for (i = 0; i < 100000; i++) + sha3_keccakf(st); + n += i; + us = clock() - bg; + } while (us < 3 * CLOCKS_PER_SEC); + + x = 0; + for (i = 0; i < 25; i++) + x += st[i]; + + printf("(%016lX) %.3f Keccak-p[1600,24] / Second.\n", + x, (CLOCKS_PER_SEC * ((double) n)) / ((double) us)); + + +} + // main int main(int argc, char **argv) { if (test_sha3() == 0 && test_shake() == 0) printf("FIPS 202 / SHA3, SHAKE128, SHAKE256 Self-Tests OK!\n"); + test_speed(); return 0; } diff --git a/sha3.c b/sha3.c index 3d6a7fe..931ae02 100644 --- a/sha3.c +++ b/sha3.c @@ -8,7 +8,7 @@ // update the state with given number of rounds -static void sha3_keccakf(uint64_t st[25], int rounds) +void sha3_keccakf(uint64_t st[25]) { // constants const uint64_t keccakf_rndc[24] = { @@ -48,7 +48,7 @@ static void sha3_keccakf(uint64_t st[25], int rounds) #endif // actual iteration - for (r = 0; r < rounds; r++) { + for (r = 0; r < KECCAKF_ROUNDS; r++) { // Theta for (i = 0; i < 5; i++) @@ -124,7 +124,7 @@ int sha3_update(sha3_ctx_t *c, const void *data, size_t len) for (i = 0; i < len; i++) { c->st.b[j++] ^= ((const uint8_t *) data)[i]; if (j >= c->rsiz) { - sha3_keccakf(c->st.q, KECCAKF_ROUNDS); + sha3_keccakf(c->st.q); j = 0; } } @@ -141,7 +141,7 @@ int sha3_final(void *md, sha3_ctx_t *c) c->st.b[c->pt] ^= 0x06; c->st.b[c->rsiz - 1] ^= 0x80; - sha3_keccakf(c->st.q, KECCAKF_ROUNDS); + sha3_keccakf(c->st.q); for (i = 0; i < c->mdlen; i++) { ((uint8_t *) md)[i] = c->st.b[i]; @@ -169,7 +169,7 @@ 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, KECCAKF_ROUNDS); + sha3_keccakf(c->st.q); c->pt = 0; } @@ -181,7 +181,7 @@ void shake_out(sha3_ctx_t *c, void *out, size_t len) j = c->pt; for (i = 0; i < len; i++) { if (j >= c->rsiz) { - sha3_keccakf(c->st.q, KECCAKF_ROUNDS); + sha3_keccakf(c->st.q); j = 0; } ((uint8_t *) out)[i] = c->st.b[j++]; diff --git a/sha3.h b/sha3.h index 02b1e9b..ba24f43 100644 --- a/sha3.h +++ b/sha3.h @@ -24,6 +24,9 @@ typedef struct { int pt, rsiz, mdlen; // these don't overflow } sha3_ctx_t; +// Compression function. +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);