forked from doegox/python-cryptoplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
266 lines (225 loc) · 10.3 KB
/
test.py
File metadata and controls
266 lines (225 loc) · 10.3 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
#!/usr/bin/env python
from pkg_resources import require
require("CryptoPlus>=1.0")
from CryptoPlus.testvectors import dict_ofb_aes, dict_ctr_aes, dict_cfb_aes, dict_cbc_aes
from CryptoPlus.testvectors import dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256,dict_cmac_tdes2,dict_cmac_tdes3
from CryptoPlus.testvectors import dict_des,dict_tdes2,dict_tdes3
from CryptoPlus.testvectors import dict_serpent128,dict_serpent192,dict_serpent256
from CryptoPlus.testvectors import dict_xts_aes
from CryptoPlus.testvectors import sha512_all_zero_messages, radiogatun32, radiogatun64
## HASHING
# SHA-512
print "SHA-512"
from CryptoPlus.Hash import python_SHA512
for i in range(0,len(sha512_all_zero_messages)):
hash = sha512_all_zero_messages[i]
hasher = python_SHA512.new(i*"\x00")
if hash <> hasher.hexdigest().upper():
print 'ERROR! SHA-512 in %i'%i
# RadioGatun
print "RadioGatun"
from CryptoPlus.Hash import python_RadioGatun
for i in range(0,len(radiogatun32)/2):
msg = radiogatun32["msg%i"%i]
hash = radiogatun32["hash%i"%i]
hasher = python_RadioGatun.new(msg,wl=32)
if hash <> hasher.hexdigest().upper():
print 'ERROR! RadioGatun[32] in %i'%i
for i in range(0,len(radiogatun64)/2):
msg = radiogatun64["msg%i"%i]
hash = radiogatun64["hash%i"%i]
hasher = python_RadioGatun.new(msg,wl=64)
if hash <> hasher.hexdigest().upper():
print 'ERROR! RadioGatun[64] in %i'%i
## CIPHERS
# PRESENT
print "PRESENT"
from CryptoPlus.testvectors import dict_present_e80_k12_tvar, dict_present_e128_k12_tvar, dict_present_e128_kvar_t12, dict_present_e80_kvar_t12
from CryptoPlus.Cipher import python_PRESENT
for i in range(1,len(dict_present_e80_k12_tvar)/3):
msg = dict_present_e80_k12_tvar['msg%i'%i].decode('hex')
key = dict_present_e80_k12_tvar['key%i'%i].decode('hex')
cip = dict_present_e80_k12_tvar['cip%i'%i].decode('hex')
cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for present_e80-k12_tvar in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for present_e80-k12_tvar in %i'%i
for i in range(1,len(dict_present_e128_k12_tvar)/3):
msg = dict_present_e128_k12_tvar['msg%i'%i].decode('hex')
key = dict_present_e128_k12_tvar['key%i'%i].decode('hex')
cip = dict_present_e128_k12_tvar['cip%i'%i].decode('hex')
cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for present_e128-k12_tvar in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for present_e128-k12_tvar in %i'%i
for i in range(1,len(dict_present_e128_kvar_t12)/3):
msg = dict_present_e128_kvar_t12['msg%i'%i].decode('hex')
key = dict_present_e128_kvar_t12['key%i'%i].decode('hex')
cip = dict_present_e128_kvar_t12['cip%i'%i].decode('hex')
cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for present_e128-kvar_t12 in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for present_e128-kvar_t12 in %i'%i
for i in range(1,len(dict_present_e80_kvar_t12)/3):
msg = dict_present_e80_kvar_t12['msg%i'%i].decode('hex')
key = dict_present_e80_kvar_t12['key%i'%i].decode('hex')
cip = dict_present_e80_kvar_t12['cip%i'%i].decode('hex')
cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for present_e80-kvar_t12 in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for present_e80-kvar_t12 in %i'%i
# CBC, CFB, OFB and CTR with AES
print "AES"
from CryptoPlus.Cipher import python_AES
from CryptoPlus.Util import util
for i in range(1,len(dict_cbc_aes)/4+1):
msg = dict_cbc_aes['msg%i'%i].decode('hex')
iv = dict_cbc_aes['iv%i'%i].decode('hex')
key = dict_cbc_aes['key%i'%i].decode('hex')
cip = dict_cbc_aes['cip%i'%i].decode('hex')
cipher = python_AES.new(key,python_AES.MODE_CBC,iv)
decipher = python_AES.new(key,python_AES.MODE_CBC,iv)
if cip <> cipher.encrypt(msg):
print 'ERROR! for CBC-AES in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for CBC-AES in %i'%i
for i in range(1,len(dict_ctr_aes)/4+1):
msg = dict_ctr_aes['msg%i'%i].decode('hex')
ctr = dict_ctr_aes['ctr%i'%i].decode('hex')
key = dict_ctr_aes['key%i'%i].decode('hex')
cip = dict_ctr_aes['cip%i'%i].decode('hex')
counter = util.Counter(ctr)
counter2= util.Counter(ctr)
cipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter)
decipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter2)
if cip <> cipher.encrypt(msg):
print 'ERROR! for CTR-AES in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for CTR-AES in %i'%i
for i in range(1,len(dict_ofb_aes)/4+1):
msg = dict_ofb_aes['msg%i'%i].decode('hex')
iv = dict_ofb_aes['iv%i'%i].decode('hex')
key = dict_ofb_aes['key%i'%i].decode('hex')
cip = dict_ofb_aes['cip%i'%i].decode('hex')
cipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
decipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
if cip <> cipher.encrypt(msg):
print 'ERROR! for OFB-AES in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for OFB-AES in %i'%i
for i in range(1,len(dict_cfb_aes)/4+1):
msg = dict_cfb_aes['msg%i'%i].decode('hex')
iv = dict_cfb_aes['iv%i'%i].decode('hex')
s = dict_cfb_aes['s%i'%i]
key = dict_cfb_aes['key%i'%i].decode('hex')
cip = dict_cfb_aes['cip%i'%i].decode('hex')
cipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
decipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
if cip <> cipher.encrypt(msg):
print 'ERROR! for CFB-AES in %i'%i
if msg <> decipher.decrypt(cip):
print 'DECRYPTION ERROR! for CFB-AES in %i'%i
# DES,TDES2/3
print "DES TDES2/3"
from CryptoPlus.Cipher import python_DES
for i in range(0,len(dict_des)/3):
msg = dict_des['msg%i'%i].decode('hex')
key = dict_des['key%i'%i].decode('hex')
cip = dict_des['cip%i'%i].decode('hex')
cipher = python_DES.new(key,python_DES.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for DES in %i'%i
if msg <> cipher.decrypt(cip):
print 'DECRYPTION ERROR! for DES in %i'%i
from CryptoPlus.Cipher import python_DES3
for d in dict_tdes2,dict_tdes3:
for i in range(0,len(d)/3):
msg = d['msg%i'%i].decode('hex')
key = d['key%i'%i].decode('hex')
cip = d['cip%i'%i].decode('hex')
cipher = python_DES3.new(key,python_DES3.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for TDES2/3 in %i'%i
if msg <> cipher.decrypt(cip):
print 'DECRYPTION ERROR! for DES in %i'%i
# Serpent128/192/256
print "Serpent"
from CryptoPlus.Cipher import python_Serpent
for d in dict_serpent128,dict_serpent192,dict_serpent256:
for i in range(0,len(d)/3):
msg = d['msg%i'%i].decode('hex')
key = d['key%i'%i].decode('hex')
cip = d['cip%i'%i].decode('hex')
cipher = python_Serpent.new(key,python_Serpent.MODE_ECB)
if cip <> cipher.encrypt(msg):
print 'ERROR! for Serpent in %i'%i
if msg <> cipher.decrypt(cip):
print 'DECRYPTION ERROR! for Serpent in %i'%i
# CMAC-AES128/192/256
print "CMAC-AES"
from CryptoPlus.Cipher import python_AES
for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256:
for i in range(0,len(d)/4):
msg = d['msg%i'%i].decode('hex')
key = d['key%i'%i].decode('hex')
if msg == '\x00':
msg = ''
mac = d['mac%i'%i].decode('hex')
cipher = python_AES.new(key,python_AES.MODE_CMAC)
if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
print 'ERROR for %i'%i
# CMAC-TDES2/3
print "CMAC-TDES"
from CryptoPlus.Cipher import python_DES3
for d in dict_cmac_tdes2,dict_cmac_tdes3:
for i in range(0,len(d)/4):
msg = d['msg%i'%i].decode('hex')
if msg == '\x00':
msg = ''
key = d['key%i'%i].decode('hex')
mac = d['mac%i'%i].decode('hex')
cipher = python_DES3.new(key,python_DES3.MODE_CMAC)
if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
print 'ERROR! on %i'%i
# XTS-AES
print "XTS-AES"
from CryptoPlus.Cipher import python_AES
for i in range(0,len(dict_xts_aes)/5):
msg = dict_xts_aes['msg%i'%i].decode('hex')
key = ( dict_xts_aes['key1_%i'%i].decode('hex') , dict_xts_aes['key2_%i'%i].decode('hex') )
cip = dict_xts_aes['cip%i'%i].decode('hex')
n = dict_xts_aes['n%i'%i].decode('hex')
cipher = python_AES.new(key,python_AES.MODE_XTS)
if cip <> cipher.encrypt(msg,n):
print 'ERROR! for XTS on %i'%i
print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
decipher = python_AES.new(key,python_AES.MODE_XTS)
if msg <> cipher.decrypt(cip,n):
print 'ERROR! for XTS (decrypt) on %i'%i
print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))
# TWOFISH
print "Twofish"
from CryptoPlus.Cipher import python_Twofish
from CryptoPlus.testvectors import dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256
from CryptoPlus.testvectors import dict_twofish_ecb_vk_k128, dict_twofish_ecb_vk_k192, dict_twofish_ecb_vk_k256
for d in dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256,dict_twofish_ecb_vk_k128:
for i in range(0,len(d)/3):
msg = d['msg%i'%i].decode('hex')
key = d['key%i'%i].decode('hex')
cip = d['cip%i'%i].decode('hex')
cipher = python_Twofish.new(key,python_Twofish.MODE_ECB)
if cip <> cipher.encrypt(msg,n):
print 'ERROR! for Twofish on %i'%i
print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
decipher = python_Twofish.new(key,python_AES.MODE_ECB)
if msg <> cipher.decrypt(cip,n):
print 'DECRYPTION ERROR! for Twofish (decrypt) on %i'%i
print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))