-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibmain.py
More file actions
499 lines (415 loc) · 11.3 KB
/
Copy pathlibmain.py
File metadata and controls
499 lines (415 loc) · 11.3 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import os
import timeit
import shutil
from functools import reduce
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30, 38)
octetSize = 8 # SBOX takes 8 bits as input
wordSize = 32
blockSize = 128 #16 for mini-aes and 128 for aes
directory = 'AES_files/'
fileNameEnc = 'f_enc_'
fileNameDec = 'f_dec_'
reduceEquation = True
def hex2bin(h):
"""Returns the string of octetSize bits representation of hexadecimal h.
usage: hex2bin('ff') --> 11111111"""
tmp = ''
h = bin(int(h, 16)).lstrip('0b')
for cpt in range(octetSize-len(h)):
tmp += '0'
for b in h:
tmp += b
return tmp
def largeHex2Bin(h):
"""Convert a hex represntation in a binary representation
usage: largeHex2Bin('00112233') = 00000000000100010010001000110011"""
result = ''
for i in range(0,len(h),2):
result += hex2bin(h[i] + h[i+1])
return result
def wordBin2hex(b):
"""Returns the string of the hexadecimal representation of binary b.
usage: wordBin2hex('00001001110011110100111100111100') --> 09cf4f3c"""
tmp = []
val = ''
result = ''
for i in range(wordSize):
if (i % octetSize == 0) and (i != 0):
tmp.append(val)
val = ''
val += b[i]
tmp.append(val)
for byte in tmp:
i = int(byte, 2)
if (i == 0):
buff = '00'
elif ( i < 16):
buff = '0' + hex(i).lstrip('0x')
else:
buff = hex(i).lstrip('0x').rstrip('L')
result += buff
return result
def bin2byte(b):
"""Returns the string of the byte (8 bits) representation of binary b.
usage: bin2byte('10') --> 00000010"""
tmp = ''
if len(b) > 8:
for cpt in range(len(b)-8,len(b)):
tmp += b[cpt]
else:
for i in range(8-len(b)):
tmp += '0'
tmp = tmp + b
return tmp
def int2bin(i):
"""Returns the string of the 8 bits representation of integer i.
usage: int2bin(255) --> 11111111"""
tmp = ""
i = bin(i).lstrip('0b')
for cpt in range(octetSize-len(i)):
tmp += '0'
tmp = tmp + i
return tmp
def int2hex(i):
"""Returns the string of hexadecimal representation of integer i.
usage: int2hex(254) --> fe"""
tmp = ""
if (i == 0):
tmp = '00'
elif ( i < 16):
tmp = '0' + hex(i).lstrip('0x')
else:
tmp = hex(i).lstrip('0x').rstrip('L')
return tmp
def bin2hex(b):
"""Returns the string of the hexadecimal representation of binary b.
usage: bin2hex('11111010') --> fa"""
tmp = []
val = ''
result = ''
for i in range(blockSize):
if (i % octetSize == 0) and (i != 0):
tmp.append(val)
val = ''
val += b[i]
tmp.append(val)
for byte in tmp:
i = int(byte, 2)
if (i == 0):
buff = '00'
elif ( i < 16):
buff = '0' + hex(i).lstrip('0x')
else:
buff = hex(i).lstrip('0x').rstrip('L')
result += buff
return result
def bin2monome(b):
"""Return the monomes corresponding to the binary.
usage: bin2monome(00110001) = x_2+x_3+x_7"""
result = ''
for i in range(octetSize):
if b[i] == '1':
tmp = 'x_%s' % (i)
result += tmp
return result
def monome2bin(m):
"""Return the binary corresponding to the monome.
usage: monome2bin(x_2+x_3+x_7) = 001100010000...00000000000"""
temp = m.split('x_')
result = ''
for i in range(128):
if str(i) in temp:
result += '1'
else:
result += '0'
return result
def xorTab(t1, t2):
"""Takes two tabs t1 and t2 of same lengths and returns t1 XOR t2."""
result = ''
for i in range(len(t1)):
result += str(int(t1[i]) ^ int(t2[i]))
return result
def xorList(mylist):
result = mylist[0]
cpt = 0
for i in range(len(mylist)):
if cpt < len(mylist)-1:
result = xorTab(result, mylist[cpt+1])
cpt += 1
return result
def galoisMultiplication(a, b):
"""Multiply two polynoms in Rijndael's galois field.
usage: galoisMultiplication('11110000', '10100111') = 01101011
algorithm: http://www.samiam.org/galois.html"""
product = '00000000'
for i in range(octetSize):
if int(b[7], 2) & 1: # low bit of b ist set
product = bin2byte(bin(int(product, 2) ^ int(a, 2)).lstrip('0b'))
aHighBit = int(a[0], 2) # aHighBit contains the high bit of a
a = bin2byte(bin(int(a, 2) << 1).lstrip('0b')) # a is rotated one bit to the left
if aHighBit & 1: # high bit of a is set
num = hex2bin('1b')
a = bin2byte(bin(int(a, 2) ^ int(num, 2)).lstrip('0b'))
b = bin2byte(bin(int(b, 2) >> 1).lstrip('0b')) # b is rotated one bit to the right
return product
def printColor(string, color=RED):
print('\033[1;%dm%s\033[0m' % (color, string))
def intToThreeChar(i):
"""Transform an integer to a 3 chars length string by adding 0
uasge: intToThreeChar(12) return 012"""
temp = str(i)
result = ''
if len(temp) == 1:
result = '00' + temp
elif len(temp) == 2:
result = '0' + temp
else:
result = temp
return result
def createFile(name):
"""Create file named by "name" for data"""
f = open(directory+name, 'w')
return f
def openFile(name):
"""Open file named by "name" for data"""
f = open(directory+name, 'a')
return f
def readFile(name):
"""Read file called name and return a list in which one element corresponds to a line"""
f = open(directory+name, 'r')
result = f.readlines()
closeFile(f)
return result
def closeFile(f):
"""Close file handled by f"""
if not(f.closed):
f.close()
return 0
def generateEquaMonomes(mt):
result = []
for b in range(len(mt)):
tmp = ''
for i in range(len(mt[b])):
if mt[b][i] == '1':
if i == 0:
tmp += '1+' # constant
else:
tmp += bin2monome(int2bin(i)) + '+'
result.append(tmp.rstrip('+'))
return result
def traiteMonomes(equa, cpt):
result =''
tmp = equa.split('+')
for m in tmp:
if m == '1':
result += '1'
else:
t = m.split('x_')
for item in t:
if item != '':
result += 'x_' + str(int(item) + ((cpt-1)*octetSize))
result += '+'
return result.rstrip('+')
def generateEquaMonomesAES(equations):
result = []
cpt = 0
for i in range(128):
if i%octetSize == 0:
cpt += 1
result.append(traiteMonomes(equations[i%octetSize], cpt))
return result
def moebiusTransform(tab):
"""Takes a tab and return tab[0 : len(tab)/2], tab[0 : len(tab)/2] ^ tab[len(tab)/2 : len(tab)].
usage: moebiusTransform(1010011101010100) --> [1100101110001010]"""
if len(tab) == 1:
return tab
else:
t1 = tab[0 : int(len(tab)/2)]
t2 = tab[int(len(tab)/2) : len(tab)]
t2 = xorTab(t1, t2)
t1 = moebiusTransform(t1)
t2 = moebiusTransform(t2)
t1 += t2
return t1
def generateMoebiusTransform(tt):
"""Creates octetSize strings, each containing the result of Moebius
transform for a boolean function of tab. The result is a tab of octetSize
cases each one containing 2**octetSize bits. Each case describe a bit of the function"""
result = []
for i in range(octetSize):
tmp = ''
for block in tt:
tmp += block[i]
result.append(moebiusTransform(tmp))
return result
def reduceEqua(equa):
print('\033[1;%dm%s\033[0m' % (CYAN, '## Reduce equation '), end='')
result = []
before = 0
after = 0
for i in range(len(equa)):
tab = equa[i].split('+')
before += len(tab)
tabtmp = []
for monomial in tab:
count = tab.count(monomial)
if (count % 2 != 0): # we remove odd items
tabtmp.append(monomial)
tabtmp = list(set(tabtmp)) # we remove even items
after += len(tabtmp)
tmp = ''
for monomial in tabtmp:
tmp += monomial
tmp += '+'
result.append(tmp.rstrip('+'))
printColor('%d -> %d monomials' % (before, after), CYAN)
return result
def generateBinaryMonomes(equations):
result = []
for i in range(len(equations)):
temp = ''
for monome in equations[i].split('+'):
if monome == '1':
temp += '1\t' + monome2bin('') + '\n'
else:
temp += '0\t' + monome2bin(monome) + '\n'
result.append(temp)
return result
def bitToLatex(bit):
bit = bit.replace('+', ' \oplus ')
bit = '$' + bit + '$'
print(bit)
def createAESFiles(val):
d = os.path.dirname(directory)
if not os.path.exists(d):
printColor('## Create directory %s' % (d), YELLOW)
os.mkdir(directory)
else:
printColor('## Directory %s already exist' % (d), RED)
fname = (fileNameEnc if val == 'enc' else fileNameDec)
for i in range(blockSize):
f = createFile(fname+'%s.txt' % (intToThreeChar(i)))
closeFile(f)
return 1
def testAESdirectory():
d = os.path.dirname(directory)
if os.path.exists(d):
printColor('## Deleting directory %s' % (d), RED)
shutil.rmtree(directory)
def existAESdirectory():
d = os.path.dirname(directory)
if os.path.exists(d):
return(True)
else:
return(False)
def writeEndFlag(val):
fname = (fileNameEnc if val == 'enc' else fileNameDec)
for i in range(blockSize):
f = openFile(fname+'%s.txt' % intToThreeChar(i))
f.write('## end\n')
closeFile(f)
def generateGenericWord(s, c):
"""Generate a tab containing wordSize variables
usage: generateGenericByte(0, 'x') return ['x_0', 'x_1', 'x_2', 'x_3', 'x_4', ..., 'x_30', 'x_31']"""
result = []
for i in range(s,wordSize+s):
result.append('%s_%s' % (c, i))
return result
def generateGenericBlock(c):
"""Generate a tab containing blockSize variables
usage: generateGenericBlock('x') return ['x_0', 'x_1', 'x_2', 'x_3', 'x_4', ..., 'x_126', 'x_127']"""
result = []
for i in range(blockSize):
result.append('%s_%s' % (c, i))
return result
def generateBitsBlock(c):
"""Generate a string containing blockSize bits
usage: generateGenericBlock('x_3') return 00010000000...00000"""
result = ''
tmp = int(c.split('_')[1])
for i in range(blockSize):
if i == tmp:
result += '1'
else:
result += '0'
return result
def generateAllBits():
"""Generate a list containing 128 input corresponding to the
conversion of integer to binary for i in range(blockSize)"""
result = []
tmp = []
for i in range(blockSize):
tmp.append('0')
for i in range(blockSize):
t = ''
tmp[i] = '1'
result.append('0\t' + t.join(tmp) + '\n')
tmp[i] = '0'
return result
def extractBlock(file, startBlock, endBlock):
tmp = []
flag = 0
allLines = readFile(file)
for line in allLines:
line = line.rstrip('\n')
if line == startBlock: flag = 1
if line == endBlock: flag = 0
if flag:
if line[0] != '#':
tmp.append(line)
return tmp
def displayTruthTable(tt):
for i in range(len(tt)):
print(i, '\t', int(tt[i], 2), '\t', int2bin(i), '\t', tt[i])
def displayEqua(tt):
mt = generateMoebiusTransform(tt)
equa = generateEquaMonomes(mt)
for i in range(octetSize):
equa2sagemath(equa[i])
print(i, '\t', equa2sagemath(equa[i]))
def treatBlock(value, block):
"""Each monomial on the line is multiplied and each line is XORed"""
result = []
for polynom in value:
t = []
tmp = polynom.split('\t')
if tmp[0] == '1':
result.append(int(tmp[0]))
else:
for i in range(blockSize):
if tmp[1][i] == '1':
t.append(int(block[i]))
result.append(reduce(lambda x, y: x&y, t))
return str(reduce(lambda x, y: x^y, result))
def controlBlock(mode, start, end, block, key=None):
flag = 0
result = ''
for i in range(blockSize):
fname = (fileNameEnc if mode == 'enc' else fileNameDec)
file = fname+'%s.txt' % intToThreeChar(i)
temp = extractBlock(file, start, end)
if (key == None):
result += treatBlock(temp, block)
else:
result += treatBlock(temp, key)
if (key == None):
block = result
else:
block = xorTab(result, block)
printColor(start)
print(block, len(block))
print(bin2hex(block), len(bin2hex(block)))
return block
def equaToLatex(equa, letter):
result = '$'
for monomial in equa.split('+'):
if monomial == '1':
result += monomial + '+'
else:
for val in monomial.split('x'):
if val != '':
result += '%s_{%s}' % (letter, val.lstrip('_'))
result += '+'
result = result.rstrip('+') + '$'
result = result.replace('+', ' \oplus ')
return result