summaryrefslogtreecommitdiff
path: root/lib/liboqs/src/common/sha3/xkcp_low/KeccakP-1600/plain-64bits/KeccakP-1600-opt64.c
blob: d813a3679bb7cc7eb9289f9a51d84bdd255c280e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
The eXtended Keccak Code Package (XKCP)
https://github.com/XKCP/XKCP

The Keccak-p permutations, designed by Guido Bertoni, Joan Daemen, Michaƫl Peeters and Gilles Van Assche.

Implementation by Gilles Van Assche and Ronny Van Keer, hereby denoted as "the implementer".

For more information, feedback or questions, please refer to the Keccak Team website:
https://keccak.team/

To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/

---

This file implements Keccak-p[1600] in a SnP-compatible way.
Please refer to SnP-documentation.h for more details.

This implementation comes with KeccakP-1600-SnP.h in the same folder.
Please refer to LowLevel.build for the exact list of other files it must be combined with.
*/

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "brg_endian.h"
#include "KeccakP-1600-SnP.h"
#include "KeccakP-1600-opt64-config.h"

#define UseBebigokimisa

#if defined(_MSC_VER)
#define ROL64(a, offset) _rotl64(a, offset)
#elif defined(KeccakP1600_useSHLD)
#define ROL64(x,N) ({ \
    register uint64_t __out; \
    register uint64_t __in = x; \
    __asm__ ("shld %2,%0,%0" : "=r"(__out) : "0"(__in), "i"(N)); \
    __out; \
    })
#else
#define ROL64(a, offset) ((((uint64_t)a) << offset) ^ (((uint64_t)a) >> (64-offset)))
#endif

#include "KeccakP-1600-64.macros"
#define FullUnrolling
#include "KeccakP-1600-unrolling.macros"
#include "SnP-Relaned.h"

static const uint64_t KeccakF1600RoundConstants[24] = {
	0x0000000000000001ULL,
	0x0000000000008082ULL,
	0x800000000000808aULL,
	0x8000000080008000ULL,
	0x000000000000808bULL,
	0x0000000080000001ULL,
	0x8000000080008081ULL,
	0x8000000000008009ULL,
	0x000000000000008aULL,
	0x0000000000000088ULL,
	0x0000000080008009ULL,
	0x000000008000000aULL,
	0x000000008000808bULL,
	0x800000000000008bULL,
	0x8000000000008089ULL,
	0x8000000000008003ULL,
	0x8000000000008002ULL,
	0x8000000000000080ULL,
	0x000000000000800aULL,
	0x800000008000000aULL,
	0x8000000080008081ULL,
	0x8000000000008080ULL,
	0x0000000080000001ULL,
	0x8000000080008008ULL
};

/* ---------------------------------------------------------------- */

void KeccakP1600_StaticInitialize(void) { }

/* ---------------------------------------------------------------- */

void KeccakP1600_Initialize(void *state) {
	memset(state, 0, 200);
	((uint64_t *)state)[ 1] = ~(uint64_t)0;
	((uint64_t *)state)[ 2] = ~(uint64_t)0;
	((uint64_t *)state)[ 8] = ~(uint64_t)0;
	((uint64_t *)state)[12] = ~(uint64_t)0;
	((uint64_t *)state)[17] = ~(uint64_t)0;
	((uint64_t *)state)[20] = ~(uint64_t)0;
}

/* ---------------------------------------------------------------- */

void KeccakP1600_AddBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	uint64_t lane;
	if (length == 0) {
		return;
	}
	if (length == 1) {
		lane = data[0];
	} else {
		lane = 0;
		memcpy(&lane, data, length);
	}
	lane <<= offset * 8;
#else
	uint64_t lane = 0;
	unsigned int i;
	for (i = 0; i < length; i++) {
		lane |= ((uint64_t)data[i]) << ((i + offset) * 8);
	}
#endif
	((uint64_t *)state)[lanePosition] ^= lane;
}

/* ---------------------------------------------------------------- */

void KeccakP1600_AddLanes(void *state, const unsigned char *data, unsigned int laneCount) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	unsigned int i = 0;
	/* If either pointer is misaligned, fall back to byte-wise xor. */
	if (((((uintptr_t)state) & 7) != 0) || ((((uintptr_t)data) & 7) != 0)) {
		for (i = 0; i < laneCount * 8; i++) {
			((unsigned char *)state)[i] ^= data[i];
		}
	} else {
		/* Otherwise... */
		for ( ; (i + 8) <= laneCount; i += 8) {
			((uint64_t *)state)[i + 0] ^= ((const uint64_t *)data)[i + 0];
			((uint64_t *)state)[i + 1] ^= ((const uint64_t *)data)[i + 1];
			((uint64_t *)state)[i + 2] ^= ((const uint64_t *)data)[i + 2];
			((uint64_t *)state)[i + 3] ^= ((const uint64_t *)data)[i + 3];
			((uint64_t *)state)[i + 4] ^= ((const uint64_t *)data)[i + 4];
			((uint64_t *)state)[i + 5] ^= ((const uint64_t *)data)[i + 5];
			((uint64_t *)state)[i + 6] ^= ((const uint64_t *)data)[i + 6];
			((uint64_t *)state)[i + 7] ^= ((const uint64_t *)data)[i + 7];
		}
		for ( ; (i + 4) <= laneCount; i += 4) {
			((uint64_t *)state)[i + 0] ^= ((const uint64_t *)data)[i + 0];
			((uint64_t *)state)[i + 1] ^= ((const uint64_t *)data)[i + 1];
			((uint64_t *)state)[i + 2] ^= ((const uint64_t *)data)[i + 2];
			((uint64_t *)state)[i + 3] ^= ((const uint64_t *)data)[i + 3];
		}
		for ( ; (i + 2) <= laneCount; i += 2) {
			((uint64_t *)state)[i + 0] ^= ((const uint64_t *)data)[i + 0];
			((uint64_t *)state)[i + 1] ^= ((const uint64_t *)data)[i + 1];
		}
		if (i < laneCount) {
			((uint64_t *)state)[i + 0] ^= ((const uint64_t *)data)[i + 0];
		}
	}
#else
	unsigned int i;
	const uint8_t *curData = data;
	for (i = 0; i < laneCount; i++, curData += 8) {
		uint64_t lane = (uint64_t)curData[0]
		                | ((uint64_t)curData[1] <<  8)
		                | ((uint64_t)curData[2] << 16)
		                | ((uint64_t)curData[3] << 24)
		                | ((uint64_t)curData[4] << 32)
		                | ((uint64_t)curData[5] << 40)
		                | ((uint64_t)curData[6] << 48)
		                | ((uint64_t)curData[7] << 56);
		((uint64_t *)state)[i] ^= lane;
	}
#endif
}

/* ---------------------------------------------------------------- */

#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
void KeccakP1600_AddByte(void *state, unsigned char byte, unsigned int offset) {
	((unsigned char *)state)[offset] ^= byte;
}
#else
void KeccakP1600_AddByte(void *state, unsigned char byte, unsigned int offset) {
	uint64_t lane = byte;
	lane <<= (offset % 8) * 8;
	((uint64_t *)state)[offset / 8] ^= lane;
}
#endif

/* ---------------------------------------------------------------- */

void KeccakP1600_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length) {
	SnP_AddBytes(state, data, offset, length, KeccakP1600_AddLanes, KeccakP1600_AddBytesInLane, 8);
}

/* ---------------------------------------------------------------- */

void KeccakP1600_OverwriteBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
		unsigned int i;
		for (i = 0; i < length; i++) {
			((unsigned char *)state)[lanePosition * 8 + offset + i] = ~data[i];
		}
	} else {
		memcpy((unsigned char *)state + lanePosition * 8 + offset, data, length);
	}
#else
	uint64_t lane = ((uint64_t *)state)[lanePosition];
	unsigned int i;
	for (i = 0; i < length; i++) {
		lane &= ~((uint64_t)0xFF << ((offset + i) * 8));
		if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
			lane |= (uint64_t)(data[i] ^ 0xFF) << ((offset + i) * 8);
		} else {
			lane |= (uint64_t)data[i] << ((offset + i) * 8);
		}
	}
	((uint64_t *)state)[lanePosition] = lane;
#endif
}

/* ---------------------------------------------------------------- */

void KeccakP1600_OverwriteLanes(void *state, const unsigned char *data, unsigned int laneCount) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	unsigned int lanePosition;

	for (lanePosition = 0; lanePosition < laneCount; lanePosition++) {
		memcpy(((uint64_t *)state) + lanePosition, data, sizeof(uint64_t));
		if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
			((uint64_t *)state)[lanePosition] = ~((uint64_t *)state)[lanePosition];
		}
	}
#else
	unsigned int lanePosition;
	const uint8_t *curData = data;
	for (lanePosition = 0; lanePosition < laneCount; lanePosition++, curData += 8) {
		uint64_t lane = (uint64_t)curData[0]
		                | ((uint64_t)curData[1] <<  8)
		                | ((uint64_t)curData[2] << 16)
		                | ((uint64_t)curData[3] << 24)
		                | ((uint64_t)curData[4] << 32)
		                | ((uint64_t)curData[5] << 40)
		                | ((uint64_t)curData[6] << 48)
		                | ((uint64_t)curData[7] << 56);
		if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
			((uint64_t *)state)[lanePosition] = ~lane;
		} else {
			((uint64_t *)state)[lanePosition] = lane;
		}
	}
#endif
}

/* ---------------------------------------------------------------- */

void KeccakP1600_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length) {
	SnP_OverwriteBytes(state, data, offset, length, KeccakP1600_OverwriteLanes, KeccakP1600_OverwriteBytesInLane, 8);
}

/* ---------------------------------------------------------------- */

void KeccakP1600_OverwriteWithZeroes(void *state, unsigned int byteCount) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	unsigned int lanePosition;

	for (lanePosition = 0; lanePosition < byteCount / 8; lanePosition++)
		if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
			((uint64_t *)state)[lanePosition] = ~(uint64_t)0;
		} else {
			((uint64_t *)state)[lanePosition] = 0;
		}
	if (byteCount % 8 != 0) {
		lanePosition = byteCount / 8;
		if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
			memset((unsigned char *)state + lanePosition * 8, 0xFF, byteCount % 8);
		} else {
			memset((unsigned char *)state + lanePosition * 8, 0, byteCount % 8);
		}
	}
#else
	unsigned int i, j;
	for (i = 0; i < byteCount; i += 8) {
		unsigned int lanePosition = i / 8;
		if (i + 8 <= byteCount) {
			if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
				((uint64_t *)state)[lanePosition] = ~(uint64_t)0;
			} else {
				((uint64_t *)state)[lanePosition] = 0;
			}
		} else {
			uint64_t lane = ((uint64_t *)state)[lanePosition];
			for (j = 0; j < byteCount % 8; j++) {
				if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
					lane |= (uint64_t)0xFF << (j * 8);
				} else {
					lane &= ~((uint64_t)0xFF << (j * 8));
				}
			}
			((uint64_t *)state)[lanePosition] = lane;
		}
	}
#endif
}

/* ---------------------------------------------------------------- */

void KeccakP1600_Permute_Nrounds(void *state, unsigned int nr) {
	declareABCDE
	unsigned int i;
	uint64_t *stateAsLanes = (uint64_t *)state;

	copyFromState(A, stateAsLanes)
	roundsN(nr)
	copyToState(stateAsLanes, A)

}

/* ---------------------------------------------------------------- */

void KeccakP1600_Permute_24rounds(void *state) {
	declareABCDE
	uint64_t *stateAsLanes = (uint64_t *)state;

	copyFromState(A, stateAsLanes)
	rounds24
	copyToState(stateAsLanes, A)
}

/* ---------------------------------------------------------------- */

void KeccakP1600_Permute_12rounds(void *state) {
	declareABCDE
	uint64_t *stateAsLanes = (uint64_t *)state;

	copyFromState(A, stateAsLanes)
	rounds12
	copyToState(stateAsLanes, A)
}

/* ---------------------------------------------------------------- */

void KeccakP1600_ExtractBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length) {
	uint64_t lane = ((const uint64_t *)state)[lanePosition];
	if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
		lane = ~lane;
	}
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	{
		uint64_t lane1[1];
		lane1[0] = lane;
		memcpy(data, (uint8_t *)lane1 + offset, length);
	}
#else
	unsigned int i;
	lane >>= offset * 8;
	for (i = 0; i < length; i++) {
		data[i] = lane & 0xFF;
		lane >>= 8;
	}
#endif
}

/* ---------------------------------------------------------------- */

#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
static void fromWordToBytes(uint8_t *bytes, const uint64_t word) {
	unsigned int i;

	for (i = 0; i < (64 / 8); i++) {
		bytes[i] = (word >> (8 * i)) & 0xFF;
	}
}
#endif

void KeccakP1600_ExtractLanes(const void *state, unsigned char *data, unsigned int laneCount) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	memcpy(data, state, laneCount * 8);
#else
	for (unsigned int i = 0; i < laneCount; i++) {
		fromWordToBytes(data + (i * 8), ((const uint64_t *)state)[i]);
	}
#endif
#define COMPL(lane) for(unsigned int i=0; i<8; i++) data[8*lane+i] = ~data[8*lane+i]
	if (laneCount > 1) {
		COMPL(1);
		if (laneCount > 2) {
			COMPL(2);
			if (laneCount > 8) {
				COMPL(8);
				if (laneCount > 12) {
					COMPL(12);
					if (laneCount > 17) {
						COMPL(17);
						if (laneCount > 20) {
							COMPL(20);
						}
					}
				}
			}
		}
	}
#undef COMPL
}

/* ---------------------------------------------------------------- */

void KeccakP1600_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length) {
	SnP_ExtractBytes(state, data, offset, length, KeccakP1600_ExtractLanes, KeccakP1600_ExtractBytesInLane, 8);
}

/* ---------------------------------------------------------------- */

void KeccakP1600_ExtractAndAddBytesInLane(const void *state, unsigned int lanePosition, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length) {
	uint64_t lane = ((const uint64_t *)state)[lanePosition];
	if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
		lane = ~lane;
	}
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
	{
		unsigned int i;
		for (i = 0; i < length; i++) {
			output[i] = input[i] ^ ((uint8_t *)&lane)[offset + i];
		}
	}
#else
	unsigned int i;
	lane >>= offset * 8;
	for (i = 0; i < length; i++) {
		output[i] = input[i] ^ (lane & 0xFF);
		lane >>= 8;
	}
#endif
}

/* ---------------------------------------------------------------- */

void KeccakP1600_ExtractAndAddLanes(const void *state, const unsigned char *input, unsigned char *output, unsigned int laneCount) {
	unsigned int i;
#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
	unsigned char temp[8];
	unsigned int j;
#else
	uint64_t lane;
#endif

	for (i = 0; i < laneCount; i++) {
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
		memcpy(&lane, input + 8 * i, sizeof(uint64_t));
		lane ^= ((const uint64_t *)state)[i];
		memcpy(output + 8 * i, &lane, sizeof(uint64_t));
#else
		fromWordToBytes(temp, ((const uint64_t *)state)[i]);
		for (j = 0; j < 8; j++) {
			output[i * 8 + j] = input[i * 8 + j] ^ temp[j];
		}
#endif
	}
#define COMPL(lane) for(i=0; i<8; i++) output[8*lane+i] = ~output[8*lane+i]
	if (laneCount > 1) {
		COMPL(1);
		if (laneCount > 2) {
			COMPL(2);
			if (laneCount > 8) {
				COMPL(8);
				if (laneCount > 12) {
					COMPL(12);
					if (laneCount > 17) {
						COMPL(17);
						if (laneCount > 20) {
							COMPL(20);
						}
					}
				}
			}
		}
	}
#undef COMPL
}

/* ---------------------------------------------------------------- */

void KeccakP1600_ExtractAndAddBytes(const void *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length) {
	SnP_ExtractAndAddBytes(state, input, output, offset, length, KeccakP1600_ExtractAndAddLanes, KeccakP1600_ExtractAndAddBytesInLane, 8);
}

/* ---------------------------------------------------------------- */

size_t KeccakF1600_FastLoop_Absorb(void *state, unsigned int laneCount, const unsigned char *data, size_t dataByteLen) {
	size_t originalDataByteLen = dataByteLen;
	declareABCDE
	uint64_t *stateAsLanes = (uint64_t *)state;

	copyFromState(A, stateAsLanes)
	while (dataByteLen >= laneCount * 8) {
		addInput(A, data, laneCount)
		rounds24
		data += laneCount * 8;
		dataByteLen -= laneCount * 8;
	}
	copyToState(stateAsLanes, A)
	return originalDataByteLen - dataByteLen;
}

/* ---------------------------------------------------------------- */

size_t KeccakP1600_12rounds_FastLoop_Absorb(void *state, unsigned int laneCount, const unsigned char *data, size_t dataByteLen) {
	size_t originalDataByteLen = dataByteLen;
	declareABCDE
	uint64_t *stateAsLanes = (uint64_t *)state;

	copyFromState(A, stateAsLanes)
	while (dataByteLen >= laneCount * 8) {
		addInput(A, data, laneCount)
		rounds12
		data += laneCount * 8;
		dataByteLen -= laneCount * 8;
	}
	copyToState(stateAsLanes, A)
	return originalDataByteLen - dataByteLen;
}