d3a097710043cf67e22743d41d02f11a70cd578d
[python-cryptoplus.git] / test / test.py
blobd3a097710043cf67e22743d41d02f11a70cd578d
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
12 # SHA-512
13 print "SHA-512"
15 from CryptoPlus.Hash import python_SHA512
17 for i in range(0,len(sha512_all_zero_messages)):
18 hash = sha512_all_zero_messages[i]
19 hasher = python_SHA512.new(i*"\x00")
20 if hash <> hasher.hexdigest().upper():
21 print 'ERROR! SHA-512 in %i'%i
23 # PRESENT
24 print "PRESENT"
26 from CryptoPlus.testvectors import dict_present_e80_k12_tvar, dict_present_e128_k12_tvar, dict_present_e128_kvar_t12, dict_present_e80_kvar_t12
27 from CryptoPlus.Cipher import python_PRESENT
29 for i in range(1,len(dict_present_e80_k12_tvar)/3):
30 msg = dict_present_e80_k12_tvar['msg%i'%i].decode('hex')
31 key = dict_present_e80_k12_tvar['key%i'%i].decode('hex')
32 cip = dict_present_e80_k12_tvar['cip%i'%i].decode('hex')
33 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
34 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
35 if cip <> cipher.encrypt(msg):
36 print 'ERROR! for present_e80-k12_tvar in %i'%i
37 if msg <> decipher.decrypt(cip):
38 print 'DECRYPTION ERROR! for present_e80-k12_tvar in %i'%i
40 for i in range(1,len(dict_present_e128_k12_tvar)/3):
41 msg = dict_present_e128_k12_tvar['msg%i'%i].decode('hex')
42 key = dict_present_e128_k12_tvar['key%i'%i].decode('hex')
43 cip = dict_present_e128_k12_tvar['cip%i'%i].decode('hex')
44 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
45 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
46 if cip <> cipher.encrypt(msg):
47 print 'ERROR! for present_e128-k12_tvar in %i'%i
48 if msg <> decipher.decrypt(cip):
49 print 'DECRYPTION ERROR! for present_e128-k12_tvar in %i'%i
51 for i in range(1,len(dict_present_e128_kvar_t12)/3):
52 msg = dict_present_e128_kvar_t12['msg%i'%i].decode('hex')
53 key = dict_present_e128_kvar_t12['key%i'%i].decode('hex')
54 cip = dict_present_e128_kvar_t12['cip%i'%i].decode('hex')
55 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
56 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
57 if cip <> cipher.encrypt(msg):
58 print 'ERROR! for present_e128-kvar_t12 in %i'%i
59 if msg <> decipher.decrypt(cip):
60 print 'DECRYPTION ERROR! for present_e128-kvar_t12 in %i'%i
62 for i in range(1,len(dict_present_e80_kvar_t12)/3):
63 msg = dict_present_e80_kvar_t12['msg%i'%i].decode('hex')
64 key = dict_present_e80_kvar_t12['key%i'%i].decode('hex')
65 cip = dict_present_e80_kvar_t12['cip%i'%i].decode('hex')
66 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
67 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
68 if cip <> cipher.encrypt(msg):
69 print 'ERROR! for present_e80-kvar_t12 in %i'%i
70 if msg <> decipher.decrypt(cip):
71 print 'DECRYPTION ERROR! for present_e80-kvar_t12 in %i'%i
73 # CBC, CFB, OFB and CTR with AES
74 print "AES"
76 from CryptoPlus.Cipher import python_AES
77 from CryptoPlus.Util import util
79 for i in range(1,len(dict_cbc_aes)/4+1):
80 msg = dict_cbc_aes['msg%i'%i].decode('hex')
81 iv = dict_cbc_aes['iv%i'%i].decode('hex')
82 key = dict_cbc_aes['key%i'%i].decode('hex')
83 cip = dict_cbc_aes['cip%i'%i].decode('hex')
84 cipher = python_AES.new(key,python_AES.MODE_CBC,iv)
85 decipher = python_AES.new(key,python_AES.MODE_CBC,iv)
86 if cip <> cipher.encrypt(msg):
87 print 'ERROR! for CBC-AES in %i'%i
88 if msg <> decipher.decrypt(cip):
89 print 'DECRYPTION ERROR! for CBC-AES in %i'%i
91 for i in range(1,len(dict_ctr_aes)/4+1):
92 msg = dict_ctr_aes['msg%i'%i].decode('hex')
93 ctr = dict_ctr_aes['ctr%i'%i].decode('hex')
94 key = dict_ctr_aes['key%i'%i].decode('hex')
95 cip = dict_ctr_aes['cip%i'%i].decode('hex')
96 counter = util.Counter(ctr)
97 counter2= util.Counter(ctr)
98 cipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter)
99 decipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter2)
100 if cip <> cipher.encrypt(msg):
101 print 'ERROR! for CTR-AES in %i'%i
102 if msg <> decipher.decrypt(cip):
103 print 'DECRYPTION ERROR! for CTR-AES in %i'%i
105 for i in range(1,len(dict_ofb_aes)/4+1):
106 msg = dict_ofb_aes['msg%i'%i].decode('hex')
107 iv = dict_ofb_aes['iv%i'%i].decode('hex')
108 key = dict_ofb_aes['key%i'%i].decode('hex')
109 cip = dict_ofb_aes['cip%i'%i].decode('hex')
110 cipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
111 decipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
112 if cip <> cipher.encrypt(msg):
113 print 'ERROR! for OFB-AES in %i'%i
114 if msg <> decipher.decrypt(cip):
115 print 'DECRYPTION ERROR! for OFB-AES in %i'%i
117 for i in range(1,len(dict_cfb_aes)/4+1):
118 msg = dict_cfb_aes['msg%i'%i].decode('hex')
119 iv = dict_cfb_aes['iv%i'%i].decode('hex')
120 s = dict_cfb_aes['s%i'%i]
121 key = dict_cfb_aes['key%i'%i].decode('hex')
122 cip = dict_cfb_aes['cip%i'%i].decode('hex')
123 cipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
124 decipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
125 if cip <> cipher.encrypt(msg):
126 print 'ERROR! for CFB-AES in %i'%i
127 if msg <> decipher.decrypt(cip):
128 print 'DECRYPTION ERROR! for CFB-AES in %i'%i
130 # DES,TDES2/3
131 print "DES TDES2/3"
133 from CryptoPlus.Cipher import python_DES
135 for i in range(0,len(dict_des)/3):
136 msg = dict_des['msg%i'%i].decode('hex')
137 key = dict_des['key%i'%i].decode('hex')
138 cip = dict_des['cip%i'%i].decode('hex')
139 cipher = python_DES.new(key,python_DES.MODE_ECB)
140 if cip <> cipher.encrypt(msg):
141 print 'ERROR! for DES in %i'%i
142 if msg <> cipher.decrypt(cip):
143 print 'DECRYPTION ERROR! for DES in %i'%i
145 from CryptoPlus.Cipher import python_DES3
147 for d in dict_tdes2,dict_tdes3:
148 for i in range(0,len(d)/3):
149 msg = d['msg%i'%i].decode('hex')
150 key = d['key%i'%i].decode('hex')
151 cip = d['cip%i'%i].decode('hex')
152 cipher = python_DES3.new(key,python_DES3.MODE_ECB)
153 if cip <> cipher.encrypt(msg):
154 print 'ERROR! for TDES2/3 in %i'%i
155 if msg <> cipher.decrypt(cip):
156 print 'DECRYPTION ERROR! for DES in %i'%i
158 # Serpent128/192/256
159 print "Serpent"
161 from CryptoPlus.Cipher import python_Serpent
163 for d in dict_serpent128,dict_serpent192,dict_serpent256:
164 for i in range(0,len(d)/3):
165 msg = d['msg%i'%i].decode('hex')
166 key = d['key%i'%i].decode('hex')
167 cip = d['cip%i'%i].decode('hex')
168 cipher = python_Serpent.new(key,python_Serpent.MODE_ECB)
169 if cip <> cipher.encrypt(msg):
170 print 'ERROR! for Serpent in %i'%i
171 if msg <> cipher.decrypt(cip):
172 print 'DECRYPTION ERROR! for Serpent in %i'%i
174 # CMAC-AES128/192/256
175 print "CMAC-AES"
177 from CryptoPlus.Cipher import python_AES
179 for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256:
180 for i in range(0,len(d)/4):
181 msg = d['msg%i'%i].decode('hex')
182 key = d['key%i'%i].decode('hex')
183 if msg == '\x00':
184 msg = ''
185 mac = d['mac%i'%i].decode('hex')
186 cipher = python_AES.new(key,python_AES.MODE_CMAC)
187 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
188 print 'ERROR for %i'%i
190 # CMAC-TDES2/3
191 print "CMAC-TDES"
192 from CryptoPlus.Cipher import python_DES3
194 for d in dict_cmac_tdes2,dict_cmac_tdes3:
195 for i in range(0,len(d)/4):
196 msg = d['msg%i'%i].decode('hex')
197 if msg == '\x00':
198 msg = ''
199 key = d['key%i'%i].decode('hex')
200 mac = d['mac%i'%i].decode('hex')
201 cipher = python_DES3.new(key,python_DES3.MODE_CMAC)
202 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
203 print 'ERROR! on %i'%i
205 # XTS-AES
206 print "XTS-AES"
208 from CryptoPlus.Cipher import python_AES
210 for i in range(0,len(dict_xts_aes)/5):
211 msg = dict_xts_aes['msg%i'%i].decode('hex')
212 key = ( dict_xts_aes['key1_%i'%i].decode('hex') , dict_xts_aes['key2_%i'%i].decode('hex') )
213 cip = dict_xts_aes['cip%i'%i].decode('hex')
214 n = dict_xts_aes['n%i'%i].decode('hex')
215 cipher = python_AES.new(key,python_AES.MODE_XTS)
216 if cip <> cipher.encrypt(msg,n):
217 print 'ERROR! for XTS on %i'%i
218 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
219 decipher = python_AES.new(key,python_AES.MODE_XTS)
220 if msg <> cipher.decrypt(cip,n):
221 print 'ERROR! for XTS (decrypt) on %i'%i
222 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))
224 # TWOFISH
225 print "Twofish"
227 from CryptoPlus.Cipher import python_Twofish
228 from CryptoPlus.testvectors import dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256
229 from CryptoPlus.testvectors import dict_twofish_ecb_vk_k128, dict_twofish_ecb_vk_k192, dict_twofish_ecb_vk_k256
231 for d in dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256,dict_twofish_ecb_vk_k128:
232 for i in range(0,len(d)/3):
233 msg = d['msg%i'%i].decode('hex')
234 key = d['key%i'%i].decode('hex')
235 cip = d['cip%i'%i].decode('hex')
236 cipher = python_Twofish.new(key,python_Twofish.MODE_ECB)
237 if cip <> cipher.encrypt(msg,n):
238 print 'ERROR! for Twofish on %i'%i
239 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
240 decipher = python_Twofish.new(key,python_AES.MODE_ECB)
241 if msg <> cipher.decrypt(cip,n):
242 print 'DECRYPTION ERROR! for Twofish (decrypt) on %i'%i
243 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))