Skip to content

Commit cf8f28e

Browse files
authored
Add bitinterleaved inplace Keccak implementation (#43)
* bitinterleaved inplace Keccak Signed-off-by: Matthias J. Kannwischer <[email protected]> * format Signed-off-by: Matthias J. Kannwischer <[email protected]> * define PLATFORM_BYTE_ORDER Signed-off-by: Matthias J. Kannwischer <[email protected]> --------- Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent e4bc215 commit cf8f28e

File tree

2 files changed

+1082
-306
lines changed

2 files changed

+1082
-306
lines changed

fips202/SnP-Relaned.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
/*
3+
The eXtended Keccak Code Package (XKCP)
4+
https://github.com/XKCP/XKCP
5+
6+
Implementation by Gilles Van Assche and Ronny Van Keer, hereby denoted as "the implementer".
7+
8+
For more information, feedback or questions, please refer to the Keccak Team website:
9+
https://keccak.team/
10+
11+
To the extent possible under law, the implementer has waived all copyright
12+
and related or neighboring rights to the source code in this file.
13+
http://creativecommons.org/publicdomain/zero/1.0/
14+
15+
---
16+
17+
This file contains macros that help implement a permutation in a SnP-compatible way.
18+
It converts an implementation that implement state input/output functions
19+
in a lane-oriented fashion (i.e., using SnP_AddLanes() and SnP_AddBytesInLane,
20+
and similarly for Overwite, Extract and ExtractAndAdd) to the byte-oriented SnP.
21+
Please refer to SnP-documentation.h for more details.
22+
*/
23+
24+
#ifndef _SnP_Relaned_h_
25+
#define _SnP_Relaned_h_
26+
27+
#define SnP_AddBytes(state, data, offset, length, SnP_AddLanes, SnP_AddBytesInLane, SnP_laneLengthInBytes) \
28+
{ \
29+
if ((offset) == 0) { \
30+
SnP_AddLanes(state, data, (length)/SnP_laneLengthInBytes); \
31+
if ((length)%SnP_laneLengthInBytes > 0) \
32+
SnP_AddBytesInLane(state, \
33+
(length)/SnP_laneLengthInBytes, \
34+
(data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
35+
0, \
36+
(length)%SnP_laneLengthInBytes); \
37+
} \
38+
else { \
39+
unsigned int _sizeLeft = (length); \
40+
unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
41+
unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
42+
const unsigned char *_curData = (data); \
43+
while(_sizeLeft > 0) { \
44+
unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
45+
if (_bytesInLane > _sizeLeft) \
46+
_bytesInLane = _sizeLeft; \
47+
SnP_AddBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
48+
_sizeLeft -= _bytesInLane; \
49+
_lanePosition++; \
50+
_offsetInLane = 0; \
51+
_curData += _bytesInLane; \
52+
} \
53+
} \
54+
}
55+
56+
#define SnP_OverwriteBytes(state, data, offset, length, SnP_OverwriteLanes, SnP_OverwriteBytesInLane, SnP_laneLengthInBytes) \
57+
{ \
58+
if ((offset) == 0) { \
59+
SnP_OverwriteLanes(state, data, (length)/SnP_laneLengthInBytes); \
60+
if ((length)%SnP_laneLengthInBytes > 0) \
61+
SnP_OverwriteBytesInLane(state, \
62+
(length)/SnP_laneLengthInBytes, \
63+
(data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
64+
0, \
65+
(length)%SnP_laneLengthInBytes); \
66+
} \
67+
else { \
68+
unsigned int _sizeLeft = (length); \
69+
unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
70+
unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
71+
const unsigned char *_curData = (data); \
72+
while(_sizeLeft > 0) { \
73+
unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
74+
if (_bytesInLane > _sizeLeft) \
75+
_bytesInLane = _sizeLeft; \
76+
SnP_OverwriteBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
77+
_sizeLeft -= _bytesInLane; \
78+
_lanePosition++; \
79+
_offsetInLane = 0; \
80+
_curData += _bytesInLane; \
81+
} \
82+
} \
83+
}
84+
85+
#define SnP_ExtractBytes(state, data, offset, length, SnP_ExtractLanes, SnP_ExtractBytesInLane, SnP_laneLengthInBytes) \
86+
{ \
87+
if ((offset) == 0) { \
88+
SnP_ExtractLanes(state, data, (length)/SnP_laneLengthInBytes); \
89+
if ((length)%SnP_laneLengthInBytes > 0) \
90+
SnP_ExtractBytesInLane(state, \
91+
(length)/SnP_laneLengthInBytes, \
92+
(data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
93+
0, \
94+
(length)%SnP_laneLengthInBytes); \
95+
} \
96+
else { \
97+
unsigned int _sizeLeft = (length); \
98+
unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
99+
unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
100+
unsigned char *_curData = (data); \
101+
while(_sizeLeft > 0) { \
102+
unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
103+
if (_bytesInLane > _sizeLeft) \
104+
_bytesInLane = _sizeLeft; \
105+
SnP_ExtractBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
106+
_sizeLeft -= _bytesInLane; \
107+
_lanePosition++; \
108+
_offsetInLane = 0; \
109+
_curData += _bytesInLane; \
110+
} \
111+
} \
112+
}
113+
114+
#define SnP_ExtractAndAddBytes(state, input, output, offset, length, SnP_ExtractAndAddLanes, SnP_ExtractAndAddBytesInLane, SnP_laneLengthInBytes) \
115+
{ \
116+
if ((offset) == 0) { \
117+
SnP_ExtractAndAddLanes(state, input, output, (length)/SnP_laneLengthInBytes); \
118+
if ((length)%SnP_laneLengthInBytes > 0) \
119+
SnP_ExtractAndAddBytesInLane(state, \
120+
(length)/SnP_laneLengthInBytes, \
121+
(input)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
122+
(output)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
123+
0, \
124+
(length)%SnP_laneLengthInBytes); \
125+
} \
126+
else { \
127+
unsigned int _sizeLeft = (length); \
128+
unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
129+
unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
130+
const unsigned char *_curInput = (input); \
131+
unsigned char *_curOutput = (output); \
132+
while(_sizeLeft > 0) { \
133+
unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
134+
if (_bytesInLane > _sizeLeft) \
135+
_bytesInLane = _sizeLeft; \
136+
SnP_ExtractAndAddBytesInLane(state, _lanePosition, _curInput, _curOutput, _offsetInLane, _bytesInLane); \
137+
_sizeLeft -= _bytesInLane; \
138+
_lanePosition++; \
139+
_offsetInLane = 0; \
140+
_curInput += _bytesInLane; \
141+
_curOutput += _bytesInLane; \
142+
} \
143+
} \
144+
}
145+
146+
#endif

0 commit comments

Comments
 (0)