Decryption tests added where they were missing
[python-cryptoplus.git] / test / test.py
blob589ecf6cf26b76b77d463647086884ceeaac9842
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 key = dict_cfb_aes['key%i'%i].decode('hex')
109 cip = dict_cfb_aes['cip%i'%i].decode('hex')
110 cipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv)
111 decipher = python_AES.new(key,python_AES.MODE_CFB,IV=iv)
112 if cip <> cipher.encrypt(msg):
113 print 'ERROR! for CFB-AES in %i'%i
114 if msg <> decipher.decrypt(cip):
115 print 'DECRYPTION ERROR! for CFB-AES in %i'%i
117 # DES,TDES2/3
118 print "DES TDES2/3"
120 from CryptoPlus.Cipher import python_DES
122 for i in range(0,len(dict_des)/3):
123 msg = dict_des['msg%i'%i].decode('hex')
124 key = dict_des['key%i'%i].decode('hex')
125 cip = dict_des['cip%i'%i].decode('hex')
126 cipher = python_DES.new(key,python_DES.MODE_ECB)
127 if cip <> cipher.encrypt(msg):
128 print 'ERROR! for DES in %i'%i
129 if msg <> cipher.decrypt(cip):
130 print 'DECRYPTION ERROR! for DES in %i'%i
132 from CryptoPlus.Cipher import python_DES3
134 for d in dict_tdes2,dict_tdes3:
135 for i in range(0,len(d)/3):
136 msg = d['msg%i'%i].decode('hex')
137 key = d['key%i'%i].decode('hex')
138 cip = d['cip%i'%i].decode('hex')
139 cipher = python_DES3.new(key,python_DES3.MODE_ECB)
140 if cip <> cipher.encrypt(msg):
141 print 'ERROR! for TDES2/3 in %i'%i
142 if msg <> cipher.decrypt(cip):
143 print 'DECRYPTION ERROR! for DES in %i'%i
145 # Serpent128/192/256
146 print "Serpent"
148 from CryptoPlus.Cipher import python_Serpent
150 for d in dict_serpent128,dict_serpent192,dict_serpent256:
151 for i in range(0,len(d)/3):
152 msg = d['msg%i'%i].decode('hex')
153 key = d['key%i'%i].decode('hex')
154 cip = d['cip%i'%i].decode('hex')
155 cipher = python_Serpent.new(key,python_Serpent.MODE_ECB)
156 if cip <> cipher.encrypt(msg):
157 print 'ERROR! for Serpent in %i'%i
158 if msg <> cipher.decrypt(cip):
159 print 'DECRYPTION ERROR! for Serpent in %i'%i
161 # CMAC-AES128/192/256
162 print "CMAC-AES"
164 from CryptoPlus.Cipher import python_AES
166 for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256:
167 for i in range(0,len(d)/4):
168 msg = d['msg%i'%i].decode('hex')
169 key = d['key%i'%i].decode('hex')
170 if msg == '\x00':
171 msg = ''
172 mac = d['mac%i'%i].decode('hex')
173 cipher = python_AES.new(key,python_AES.MODE_CMAC)
174 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
175 print 'ERROR for %i'%i
177 # CMAC-TDES2/3
178 print "CMAC-TDES"
179 from CryptoPlus.Cipher import python_DES3
181 for d in dict_cmac_tdes2,dict_cmac_tdes3:
182 for i in range(0,len(d)/4):
183 msg = d['msg%i'%i].decode('hex')
184 if msg == '\x00':
185 msg = ''
186 key = d['key%i'%i].decode('hex')
187 mac = d['mac%i'%i].decode('hex')
188 cipher = python_DES3.new(key,python_DES3.MODE_CMAC)
189 if mac <> cipher.encrypt(msg)[:d['taglength%i'%i]]:
190 print 'ERROR! on %i'%i
192 # XTS-AES
193 print "XTS-AES"
195 from CryptoPlus.Cipher import python_AES
197 for i in range(0,len(dict_xts_aes)/5):
198 msg = dict_xts_aes['msg%i'%i].decode('hex')
199 key = ( dict_xts_aes['key1_%i'%i].decode('hex') , dict_xts_aes['key2_%i'%i].decode('hex') )
200 cip = dict_xts_aes['cip%i'%i].decode('hex')
201 n = dict_xts_aes['n%i'%i].decode('hex')
202 cipher = python_AES.new(key,python_AES.MODE_XTS)
203 if cip <> cipher.encrypt(msg,n):
204 print 'ERROR! for XTS on %i'%i
205 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))
206 decipher = python_AES.new(key,python_AES.MODE_XTS)
207 if msg <> cipher.decrypt(cip,n):
208 print 'ERROR! for XTS (decrypt) on %i'%i
209 print 'got %s \n expected %s'%(decipher.decrypt(cip,n).encode('hex'),msg.encode('hex'))