1/* 2Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, 3Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby 4denoted as "the implementer". 5 6For more information, feedback or questions, please refer to our websites: 7http://keccak.noekeon.org/ 8http://keyak.noekeon.org/ 9http://ketje.noekeon.org/ 10 11To the extent possible under law, the implementer has waived all copyright 12and related or neighboring rights to the source code in this file. 13http://creativecommons.org/publicdomain/zero/1.0/ 14*/ 15 16/* expect PlSnP_baseParallelism, PlSnP_targetParallelism */ 17 18/* expect SnP_stateSizeInBytes, SnP_stateAlignment */ 19 20/* expect prefix */ 21 22/* expect SnP_* */ 23 24 25#define JOIN0(a, b) a ## b 26#define JOIN(a, b) JOIN0(a, b) 27 28#define PlSnP_StaticInitialize JOIN(prefix, _StaticInitialize) 29#define PlSnP_InitializeAll JOIN(prefix, _InitializeAll) 30#define PlSnP_AddByte JOIN(prefix, _AddByte) 31#define PlSnP_AddBytes JOIN(prefix, _AddBytes) 32#define PlSnP_AddLanesAll JOIN(prefix, _AddLanesAll) 33#define PlSnP_OverwriteBytes JOIN(prefix, _OverwriteBytes) 34#define PlSnP_OverwriteLanesAll JOIN(prefix, _OverwriteLanesAll) 35#define PlSnP_OverwriteWithZeroes JOIN(prefix, _OverwriteWithZeroes) 36#define PlSnP_ExtractBytes JOIN(prefix, _ExtractBytes) 37#define PlSnP_ExtractLanesAll JOIN(prefix, _ExtractLanesAll) 38#define PlSnP_ExtractAndAddBytes JOIN(prefix, _ExtractAndAddBytes) 39#define PlSnP_ExtractAndAddLanesAll JOIN(prefix, _ExtractAndAddLanesAll) 40 41#if (PlSnP_baseParallelism == 1) 42 #define SnP_stateSizeInBytes JOIN(SnP, _stateSizeInBytes) 43 #define SnP_stateAlignment JOIN(SnP, _stateAlignment) 44#else 45 #define SnP_stateSizeInBytes JOIN(SnP, _statesSizeInBytes) 46 #define SnP_stateAlignment JOIN(SnP, _statesAlignment) 47#endif 48#define PlSnP_factor ((PlSnP_targetParallelism)/(PlSnP_baseParallelism)) 49#define SnP_stateOffset (((SnP_stateSizeInBytes+(SnP_stateAlignment-1))/SnP_stateAlignment)*SnP_stateAlignment) 50#define stateWithIndex(i) ((unsigned char *)states+((i)*SnP_stateOffset)) 51 52#define SnP_StaticInitialize JOIN(SnP, _StaticInitialize) 53#define SnP_Initialize JOIN(SnP, _Initialize) 54#define SnP_InitializeAll JOIN(SnP, _InitializeAll) 55#define SnP_AddByte JOIN(SnP, _AddByte) 56#define SnP_AddBytes JOIN(SnP, _AddBytes) 57#define SnP_AddLanesAll JOIN(SnP, _AddLanesAll) 58#define SnP_OverwriteBytes JOIN(SnP, _OverwriteBytes) 59#define SnP_OverwriteLanesAll JOIN(SnP, _OverwriteLanesAll) 60#define SnP_OverwriteWithZeroes JOIN(SnP, _OverwriteWithZeroes) 61#define SnP_ExtractBytes JOIN(SnP, _ExtractBytes) 62#define SnP_ExtractLanesAll JOIN(SnP, _ExtractLanesAll) 63#define SnP_ExtractAndAddBytes JOIN(SnP, _ExtractAndAddBytes) 64#define SnP_ExtractAndAddLanesAll JOIN(SnP, _ExtractAndAddLanesAll) 65 66void PlSnP_StaticInitialize( void ) 67{ 68 SnP_StaticInitialize(); 69} 70 71void PlSnP_InitializeAll(void *states) 72{ 73 unsigned int i; 74 75 for(i=0; i<PlSnP_factor; i++) 76 #if (PlSnP_baseParallelism == 1) 77 SnP_Initialize(stateWithIndex(i)); 78 #else 79 SnP_InitializeAll(stateWithIndex(i)); 80 #endif 81} 82 83void PlSnP_AddByte(void *states, unsigned int instanceIndex, unsigned char byte, unsigned int offset) 84{ 85 #if (PlSnP_baseParallelism == 1) 86 SnP_AddByte(stateWithIndex(instanceIndex), byte, offset); 87 #else 88 SnP_AddByte(stateWithIndex(instanceIndex/PlSnP_baseParallelism), instanceIndex%PlSnP_baseParallelism, byte, offset); 89 #endif 90} 91 92void PlSnP_AddBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length) 93{ 94 #if (PlSnP_baseParallelism == 1) 95 SnP_AddBytes(stateWithIndex(instanceIndex), data, offset, length); 96 #else 97 SnP_AddBytes(stateWithIndex(instanceIndex/PlSnP_baseParallelism), instanceIndex%PlSnP_baseParallelism, data, offset, length); 98 #endif 99} 100 101void PlSnP_AddLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset) 102{ 103 unsigned int i; 104 105 for(i=0; i<PlSnP_factor; i++) { 106 #if (PlSnP_baseParallelism == 1) 107 SnP_AddBytes(stateWithIndex(i), data, 0, laneCount*SnP_laneLengthInBytes); 108 #else 109 SnP_AddLanesAll(stateWithIndex(i), data, laneCount, laneOffset); 110 #endif 111 data += PlSnP_baseParallelism*laneOffset*SnP_laneLengthInBytes; 112 } 113} 114 115void PlSnP_OverwriteBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length) 116{ 117 #if (PlSnP_baseParallelism == 1) 118 SnP_OverwriteBytes(stateWithIndex(instanceIndex), data, offset, length); 119 #else 120 SnP_OverwriteBytes(stateWithIndex(instanceIndex/PlSnP_baseParallelism), instanceIndex%PlSnP_baseParallelism, data, offset, length); 121 #endif 122} 123 124void PlSnP_OverwriteLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset) 125{ 126 unsigned int i; 127 128 for(i=0; i<PlSnP_factor; i++) { 129 #if (PlSnP_baseParallelism == 1) 130 SnP_OverwriteBytes(stateWithIndex(i), data, 0, laneCount*SnP_laneLengthInBytes); 131 #else 132 SnP_OverwriteLanesAll(stateWithIndex(i), data, laneCount, laneOffset); 133 #endif 134 data += PlSnP_baseParallelism*laneOffset*SnP_laneLengthInBytes; 135 } 136} 137 138void PlSnP_OverwriteWithZeroes(void *states, unsigned int instanceIndex, unsigned int byteCount) 139{ 140 #if (PlSnP_baseParallelism == 1) 141 SnP_OverwriteWithZeroes(stateWithIndex(instanceIndex), byteCount); 142 #else 143 SnP_OverwriteWithZeroes(stateWithIndex(instanceIndex/PlSnP_baseParallelism), instanceIndex%PlSnP_baseParallelism, byteCount); 144 #endif 145} 146 147void PlSnP_PermuteAll(void *states) 148{ 149 unsigned int i; 150 151 for(i=0; i<PlSnP_factor; i++) { 152 #if (PlSnP_baseParallelism == 1) 153 SnP_Permute(stateWithIndex(i)); 154 #else 155 SnP_PermuteAll(stateWithIndex(i)); 156 #endif 157 } 158} 159 160#if (defined(SnP_Permute_12rounds) || defined(SnP_PermuteAll_12rounds)) 161void PlSnP_PermuteAll_12rounds(void *states) 162{ 163 unsigned int i; 164 165 for(i=0; i<PlSnP_factor; i++) { 166 #if (PlSnP_baseParallelism == 1) 167 SnP_Permute_12rounds(stateWithIndex(i)); 168 #else 169 SnP_PermuteAll_12rounds(stateWithIndex(i)); 170 #endif 171 } 172} 173#endif 174 175void PlSnP_ExtractBytes(void *states, unsigned int instanceIndex, unsigned char *data, unsigned int offset, unsigned int length) 176{ 177 #if (PlSnP_baseParallelism == 1) 178 SnP_ExtractBytes(stateWithIndex(instanceIndex), data, offset, length); 179 #else 180 SnP_ExtractBytes(stateWithIndex(instanceIndex/PlSnP_baseParallelism), instanceIndex%PlSnP_baseParallelism, data, offset, length); 181 #endif 182} 183 184void PlSnP_ExtractLanesAll(const void *states, unsigned char *data, unsigned int laneCount, unsigned int laneOffset) 185{ 186 unsigned int i; 187 188 for(i=0; i<PlSnP_factor; i++) { 189 #if (PlSnP_baseParallelism == 1) 190 SnP_ExtractBytes(stateWithIndex(i), data, 0, laneCount*SnP_laneLengthInBytes); 191 #else 192 SnP_ExtractLanesAll(stateWithIndex(i), data, laneCount, laneOffset); 193 #endif 194 data += laneOffset*SnP_laneLengthInBytes*PlSnP_baseParallelism; 195 } 196} 197 198void PlSnP_ExtractAndAddBytes(void *states, unsigned int instanceIndex, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length) 199{ 200 #if (PlSnP_baseParallelism == 1) 201 SnP_ExtractAndAddBytes(stateWithIndex(instanceIndex), input, output, offset, length); 202 #else 203 SnP_ExtractAndAddBytes(stateWithIndex(instanceIndex/PlSnP_baseParallelism), instanceIndex%PlSnP_baseParallelism, input, output, offset, length); 204 #endif 205} 206 207void PlSnP_ExtractAndAddLanesAll(const void *states, const unsigned char *input, unsigned char *output, unsigned int laneCount, unsigned int laneOffset) 208{ 209 unsigned int i; 210 211 for(i=0; i<PlSnP_factor; i++) { 212 #if (PlSnP_baseParallelism == 1) 213 SnP_ExtractAndAddBytes(stateWithIndex(i), input, output, 0, laneCount*SnP_laneLengthInBytes); 214 #else 215 SnP_ExtractAndAddLanesAll(stateWithIndex(i), input, output, laneCount, laneOffset); 216 #endif 217 input += laneOffset*SnP_laneLengthInBytes*PlSnP_baseParallelism; 218 output += laneOffset*SnP_laneLengthInBytes*PlSnP_baseParallelism; 219 } 220} 221 222#undef PlSnP_factor 223#undef SnP_stateOffset 224#undef stateWithIndex 225#undef JOIN0 226#undef JOIN 227#undef PlSnP_StaticInitialize 228#undef PlSnP_InitializeAll 229#undef PlSnP_AddByte 230#undef PlSnP_AddBytes 231#undef PlSnP_AddLanesAll 232#undef PlSnP_OverwriteBytes 233#undef PlSnP_OverwriteLanesAll 234#undef PlSnP_OverwriteWithZeroes 235#undef PlSnP_PermuteAll 236#undef PlSnP_ExtractBytes 237#undef PlSnP_ExtractLanesAll 238#undef PlSnP_ExtractAndAddBytes 239#undef PlSnP_ExtractAndAddLanesAll 240#undef SnP_stateAlignment 241#undef SnP_stateSizeInBytes 242#undef PlSnP_factor 243#undef SnP_stateOffset 244#undef stateWithIndex 245#undef SnP_StaticInitialize 246#undef SnP_Initialize 247#undef SnP_InitializeAll 248#undef SnP_AddByte 249#undef SnP_AddBytes 250#undef SnP_AddLanesAll 251#undef SnP_OverwriteBytes 252#undef SnP_OverwriteWithZeroes 253#undef SnP_OverwriteLanesAll 254#undef SnP_ExtractBytes 255#undef SnP_ExtractLanesAll 256#undef SnP_ExtractAndAddBytes 257#undef SnP_ExtractAndAddLanesAll 258