21# if __has_feature(undefined_behavior_sanitizer)
22# define ALLOW_MISALIGNED_ACCESS __attribute__((no_sanitize("alignment")))
25#ifndef ALLOW_MISALIGNED_ACCESS
26# define ALLOW_MISALIGNED_ACCESS
29typedef unsigned char UINT8;
30typedef unsigned long long int UINT64;
32#if defined(KeccakP1600_useLaneComplementing)
33#define UseBebigokimisa
37#define ROL64(a, offset) _rotl64(a, offset)
38#elif defined(KeccakP1600_useSHLD)
39 #define ROL64(x,N) ({ \
40 register UINT64 __out; \
41 register UINT64 __in = x; \
42 __asm__ ("shld %2,%0,%0" : "=r"(__out) : "0"(__in), "i"(N)); \
46#define ROL64(a, offset) ((((UINT64)a) << offset) ^ (((UINT64)a) >> (64-offset)))
49#include "KeccakP-1600-64.macros"
50#ifdef KeccakP1600_fullUnrolling
53#define Unrolling KeccakP1600_unrolling
55#include "KeccakP-1600-unrolling.macros"
58static const UINT64 KeccakF1600RoundConstants[24] = {
59 0x0000000000000001ULL,
60 0x0000000000008082ULL,
61 0x800000000000808aULL,
62 0x8000000080008000ULL,
63 0x000000000000808bULL,
64 0x0000000080000001ULL,
65 0x8000000080008081ULL,
66 0x8000000000008009ULL,
67 0x000000000000008aULL,
68 0x0000000000000088ULL,
69 0x0000000080008009ULL,
70 0x000000008000000aULL,
71 0x000000008000808bULL,
72 0x800000000000008bULL,
73 0x8000000000008089ULL,
74 0x8000000000008003ULL,
75 0x8000000000008002ULL,
76 0x8000000000000080ULL,
77 0x000000000000800aULL,
78 0x800000008000000aULL,
79 0x8000000080008081ULL,
80 0x8000000000008080ULL,
81 0x0000000080000001ULL,
82 0x8000000080008008ULL };
89#ifdef KeccakP1600_useLaneComplementing
103#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
117 for(i=0; i<length; i++)
128#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
130#ifdef NO_MISALIGNED_ACCESSES
132 if (((((uintptr_t)
state) & 7) != 0) || ((((uintptr_t)
data) & 7) != 0)) {
133 for (i = 0; i < laneCount * 8; i++) {
141 for( ; (i+8)<=laneCount; i+=8) {
151 for( ; (i+4)<=laneCount; i+=4) {
157 for( ; (i+2)<=laneCount; i+=2) {
168 for(i=0; i<laneCount; i++, curData+=8) {
170 | ((
UINT64)curData[1] << 8)
171 | ((
UINT64)curData[2] << 16)
172 | ((
UINT64)curData[3] << 24)
173 | ((
UINT64)curData[4] <<32)
174 | ((
UINT64)curData[5] << 40)
175 | ((
UINT64)curData[6] << 48)
176 | ((
UINT64)curData[7] << 56);
184#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
204#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
205#ifdef KeccakP1600_useLaneComplementing
206 if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
208 for(i=0; i<length; i++)
217#error "Not yet implemented"
225#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
226#ifdef KeccakP1600_useLaneComplementing
227 unsigned int lanePosition;
229 for(lanePosition=0; lanePosition<laneCount; lanePosition++)
230 if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
238#error "Not yet implemented"
253#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
254#ifdef KeccakP1600_useLaneComplementing
255 unsigned int lanePosition;
257 for(lanePosition=0; lanePosition<byteCount/8; lanePosition++)
258 if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
262 if (byteCount%8 != 0) {
263 lanePosition = byteCount/8;
264 if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
265 memset((
unsigned char*)
state+lanePosition*8, 0xFF, byteCount%8);
267 memset((
unsigned char*)
state+lanePosition*8, 0, byteCount%8);
273#error "Not yet implemented"
285 copyFromState(
A, stateAsLanes)
287 copyToState(stateAsLanes,
A)
296 #ifndef KeccakP1600_fullUnrolling
301 copyFromState(
A, stateAsLanes)
303 copyToState(stateAsLanes,
A)
311 #ifndef KeccakP1600_fullUnrolling
316 copyFromState(
A, stateAsLanes)
318 copyToState(stateAsLanes,
A)
326#ifdef KeccakP1600_useLaneComplementing
327 if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
330#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
339 for(i=0; i<length; i++) {
340 data[i] = lane & 0xFF;
348#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
349void fromWordToBytes(
UINT8 *bytes,
const UINT64 word)
353 for(i=0; i<(64/8); i++)
354 bytes[i] = (word >> (8*i)) & 0xFF;
360#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
365 for(i=0; i<laneCount; i++)
368#ifdef KeccakP1600_useLaneComplementing
375 if (laneCount > 12) {
377 if (laneCount > 17) {
379 if (laneCount > 20) {
402#ifdef KeccakP1600_useLaneComplementing
403 if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
406#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
411 for(i=0; i<length; i++)
417 for(i=0; i<length; i++) {
418 output[i] = input[i] ^ (lane & 0xFF);
429#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
430 unsigned char temp[8];
434 for(i=0; i<laneCount; i++) {
435#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
440 output[i*8+
j] = input[i*8+
j] ^ temp[
j];
443#ifdef KeccakP1600_useLaneComplementing
450 if (laneCount > 12) {
452 if (laneCount > 17) {
454 if (laneCount > 20) {
477 size_t originalDataByteLen = dataByteLen;
479 #ifndef KeccakP1600_fullUnrolling
485 copyFromState(
A, stateAsLanes)
486 while(dataByteLen >= laneCount*8) {
487 addInput(
A, inDataAsLanes, laneCount)
489 inDataAsLanes += laneCount;
490 dataByteLen -= laneCount*8;
492 copyToState(stateAsLanes,
A)
493 return originalDataByteLen - dataByteLen;
ALLOW_MISALIGNED_ACCESS void KeccakP1600_ExtractAndAddLanes(const void *state, const unsigned char *input, unsigned char *output, unsigned int laneCount)
void KeccakP1600_OverwriteBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
ALLOW_MISALIGNED_ACCESS void KeccakP1600_AddLanes(void *state, const unsigned char *data, unsigned int laneCount)
void KeccakP1600_ExtractAndAddBytesInLane(const void *state, unsigned int lanePosition, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
ALLOW_MISALIGNED_ACCESS void KeccakP1600_OverwriteLanes(void *state, const unsigned char *data, unsigned int laneCount)
void KeccakP1600_ExtractBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length)
ALLOW_MISALIGNED_ACCESS void KeccakP1600_ExtractLanes(const void *state, unsigned char *data, unsigned int laneCount)
void KeccakP1600_AddBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
#define ALLOW_MISALIGNED_ACCESS
void KeccakP1600_OverwriteWithZeroes(void *state, unsigned int byteCount)
void KeccakP1600_ExtractAndAddBytes(const void *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
void KeccakP1600_Permute_24rounds(void *state)
void KeccakP1600_OverwriteBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
void KeccakP1600_ExtractLanes(const void *state, unsigned char *data, unsigned int laneCount)
void KeccakP1600_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length)
void KeccakP1600_Permute_Nrounds(void *state, unsigned int nr)
ALLOW_MISALIGNED_ACCESS void KeccakP1600_AddLanes(void *state, const unsigned char *data, unsigned int laneCount)
ALLOW_MISALIGNED_ACCESS size_t KeccakF1600_FastLoop_Absorb(void *state, unsigned int laneCount, const unsigned char *data, size_t dataByteLen)
void KeccakP1600_ExtractAndAddBytesInLane(const void *state, unsigned int lanePosition, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
void KeccakP1600_ExtractAndAddLanes(const void *state, const unsigned char *input, unsigned char *output, unsigned int laneCount)
void KeccakP1600_Initialize(void *state)
void KeccakP1600_Permute_12rounds(void *state)
void KeccakP1600_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
void KeccakP1600_ExtractBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length)
void KeccakP1600_AddBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
unsigned long long int UINT64
void KeccakP1600_OverwriteLanes(void *state, const unsigned char *data, unsigned int laneCount)
void KeccakP1600_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
memset(ptr, 0, type->size)
#define SnP_AddBytes(state, data, offset, length, SnP_AddLanes, SnP_AddBytesInLane, SnP_laneLengthInBytes)
#define SnP_ExtractAndAddBytes(state, input, output, offset, length, SnP_ExtractAndAddLanes, SnP_ExtractAndAddBytesInLane, SnP_laneLengthInBytes)
#define SnP_OverwriteBytes(state, data, offset, length, SnP_OverwriteLanes, SnP_OverwriteBytesInLane, SnP_laneLengthInBytes)
#define SnP_ExtractBytes(state, data, offset, length, SnP_ExtractLanes, SnP_ExtractBytesInLane, SnP_laneLengthInBytes)
#define KeccakP1600_AddByte(state, byte, offset)