-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibminiaes.py
More file actions
311 lines (259 loc) · 7.36 KB
/
Copy pathlibminiaes.py
File metadata and controls
311 lines (259 loc) · 7.36 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
nibbleSize = 4 # a nibble is 4 bits length
blockSize = 2**nibbleSize # in mini-aes a block is 4 nibbles length
int2hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E' , 'F']
sbox = [
'1110',
'0100',
'1101',
'0001',
'0010',
'1111',
'1011',
'1000',
'0011',
'1010',
'0110',
'1100',
'0101',
'1001',
'0000',
'0111',
]
gf24mult = [
'0000000000000000',
'0123456789ABCDEF',
'02468ACE3175B9FD',
'0365CFA9B8DE7412',
'048C37BF62EA51D9',
'05AF72D8EB419C36',
'06CABD71539FE824',
'07E9F816DA3425CB',
'083B6E5DC4F7A291',
'09182B3A4D5C6F7E',
'0A7DE493F5821B6C',
'0B5EA1F47C29D683',
'0CB759E2A61DF348',
'0D941C852FB63EA7',
'0EF1D32C97684AB5',
'0FD296481EC3875A'
]
def intToNibble(i):
"""Returns the string of nibbleSize bits representation of integer i.
usage: intToNibble(15) --> 1111"""
tmp = ''
i = bin(i).lstrip('0b')
for cpt in range(nibbleSize-len(i)):
tmp += '0'
for b in i:
tmp += b
return tmp
def hexToNibble(h):
"""Returns the string of nibbleSize bits representation of hexadecimal h.
usage: hexToNibble(F) --> 1111"""
return intToNibble(int(h, 16))
def intToBinOctect(i):
"""Returns the string of the 8 bits representation of integer i.
usage: intToBinOctect(134) --> 10000110"""
tmp = ""
i = bin(i).lstrip('0b')
for cpt in range(8-len(i)):
tmp += '0'
tmp = tmp + i
return tmp
def intToBinBlock(i):
"""Returns the string of the 16 bits representation of integer i.
usage: intToBinBlock(65523) --> 1111111111110011"""
tmp = ""
i = bin(i).lstrip('0b')
for cpt in range(16-len(i)):
tmp += '0'
tmp = tmp + i
return tmp
def nibbleToInt(nibble):
"""Returns a string of the integer representation for a nibbleSize bits.
usage: nibbleToInt(1111) --> 15"""
result = 0
for i in range(len(nibble)):
result += int(nibble[i]) * 2**(len(nibble)-(i+1))
return result
def nibbleToHex(nibble):
"""Returns a string of the hexadecimal representation for a nibbleSize bits.
usage: nibbleToInt(1111) --> F"""
return int2hex[nibbleToInt(nibble)]
def blockToNibbles(block):
""" Take a string of blockSize and return blockSize/nibbleSize strings of nibbleSize.
usage: blockToNibbles([1001110001100011) --> 1001 1100 0110 0011 """
result = ''
tmp = ''
for cpt in range(len(block)):
if not(cpt % nibbleSize) and (cpt != 0):
result += tmp +' '
tmp = ''
tmp += block[cpt]
result += tmp
return result.lstrip(' ')
def mapSbox(nibble):
return sbox[nibbleToInt(nibble)]
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 equaToLatex(equa, letter, num):
result = '$'
for monomial in equa.split('+'):
if monomial == '1':
result += monomial + '+'
else:
for val in monomial.split('x'):
if val != '':
if (num):
result += '%s_{%s,%s}' % (letter, num, val)
else:
result += '%s_{%s}' % (letter, val)
result += '+'
result = result.rstrip('+') + '$'
result = result.replace('+', ' \oplus ')
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(tab):
"""Creates blockSize strings, each containing the result of Moebius Transform for a boolean function of tab.
The result is a tab of 16 cases each one containing 65536 bits. Each case describe a bit of the function"""
result = []
for i in range(blockSize):
tmp = ''
for block in tab:
tmp += block[i]
result.append(moebiusTransform(tmp))
return result
def definesMonomeBlock(mt):
"""Takes a moebius transform table and calulates corresponding monome.
Scale is for block of 16 bits (a mini-aes block)"""
tab = []
equa = ''
for i in range(2**blockSize):
if mt[i] == '1':
tmp = intToBinBlock(i)
if i == 0:
tab.append('1') # constant
else:
monome = ''
for bit in range(blockSize):
if tmp[bit] == '1':
monome += 'x' + str(bit+1)
tab.append(monome)
else:
tab.append('\t')
for i in range(2**blockSize):
if tab[i] != '\t':
equa += tab[i] + '+'
return(equa.rstrip('+'))
def gf24Multiply(n1, n2):
"""Returns the string of nibbleSize bits representation of n1xn2 in GF24 (modulo x4+x+1).
usage: gf2Multiply('1111', '0000') --> '1111' """
i1 = nibbleToInt(n1)
i2 = nibbleToInt(n2)
return hexToNibble(gf24mult[i1][i2])
def generateNibbleSubTruthTable():
"""Returns the truth table of the mini aes NibbleSub function."""
result = []
for i in range(2**blockSize):
tmp = map(mapSbox, blockToNibbles(intToBinBlock(i)).split(' '))
result.append(''.join(tmp))
return result
def generateShiftRowTruthTable():
"""Returns the truth table of the mini aes ShiftRow function."""
result = []
for i in range(2**blockSize):
tmp = []
tabBlock = blockToNibbles(intToBinBlock(i)).split(' ')
tmp.append(tabBlock[0])
tmp.append(tabBlock[3])
tmp.append(tabBlock[2])
tmp.append(tabBlock[1])
result.append(''.join(tmp))
return result
def generateMixColumnsTruthTable():
"""Returns the truth table of the mini aes MixColumns function.
d0 =(0011 x c0) + (0010 x c1)
d1 =(0010 x c0) + (0011 x c1)
d2 =(0011 x c2) + (0010 x c3)
d3 =(0010 x c2) + (0011 x c3)"""
result = []
for i in range(2**blockSize):
tmp = blockToNibbles(intToBinBlock(i)).split(' ')
c0 = tmp[0]
c1 = tmp[1]
c2 = tmp[2]
c3 = tmp[3]
d0 = xorTab(gf24Multiply('0011', c0), gf24Multiply('0010', c1))
d1 = xorTab(gf24Multiply('0010', c0), gf24Multiply('0011', c1))
d2 = xorTab(gf24Multiply('0011', c2), gf24Multiply('0010', c3))
d3 = xorTab(gf24Multiply('0010', c2), gf24Multiply('0011', c3))
result.append(d0 + d1 + d2 + d3)
return result
def generateRoundsKeysTruthTable():
"""Returns the truth table of the mini aes rounds keys."""
k0 = []
k1 = []
k2 = []
for i in range(2**blockSize):
tmp = blockToNibbles(intToBinBlock(i)).split(' ')
w0 = tmp[0]
w1 = tmp[1]
w2 = tmp[2]
w3 = tmp[3]
w4 = xorTab( xorTab(sbox[nibbleToInt(w3)], '0001'), w0)
w5 = xorTab(w1, w4)
w6 = xorTab(w2, w5)
w7 = xorTab(w3, w6)
w8 = xorTab( xorTab(sbox[nibbleToInt(w7)], '0010'), w4)
w9 = xorTab(w5, w8)
w10 = xorTab(w6, w9)
w11 = xorTab(w7, w10)
k0.append(w0 + w1 + w2 + w3)
k1.append(w4 + w5 + w6 + w7)
k2.append(w8 + w9 + w10 + w11)
return(k0, k1, k2)
def generateRoundOneTruthTable():
"""Returns the truth table of the mini aes first round.
Equation is C = NS + SR + MC"""
result = []
ns_tt = generateNibbleSubTruthTable()
sr_tt = generateShiftRowTruthTable()
mc_tt = generateMixColumnsTruthTable()
for i in range(2**blockSize):
tmp = mc_tt[ int(sr_tt[ int(ns_tt[i], 2) ], 2) ]
result.append(tmp)
return result
def generateRoundTwoTruthTable():
"""Returns the truth table of the mini aes second round.
Equation is C = NS + SR"""
result = []
ns_tt = generateNibbleSubTruthTable()
sr_tt = generateShiftRowTruthTable()
for i in range(2**blockSize):
tmp = sr_tt[ int(ns_tt[i], 2) ]
result.append(tmp)
return result