added whole SHA2 family from http://reikon.us/sha2/
[python-cryptoplus.git] / test / test.py
blob5abd43cfe742ddaef89818e415662ac792a3542d
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
11 # PRESENT
12 print "PRESENT"
14 from CryptoPlus.testvectors import dict_present_e80_k12_tvar, dict_present_e128_k12_tvar, dict_present_e128_kvar_t12, dict_present_e80_kvar_t12
15 from CryptoPlus.Cipher import python_PRESENT
17 for i in range(1,len(dict_present_e80_k12_tvar)/3):
18 msg = dict_present_e80_k12_tvar['msg%i'%i].decode('hex')
19 key = dict_present_e80_k12_tvar['key%i'%i].decode('hex')
20 cip = dict_present_e80_k12_tvar['cip%i'%i].decode('hex')
21 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
22 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
23 if cip <> cipher.encrypt(msg):
24 print 'ERROR! for present_e80-k12_tvar in %i'%i
25 if msg <> decipher.decrypt(cip):
26 print 'DECRYPTION ERROR! for present_e80-k12_tvar in %i'%i
28 for i in range(1,len(dict_present_e128_k12_tvar)/3):
29 msg = dict_present_e128_k12_tvar['msg%i'%i].decode('hex')
30 key = dict_present_e128_k12_tvar['key%i'%i].decode('hex')
31 cip = dict_present_e128_k12_tvar['cip%i'%i].decode('hex')
32 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
33 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
34 if cip <> cipher.encrypt(msg):
35 print 'ERROR! for present_e128-k12_tvar in %i'%i
36 if msg <> decipher.decrypt(cip):
37 print 'DECRYPTION ERROR! for present_e128-k12_tvar in %i'%i
39 for i in range(1,len(dict_present_e128_kvar_t12)/3):
40 msg = dict_present_e128_kvar_t12['msg%i'%i].decode('hex')
41 key = dict_present_e128_kvar_t12['key%i'%i].decode('hex')
42 cip = dict_present_e128_kvar_t12['cip%i'%i].decode('hex')
43 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
44 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
45 if cip <> cipher.encrypt(msg):
46 print 'ERROR! for present_e128-kvar_t12 in %i'%i
47 if msg <> decipher.decrypt(cip):
48 print 'DECRYPTION ERROR! for present_e128-kvar_t12 in %i'%i
50 for i in range(1,len(dict_present_e80_kvar_t12)/3):
51 msg = dict_present_e80_kvar_t12['msg%i'%i].decode('hex')
52 key = dict_present_e80_kvar_t12['key%i'%i].decode('hex')
53 cip = dict_present_e80_kvar_t12['cip%i'%i].decode('hex')
54 cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
55 decipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB)
56 if cip <> cipher.encrypt(msg):
57 print 'ERROR! for present_e80-kvar_t12 in %i'%i
58 if msg <> decipher.decrypt(cip):
59 print 'DECRYPTION ERROR! for present_e80-kvar_t12 in %i'%i
61 # CBC, CFB, OFB and CTR with AES
62 print "AES"
64 from CryptoPlus.Cipher import python_AES
65 from CryptoPlus.Util import util
67 for i in range(1,len(dict_cbc_aes)/4+1):
68 msg = dict_cbc_aes['msg%i'%i].decode('hex')
69 iv = dict_cbc_aes['iv%i'%i].decode('hex')
70 key = dict_cbc_aes['key%i'%i].decode('hex')
71 cip = dict_cbc_aes['cip%i'%i].decode('hex')
72 cipher = python_AES.new(key,python_AES.MODE_CBC,iv)
73 decipher = python_AES.new(key,python_AES.MODE_CBC,iv)
74 if cip <> cipher.encrypt(msg):
75 print 'ERROR! for CBC-AES in %i'%i
76 if msg <> decipher.decrypt(cip):
77 print 'DECRYPTION ERROR! for CBC-AES in %i'%i
79 for i in range(1,len(dict_ctr_aes)/4+1):
80 msg = dict_ctr_aes['msg%i'%i].decode('hex')
81 ctr = dict_ctr_aes['ctr%i'%i].decode('hex')
82 key = dict_ctr_aes['key%i'%i].decode('hex')
83 cip = dict_ctr_aes['cip%i'%i].decode('hex')
84 counter = util.Counter(ctr)
85 counter2= util.Counter(ctr)
86 cipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter)
87 decipher = python_AES.new(key,python_AES.MODE_CTR,counter=counter2)
88 if cip <> cipher.encrypt(msg):
89 print 'ERROR! for CTR-AES in %i'%i
90 if msg <> decipher.decrypt(cip):
91 print 'DECRYPTION ERROR! for CTR-AES in %i'%i
93 for i in range(1,len(dict_ofb_aes)/4+1):
94 msg = dict_ofb_aes['msg%i'%i].decode('hex')
95 iv = dict_ofb_aes['iv%i'%i].decode('hex')
96 key = dict_ofb_aes['key%i'%i].decode('hex')
97 cip = dict_ofb_aes['cip%i'%i].decode('hex')
98 cipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
99 decipher = python_AES.new(key,python_AES.MODE_OFB,IV=iv)
100 if cip <> cipher.encrypt(msg):
101 print 'ERROR! for OFB-AES in %i'%i
102 if msg <> decipher.decrypt(cip):
103 print 'DECRYPTION ERROR! for OFB-AES in %i'%i
105 for i in range(1,len(dict_cfb_aes)/4+1):
106 msg = dict_cfb_aes['msg%i'%i].decode('hex')
107 iv = dict_cfb_aes['iv%i'%i].decode('hex')
108 s = dict_cfb_aes['s%i'%i]
109 key = dict_cfb_aes['key%i'%i].decode('hex')
110 cip = dict_cfb_aes['cip%i'%i].decode('hex')
111 cipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
112 decipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv,segment_size=s)
113 if cip <> cipher.encrypt(msg):
114 print 'ERROR! for CFB-AES in %i'%i
115 if msg <> decipher.decrypt(cip):
116 print 'DECRYPTION ERROR! for CFB-AES in %i'%i
118 # DES,TDES2/3
119 print "DES TDES2/3"
121 from CryptoPlus.Cipher import python_DES
123 for i in range(0,len(dict_des)/3):
124 msg = dict_des['msg%i'%i].decode('hex')
125 key = dict_des['key%i'%i].decode('hex')
126 cip = dict_des['cip%i'%i].decode('hex')
127 cipher = python_DES.new(key,python_DES.MODE_ECB)
128 if cip <> cipher.encrypt(msg):
129 print 'ERROR! for DES in %i'%i
130 if msg <> cipher.decrypt(cip):
131 print 'DECRYPTION ERROR! for DES in %i'%i
133 from CryptoPlus.Cipher import python_DES3
135 for d in dict_tdes2,dict_tdes3:
136 for i in range(0,len(d)/3):
137 msg = d['msg%i'%i].decode('hex')
138 key = d['key%i'%i].decode('hex')
139 cip = d['cip%i'%i].decode('hex')
140 cipher = python_DES3.new(key,python_DES3.MODE_ECB)
141 if cip <> cipher.encrypt(msg):
142 print 'ERROR! for TDES2/3 in %i'%i
143 if msg <> cipher.decrypt(cip):
144 print 'DECRYPTION ERROR! for DES in %i'%i
146 # Serpent128/192/256
147 print "Serpent"
149 from CryptoPlus.Cipher import python_Serpent
151 for d in dict_serpent128,dict_serpent192,dict_serpent256:
152 for i in range(0,len(d)/3):
153 msg = d['msg%i'%i].decode('hex')
154 key = d['key%i'%i].decode('hex')
155 cip = d['cip%i'%i].decode('hex')
156 cipher = python_Serpent.new(key,python_Serpent.MODE_ECB)
157 if cip <> cipher.encrypt(msg):
158 print 'ERROR! for Serpent in %i'%i
159 if msg <> cipher.decrypt(cip):
160 print 'DECRYPTION ERROR! for Serpent in %i'%i
162 # CMAC-AES128/192/256
163 print "CMAC-AES"
165 from CryptoPlus.Cipher import python_AES
167 for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256:
168 for i in range(0,len(d)/4):
169 msg = d['msg%i'%i].decode('hex')
170 key = d['key%i'%i].decode('hex')
171 if msg == '\x00':
172 msg = ''
173 mac = d['mac%i'%i].decode('hex')
174 cipher = python_AES.new(key,python_AES.MODE_CMAC)
175 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
176 print 'ERROR for %i'%i
178 # CMAC-TDES2/3
179 print "CMAC-TDES"
180 from CryptoPlus.Cipher import python_DES3
182 for d in dict_cmac_tdes2,dict_cmac_tdes3:
183 for i in range(0,len(d)/4):
184 msg = d['msg%i'%i].decode('hex')
185 if msg == '\x00':
186 msg = ''
187 key = d['key%i'%i].decode('hex')
188 mac = d['mac%i'%i].decode('hex')
189 cipher = python_DES3.new(key,python_DES3.MODE_CMAC)
190 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
191 print 'ERROR! on %i'%i
193 # XTS-AES
194 print "XTS-AES"
196 from CryptoPlus.Cipher import python_AES
198 for i in range(0,len(dict_xts_aes)/5):
199 msg = dict_xts_aes['msg%i'%i].decode('hex')
200 key = ( dict_xts_aes['key1_%i'%i].decode('hex') , dict_xts_aes['key2_%i'%i].decode('hex') )
201 cip = dict_xts_aes['cip%i'%i].decode('hex')
202 n = dict_xts_aes['n%i'%i].decode('hex')
203 cipher = python_AES.new(key,python_AES.MODE_XTS)
204 if cip <> cipher.encrypt(msg,n):
205 print 'ERROR! for XTS on %i'%i
206 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
207 decipher = python_AES.new(key,python_AES.MODE_XTS)
208 if msg <> cipher.decrypt(cip,n):
209 print 'ERROR! for XTS (decrypt) on %i'%i
210 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))
212 # TWOFISH
213 print "Twofish"
215 from CryptoPlus.Cipher import python_Twofish
216 from CryptoPlus.testvectors import dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256
217 from CryptoPlus.testvectors import dict_twofish_ecb_vk_k128, dict_twofish_ecb_vk_k192, dict_twofish_ecb_vk_k256
219 for d in dict_twofish_ecb_vt_k128, dict_twofish_ecb_vt_k192, dict_twofish_ecb_vt_k256,dict_twofish_ecb_vk_k128:
220 for i in range(0,len(d)/3):
221 msg = d['msg%i'%i].decode('hex')
222 key = d['key%i'%i].decode('hex')
223 cip = d['cip%i'%i].decode('hex')
224 cipher = python_Twofish.new(key,python_Twofish.MODE_ECB)
225 if cip <> cipher.encrypt(msg,n):
226 print 'ERROR! for Twofish on %i'%i
227 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
228 decipher = python_Twofish.new(key,python_AES.MODE_ECB)
229 if msg <> cipher.decrypt(cip,n):
230 print 'DECRYPTION ERROR! for Twofish (decrypt) on %i'%i
231 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))