From 0231c7d145929b6ad9d0f08abb532d6dca82135a Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Thu, 16 Nov 2023 18:34:58 +0000 Subject: [PATCH 01/13] Introduce function to return library/core directory Add crypto_core_directory in build_tree.py so that the libary/core directory can be returned based on what repository we are in. Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index a657a5138..4a42d9a2b 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -21,6 +21,14 @@ def looks_like_mbedtls_root(path: str) -> bool: def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) +def crypto_core_directory() -> str: + if looks_like_tf_psa_crypto_root(os.path.curdir): + return "core" + elif looks_like_mbedtls_root(os.path.curdir): + return "library" + else: + raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') + def check_repo_path(): """ Check that the current working directory is the project root, and throw From 372e532d3e46dabeb61c776f0add8f3e2f1a7ab8 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Wed, 22 Nov 2023 17:00:34 +0000 Subject: [PATCH 02/13] Improve implementation of crypto_core_directory Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index 4a42d9a2b..9c0b5ba96 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -7,6 +7,7 @@ import os import inspect +from typing import Optional def looks_like_tf_psa_crypto_root(path: str) -> bool: """Whether the given directory looks like the root of the PSA Crypto source tree.""" @@ -21,10 +22,12 @@ def looks_like_mbedtls_root(path: str) -> bool: def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) -def crypto_core_directory() -> str: - if looks_like_tf_psa_crypto_root(os.path.curdir): +def crypto_core_directory(root: Optional[str] = None) -> str: + if root is None: + root = guess_mbedtls_root() + if looks_like_tf_psa_crypto_root(root): return "core" - elif looks_like_mbedtls_root(os.path.curdir): + elif looks_like_mbedtls_root(root): return "library" else: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') From d9903421d8ee4c751d9f944b327faebc606e3aaa Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Wed, 22 Nov 2023 17:18:22 +0000 Subject: [PATCH 03/13] Rename guess_mbedtls_root to guess_project_root Rename for consistency. Also, replace all calls to this function with correct name. Signed-off-by: Thomas Daubney --- scripts/audit-validity-dates.py | 2 +- scripts/framework_dev/build_tree.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/audit-validity-dates.py b/scripts/audit-validity-dates.py index 96b705a28..ab09b4a1e 100755 --- a/scripts/audit-validity-dates.py +++ b/scripts/audit-validity-dates.py @@ -265,7 +265,7 @@ class Auditor: @staticmethod def find_test_dir(): """Get the relative path for the Mbed TLS test directory.""" - return os.path.relpath(build_tree.guess_mbedtls_root() + '/tests') + return os.path.relpath(build_tree.guess_project_root() + '/tests') class TestDataAuditor(Auditor): diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index 9c0b5ba96..da455a7f2 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -24,7 +24,7 @@ def looks_like_root(path: str) -> bool: def crypto_core_directory(root: Optional[str] = None) -> str: if root is None: - root = guess_mbedtls_root() + root = guess_project_root() if looks_like_tf_psa_crypto_root(root): return "core" elif looks_like_mbedtls_root(root): @@ -55,10 +55,10 @@ def chdir_to_root() -> None: raise Exception('Mbed TLS source tree not found') -def guess_mbedtls_root(): - """Guess mbedTLS source code directory. +def guess_project_root(): + """Guess project source code directory. - Return the first possible mbedTLS root directory + Return the first possible project root directory. """ dirs = set({}) for frame in inspect.stack(): @@ -71,4 +71,4 @@ def guess_mbedtls_root(): dirs.add(d) if looks_like_root(d): return d - raise Exception('Mbed TLS source tree not found') + raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') From acf4569625dd505bf09ba8502fb40506e87f0309 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Thu, 23 Nov 2023 10:14:12 +0000 Subject: [PATCH 04/13] Introduce project_crypto_name in build_tree.py Add new function to build_tree.py to return the crypto name for the project; either tfpsacrypto or mbedcrypto. Deploy this function where needed. Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index da455a7f2..f506c4142 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -32,6 +32,16 @@ def crypto_core_directory(root: Optional[str] = None) -> str: else: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') +def project_crypto_name(root: Optional[str] = None) -> str: + if root is None: + root = guess_project_root() + if looks_like_tf_psa_crypto_root(root): + return "tfpsacrypto" + elif looks_like_mbedtls_root(root): + return "mbedcrypto" + else: + raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') + def check_repo_path(): """ Check that the current working directory is the project root, and throw From 415486a63b76be43b3e1c2e7da05a77d615b900e Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Fri, 24 Nov 2023 10:48:44 +0000 Subject: [PATCH 05/13] Use os.path.join in crypto_core_directory Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index f506c4142..fa309d3e6 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -26,9 +26,9 @@ def crypto_core_directory(root: Optional[str] = None) -> str: if root is None: root = guess_project_root() if looks_like_tf_psa_crypto_root(root): - return "core" + return os.path.join(root, "core") elif looks_like_mbedtls_root(root): - return "library" + return os.path.join(root, "library") else: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') From ebae927a747f2959dd5df3b3bfabd855f08b4903 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Fri, 24 Nov 2023 10:54:56 +0000 Subject: [PATCH 06/13] Add documentation for new public functions Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index fa309d3e6..4e0ae1953 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -23,6 +23,7 @@ def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) def crypto_core_directory(root: Optional[str] = None) -> str: + """Return the path of the library code for either TF-PSA-Crypto or Mbed TLS.""" if root is None: root = guess_project_root() if looks_like_tf_psa_crypto_root(root): @@ -33,6 +34,7 @@ def crypto_core_directory(root: Optional[str] = None) -> str: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') def project_crypto_name(root: Optional[str] = None) -> str: + """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS.""" if root is None: root = guess_project_root() if looks_like_tf_psa_crypto_root(root): From f880d037c03d81c5d417fc033929a5d1ff606c71 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Thu, 30 Nov 2023 13:56:09 +0000 Subject: [PATCH 07/13] Rename project_crypto_name Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index 4e0ae1953..c2a370d7e 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -33,7 +33,7 @@ def crypto_core_directory(root: Optional[str] = None) -> str: else: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') -def project_crypto_name(root: Optional[str] = None) -> str: +def crypto_library_filename(root: Optional[str] = None) -> str: """Return the crypto library filename for either TF-PSA-Crypto or Mbed TLS.""" if root is None: root = guess_project_root() From 176d3a6f90698a1880d43aee7f19e2290867f6df Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Thu, 30 Nov 2023 13:59:30 +0000 Subject: [PATCH 08/13] Improve documentation of crypto_core_directory Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index c2a370d7e..ff8d57594 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -23,7 +23,10 @@ def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) def crypto_core_directory(root: Optional[str] = None) -> str: - """Return the path of the library code for either TF-PSA-Crypto or Mbed TLS.""" + """ + Return the path of the directory containing the PSA crypto core + for either TF-PSA-Crypto or Mbed TLS. + """ if root is None: root = guess_project_root() if looks_like_tf_psa_crypto_root(root): From d2eedbd27d4a509e2db13da41a2efc57334a30b1 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Thu, 30 Nov 2023 17:25:55 +0000 Subject: [PATCH 09/13] Introduce guess_mbedtls_root Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index ff8d57594..1868a0f89 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -48,8 +48,7 @@ def crypto_library_filename(root: Optional[str] = None) -> str: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') def check_repo_path(): - """ - Check that the current working directory is the project root, and throw + """Check that the current working directory is the project root, and throw an exception if not. """ if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): @@ -69,7 +68,6 @@ def chdir_to_root() -> None: return raise Exception('Mbed TLS source tree not found') - def guess_project_root(): """Guess project source code directory. @@ -87,3 +85,16 @@ def guess_project_root(): if looks_like_root(d): return d raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') + +def guess_mbedtls_root(root: Optional[str] = None) -> str: + """Guess Mbed TLS source code directory. + + Return the first possible Mbed TLS root directory. + Raise an exception if we are not in Mbed TLS. + """ + if root is None: + root = guess_project_root() + if looks_like_mbedtls_root(root): + return root + else: + raise Exception('Mbed TLS source tree not found') From 19da414938b5db0ea560ee4b458264c79241e321 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Thu, 30 Nov 2023 17:33:54 +0000 Subject: [PATCH 10/13] Introduce guess_tf_psa_crypto_root Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index 1868a0f89..86c838900 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -98,3 +98,16 @@ def guess_mbedtls_root(root: Optional[str] = None) -> str: return root else: raise Exception('Mbed TLS source tree not found') + +def guess_tf_psa_crypto_root(root: Optional[str] = None) -> str: + """Guess TF-PSA-Crypto source code directory. + + Return the first possible TF-PSA-Crypto root directory. + Raise an exception if we are not in TF-PSA-Crypto. + """ + if root is None: + root = guess_project_root() + if looks_like_tf_psa_crypto_root(root): + return root + else: + raise Exception('TF-PSA-Crypto source tree not found') From a62109a82213cf50ff4e15e21ccfbdb5dcbbee68 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Fri, 1 Dec 2023 09:52:35 +0000 Subject: [PATCH 11/13] Remove trailing whitespace Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index 86c838900..14790fc9c 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -24,7 +24,7 @@ def looks_like_root(path: str) -> bool: def crypto_core_directory(root: Optional[str] = None) -> str: """ - Return the path of the directory containing the PSA crypto core + Return the path of the directory containing the PSA crypto core for either TF-PSA-Crypto or Mbed TLS. """ if root is None: From f8d03139d41bf41178559dcb63ed057ea35ea109 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Fri, 1 Dec 2023 17:18:38 +0000 Subject: [PATCH 12/13] Modify crypto_core_directory to also return a relative path Signed-off-by: Thomas Daubney --- scripts/framework_dev/build_tree.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/framework_dev/build_tree.py b/scripts/framework_dev/build_tree.py index 14790fc9c..ec67e4cdf 100644 --- a/scripts/framework_dev/build_tree.py +++ b/scripts/framework_dev/build_tree.py @@ -22,16 +22,23 @@ def looks_like_mbedtls_root(path: str) -> bool: def looks_like_root(path: str) -> bool: return looks_like_tf_psa_crypto_root(path) or looks_like_mbedtls_root(path) -def crypto_core_directory(root: Optional[str] = None) -> str: +def crypto_core_directory(root: Optional[str] = None, relative: Optional[bool] = False) -> str: """ Return the path of the directory containing the PSA crypto core for either TF-PSA-Crypto or Mbed TLS. + + Returns either the full path or relative path depending on the + "relative" boolean argument. """ if root is None: root = guess_project_root() if looks_like_tf_psa_crypto_root(root): + if relative: + return "core" return os.path.join(root, "core") elif looks_like_mbedtls_root(root): + if relative: + return "library" return os.path.join(root, "library") else: raise Exception('Neither Mbed TLS nor TF-PSA-Crypto source tree found') From ebd330453ea1e4471a38cb5c3d2275913d1a48c6 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Fri, 1 Dec 2023 18:27:25 +0000 Subject: [PATCH 13/13] Use guess_mbedtls_root in Mbed-TLS-only script Signed-off-by: Thomas Daubney --- scripts/audit-validity-dates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/audit-validity-dates.py b/scripts/audit-validity-dates.py index ab09b4a1e..96b705a28 100755 --- a/scripts/audit-validity-dates.py +++ b/scripts/audit-validity-dates.py @@ -265,7 +265,7 @@ class Auditor: @staticmethod def find_test_dir(): """Get the relative path for the Mbed TLS test directory.""" - return os.path.relpath(build_tree.guess_project_root() + '/tests') + return os.path.relpath(build_tree.guess_mbedtls_root() + '/tests') class TestDataAuditor(Auditor):