Merge pull request #278 from gilles-peskine-arm/generate_mldsa_tests-create

Support committed generated test data and generate PQCP test data
diff --git a/psasim/src/aut_psa_key_agreement.c b/psasim/src/aut_psa_key_agreement.c
index 4a0aab1..4ab1bf4 100644
--- a/psasim/src/aut_psa_key_agreement.c
+++ b/psasim/src/aut_psa_key_agreement.c
@@ -69,7 +69,7 @@
         return EXIT_FAILURE;
     }
 
-    mbedtls_printf("Client Public Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", client_pk_len);
+    mbedtls_printf("Client Public Key (%zu bytes):\n", client_pk_len);
 
     for (size_t j = 0; j < client_pk_len; j++) {
         if (j % 8 == 0) {
@@ -108,7 +108,7 @@
         return EXIT_FAILURE;
     }
 
-    mbedtls_printf("Server Public Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", sizeof(server_pk));
+    mbedtls_printf("Server Public Key (%zu bytes):\n", sizeof(server_pk));
 
     for (size_t j = 0; j < sizeof(server_pk); j++) {
         if (j % 8 == 0) {
@@ -129,7 +129,7 @@
         return EXIT_FAILURE;
     }
 
-    mbedtls_printf("Derived Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", derived_key_len);
+    mbedtls_printf("Derived Key (%zu bytes):\n", derived_key_len);
 
     for (size_t j = 0; j < derived_key_len; j++) {
         if (j % 8 == 0) {
diff --git a/scripts/assemble_changelog.py b/scripts/assemble_changelog.py
index 07e6fc5..69ee1b1 100755
--- a/scripts/assemble_changelog.py
+++ b/scripts/assemble_changelog.py
@@ -94,11 +94,6 @@
         raise NotImplementedError
 
     @classmethod
-    def version_title_text(cls, version_title):
-        """Return the text of a formatted version section title."""
-        raise NotImplementedError
-
-    @classmethod
     def split_categories(cls, version_body):
         """Split a changelog version section body into categories.
 
@@ -141,10 +136,6 @@
                 top_version_title, top_version_body,
                 changelog_file_content[top_version_end:])
 
-    @classmethod
-    def version_title_text(cls, version_title):
-        return re.sub(r'\n.*', version_title, re.DOTALL)
-
     _category_title_re = re.compile(r'(^\w.*)\n+', re.MULTILINE)
     @classmethod
     def split_categories(cls, version_body):
diff --git a/scripts/check_names.py b/scripts/check_names.py
index a8a6d52..d045031 100755
--- a/scripts/check_names.py
+++ b/scripts/check_names.py
@@ -1159,6 +1159,14 @@
 
         If you add an exception, make sure to explain why!
         """
+        # The platform requirements headers define macros that are meant to
+        # be consumed by system headers. These macros are in a namespace
+        # reserved by the C language (two initial underscores, or an
+        # initial underscore followed by an uppercase letter).
+        if group == 'internal_macros' and \
+           '_platform_requirements.h' in match.filename and \
+           re.match(r'_[A-Z_]', match.name):
+            return True
         # We use some short macros that start with a lowercase letter
         # internally in bignum code. They are grandfathered in. They
         # may be in a header file, but only in a source directory, not
diff --git a/scripts/generate_test_code.py b/scripts/generate_test_code.py
index 2ac00ad..4b7f939 100755
--- a/scripts/generate_test_code.py
+++ b/scripts/generate_test_code.py
@@ -762,7 +762,7 @@
         raise ValueError('Expected split character. Found string!')
     out = re.sub(r'(\\.)|' + split_char,
                  lambda m: m.group(1) or '\n', inp_str,
-                 len(inp_str)).split('\n')
+                 count=len(inp_str)).split('\n')
     out = [x for x in out if x]
     return out
 
diff --git a/scripts/mbedtls_framework/c_wrapper_generator.py b/scripts/mbedtls_framework/c_wrapper_generator.py
index f15f3a7..bc77703 100644
--- a/scripts/mbedtls_framework/c_wrapper_generator.py
+++ b/scripts/mbedtls_framework/c_wrapper_generator.py
@@ -110,6 +110,17 @@
 
             ''')
 
+        if not header:
+            # On Mingw-w64, force the use of a C99-compliant printf() and friends.
+            # This is necessary on older versions of Mingw and/or Windows runtimes
+            # where snprintf does not always zero-terminate the buffer, and does
+            # not support formats such as "%zu" for size_t and "%lld" for long long.
+            prologue += strip_indentation(f'''
+                #if !defined(__USE_MINGW_ANSI_STDIO)
+                #define __USE_MINGW_ANSI_STDIO 1
+                #endif
+            ''')
+
         for include in self._INCLUDES:
             prologue += "#include {}\n".format(include)
 
@@ -359,7 +370,7 @@
         """Preprocessor symbol used as a guard against multiple inclusion."""
         # Heuristic to strip irrelevant leading directories
         filename = re.sub(r'.*include[\\/]', r'', filename)
-        return re.sub(r'[^0-9A-Za-z]', r'_', filename, re.A).upper()
+        return re.sub(r'[^0-9A-Za-z]', r'_', filename, flags=re.A).upper()
 
     def write_h_file(self, filename: str) -> None:
         """Output a header file with function wrapper declarations and macro definitions."""
@@ -403,7 +414,6 @@
 #if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS)
 #include <stdio.h>
 #include <inttypes.h>
-#include <mbedtls/debug.h> // for MBEDTLS_PRINTF_SIZET
 #include <mbedtls/platform.h> // for mbedtls_fprintf
 #endif /* defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS) */
 """)
@@ -412,7 +422,7 @@
         'int': '%d',
         'long': '%ld',
         'long long': '%lld',
-        'size_t': '%"MBEDTLS_PRINTF_SIZET"',
+        'size_t': '%zu',
         'unsigned': '0x%08x',
         'unsigned int': '0x%08x',
         'unsigned long': '0x%08lx',
diff --git a/tests/include/test/arguments.h b/tests/include/test/arguments.h
index 6d267b6..3f43d43 100644
--- a/tests/include/test/arguments.h
+++ b/tests/include/test/arguments.h
@@ -14,7 +14,7 @@
 #ifndef TEST_ARGUMENTS_H
 #define TEST_ARGUMENTS_H
 
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 #include <stdint.h>
 #include <stdlib.h>
 
diff --git a/tests/include/test/asn1_helpers.h b/tests/include/test/asn1_helpers.h
index 2eb9171..e9ed739 100644
--- a/tests/include/test/asn1_helpers.h
+++ b/tests/include/test/asn1_helpers.h
@@ -8,6 +8,7 @@
 #ifndef ASN1_HELPERS_H
 #define ASN1_HELPERS_H
 
+#include "test_common.h"
 #include "test/helpers.h"
 
 /** Skip past an INTEGER in an ASN.1 buffer.
diff --git a/tests/include/test/bignum_codepath_check.h b/tests/include/test/bignum_codepath_check.h
index 3d72be1..65f0c9f 100644
--- a/tests/include/test/bignum_codepath_check.h
+++ b/tests/include/test/bignum_codepath_check.h
@@ -17,6 +17,8 @@
 #ifndef BIGNUM_CODEPATH_CHECK_H
 #define BIGNUM_CODEPATH_CHECK_H
 
+#include "test_common.h"
+
 #include "bignum_core.h"
 
 #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
diff --git a/tests/include/test/bignum_helpers.h b/tests/include/test/bignum_helpers.h
index 49e290b..d6b5458 100644
--- a/tests/include/test/bignum_helpers.h
+++ b/tests/include/test/bignum_helpers.h
@@ -13,7 +13,7 @@
 #ifndef TEST_BIGNUM_HELPERS_H
 #define TEST_BIGNUM_HELPERS_H
 
-#include <mbedtls/build_info.h>
+#include "test_common.h"
 
 #if defined(MBEDTLS_BIGNUM_C)
 
diff --git a/tests/include/test/constant_flow.h b/tests/include/test/constant_flow.h
index c5658eb..eac0bcf 100644
--- a/tests/include/test/constant_flow.h
+++ b/tests/include/test/constant_flow.h
@@ -12,7 +12,7 @@
 #ifndef TEST_CONSTANT_FLOW_H
 #define TEST_CONSTANT_FLOW_H
 
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 
 /*
  * This file defines the two macros
diff --git a/tests/include/test/fake_external_rng_for_test.h b/tests/include/test/fake_external_rng_for_test.h
index 4c5177c..997a712 100644
--- a/tests/include/test/fake_external_rng_for_test.h
+++ b/tests/include/test/fake_external_rng_for_test.h
@@ -10,7 +10,7 @@
 #ifndef FAKE_EXTERNAL_RNG_FOR_TEST_H
 #define FAKE_EXTERNAL_RNG_FOR_TEST_H
 
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 
 #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
 /** Enable the insecure implementation of mbedtls_psa_external_get_random().
diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h
index f1700bb..8eabcd0 100644
--- a/tests/include/test/helpers.h
+++ b/tests/include/test/helpers.h
@@ -13,12 +13,7 @@
 #ifndef TEST_HELPERS_H
 #define TEST_HELPERS_H
 
-/* Most fields of publicly available structs are private and are wrapped with
- * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
- * directly (without using the MBEDTLS_PRIVATE wrapper). */
-#define MBEDTLS_ALLOW_PRIVATE_ACCESS
-
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 
 #if defined(__SANITIZE_ADDRESS__) /* gcc -fsanitize=address */
 #  define MBEDTLS_TEST_HAVE_ASAN
@@ -241,6 +236,19 @@
 void mbedtls_test_fail(const char *test, int line_no, const char *filename);
 
 /**
+ * \brief           Record the current test case as a failure
+ *                  and show the value of errno.
+ *
+ *                  This function is usually called via #TEST_ASSERT_ERRNO.
+ *
+ * \param test      Description of the failure or assertion that failed. This
+ *                  MUST be a string literal.
+ * \param line_no   Line number where the failure originated.
+ * \param filename  Filename where the failure originated.
+ */
+void mbedtls_test_fail_errno(const char *test, int line_no, const char *filename);
+
+/**
  * \brief           Record the current test case as skipped.
  *
  *                  This function can be called directly however it is usually
diff --git a/tests/include/test/macros.h b/tests/include/test/macros.h
index 544db91..7c0433a 100644
--- a/tests/include/test/macros.h
+++ b/tests/include/test/macros.h
@@ -12,7 +12,7 @@
 #ifndef TEST_MACROS_H
 #define TEST_MACROS_H
 
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 
 #include <stdlib.h>
 
@@ -54,6 +54,21 @@
         }                                                    \
     } while (0)
 
+/** \brief Evaluate an integer expression. If the value is 0 (i.e. false),
+ *         mark the test case as failed and display errno.
+ *
+ * This is intended for functions that follow the Unix API convention of
+ * returning a particular value (often -1) and setting errno on failure,
+ * e.g. `TEST_ASSERT_ERRNO(open(...) != -1)`.
+ */
+#define TEST_ASSERT_ERRNO(expr)                                         \
+    do {                                                                \
+        if (!(expr)) {                                                  \
+            mbedtls_test_fail_errno(#expr, __LINE__, __FILE__);         \
+            goto exit;                                                  \
+        }                                                               \
+    } while (0)
+
 /** This macro asserts fails the test with given output message.
  *
  * \param   MESSAGE The message to be outputed on assertion
diff --git a/tests/include/test/memory.h b/tests/include/test/memory.h
index 940d9e6..30c4ddf 100644
--- a/tests/include/test/memory.h
+++ b/tests/include/test/memory.h
@@ -12,7 +12,8 @@
 #ifndef TEST_MEMORY_H
 #define TEST_MEMORY_H
 
-#include "mbedtls/build_info.h"
+#include "test_common.h"
+
 #include "mbedtls/platform.h"
 #include "test/helpers.h"
 
diff --git a/tests/include/test/pk_helpers.h b/tests/include/test/pk_helpers.h
index e358657..332fea8 100644
--- a/tests/include/test/pk_helpers.h
+++ b/tests/include/test/pk_helpers.h
@@ -10,6 +10,8 @@
 #ifndef PK_HELPERS_H
 #define PK_HELPERS_H
 
+#include "test_common.h"
+
 #if defined(MBEDTLS_PK_C)
 
 #include <psa/crypto.h>
diff --git a/tests/include/test/psa_crypto_helpers.h b/tests/include/test/psa_crypto_helpers.h
index 962ca1e..c19eb43 100644
--- a/tests/include/test/psa_crypto_helpers.h
+++ b/tests/include/test/psa_crypto_helpers.h
@@ -9,6 +9,7 @@
 #ifndef PSA_CRYPTO_HELPERS_H
 #define PSA_CRYPTO_HELPERS_H
 
+#include "test_common.h"
 #include "test/helpers.h"
 
 #if (MBEDTLS_VERSION_MAJOR < 4 && defined(MBEDTLS_PSA_CRYPTO_C)) || \
diff --git a/tests/include/test/psa_exercise_key.h b/tests/include/test/psa_exercise_key.h
index b1e285a..d3eb852 100644
--- a/tests/include/test/psa_exercise_key.h
+++ b/tests/include/test/psa_exercise_key.h
@@ -9,6 +9,7 @@
 #ifndef PSA_EXERCISE_KEY_H
 #define PSA_EXERCISE_KEY_H
 
+#include "test_common.h"
 #include "test/helpers.h"
 #include "test/psa_crypto_helpers.h"
 
diff --git a/tests/include/test/psa_helpers.h b/tests/include/test/psa_helpers.h
index b617189..87ffa78 100644
--- a/tests/include/test/psa_helpers.h
+++ b/tests/include/test/psa_helpers.h
@@ -9,6 +9,8 @@
 #ifndef PSA_HELPERS_H
 #define PSA_HELPERS_H
 
+#include "test_common.h"
+
 #if defined(MBEDTLS_PSA_CRYPTO_SPM)
 #include "spm/psa_defs.h"
 #endif
diff --git a/tests/include/test/psa_memory_poisoning_wrappers.h b/tests/include/test/psa_memory_poisoning_wrappers.h
index 3f30b65..95d48f0 100644
--- a/tests/include/test/psa_memory_poisoning_wrappers.h
+++ b/tests/include/test/psa_memory_poisoning_wrappers.h
@@ -15,6 +15,8 @@
 #ifndef PSA_MEMORY_POISONING_WRAPPERS_H
 #define PSA_MEMORY_POISONING_WRAPPERS_H
 
+#include "test_common.h"
+
 #include "psa/crypto.h"
 
 #include "test/memory.h"
diff --git a/tests/include/test/random.h b/tests/include/test/random.h
index 6304e05..fd235a7 100644
--- a/tests/include/test/random.h
+++ b/tests/include/test/random.h
@@ -13,7 +13,7 @@
 #ifndef TEST_RANDOM_H
 #define TEST_RANDOM_H
 
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 
 #include <stddef.h>
 #include <stdint.h>
diff --git a/tests/include/test/test_common.h b/tests/include/test/test_common.h
new file mode 100644
index 0000000..baab667
--- /dev/null
+++ b/tests/include/test/test_common.h
@@ -0,0 +1,34 @@
+/**
+ * \file test_common.h
+ *
+ * \brief Common things for all Mbed TLS and TF-PSA-Crypto test code.
+ *
+ * Include this header first in all headers in `include/test/`.
+ * Include this or another header from `include/test/` in all test C files.
+ */
+
+/*
+ *  Copyright The Mbed TLS Contributors
+ *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+ */
+
+#ifndef TEST_TEST_COMMON_H
+#define TEST_TEST_COMMON_H
+
+/* On Mingw-w64, force the use of a C99-compliant printf() and friends.
+ * This is necessary on older versions of Mingw and/or Windows runtimes
+ * where snprintf does not always zero-terminate the buffer, and does
+ * not support formats such as "%zu" for size_t and "%lld" for long long.
+ */
+#if !defined(__USE_MINGW_ANSI_STDIO)
+#define __USE_MINGW_ANSI_STDIO 1
+#endif
+
+#include <mbedtls/build_info.h>
+
+/* Most fields of publicly available structs are private and are wrapped with
+ * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
+ * directly (without using the MBEDTLS_PRIVATE wrapper). */
+#define MBEDTLS_ALLOW_PRIVATE_ACCESS
+
+#endif /* TEST_TEST_COMMON_H */
diff --git a/tests/include/test/threading_helpers.h b/tests/include/test/threading_helpers.h
index dbe2f4c..325aea6 100644
--- a/tests/include/test/threading_helpers.h
+++ b/tests/include/test/threading_helpers.h
@@ -13,18 +13,12 @@
 #ifndef THREADING_HELPERS_H
 #define THREADING_HELPERS_H
 
-#include "mbedtls/private_access.h"
-#include "mbedtls/build_info.h"
+#include "test_common.h"
 
 #if defined MBEDTLS_THREADING_C
 
 #include <mbedtls/threading.h>
 
-/* Most fields of publicly available structs are private and are wrapped with
- * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
- * directly (without using the MBEDTLS_PRIVATE wrapper). */
-#define MBEDTLS_ALLOW_PRIVATE_ACCESS
-
 /* Error in thread management */
 #define MBEDTLS_ERR_THREADING_THREAD_ERROR                 -0x001F
 
diff --git a/tests/programs/metatest.c b/tests/programs/metatest.c
index b862889..4345061 100644
--- a/tests/programs/metatest.c
+++ b/tests/programs/metatest.c
@@ -26,6 +26,14 @@
  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  */
 
+/* On Mingw-w64, force the use of a C99-compliant printf() and friends.
+ * This is necessary on older versions of Mingw and/or Windows runtimes
+ * where snprintf does not always zero-terminate the buffer, and does
+ * not support formats such as "%zu" for size_t and "%lld" for long long.
+ */
+#if !defined(__USE_MINGW_ANSI_STDIO)
+#define __USE_MINGW_ANSI_STDIO 1
+#endif
 
 #include <mbedtls/debug.h>
 #include <mbedtls/platform.h>
@@ -200,10 +208,7 @@
     size_t start = 0, offset = 0, count = 0;
     char direction = 'r';
     if (sscanf(name,
-               "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
-               "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
-               "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
-               "_%c",
+               "%*[^0-9]%zu%*[^0-9]%zu%*[^0-9]%zu_%c",
                &start, &offset, &count, &direction) != 4) {
         mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
         return;
@@ -217,22 +222,19 @@
 
     if (start > sizeof(aligned.buf)) {
         mbedtls_fprintf(stderr,
-                        "%s: start=%" MBEDTLS_PRINTF_SIZET
-                        " > size=%" MBEDTLS_PRINTF_SIZET,
+                        "%s: start=%zu > size=%zu",
                         __func__, start, sizeof(aligned.buf));
         return;
     }
     if (start + count > sizeof(aligned.buf)) {
         mbedtls_fprintf(stderr,
-                        "%s: start+count=%" MBEDTLS_PRINTF_SIZET
-                        " > size=%" MBEDTLS_PRINTF_SIZET,
+                        "%s: start+count=%zu > size=%zu",
                         __func__, start + count, sizeof(aligned.buf));
         return;
     }
     if (offset >= count) {
         mbedtls_fprintf(stderr,
-                        "%s: offset=%" MBEDTLS_PRINTF_SIZET
-                        " >= count=%" MBEDTLS_PRINTF_SIZET,
+                        "%s: offset=%zu >= count=%zu",
                         __func__, offset, count);
         return;
     }
diff --git a/tests/src/helpers.c b/tests/src/helpers.c
index 963897b..db4b616 100644
--- a/tests/src/helpers.c
+++ b/tests/src/helpers.c
@@ -6,6 +6,7 @@
 #include <test/constant_flow.h>
 #include <test/helpers.h>
 #include <test/macros.h>
+#include <errno.h>
 #include <string.h>
 
 #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
@@ -571,6 +572,31 @@
     return 0;
 }
 
+void mbedtls_test_fail_errno(const char *test,
+                             int line_no, const char *filename)
+{
+#ifdef MBEDTLS_THREADING_C
+    mbedtls_mutex_lock(&mbedtls_test_info_mutex);
+#endif /* MBEDTLS_THREADING_C */
+
+    /* Don't use accessor, we already hold mutex. */
+    if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
+        /* If we've already recorded the test as having failed then don't
+         * overwrite any previous information about the failure. */
+
+        char buf[MBEDTLS_TEST_LINE_LENGTH];
+        mbedtls_test_fail_internal(test, line_no, filename);
+        (void) mbedtls_snprintf(buf, sizeof(buf),
+                                "errno = %d (%s)",
+                                errno, strerror(errno));
+        mbedtls_test_set_line1_internal(buf);
+    }
+
+#ifdef MBEDTLS_THREADING_C
+    mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
+#endif /* MBEDTLS_THREADING_C */
+}
+
 int mbedtls_test_unhexify(unsigned char *obuf,
                           size_t obufmax,
                           const char *ibuf,
diff --git a/tests/src/pk_helpers.c b/tests/src/pk_helpers.c
index dbdf144..4bef98f 100644
--- a/tests/src/pk_helpers.c
+++ b/tests/src/pk_helpers.c
@@ -7,12 +7,12 @@
  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  */
 
-#include <mbedtls/pk.h>
 #include <test/macros.h>
 #include <test/helpers.h>
 #include <test/pk_helpers.h>
 #include <test/psa_helpers.h>
 #include <test/test_keys.h>
+#include <mbedtls/pk.h>
 #include "psa_util_internal.h"
 
 /* Functions like mbedtls_pk_wrap_psa() are only available in tf-psa-crypto and