xref: /freebsd/sys/contrib/openzfs/module/icp/algs/edonr/edonr.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Based on Edon-R implementation for SUPERCOP, based on NIST API.
24  * Copyright (c) 2009, 2010, Jørn Amundsen <jorn.amundsen@ntnu.no>
25  * Copyright (c) 2013 Saso Kiselkov, All rights reserved
26  * Copyright (c) 2023 Tino Reichardt <milky-zfs@mcmilk.de>
27  */
28 
29 #include <sys/zfs_context.h>
30 #include <sys/string.h>
31 #include <sys/edonr.h>
32 
33 /*
34  * We need 1196 byte stack for Q512() on i386
35  * - we define this pragma to make gcc happy
36  */
37 #if defined(__GNUC__) && defined(_ILP32)
38 #pragma GCC diagnostic ignored "-Wframe-larger-than="
39 #endif
40 
41 /*
42  * Insert compiler memory barriers to reduce stack frame size.
43  */
44 #define	MEMORY_BARRIER   asm volatile("" ::: "memory");
45 
46 #if defined(_ZFS_BIG_ENDIAN)
47 #define	ld_swap64(s, d) (d = __builtin_bswap64(*(s)))
48 #define	st_swap64(s, d) (*(d) = __builtin_bswap64(s))
49 #else
50 #define	ld_swap64(s, d) (d = *(s))
51 #define	st_swap64(s, d) (*(d) = s)
52 #endif
53 
54 #define	hashState512(x)	((x)->pipe->p512)
55 
56 /* rotate shortcuts */
57 #define	rotl64(x, n)	(((x) << (n)) | ((x) >> (64 - (n))))
58 
59 /* EdonR512 initial double chaining pipe */
60 static const uint64_t i512p2[16] = {
61 	0x8081828384858687ull, 0x88898a8b8c8d8e8full,
62 	0x9091929394959697ull, 0x98999a9b9c9d9e9full,
63 	0xa0a1a2a3a4a5a6a7ull, 0xa8a9aaabacadaeafull,
64 	0xb0b1b2b3b4b5b6b7ull, 0xb8b9babbbcbdbebfull,
65 	0xc0c1c2c3c4c5c6c7ull, 0xc8c9cacbcccdcecfull,
66 	0xd0d1d2d3d4d5d6d7ull, 0xd8d9dadbdcdddedfull,
67 	0xe0e1e2e3e4e5e6e7ull, 0xe8e9eaebecedeeefull,
68 	0xf0f1f2f3f4f5f6f7ull, 0xf8f9fafbfcfdfeffull
69 };
70 
71 #define	LS1_512(x0, x1, x2, x3, x4, x5, x6, x7)		\
72 {							\
73 	MEMORY_BARRIER					\
74 	z1 = x0 + x4, z2 = x1 + x7; z5 = z1 + z2;	\
75 	s0 = 0xaaaaaaaaaaaaaaaaull + z5 + x2;		\
76 	s1 = rotl64(z5 + x3, 5);			\
77 	s2 = rotl64(z5 + x6, 15); z3 = x2 + x3;		\
78 	s5 = rotl64(z1 + z3 + x5, 40); z4 = x5 + x6;	\
79 	s6 = rotl64(z2 + z4 + x0, 50); z6 = z3 + z4;	\
80 	s3 = rotl64(z6 + x7, 22);			\
81 	s4 = rotl64(z6 + x1, 31);			\
82 	s7 = rotl64(z6 + x4, 59);			\
83 }
84 
85 #define	LS2_512(y0, y1, y2, y3, y4, y5, y6, y7)		\
86 {							\
87 	z1 = y0 + y1, z2 = y2 + y5; z6 = z1 + z2;	\
88 	t0  = ~0xaaaaaaaaaaaaaaaaull + z6 + y7;		\
89 	t2 = rotl64(z6 + y3, 19);			\
90 	z3 = y3 + y4, z5 = z1 + z3;			\
91 	t1 = rotl64(z5 + y6, 10);			\
92 	t4 = rotl64(z5 + y5, 36);			\
93 	z4 = y6 + y7, z8 = z3 + z4;			\
94 	t3 = rotl64(z8 + y2, 29);			\
95 	t7 = rotl64(z8 + y0, 55); z7 = z2 + z4;		\
96 	t5 = rotl64(z7 + y4, 44);			\
97 	t6 = rotl64(z7 + y1, 48);			\
98 }
99 
100 #define	QEF_512(r0, r1, r2, r3, r4, r5, r6, r7)		\
101 {							\
102 	z1 = s0 ^ s4, z5 = t0 ^ t1;			\
103 	r0 = (z1 ^ s1) + (z5 ^ t5); z8 = t6 ^ t7;	\
104 	r1 = (z1 ^ s7) + (t2 ^ z8); z3 = s2 ^ s3;	\
105 	r7 = (z3 ^ s5) + (t4 ^ z8); z7 = t3 ^ t4;	\
106 	r3 = (z3 ^ s4) + (t0 ^ z7); z4 = s5 ^ s6;	\
107 	r5 = (s3 ^ z4) + (z7 ^ t6); z6 = t2 ^ t5;	\
108 	r6 = (s2 ^ z4) + (z6 ^ t7); z2 = s1 ^ s7;	\
109 	r4 = (s0 ^ z2) + (t1 ^ z6);			\
110 	r2 = (z2 ^ s6) + (z5 ^ t3);			\
111 }
112 
113 static inline size_t
114 Q512(size_t bitlen, const uint64_t *data, uint64_t *p)
115 {
116 	size_t bl;
117 
118 	for (bl = bitlen; bl >= EdonR512_BLOCK_BITSIZE;
119 	    bl -= EdonR512_BLOCK_BITSIZE, data += 16) {
120 		uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
121 		uint64_t p0, p1, p2, p3, p4, p5, p6, p7;
122 		uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
123 		uint64_t t0, t1, t2, t3, t4, t5, t6, t7;
124 		uint64_t z1, z2, z3, z4, z5, z6, z7, z8;
125 
126 #if defined(_ZFS_BIG_ENDIAN)
127 		uint64_t swp0, swp1, swp2, swp3, swp4, swp5, swp6, swp7,
128 		    swp8, swp9, swp10, swp11, swp12, swp13, swp14, swp15;
129 #define	d(j)	swp##j
130 #define	s64(j)	ld_swap64((uint64_t *)data+j, swp##j)
131 		s64(0);
132 		s64(1);
133 		s64(2);
134 		s64(3);
135 		s64(4);
136 		s64(5);
137 		s64(6);
138 		s64(7);
139 		s64(8);
140 		s64(9);
141 		s64(10);
142 		s64(11);
143 		s64(12);
144 		s64(13);
145 		s64(14);
146 		s64(15);
147 #else
148 #define	d(j)	data[j]
149 #endif
150 
151 		/* First row of quasigroup e-transformations */
152 		LS1_512(d(15), d(14), d(13), d(12), d(11), d(10), d(9), d(8));
153 		LS2_512(d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7));
154 		QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);
155 
156 		LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);
157 		LS2_512(d(8), d(9), d(10), d(11), d(12), d(13), d(14), d(15));
158 		QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);
159 
160 		/* Second row of quasigroup e-transformations */
161 		LS1_512(p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
162 		LS2_512(p0, p1, p2, p3, p4, p5, p6, p7);
163 		QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);
164 
165 		LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);
166 		LS2_512(q0, q1, q2, q3, q4, q5, q6, q7);
167 		QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);
168 
169 		/* Third row of quasigroup e-transformations */
170 		LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);
171 		LS2_512(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
172 		QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);
173 
174 		LS1_512(q0, q1, q2, q3, q4, q5, q6, q7);
175 		LS2_512(p0, p1, p2, p3, p4, p5, p6, p7);
176 		QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);
177 
178 		/* Fourth row of quasigroup e-transformations */
179 		LS1_512(d(7), d(6), d(5), d(4), d(3), d(2), d(1), d(0));
180 		LS2_512(p0, p1, p2, p3, p4, p5, p6, p7);
181 		QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);
182 
183 		LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);
184 		LS2_512(q0, q1, q2, q3, q4, q5, q6, q7);
185 		QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);
186 
187 		/* Edon-R tweak on the original SHA-3 Edon-R submission. */
188 		p[0] ^= d(8) ^ p0;
189 		p[1] ^= d(9) ^ p1;
190 		p[2] ^= d(10) ^ p2;
191 		p[3] ^= d(11) ^ p3;
192 		p[4] ^= d(12) ^ p4;
193 		p[5] ^= d(13) ^ p5;
194 		p[6] ^= d(14) ^ p6;
195 		p[7] ^= d(15) ^ p7;
196 		p[8] ^= d(0) ^ q0;
197 		p[9] ^= d(1) ^ q1;
198 		p[10] ^= d(2) ^ q2;
199 		p[11] ^= d(3) ^ q3;
200 		p[12] ^= d(4) ^ q4;
201 		p[13] ^= d(5) ^ q5;
202 		p[14] ^= d(6) ^ q6;
203 		p[15] ^= d(7) ^ q7;
204 	}
205 
206 #undef s64
207 #undef d
208 	return (bitlen - bl);
209 }
210 
211 void
212 EdonRInit(EdonRState *state)
213 {
214 	state->bits_processed = 0;
215 	state->unprocessed_bits = 0;
216 	memcpy(hashState512(state)->DoublePipe, i512p2, sizeof (i512p2));
217 }
218 
219 void
220 EdonRUpdate(EdonRState *state, const uint8_t *data, size_t databitlen)
221 {
222 	uint64_t *data64;
223 	size_t bits_processed;
224 
225 	if (state->unprocessed_bits > 0) {
226 		/* LastBytes = databitlen / 8 */
227 		int LastBytes = (int)databitlen >> 3;
228 
229 		ASSERT(state->unprocessed_bits + databitlen <=
230 		    EdonR512_BLOCK_SIZE * 8);
231 
232 		memcpy(hashState512(state)->LastPart
233 		    + (state->unprocessed_bits >> 3), data, LastBytes);
234 		state->unprocessed_bits += (int)databitlen;
235 		databitlen = state->unprocessed_bits;
236 		/* LINTED E_BAD_PTR_CAST_ALIGN */
237 		data64 = (uint64_t *)hashState512(state)->LastPart;
238 	} else
239 		/* LINTED E_BAD_PTR_CAST_ALIGN */
240 		data64 = (uint64_t *)data;
241 
242 	bits_processed = Q512(databitlen, data64,
243 	    hashState512(state)->DoublePipe);
244 	state->bits_processed += bits_processed;
245 	databitlen -= bits_processed;
246 	state->unprocessed_bits = (int)databitlen;
247 	if (databitlen > 0) {
248 		/* LastBytes = Ceil(databitlen / 8) */
249 		int LastBytes = ((~(((-(int)databitlen) >> 3) & 0x03ff)) + 1) \
250 		    & 0x03ff;
251 
252 		data64 += bits_processed >> 6;	/* byte size update */
253 		memmove(hashState512(state)->LastPart, data64, LastBytes);
254 	}
255 }
256 
257 void
258 EdonRFinal(EdonRState *state, uint8_t *hashval)
259 {
260 	uint64_t *data64, num_bits;
261 	size_t databitlen;
262 	int LastByte, PadOnePosition;
263 
264 	num_bits = state->bits_processed + state->unprocessed_bits;
265 	LastByte = (int)state->unprocessed_bits >> 3;
266 	PadOnePosition = 7 - (state->unprocessed_bits & 0x07);
267 	hashState512(state)->LastPart[LastByte] =
268 	    (hashState512(state)->LastPart[LastByte] \
269 	    & (0xff << (PadOnePosition + 1))) ^ (0x01 << PadOnePosition);
270 	/* LINTED E_BAD_PTR_CAST_ALIGN */
271 	data64 = (uint64_t *)hashState512(state)->LastPart;
272 
273 	if (state->unprocessed_bits < 960) {
274 		memset((hashState512(state)->LastPart) +
275 		    LastByte + 1, 0x00, EdonR512_BLOCK_SIZE - LastByte - 9);
276 		databitlen = EdonR512_BLOCK_SIZE * 8;
277 #if defined(_ZFS_BIG_ENDIAN)
278 		st_swap64(num_bits, data64 + 15);
279 #else
280 		data64[15] = num_bits;
281 #endif
282 	} else {
283 		memset((hashState512(state)->LastPart) + LastByte + 1,
284 		    0x00, EdonR512_BLOCK_SIZE * 2 - LastByte - 9);
285 		databitlen = EdonR512_BLOCK_SIZE * 16;
286 #if defined(_ZFS_BIG_ENDIAN)
287 		st_swap64(num_bits, data64 + 31);
288 #else
289 		data64[31] = num_bits;
290 #endif
291 	}
292 
293 	state->bits_processed += Q512(databitlen, data64,
294 	    hashState512(state)->DoublePipe);
295 
296 #if defined(_ZFS_BIG_ENDIAN)
297 	data64 = (uint64_t *)hashval;
298 	uint64_t *s64 = hashState512(state)->DoublePipe + 8;
299 	int j;
300 
301 	for (j = 0; j < EdonR512_DIGEST_SIZE >> 3; j++)
302 		st_swap64(s64[j], data64 + j);
303 #else
304 	memcpy(hashval, hashState512(state)->DoublePipe + 8,
305 	    EdonR512_DIGEST_SIZE);
306 #endif
307 }
308 
309 void
310 EdonRHash(const uint8_t *data, size_t databitlen, uint8_t *hashval)
311 {
312 	EdonRState state;
313 
314 	EdonRInit(&state);
315 	EdonRUpdate(&state, data, databitlen);
316 	EdonRFinal(&state, hashval);
317 }
318 
319 #ifdef _KERNEL
320 EXPORT_SYMBOL(EdonRInit);
321 EXPORT_SYMBOL(EdonRUpdate);
322 EXPORT_SYMBOL(EdonRHash);
323 EXPORT_SYMBOL(EdonRFinal);
324 #endif
325