-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes-ctr_tests.py
More file actions
272 lines (248 loc) · 13.4 KB
/
aes-ctr_tests.py
File metadata and controls
272 lines (248 loc) · 13.4 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Test-Framework for AES-CTR Software Project
import sys
import os
import time
import subprocess
import re
import secrets
from tqdm import tqdm
from icecream import ic
# Print usage
def print_usage():
print()
print('Usage:')
print(' ', sys.argv[0], '<path-to-executable-under-test> <path-to-result-file>')
print()
# Test compatibility to openssl tool
def test_openssl_compatibility(exec_path, result_file):
test_name = 'OpenSSL Compatibility Test'
print('--- Performing >>', test_name, '<< ...')
# Create test directories
openssl_tmp_dir = './tmp-openssl/'
aes_ctr_tmp_dir = './tmp-aes-ctr/'
os.makedirs(openssl_tmp_dir, exist_ok=True)
os.makedirs(aes_ctr_tmp_dir, exist_ok=True)
# Initialize test data
key_128 = '000102030405060708090a0b0c0d0e0f'
key_256 = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
iv = '00112233445566778899aabbccddeeff'
data = '0123456789abcdef'
# Initialize test files
test_file = 'test.txt'
test_enc128_file = 'test.txt.enc128'
test_dec128_file = 'test.txt.dec128'
test_enc256_file = 'test.txt.enc256'
test_dec256_file = 'test.txt.dec256'
# Perform commands with openssl tool
with open(openssl_tmp_dir + 'test.txt', 'w') as f:
f.write(data)
openssl_cmd = 'openssl enc -aes-128-ctr -K ' + key_128 + ' -iv ' + iv + ' -in ' + test_file + ' -out ' + test_enc128_file
print(openssl_cmd)
subprocess.run(openssl_cmd.split(' '), cwd=openssl_tmp_dir, capture_output=True, text=True)
openssl_cmd = 'openssl enc -d -aes-128-ctr -K ' + key_128 + ' -iv ' + iv + ' -in ' + test_enc128_file + ' -out ' + test_dec128_file
subprocess.run(openssl_cmd.split(' '), cwd=openssl_tmp_dir, capture_output=True, text=True)
openssl_cmd = 'openssl enc -aes-256-ctr -K ' + key_256 + ' -iv ' + iv + ' -in ' + test_file + ' -out ' + test_enc256_file
subprocess.run(openssl_cmd.split(' '), cwd=openssl_tmp_dir, capture_output=True, text=True)
openssl_cmd = 'openssl enc -d -aes-256-ctr -K ' + key_256 + ' -iv ' + iv + ' -in ' + test_enc256_file + ' -out ' + test_dec256_file
subprocess.run(openssl_cmd.split(' '), cwd=openssl_tmp_dir, capture_output=True, text=True)
# Perform command with executable under test and compare
with open(aes_ctr_tmp_dir + 'test.txt', 'w') as f:
f.write(data)
aes_ctr_cmd = './' + exec_path + ' -c encrypt -k ' + key_128 + ' -v ' + iv + ' -i ' + test_file + ' -o ' + test_enc128_file
foo = subprocess.run(aes_ctr_cmd.split(' '), cwd=aes_ctr_tmp_dir, capture_output=True, text=True)
print(foo.stdout)
aes_ctr_cmd = './' + exec_path + ' -c decrypt -k ' + key_128 + ' -v ' + iv + ' -i ' + test_enc128_file + ' -o ' + test_dec128_file
subprocess.run(aes_ctr_cmd.split(' '), cwd=aes_ctr_tmp_dir, capture_output=True, text=True)
aes_ctr_cmd = './' + exec_path + ' -c encrypt -k ' + key_256 + ' -v ' + iv + ' -i ' + test_file + ' -o ' + test_enc256_file
subprocess.run(aes_ctr_cmd.split(' '), cwd=aes_ctr_tmp_dir, capture_output=True, text=True)
aes_ctr_cmd = './' + exec_path + ' -c decrypt -k ' + key_256 + ' -v ' + iv + ' -i ' + test_enc256_file + ' -o ' + test_dec256_file
subprocess.run(aes_ctr_cmd.split(' '), cwd=aes_ctr_tmp_dir, capture_output=True, text=True)
# Compare files
list_of_files = [
test_enc128_file,
test_dec128_file,
test_enc256_file,
test_dec256_file,
]
for filename in list_of_files:
file1 = openssl_tmp_dir + filename
file2 = aes_ctr_tmp_dir + filename
file_cmp = subprocess.run(['cmp', '-s', file1, file2], cwd='./')
if file_cmp.returncode == 0:
result_file.write(test_name + ', ' + filename + ', OK, \n')
else:
result_file.write(test_name + ', ' + filename + ', MISMATCH, \n')
# Delete temporary files
subprocess.run(['rm', '-r', openssl_tmp_dir], cwd='./', capture_output=True, text=True)
subprocess.run(['rm', '-r', aes_ctr_tmp_dir], cwd='./', capture_output=True, text=True)
# Get file size
def get_file_size(exec_path, result_file):
test_name = 'File Size Test'
print('--- Performing >>', test_name, '<< ...')
exec_size = os.path.getsize(exec_path)
result_file.write(test_name + ', File Size, ' + str(exec_size) + ', bytes\n')
# Get section sizes
def get_section_sizes(exec_path, result_file):
test_name = 'Section Sizes Test'
print('--- Performing >>', test_name, '<< ...')
print('--- Performing >>', exec_path, '<< ...')
# size = subprocess.run(['size', '-A', exec_path], cwd='./', capture_output=True, text=True)
# size_text_sec = re.search('\.text\s+([0-9]+)', size.stdout).group(1)
# size_rodata_sec = re.search('\.rodata\s+([0-9]+)', size.stdout).group(1)
# size_data_sec = re.search('\.data\s+([0-9]+)', size.stdout).group(1)
# size_bss_sec = re.search('\.bss\s+([0-9]+)', size.stdout).group(1)
# result_file.write(test_name + ', .text, ' + size_text_sec + ', bytes\n')
# result_file.write(test_name + ', .rodata, ' + size_rodata_sec + ', bytes\n')
# result_file.write(test_name + ', .data, ' + size_data_sec + ', bytes\n')
# result_file.write(test_name + ', .bss, ' + size_bss_sec + ', bytes\n')
size = subprocess.run(['objdump', '-h', exec_path], cwd='./', capture_output=True, text=True)
sections = size.stdout.split('\n')
size_text_sec = 0
size_rodata_sec = 0
size_data_sec = 0
size_bss_sec = 0
for section in sections:
match = re.search('\.text\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)', section)
if match:
if match.group(2) == 'AX':
size_text_sec = int(match.group(3), 16)
match = re.search('\.rodata\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)', section)
if match:
if match.group(2) == 'AR':
size_rodata_sec = int(match.group(3), 16)
match = re.search('\.data\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)', section)
if match:
if match.group(2) == 'WA':
size_data_sec = int(match.group(3), 16)
match = re.search('\.bss\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)', section)
if match:
if match.group(2) == 'WA':
size_bss_sec = int(match.group(3), 16)
result_file.write(test_name + ',.text,' + str(size_text_sec) + ', bytes\n')
result_file.write(test_name + ',.rodata,' + str(size_rodata_sec) + ', bytes\n')
result_file.write(test_name + ',.data,' + str(size_data_sec) + ', bytes\n')
result_file.write(test_name + ',.bss,' + str(size_bss_sec) + ', bytes\n')
# Get global heap maximum
def get_global_heap_max(exec_path, result_file):
test_name = 'Global Heap Maximum Test'
print('--- Performing >>', test_name, '<< ...')
# Test parameters
filesizes = [1024, 1024*1024, 1024*1024*1024]
test_in_file = 'dhat-test.tmp'
test_out_file = 'dhat-test.out.tmp'
key_128 = ''.join('{:02x}'.format(x) for x in secrets.token_bytes(16))
key_256 = ''.join('{:02x}'.format(x) for x in secrets.token_bytes(32))
iv = ''.join('{:02x}'.format(x) for x in secrets.token_bytes(16))
commands = [
exec_path + ' -c encrypt -k ' + key_128 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
exec_path + ' -c decrypt -k ' + key_128 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
exec_path + ' -c encrypt -k ' + key_256 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
exec_path + ' -c decrypt -k ' + key_256 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
]
# Loop through tests with different file sizes and commands
with tqdm(total=len(filesizes)*len(commands)) as pbar:
for filesize in filesizes:
for cmd in commands:
# Initialize random test data
subprocess.run(['openssl', 'rand', '-out', test_in_file, str(filesize)])
# Call valgrind
valgrind = subprocess.run(['valgrind', '--tool=dhat', '--dhat-out-file=dhat-output.tmp'] + cmd.split(' '),
cwd='./', capture_output=True, text=True)
global_heap_max = re.sub(',', '', re.search('At.t-gmax:.(\d+|\d{1,3}(,\d{3})*)\s', valgrind.stderr).group(1))
# Write result to file
result_file.write(test_name + ', AES-CTR-' + str((len(cmd)-len(commands[0]))*4+128) + ' '
+ cmd[len(exec_path)+4:len(exec_path)+11] + ' ' + str(filesize) + ' bytes, '
+ global_heap_max + ', ' + cmd + '\n')
# Remove temporary files
subprocess.run(['rm', 'dhat-output.tmp'], cwd='./', capture_output=True, text=True)
subprocess.run(['rm', test_in_file], cwd='./', capture_output=True, text=True)
subprocess.run(['rm', test_out_file], cwd='./', capture_output=True, text=True)
pbar.update(1)
# Get exection time
def get_execution_time(exec_path, result_file):
test_name = 'Execution Time Test'
print('--- Performing >>', test_name, '<< ...')
# Get rights to use perf counters
os.system('sudo -S sh -c \'echo 0 > /proc/sys/kernel/perf_event_paranoid\'')
# Test parameters
filesizes = [1024, 1024*1024, 1024*1024*1024]
test_in_file = 'perf-test.tmp'
test_out_file = 'perf-test.out.tmp'
key_128 = ''.join('{:02x}'.format(x) for x in secrets.token_bytes(16))
key_256 = ''.join('{:02x}'.format(x) for x in secrets.token_bytes(32))
iv = ''.join('{:02x}'.format(x) for x in secrets.token_bytes(16))
commands = [
exec_path + ' -c encrypt -k ' + key_128 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
exec_path + ' -c decrypt -k ' + key_128 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
exec_path + ' -c encrypt -k ' + key_256 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
exec_path + ' -c decrypt -k ' + key_256 + ' -v ' + iv + ' -i ' + test_in_file + ' -o ' + test_out_file,
]
n_measurements = 3
# Loop through tests with different file sizes and commands
with tqdm(total=len(filesizes)*len(commands)*n_measurements) as pbar:
for filesize in filesizes:
for cmd in commands:
time_data = []
# Loop through several measurement runs
for n in range(n_measurements):
# Initialize random test data
subprocess.run(['openssl', 'rand', '-out', test_in_file, str(filesize)])
# Call perf
perf = subprocess.run(['perf', 'stat'] + cmd.split(' '), cwd='./', capture_output=True, text=True)
time_str = re.sub(',', '.', re.search('([0-9]*,[0-9]*).seconds.time', perf.stderr).group(1))
time_data.append(float(time_str))
# Remove temporary files
subprocess.run(['rm', test_in_file], cwd='./', capture_output=True, text=True)
subprocess.run(['rm', test_out_file], cwd='./', capture_output=True, text=True)
pbar.update(1)
# Write results to file
result_file.write(test_name + ', AES-CTR-' + str((len(cmd)-len(commands[0]))*4+128) + ' '
+ cmd[len(exec_path)+4:len(exec_path)+11] + ' ' + str(filesize) + ' bytes MIN, '
+ str(min(time_data)) + ', ' + cmd + '\n')
result_file.write(test_name + ', AES-CTR-' + str((len(cmd)-len(commands[0]))*4+128) + ' '
+ cmd[len(exec_path)+4:len(exec_path)+11] + ' ' + str(filesize) + ' bytes MAX, '
+ str(max(time_data)) + ', ' + cmd + '\n')
result_file.write(test_name + ', AES-CTR-' + str((len(cmd)-len(commands[0]))*4+128) + ' '
+ cmd[len(exec_path)+4:len(exec_path)+11] + ' ' + str(filesize) + ' bytes AVG, '
+ str(sum(time_data)/len(time_data)) + ', ' + cmd + '\n')
# List of tests
list_of_tests = [
test_openssl_compatibility,
# get_file_size,
# get_section_sizes,
# get_global_heap_max,
# get_execution_time,
]
# Main function
def main():
# Check command line arguments
if len(sys.argv) != 3:
print('!!! ERROR: Number of arguments does not match!')
print_usage()
sys.exit(1)
# Check executable path
exec_under_test_path = sys.argv[1]
if not (os.path.isfile(exec_under_test_path) and os.access(exec_under_test_path, os.X_OK)):
print('!!! ERROR: No executable found at given path!')
print_usage()
sys.exit(1)
# Check result file path
result_file_path = sys.argv[2]
try:
result_file = open(result_file_path, 'w')
result_file.write('Test, Result Name, Result Value, Comment\n')
except OSError:
print('!!! ERROR: Cannot open result file for writing!')
print_usage()
sys.exit(1)
# Perform tests
local_time = time.ctime(time.time())
print('### Starting Test Procedures (' + local_time + ') ...')
for test in list_of_tests:
test(exec_under_test_path, result_file)
local_time = time.ctime(time.time())
print('### Finished Testing! (' + local_time + ')')
# Close result file
result_file.close()
if __name__ == "__main__":
main()