TF-PSA-Crypto#

//third_party/tf_psa_crypto provides Bazel and GN build integration for TF-PSA-Crypto, the standalone PSA Certified Crypto API (<psa/crypto.h>) implementation split out from MbedTLS 4.0. It does not bundle TF-PSA-Crypto source code for downstream consumers.

Bazel#

Pigweed exposes two label flags in @pigweed//third_party/tf_psa_crypto:

  • @pigweed//third_party/tf_psa_crypto:psa_crypto selects the <psa/crypto.h> implementation. It defaults to @pigweed//pw_build:unspecified_backend for downstream consumers. Pigweed’s own .bazelrc binds it to the dev-only @tf_psa_crypto repository declared in //bazel:dev.MODULE.bazel.

  • @pigweed//third_party/tf_psa_crypto:tf_psa_crypto_config selects the compile-time configuration header (TF_PSA_CRYPTO_CONFIG_FILE) when building TF-PSA-Crypto with @pigweed//third_party/tf_psa_crypto:tf_psa_crypto.BUILD.bazel. It defaults to :default_config, which picks :mcu_config on @platforms//os:none and :host_config on all other platforms. RTOS platforms whose OS constraint is not @platforms//os:none (for example, Zephyr) should override tf_psa_crypto_config or psa_crypto instead of relying on :host_config.

Use an existing PSA Crypto provider#

If your project already builds a PSA Crypto provider (such as a vendor SDK or MbedTLS with MBEDTLS_PSA_CRYPTO_C), reuse that target. Point psa_crypto at it in your platform or .bazelrc:

common --@pigweed//third_party/tf_psa_crypto:psa_crypto=//crypto:psa_crypto

Building TF-PSA-Crypto from a release tarball#

Declare an http_archive for a TF-PSA-Crypto 1.1.2+ release tarball in MODULE.bazel using Pigweed’s build file template:

http_archive = use_repo_rule(
    "@bazel_tools//tools/build_defs/repo:http.bzl",
    "http_archive",
)

http_archive(
    name = "tf_psa_crypto",
    build_file = "@pigweed//third_party/tf_psa_crypto:tf_psa_crypto.BUILD.bazel",
    sha256 = "799764c15cebd0d8cc161c71f9601009b2992f471828739fb4d72573b87f8298",
    strip_prefix = "tf-psa-crypto-1.1.2",
    urls = ["https://github.com/Mbed-TLS/TF-PSA-Crypto/releases/download/tf-psa-crypto-1.1.2/tf-psa-crypto-1.1.2.tar.bz2"],
)

Then bind psa_crypto to that repository in your platform or .bazelrc:

common --@pigweed//third_party/tf_psa_crypto:psa_crypto=@tf_psa_crypto

Supply a custom configuration header#

Define a cc_library that exports your configuration header and sets TF_PSA_CRYPTO_CONFIG_FILE:

cc_library(
    name = "tf_psa_crypto_config",
    hdrs = ["my_config/my_tf_psa_crypto_config.h"],
    defines = [
        "TF_PSA_CRYPTO_CONFIG_FILE='\"my_tf_psa_crypto_config.h\"'",
    ],
    strip_include_prefix = "my_config",
    tags = ["noclangtidy"],
)

Custom host configs that enable MBEDTLS_THREADING_PTHREAD on Linux or MBEDTLS_PSA_BUILTIN_GET_ENTROPY on Windows must add matching linkopts (-pthread on Linux; -lbcrypt or -DEFAULTLIB:bcrypt.lib on Windows).

Then override tf_psa_crypto_config in your platform or .bazelrc:

common --@pigweed//third_party/tf_psa_crypto:tf_psa_crypto_config=//crypto:tf_psa_crypto_config

GN#

Configure the GN build args declared in $pw_external_tf_psa_crypto/tf_psa_crypto.gni:

  • dir_pw_third_party_tf_psa_crypto: Path to a TF-PSA-Crypto 1.1.2 or newer release tarball (or a checkout with generated files such as core/psa_crypto_driver_wrappers_no_static.c pre-built). A raw git checkout without generated files will not build.

  • pw_third_party_tf_psa_crypto_ALIAS: Label of an existing PSA Crypto build target to alias at $pw_external_tf_psa_crypto. Use this instead of dir_pw_third_party_tf_psa_crypto to avoid linking two copies of PSA Crypto.

  • pw_third_party_tf_psa_crypto_CONFIG_HEADER: Path to the TF_PSA_CRYPTO_CONFIG_FILE header when building from dir_pw_third_party_tf_psa_crypto. Defaults to configs/config_pigweed_host.h on host toolchains and configs/config_pigweed.h on target toolchains.

Example gn gen invocation:

gn gen out --args='
    dir_pw_third_party_tf_psa_crypto="/path/to/tf-psa-crypto-1.1.2"
'

After configuration, $pw_external_tf_psa_crypto provides the pw_source_set target $pw_external_tf_psa_crypto:tf_psa_crypto.