License includes hash functions
[python-cryptoplus.git] / test / test.py
blob40397c1c995f47a6199f99843d1052ca9f71bd87
1 #!/usr/bin/env python
3 from pkg_resources import require
4 require("CryptoPlus>=1.0")
5 from CryptoPlus.testvectors import dict_ofb_aes, dict_ctr_aes, dict_cfb_aes, dict_cbc_aes
6 from CryptoPlus.testvectors import dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256,dict_cmac_tdes2,dict_cmac_tdes3
7 from CryptoPlus.testvectors import dict_des,dict_tdes2,dict_tdes3
8 from CryptoPlus.testvectors import dict_serpent128,dict_serpent192,dict_serpent256
9 from CryptoPlus.testvectors import dict_xts_aes
10 from CryptoPlus.testvectors import sha512_all_zero_messages, radiogatun32, radiogatun64
12 ## HASHING
14 # SHA-512
15 print "SHA-512"
17 from CryptoPlus.Hash import python_SHA512
19 for i in range(0,len(sha512_all_zero_messages)):
20 hash = sha512_all_zero_messages[i]
21 hasher = python_SHA512.new(i*"\x00")
22 if hash <> hasher.hexdigest().upper():
23 print 'ERROR! SHA-512 in %i'%i
25 # RadioGatun
26 print "RadioGatun"
28 from CryptoPlus.Hash import python_RadioGatun
30 for i in range(0,len(radiogatun32)/2):
31 msg = radiogatun32["msg%i"%i]
32 hash = radiogatun32["hash%i"%i]
33 hasher = python_RadioGatun.new(32,msg)
34 if hash <> hasher.hexdigest().upper():
35 print 'ERROR! RadioGatun[32] in %i'%i
37 for i in range(0,len(radiogatun64)/2):
38 msg = radiogatun64["msg%i"%i]
39 hash = radiogatun64["hash%i"%i]
40 hasher = python_RadioGatun.new(64,msg)
41 if hash <> hasher.hexdigest().upper():
42 print 'ERROR! RadioGatun[64] in %i'%i
44 ## CIPHERS
46 # PRESENT
47 print "PRESENT"
49 from CryptoPlus.testvectors import dict_present_e80_k12_tvar, dict_present_e128_k12_tvar, dict_present_e128_kvar_t12, dict_present_e80_kvar_t12
50 from CryptoPlus.Cipher import python_PRESENT
52 for i in range(1,len(dict_present_e80_k12_tvar)/3):
53 msg = dict_present_e80_k12_tvar['msg%i'%i].decode('hex')
54 key = dict_present_e80_k12_tvar['key%i'%i].decode('hex')
55 cip = dict_present_e80_k12_tvar['cip%i'%i].decode('hex')
56 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
57 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
58 if cip <> cipher.encrypt(msg):
59 print 'ERROR! for present_e80-k12_tvar in %i'%i
60 if msg <> decipher.decrypt(cip):
61 print 'DECRYPTION ERROR! for present_e80-k12_tvar in %i'%i
63 for i in range(1,len(dict_present_e128_k12_tvar)/3):
64 msg = dict_present_e128_k12_tvar['msg%i'%i].decode('hex')
65 key = dict_present_e128_k12_tvar['key%i'%i].decode('hex')
66 cip = dict_present_e128_k12_tvar['cip%i'%i].decode('hex')
67 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
68 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
69 if cip <> cipher.encrypt(msg):
70 print 'ERROR! for present_e128-k12_tvar in %i'%i
71 if msg <> decipher.decrypt(cip):
72 print 'DECRYPTION ERROR! for present_e128-k12_tvar in %i'%i
74 for i in range(1,len(dict_present_e128_kvar_t12)/3):
75 msg = dict_present_e128_kvar_t12['msg%i'%i].decode('hex')
76 key = dict_present_e128_kvar_t12['key%i'%i].decode('hex')
77 cip = dict_present_e128_kvar_t12['cip%i'%i].decode('hex')
78 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
79 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
80 if cip <> cipher.encrypt(msg):
81 print 'ERROR! for present_e128-kvar_t12 in %i'%i
82 if msg <> decipher.decrypt(cip):
83 print 'DECRYPTION ERROR! for present_e128-kvar_t12 in %i'%i
85 for i in range(1,len(dict_present_e80_kvar_t12)/3):
86 msg = dict_present_e80_kvar_t12['msg%i'%i].decode('hex')
87 key = dict_present_e80_kvar_t12['key%i'%i].decode('hex')
88 cip = dict_present_e80_kvar_t12['cip%i'%i].decode('hex')
89 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
90 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
91 if cip <> cipher.encrypt(msg):
92 print 'ERROR! for present_e80-kvar_t12 in %i'%i
93 if msg <> decipher.decrypt(cip):
94 print 'DECRYPTION ERROR! for present_e80-kvar_t12 in %i'%i
96 # CBC, CFB, OFB and CTR with AES
97 print "AES"
99 from CryptoPlus.Cipher import python_AES
100 from CryptoPlus.Util import util
102 for i in range(1,len(dict_cbc_aes)/4+1):
103 msg = dict_cbc_aes['msg%i'%i].decode('hex')
104 iv = dict_cbc_aes['iv%i'%i].decode('hex')
105 key = dict_cbc_aes['key%i'%i].decode('hex')
106 cip = dict_cbc_aes['cip%i'%i].decode('hex')
107 cipher = python_AES.new(key,python_AES.MODE_CBC,iv)
108 decipher = python_AES.new(key,python_AES.MODE_CBC,iv)
109 if cip <> cipher.encrypt(msg):
110 print 'ERROR! for CBC-AES in %i'%i
111 if msg <> decipher.decrypt(cip):
112 print 'DECRYPTION ERROR! for CBC-AES in %i'%i
114 for i in range(1,len(dict_ctr_aes)/4+1):
115 msg = dict_ctr_aes['msg%i'%i].decode('hex')
116 ctr = dict_ctr_aes['ctr%i'%i].decode('hex')
117 key = dict_ctr_aes['key%i'%i].decode('hex')
118 cip = dict_ctr_aes['cip%i'%i].decode('hex')
119 counter = util.Counter(ctr)
120 counter2= util.Counter(ctr)
121 cipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter)
122 decipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter2)
123 if cip <> cipher.encrypt(msg):
124 print 'ERROR! for CTR-AES in %i'%i
125 if msg <> decipher.decrypt(cip):
126 print 'DECRYPTION ERROR! for CTR-AES in %i'%i
128 for i in range(1,len(dict_ofb_aes)/4+1):
129 msg = dict_ofb_aes['msg%i'%i].decode('hex')
130 iv = dict_ofb_aes['iv%i'%i].decode('hex')
131 key = dict_ofb_aes['key%i'%i].decode('hex')
132 cip = dict_ofb_aes['cip%i'%i].decode('hex')
133 cipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
134 decipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
135 if cip <> cipher.encrypt(msg):
136 print 'ERROR! for OFB-AES in %i'%i
137 if msg <> decipher.decrypt(cip):
138 print 'DECRYPTION ERROR! for OFB-AES in %i'%i
140 for i in range(1,len(dict_cfb_aes)/4+1):
141 msg = dict_cfb_aes['msg%i'%i].decode('hex')
142 iv = dict_cfb_aes['iv%i'%i].decode('hex')
143 s = dict_cfb_aes['s%i'%i]
144 key = dict_cfb_aes['key%i'%i].decode('hex')
145 cip = dict_cfb_aes['cip%i'%i].decode('hex')
146 cipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
147 decipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
148 if cip <> cipher.encrypt(msg):
149 print 'ERROR! for CFB-AES in %i'%i
150 if msg <> decipher.decrypt(cip):
151 print 'DECRYPTION ERROR! for CFB-AES in %i'%i
153 # DES,TDES2/3
154 print "DES TDES2/3"
156 from CryptoPlus.Cipher import python_DES
158 for i in range(0,len(dict_des)/3):
159 msg = dict_des['msg%i'%i].decode('hex')
160 key = dict_des['key%i'%i].decode('hex')
161 cip = dict_des['cip%i'%i].decode('hex')
162 cipher = python_DES.new(key,python_DES.MODE_ECB)
163 if cip <> cipher.encrypt(msg):
164 print 'ERROR! for DES in %i'%i
165 if msg <> cipher.decrypt(cip):
166 print 'DECRYPTION ERROR! for DES in %i'%i
168 from CryptoPlus.Cipher import python_DES3
170 for d in dict_tdes2,dict_tdes3:
171 for i in range(0,len(d)/3):
172 msg = d['msg%i'%i].decode('hex')
173 key = d['key%i'%i].decode('hex')
174 cip = d['cip%i'%i].decode('hex')
175 cipher = python_DES3.new(key,python_DES3.MODE_ECB)
176 if cip <> cipher.encrypt(msg):
177 print 'ERROR! for TDES2/3 in %i'%i
178 if msg <> cipher.decrypt(cip):
179 print 'DECRYPTION ERROR! for DES in %i'%i
181 # Serpent128/192/256
182 print "Serpent"
184 from CryptoPlus.Cipher import python_Serpent
186 for d in dict_serpent128,dict_serpent192,dict_serpent256:
187 for i in range(0,len(d)/3):
188 msg = d['msg%i'%i].decode('hex')
189 key = d['key%i'%i].decode('hex')
190 cip = d['cip%i'%i].decode('hex')
191 cipher = python_Serpent.new(key,python_Serpent.MODE_ECB)
192 if cip <> cipher.encrypt(msg):
193 print 'ERROR! for Serpent in %i'%i
194 if msg <> cipher.decrypt(cip):
195 print 'DECRYPTION ERROR! for Serpent in %i'%i
197 # CMAC-AES128/192/256
198 print "CMAC-AES"
200 from CryptoPlus.Cipher import python_AES
202 for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256:
203 for i in range(0,len(d)/4):
204 msg = d['msg%i'%i].decode('hex')
205 key = d['key%i'%i].decode('hex')
206 if msg == '\x00':
207 msg = ''
208 mac = d['mac%i'%i].decode('hex')
209 cipher = python_AES.new(key,python_AES.MODE_CMAC)
210 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
211 print 'ERROR for %i'%i
213 # CMAC-TDES2/3
214 print "CMAC-TDES"
215 from CryptoPlus.Cipher import python_DES3
217 for d in dict_cmac_tdes2,dict_cmac_tdes3:
218 for i in range(0,len(d)/4):
219 msg = d['msg%i'%i].decode('hex')
220 if msg == '\x00':
221 msg = ''
222 key = d['key%i'%i].decode('hex')
223 mac = d['mac%i'%i].decode('hex')
224 cipher = python_DES3.new(key,python_DES3.MODE_CMAC)
225 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
226 print 'ERROR! on %i'%i
228 # XTS-AES
229 print "XTS-AES"
231 from CryptoPlus.Cipher import python_AES
233 for i in range(0,len(dict_xts_aes)/5):
234 msg = dict_xts_aes['msg%i'%i].decode('hex')
235 key = ( dict_xts_aes['key1_%i'%i].decode('hex') , dict_xts_aes['key2_%i'%i].decode('hex') )
236 cip = dict_xts_aes['cip%i'%i].decode('hex')
237 n = dict_xts_aes['n%i'%i].decode('hex')
238 cipher = python_AES.new(key,python_AES.MODE_XTS)
239 if cip <> cipher.encrypt(msg,n):
240 print 'ERROR! for XTS on %i'%i
241 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
242 decipher = python_AES.new(key,python_AES.MODE_XTS)
243 if msg <> cipher.decrypt(cip,n):
244 print 'ERROR! for XTS (decrypt) on %i'%i
245 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))
247 # TWOFISH
248 print "Twofish"
250 from CryptoPlus.Cipher import python_Twofish
251 from CryptoPlus.testvectors import dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256
252 from CryptoPlus.testvectors import dict_twofish_ecb_vk_k128, dict_twofish_ecb_vk_k192, dict_twofish_ecb_vk_k256
254 for d in dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256,dict_twofish_ecb_vk_k128:
255 for i in range(0,len(d)/3):
256 msg = d['msg%i'%i].decode('hex')
257 key = d['key%i'%i].decode('hex')
258 cip = d['cip%i'%i].decode('hex')
259 cipher = python_Twofish.new(key,python_Twofish.MODE_ECB)
260 if cip <> cipher.encrypt(msg,n):
261 print 'ERROR! for Twofish on %i'%i
262 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
263 decipher = python_Twofish.new(key,python_AES.MODE_ECB)
264 if msg <> cipher.decrypt(cip,n):
265 print 'DECRYPTION ERROR! for Twofish (decrypt) on %i'%i
266 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))