diff options
Diffstat (limited to '')
-rw-r--r-- | test/cavp.py | 104 | ||||
-rw-r--r-- | test/file.py | 73 | ||||
-rw-r--r-- | test/nist-sp-800-38a.py | 80 | ||||
-rw-r--r-- | test/toolkit.py | 4 |
4 files changed, 156 insertions, 105 deletions
diff --git a/test/cavp.py b/test/cavp.py index 36ab6f7..096d18b 100644 --- a/test/cavp.py +++ b/test/cavp.py @@ -2,7 +2,9 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. +import argparse from collections import OrderedDict +from collections.abc import MutableSequence import configparser from datetime import datetime from enum import Enum @@ -16,14 +18,16 @@ from toolkit import * class _MultiOrderedDict(OrderedDict): def __setitem__(self, key, value): - if isinstance(value, list) and key in self: + if isinstance(value, MutableSequence) and key in self: self[key].extend(value) else: super(OrderedDict, self).__setitem__(key, value) def verify_test_output(actual, expected): if len(actual) != len(expected): - logging.error('Unexpected output length {0} (expected {1})'.format(len(actual), len(expected))) + logging.error('Unexpected output length!') + logging.error('\tExpected: %d', len(expected)) + logging.error('\tActual: %d', len(actual)) return False if actual != expected: logging.error('Expected output:\n' + '\n'.join(expected)) @@ -117,7 +121,7 @@ class TestFile: return TestExitCode.ERROR def _parse_path(self): - logging.info('Trying to parse test file path \'{0}\'...'.format(self._path)) + logging.info('Trying to parse test file path \'%s\'...', self._path) stub = self._strip_extension(os.path.basename(self._path)) if not stub: return stub = self._strip_algorithm(stub) @@ -133,19 +137,19 @@ class TestFile: def _strip_extension(self, path): stub, ext = os.path.splitext(path) if ext != self._RECOGNIZED_EXT: - logging.warn('Unknown test vectors file extension \'{0}\'!'.format(self._path)) + logging.warning('Unknown test vectors file extension \'%s\'!', self._path) return None return stub def _strip_algorithm(self, stub): key_size = stub[-3:] - maybe_algorithm = 'aes{0}'.format(key_size) + maybe_algorithm = 'aes{}'.format(key_size) self._algorithm = Algorithm.try_parse(maybe_algorithm) if self._algorithm is not None: - logging.info('\tAlgorithm: {0}'.format(self._algorithm)) + logging.info('\tAlgorithm: %s', self._algorithm) return stub[0:-3] else: - logging.warn('Unknown or unsupported algorithm: ' + self._path) + logging.warning('Unknown or unsupported algorithm: ' + self._path) return None _RECOGNIZED_METHODS = ('GFSbox', 'KeySbox', 'VarKey', 'VarTxt') @@ -153,23 +157,19 @@ class TestFile: def _strip_method(self, stub): for method in self._RECOGNIZED_METHODS: if stub.endswith(method): - logging.info('\tMethod: {0}'.format(method)) + logging.info('\tMethod: %s', method) return stub[0:len(stub) - len(method)] - logging.warn('Unknown or unsupported method: ' + self._path) + logging.warning('Unknown or unsupported method: ' + self._path) def _strip_mode(self, stub): self._mode = Mode.try_parse(stub) if self._mode is not None: - logging.info('\tMode: {0}'.format(self._mode)) + logging.info('\tMode: %s', self._mode) return self._mode else: - logging.warn('Unknown or unsupported mode: ' + self._path) + logging.warning('Unknown or unsupported mode: ' + self._path) return None -def _build_default_log_path(): - return datetime.now().strftime('{}_%Y-%m-%d_%H-%M-%S.log').format( - os.path.splitext(os.path.basename(__file__))[0]) - class TestArchive(zipfile.ZipFile): def __init__(self, path): super().__init__(path) @@ -179,40 +179,60 @@ class TestArchive(zipfile.ZipFile): for p in self.namelist(): yield TestFile(self.extract(p, tmp_dir)) -if __name__ == '__main__': - import argparse - parser = argparse.ArgumentParser() - parser.add_argument('--path', '-p', nargs='*', - help='set path to block encryption utilities') - parser.add_argument('--sde', '-e', action='store_true', - help='use Intel SDE to run *.exe files') - parser.add_argument('--use-boxes', '-b', action='store_true', - help='use the "boxes" interface') - parser.add_argument('--archive', '-a', default='KAT_AES.zip', - help='set path of the archive with the test vectors') - parser.add_argument('--log', '-l', default=_build_default_log_path(), - help='set log file path') - args = parser.parse_args() +def _build_default_log_path(): + return datetime.now().strftime('{}_%Y-%m-%d_%H-%M-%S.log').format( + os.path.splitext(os.path.basename(__file__))[0]) - logging.basicConfig(filename=args.log, - format='%(asctime)s | %(module)s | %(levelname)s | %(message)s', - level=logging.DEBUG) +def run_tests(archive_path, tools_path=(), use_sde=False, use_boxes=False, log_path=None): + if log_path is None: + log_path = _build_default_log_path() - tools = Tools(args.path, use_sde=args.sde) - archive = TestArchive(args.archive) + logging.basicConfig( + filename=log_path, + format='%(asctime)s | %(module)s | %(levelname)s | %(message)s', + level=logging.DEBUG) + + tools = Tools(tools_path, use_sde=use_sde) + archive = TestArchive(archive_path) exit_codes = [] for test_file in archive.enum_test_files(): - exit_codes.append(test_file.run_encryption_tests(tools, args.use_boxes)) - exit_codes.append(test_file.run_decryption_tests(tools, args.use_boxes)) + exit_codes.append(test_file.run_encryption_tests(tools, use_boxes)) + exit_codes.append(test_file.run_decryption_tests(tools, use_boxes)) logging.info('Test exit codes:') - logging.info('\tSkipped: {}'.format(exit_codes.count(TestExitCode.SKIPPED))) - logging.info('\tError(s): {}'.format(exit_codes.count(TestExitCode.ERROR))) - logging.info('\tSucceeded: {}'.format(exit_codes.count(TestExitCode.SUCCESS))) - logging.info('\tFailed: {}'.format(exit_codes.count(TestExitCode.FAILURE))) + logging.info('\tSkipped: %d', exit_codes.count(TestExitCode.SKIPPED)) + logging.info('\tError(s): %d', exit_codes.count(TestExitCode.ERROR)) + logging.info('\tSucceeded: %d', exit_codes.count(TestExitCode.SUCCESS)) + logging.info('\tFailed: %d', exit_codes.count(TestExitCode.FAILURE)) + if (exit_codes.count(TestExitCode.ERROR) == 0 and exit_codes.count(TestExitCode.FAILURE) == 0): - sys.exit() + return 0 else: - sys.exit(1) + return 1 + +def _parse_args(args=sys.argv): + parser = argparse.ArgumentParser() + + parser.add_argument('--path', '-p', dest='tools_path', metavar='PATH', + nargs='*', + help='set block encryption utilities directory path') + parser.add_argument('--sde', '-e', action='store_true', dest='use_sde', + help='use Intel SDE to run the utilities') + parser.add_argument('--boxes', '-b', action='store_true', dest='use_boxes', + help='use the "boxes" interface') + parser.add_argument('--archive', '-a', dest='archive_path', metavar='PATH', + default='KAT_AES.zip', + help='set test vectors archive file path') + parser.add_argument('--log', '-l', dest='log_path', metavar='PATH', + help='set log file path') + + return parser.parse_args(args[1:]) + +def main(args=sys.argv): + args = _parse_args(args) + return run_tests(**vars(args)) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/test/file.py b/test/file.py index 13355a0..3c5f7e6 100644 --- a/test/file.py +++ b/test/file.py @@ -2,6 +2,7 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. +import argparse from contextlib import contextmanager from datetime import datetime from enum import Enum @@ -81,7 +82,7 @@ def run_encryption_test(tools, algorithm, mode, key, plaintext_path, tools.run_encrypt_file(algorithm, mode, key, plaintext_path, tmp_path, iv) if force: - logging.warn('Overwriting expected ciphertext file') + logging.warning('Overwriting expected ciphertext file') shutil.copy(tmp_path, ciphertext_path) return TestExitCode.SKIPPED if filecmp.cmp(ciphertext_path, tmp_path): @@ -125,14 +126,14 @@ def enum_tests(suite_dir): algorithm = os.path.basename(algorithm_dir) maybe_algorithm = Algorithm.try_parse(algorithm) if maybe_algorithm is None: - logging.warn('Unknown or unsupported algorithm: ' + algorithm) + logging.warning('Unknown or unsupported algorithm: ' + algorithm) continue algorithm = maybe_algorithm for mode_dir in _list_dirs(algorithm_dir): mode = os.path.basename(mode_dir) maybe_mode = Mode.try_parse(mode) if maybe_mode is None: - logging.warn('Unknown or unsupported mode: ' + mode) + logging.warning('Unknown or unsupported mode: ' + mode) continue mode = maybe_mode for key_path in _list_keys(mode_dir): @@ -152,39 +153,53 @@ def _build_default_log_path(): return datetime.now().strftime('{}_%Y-%m-%d_%H-%M-%S.log').format( os.path.splitext(os.path.basename(__file__))[0]) -if __name__ == '__main__': - import argparse - parser = argparse.ArgumentParser() - parser.add_argument('--path', '-p', nargs='*', - help='set path to file encryption utilities') - parser.add_argument('--sde', '-e', action='store_true', - help='use Intel SDE to run *.exe files') - parser.add_argument('--log', '-l', default=_build_default_log_path(), - help='set log file path') - parser.add_argument('--force', '-f', action='store_true', - help='overwrite ciphertext files') - parser.add_argument('--suite', '-s', default='file', - help='set test suite directory path') - args = parser.parse_args() +def run_tests(suite_path, tools_path=(), log_path=None, use_sde=False, force=False): + if log_path is None: + log_path = _build_default_log_path() - logging.basicConfig(filename=args.log, - format='%(asctime)s | %(module)s | %(levelname)s | %(message)s', - level=logging.DEBUG) + logging.basicConfig( + filename=log_path, + format='%(asctime)s | %(module)s | %(levelname)s | %(message)s', + level=logging.DEBUG) - tools = Tools(args.path, use_sde=args.sde) + tools = Tools(tools_path, use_sde=use_sde) exit_codes = [] - for test in enum_tests(args.suite): - exit_codes.append(run_encryption_test(tools, *test, args.force)) + for test in enum_tests(suite_path): + exit_codes.append(run_encryption_test(tools, *test, force)) exit_codes.append(run_decryption_test(tools, *test)) logging.info('Test exit codes:') - logging.info('\tSkipped: {}'.format(exit_codes.count(TestExitCode.SKIPPED))) - logging.info('\tError(s): {}'.format(exit_codes.count(TestExitCode.ERROR))) - logging.info('\tSucceeded: {}'.format(exit_codes.count(TestExitCode.SUCCESS))) - logging.info('\tFailed: {}'.format(exit_codes.count(TestExitCode.FAILURE))) + logging.info('\tSkipped: %d', exit_codes.count(TestExitCode.SKIPPED)) + logging.info('\tError(s): %d', exit_codes.count(TestExitCode.ERROR)) + logging.info('\tSucceeded: %d', exit_codes.count(TestExitCode.SUCCESS)) + logging.info('\tFailed: %d', exit_codes.count(TestExitCode.FAILURE)) + if (exit_codes.count(TestExitCode.ERROR) == 0 and exit_codes.count(TestExitCode.FAILURE) == 0): - sys.exit() + return 0 else: - sys.exit(1) + return 1 + +def _parse_args(args=sys.argv): + parser = argparse.ArgumentParser() + + parser.add_argument('--path', '-p', dest='tools_path', metavar='PATH', + nargs='*', + help='set file encryption utilities directory path') + parser.add_argument('--sde', '-e', dest='use_sde', action='store_true', + help='use Intel SDE to run the utilities') + parser.add_argument('--log', '-l', dest='log_path', metavar='PATH', + help='set log file path') + parser.add_argument('--force', '-f', action='store_true', + help='overwrite ciphertext files') + parser.add_argument('--suite', '-s', dest='suite_path', default='file/', + help='set test suite directory path') + + return parser.parse_args(args[1:]) + +def main(args=sys.argv): + return run_tests(**vars(_parse_args(args))) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/test/nist-sp-800-38a.py b/test/nist-sp-800-38a.py index 5f4d15d..c3bbe13 100644 --- a/test/nist-sp-800-38a.py +++ b/test/nist-sp-800-38a.py @@ -2,6 +2,7 @@ # This file is licensed under the terms of the MIT License. # See LICENSE.txt for details. +import argparse from datetime import datetime from enum import Enum import logging @@ -137,10 +138,10 @@ _TEST_CIPHERTEXTS = { } } -def get_test_plaintexts(algorithm=None, mode=None): +def get_test_plaintexts(*_): return _TEST_PLAINTEXTS -def get_test_key(algorithm, mode=None): +def get_test_key(algorithm, *_): return _TEST_KEYS[algorithm] def get_test_iv(algorithm, mode): @@ -158,7 +159,9 @@ def get_tested_algorithms_and_modes(): def verify_test_output(actual, expected): if len(actual) != len(expected): - logging.error('Unexpected output length {0} (expected {1})'.format(len(actual), len(expected))) + logging.error('Unexpected output length!') + logging.error('\tExpected: %d', len(expected)) + logging.error('\tActual: %d', len(actual)) return False if actual != expected: logging.error('Expected output:\n' + '\n'.join(expected)) @@ -170,8 +173,8 @@ class TestExitCode(Enum): def run_encryption_test(tools, algorithm, mode, use_boxes=False): logging.info('Running encryption test...') - logging.info('Algorithm: {}'.format(algorithm)) - logging.info('Mode: {}'.format(mode)) + logging.info('Algorithm: %s', algorithm) + logging.info('Mode: %s', mode) try: plaintexts = get_test_plaintexts(algorithm, mode) @@ -191,8 +194,8 @@ def run_encryption_test(tools, algorithm, mode, use_boxes=False): def run_decryption_test(tools, algorithm, mode, use_boxes=False): logging.info('Running decryption test...') - logging.info('Algorithm: {}'.format(algorithm)) - logging.info('Mode: {}'.format(mode)) + logging.info('Algorithm: %s', algorithm) + logging.info('Mode: %s', mode) try: ciphertexts = get_test_ciphertexts(algorithm, mode) @@ -214,37 +217,52 @@ def _build_default_log_path(): return datetime.now().strftime('{}_%Y-%m-%d_%H-%M-%S.log').format( os.path.splitext(os.path.basename(__file__))[0]) -if __name__ == '__main__': - import argparse - parser = argparse.ArgumentParser() - parser.add_argument('--path', '-p', nargs='*', - help='set path to block encryption utilities') - parser.add_argument('--sde', '-e', action='store_true', - help='use Intel SDE to run *.exe files') - parser.add_argument('--use-boxes', '-b', action='store_true', - help='use the "boxes" interface') - parser.add_argument('--log', '-l', default=_build_default_log_path(), - help='set log file path') - args = parser.parse_args() +def run_tests(tools_path=(), use_sde=False, use_boxes=False, log_path=None): + if log_path is None: + log_path = _build_default_log_path() - tools = Tools(args.path, use_sde=args.sde) + logging.basicConfig( + filename=log_path, + format='%(asctime)s | %(module)s | %(levelname)s | %(message)s', + level=logging.DEBUG) - logging.basicConfig(filename=args.log, - format='%(asctime)s | %(module)s | %(levelname)s | %(message)s', - level=logging.DEBUG) + tools = Tools(tools_path, use_sde=use_sde) exit_codes = [] for algorithm, mode in get_tested_algorithms_and_modes(): - exit_codes.append(run_encryption_test(tools, algorithm, mode, use_boxes=args.use_boxes)) - exit_codes.append(run_decryption_test(tools, algorithm, mode, use_boxes=args.use_boxes)) + exit_codes.append(run_encryption_test(tools, algorithm, mode, use_boxes=use_boxes)) + exit_codes.append(run_decryption_test(tools, algorithm, mode, use_boxes=use_boxes)) logging.info('Test exit codes:') - logging.info('\tSkipped: {}'.format(exit_codes.count(TestExitCode.SKIPPED))) - logging.info('\tError(s): {}'.format(exit_codes.count(TestExitCode.ERROR))) - logging.info('\tSucceeded: {}'.format(exit_codes.count(TestExitCode.SUCCESS))) - logging.info('\tFailed: {}'.format(exit_codes.count(TestExitCode.FAILURE))) + logging.info('\tSkipped: %d', exit_codes.count(TestExitCode.SKIPPED)) + logging.info('\tError(s): %d', exit_codes.count(TestExitCode.ERROR)) + logging.info('\tSucceeded: %d', exit_codes.count(TestExitCode.SUCCESS)) + logging.info('\tFailed: %d', exit_codes.count(TestExitCode.FAILURE)) + if (exit_codes.count(TestExitCode.ERROR) == 0 and exit_codes.count(TestExitCode.FAILURE) == 0): - sys.exit() + return 0 else: - sys.exit(1) + return 1 + +def _parse_args(args=sys.argv): + parser = argparse.ArgumentParser() + + parser.add_argument('--path', '-p', dest='tools_path', metavar='PATH', + nargs='*', + help='set block encryption utilities directory path') + parser.add_argument('--sde', '-e', action='store_true', dest='use_sde', + help='use Intel SDE to run the utilities') + parser.add_argument('--boxes', '-b', action='store_true', dest='use_boxes', + help='use the "boxes" interface') + parser.add_argument('--log', '-l', dest='log_path', metavar='PATH', + help='set log file path') + + return parser.parse_args(args[1:]) + +def main(args=sys.argv): + args = _parse_args(args) + return run_tests(**vars(args)) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/test/toolkit.py b/test/toolkit.py index e94fe29..095127d 100644 --- a/test/toolkit.py +++ b/test/toolkit.py @@ -72,7 +72,6 @@ class Tools: else: os.environ['PATH'] += os.pathsep + str(search_dirs) self._use_sde = use_sde - self._logger = logging.getLogger(__name__) _ENCRYPT_BLOCK = 'encrypt_block.exe' _DECRYPT_BLOCK = 'decrypt_block.exe' @@ -82,8 +81,7 @@ class Tools: def run(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))) + logging.info('Trying to execute: ' + subprocess.list2cmdline(cmd_list)) try: output = subprocess.check_output( cmd_list, universal_newlines=True, stderr=subprocess.STDOUT) |