1 /*
2 Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3 Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4 denoted as "the implementer".
5 
6 For more information, feedback or questions, please refer to our websites:
7 http://keccak.noekeon.org/
8 http://keyak.noekeon.org/
9 http://ketje.noekeon.org/
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 #ifndef _SnP_Relaned_h_
17 #define _SnP_Relaned_h_
18 
19 #define SnP_AddBytes(state, data, offset, length, SnP_AddLanes, SnP_AddBytesInLane, SnP_laneLengthInBytes) \
20     { \
21         if ((offset) == 0) { \
22             SnP_AddLanes(state, data, (length)/SnP_laneLengthInBytes); \
23             SnP_AddBytesInLane(state, \
24                 (length)/SnP_laneLengthInBytes, \
25                 (data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
26                 0, \
27                 (length)%SnP_laneLengthInBytes); \
28         } \
29         else { \
30             unsigned int _sizeLeft = (length); \
31             unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
32             unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
33             const unsigned char *_curData = (data); \
34             while(_sizeLeft > 0) { \
35                 unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
36                 if (_bytesInLane > _sizeLeft) \
37                     _bytesInLane = _sizeLeft; \
38                 SnP_AddBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
39                 _sizeLeft -= _bytesInLane; \
40                 _lanePosition++; \
41                 _offsetInLane = 0; \
42                 _curData += _bytesInLane; \
43             } \
44         } \
45     }
46 
47 #define SnP_OverwriteBytes(state, data, offset, length, SnP_OverwriteLanes, SnP_OverwriteBytesInLane, SnP_laneLengthInBytes) \
48     { \
49         if ((offset) == 0) { \
50             SnP_OverwriteLanes(state, data, (length)/SnP_laneLengthInBytes); \
51             SnP_OverwriteBytesInLane(state, \
52                 (length)/SnP_laneLengthInBytes, \
53                 (data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
54                 0, \
55                 (length)%SnP_laneLengthInBytes); \
56         } \
57         else { \
58             unsigned int _sizeLeft = (length); \
59             unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
60             unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
61             const unsigned char *_curData = (data); \
62             while(_sizeLeft > 0) { \
63                 unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
64                 if (_bytesInLane > _sizeLeft) \
65                     _bytesInLane = _sizeLeft; \
66                 SnP_OverwriteBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
67                 _sizeLeft -= _bytesInLane; \
68                 _lanePosition++; \
69                 _offsetInLane = 0; \
70                 _curData += _bytesInLane; \
71             } \
72         } \
73     }
74 
75 #define SnP_ExtractBytes(state, data, offset, length, SnP_ExtractLanes, SnP_ExtractBytesInLane, SnP_laneLengthInBytes) \
76     { \
77         if ((offset) == 0) { \
78             SnP_ExtractLanes(state, data, (length)/SnP_laneLengthInBytes); \
79             SnP_ExtractBytesInLane(state, \
80                 (length)/SnP_laneLengthInBytes, \
81                 (data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
82                 0, \
83                 (length)%SnP_laneLengthInBytes); \
84         } \
85         else { \
86             unsigned int _sizeLeft = (length); \
87             unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
88             unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
89             unsigned char *_curData = (data); \
90             while(_sizeLeft > 0) { \
91                 unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
92                 if (_bytesInLane > _sizeLeft) \
93                     _bytesInLane = _sizeLeft; \
94                 SnP_ExtractBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
95                 _sizeLeft -= _bytesInLane; \
96                 _lanePosition++; \
97                 _offsetInLane = 0; \
98                 _curData += _bytesInLane; \
99             } \
100         } \
101     }
102 
103 #define SnP_ExtractAndAddBytes(state, input, output, offset, length, SnP_ExtractAndAddLanes, SnP_ExtractAndAddBytesInLane, SnP_laneLengthInBytes) \
104     { \
105         if ((offset) == 0) { \
106             SnP_ExtractAndAddLanes(state, input, output, (length)/SnP_laneLengthInBytes); \
107             SnP_ExtractAndAddBytesInLane(state, \
108                 (length)/SnP_laneLengthInBytes, \
109                 (input)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
110                 (output)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
111                 0, \
112                 (length)%SnP_laneLengthInBytes); \
113         } \
114         else { \
115             unsigned int _sizeLeft = (length); \
116             unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
117             unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
118             const unsigned char *_curInput = (input); \
119             unsigned char *_curOutput = (output); \
120             while(_sizeLeft > 0) { \
121                 unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
122                 if (_bytesInLane > _sizeLeft) \
123                     _bytesInLane = _sizeLeft; \
124                 SnP_ExtractAndAddBytesInLane(state, _lanePosition, _curInput, _curOutput, _offsetInLane, _bytesInLane); \
125                 _sizeLeft -= _bytesInLane; \
126                 _lanePosition++; \
127                 _offsetInLane = 0; \
128                 _curInput += _bytesInLane; \
129                 _curOutput += _bytesInLane; \
130             } \
131         } \
132     }
133 
134 #endif
135