forked from DenseLance/HIGHT-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hight.py
152 lines (134 loc) · 3.72 KB
/
hight.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
def list_to_byte(lst):
byte = 0
for bit in lst:
byte = (byte << 1) | bit
return byte
def rotate_bits(x, n): # shift bits leftward
return ((x << n) % 256) | (x >> (8 - n))
def whitening_key_generation(MK):
WK = [None] * 8
for i in range(4):
WK[i] = MK[i + 12]
WK[i + 4] = MK[i]
return WK
def constant_generation():
s = [0, 1, 0, 1, 1, 0, 1]
delta = [list_to_byte(s[::-1])]
for i in range(1, 128):
s.append(s[i + 2] ^ s[i - 1])
delta.append(list_to_byte(s[i:i + 7][::-1]))
return delta
def subkey_generation(delta, MK):
SK = [None] * 128
for i in range(8):
for j in range(8):
SK[16 * i + j] = (MK[(j - i) % 8] + delta[16 * i + j]) % 256
for j in range(8):
SK[16 * i + j + 8] = (MK[(j - i) % 8 + 8] + delta[16 * i + j + 8]) % 256
return SK
def encryption_key_schedule(MK):
delta = constant_generation()
WK = whitening_key_generation(MK)
SK = subkey_generation(delta, MK)
return WK, SK
def decryption_key_schedule(MK):
delta = constant_generation()
WK = whitening_key_generation(MK)
SK = subkey_generation(delta, MK)[::-1]
return WK, SK
def encryption_initial_transformation(P, WK):
X_0 = [
(P[0] + WK[0]) % 256,
P[1],
P[2] ^ WK[1],
P[3],
(P[4] + WK[2]) % 256,
P[5],
P[6] ^ WK[3],
P[7]
]
return X_0
def decryption_initial_transformation(C, WK):
X_0 = [
C[7],
(C[0] - WK[4]) % 256,
C[1],
C[2] ^ WK[5],
C[3],
(C[4] - WK[6]) % 256,
C[5],
C[6] ^ WK[7]
]
return X_0
def f_0(x):
return rotate_bits(x,1) ^ rotate_bits(x,2) ^ rotate_bits(x,7)
def f_1(x):
return rotate_bits(x,3) ^ rotate_bits(x,4) ^ rotate_bits(x,6)
def encryption_round_function(i, X_i, SK):
X_j = [
X_i[7] ^ ((f_0(X_i[6]) + SK[4 * i + 3]) % 256),
X_i[0],
(X_i[1] + (f_1(X_i[0]) ^ SK[4 * i])) % 256,
X_i[2],
X_i[3] ^ ((f_0(X_i[2]) + SK[4 * i + 1]) % 256),
X_i[4],
(X_i[5] + (f_1(X_i[4]) ^ SK[4 * i + 2])) % 256,
X_i[6]
]
return X_j
def decryption_round_function(i, X_i, SK):
X_j = [
X_i[1],
(X_i[2] - (f_1(X_i[1]) ^ SK[4 * i + 3])) % 256,
X_i[3],
X_i[4] ^ ((f_0(X_i[3]) + SK[4 * i + 2]) % 256),
X_i[5],
(X_i[6] - (f_1(X_i[5]) ^ SK[4 * i + 1])) % 256,
X_i[7],
X_i[0] ^ ((f_0(X_i[7]) + SK[4 * i]) % 256)
]
return X_j
def encryption_final_transformation(X_32, WK):
C = [
(X_32[1] + WK[4]) % 256,
X_32[2],
X_32[3] ^ WK[5],
X_32[4],
(X_32[5] + WK[6]) % 256,
X_32[6],
X_32[7] ^ WK[7],
X_32[0]
]
return C
def decryption_final_transformation(X_32, WK):
D = [
(X_32[0] - WK[0]) % 256,
X_32[1],
X_32[2] ^ WK[1],
X_32[3],
(X_32[4] - WK[2]) % 256,
X_32[5],
X_32[6] ^ WK[3],
X_32[7]
]
return D
def encryption_transformation(P, WK, SK):
X_i = encryption_initial_transformation(P, WK)
for i in range(32):
X_i = encryption_round_function(i, X_i, SK)
C = encryption_final_transformation(X_i, WK)
return C
def decryption_transformation(C, WK, SK):
X_i = decryption_initial_transformation(C, WK)
for i in range(32):
X_i = decryption_round_function(i, X_i, SK)
D = decryption_final_transformation(X_i, WK)
return D
def hight_encryption(P, MK):
WK, SK = encryption_key_schedule(MK)
C = encryption_transformation(P, WK, SK)
return C
def hight_decryption(C, MK):
WK, SK = decryption_key_schedule(MK)
D = decryption_transformation(C, WK, SK)
return D