13 #define SHA3_ASSERT(x) 15 #define SHA3_TRACE(format, ...) 16 #define SHA3_TRACE_BUF(format, buf, l, ...) 18 #define SHA3_TRACE(format, args...) 19 #define SHA3_TRACE_BUF(format, buf, l, args...) 25 #define SHA3_USE_KECCAK_FLAG 0x80000000 26 #define SHA3_CW(x) ((x) & (~SHA3_USE_KECCAK_FLAG)) 29 #define SHA3_CONST(x) x 31 #define SHA3_CONST(x) x##L 35 #define SHA3_ROTL64(x, y) \ 36 (((x) << (y)) | ((x) >> ((sizeof(uint64_t) * 8) - (y)))) 53 static const unsigned keccakf_rotc[24] = {1, 3, 6, 10, 15, 21, 28, 36,
54 45, 55, 2, 14, 27, 41, 56, 8,
55 25, 43, 62, 18, 39, 61, 20, 44};
57 static const unsigned keccakf_piln[24] = {10, 7, 11, 17, 18, 3, 5, 16,
58 8, 21, 24, 4, 15, 23, 19, 13,
59 12, 2, 20, 14, 22, 9, 6, 1};
67 #define KECCAK_ROUNDS 24 69 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ 70 for (i = 0; i < 25; i++) {
77 for (i = 0; i < 5; i++)
78 bc[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20];
80 for (i = 0; i < 5; i++) {
81 t = bc[(i + 4) % 5] ^
SHA3_ROTL64(bc[(i + 1) % 5], 1);
82 for (j = 0; j < 25; j += 5) s[j + i] ^= t;
87 for (i = 0; i < 24; i++) {
95 for (j = 0; j < 25; j += 5) {
96 for (i = 0; i < 5; i++) bc[i] = s[j + i];
97 for (i = 0; i < 5; i++) s[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
104 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ 105 for (i = 0; i < 25; i++) {
116 if (bitSize != 256 && bitSize != 384 && bitSize != 512)
118 memset(ctx, 0,
sizeof(*ctx));
146 unsigned old_tail = (8 - ctx->
byteIndex) & 7;
152 const uint8_t *buf = bufIn;
159 if (len < old_tail) {
161 SHA3_TRACE(
"because %d<%d, store it and return",
172 SHA3_TRACE(
"completing one word with %d bytes", (
unsigned)old_tail);
194 words = len /
sizeof(uint64_t);
195 tail = len - words *
sizeof(uint64_t);
197 SHA3_TRACE(
"have %d full words to process", (
unsigned)words);
199 for (i = 0; i < words; i++, buf +=
sizeof(uint64_t)) {
201 (uint64_t)(buf[0]) | ((uint64_t)(buf[1]) << 8 * 1)
202 | ((uint64_t)(buf[2]) << 8 * 2) | ((uint64_t)(buf[3]) << 8 * 3)
203 | ((uint64_t)(buf[4]) << 8 * 4) | ((uint64_t)(buf[5]) << 8 * 5)
204 | ((uint64_t)(buf[6]) << 8 * 6) | ((uint64_t)(buf[7]) << 8 * 7);
205 #if defined(__x86_64__) || defined(__i386__) 216 SHA3_TRACE(
"have %d bytes left to process, save them", (
unsigned)tail);
221 SHA3_TRACE(
"Store byte %02x '%c'", *buf, *buf);
246 t = (uint64_t)(((uint64_t)1) << (ctx->
byteIndex * 8));
249 t = (uint64_t)(((uint64_t)(0x02 | (1 << 2)))
268 const unsigned t1 = (uint32_t)ctx->s[i];
269 const unsigned t2 = (uint32_t)((ctx->s[i] >> 16) >> 16);
270 ctx->sb[i * 8 + 0] = (uint8_t)(t1);
271 ctx->sb[i * 8 + 1] = (uint8_t)(t1 >> 8);
272 ctx->sb[i * 8 + 2] = (uint8_t)(t1 >> 16);
273 ctx->sb[i * 8 + 3] = (uint8_t)(t1 >> 24);
274 ctx->sb[i * 8 + 4] = (uint8_t)(t2);
275 ctx->sb[i * 8 + 5] = (uint8_t)(t2 >> 8);
276 ctx->sb[i * 8 + 6] = (uint8_t)(t2 >> 16);
277 ctx->sb[i * 8 + 7] = (uint8_t)(t2 >> 24);
303 if (outBytes > bitSize / 8) outBytes = bitSize / 8;
304 memcpy(out, h, outBytes);
void sha3_Init512(void *priv)
#define SHA3_TRACE(format, args...)
sha3_return_t sha3_HashBuffer(unsigned bitSize, enum SHA3_FLAGS flags, const void *in, unsigned inBytes, void *out, unsigned outBytes)
#define SHA3_KECCAK_SPONGE_WORDS
#define SHA3_USE_KECCAK_FLAG
void keccakf(uint64_t s[25])
void sha3_Init256(void *priv)
#define SHA3_ROTL64(x, y)
enum SHA3_FLAGS sha3_SetFlags(void *priv, enum SHA3_FLAGS flags)
sha3_return_t sha3_Init(void *priv, unsigned bitSize)
enum SHA3_RETURN sha3_return_t
void sha3_Update(void *priv, void const *bufIn, size_t len)
static const unsigned keccakf_piln[24]
void sha3_Init384(void *priv)
static const uint64_t keccakf_rndc[24]
static const unsigned keccakf_rotc[24]
#define SHA3_TRACE_BUF(format, buf, l, args...)
void const * sha3_Finalize(void *priv)