forked from microsoft/Picnic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowMCEnc.c
300 lines (257 loc) · 9.79 KB
/
LowMCEnc.c
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
/*! @file LowMCEnc.c
* @brief C implementation of LowMC encrypt. This is used to generate keys
* for the Picnic signature scheme.
*
* This file is part of the reference implementation of the Picnic and Fish
* signature schemes, described in the paper:
*
* Post-Quantum Zero-Knowledge and Signatures from Symmetric-Key Primitives
* Melissa Chase and David Derler and Steven Goldfeder and Claudio Orlandi and
* Sebastian Ramacher and Christian Rechberger and Daniel Slamanig and Greg
* Zaverucha
* Cryptology ePrint Archive: Report 2017/279
* http://eprint.iacr.org/2017/279
*
* The code is provided under the MIT license, see LICENSE for
* more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <immintrin.h>
#include "LowMCConstants.h"
#ifdef __WINDOWS__
#define DATA_PATH "data\\"
#else
#define DATA_PATH "data/"
#endif
#define t 8
uint32_t(*linearMatricesLookupTable)[STATE_SIZE_BITS / t][1 << t][STATE_SIZE_WORDS];
uint32_t(*keyMatricesLookupTable)[STATE_SIZE_BITS / t][1 << t][STATE_SIZE_WORDS];
uint32_t(*roundConstants)[STATE_SIZE_WORDS];
__m256i(*linearMatricesLookupTableVec)[STATE_SIZE_BITS / t][1 << t][1];
__m256i(*keyMatricesLookupTableVec)[STATE_SIZE_BITS / t][1 << t][1];
__m256i(*roundConstantsVec)[1];
/* Helper functions */
/* Get one bit from a byte array */
static uint8_t getBit(const uint32_t* array, uint32_t bitNumber)
{
return (array[bitNumber / WORD_SIZE_BITS]
>> (WORD_SIZE_BITS - 1 - (bitNumber % WORD_SIZE_BITS))) & 0x01;
}
/* Set a specific bit in a byte array to a given value */
static void setBit(uint32_t* bytes, uint32_t bitNumber, uint8_t val)
{
bytes[bitNumber / WORD_SIZE_BITS] = (bytes[bitNumber / WORD_SIZE_BITS]
& ~(1 << (WORD_SIZE_BITS - 1 - (bitNumber % WORD_SIZE_BITS))))
| (val << (WORD_SIZE_BITS - 1 - (bitNumber % WORD_SIZE_BITS)));
}
static void xor_bytes(uint32_t * in1, uint32_t * in2, uint32_t * out, uint32_t numBytes) {
for (uint32_t i = 0; i < numBytes; i++) {
out[i] = in1[i] ^ in2[i];
}
}
static void multiplyWithMatrix(uint32_t* state,
uint32_t matrix[STATE_SIZE_BITS / t][1 << t][STATE_SIZE_WORDS],
uint32_t* output, lowmcparams_t* params)
{
uint32_t temp[STATE_SIZE_WORDS] = { 0 };
for (uint32_t i = 0; i < params->stateSizeWords; i++) {
xor_bytes(temp, matrix[i * (sizeof(uint32_t))][(state[i] >> 24) & 0xFF], temp,
params->stateSizeWords);
xor_bytes(temp, matrix[i * (sizeof(uint32_t)) + 1][(state[i] >> 16) & 0xFF],
temp, params->stateSizeWords);
xor_bytes(temp, matrix[i * (sizeof(uint32_t)) + 2][(state[i] >> 8) & 0xFF],
temp, params->stateSizeWords);
xor_bytes(temp, matrix[i * (sizeof(uint32_t)) + 3][(state[i]) & 0xFF], temp,
params->stateSizeWords);
}
memcpy(output, &temp, params->stateSizeBytes);
}
// starts from the left, and interprets the first bit t encounters as the highest order bit (compared to the lookup table
// in lowmc reference code. may need to change this
static void substitution(uint32_t* state, lowmcparams_t* params)
{
for (uint32_t i = 0; i < params->numSboxes * 3; i += 3) {
uint8_t a = getBit(state, params->stateSizeBits - 1 - i - 2);
uint8_t b = getBit(state, params->stateSizeBits - 1 - i - 1);
uint8_t c = getBit(state, params->stateSizeBits - 1 - i);
setBit(state, params->stateSizeBits - 1 - i - 2, a ^ (b & c));
setBit(state, params->stateSizeBits - 1 - i - 1, a ^ b ^ (a & c));
setBit(state, params->stateSizeBits - 1 - i, a ^ b ^ c ^ (a & b));
}
}
static void computeRoundKey(uint32_t* key, uint32_t* output, uint32_t round, lowmcparams_t* params)
{
multiplyWithMatrix(key, keyMatricesLookupTable[round], output, params);
}
int readRoundConstants(lowmcparams_t* params, FILE* fp)
{
size_t ret;
uint32_t numRounds = params->numRounds;
uint32_t stateWords = params->stateSizeWords;
roundConstants = malloc(
numRounds * sizeof(*roundConstants));
if (roundConstants == NULL) {
fprintf(stderr, "%s: Failed to allocate roundConstants.\n", __FUNCTION__);
fclose(fp);
return -1;
}
#ifdef WITH_AVX
roundConstantsVec = _mm_malloc(
numRounds * sizeof(*roundConstantsVec), 32);
if (roundConstantsVec == NULL) {
fprintf(stderr, "%s: Failed to allocate roundConstantsVec.\n", __FUNCTION__);
fclose(fp);
return -1;
}
#endif
for (uint32_t i = 0; i < numRounds; i++) {
ret = fread(&roundConstants[i],
sizeof(uint32_t), stateWords, fp);
#ifdef WITH_AVX
*roundConstantsVec[i] = _mm256_loadu_si256(
(__m256i*)roundConstants[i]);
#endif
if (ret != stateWords) {
fprintf(stderr, "%s: Failed to read round constants, aborting.\n", __FUNCTION__);
fclose(fp);
free(roundConstants);
#ifdef WITH_AVX
_mm_free(roundConstantsVec);
#endif
return -1;
}
}
fclose(fp);
return 0;
}
int readLookupTables(lowmcparams_t* params)
{
FILE * fp;
size_t ret;
char filename[100];
sprintf(filename, DATA_PATH "lookupTables_%d_%d_%d.bin", params->stateSizeBits, params->numSboxes, params->numRounds);
fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "%s: Failed to open '%s'.\n", __FUNCTION__, filename);
return -1;
}
uint32_t numRounds = params->numRounds;
uint32_t stateBits = params->stateSizeBits;
uint32_t stateWords = params->stateSizeWords;
linearMatricesLookupTable = malloc(
numRounds * sizeof(*linearMatricesLookupTable));
if (linearMatricesLookupTable == NULL) {
fprintf(stderr, "%s: Failed to allocate linearMatricesLookupTable.\n", __FUNCTION__);
fclose(fp);
return -1;
}
#ifdef WITH_AVX
linearMatricesLookupTableVec = _mm_malloc(
numRounds * sizeof(*linearMatricesLookupTableVec), 32);
if (linearMatricesLookupTableVec == NULL) {
fprintf(stderr, "%s: Failed to allocate keyMatricesLookupTableVec.\n", __FUNCTION__);
fclose(fp);
return -1;
}
#endif
for (uint32_t i = 0; i < numRounds; i++) {
for (uint32_t j = 0; j < stateBits / t; j++) {
for (uint32_t k = 0; k < (1 << t); k++) {
ret = fread(linearMatricesLookupTable[i][j][k], sizeof(uint32_t), stateWords, fp);
#ifdef WITH_AVX
*linearMatricesLookupTableVec[i][j][k] = _mm256_loadu_si256(
(__m256i*)linearMatricesLookupTable[i][j][k]);
#endif
if (ret != stateWords) {
fprintf(stderr, "%s: Failed to read look up table, aborting.\n", __FUNCTION__);
fclose(fp);
free(linearMatricesLookupTable);
#ifdef WITH_AVX
_mm_free(linearMatricesLookupTableVec);
#endif
return -1;
}
}
}
}
numRounds++;
keyMatricesLookupTable = malloc(numRounds * sizeof(*keyMatricesLookupTable));
if (keyMatricesLookupTable == NULL) {
fprintf(stderr, "%s: Failed to allocate keyMatricesLookupTable.\n", __FUNCTION__);
fclose(fp);
free(linearMatricesLookupTable);
return -1;
}
#ifdef WITH_AVX
keyMatricesLookupTableVec = _mm_malloc(numRounds * sizeof(*keyMatricesLookupTableVec), 32);
if (keyMatricesLookupTableVec == NULL) {
fprintf(stderr, "%s: Failed to allocate keyMatricesLookupTableVec.\n", __FUNCTION__);
fclose(fp);
_mm_free(linearMatricesLookupTableVec);
return -1;
}
#endif
for (uint32_t i = 0; i < numRounds; i++) {
for (uint32_t j = 0; j < stateBits / t; j++) {
for (uint32_t k = 0; k < (1 << t); k++) {
ret = fread(keyMatricesLookupTable[i][j][k], sizeof(uint32_t),
stateWords, fp);
#ifdef WITH_AVX
*keyMatricesLookupTableVec[i][j][k] = _mm256_loadu_si256(
(__m256i*)keyMatricesLookupTable[i][j][k]);
#endif
if (ret != stateWords) {
fprintf(stderr, "%s: Failed to read look up table, aborting. \n", __FUNCTION__);
fclose(fp);
free(linearMatricesLookupTable);
free(keyMatricesLookupTable);
#ifdef WITH_AVX
_mm_free(linearMatricesLookupTableVec);
_mm_free(keyMatricesLookupTable);
#endif
return -1;
}
}
}
}
return readRoundConstants(params, fp);
}
void freeRoundConstants()
{
free(roundConstants);
#ifdef WITH_AVX
_mm_free(roundConstantsVec);
#endif
}
void freeLookupTables()
{
free(linearMatricesLookupTable);
free(keyMatricesLookupTable);
#ifdef WITH_AVX
_mm_free(linearMatricesLookupTableVec);
_mm_free(keyMatricesLookupTableVec);
#endif
freeRoundConstants();
}
void LowMCEnc(const uint32_t* plaintext, uint32_t* output, uint32_t* key, lowmcparams_t* params)
{
uint32_t roundKey[LOWMC_MAX_STATE_SIZE / sizeof(uint32_t)];
if (plaintext != output) {
/* output will hold the intermediate state */
memcpy(output, plaintext, params->stateSizeBytes);
}
computeRoundKey(key, roundKey, 0, params);
xor_bytes(output, roundKey, output, params->stateSizeWords);
for (uint32_t r = 1; r <= params->numRounds; r++) {
computeRoundKey(key, roundKey, r, params);
substitution(output, params);
multiplyWithMatrix(output, linearMatricesLookupTable[r - 1], output, params);
xor_bytes(output, roundConstants[r - 1], output, params->stateSizeWords);
xor_bytes(output, roundKey, output, params->stateSizeWords);
}
}