-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_encoder.py
217 lines (195 loc) · 7.06 KB
/
test_encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/python
#
#
# Copyright Muis IT 2011 - 2016
#
# This file is part of AWS Freezer
#
# AWS Freezer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AWS Freezer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AWS Freezer (see the COPYING file).
# If not, see <http://www.gnu.org/licenses/>.
import sys
sys.path.append("interfaces")
sys.path.append("debug")
sys.path.append("model")
import globals
import debug
import config
import encoder
import Crypto.Cipher
import Crypto.Random
import Crypto.Random.random
import binascii
import json
import tempfile
import os
import os.path
import key
def header():
print "Freezer Encoder Test"
def usage():
print "Usage: test_encoder"
globals.Config.printOptions()
globals.Config.setOption("iterations",globals.Config.Option("-i","--iterations","Number of iterations","1",True))
globals.Config.setOption("size",globals.Config.Option("-s","--file-size","Maximum file size to test with","1024",True))
globals.Config.setOption("enckey",globals.Config.Option("-e","--encryption-key","Encryption algorithm","AES",True))
def create_file(fsize):
(fhandle, fname) = tempfile.mkstemp(".txt","test_encoder")
txt="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
txtsz=len(txt) - 1
obj = Crypto.Random.random.StrongRandom()
for i in range(fsize):
c = txt[obj.randint(0,txtsz)]
os.write(fhandle,c)
os.close(fhandle)
return fname
def create_key(algo):
dct = key.Key()
obj = Crypto.Random.random.StrongRandom()
choice = obj.randint(1,3)
if algo == "AES":
dct.algorithm="AES"
dct.blocksize=16
if choice == 1:
dct.keysize=128
elif choice == 2:
dct.keysize=196
else:
dct.keysize=256
if algo == "Blowfish":
dct.algorithm="Blowfish"
dct.blocksize=8
if choice == 1:
dct.keysize=128
elif choice == 2:
dct.keysize=196
else:
dct.keysize=256
if algo == "CAST":
dct.algorithm="CAST"
dct.blocksize=8
if choice == 1:
dct.keysize=40
elif choice == 2:
dct.keysize=80
else:
dct.keysize=128
if algo == "RC5":
dct.algorithm="RC5"
dct.blocksize=8
if choice == 1:
dct.keysize=128
elif choice == 2:
dct.keysize=196
else:
dct.keysize=512
if algo == "DES":
dct.algorithm="DES"
dct.blocksize=8
dct.keysize=64
if algo == "DES3":
dct.algorithm="DES3"
dct.blocksize=8
dct.keysize=128
if algo == "IDEA":
dct.algorithm="IDEA"
dct.blocksize=8
dct.keysize=128
if algo == "none":
dct.algorithm="none"
dct.keysize=8
dct.keystring= binascii.hexlify(Crypto.Random.get_random_bytes(int(dct.keysize / 8)))
if dct.algorithm == "none":
dct=None
return dct
if __name__ == "__main__":
files=globals.Config.readArguments(sys.argv[1:],header,usage)
globals.Reporter.message("Started Test")
algo = globals.Config.getValue("enckey")
num = int(globals.Config.getValue('iterations'))
globals.Reporter.message("running " + str(num) + " iterations","main")
rnd = Crypto.Random.random.StrongRandom()
maxfsize = int(globals.Config.getValue("size"))
for i in range(num):
keyvals = create_key(algo)
filesize = rnd.randint(maxfsize / 10,maxfsize)
globals.Reporter.message("creating random file of " + str(filesize) + " size","main")
fname = create_file(filesize)
globals.Reporter.message("file is called '" +str(fname)+"'","main")
enc = encoder.Encoder(keyvals,fname)
if hasattr(enc,'iv'):
globals.Reporter.message("IV is " + binascii.hexlify(enc.iv),"main")
tsize = enc.total_size()
globals.Reporter.message("total encoded size is " + str(tsize),"main")
chunksize=enc.chunksize(Crypto.Random.random.randint(2,35))
globals.Reporter.message("reading chunks of size "+ str(chunksize),"main")
offset = 0
allchunks=[]
while offset < tsize:
csize = offset+chunksize
if csize > tsize:
csize=tsize
csize = csize - offset
chunk = enc.read(offset, csize)
offset += chunksize
allchunks+=chunk
enc.close_file()
globals.Reporter.message("displaying encoded file of size "+ str(len(allchunks)),"main")
tsize = len(allchunks)
pos=0
while pos < tsize:
endpos = pos+16
if endpos > tsize:
endpos = tsize
display_chunk="".join(allchunks[pos:endpos])
#print "%04d" % (pos) + ": " + binascii.hexlify(display_chunk)
pos = endpos
globals.Reporter.message("writing chunks to decoder and file " + fname+ ".out","main")
decoder= encoder.Decoder(keyvals,fname + ".out")
offset = 0
echunks=chunksize
chunksize = rnd.randint(2,1024)
while offset < tsize:
globals.Reporter.message("decoding at "+str(offset) + "/" + str(tsize),"main")
endpos = offset+chunksize
if endpos > tsize:
endpos=tsize
prtchunk = "".join(allchunks[offset:endpos])
decoder.write(offset, prtchunk, endpos - offset)
offset=endpos
decoder.close_file()
globals.Reporter.message("end of decoding","main")
# now check both files
if not os.path.exists(fname):
print "%8d"%i + ": ERROR: initial file does not exist"
elif not os.path.exists(fname + ".out"):
print "%8d"%i + ": ERROR: output file does not exist"
else:
sz1 = os.path.getsize(fname)
sz2 = os.path.getsize(fname+".out")
if sz1 != sz2:
print "%8d"%i + ": ERROR: file size differences: " + str(sz1) + " vs " + str(sz2)
else:
fl = open(fname,"rb")
buf1 = fl.read(sz1)
fl.close()
fl = open(fname+".out","rb")
buf2 = fl.read(sz1)
fl.close()
if buf1 != buf2:
print "%8d"%i + ": ERROR: file content differences"
else:
if keyvals == None:
print "%8d"%i+": OKAY " + "none/" + str(echunks) + "/" + str(chunksize) + "/" + str(sz1) + "/" + str(algo)
else:
print "%8d"%i+": OKAY " + str(keyvals.keysize) + "/" + str(echunks) + "/" + str(chunksize) + "/" + str(sz1) + "/" + str(algo)