-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.py
288 lines (224 loc) · 9.12 KB
/
fish.py
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
from util import key_generation
import cts
import util
#===========================
tweaks = [3,4]
blocksize = 256
nr = 76
nb_key = 4
mode = cts.MODE_CBC
r_x, r_y = 1, 1
nw = blocksize // 64
#===========================
#set iv
if ( nw == 4 ):
iv = cts.INITIAL_VECTEUR_4
elif (nw == 8 ):
iv = cts.INITIAL_VECTEUR_8
elif (nw == 16 ):
iv = cts.INITIAL_VECTEUR_16
#generation of initials list of keys, nb_keys in a round = nb of messages in a bloc = blocksize / messagesize
keys= key_generation(nb_key)
class cipher_threefish:
def __init__(self, blockSize, nr, key, tweak, mode, c_bloc):
if blockSize not in [ 16,256, 512, 1024]:
exit(0)
if nr not in [ 72, 76, 80]:
exit(0)
if len(tweak) != 2:
exit(0)
self.t = [None]*3
self.t[0], self.t[1] = tweak[0], tweak[1]
self.t[2] = tweak[0] ^ tweak[1]
self.blockSize = blockSize or cts.BLOCK_SIZE_BITS_512
#nr is the total number of rounds
self.nr = nr or cts.ROUNDS_76
#nw is the total number of words
self.nw = int(blockSize/64)
self.mode = mode
#c_bloc is the bloc of bytes to cipher
self.c_bloc = c_bloc[0:len(c_bloc)]
#generation of the first round's sub key calculated by the key and tweak
self.sub_key = key[0:len(key)]
self.k = key
# k is the copy ot the originate key used by key_update()
for i in range(self.nw-3):
self.sub_key[i] = key[i]
self.sub_key[self.nw-3] = ( self.k[self.nw-3] + self.t[0] ) % 2**64
self.sub_key[self.nw-2] = ( self.k[self.nw-2] + self.t[1] ) % 2**64
self.sub_key[self.nw-1] = self.k[self.nw-1]
#calculate k (n+1)
k_add = self.k[0]
for i in range(1,self.nw):
k_add = k_add^self.k[i]
self.k.append(k_add^cts.EXTENDED_KEY_SCHEDULE_CONST)
if self.nw == 4:
self.pi = cts.PI4_NW_4
self.rpi = cts.RPI4_NW_4
self.r = cts.R4_4_4
elif self.nw == 8:
self.pi = cts.PI8_NW_8
self.rpi = cts.RPI8_NW_8
self.r = cts.R8_8_8
elif self.nw == 16:
self.pi = cts.PI16_NW_16
self.rpi = cts.RPI16_NW_16
self.r = cts.R16_16_16
self.depth = cts.DEPTH_OF_D_IN_R
self.t[0], self.t[1] = tweak[0], tweak[1]
self.t.append(tweak[0]^tweak[1])
self.nk = nr/4 + 1
def mix(self, r_x, r_y, np):
np*=2
self.c_bloc[np] = (self.c_bloc[np] + self.c_bloc[np+1])%(2**64)
self.c_bloc[np+1] = self.c_bloc[np]^ (util.rotl(self.c_bloc[np+1], self.r[r_x % self.depth][r_y]))
def demix(self, r_x, r_y, np):
np*=2
self.c_bloc[np+1] = (self.c_bloc[np+1]^self.c_bloc[np] )%(2**64)
self.c_bloc[np+1] = util.rotr(self.c_bloc[np+1], self.r[r_x % self.depth][r_y])
self.c_bloc[np] = (self.c_bloc[np] - self.c_bloc[np+1])%(2**64)
# update the subkey (executed after the end of one round's calculation) for the next round
def key_update(self, c_round):
for i in range(self.nw-3):
self.sub_key[i] = self.k[(int(c_round/4)+i)%(self.nw+1)]
self.sub_key[self.nw-3] = ( self.k[(int(c_round/4)+self.nw-3)%(self.nw+1)] + self.t[int(c_round/4)%3] ) % (2**64)
self.sub_key[self.nw-2] = ( self.k[(int(c_round/4)+self.nw-2)%(self.nw+1)] + self.t[(int(c_round/4)+1)%3] ) % (2**64)
self.sub_key[self.nw-3] = ( self.k[(int(c_round/4)+self.nw-3)%(self.nw+1)] + int(c_round/4) ) % (2**64)
def diminution(self):
for i in range(len(self.c_bloc)):
self.c_bloc[i] = (self.c_bloc[i] - self.sub_key[i] )%(2**64)
def addition(self):
for i in range(len(self.c_bloc)):
self.c_bloc[i] = (self.c_bloc[i] + self.sub_key[i] ) %(2**64)
def permutation(self):
tmp = self.c_bloc[0:len(self.c_bloc)]
for i in range(self.nw):
self.c_bloc[i] = tmp[self.pi[i]]
def depermutation(self):
tmp = self.c_bloc[0:len(self.c_bloc)]
for i in range(self.nw):
self.c_bloc[i] = tmp[self.rpi[i]]
def get_blocs(self):
print(self.c_bloc)
def get_keys(self):
print(self.sub_key)
def cipher_threefish_blocs(mode,c_blocs, keys, blocksize, nr, tweaks):
cipher_blocs=[]
cipher = []
for k in range(len(c_blocs)):
cipher.append(cipher_threefish (blocksize, nr, keys, tweaks, mode, c_blocs[k]))
if (mode == cts.MODE_CBC):
for w in range(nw):
if(k == 0):
cipher[k].c_bloc[w]^=iv[w]
else:
cipher[k].c_bloc[w]^=cipher[k-1].c_bloc[w]
#and for each bloc k to cipher
for i in range(nr+1):
# for each round
if(i>0 and i<77):
for b in range( int(cipher[k].nw/2)):
cipher[k].mix(r_x,r_y,b)
cipher[k].permutation()
if(i%4 == 0):
cipher[k].key_update(i)
cipher[k].addition()
cipher_blocs.append(cipher[k].c_bloc)
return cipher_blocs
def decipher_threefish_blocs(mode,c_blocs, keys, blocksize, nr, tweaks):
cipher=[]
for i in range(len(c_blocs)):
cipher.append(cipher_threefish (blocksize, nr, keys, tweaks, mode, c_blocs[i]))
#start to decipher each bloc
for k in range(len(c_blocs)-1,-1,-1):
for i in range(nr,-1,-1):
if(i%4==0):
cipher[k].key_update(i)
cipher[k].diminution()
if(i>0 and i<77):
cipher[k].depermutation()
for b in range(int(cipher[k].nw/2)):
cipher[k].demix(r_x,r_y,b)
# if on mode CBC, we xor with the precedent ciphered blocs
if (mode == cts.MODE_CBC):
for w in range(nw):
if (k==0):
cipher[k].c_bloc[w]^=iv[w]
else:
cipher[k].c_bloc[w]^=cipher[k-1].c_bloc[w]
for i in range(len(cipher)):
c_blocs[i]=cipher[i].c_bloc
return c_blocs
def decipher_threefish_file(filename,mode):
fo = open(filename,"r")
str1=fo.read()
fo.closed
blocs_list=str1.split('0b')
c_blocs = []
tmp_list = []
for i in range(1,len(blocs_list)):
tmp_list.append(int(blocs_list[i],2))
if (i%4 == 0 ):
c_blocs.append(tmp_list)
tmp_list=[]
#set iv
if ( nw == 4 ) : iv = cts.INITIAL_VECTEUR_4
elif (nw == 8 ) : iv = cts.INITIAL_VECTEUR_8
elif (nw == 16 ) : iv = cts.INITIAL_VECTEUR_16
#generation of initials list of keys, nb_keys in a round = nb of messages in a bloc = blocksize / messagesize
keys= key_generation(nb_key)
#c_blocs= util.readFile(filename, blocksize)
c_blocs = decipher_threefish_blocs(mode,c_blocs, keys, blocksize, nr, tweaks)
fo = open(filename,"w")
fo.write(util.writeMsg(c_blocs))
fo.close()
def cipher_threefish_file(filename,mode):
#set iv
if ( nw == 4 ) : iv = cts.INITIAL_VECTEUR_4
elif (nw == 8 ) : iv = cts.INITIAL_VECTEUR_8
elif (nw == 16 ) : iv = cts.INITIAL_VECTEUR_16
#generation of initials list of keys, nb_keys in a round = nb of messages in a bloc = blocksize / messagesize
keys = key_generation(nb_key)
c_blocs = util.readFile(filename, blocksize)
c_blocs = cipher_threefish_blocs(mode,c_blocs, keys, blocksize, nr, tweaks)
#util.writeFile(filename, c_blocs)
str1=""
for i in range(len(c_blocs)):
for j in range(nw):
str1+=str(bin(c_blocs[i][j]))
fo = open(filename,"w+")
fo.write(str1)
fo.close()
def cipher_threefish_msg(msg,mode):
#set iv
if ( nw == 4 ) : iv = cts.INITIAL_VECTEUR_4
elif (nw == 8 ) : iv = cts.INITIAL_VECTEUR_8
elif (nw == 16 ) : iv = cts.INITIAL_VECTEUR_16
#generation of initials list of keys, nb_keys in a round = nb of messages in a bloc = blocksize / messagesize
keys= key_generation(nb_key)
#get blocs from files
c_blocs= util.readMsg(msg,blocksize)
c_blocs = cipher_threefish_blocs(mode,c_blocs, keys, blocksize, nr, tweaks)
str1=""
for i in range(len(c_blocs)):
for j in range(nw):
str1+=str(bin(c_blocs[i][j]))
return str1
def decipher_threefish_msg(msg,mode):
blocs_list=msg.split('0b')
c_blocs, tmp_list = [], []
for i in range(1,len(blocs_list)):
tmp_list.append(int(blocs_list[i],2))
if (i%4 == 0 ):
c_blocs.append(tmp_list)
tmp_list=[]
#set iv
if ( nw == 4 ) : iv = cts.INITIAL_VECTEUR_4
elif (nw == 8 ) : iv = cts.INITIAL_VECTEUR_8
elif (nw == 16 ) : iv = cts.INITIAL_VECTEUR_16
#generation of initials list of keys, nb_keys in a round = nb of messages in a bloc = blocksize / messagesize
keys= key_generation(nb_key)
#c_blocs= util.readFile(filename, blocksize)
c_blocs = decipher_threefish_blocs(mode,c_blocs, keys, blocksize, nr, tweaks)
decipher_text=util.writeMsg(c_blocs)
return decipher_text