xref: /freebsd/sys/contrib/openzfs/module/icp/algs/edonr/edonr.c (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 /*
2  * IDI,NTNU
3  *
4  * CDDL HEADER START
5  *
6  * The contents of this file are subject to the terms of the
7  * Common Development and Distribution License (the "License").
8  * You may not use this file except in compliance with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://opensource.org/licenses/CDDL-1.0.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  *
23  * Copyright (C) 2009, 2010, Jorn Amundsen <jorn.amundsen@ntnu.no>
24  * Tweaked Edon-R implementation for SUPERCOP, based on NIST API.
25  *
26  * $Id: edonr.c 517 2013-02-17 20:34:39Z joern $
27  */
28 /*
29  * Portions copyright (c) 2013, Saso Kiselkov, All rights reserved
30  */
31 
32 /*
33  * Unlike sha2 or skein, we won't expose edonr via the Kernel Cryptographic
34  * Framework (KCF), because Edon-R is *NOT* suitable for general-purpose
35  * cryptographic use. Users of Edon-R must interface directly to this module.
36  */
37 
38 #include <sys/string.h>
39 #include <sys/edonr.h>
40 #include <sys/debug.h>
41 
42 /* big endian support, provides no-op's if run on little endian hosts */
43 #include "edonr_byteorder.h"
44 
45 #define	hashState224(x)	((x)->pipe->p256)
46 #define	hashState256(x)	((x)->pipe->p256)
47 #define	hashState384(x)	((x)->pipe->p512)
48 #define	hashState512(x)	((x)->pipe->p512)
49 
50 /* rotate shortcuts */
51 #define	rotl32(x, n)	(((x) << (n)) | ((x) >> (32 - (n))))
52 #define	rotr32(x, n)	(((x) >> (n)) | ((x) << (32 - (n))))
53 
54 #define	rotl64(x, n)	(((x) << (n)) | ((x) >> (64 - (n))))
55 #define	rotr64(x, n)	(((x) >> (n)) | ((x) << (64 - (n))))
56 
57 #if !defined(__C99_RESTRICT)
58 #define	restrict	/* restrict */
59 #endif
60 
61 #define	EDONR_VALID_HASHBITLEN(x) \
62 	((x) == 512 || (x) == 384 || (x) == 256 || (x) == 224)
63 
64 /* EdonR224 initial double chaining pipe */
65 static const uint32_t i224p2[16] = {
66 	0x00010203ul, 0x04050607ul, 0x08090a0bul, 0x0c0d0e0ful,
67 	0x10111213ul, 0x14151617ul, 0x18191a1bul, 0x1c1d1e1ful,
68 	0x20212223ul, 0x24252627ul, 0x28292a2bul, 0x2c2d2e2ful,
69 	0x30313233ul, 0x34353637ul, 0x38393a3bul, 0x3c3d3e3ful,
70 };
71 
72 /* EdonR256 initial double chaining pipe */
73 static const uint32_t i256p2[16] = {
74 	0x40414243ul, 0x44454647ul, 0x48494a4bul, 0x4c4d4e4ful,
75 	0x50515253ul, 0x54555657ul, 0x58595a5bul, 0x5c5d5e5ful,
76 	0x60616263ul, 0x64656667ul, 0x68696a6bul, 0x6c6d6e6ful,
77 	0x70717273ul, 0x74757677ul, 0x78797a7bul, 0x7c7d7e7ful,
78 };
79 
80 /* EdonR384 initial double chaining pipe */
81 static const uint64_t i384p2[16] = {
82 	0x0001020304050607ull, 0x08090a0b0c0d0e0full,
83 	0x1011121314151617ull, 0x18191a1b1c1d1e1full,
84 	0x2021222324252627ull, 0x28292a2b2c2d2e2full,
85 	0x3031323334353637ull, 0x38393a3b3c3d3e3full,
86 	0x4041424344454647ull, 0x48494a4b4c4d4e4full,
87 	0x5051525354555657ull, 0x58595a5b5c5d5e5full,
88 	0x6061626364656667ull, 0x68696a6b6c6d6e6full,
89 	0x7071727374757677ull, 0x78797a7b7c7d7e7full
90 };
91 
92 /* EdonR512 initial double chaining pipe */
93 static const uint64_t i512p2[16] = {
94 	0x8081828384858687ull, 0x88898a8b8c8d8e8full,
95 	0x9091929394959697ull, 0x98999a9b9c9d9e9full,
96 	0xa0a1a2a3a4a5a6a7ull, 0xa8a9aaabacadaeafull,
97 	0xb0b1b2b3b4b5b6b7ull, 0xb8b9babbbcbdbebfull,
98 	0xc0c1c2c3c4c5c6c7ull, 0xc8c9cacbcccdcecfull,
99 	0xd0d1d2d3d4d5d6d7ull, 0xd8d9dadbdcdddedfull,
100 	0xe0e1e2e3e4e5e6e7ull, 0xe8e9eaebecedeeefull,
101 	0xf0f1f2f3f4f5f6f7ull, 0xf8f9fafbfcfdfeffull
102 };
103 
104 /*
105  * First Latin Square
106  * 0   7   1   3   2   4   6   5
107  * 4   1   7   6   3   0   5   2
108  * 7   0   4   2   5   3   1   6
109  * 1   4   0   5   6   2   7   3
110  * 2   3   6   7   1   5   0   4
111  * 5   2   3   1   7   6   4   0
112  * 3   6   5   0   4   7   2   1
113  * 6   5   2   4   0   1   3   7
114  */
115 #define	LS1_256(c, x0, x1, x2, x3, x4, x5, x6, x7)			\
116 {									\
117 	uint32_t x04, x17, x23, x56, x07, x26;				\
118 	x04 = x0+x4, x17 = x1+x7, x07 = x04+x17;			\
119 	s0 = c + x07 + x2;						\
120 	s1 = rotl32(x07 + x3, 4);					\
121 	s2 = rotl32(x07 + x6, 8);					\
122 	x23 = x2 + x3;							\
123 	s5 = rotl32(x04 + x23 + x5, 22);				\
124 	x56 = x5 + x6;							\
125 	s6 = rotl32(x17 + x56 + x0, 24);				\
126 	x26 = x23+x56;							\
127 	s3 = rotl32(x26 + x7, 13);					\
128 	s4 = rotl32(x26 + x1, 17);					\
129 	s7 = rotl32(x26 + x4, 29);					\
130 }
131 
132 #define	LS1_512(c, x0, x1, x2, x3, x4, x5, x6, x7)			\
133 {									\
134 	uint64_t x04, x17, x23, x56, x07, x26;				\
135 	x04 = x0+x4, x17 = x1+x7, x07 = x04+x17;			\
136 	s0 = c + x07 + x2;						\
137 	s1 = rotl64(x07 + x3, 5);					\
138 	s2 = rotl64(x07 + x6, 15);					\
139 	x23 = x2 + x3;							\
140 	s5 = rotl64(x04 + x23 + x5, 40);				\
141 	x56 = x5 + x6;							\
142 	s6 = rotl64(x17 + x56 + x0, 50);				\
143 	x26 = x23+x56;							\
144 	s3 = rotl64(x26 + x7, 22);					\
145 	s4 = rotl64(x26 + x1, 31);					\
146 	s7 = rotl64(x26 + x4, 59);					\
147 }
148 
149 /*
150  * Second Orthogonal Latin Square
151  * 0   4   2   3   1   6   5   7
152  * 7   6   3   2   5   4   1   0
153  * 5   3   1   6   0   2   7   4
154  * 1   0   5   4   3   7   2   6
155  * 2   1   0   7   4   5   6   3
156  * 3   5   7   0   6   1   4   2
157  * 4   7   6   1   2   0   3   5
158  * 6   2   4   5   7   3   0   1
159  */
160 #define	LS2_256(c, y0, y1, y2, y3, y4, y5, y6, y7)			\
161 {									\
162 	uint32_t y01, y25, y34, y67, y04, y05, y27, y37;		\
163 	y01 = y0+y1, y25 = y2+y5, y05 = y01+y25;			\
164 	t0  = ~c + y05 + y7;						\
165 	t2 = rotl32(y05 + y3, 9);					\
166 	y34 = y3+y4, y04 = y01+y34;					\
167 	t1 = rotl32(y04 + y6, 5);					\
168 	t4 = rotl32(y04 + y5, 15);					\
169 	y67 = y6+y7, y37 = y34+y67;					\
170 	t3 = rotl32(y37 + y2, 11);					\
171 	t7 = rotl32(y37 + y0, 27);					\
172 	y27 = y25+y67;							\
173 	t5 = rotl32(y27 + y4, 20);					\
174 	t6 = rotl32(y27 + y1, 25);					\
175 }
176 
177 #define	LS2_512(c, y0, y1, y2, y3, y4, y5, y6, y7)			\
178 {									\
179 	uint64_t y01, y25, y34, y67, y04, y05, y27, y37;		\
180 	y01 = y0+y1, y25 = y2+y5, y05 = y01+y25;			\
181 	t0  = ~c + y05 + y7;						\
182 	t2 = rotl64(y05 + y3, 19);					\
183 	y34 = y3+y4, y04 = y01+y34;					\
184 	t1 = rotl64(y04 + y6, 10);					\
185 	t4 = rotl64(y04 + y5, 36);					\
186 	y67 = y6+y7, y37 = y34+y67;					\
187 	t3 = rotl64(y37 + y2, 29);					\
188 	t7 = rotl64(y37 + y0, 55);					\
189 	y27 = y25+y67;							\
190 	t5 = rotl64(y27 + y4, 44);					\
191 	t6 = rotl64(y27 + y1, 48);					\
192 }
193 
194 #define	quasi_exform256(r0, r1, r2, r3, r4, r5, r6, r7)			\
195 {									\
196 	uint32_t s04, s17, s23, s56, t01, t25, t34, t67;		\
197 	s04 = s0 ^ s4, t01 = t0 ^ t1;					\
198 	r0 = (s04 ^ s1) + (t01 ^ t5);					\
199 	t67 = t6 ^ t7;							\
200 	r1 = (s04 ^ s7) + (t2 ^ t67);					\
201 	s23 = s2 ^ s3;							\
202 	r7 = (s23 ^ s5) + (t4 ^ t67);					\
203 	t34 = t3 ^ t4;							\
204 	r3 = (s23 ^ s4) + (t0 ^ t34);					\
205 	s56 = s5 ^ s6;							\
206 	r5 = (s3 ^ s56) + (t34 ^ t6);					\
207 	t25 = t2 ^ t5;							\
208 	r6 = (s2 ^ s56) + (t25 ^ t7);					\
209 	s17 = s1 ^ s7;							\
210 	r4 = (s0 ^ s17) + (t1 ^ t25);					\
211 	r2 = (s17 ^ s6) + (t01 ^ t3);					\
212 }
213 
214 #define	quasi_exform512(r0, r1, r2, r3, r4, r5, r6, r7)			\
215 {									\
216 	uint64_t s04, s17, s23, s56, t01, t25, t34, t67;		\
217 	s04 = s0 ^ s4, t01 = t0 ^ t1;					\
218 	r0 = (s04 ^ s1) + (t01 ^ t5);					\
219 	t67 = t6 ^ t7;							\
220 	r1 = (s04 ^ s7) + (t2 ^ t67);					\
221 	s23 = s2 ^ s3;							\
222 	r7 = (s23 ^ s5) + (t4 ^ t67);					\
223 	t34 = t3 ^ t4;							\
224 	r3 = (s23 ^ s4) + (t0 ^ t34);					\
225 	s56 = s5 ^ s6;							\
226 	r5 = (s3 ^ s56) + (t34 ^ t6);					\
227 	t25 = t2 ^ t5;							\
228 	r6 = (s2 ^ s56) + (t25 ^ t7);					\
229 	s17 = s1 ^ s7;							\
230 	r4 = (s0 ^ s17) + (t1 ^ t25);					\
231 	r2 = (s17 ^ s6) + (t01 ^ t3);					\
232 }
233 
234 static size_t
235 Q256(size_t bitlen, const uint32_t *data, uint32_t *restrict p)
236 {
237 	size_t bl;
238 
239 	for (bl = bitlen; bl >= EdonR256_BLOCK_BITSIZE;
240 	    bl -= EdonR256_BLOCK_BITSIZE, data += 16) {
241 		uint32_t s0, s1, s2, s3, s4, s5, s6, s7, t0, t1, t2, t3, t4,
242 		    t5, t6, t7;
243 		uint32_t p0, p1, p2, p3, p4, p5, p6, p7, q0, q1, q2, q3, q4,
244 		    q5, q6, q7;
245 		const uint32_t defix = 0xaaaaaaaa;
246 #if defined(MACHINE_IS_BIG_ENDIAN)
247 		uint32_t swp0, swp1, swp2, swp3, swp4, swp5, swp6, swp7, swp8,
248 		    swp9, swp10, swp11, swp12, swp13, swp14, swp15;
249 #define	d(j)	swp ## j
250 #define	s32(j)	ld_swap32((uint32_t *)data + j, swp ## j)
251 #else
252 #define	d(j)	data[j]
253 #endif
254 
255 		/* First row of quasigroup e-transformations */
256 #if defined(MACHINE_IS_BIG_ENDIAN)
257 		s32(8);
258 		s32(9);
259 		s32(10);
260 		s32(11);
261 		s32(12);
262 		s32(13);
263 		s32(14);
264 		s32(15);
265 #endif
266 		LS1_256(defix, d(15), d(14), d(13), d(12), d(11), d(10), d(9),
267 		    d(8));
268 #if defined(MACHINE_IS_BIG_ENDIAN)
269 		s32(0);
270 		s32(1);
271 		s32(2);
272 		s32(3);
273 		s32(4);
274 		s32(5);
275 		s32(6);
276 		s32(7);
277 #undef s32
278 #endif
279 		LS2_256(defix, d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7));
280 		quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7);
281 
282 		LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
283 		LS2_256(defix, d(8), d(9), d(10), d(11), d(12), d(13), d(14),
284 		    d(15));
285 		quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7);
286 
287 		/* Second row of quasigroup e-transformations */
288 		LS1_256(defix, p[8], p[9], p[10], p[11], p[12], p[13], p[14],
289 		    p[15]);
290 		LS2_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
291 		quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7);
292 
293 		LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
294 		LS2_256(defix, q0, q1, q2, q3, q4, q5, q6, q7);
295 		quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7);
296 
297 		/* Third row of quasigroup e-transformations */
298 		LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
299 		LS2_256(defix, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
300 		quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7);
301 
302 		LS1_256(defix, q0, q1, q2, q3, q4, q5, q6, q7);
303 		LS2_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
304 		quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7);
305 
306 		/* Fourth row of quasigroup e-transformations */
307 		LS1_256(defix, d(7), d(6), d(5), d(4), d(3), d(2), d(1), d(0));
308 		LS2_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
309 		quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7);
310 
311 		LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7);
312 		LS2_256(defix, q0, q1, q2, q3, q4, q5, q6, q7);
313 		quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7);
314 
315 		/* Edon-R tweak on the original SHA-3 Edon-R submission. */
316 		p[0] ^= d(8) ^ p0;
317 		p[1] ^= d(9) ^ p1;
318 		p[2] ^= d(10) ^ p2;
319 		p[3] ^= d(11) ^ p3;
320 		p[4] ^= d(12) ^ p4;
321 		p[5] ^= d(13) ^ p5;
322 		p[6] ^= d(14) ^ p6;
323 		p[7] ^= d(15) ^ p7;
324 		p[8] ^= d(0) ^ q0;
325 		p[9] ^= d(1) ^ q1;
326 		p[10] ^= d(2) ^ q2;
327 		p[11] ^= d(3) ^ q3;
328 		p[12] ^= d(4) ^ q4;
329 		p[13] ^= d(5) ^ q5;
330 		p[14] ^= d(6) ^ q6;
331 		p[15] ^= d(7) ^ q7;
332 	}
333 
334 #undef d
335 	return (bitlen - bl);
336 }
337 
338 /*
339  * Why is this #pragma here?
340  *
341  * Checksum functions like this one can go over the stack frame size check
342  * Linux imposes on 32-bit platforms (-Wframe-larger-than=1024).  We can
343  * safely ignore the compiler error since we know that in OpenZFS, that
344  * the function will be called from a worker thread that won't be using
345  * much stack.  The only function that goes over the 1k limit is Q512(),
346  * which only goes over it by a hair (1248 bytes on ARM32).
347  */
348 #include <sys/isa_defs.h>	/* for _ILP32 */
349 #ifdef _ILP32   /* We're 32-bit, assume small stack frames */
350 #pragma GCC diagnostic ignored "-Wframe-larger-than="
351 #endif
352 
353 #if defined(__IBMC__) && defined(_AIX) && defined(__64BIT__)
354 static inline size_t
355 #else
356 static size_t
357 #endif
358 Q512(size_t bitlen, const uint64_t *data, uint64_t *restrict p)
359 {
360 	size_t bl;
361 
362 	for (bl = bitlen; bl >= EdonR512_BLOCK_BITSIZE;
363 	    bl -= EdonR512_BLOCK_BITSIZE, data += 16) {
364 		uint64_t s0, s1, s2, s3, s4, s5, s6, s7, t0, t1, t2, t3, t4,
365 		    t5, t6, t7;
366 		uint64_t p0, p1, p2, p3, p4, p5, p6, p7, q0, q1, q2, q3, q4,
367 		    q5, q6, q7;
368 		const uint64_t defix = 0xaaaaaaaaaaaaaaaaull;
369 #if defined(MACHINE_IS_BIG_ENDIAN)
370 		uint64_t swp0, swp1, swp2, swp3, swp4, swp5, swp6, swp7, swp8,
371 		    swp9, swp10, swp11, swp12, swp13, swp14, swp15;
372 #define	d(j)	swp##j
373 #define	s64(j)	ld_swap64((uint64_t *)data+j, swp##j)
374 #else
375 #define	d(j)	data[j]
376 #endif
377 
378 		/* First row of quasigroup e-transformations */
379 #if defined(MACHINE_IS_BIG_ENDIAN)
380 		s64(8);
381 		s64(9);
382 		s64(10);
383 		s64(11);
384 		s64(12);
385 		s64(13);
386 		s64(14);
387 		s64(15);
388 #endif
389 		LS1_512(defix, d(15), d(14), d(13), d(12), d(11), d(10), d(9),
390 		    d(8));
391 #if defined(MACHINE_IS_BIG_ENDIAN)
392 		s64(0);
393 		s64(1);
394 		s64(2);
395 		s64(3);
396 		s64(4);
397 		s64(5);
398 		s64(6);
399 		s64(7);
400 #undef s64
401 #endif
402 		LS2_512(defix, d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7));
403 		quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7);
404 
405 		LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
406 		LS2_512(defix, d(8), d(9), d(10), d(11), d(12), d(13), d(14),
407 		    d(15));
408 		quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7);
409 
410 		/* Second row of quasigroup e-transformations */
411 		LS1_512(defix, p[8], p[9], p[10], p[11], p[12], p[13], p[14],
412 		    p[15]);
413 		LS2_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
414 		quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7);
415 
416 		LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
417 		LS2_512(defix, q0, q1, q2, q3, q4, q5, q6, q7);
418 		quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7);
419 
420 		/* Third row of quasigroup e-transformations */
421 		LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
422 		LS2_512(defix, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
423 		quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7);
424 
425 		LS1_512(defix, q0, q1, q2, q3, q4, q5, q6, q7);
426 		LS2_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
427 		quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7);
428 
429 		/* Fourth row of quasigroup e-transformations */
430 		LS1_512(defix, d(7), d(6), d(5), d(4), d(3), d(2), d(1), d(0));
431 		LS2_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
432 		quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7);
433 
434 		LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7);
435 		LS2_512(defix, q0, q1, q2, q3, q4, q5, q6, q7);
436 		quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7);
437 
438 		/* Edon-R tweak on the original SHA-3 Edon-R submission. */
439 		p[0] ^= d(8) ^ p0;
440 		p[1] ^= d(9) ^ p1;
441 		p[2] ^= d(10) ^ p2;
442 		p[3] ^= d(11) ^ p3;
443 		p[4] ^= d(12) ^ p4;
444 		p[5] ^= d(13) ^ p5;
445 		p[6] ^= d(14) ^ p6;
446 		p[7] ^= d(15) ^ p7;
447 		p[8] ^= d(0) ^ q0;
448 		p[9] ^= d(1) ^ q1;
449 		p[10] ^= d(2) ^ q2;
450 		p[11] ^= d(3) ^ q3;
451 		p[12] ^= d(4) ^ q4;
452 		p[13] ^= d(5) ^ q5;
453 		p[14] ^= d(6) ^ q6;
454 		p[15] ^= d(7) ^ q7;
455 	}
456 
457 #undef d
458 	return (bitlen - bl);
459 }
460 
461 void
462 EdonRInit(EdonRState *state, size_t hashbitlen)
463 {
464 	ASSERT(EDONR_VALID_HASHBITLEN(hashbitlen));
465 	switch (hashbitlen) {
466 	case 224:
467 		state->hashbitlen = 224;
468 		state->bits_processed = 0;
469 		state->unprocessed_bits = 0;
470 		memcpy(hashState224(state)->DoublePipe, i224p2,
471 		    sizeof (i224p2));
472 		break;
473 
474 	case 256:
475 		state->hashbitlen = 256;
476 		state->bits_processed = 0;
477 		state->unprocessed_bits = 0;
478 		memcpy(hashState256(state)->DoublePipe, i256p2,
479 		    sizeof (i256p2));
480 		break;
481 
482 	case 384:
483 		state->hashbitlen = 384;
484 		state->bits_processed = 0;
485 		state->unprocessed_bits = 0;
486 		memcpy(hashState384(state)->DoublePipe, i384p2,
487 		    sizeof (i384p2));
488 		break;
489 
490 	case 512:
491 		state->hashbitlen = 512;
492 		state->bits_processed = 0;
493 		state->unprocessed_bits = 0;
494 		memcpy(hashState224(state)->DoublePipe, i512p2,
495 		    sizeof (i512p2));
496 		break;
497 	}
498 }
499 
500 
501 void
502 EdonRUpdate(EdonRState *state, const uint8_t *data, size_t databitlen)
503 {
504 	uint32_t *data32;
505 	uint64_t *data64;
506 
507 	size_t bits_processed;
508 
509 	ASSERT(EDONR_VALID_HASHBITLEN(state->hashbitlen));
510 	switch (state->hashbitlen) {
511 	case 224:
512 	case 256:
513 		if (state->unprocessed_bits > 0) {
514 			/* LastBytes = databitlen / 8 */
515 			int LastBytes = (int)databitlen >> 3;
516 
517 			ASSERT(state->unprocessed_bits + databitlen <=
518 			    EdonR256_BLOCK_SIZE * 8);
519 
520 			memcpy(hashState256(state)->LastPart
521 			    + (state->unprocessed_bits >> 3),
522 			    data, LastBytes);
523 			state->unprocessed_bits += (int)databitlen;
524 			databitlen = state->unprocessed_bits;
525 			/* LINTED E_BAD_PTR_CAST_ALIGN */
526 			data32 = (uint32_t *)hashState256(state)->LastPart;
527 		} else
528 			/* LINTED E_BAD_PTR_CAST_ALIGN */
529 			data32 = (uint32_t *)data;
530 
531 		bits_processed = Q256(databitlen, data32,
532 		    hashState256(state)->DoublePipe);
533 		state->bits_processed += bits_processed;
534 		databitlen -= bits_processed;
535 		state->unprocessed_bits = (int)databitlen;
536 		if (databitlen > 0) {
537 			/* LastBytes = Ceil(databitlen / 8) */
538 			int LastBytes =
539 			    ((~(((-(int)databitlen) >> 3) & 0x01ff)) +
540 			    1) & 0x01ff;
541 
542 			data32 += bits_processed >> 5;	/* byte size update */
543 			memmove(hashState256(state)->LastPart,
544 			    data32, LastBytes);
545 		}
546 		break;
547 
548 	case 384:
549 	case 512:
550 		if (state->unprocessed_bits > 0) {
551 			/* LastBytes = databitlen / 8 */
552 			int LastBytes = (int)databitlen >> 3;
553 
554 			ASSERT(state->unprocessed_bits + databitlen <=
555 			    EdonR512_BLOCK_SIZE * 8);
556 
557 			memcpy(hashState512(state)->LastPart
558 			    + (state->unprocessed_bits >> 3),
559 			    data, LastBytes);
560 			state->unprocessed_bits += (int)databitlen;
561 			databitlen = state->unprocessed_bits;
562 			/* LINTED E_BAD_PTR_CAST_ALIGN */
563 			data64 = (uint64_t *)hashState512(state)->LastPart;
564 		} else
565 			/* LINTED E_BAD_PTR_CAST_ALIGN */
566 			data64 = (uint64_t *)data;
567 
568 		bits_processed = Q512(databitlen, data64,
569 		    hashState512(state)->DoublePipe);
570 		state->bits_processed += bits_processed;
571 		databitlen -= bits_processed;
572 		state->unprocessed_bits = (int)databitlen;
573 		if (databitlen > 0) {
574 			/* LastBytes = Ceil(databitlen / 8) */
575 			int LastBytes =
576 			    ((~(((-(int)databitlen) >> 3) & 0x03ff)) +
577 			    1) & 0x03ff;
578 
579 			data64 += bits_processed >> 6;	/* byte size update */
580 			memmove(hashState512(state)->LastPart,
581 			    data64, LastBytes);
582 		}
583 		break;
584 	}
585 }
586 
587 void
588 EdonRFinal(EdonRState *state, uint8_t *hashval)
589 {
590 	uint32_t *data32;
591 	uint64_t *data64, num_bits;
592 
593 	size_t databitlen;
594 	int LastByte, PadOnePosition;
595 
596 	num_bits = state->bits_processed + state->unprocessed_bits;
597 	ASSERT(EDONR_VALID_HASHBITLEN(state->hashbitlen));
598 	switch (state->hashbitlen) {
599 	case 224:
600 	case 256:
601 		LastByte = (int)state->unprocessed_bits >> 3;
602 		PadOnePosition = 7 - (state->unprocessed_bits & 0x07);
603 		hashState256(state)->LastPart[LastByte] =
604 		    (hashState256(state)->LastPart[LastByte]
605 		    & (0xff << (PadOnePosition + 1))) ^
606 		    (0x01 << PadOnePosition);
607 		/* LINTED E_BAD_PTR_CAST_ALIGN */
608 		data64 = (uint64_t *)hashState256(state)->LastPart;
609 
610 		if (state->unprocessed_bits < 448) {
611 			(void) memset((hashState256(state)->LastPart) +
612 			    LastByte + 1, 0x00,
613 			    EdonR256_BLOCK_SIZE - LastByte - 9);
614 			databitlen = EdonR256_BLOCK_SIZE * 8;
615 #if defined(MACHINE_IS_BIG_ENDIAN)
616 			st_swap64(num_bits, data64 + 7);
617 #else
618 			data64[7] = num_bits;
619 #endif
620 		} else {
621 			(void) memset((hashState256(state)->LastPart) +
622 			    LastByte + 1, 0x00,
623 			    EdonR256_BLOCK_SIZE * 2 - LastByte - 9);
624 			databitlen = EdonR256_BLOCK_SIZE * 16;
625 #if defined(MACHINE_IS_BIG_ENDIAN)
626 			st_swap64(num_bits, data64 + 15);
627 #else
628 			data64[15] = num_bits;
629 #endif
630 		}
631 
632 		/* LINTED E_BAD_PTR_CAST_ALIGN */
633 		data32 = (uint32_t *)hashState256(state)->LastPart;
634 		state->bits_processed += Q256(databitlen, data32,
635 		    hashState256(state)->DoublePipe);
636 		break;
637 
638 	case 384:
639 	case 512:
640 		LastByte = (int)state->unprocessed_bits >> 3;
641 		PadOnePosition = 7 - (state->unprocessed_bits & 0x07);
642 		hashState512(state)->LastPart[LastByte] =
643 		    (hashState512(state)->LastPart[LastByte]
644 		    & (0xff << (PadOnePosition + 1))) ^
645 		    (0x01 << PadOnePosition);
646 		/* LINTED E_BAD_PTR_CAST_ALIGN */
647 		data64 = (uint64_t *)hashState512(state)->LastPart;
648 
649 		if (state->unprocessed_bits < 960) {
650 			(void) memset((hashState512(state)->LastPart) +
651 			    LastByte + 1, 0x00,
652 			    EdonR512_BLOCK_SIZE - LastByte - 9);
653 			databitlen = EdonR512_BLOCK_SIZE * 8;
654 #if defined(MACHINE_IS_BIG_ENDIAN)
655 			st_swap64(num_bits, data64 + 15);
656 #else
657 			data64[15] = num_bits;
658 #endif
659 		} else {
660 			(void) memset((hashState512(state)->LastPart) +
661 			    LastByte + 1, 0x00,
662 			    EdonR512_BLOCK_SIZE * 2 - LastByte - 9);
663 			databitlen = EdonR512_BLOCK_SIZE * 16;
664 #if defined(MACHINE_IS_BIG_ENDIAN)
665 			st_swap64(num_bits, data64 + 31);
666 #else
667 			data64[31] = num_bits;
668 #endif
669 		}
670 
671 		state->bits_processed += Q512(databitlen, data64,
672 		    hashState512(state)->DoublePipe);
673 		break;
674 	}
675 
676 	switch (state->hashbitlen) {
677 	case 224: {
678 #if defined(MACHINE_IS_BIG_ENDIAN)
679 		uint32_t *d32 = (uint32_t *)hashval;
680 		uint32_t *s32 = hashState224(state)->DoublePipe + 9;
681 		int j;
682 
683 		for (j = 0; j < EdonR224_DIGEST_SIZE >> 2; j++)
684 			st_swap32(s32[j], d32 + j);
685 #else
686 		memcpy(hashval, hashState256(state)->DoublePipe + 9,
687 		    EdonR224_DIGEST_SIZE);
688 #endif
689 		break;
690 	}
691 	case 256: {
692 #if defined(MACHINE_IS_BIG_ENDIAN)
693 		uint32_t *d32 = (uint32_t *)hashval;
694 		uint32_t *s32 = hashState224(state)->DoublePipe + 8;
695 		int j;
696 
697 		for (j = 0; j < EdonR256_DIGEST_SIZE >> 2; j++)
698 			st_swap32(s32[j], d32 + j);
699 #else
700 		memcpy(hashval, hashState256(state)->DoublePipe + 8,
701 		    EdonR256_DIGEST_SIZE);
702 #endif
703 		break;
704 	}
705 	case 384: {
706 #if defined(MACHINE_IS_BIG_ENDIAN)
707 		uint64_t *d64 = (uint64_t *)hashval;
708 		uint64_t *s64 = hashState384(state)->DoublePipe + 10;
709 		int j;
710 
711 		for (j = 0; j < EdonR384_DIGEST_SIZE >> 3; j++)
712 			st_swap64(s64[j], d64 + j);
713 #else
714 		memcpy(hashval, hashState384(state)->DoublePipe + 10,
715 		    EdonR384_DIGEST_SIZE);
716 #endif
717 		break;
718 	}
719 	case 512: {
720 #if defined(MACHINE_IS_BIG_ENDIAN)
721 		uint64_t *d64 = (uint64_t *)hashval;
722 		uint64_t *s64 = hashState512(state)->DoublePipe + 8;
723 		int j;
724 
725 		for (j = 0; j < EdonR512_DIGEST_SIZE >> 3; j++)
726 			st_swap64(s64[j], d64 + j);
727 #else
728 		memcpy(hashval, hashState512(state)->DoublePipe + 8,
729 		    EdonR512_DIGEST_SIZE);
730 #endif
731 		break;
732 	}
733 	}
734 }
735 
736 
737 void
738 EdonRHash(size_t hashbitlen, const uint8_t *data, size_t databitlen,
739     uint8_t *hashval)
740 {
741 	EdonRState state;
742 
743 	EdonRInit(&state, hashbitlen);
744 	EdonRUpdate(&state, data, databitlen);
745 	EdonRFinal(&state, hashval);
746 }
747 
748 #ifdef _KERNEL
749 EXPORT_SYMBOL(EdonRInit);
750 EXPORT_SYMBOL(EdonRUpdate);
751 EXPORT_SYMBOL(EdonRHash);
752 EXPORT_SYMBOL(EdonRFinal);
753 #endif
754