Use require() in test files
[python-cryptoplus.git] / test / test.py
blob092f949cc031d2db915fb0fc2e83dc8dbb21bdd7
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
130 from CryptoPlus.Cipher import python_DES3
132 for dict in dict_tdes2,dict_tdes3:
133 for i in range(0,len(dict)/3):
134 msg = dict['msg%i'%i].decode('hex')
135 key = dict['key%i'%i].decode('hex')
136 cip = dict['cip%i'%i].decode('hex')
137 cipher = python_DES3.new(key,python_DES3.MODE_ECB)
138 if cip <> cipher.encrypt(msg):
139 print 'ERROR! for TDES2/3 in %i'%i
141 # Serpent128/192/256
142 print "Serpent"
144 from CryptoPlus.Cipher import python_Serpent
146 for dict in dict_serpent128,dict_serpent192,dict_serpent256:
147 for i in range(0,len(dict)/3):
148 msg = dict['msg%i'%i].decode('hex')
149 key = dict['key%i'%i].decode('hex')
150 cip = dict['cip%i'%i].decode('hex')
151 cipher = python_Serpent.new(key,python_Serpent.MODE_ECB)
152 if cip <> cipher.encrypt(msg):
153 print 'ERROR! for Serpent in %i'%i
155 # CMAC-AES128/192/256
156 print "CMAC-AES"
158 from CryptoPlus.Cipher import python_AES
160 for dict in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256:
161 for i in range(0,len(dict)/4):
162 msg = dict['msg%i'%i].decode('hex')
163 key = dict['key%i'%i].decode('hex')
164 if msg == '\x00':
165 msg = ''
166 mac = dict['mac%i'%i].decode('hex')
167 cipher = python_AES.new(key,python_AES.MODE_CMAC)
168 if mac <> cipher.encrypt(msg)[:dict['taglength%i'%i]]:
169 print 'ERROR for %i'%i
171 # CMAC-TDES2/3
172 print "CMAC-TDES"
173 from CryptoPlus.Cipher import python_DES3
175 for dict in dict_cmac_tdes2,dict_cmac_tdes3:
176 for i in range(0,len(dict)/4):
177 msg = dict['msg%i'%i].decode('hex')
178 if msg == '\x00':
179 msg = ''
180 key = dict['key%i'%i].decode('hex')
181 mac = dict['mac%i'%i].decode('hex')
182 cipher = python_DES3.new(key,python_DES3.MODE_CMAC)
183 if mac <> cipher.encrypt(msg)[:dict['taglength%i'%i]]:
184 print 'ERROR! on %i'%i
186 # XTS-AES
187 print "XTS-AES"
189 from CryptoPlus.Cipher import python_AES
191 for i in range(0,len(dict_xts_aes)/5):
192 msg = dict_xts_aes['msg%i'%i].decode('hex')
193 key = ( dict_xts_aes['key1_%i'%i].decode('hex') , dict_xts_aes['key2_%i'%i].decode('hex') )
194 cip = dict_xts_aes['cip%i'%i].decode('hex')
195 n = dict_xts_aes['n%i'%i].decode('hex')
196 cipher = python_AES.new(key,python_AES.MODE_XTS)
197 if cip <> cipher.encrypt(msg,n):
198 print 'ERROR! for XTS on %i'%i
199 print 'got %s \n expected %s'%(cipher.encrypt(msg,n).encode('hex'),cip.encode('hex'))