-
Notifications
You must be signed in to change notification settings - Fork 3
/
tea.py
221 lines (170 loc) · 5.52 KB
/
tea.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
import math, sys
sys.path.append("..")
from cryptopals_lib import *
class TEA():
MAX_UINT = 0xffffffff
DELTA = 0x9E3779B9 #2654435769
INIT_TEMP = 0xC6EF3720
def __init__(self, key):
self.key = bytes_to_intarray(key, 4)
if len(self.key) != 4:
raise ValueException("Key is not 16 bytes long")
def encrypt(self, plaintext):
output = []
#Split input
int_plaintext = bytes_to_intarray(plaintext, 4)
#Pad so its a mult of 2
if len(int_plaintext) %2 == 1:
int_plaintext.append(0)
#Split into chunks of length 2
for idx in range(0, len(int_plaintext), 2):
output += self._block_encrypt(int_plaintext[idx], int_plaintext[idx + 1])
#print(output)
return intarray_to_bytes(output, 4)
def encrypt_xtea(self, plaintext):
output = []
#Split input
int_plaintext = bytes_to_intarray(plaintext, 4)
#Pad so its a mult of 2
if len(int_plaintext) %2 == 1:
int_plaintext.append(0)
#Split into chunks of length 2
for idx in range(0, len(int_plaintext), 2):
output += self._block_encrypt_xtea(int_plaintext[idx], int_plaintext[idx + 1])
#print(output)
return intarray_to_bytes(output, 4)
def _block_encrypt(self, y, z):
temp = 0#INIT_TEMP
#32 Rounds
for _ in range(32):
temp = asint32(temp + self.DELTA)
y = asint32(y + (
asint32(asint32(z << 4) + self.key[0]) ^
asint32(z + temp) ^
asint32((z >> 5) + self.key[1])
))
z = asint32(z + (
asint32(asint32(y << 4) + self.key[2]) ^
asint32(y + temp) ^
asint32((y >> 5) + self.key[3])
))
return [y,z]
def _block_encrypt_xtea(self, y, z):
temp = 0#INIT_TEMP
#32 Rounds
for _ in range(32):
y = asint32(y + (
asint32((asint32(z << 4) ^ (z >> 5)) + z) ^
asint32(temp + self.key[temp&3])
))
temp = asint32(temp + self.DELTA)
z = asint32(z + (
asint32((asint32(y << 4) ^ (y >> 5)) + y) ^
asint32(temp + self.key[(temp >> 11)&3])
))
return [y,z]
def decrypt(self, ciphertext):
output = []
#Split input
int_ciphertext = bytes_to_intarray(ciphertext, 4)
#Split into chunks of length 2
for idx in range(0, len(int_ciphertext), 2):
output += self._block_decrypt(int_ciphertext[idx], int_ciphertext[idx + 1])
return intarray_to_bytes(output, 4)
def decrypt_xtea(self, ciphertext):
output = []
#Split input
int_ciphertext = bytes_to_intarray(ciphertext, 4)
#Split into chunks of length 2
for idx in range(0, len(int_ciphertext), 2):
output += self._block_decrypt_xtea(int_ciphertext[idx], int_ciphertext[idx + 1])
return intarray_to_bytes(output, 4)
def _block_decrypt(self, y, z):
temp = self.INIT_TEMP
#32 Rounds
for _ in range(32):
z = asint32(z - (
asint32(asint32(y << 4) + self.key[2]) ^
asint32(y + temp) ^
asint32((y >> 5) + self.key[3])
))
y = asint32(y - (
asint32(asint32(z << 4) + self.key[0]) ^
asint32(z + temp) ^
asint32((z >> 5) + self.key[1])
))
temp = asint32(temp - self.DELTA)
#print(z,y,temp)
return [y,z]
def _block_decrypt_xtea(self, y, z):
temp = self.INIT_TEMP
#32 Rounds
for _ in range(32):
z = asint32(z - (
asint32((asint32(y << 4) ^ (y >> 5)) + y) ^
asint32(temp + self.key[(temp>>11) & 3])
))
temp = asint32(temp - self.DELTA)
y = asint32(y - (
asint32((asint32(z << 4) ^ (z >> 5)) + z) ^
asint32(temp + self.key[temp & 3])
))
#print(z,y,temp)
return [y,z]
if __name__ == '__main__':
#Test Vectors https://github.com/liut/TeaCrypt/blob/master/tea/tea_test.go
key = hex_to_bytes("00000000000000000000000000000000")
tea = TEA(key)
message = b"\x00" * 8
ciphertext = tea.encrypt(message)
print(f"Ciphertext: {ciphertext.hex()}")
#Ciphertext: 0a3aea4140a9ba94
message2 = tea.decrypt(ciphertext)
print(f"Message: {message2.hex()}")
#Message: 0000000000000000
key = hex_to_bytes("00000000000000000000000000000000")
tea = TEA(key)
message = b"\x04\x03\x02\x01\x08\x07\x06\x05"
ciphertext = tea.encrypt(message)
print(f"Ciphertext: {ciphertext.hex()}")
#Ciphertext: f39c2f6a553ccffc
message2 = tea.decrypt(ciphertext)
print(f"Message: {message2.hex()}")
#Message: 0403020108070605
key = hex_to_bytes("33221100 77665544 BBAA9988 FFEEDDCC")
tea = TEA(key)
message = b"\x04\x03\x02\x01\x08\x07\x06\x05"
ciphertext = tea.encrypt(message)
print(f"Ciphertext: {ciphertext.hex()}")
#Ciphertext: a2c0b1deb35d747e
message2 = tea.decrypt(ciphertext)
print(f"Message: {message2.hex()}")
#Message: 0403020108070605
key = hex_to_bytes("33221100 77665544 BBAA9988 FFEEDDCC")
tea = TEA(key)
message = b"\x67\x45\x23\x01\xEF\xCD\xAB\x89"
ciphertext = tea.encrypt(message)
print(f"Ciphertext: {ciphertext.hex()}")
#Ciphertext: 926b6c123e3a65c0
message2 = tea.decrypt(ciphertext)
print(f"Message: {message2.hex()}")
#Message: 67452301efcdab89
#Test Vectors https://www.tayloredge.com/reference/Mathematics/XTEA.pdf
key = intarray_to_bytes([0x27F917B1,0xC1DA8993,0x60E2ACAA,0xA6EB923D], 4)
tea = TEA(key)
message = intarray_to_bytes([0xAF20A390,0x547571AA], 4)
ciphertext = tea.encrypt_xtea(message)
print(f"Ciphertext: {ciphertext.hex()}")
#Ciphertext: af2864d28322200a
message2 = tea.decrypt_xtea(ciphertext)
print(f"Message: {message2.hex()}")
#Message: 90a320afaa717554
key = intarray_to_bytes([0x31415926,0x53589793,0x23846264,0x33832795], 4)
tea = TEA(key)
message = intarray_to_bytes([0x02884197,0x16939937], 4)
ciphertext = tea.encrypt_xtea(message)
print(f"Ciphertext: {ciphertext.hex()}")
#Ciphertext: 7d00e246eac2bb58
message2 = tea.decrypt_xtea(ciphertext)
print(f"Message: {message2.hex()}")
#Message: 9741880237999316