· 6 years ago · Aug 09, 2019, 10:56 AM
1// keccak.c
2// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
3// A baseline Keccak (3rd round) implementation.
4#include <iostream>
5#include <iomanip>
6#include <sstream>
7
8#include "Keccak.h"
9
10const uint64_t keccakf_rndc[24] =
11{
12 0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
13 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
14 0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
15 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
16 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
17 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
18 0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
19 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
20};
21
22const int keccakf_rotc[24] =
23{
24 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
25 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
26};
27
28const int keccakf_piln[24] =
29{
30 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
31 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
32};
33
34// update the state with given number of rounds
35
36void keccakf(uint64_t st[25], int rounds)
37{
38 int i, j, round;
39 uint64_t t, bc[5];
40
41 for (round = 0; round < rounds; round++) {
42
43 // Theta
44 for (i = 0; i < 5; i++)
45 {
46 bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
47 }
48
49 for (i = 0; i < 5; i++) {
50 t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
51 for (j = 0; j < 25; j += 5)
52 st[j + i] ^= t;
53 }
54
55 // Rho Pi
56 t = st[1];
57 for (i = 0; i < 24; i++) {
58 j = keccakf_piln[i];
59 bc[0] = st[j];
60 st[j] = ROTL64(t, keccakf_rotc[i]);
61 t = bc[0];
62 }
63
64 // Chi
65 for (j = 0; j < 25; j += 5) {
66 for (i = 0; i < 5; i++)
67 bc[i] = st[j + i];
68 for (i = 0; i < 5; i++)
69 st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
70 }
71
72 // Iota
73 st[0] ^= keccakf_rndc[round];
74 }
75}
76
77// compute a keccak hash (md) of given byte length from "in"
78typedef uint64_t state_t[25];
79
80int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen)
81{
82 state_t st;
83 uint8_t temp[144];
84 int i, rsiz, rsizw;
85
86
87 /* for some reason the enum from hash-ops.h is not valid here when
88 compiling - is this a C vs C++ thing? Anyhow, lets just redefine it for
89 now. */
90
91 const int HASH_DATA_AREA = 136;
92
93 rsiz = sizeof(state_t) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen;
94 rsizw = rsiz / 8;
95
96 memset(st, 0, sizeof(st));
97
98 for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) {
99 for (i = 0; i < rsizw; i++)
100 st[i] ^= ((uint64_t *) in)[i];
101 keccakf(st, KECCAK_ROUNDS);
102 }
103
104 // last block and padding
105 memcpy(temp, in, inlen);
106 temp[inlen++] = 1;
107 memset(temp + inlen, 0, rsiz - inlen);
108 temp[rsiz - 1] |= 0x80;
109
110 for (i = 0; i < rsizw; i++)
111 st[i] ^= ((uint64_t *) temp)[i];
112
113 keccakf(st, KECCAK_ROUNDS);
114
115 memcpy(md, st, mdlen);
116
117 return 0;
118}
119
120void keccak1600(const uint8_t *in, int inlen, uint8_t *md)
121{
122 keccak(in, inlen, md, sizeof(state_t));
123}
124
125struct SecretKey {
126 uint8_t data[32];
127};
128
129std::string hexStr(unsigned char* data, int len)
130{
131 std::stringstream ss;
132 ss << std::hex;
133 for(int i=0;i<len;++i)
134 ss << std::setw(2) << std::setfill('0') << (int)data[i];
135 return ss.str();
136}
137
138int main()
139{
140 struct SecretKey in;
141
142 struct SecretKey out;
143
144 for (int i = 0; i < 32; i++)
145 {
146 in.data[i] = i;
147 }
148
149 keccak((uint8_t *)&in, sizeof(in), (uint8_t *)&out, sizeof(out));
150
151 std::cout << hexStr(out.data, 32) << std::endl;
152}