Commit Graph

12254 Commits

Author SHA1 Message Date
Gilles Peskine 1fba034eb7 Merge remote-tracking branch 'main' into mldsa-pqcp-driver-framework 2026-04-07 14:28:56 +02:00
Gilles Peskine a0e5850d61 Fix the Mbed TLS build when TF-PSA-Crypto only has a little MLDSA
When building Mbed TLS with test drivers, "psa_crypto_mldsa.h" is not on the
include path. Rather than get it on, which seems complicated and is not
desirable in the long term, arrange to do without this header. We just need
to define the macro PSA_ALG_IS_ML_DSA, and its behavior is defined by the
PSA Crypto API specification so pretty much set in stone.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-31 15:19:41 +02:00
Gilles Peskine c8672388d8 Fix the build when TF-PSA-Crypto only has a little MLDSA
When TF-PSA-Crypto has pure ML-DSA, we need to handle it in the test driver
for signatures. But we must not try to reference ML-DSA identifiers in
TF-PSA-Crypto branches where they don't exist yet, even though the
compilation option already exists (which notably includes the TF-PSA-Crypto
1.1.0 release).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-31 15:19:41 +02:00
Gilles Peskine e10d0ffc81 Add passthrough for pure ML-DSA to the test driver
We don't support ML-DSA in libtestdriver1 yet, because it's a copy of the
`builtin` driver but ML-DSA is provided by the `pqcp` driver. This means
that we can't test “driver-only” ML-DSA builds, but it should be possible to
enable ML-DSA in a build that dispatches through the test driver. This is
currently impossible because pure ML-DSA is not a sign-the-hash algorithm,
but the code in the test driver for signatures assumes that all signature
algorithms are sign-the-hash. Fix this in a minimal way by making the test
driver activate the fallback mechanism of driver dispatch when the algorithm
is pure ML-DSA. (Don't do this for all algorithms that are not sign-the-hash,
because in general, we do want the test driver to fail if it's given an
algorithm that it doesn't support.)

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-31 15:19:41 +02:00
Gilles Peskine be3764a69f Work around Valgrind hooking into _exit()
When running tests that use mbedtls_test_fork_run_child() under Valgrind,
bad things happen. Specifically:

* Valgrind reports leaks in the child. Those leaks do exist, but they're
  deliberate since we avoid cleaning up in the child (because we want to
  do as little as possible in the child, and because cleanups such as
  destroying PSA persistent keys would have undesirable effects outside
  the child process).
* Valgrind's overridden `_exit()` doesn't just perform checks, but also for
  some reason it causes the file description on the .datax file to seek
  backwards, causing tests to run again in a loop.

Avoid this by calling `execve()` (via `execlp()`) rather than `_exit()` if
it looks like the test is run under Valgrind. This is safe as long as
Valgrind isn't run with `--trace-children=yes`.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-23 19:24:18 +01:00
Gilles Peskine a5b6f6f778 Explain why the child calls _exit() and not exit()
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-23 19:24:18 +01:00
Gilles Peskine 1a5bf10ca0 Declare platform requirements for test helpers before including any system header
If we rely on `tf_psa_crypto_common.h`, it's too late. And `common.h in 3.6
doesn't have platform requirements.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 23:45:49 +01:00
Gilles Peskine a2083218c7 Keep exposing the library common.h in test/macros.h
Partially revert "Give test code access to internal macros of the library".
I think that it would be better not to require every user of `test/macros.h`
to have access to the library source, but it's out of scope here.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 17:47:22 +01:00
Gilles Peskine a55f15580f Avoid a buffer overread if the child reports a wrong length
The child code isn't supposed to cause memory corruption, but if it does,
try to report a problem rather than mess up further.

Adapt the code to report the failure to the parent accordingly. In
particular, we need to make sure that the first byte written to the
reporting pipe is the result code in all cases, so don't jump over the
writing of the result code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 15:55:52 +01:00
Gilles Peskine f4677c89d6 Fix null pointer dereference in the child if fdopen fails
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 15:53:57 +01:00
Gilles Peskine f7df78d3ab Fix reading of child output when it's fragmented
In the success case, we were only reporting the correct data written by the
child if the data was read in a single `read` call.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 15:53:57 +01:00
Gilles Peskine 96c9dca216 Fix exact-size check on failure in the child
When reading data from the child, if the child reports a failure, the parent
expects the child to write an `mbedtls_test_info_t` structure, no less, no
more. To achieve this, we try reading at least one byte more, and check that
we couldn't read more than the expected size. This commit fixes two bugs:

* On success, don't require the child to fill the output buffer. This check
  was only intended for the failure case, but was accidentally put in the
  wrong place.
* On failure, we weren't checking that the child had written at least the
  expected size, which could have been worse (we'd end up with a
  child_test_info structure that's only partially initialized).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 15:53:57 +01:00
Gilles Peskine 0384a5929a Give test code access to internal macros of the library
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 15:53:57 +01:00
Gilles Peskine dbfd1a6fa9 New test helper mbedtls_test_fork_run_child()
Run some code in a child process. Propagate output from the child if the
test succeeds, and propagate the test result information otherwise.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-16 15:53:57 +01:00
Gilles Peskine d1a8b5b596 Merge pull request #289 from gilles-peskine-arm/iar-1.1.0-framework
IAR build fixes before 1.1.0/4.1.0/3.6.6: framework support
2026-03-16 14:48:45 +01:00
Gilles Peskine 96b9d674eb Switch off overeager IAR warning
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-11 20:11:47 +01:00
Gilles Peskine 4ad123d54f IAR: let test code use stdio FILE functions
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-11 20:11:47 +01:00
Gilles Peskine 25c1750beb Sort out inclusions of <test/build_info.h> vs "test_common.h"
Include `"test_common.h"` as the first thing in C files, and
`<test/build_info.h>` as the first thing in header files.

This requires moving `<test/test_common.h>` to its intended location
`"test_common.h"`.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-11 20:11:47 +01:00
Gilles Peskine 30d9a6210b Split <test/build_info.h> out of test_common.h
The header `test_common.h` contains two kinds of things:

* Things to do at the beginning of individual C files. Specifically,
  defining macros that notify system headers about what we want from them.
  Keep those in `test_common.h`, which will subsequently be moved out of
  the include directory.
* Things to do at the beginning of every header. In particular, read the
  library configuration. Move them to a new header `build_info.h`,
  which is the only one intended to be included from headers.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-11 20:11:47 +01:00
Gilles Peskine 7bc3cdfd12 Update paths after moving some scripts to the framework
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-03-09 10:50:57 +01:00
Gilles Peskine 6d5987a954 Include <test/test_common.h> first in test code
This lets us define things that we want to have everywhere in test code. In
particular, this lets us define platform-specific symbols that influence
what system headers declare. This also takes care of including the library
configuration.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-02-20 21:46:11 +01:00
Gilles Peskine 4f3a21f40d Stop using MBEDTLS_PRINTF_SIZET
Since Mbed TLS 3.6.0, all officially supported versions of Visual Studio
a printf function family that is sufficiently compliant to C99 for our
purposes, in particular supporting `%zu` for `size_t`. The only platform
without `%zu` that we semi-officially support is older versions of MinGW,
still used in our CI. MinGW provides either a Windows legacy printf or a
standards-compliant printf depending on the value of
`__USE_MINGW_ANSI_STDIO` when compiling each C file. Force the use of the
compliant version. Don't rely on `MBEDTLS_PRINTF_SIZET`, which is defined in
`<mbedtls/debug.h>` and no longer considers the Windows legacy version in
Mbed TLS >= 4.1.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-02-20 20:31:56 +01:00
Gilles Peskine f41a9f6056 New macro TEST_ASSERT_ERRNO
Show errno if a test assertion fails.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-02-17 17:21:20 +01:00
Gilles Peskine 2ffd883d80 Driver entry point test code: add XOF support
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2026-02-09 21:16:28 +01:00
Valerio Setti c32c5064c0 tests: pk_helpers: fix code style
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-15 10:11:37 +01:00
Valerio Setti 73c4f2e5d6 tests: pk_helpers: improve documentation of pk_context_populate_method_t
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-14 23:33:50 +01:00
Valerio Setti 4eebe42a11 tests: pk_helpers: optimize failure reporting in mbedtls_pk_helpers_populate_context
Keep TEST_EQUAL() on the function that might fail so that if a failure
happen the message will report the actual function that failed.

Documentation of the helper function is also updated.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-14 23:09:35 +01:00
Valerio Setti 2d7c5cb574 tests: pk_helpers: fix outer guard in functions implementation
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-14 12:54:37 +01:00
Valerio Setti 43e4dc061d tests: pk_helpers: update documentation
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-14 12:54:10 +01:00
Valerio Setti 2b3cc12c01 tests: pk_helpers: let all helper function return an error code
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-14 12:53:25 +01:00
Valerio Setti fa298be91a tests: src: pk_helpers: style fix in code and documentation
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-08 09:28:34 +01:00
Valerio Setti e28511869c tests: src: pk_helpers: select predefined keys using PSA key type and bits
A new look-up table is created, "predefined_keys_psa", to list all the
predefined keys together with the corresponding PSA key type and bits.
A new look-up table was created in order not to conflict with the already
existing "predefined_keys" one.

"mbedtls_pk_helpers_get_predefined_key_data" is modified in order to
use the new look-up table.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-08 09:16:21 +01:00
Valerio Setti 88e0eb729e tests: src: pk_helpers: add documentation and proper functions prefix
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-07 18:25:07 +01:00
Valerio Setti 0a3c8c4fdd tests: src: move PK helpers out of tf-psa-crypto to this repo
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2026-01-07 12:24:32 +01:00
Ronald Cron 17750d1f3a test_driver.py: Fix/Improve some comments
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-12-11 14:40:46 +01:00
Ronald Cron c806e683f8 test_driver.py: Get rid of one subdirectory in include/ limitation
Get rid of one subdirectory in include/ limitation.
Prefix all file basename instead of changing the
name of `include/mbedtls` to `include/libtestdriver1`.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-12-09 11:04:50 +01:00
Ronald Cron 9d7330c411 tests: drivers: Factorize internal header inclusion pre-processor logic
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-12-09 01:04:19 +01:00
Ronald Cron ca68ad8377 Move "libtestdriver1_" aliases to test_driver_common.h
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-12-05 14:45:32 +01:00
Ronald Cron 0d9ae6a986 Update test drivers for TF-PSA-Crypto test driver
Update test drivers for TF-PSA-Crypto test driver
based on a copy of `drivers/builtin`.

We mainly keep using what is already in place for
libtestdriver1 in Mbed TLS by still defining
MBEDTLS_TEST_LIBTESTDRIVER1.

As in TF-PSA-Crypto, the test driver is a derivative
of a clone of the built-in driver, not of the whole
library, the paths to the driver internal headers
are different. The paths are relative to
`drivers/libtestdriver1/include`.

Otherwise, `psa_key_attributes_t` is prefixed with
`libtestdriver1_` in Mbed TLS test driver but not
in TF-PSA-Crypto one. Thus in TF-PSA-Crypto case
define `libtestdriver1_psa_key_attributes_t` as
`psa_key_attributes_t`.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-12-05 14:45:32 +01:00
Minos Galanakis 4579964747 Merge remote-tracking branch 'restricted/main-restricted' into framework-common
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2025-10-02 15:25:04 +01:00
Ronald Cron fe3e56f8af Fix clang warning
Fix clang warning related to mismatch
between function prototype and
function definition: void func(void) vs void func().

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-09-22 10:32:51 +02:00
Gilles Peskine 82a7962c5f Merge remote-tracking branch 'main' into restricted-framework-merge-public-20250916 2025-09-16 16:16:24 +02:00
Ronald Cron 275f8dd5c7 Remove MBEDTLS_PLATFORM_GET_ENTROPY_ALT
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2025-09-09 08:27:45 +02:00
Gilles Peskine 5b46871e5a Double free on a mutex is now a no-op
Since TF-PSA-Crypto 1.0 (threading internal interface version 4.0.0.1),
`mbedtls_mutex_free()` on an all-bits-zero mutex is defined to be a no-op.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-09-06 19:59:31 +02:00
Gilles Peskine bedd856cad Support new threading interface where mutex_init returns int
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-09-05 21:15:55 +02:00
Gilles Peskine 8ef6acd627 Define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA for TF-PSA-Crypto
In Mbed TLS 3.6, this error was used in the library as well as the test
framework, but since TF-PSA-Crypto 1.0, it has been removed from the
library.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-09-02 00:16:32 +02:00
Gilles Peskine bf196ac2fd Only define mutex usage test helpers when mutex usage tests are used
Fixes an unused function warning.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-09-01 19:47:24 +02:00
Gilles Peskine 34bd04d6dd Support mutex API type being separate from the platform type
The way the mutex usage verification framework plugs into the library
requires it to have access to the API type, which holds the state field. In
the new threading API, the mutable function that the framework overrides now
only receives a pointer to the platform object that is embedded in the API
object. Hence we need to calculate a pointer to the containing API object
when given a pointer to the platform object. It's ugly, but it works.

A follow-up should clean this up by changing how the mutex usage
verification framework plugs into the library. It should use a more normal
hook function mechanism instead of replacing the function pointers.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-09-01 19:47:23 +02:00
Gilles Peskine 2906014a4c Support mutex API functions not being the mutable pointers
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2025-09-01 19:47:23 +02:00
Anton Matkin af60653c4c Changed the private header preprocessor condition, so that the old (non-private) path is only taken in case of mbedtls 3.6
Signed-off-by: Anton Matkin <anton.matkin@arm.com>
2025-08-21 12:12:14 +02:00