-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalka.py
More file actions
215 lines (173 loc) · 9.11 KB
/
halka.py
File metadata and controls
215 lines (173 loc) · 9.11 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
from utils import b2matrix, matrix2b, xor_bytes, rotate, shift_by, xor_bits, bits2number, number2bits
s_box = (
0x24, 0x2c, 0x20, 0xdc, 0x26, 0x73, 0xd8, 0x91, 0x25, 0xb7, 0x8f, 0x9c, 0xda, 0x1f, 0xfe, 0xe9,
0x9f, 0xa4, 0xd5, 0x6d, 0xc3, 0x71, 0x32, 0x78, 0x96, 0xdb, 0x55, 0xb9, 0x4c, 0x49, 0x6e, 0x42,
0x9a, 0xf9, 0x1d, 0x64, 0x03, 0x5c, 0xa0, 0x00, 0x4a, 0xd7, 0xe3, 0x8e, 0x75, 0xaf, 0x0b, 0x0a,
0x7d, 0x4d, 0x5b, 0x1a, 0x1c, 0xe7, 0x6a, 0x74, 0x10, 0x06, 0x92, 0x29, 0x81, 0x79, 0x17, 0x40,
0x07, 0x7b, 0x69, 0xca, 0xc8, 0xb8, 0xef, 0x84, 0xc2, 0x37, 0x3a, 0x98, 0xdf, 0x66, 0x12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x87, 0xa1,
0xf2, 0x72, 0x48, 0x14, 0xc2, 0xc0, 0x8d, 0x89, 0x44, 0xd3, 0x43, 0xeb, 0x67, 0xdb, 0xad, 0xef,
0xb7, 0xdf, 0xa6, 0xd7, 0x81, 0x12, 0xe4, 0x29, 0x06, 0xde, 0x0c, 0x19, 0x03, 0xc8, 0x6f, 0x4c,
0xc6, 0x5a, 0xc4, 0x2a, 0xe1, 0xd6, 0xe0, 0x35, 0xa4, 0x0f, 0x8a, 0x74, 0xf9, 0x9f, 0xb9, 0x46,
0xb0, 0x56, 0xc1, 0xf7, 0x8e, 0xb3, 0x98, 0xed, 0x94, 0x21, 0xfd, 0xf5, 0x53, 0xa2, 0x0e, 0xe9,
)
permutation_map = [10, 21, 28, 38, 44, 48, 59, 1, 51, 15, 41, 2, 60, 34, 24, 20, 56, 6,
17, 31, 36, 53, 12, 46, 30, 52, 11, 4, 23, 35, 40, 63, 8, 39, 3, 43,57,49,
16,25,37,42,61,50, 0, 9, 18,26,58,55, 7, 19,29,14,47,32,33, 5, 62,45,13,54,22,27]
def shift_rows(s, operations_per_round=None):
""" shift rows for p-box """
s_flatten = []
for row in s:
s_flatten+=row
new_s_flatten = s_flatten.copy()
for i,s_b in enumerate(s_flatten):
new_s_flatten[permutation_map[i]] = s_flatten[i]
ind = 0
for i,row in enumerate(s):
for j,col in enumerate(row):
s[i][j] = new_s_flatten[ind]
ind+=1
# if operations_per_round:
# operations_per_round['perm']+=1
if operations_per_round:
operations_per_round['perm']=i*j
return s, operations_per_round
def inv_shift_rows(s):
""" for decrypting p-box"""
s_flatten = []
for row in s:
s_flatten+=row
new_s_flatten = s_flatten.copy()
for i,s_b in enumerate(s_flatten):
new_s_flatten[permutation_map.index(i)] = s_flatten[i]
ind = 0
for i,row in enumerate(s):
for j,col in enumerate(row):
s[i][j] = new_s_flatten[ind]
ind+=1
return s
def add_round_key(s, k, operations_per_round=None):
for i in range(8):
for j in range(8):
s[i][j] ^= k[i][j]
# if operations_per_round:
# operations_per_round['xor']+=1
if operations_per_round:
operations_per_round['xor']=i*j
return s, operations_per_round
def sub_bits(s):
return number2bits(s_box[bits2number(s)])
def inv_sub_bits(s):
return number2bits(inv_s_box[bits2number(s)])
def sub_bytes(s, operations_per_round=None):
for idx, byte_word in enumerate(s):
s[idx] = sub_bits((byte_word))
if operations_per_round:
operations_per_round['sub']+=1
if operations_per_round:
operations_per_round['sub']=idx
return s, operations_per_round
def inv_sub_bytes(s):
for idx, byte_word in enumerate(s):
s[idx] = inv_sub_bits((byte_word))
return s
class HALKA:
"""
Class for HALKA
"""
def __init__(self, master_key):
"""
Initializes the object with a given key.
"""
assert len(master_key) == 80 # key size must be 80 bits
self.n_rounds = 24
# for halka, length of self._key_matrices should be 25, each subarr is 64 bits
self._key_matrices = self._expand_key(master_key)
self.operations_all_rounds = {'sub':0, 'perm':0, 'xor':0}
def _expand_key(self, master_key):
keys = []
newKS = master_key
for round_counter in range(self.n_rounds+1):
key = []
# rotate 57 bits to the left
newKS = rotate(newKS)
newKS = shift_by(newKS, 57)
# subbing leftmost 8 bit to our s box: 79...72
left_most_8 = newKS[72:]
eight_bits = sub_bits(left_most_8) # apply s-box
newKS[72:] = eight_bits
# xor our round coutner with leftmost 8 bit
rc = number2bits(round_counter, 5) #round counter -> bits
five_bits = newKS[15:20] # 15...19
five_bits = xor_bits(five_bits, rc)
newKS[15:20] = five_bits
# adding left most 64 bits (8x8): 79...16
left_most_64 = b2matrix(list(reversed(newKS[16:])), n=8)
key.append(left_most_64)
keys.extend(key)
return keys
def encrypt_block(self, plaintext):
"""
Encrypts a single block of 8 byte long plaintext.
"""
# each block is 64 bits
assert len(plaintext) == 64
# 8x8 array -> 8 bytes words
plain_state = b2matrix(plaintext, n=8)
plain_state,_ = add_round_key(plain_state, self._key_matrices[0])
for i in range(1, self.n_rounds):
if i ==1:
operations_per_round = {'sub':0, 'perm':0, 'xor':0}
else:
operations_per_round = None
# S block -> XOR with eight 8-bit S-boxes
plain_state, operations_per_round = sub_bytes(plain_state, operations_per_round)
plain_state, operations_per_round = shift_rows(plain_state, operations_per_round) # getting back 8x8
plain_state, operations_per_round = add_round_key(plain_state, self._key_matrices[i], operations_per_round)
if i ==1:
self.operations_one_round = operations_per_round
for k in self.operations_one_round.keys():
self.operations_all_rounds[k]+=self.operations_one_round[k]*self.n_rounds
plain_state,_ = sub_bytes(plain_state)
plain_state,_ = shift_rows(plain_state)
plain_state,_ = add_round_key(plain_state, self._key_matrices[-1])
return sum(plain_state, [])
def decrypt_block(self, ciphertext):
"""
Decrypts a single block of 8 byte long ciphertext.
"""
assert len(ciphertext) == 64
cipher_state = b2matrix(ciphertext, n=8)
cipher_state,_ = add_round_key(cipher_state, self._key_matrices[-1])
cipher_state = inv_shift_rows(cipher_state)
cipher_state = inv_sub_bytes(cipher_state)
for i in range(self.n_rounds - 1, 0, -1):
cipher_state,_ = add_round_key(cipher_state, self._key_matrices[i])
cipher_state = inv_shift_rows(cipher_state) # no mix columns
cipher_state = inv_sub_bytes(cipher_state)
cipher_state,_ = add_round_key(cipher_state, self._key_matrices[0])
return sum(cipher_state, [])