2 * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
20 #include <kripto/cast.h>
21 #include <kripto/loadstore.h>
22 #include <kripto/rotate.h>
23 #include <kripto/memwipe.h>
24 #include <kripto/hash.h>
25 #include <kripto/desc/hash.h>
26 #include <kripto/object/hash.h>
28 #include <kripto/hash/keccak1600.h>
32 struct kripto_hash_object obj
;
40 static const uint64_t rc
[48] =
42 0x0000000000000001, 0x0000000000008082,
43 0x800000000000808A, 0x8000000080008000,
44 0x000000000000808B, 0x0000000080000001,
45 0x8000000080008081, 0x8000000000008009,
46 0x000000000000008A, 0x0000000000000088,
47 0x0000000080008009, 0x000000008000000A,
48 0x000000008000808B, 0x800000000000008B,
49 0x8000000000008089, 0x8000000000008003,
50 0x8000000000008002, 0x8000000000000080,
51 0x000000000000800A, 0x800000008000000A,
52 0x8000000080008081, 0x8000000000008080,
53 0x0000000080000001, 0x8000000080008008,
54 0x8000000080008082, 0x800000008000800A,
55 0x8000000000000003, 0x8000000080000009,
56 0x8000000000008082, 0x0000000000008009,
57 0x8000000000000080, 0x0000000000008083,
58 0x8000000000000081, 0x0000000000000001,
59 0x000000000000800B, 0x8000000080008001,
60 0x0000000000000080, 0x8000000000008000,
61 0x8000000080008001, 0x0000000000000009,
62 0x800000008000808B, 0x0000000000000081,
63 0x8000000000000082, 0x000000008000008B,
64 0x8000000080008009, 0x8000000080000000,
65 0x0000000080000080, 0x0000000080008003
68 static void keccak1600_F(kripto_hash
*s
)
70 uint64_t a0
= LOAD64L(s
->s
);
71 uint64_t a1
= LOAD64L(s
->s
+ 8);
72 uint64_t a2
= LOAD64L(s
->s
+ 16);
73 uint64_t a3
= LOAD64L(s
->s
+ 24);
74 uint64_t a4
= LOAD64L(s
->s
+ 32);
75 uint64_t a5
= LOAD64L(s
->s
+ 40);
76 uint64_t a6
= LOAD64L(s
->s
+ 48);
77 uint64_t a7
= LOAD64L(s
->s
+ 56);
78 uint64_t a8
= LOAD64L(s
->s
+ 64);
79 uint64_t a9
= LOAD64L(s
->s
+ 72);
80 uint64_t a10
= LOAD64L(s
->s
+ 80);
81 uint64_t a11
= LOAD64L(s
->s
+ 88);
82 uint64_t a12
= LOAD64L(s
->s
+ 96);
83 uint64_t a13
= LOAD64L(s
->s
+ 104);
84 uint64_t a14
= LOAD64L(s
->s
+ 112);
85 uint64_t a15
= LOAD64L(s
->s
+ 120);
86 uint64_t a16
= LOAD64L(s
->s
+ 128);
87 uint64_t a17
= LOAD64L(s
->s
+ 136);
88 uint64_t a18
= LOAD64L(s
->s
+ 144);
89 uint64_t a19
= LOAD64L(s
->s
+ 152);
90 uint64_t a20
= LOAD64L(s
->s
+ 160);
91 uint64_t a21
= LOAD64L(s
->s
+ 168);
92 uint64_t a22
= LOAD64L(s
->s
+ 176);
93 uint64_t a23
= LOAD64L(s
->s
+ 184);
94 uint64_t a24
= LOAD64L(s
->s
+ 192);
136 for(i
= 0; i
< s
->r
; i
++)
138 c0
= a0
^ a5
^ a10
^ a15
^ a20
;
139 c1
= a1
^ a6
^ a11
^ a16
^ a21
;
140 c2
= a2
^ a7
^ a12
^ a17
^ a22
;
141 c3
= a3
^ a8
^ a13
^ a18
^ a23
;
142 c4
= a4
^ a9
^ a14
^ a19
^ a24
;
144 d0
= ROL64_01(c1
) ^ c4
;
145 d1
= ROL64_01(c2
) ^ c0
;
146 d2
= ROL64_01(c3
) ^ c1
;
147 d3
= ROL64_01(c4
) ^ c2
;
148 d4
= ROL64_01(c0
) ^ c3
;
161 b0
= c0
^ ((~c1
) & c2
) ^ rc
[i
];
162 b1
= c1
^ ((~c2
) & c3
);
163 b2
= c2
^ ((~c3
) & c4
);
164 b3
= c3
^ ((~c4
) & c0
);
165 b4
= c4
^ ((~c0
) & c1
);
178 b5
= c0
^ ((~c1
) & c2
);
179 b6
= c1
^ ((~c2
) & c3
);
180 b7
= c2
^ ((~c3
) & c4
);
181 b8
= c3
^ ((~c4
) & c0
);
182 b9
= c4
^ ((~c0
) & c1
);
195 b10
= c0
^ ((~c1
) & c2
);
196 b11
= c1
^ ((~c2
) & c3
);
197 b12
= c2
^ ((~c3
) & c4
);
198 b13
= c3
^ ((~c4
) & c0
);
199 b14
= c4
^ ((~c0
) & c1
);
212 b15
= c0
^ ((~c1
) & c2
);
213 b16
= c1
^ ((~c2
) & c3
);
214 b17
= c2
^ ((~c3
) & c4
);
215 b18
= c3
^ ((~c4
) & c0
);
216 b19
= c4
^ ((~c0
) & c1
);
229 b20
= c0
^ ((~c1
) & c2
);
230 b21
= c1
^ ((~c2
) & c3
);
231 b22
= c2
^ ((~c3
) & c4
);
232 b23
= c3
^ ((~c4
) & c0
);
233 b24
= c4
^ ((~c0
) & c1
);
263 STORE64L(a1
, s
->s
+ 8);
264 STORE64L(a2
, s
->s
+ 16);
265 STORE64L(a3
, s
->s
+ 24);
266 STORE64L(a4
, s
->s
+ 32);
267 STORE64L(a5
, s
->s
+ 40);
268 STORE64L(a6
, s
->s
+ 48);
269 STORE64L(a7
, s
->s
+ 56);
270 STORE64L(a8
, s
->s
+ 64);
271 STORE64L(a9
, s
->s
+ 72);
272 STORE64L(a10
, s
->s
+ 80);
273 STORE64L(a11
, s
->s
+ 88);
274 STORE64L(a12
, s
->s
+ 96);
275 STORE64L(a13
, s
->s
+ 104);
276 STORE64L(a14
, s
->s
+ 112);
277 STORE64L(a15
, s
->s
+ 120);
278 STORE64L(a16
, s
->s
+ 128);
279 STORE64L(a17
, s
->s
+ 136);
280 STORE64L(a18
, s
->s
+ 144);
281 STORE64L(a19
, s
->s
+ 152);
282 STORE64L(a20
, s
->s
+ 160);
283 STORE64L(a21
, s
->s
+ 168);
284 STORE64L(a22
, s
->s
+ 176);
285 STORE64L(a23
, s
->s
+ 184);
286 STORE64L(a24
, s
->s
+ 192);
289 static kripto_hash
*keccak1600_recreate
301 s
->rate
= 200 - (len
<< 1);
303 memset(s
->s
, 0, 200);
308 static void keccak1600_input
317 /* switch back to input mode */
318 if(s
->o
) s
->o
= s
->i
= 0;
321 for(i
= 0; i
< len
; i
++)
329 s
->s
[s
->i
++] ^= CU8(in
)[i
];
333 static void keccak1600_output
342 /* switch to output mode */
347 s
->s
[s
->rate
- 1] ^= 0x80;
356 for(i
= 0; i
< len
; i
++)
364 U8(out
)[i
] = s
->s
[s
->i
++];
368 static kripto_hash
*keccak1600_create(unsigned int r
, size_t len
)
372 s
= malloc(sizeof(struct kripto_hash
));
375 s
->obj
.desc
= kripto_hash_keccak1600
;
377 (void)keccak1600_recreate(s
, r
, len
);
382 static void keccak1600_destroy(kripto_hash
*s
)
384 kripto_memwipe(s
, sizeof(kripto_hash
));
388 static int keccak1600_hash
399 (void)keccak1600_recreate(&s
, r
, out_len
);
400 keccak1600_input(&s
, in
, in_len
);
401 keccak1600_output(&s
, out
, out_len
);
403 kripto_memwipe(&s
, sizeof(kripto_hash
));
408 static const kripto_hash_desc keccak1600
=
411 &keccak1600_recreate
,
416 SIZE_MAX
, /* max output */
420 const kripto_hash_desc
*const kripto_hash_keccak1600
= &keccak1600
;