Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt SHA256 dependency in generate_pkcs7_tests.py #46

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scripts/generate_pkcs7_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import sys
from os.path import exists

from mbedtls_framework import test_case

PKCS7_TEST_FILE = "../suites/test_suite_pkcs7.data"

class Test: # pylint: disable=too-few-public-methods
Expand All @@ -37,7 +39,11 @@ class TestData:
Take in test_suite_pkcs7.data file.
Allow for new tests to be added.
"""
mandatory_dep = "MBEDTLS_MD_CAN_SHA256"
# In Mbed TLS 3.6, PKCS7 code belongs to the USE_PSA_CRYPTO domain (see
# transition-guards.md for more information) thus use
# test_case.hash_dependency_symbol() to get the proper dependency symbol
# for SHA256.
mandatory_dep = test_case.hash_dependency_symbol("PSA_ALG_SHA_256")
test_name = "PKCS7 Parse Failure Invalid ASN1"
test_function = "pkcs7_asn1_fail:"
def __init__(self, file_name):
Expand Down
32 changes: 32 additions & 0 deletions scripts/mbedtls_framework/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import sys
from typing import Iterable, List, Optional

from . import build_tree
from . import psa_information
from . import typing_util

def hex_string(data: bytes) -> str:
Expand Down Expand Up @@ -89,3 +91,33 @@ def write_data_file(filename: str,
tc.write(out)
out.write('\n# End of automatically generated file.\n')
os.replace(tempfile, filename)

def hash_dependency_symbol(psa_hash_alg: str) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function name should include something like compat36. Also, the description should lead with the fact that it's about 3.6 compatibility: for us it's obvious due the context of the PR, but otherwise "legacy or USE_PSA_CRYPTO domain" in the first sentence will make little sense to future readers as those only exist in 3.6.

"""Get the dependency symbol for an hash algorithm when the algorithm
support is needed by legacy or USE_PSA_CRYPTO domain code.

The function should be used only for dependencies of legacy or
USE_PSA_CRYPTO domain code. Otherwise, just use the PSA_WANT_ prefixed
symbols. For more information about the legacy and USE_PSA_CRYPTO domain,
as well as MBEDTLS_MD_CAN_ prefixed symbols, see transition-guards.md.

psa_hash_alg PSA macro of an hash algorithm (e.g. PSA_ALG_SHA_256)

If invoked in the context of Mbed TLS 4.x or TF-PSA-Crypto, the function
just returns the PSA_WANT_ prefixed symbol that corresponds to
psa_hash_alg.

If invoked in the context of Mbed TLS 3.6, the function returns the
MBEDTLS_MD_CAN_ prefixed symbol that corresponds to psa_hash_alg.

"""
if not psa_hash_alg.startswith('PSA_ALG_') or \
psa_hash_alg[8:] not in ['MD5', 'RIPEMD160', 'SHA_1', 'SHA_224',
'SHA_256', 'SHA_384', 'SHA_512', 'SHA3_224',
'SHA3_256', 'SHA3_384', 'SHA3_512']:
raise ValueError('Unable to determine dependency symbol for ' + psa_hash_alg)

if build_tree.is_mbedtls_3_6():
return "MBEDTLS_MD_CAN_" + psa_hash_alg[8:].replace("_", "")
Copy link
Contributor

@mpg mpg Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite correct: for SHA3 we want to keep the underscore. I think using a dict would be easier and more readable.

else:
return psa_information.psa_want_symbol(psa_hash_alg)