update
This commit is contained in:
32
main.c
32
main.c
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include "sha3.h"
|
#include "sha3.h"
|
||||||
|
|
||||||
// read a hex string, return byte length or -1 on error.
|
// read a hex string, return byte length or -1 on error.
|
||||||
@@ -153,11 +154,42 @@ int test_shake()
|
|||||||
return fails;
|
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
|
// main
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (test_sha3() == 0 && test_shake() == 0)
|
if (test_sha3() == 0 && test_shake() == 0)
|
||||||
printf("FIPS 202 / SHA3, SHAKE128, SHAKE256 Self-Tests OK!\n");
|
printf("FIPS 202 / SHA3, SHAKE128, SHAKE256 Self-Tests OK!\n");
|
||||||
|
test_speed();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
12
sha3.c
12
sha3.c
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
// update the state with given number of rounds
|
// 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
|
// constants
|
||||||
const uint64_t keccakf_rndc[24] = {
|
const uint64_t keccakf_rndc[24] = {
|
||||||
@@ -48,7 +48,7 @@ static void sha3_keccakf(uint64_t st[25], int rounds)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// actual iteration
|
// actual iteration
|
||||||
for (r = 0; r < rounds; r++) {
|
for (r = 0; r < KECCAKF_ROUNDS; r++) {
|
||||||
|
|
||||||
// Theta
|
// Theta
|
||||||
for (i = 0; i < 5; i++)
|
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++) {
|
for (i = 0; i < len; i++) {
|
||||||
c->st.b[j++] ^= ((const uint8_t *) data)[i];
|
c->st.b[j++] ^= ((const uint8_t *) data)[i];
|
||||||
if (j >= c->rsiz) {
|
if (j >= c->rsiz) {
|
||||||
sha3_keccakf(c->st.q, KECCAKF_ROUNDS);
|
sha3_keccakf(c->st.q);
|
||||||
j = 0;
|
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->pt] ^= 0x06;
|
||||||
c->st.b[c->rsiz - 1] ^= 0x80;
|
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++) {
|
for (i = 0; i < c->mdlen; i++) {
|
||||||
((uint8_t *) md)[i] = c->st.b[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->pt] ^= 0x1F;
|
||||||
c->st.b[c->rsiz - 1] ^= 0x80;
|
c->st.b[c->rsiz - 1] ^= 0x80;
|
||||||
sha3_keccakf(c->st.q, KECCAKF_ROUNDS);
|
sha3_keccakf(c->st.q);
|
||||||
c->pt = 0;
|
c->pt = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +181,7 @@ void shake_out(sha3_ctx_t *c, void *out, size_t len)
|
|||||||
j = c->pt;
|
j = c->pt;
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (j >= c->rsiz) {
|
if (j >= c->rsiz) {
|
||||||
sha3_keccakf(c->st.q, KECCAKF_ROUNDS);
|
sha3_keccakf(c->st.q);
|
||||||
j = 0;
|
j = 0;
|
||||||
}
|
}
|
||||||
((uint8_t *) out)[i] = c->st.b[j++];
|
((uint8_t *) out)[i] = c->st.b[j++];
|
||||||
|
|||||||
3
sha3.h
3
sha3.h
@@ -24,6 +24,9 @@ typedef struct {
|
|||||||
int pt, rsiz, mdlen; // these don't overflow
|
int pt, rsiz, mdlen; // these don't overflow
|
||||||
} sha3_ctx_t;
|
} sha3_ctx_t;
|
||||||
|
|
||||||
|
// Compression function.
|
||||||
|
void sha3_keccakf(uint64_t st[25]);
|
||||||
|
|
||||||
// OpenSSL - like interfece
|
// OpenSSL - like interfece
|
||||||
int sha3_init(sha3_ctx_t *c, int mdlen); // mdlen = hash output in bytes
|
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_update(sha3_ctx_t *c, const void *data, size_t len);
|
||||||
|
|||||||
Reference in New Issue
Block a user