Path: blob/master/crypto/c_keccak.c
1299 views
// keccak.c1// 19-Nov-11 Markku-Juhani O. Saarinen <[email protected]>2// A baseline Keccak (3rd round) implementation.34#include "hash-ops.h"5#include "c_keccak.h"67const uint64_t keccakf_rndc[24] =8{90x0000000000000001, 0x0000000000008082, 0x800000000000808a,100x8000000080008000, 0x000000000000808b, 0x0000000080000001,110x8000000080008081, 0x8000000000008009, 0x000000000000008a,120x0000000000000088, 0x0000000080008009, 0x000000008000000a,130x000000008000808b, 0x800000000000008b, 0x8000000000008089,140x8000000000008003, 0x8000000000008002, 0x8000000000000080,150x000000000000800a, 0x800000008000000a, 0x8000000080008081,160x8000000000008080, 0x0000000080000001, 0x800000008000800817};1819const int keccakf_rotc[24] =20{211, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,2227, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 4423};2425const int keccakf_piln[24] =26{2710, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,2815, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 129};3031// update the state with given number of rounds3233void keccakf(uint64_t st[25], int rounds)34{35int i, j, round;36uint64_t t, bc[5];3738for (round = 0; round < rounds; ++round) {3940// Theta41bc[0] = st[0] ^ st[5] ^ st[10] ^ st[15] ^ st[20];42bc[1] = st[1] ^ st[6] ^ st[11] ^ st[16] ^ st[21];43bc[2] = st[2] ^ st[7] ^ st[12] ^ st[17] ^ st[22];44bc[3] = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23];45bc[4] = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24];4647for (i = 0; i < 5; ++i) {48t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);49st[i ] ^= t;50st[i + 5] ^= t;51st[i + 10] ^= t;52st[i + 15] ^= t;53st[i + 20] ^= t;54}5556// Rho Pi57t = st[1];58for (i = 0; i < 24; ++i) {59bc[0] = st[keccakf_piln[i]];60st[keccakf_piln[i]] = ROTL64(t, keccakf_rotc[i]);61t = bc[0];62}6364// Chi65for (j = 0; j < 25; j += 5) {66bc[0] = st[j ];67bc[1] = st[j + 1];68bc[2] = st[j + 2];69bc[3] = st[j + 3];70bc[4] = st[j + 4];71st[j ] ^= (~bc[1]) & bc[2];72st[j + 1] ^= (~bc[2]) & bc[3];73st[j + 2] ^= (~bc[3]) & bc[4];74st[j + 3] ^= (~bc[4]) & bc[0];75st[j + 4] ^= (~bc[0]) & bc[1];76}7778// Iota79st[0] ^= keccakf_rndc[round];80}81}8283// compute a keccak hash (md) of given byte length from "in"84typedef uint64_t state_t[25];8586int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen)87{88state_t st;89uint8_t temp[144];90int i, rsiz, rsizw;9192rsiz = sizeof(state_t) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen;93rsizw = rsiz / 8;9495memset(st, 0, sizeof(st));9697for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) {98for (i = 0; i < rsizw; i++)99st[i] ^= ((uint64_t *) in)[i];100keccakf(st, KECCAK_ROUNDS);101}102103// last block and padding104memcpy(temp, in, inlen);105temp[inlen++] = 1;106memset(temp + inlen, 0, rsiz - inlen);107temp[rsiz - 1] |= 0x80;108109for (i = 0; i < rsizw; i++)110st[i] ^= ((uint64_t *) temp)[i];111112keccakf(st, KECCAK_ROUNDS);113114memcpy(md, st, mdlen);115116return 0;117}118119void keccak1600(const uint8_t *in, int inlen, uint8_t *md)120{121keccak(in, inlen, md, sizeof(state_t));122}123124125