2 * Copyright (C) 2013 Gregor Pintar <grpintar@gmail.com>
4 * Permission is granted to deal in this work without any restriction,
5 * including unlimited rights to use, publicly perform, publish,
6 * reproduce, relicence, modify, merge, and/or distribute in any form,
7 * for any purpose, with or without fee, and by any means.
9 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind,
10 * to the utmost extent permitted by applicable law. In no event
11 * shall a licensor, author or contributor be held liable for any
12 * issues arising in any way out of dealing in the work.
21 #include <kripto/cast.h>
22 #include <kripto/loadstore.h>
23 #include <kripto/rotate.h>
24 #include <kripto/memwipe.h>
25 #include <kripto/hash.h>
26 #include <kripto/desc/hash.h>
27 #include <kripto/object/hash.h>
29 #include <kripto/hash/keccak1600.h>
33 struct kripto_hash_object obj
;
41 static const uint64_t rc
[48] =
43 0x0000000000000001, 0x0000000000008082,
44 0x800000000000808A, 0x8000000080008000,
45 0x000000000000808B, 0x0000000080000001,
46 0x8000000080008081, 0x8000000000008009,
47 0x000000000000008A, 0x0000000000000088,
48 0x0000000080008009, 0x000000008000000A,
49 0x000000008000808B, 0x800000000000008B,
50 0x8000000000008089, 0x8000000000008003,
51 0x8000000000008002, 0x8000000000000080,
52 0x000000000000800A, 0x800000008000000A,
53 0x8000000080008081, 0x8000000000008080,
54 0x0000000080000001, 0x8000000080008008,
55 0x8000000080008082, 0x800000008000800A,
56 0x8000000000000003, 0x8000000080000009,
57 0x8000000000008082, 0x0000000000008009,
58 0x8000000000000080, 0x0000000000008083,
59 0x8000000000000081, 0x0000000000000001,
60 0x000000000000800B, 0x8000000080008001,
61 0x0000000000000080, 0x8000000000008000,
62 0x8000000080008001, 0x0000000000000009,
63 0x800000008000808B, 0x0000000000000081,
64 0x8000000000000082, 0x000000008000008B,
65 0x8000000080008009, 0x8000000080000000,
66 0x0000000080000080, 0x0000000080008003
69 static void keccak1600_F(kripto_hash
*s
)
71 uint64_t a0
= LOAD64L(s
->s
);
72 uint64_t a1
= LOAD64L(s
->s
+ 8);
73 uint64_t a2
= LOAD64L(s
->s
+ 16);
74 uint64_t a3
= LOAD64L(s
->s
+ 24);
75 uint64_t a4
= LOAD64L(s
->s
+ 32);
76 uint64_t a5
= LOAD64L(s
->s
+ 40);
77 uint64_t a6
= LOAD64L(s
->s
+ 48);
78 uint64_t a7
= LOAD64L(s
->s
+ 56);
79 uint64_t a8
= LOAD64L(s
->s
+ 64);
80 uint64_t a9
= LOAD64L(s
->s
+ 72);
81 uint64_t a10
= LOAD64L(s
->s
+ 80);
82 uint64_t a11
= LOAD64L(s
->s
+ 88);
83 uint64_t a12
= LOAD64L(s
->s
+ 96);
84 uint64_t a13
= LOAD64L(s
->s
+ 104);
85 uint64_t a14
= LOAD64L(s
->s
+ 112);
86 uint64_t a15
= LOAD64L(s
->s
+ 120);
87 uint64_t a16
= LOAD64L(s
->s
+ 128);
88 uint64_t a17
= LOAD64L(s
->s
+ 136);
89 uint64_t a18
= LOAD64L(s
->s
+ 144);
90 uint64_t a19
= LOAD64L(s
->s
+ 152);
91 uint64_t a20
= LOAD64L(s
->s
+ 160);
92 uint64_t a21
= LOAD64L(s
->s
+ 168);
93 uint64_t a22
= LOAD64L(s
->s
+ 176);
94 uint64_t a23
= LOAD64L(s
->s
+ 184);
95 uint64_t a24
= LOAD64L(s
->s
+ 192);
137 for(i
= 0; i
< s
->r
; i
++)
139 c0
= a0
^ a5
^ a10
^ a15
^ a20
;
140 c1
= a1
^ a6
^ a11
^ a16
^ a21
;
141 c2
= a2
^ a7
^ a12
^ a17
^ a22
;
142 c3
= a3
^ a8
^ a13
^ a18
^ a23
;
143 c4
= a4
^ a9
^ a14
^ a19
^ a24
;
145 d0
= ROL64(c1
, 1) ^ c4
;
146 d1
= ROL64(c2
, 1) ^ c0
;
147 d2
= ROL64(c3
, 1) ^ c1
;
148 d3
= ROL64(c4
, 1) ^ c2
;
149 d4
= ROL64(c0
, 1) ^ c3
;
162 b0
= c0
^ ((~c1
) & c2
) ^ rc
[i
];
163 b1
= c1
^ ((~c2
) & c3
);
164 b2
= c2
^ ((~c3
) & c4
);
165 b3
= c3
^ ((~c4
) & c0
);
166 b4
= c4
^ ((~c0
) & c1
);
179 b5
= c0
^ ((~c1
) & c2
);
180 b6
= c1
^ ((~c2
) & c3
);
181 b7
= c2
^ ((~c3
) & c4
);
182 b8
= c3
^ ((~c4
) & c0
);
183 b9
= c4
^ ((~c0
) & c1
);
196 b10
= c0
^ ((~c1
) & c2
);
197 b11
= c1
^ ((~c2
) & c3
);
198 b12
= c2
^ ((~c3
) & c4
);
199 b13
= c3
^ ((~c4
) & c0
);
200 b14
= c4
^ ((~c0
) & c1
);
213 b15
= c0
^ ((~c1
) & c2
);
214 b16
= c1
^ ((~c2
) & c3
);
215 b17
= c2
^ ((~c3
) & c4
);
216 b18
= c3
^ ((~c4
) & c0
);
217 b19
= c4
^ ((~c0
) & c1
);
230 b20
= c0
^ ((~c1
) & c2
);
231 b21
= c1
^ ((~c2
) & c3
);
232 b22
= c2
^ ((~c3
) & c4
);
233 b23
= c3
^ ((~c4
) & c0
);
234 b24
= c4
^ ((~c0
) & c1
);
264 STORE64L(a1
, s
->s
+ 8);
265 STORE64L(a2
, s
->s
+ 16);
266 STORE64L(a3
, s
->s
+ 24);
267 STORE64L(a4
, s
->s
+ 32);
268 STORE64L(a5
, s
->s
+ 40);
269 STORE64L(a6
, s
->s
+ 48);
270 STORE64L(a7
, s
->s
+ 56);
271 STORE64L(a8
, s
->s
+ 64);
272 STORE64L(a9
, s
->s
+ 72);
273 STORE64L(a10
, s
->s
+ 80);
274 STORE64L(a11
, s
->s
+ 88);
275 STORE64L(a12
, s
->s
+ 96);
276 STORE64L(a13
, s
->s
+ 104);
277 STORE64L(a14
, s
->s
+ 112);
278 STORE64L(a15
, s
->s
+ 120);
279 STORE64L(a16
, s
->s
+ 128);
280 STORE64L(a17
, s
->s
+ 136);
281 STORE64L(a18
, s
->s
+ 144);
282 STORE64L(a19
, s
->s
+ 152);
283 STORE64L(a20
, s
->s
+ 160);
284 STORE64L(a21
, s
->s
+ 168);
285 STORE64L(a22
, s
->s
+ 176);
286 STORE64L(a23
, s
->s
+ 184);
287 STORE64L(a24
, s
->s
+ 192);
290 static kripto_hash
*keccak1600_recreate
302 s
->rate
= 200 - (len
<< 1);
304 memset(s
->s
, 0, 200);
309 static void keccak1600_input
318 /* switch back to input mode */
319 if(s
->o
) s
->o
= s
->i
= 0;
322 for(i
= 0; i
< len
; i
++)
324 s
->s
[s
->i
++] ^= CU8(in
)[i
];
334 static void keccak1600_output
343 /* switch to output mode */
348 s
->s
[s
->rate
- 1] ^= 0x80;
357 for(i
= 0; i
< len
; i
++)
365 U8(out
)[i
] = s
->s
[s
->i
++];
369 static kripto_hash
*keccak1600_create(unsigned int r
, size_t len
)
373 s
= malloc(sizeof(struct kripto_hash
));
376 s
->obj
.desc
= kripto_hash_keccak1600
;
378 (void)keccak1600_recreate(s
, r
, len
);
383 static void keccak1600_destroy(kripto_hash
*s
)
385 kripto_memwipe(s
, sizeof(kripto_hash
));
389 static int keccak1600_hash
400 (void)keccak1600_recreate(&s
, r
, out_len
);
401 keccak1600_input(&s
, in
, in_len
);
402 keccak1600_output(&s
, out
, out_len
);
404 kripto_memwipe(&s
, sizeof(kripto_hash
));
409 static const kripto_hash_desc keccak1600
=
412 &keccak1600_recreate
,
417 SIZE_MAX
, /* max output */
421 const kripto_hash_desc
*const kripto_hash_keccak1600
= &keccak1600
;
423 const kripto_hash_desc
*const kripto_hash_sha3
= &keccak1600
;