forked from LowMC/lowmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LowMC.cpp
executable file
·388 lines (339 loc) · 11 KB
/
LowMC.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include "LowMC.h"
/////////////////////////////
// LowMC functions //
/////////////////////////////
block LowMC::encrypt (const block message) {
block c = message ^ roundkeys[0];
for (unsigned r = 1; r <= rounds; ++r) {
c = Substitution(c);
c = MultiplyWithGF2Matrix(LinMatrices[r-1], c);
c ^= roundconstants[r-1];
c ^= roundkeys[r];
}
return c;
}
block LowMC::decrypt (const block message) {
block c = message;
for (unsigned r = rounds; r > 0; --r) {
c ^= roundkeys[r];
c ^= roundconstants[r-1];
c = MultiplyWithGF2Matrix(invLinMatrices[r-1], c);
c = invSubstitution(c);
}
c ^= roundkeys[0];
return c;
}
void LowMC::set_key (keyblock k) {
key = k;
keyschedule();
}
void LowMC::print_matrices() {
std::cout << "LowMC matrices and constants" << std::endl;
std::cout << "============================" << std::endl;
std::cout << "Block size: " << blocksize << std::endl;
std::cout << "Key size: " << keysize << std::endl;
std::cout << "Rounds: " << rounds << std::endl;
std::cout << std::endl;
std::cout << "Linear layer matrices" << std::endl;
std::cout << "---------------------" << std::endl;
for (unsigned r = 1; r <= rounds; ++r) {
std::cout << "Linear layer " << r << ":" << std::endl;
for (auto row: LinMatrices[r-1]) {
std::cout << "[";
for (unsigned i = 0; i < blocksize; ++i) {
std::cout << row[i];
if (i != blocksize - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
std::cout << std::endl;
}
std::cout << "Round constants" << std::endl;
std::cout << "---------------------" << std::endl;
for (unsigned r = 1; r <= rounds; ++r) {
std::cout << "Round constant " << r << ":" << std::endl;
std::cout << "[";
for (unsigned i = 0; i < blocksize; ++i) {
std::cout << roundconstants[r-1][i];
if (i != blocksize - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
std::cout << std::endl;
}
std::cout << "Round key matrices" << std::endl;
std::cout << "---------------------" << std::endl;
for (unsigned r = 0; r <= rounds; ++r) {
std::cout << "Round key matrix " << r << ":" << std::endl;
for (auto row: KeyMatrices[r]) {
std::cout << "[";
for (unsigned i = 0; i < keysize; ++i) {
std::cout << row[i];
if (i != keysize - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
if (r != rounds) {
std::cout << std::endl;
}
}
}
/////////////////////////////
// LowMC private functions //
/////////////////////////////
block LowMC::Substitution (const block message) {
block temp = 0;
//Get the identity part of the message
temp ^= (message >> 3*numofboxes);
//Get the rest through the Sboxes
for (unsigned i = 1; i <= numofboxes; ++i) {
temp <<= 3;
temp ^= Sbox[ ((message >> 3*(numofboxes-i))
& block(0x7)).to_ulong()];
}
return temp;
}
block LowMC::invSubstitution (const block message) {
block temp = 0;
//Get the identity part of the message
temp ^= (message >> 3*numofboxes);
//Get the rest through the invSboxes
for (unsigned i = 1; i <= numofboxes; ++i) {
temp <<= 3;
temp ^= invSbox[ ((message >> 3*(numofboxes-i))
& block(0x7)).to_ulong()];
}
return temp;
}
block LowMC::MultiplyWithGF2Matrix
(const std::vector<block> matrix, const block message) {
block temp = 0;
for (unsigned i = 0; i < blocksize; ++i) {
temp[i] = (message & matrix[i]).count() % 2;
}
return temp;
}
block LowMC::MultiplyWithGF2Matrix_Key
(const std::vector<keyblock> matrix, const keyblock k) {
block temp = 0;
for (unsigned i = 0; i < blocksize; ++i) {
temp[i] = (k & matrix[i]).count() % 2;
}
return temp;
}
void LowMC::keyschedule () {
roundkeys.clear();
for (unsigned r = 0; r <= rounds; ++r) {
roundkeys.push_back( MultiplyWithGF2Matrix_Key (KeyMatrices[r], key) );
}
return;
}
void LowMC::instantiate_LowMC () {
// Create LinMatrices and invLinMatrices
LinMatrices.clear();
invLinMatrices.clear();
for (unsigned r = 0; r < rounds; ++r) {
// Create matrix
std::vector<block> mat;
// Fill matrix with random bits
do {
mat.clear();
for (unsigned i = 0; i < blocksize; ++i) {
mat.push_back( getrandblock () );
}
// Repeat if matrix is not invertible
} while ( rank_of_Matrix(mat) != blocksize );
LinMatrices.push_back(mat);
invLinMatrices.push_back(invert_Matrix (LinMatrices.back()));
}
// Create roundconstants
roundconstants.clear();
for (unsigned r = 0; r < rounds; ++r) {
roundconstants.push_back( getrandblock () );
}
// Create KeyMatrices
KeyMatrices.clear();
for (unsigned r = 0; r <= rounds; ++r) {
// Create matrix
std::vector<keyblock> mat;
// Fill matrix with random bits
do {
mat.clear();
for (unsigned i = 0; i < blocksize; ++i) {
mat.push_back( getrandkeyblock () );
}
// Repeat if matrix is not of maximal rank
} while ( rank_of_Matrix_Key(mat) < std::min(blocksize, keysize) );
KeyMatrices.push_back(mat);
}
return;
}
/////////////////////////////
// Binary matrix functions //
/////////////////////////////
unsigned LowMC::rank_of_Matrix (const std::vector<block> matrix) {
std::vector<block> mat; //Copy of the matrix
for (auto u : matrix) {
mat.push_back(u);
}
unsigned size = mat[0].size();
//Transform to upper triangular matrix
unsigned row = 0;
for (unsigned col = 1; col <= size; ++col) {
if ( !mat[row][size-col] ) {
unsigned r = row;
while (r < mat.size() && !mat[r][size-col]) {
++r;
}
if (r >= mat.size()) {
continue;
} else {
auto temp = mat[row];
mat[row] = mat[r];
mat[r] = temp;
}
}
for (unsigned i = row+1; i < mat.size(); ++i) {
if ( mat[i][size-col] ) mat[i] ^= mat[row];
}
++row;
if (row == size) break;
}
return row;
}
unsigned LowMC::rank_of_Matrix_Key (const std::vector<keyblock> matrix) {
std::vector<keyblock> mat; //Copy of the matrix
for (auto u : matrix) {
mat.push_back(u);
}
unsigned size = mat[0].size();
//Transform to upper triangular matrix
unsigned row = 0;
for (unsigned col = 1; col <= size; ++col) {
if ( !mat[row][size-col] ) {
unsigned r = row;
while (r < mat.size() && !mat[r][size-col]) {
++r;
}
if (r >= mat.size()) {
continue;
} else {
auto temp = mat[row];
mat[row] = mat[r];
mat[r] = temp;
}
}
for (unsigned i = row+1; i < mat.size(); ++i) {
if ( mat[i][size-col] ) mat[i] ^= mat[row];
}
++row;
if (row == size) break;
}
return row;
}
std::vector<block> LowMC::invert_Matrix (const std::vector<block> matrix) {
std::vector<block> mat; //Copy of the matrix
for (auto u : matrix) {
mat.push_back(u);
}
std::vector<block> invmat(blocksize, 0); //To hold the inverted matrix
for (unsigned i = 0; i < blocksize; ++i) {
invmat[i][i] = 1;
}
unsigned size = mat[0].size();
//Transform to upper triangular matrix
unsigned row = 0;
for (unsigned col = 0; col < size; ++col) {
if ( !mat[row][col] ) {
unsigned r = row+1;
while (r < mat.size() && !mat[r][col]) {
++r;
}
if (r >= mat.size()) {
continue;
} else {
auto temp = mat[row];
mat[row] = mat[r];
mat[r] = temp;
temp = invmat[row];
invmat[row] = invmat[r];
invmat[r] = temp;
}
}
for (unsigned i = row+1; i < mat.size(); ++i) {
if ( mat[i][col] ) {
mat[i] ^= mat[row];
invmat[i] ^= invmat[row];
}
}
++row;
}
//Transform to identity matrix
for (unsigned col = size; col > 0; --col) {
for (unsigned r = 0; r < col-1; ++r) {
if (mat[r][col-1]) {
mat[r] ^= mat[col-1];
invmat[r] ^= invmat[col-1];
}
}
}
return invmat;
}
///////////////////////
// Pseudorandom bits //
///////////////////////
block LowMC::getrandblock () {
block tmp = 0;
for (unsigned i = 0; i < blocksize; ++i) tmp[i] = getrandbit ();
return tmp;
}
keyblock LowMC::getrandkeyblock () {
keyblock tmp = 0;
for (unsigned i = 0; i < keysize; ++i) tmp[i] = getrandbit ();
return tmp;
}
// Uses the Grain LSFR as self-shrinking generator to create pseudorandom bits
// Is initialized with the all 1s state
// The first 160 bits are thrown away
bool LowMC::getrandbit () {
static std::bitset<80> state; //Keeps the 80 bit LSFR state
bool tmp = 0;
//If state has not been initialized yet
if (state.none ()) {
state.set (); //Initialize with all bits set
//Throw the first 160 bits away
for (unsigned i = 0; i < 160; ++i) {
//Update the state
tmp = state[0] ^ state[13] ^ state[23]
^ state[38] ^ state[51] ^ state[62];
state >>= 1;
state[79] = tmp;
}
}
//choice records whether the first bit is 1 or 0.
//The second bit is produced if the first bit is 1.
bool choice = false;
do {
//Update the state
tmp = state[0] ^ state[13] ^ state[23]
^ state[38] ^ state[51] ^ state[62];
state >>= 1;
state[79] = tmp;
choice = tmp;
tmp = state[0] ^ state[13] ^ state[23]
^ state[38] ^ state[51] ^ state[62];
state >>= 1;
state[79] = tmp;
} while (! choice);
return tmp;
}