aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/toolkit.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/toolkit.py103
1 files changed, 53 insertions, 50 deletions
diff --git a/test/toolkit.py b/test/toolkit.py
index 1a5859b..a69ce73 100644
--- a/test/toolkit.py
+++ b/test/toolkit.py
@@ -2,62 +2,65 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+import logging
import os.path
import subprocess
import sys
+AES128, AES192, AES256 = 'aes128', 'aes192', 'aes256'
+ECB, CBC, CFB, OFB, CTR = 'ecb', 'cbc', 'cfb', 'ofb', 'ctr'
+
+class EncryptionInput:
+ def __init__(self, key, plaintexts, iv=None):
+ self.key = key
+ self.plaintexts = plaintexts
+ self.iv = iv
+
+ def to_args(self):
+ args = [self.key]
+ if self.iv is not None:
+ args.append(self.iv)
+ args.extend(self.plaintexts)
+ return args
+
+class DecryptionInput:
+ def __init__(self, key, ciphertexts, iv=None):
+ self.key = key
+ self.ciphertexts = ciphertexts
+ self.iv = iv
+
+ def to_args(self):
+ args = [self.key]
+ if self.iv is not None:
+ args.append(self.iv)
+ args.extend(self.ciphertexts)
+ return args
+
class Tools:
def __init__(self, root_dir_path, use_sde=False):
self._root_dir_path = root_dir_path
self._use_sde = use_sde
+ self._logger = logging.getLogger(__name__)
+
+ def _get_tool_path(self, fn):
+ return os.path.join(self._root_dir_path, fn)
+
+ def get_encrypt_tool_path(self, algo, mode):
+ return self._get_tool_path('{0}{1}_encrypt.exe'.format(algo, mode))
+
+ def get_decrypt_tool_path(self, algo, mode):
+ return self._get_tool_path('{0}{1}_decrypt.exe'.format(algo, mode))
+
+ def run_tool(self, tool_path, args):
+ cmd_list = ['sde', '--', tool_path] if self._use_sde else [tool_path]
+ cmd_list.extend(args)
+ logging.info('Trying to execute: {0}'.format(subprocess.list2cmdline(cmd_list)))
+ output = subprocess.check_output(cmd_list, universal_newlines=True)
+ logging.info('Output:\n' + output)
+ return output.split()
+
+ def run_encrypt_tool(self, algo, mode, input):
+ return self.run_tool(self.get_encrypt_tool_path(algo, mode), input.to_args())
- def _get_tool_path(self, tool_name):
- return os.path.join(self._root_dir_path, tool_name)
-
- def _get_encrypt_tool_path(self, prefix):
- return self._get_tool_path('{0}_encrypt.exe'.format(prefix))
-
- def _get_decrypt_tool_path(self, prefix):
- return self._get_tool_path('{0}_decrypt.exe'.format(prefix))
-
- def _capture_tool_output(self, tool_path, args):
- with_sde = ['sde', '--', tool_path] if self._use_sde else [tool_path]
- return subprocess.check_output(with_sde + args, universal_newlines=True).split()
-
- def encrypt(self, prefix, key, args):
- print('Encrypting using \'{0}\'...'.format(prefix))
- print('\tKey:', key)
- return self._capture_tool_output(self._get_encrypt_tool_path(prefix), [key] + args)
-
- def decrypt(self, prefix, key, args):
- print('Decrypting using \'{0}\'...'.format(prefix))
- print('\tKey:', key)
- return self._capture_tool_output(self._get_decrypt_tool_path(prefix), [key] + args)
-
- def encrypt_with_iv(self, prefix, key, iv, args):
- print('Encrypting using \'{0}\'...'.format(prefix))
- print('\tKey:', key)
- print('\tInitialization vector:', iv)
- return self._capture_tool_output(self._get_encrypt_tool_path(prefix), [key, iv] + args)
-
- def decrypt_with_iv(self, prefix, key, iv, args):
- print('Decrypting using \'{0}\'...'.format(prefix))
- print('\tKey:', key)
- print('\tInitialization vector:', iv)
- return self._capture_tool_output(self._get_decrypt_tool_path(prefix), [key, iv] + args)
-
- def detect_mismatches(self, input, actual_output, expected_output):
- if len(actual_output) != len(expected_output):
- print('Unexpected output length!', file=sys.stderr)
- print('\tExpected length:', len(expected_output), file=sys.stderr)
- print('\tActual length:', len(actual_output), file=sys.stderr)
- return False
- no_mismatches = True
- for i in range(len(input)):
- if actual_output[i] != expected_output[i]:
- print('A mismatch detected!', file=sys.stderr)
- print('\tInput:', input[i], file=sys.stderr)
- print('\tExpected:', expected_output[i], file=sys.stderr)
- print('\tActual:', actual_output[i], file=sys.stderr)
- no_mismatches = False
- return no_mismatches
+ def run_decrypt_tool(self, algo, mode, input):
+ return self.run_tool(self.get_decrypt_tool_path(algo, mode), input.to_args())