crypto_data_tests.py (4833B)
1 """Generate test data for cryptographic mechanisms. 2 3 This module is a work in progress, only implementing a few cases for now. 4 """ 5 6 # Copyright The Mbed TLS Contributors 7 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 8 # 9 10 import hashlib 11 from typing import Callable, Dict, Iterator, List, Optional #pylint: disable=unused-import 12 13 from . import crypto_knowledge 14 from . import psa_information 15 from . import psa_test_case 16 from . import test_case 17 18 19 class HashPSALowLevel: 20 """Generate test cases for the PSA low-level hash interface.""" 21 22 def __init__(self, info: psa_information.Information) -> None: 23 self.info = info 24 base_algorithms = sorted(info.constructors.algorithms) 25 all_algorithms = \ 26 [crypto_knowledge.Algorithm(expr) 27 for expr in info.constructors.generate_expressions(base_algorithms)] 28 self.algorithms = \ 29 [alg 30 for alg in all_algorithms 31 if (not alg.is_wildcard and 32 alg.can_do(crypto_knowledge.AlgorithmCategory.HASH))] 33 34 # CALCULATE[alg] = function to return the hash of its argument in hex 35 # TO-DO: implement the None entries with a third-party library, because 36 # hashlib might not have everything, depending on the Python version and 37 # the underlying OpenSSL. On Ubuntu 16.04, truncated sha512 and sha3/shake 38 # are not available. On Ubuntu 22.04, md2, md4 and ripemd160 are not 39 # available. 40 CALCULATE = { 41 'PSA_ALG_MD5': lambda data: hashlib.md5(data).hexdigest(), 42 'PSA_ALG_RIPEMD160': None, #lambda data: hashlib.new('ripdemd160').hexdigest() 43 'PSA_ALG_SHA_1': lambda data: hashlib.sha1(data).hexdigest(), 44 'PSA_ALG_SHA_224': lambda data: hashlib.sha224(data).hexdigest(), 45 'PSA_ALG_SHA_256': lambda data: hashlib.sha256(data).hexdigest(), 46 'PSA_ALG_SHA_384': lambda data: hashlib.sha384(data).hexdigest(), 47 'PSA_ALG_SHA_512': lambda data: hashlib.sha512(data).hexdigest(), 48 'PSA_ALG_SHA_512_224': None, #lambda data: hashlib.new('sha512_224').hexdigest() 49 'PSA_ALG_SHA_512_256': None, #lambda data: hashlib.new('sha512_256').hexdigest() 50 'PSA_ALG_SHA3_224': None, #lambda data: hashlib.sha3_224(data).hexdigest(), 51 'PSA_ALG_SHA3_256': None, #lambda data: hashlib.sha3_256(data).hexdigest(), 52 'PSA_ALG_SHA3_384': None, #lambda data: hashlib.sha3_384(data).hexdigest(), 53 'PSA_ALG_SHA3_512': None, #lambda data: hashlib.sha3_512(data).hexdigest(), 54 'PSA_ALG_SHAKE256_512': None, #lambda data: hashlib.shake_256(data).hexdigest(64), 55 } #type: Dict[str, Optional[Callable[[bytes], str]]] 56 57 @staticmethod 58 def one_test_case(alg: crypto_knowledge.Algorithm, 59 function: str, note: str, 60 arguments: List[str]) -> test_case.TestCase: 61 """Construct one test case involving a hash.""" 62 tc = psa_test_case.TestCase(dependency_prefix='MBEDTLS_PSA_BUILTIN_') 63 tc.set_description('{}{} {}' 64 .format(function, 65 ' ' + note if note else '', 66 alg.short_expression())) 67 tc.set_function(function) 68 tc.set_arguments([alg.expression] + 69 ['"{}"'.format(arg) for arg in arguments]) 70 return tc 71 72 def test_cases_for_hash(self, 73 alg: crypto_knowledge.Algorithm 74 ) -> Iterator[test_case.TestCase]: 75 """Enumerate all test cases for one hash algorithm.""" 76 calc = self.CALCULATE[alg.expression] 77 if calc is None: 78 return # not implemented yet 79 80 short = b'abc' 81 hash_short = calc(short) 82 long = (b'Hello, world. Here are 16 unprintable bytes: [' 83 b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a' 84 b'\x80\x81\x82\x83\xfe\xff]. ' 85 b' This message was brought to you by a natural intelligence. ' 86 b' If you can read this, good luck with your debugging!') 87 hash_long = calc(long) 88 89 yield self.one_test_case(alg, 'hash_empty', '', [calc(b'')]) 90 yield self.one_test_case(alg, 'hash_valid_one_shot', '', 91 [short.hex(), hash_short]) 92 for n in [0, 1, 64, len(long) - 1, len(long)]: 93 yield self.one_test_case(alg, 'hash_valid_multipart', 94 '{} + {}'.format(n, len(long) - n), 95 [long[:n].hex(), calc(long[:n]), 96 long[n:].hex(), hash_long]) 97 98 def all_test_cases(self) -> Iterator[test_case.TestCase]: 99 """Enumerate all test cases for all hash algorithms.""" 100 for alg in self.algorithms: 101 yield from self.test_cases_for_hash(alg)