-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiaes_equa.py
More file actions
executable file
·275 lines (220 loc) · 6.91 KB
/
Copy pathminiaes_equa.py
File metadata and controls
executable file
·275 lines (220 loc) · 6.91 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
from libminiaes import *
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(30, 38)
directory = 'mini_AES_files/'
def printColor(string, color=RED):
print('\033[1;%dm%s\033[0m' % (color, string))
def blockToStr(block):
result = ''
for i in range(blockSize):
if ((i % nibbleSize) == 0) & (i != 0): result += ' '
result += block[i]
return result
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"""
global directory
f = open(directory+name, 'w')
return f
def openFile(name):
"""Open file named by "name" for data"""
global directory
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"""
global directory
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 createMiniAESFiles():
global directory
d = os.path.dirname(directory)
if not os.path.exists(d):
printColor('## Create directory %s' % (d), GREEN)
os.mkdir(directory)
else:
printColor('## Directory %s already exist' % (d), RED)
for i in range(blockSize):
f = createFile('f_%s.txt' % (intToThreeChar(i)))
closeFile(f)
printColor('## Files generated', GREEN)
return 1
def generateBitsBlock(c):
"""Generate a string containing blockSize bits
usage: generateGenericBlock('x3') return 000100...00000"""
result = ''
tmp = int(c.split('_')[1])
for i in range(blockSize):
if i == tmp:
result += '1'
else:
result += '0'
return result
def equaToLines(equa):
result = ''
tmp = equa.split('+')
for monomial in tmp:
if monomial == '1':
result += '1\t'
for i in range(1,blockSize+1):
result += '0'
result += '\n'
else:
result += '0\t'
tmp = monomial.split('x')
for i in range(1,blockSize+1):
if str(i) in tmp:
result += '1'
else:
result += '0'
result += '\n'
return(result)
def generateFiles():
createMiniAESFiles()
printColor('## Generating the truth table of function expansion key', GREEN)
(k0, k1, k2) = generateRoundsKeysTruthTable()
printColor('## Generating the truth table of function round one', GREEN)
r1 = generateRoundOneTruthTable()
printColor('## Generating the truth table of function round two', GREEN)
r2 = generateRoundTwoTruthTable()
printColor('## Calculating Moebius transform for K0', GREEN)
mtk0 = generateMoebiusTransform(k0)
printColor('## Calculating Moebius transform for K1', GREEN)
mtk1 = generateMoebiusTransform(k1)
printColor('## Calculating Moebius transform for K2', GREEN)
mtk2 = generateMoebiusTransform(k2)
printColor('## Calculating Moebius transform for R1', GREEN)
mtr1 = generateMoebiusTransform(r1)
printColor('## Calculating Moebius transform for R2', GREEN)
mtr2 = generateMoebiusTransform(r2)
for i in range(blockSize):
printColor('## Generating file for bit %s' % (i+1), MAGENTA)
f = openFile('f_%s.txt' % (intToThreeChar(i)))
equa = definesMonomeBlock(mtk0[i])
f.write('## addRoundKey1\n')
f.write('%s' % (equaToLines(equa)))
equa = definesMonomeBlock(mtr1[i])
f.write('## RoundOne\n')
f.write('%s' % (equaToLines(equa)))
equa = definesMonomeBlock(mtk1[i])
f.write('## addRoundKey2\n')
f.write('%s' % (equaToLines(equa)))
equa = definesMonomeBlock(mtr2[i])
f.write('## RoundTwo\n')
f.write('%s' % (equaToLines(equa)))
equa = definesMonomeBlock(mtk2[i])
f.write('## addRoundKey3\n')
f.write('%s' % (equaToLines(equa)))
f.write('## end\n')
closeFile(f)
def treatBlock(roundBlock, block):
"""Each monomial on the line is multiplied and each line is XORed"""
result = []
for polynom in roundBlock:
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 treatKey(beginMark, endMark, key, clear):
result = ''
for i in range(blockSize):
temp = []
flag = 0
allLines = readFile('f_%s.txt' % (intToThreeChar(i)))
for line in allLines:
line = line.rstrip('\n')
if line == beginMark: flag = 1
if line == endMark: flag = 0
if flag:
if line[0] != '#':
temp.append(line)
result += treatBlock(temp, key)
return(xorTab(clear, result))
def treatRound(beginMark, endMark, block):
result = ''
for i in range(blockSize):
temp = []
flag = 0
allLines = readFile('f_%s.txt' % (intToThreeChar(i)))
for line in allLines:
line = line.rstrip('\n')
if line == beginMark: flag = 1
if line == endMark: flag = 0
if flag:
if line[0] != '#':
temp.append(line)
result += treatBlock(temp, block)
return(result)
def controlCipheringProcess():
clear = '1001110001100011'
key = '1100001111110000'
printColor('## Clear block %s' % blockToStr(clear), BLUE)
printColor('## Key block %s' % blockToStr(key), BLUE)
block = treatKey('## addRoundKey1', '## RoundOne', key, clear)
printColor('## addRoundKey')
print(blockToStr(block))
block = treatRound('## RoundOne', '## addRoundKey2', block)
printColor('## RoundOne')
print(blockToStr(block))
block = treatKey('## addRoundKey2', '## RoundTwo', key, block)
printColor('## addRoundKey')
print(blockToStr(block))
block = treatRound('## RoundTwo', '## addRoundKey3', block)
printColor('## RoundTwo')
print(blockToStr(block))
block = treatKey('## addRoundKey3', '## end', key, block)
printColor('## addRoundKey')
print(blockToStr(block))
printColor('## Cipher block %s' % blockToStr(block), BLUE)
def printLatexEqua():
(ttk0, ttk1, ttk2) = generateRoundsKeysTruthTable()
#ttr1 = generateRoundOneTruthTable()
ttr2 = generateRoundTwoTruthTable()
#mtk0 = generateMoebiusTransform(ttk0)
#mtr1 = generateMoebiusTransform(ttr1)
#mtk1 = generateMoebiusTransform(ttk1)
mtr2 = generateMoebiusTransform(ttr2)
mtk2 = generateMoebiusTransform(ttk2)
for i in range(blockSize):
#eqk0 = definesMonomeBlock(mtk0[i])
#eqr1 = definesMonomeBlock(mtr1[i])
#eqk1 = definesMonomeBlock(mtk1[i])
eqr2 = definesMonomeBlock(mtr2[i])
eqk2 = definesMonomeBlock(mtk2[i])
#lak0 = equaToLatex(eqk0, 'k', False).strip('$')
#lar1 = equaToLatex(eqr1, 'b', '0').strip('$')
#lak1 = equaToLatex(eqk1, 'k', False).strip('$')
lar2 = equaToLatex(eqr2, 'b', '1').strip('$')
lak2 = equaToLatex(eqk2, 'k', False).strip('$')
#print("$b_{1,%s} = %s \\oplus %s \\oplus %s$" % (i+1, lak0, lar1, lak1), end='\n\n\\medskip\n\n')
print("$b_{2,%s} = %s \\oplus %s$" % (i+1, lar2, lak2), end='\n\n\\medskip\n\n')
if __name__ == "__main__":
printLatexEqua()
# generateFiles()
# controlCipheringProcess()