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

Align 3.6 test helpers with development #9547

Open
wants to merge 3 commits into
base: mbedtls-3.6
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions tests/include/test/drivers/crypto_config_test_driver_extension.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/**
* This file is intended to be used to build PSA test driver libraries. It is
* intended to be appended by the test build system to the crypto_config.h file
* of the Mbed TLS library the test library will be linked to. It mirrors the
* PSA_ACCEL_* macros defining the cryptographic operations the test library
* supports.
* This file is intended to be used to build PSA external test driver
* libraries (libtestdriver1).
*
* It is intended to be appended by the test build system to the
* crypto_config.h file of the Mbed TLS library the test library will be
* linked to (see `tests/Makefile` libtestdriver1 target). This is done in
* order to insert it at the right time: after the main configuration
* (PSA_WANT) but before the logic that determines what built-ins to enable
* based on PSA_WANT and MBEDTLS_PSA_ACCEL macros.
*
* It reverses the PSA_ACCEL_* macros defining the cryptographic operations
* that will be accelerated in the main library:
* - When something is accelerated in the main library, we need it supported
* in libtestdriver1, so we disable the accel macro in order to the built-in
* to be enabled.
* - When something is NOT accelerated in the main library, we don't need it
* in libtestdriver1, so we enable its accel macro in order to the built-in
* to be disabled, to keep libtestdriver1 minimal. (We can't adjust the
* PSA_WANT macros as they need to be the same between libtestdriver1 and
* the main library, since they determine the ABI between the two.)
*/

#include "psa/crypto_legacy.h"
Expand Down
43 changes: 23 additions & 20 deletions tests/include/test/psa_crypto_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

#include "test/helpers.h"

#if defined(MBEDTLS_PSA_CRYPTO_C)
#if ((MBEDTLS_VERSION_MAJOR < 4) \
&& defined(MBEDTLS_PSA_CRYPTO_C)) \
|| (MBEDTLS_VERSION_MAJOR >= 4 \
&& defined(MBEDTLS_PSA_CRYPTO_CLIENT))
#include "test/psa_helpers.h"
#include <psa/crypto.h>
#endif
Expand Down Expand Up @@ -40,12 +43,19 @@
mbedtls_psa_crypto_free(); \
} \
while (0)
#else /*MBEDTLS_PSA_CRYPTO_C */
#elif MBEDTLS_VERSION >= 4 && \
defined(MBEDTLS_PSA_CRYPTO_CLIENT) /* MBEDTLS_PSA_CRYPTO_CLIENT && !MBEDTLS_PSA_CRYPTO_C */
#define PSA_INIT() PSA_ASSERT(psa_crypto_init())
#define PSA_DONE() mbedtls_psa_crypto_free();
#else /* MBEDTLS_PSA_CRYPTO_CLIENT && !MBEDTLS_PSA_CRYPTO_C */
#define PSA_INIT() ((void) 0)
#define PSA_DONE() ((void) 0)
#endif /* MBEDTLS_PSA_CRYPTO_C */

#if defined(MBEDTLS_PSA_CRYPTO_C)
#if ((MBEDTLS_VERSION_MAJOR < 4) \
&& defined(MBEDTLS_PSA_CRYPTO_C)) \
|| (MBEDTLS_VERSION_MAJOR >= 4 \
&& defined(MBEDTLS_PSA_CRYPTO_CLIENT))

#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)

Expand Down Expand Up @@ -293,18 +303,19 @@ uint64_t mbedtls_test_parse_binary_string(data_t *bin_string);
* \param alg The AEAD algorithm.
* \param nonce_length The nonce length in number of bytes.
*/

#if defined(MBEDTLS_GCM_ALT) || \
defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
#define MBEDTLS_TEST_HAVE_ALT_GCM 1
#define MBEDTLS_TEST_HAVE_ACCEL_GCM 1
#else
#define MBEDTLS_TEST_HAVE_ALT_GCM 0
#define MBEDTLS_TEST_HAVE_ACCEL_GCM 0
#endif

#define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \
nonce_length) \
do \
{ \
if ((MBEDTLS_TEST_HAVE_ALT_GCM) && \
if ((MBEDTLS_TEST_HAVE_ACCEL_GCM) && \
(PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \
PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \
((nonce_length) != 12)) \
Expand All @@ -315,7 +326,7 @@ uint64_t mbedtls_test_parse_binary_string(data_t *bin_string);
} \
while (0)

#endif /* MBEDTLS_PSA_CRYPTO_C */
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT || MBEDTLS_PSA_CRYPTO_C */

/** \def USE_PSA_INIT
*
Expand All @@ -334,18 +345,9 @@ uint64_t mbedtls_test_parse_binary_string(data_t *bin_string);
* This is like #PSA_DONE except it does nothing under the same conditions as
* #USE_PSA_INIT.
*/
#if defined(MBEDTLS_USE_PSA_CRYPTO)
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
#define USE_PSA_INIT() PSA_INIT()
#define USE_PSA_DONE() PSA_DONE()
#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
/* TLS 1.3 must work without having called psa_crypto_init(), for backward
* compatibility with Mbed TLS <= 3.5 when connecting with a peer that
* supports both TLS 1.2 and TLS 1.3. See mbedtls_ssl_tls13_crypto_init()
* and https://github.com/Mbed-TLS/mbedtls/issues/9072 . */
#define USE_PSA_INIT() ((void) 0)
/* TLS 1.3 may have initialized the PSA subsystem. Shut it down cleanly,
* otherwise Asan and Valgrind would notice a resource leak. */
#define USE_PSA_DONE() PSA_DONE()
#else /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
/* Define empty macros so that we can use them in the preamble and teardown
* of every test function that uses PSA conditionally based on
Expand Down Expand Up @@ -417,12 +419,13 @@ uint64_t mbedtls_test_parse_binary_string(data_t *bin_string);
* This is like #PSA_DONE except it does nothing under the same conditions as
* #MD_OR_USE_PSA_INIT.
*/
#if defined(MBEDTLS_MD_SOME_PSA)
#if defined(MBEDTLS_MD_SOME_PSA) || \
defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
#define MD_OR_USE_PSA_INIT() PSA_INIT()
#define MD_OR_USE_PSA_DONE() PSA_DONE()
#else
#define MD_OR_USE_PSA_INIT() USE_PSA_INIT()
#define MD_OR_USE_PSA_DONE() USE_PSA_DONE()
#define MD_OR_USE_PSA_INIT() ((void) 0)
#define MD_OR_USE_PSA_DONE() ((void) 0)
#endif

/** \def AES_PSA_INIT
Expand Down
27 changes: 16 additions & 11 deletions tests/include/test/ssl_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,26 @@
#endif

#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
#if defined(MBEDTLS_SSL_HAVE_AES)
#if defined(MBEDTLS_SSL_HAVE_GCM)
#if defined(MBEDTLS_MD_CAN_SHA384)
#if defined(MBEDTLS_SSL_HAVE_AES) || defined(PSA_WANT_KEY_TYPE_AES)
#if defined(MBEDTLS_SSL_HAVE_GCM) || defined(PSA_WANT_ALG_GCM)
#if defined(MBEDTLS_MD_CAN_SHA384) || defined(PSA_WANT_ALG_SHA384)
#define MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384
#endif
#if defined(MBEDTLS_MD_CAN_SHA256)
#if defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256)
#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256
#endif
#endif /* MBEDTLS_SSL_HAVE_GCM */
#if defined(MBEDTLS_SSL_HAVE_CCM) && defined(MBEDTLS_MD_CAN_SHA256)
#endif /* MBEDTLS_SSL_HAVE_GCM || PSA_WANT_ALG_GCM */
#if defined(PSA_WANT_ALG_CCM) \
&& (defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256))
#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256
#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256
#endif
#endif /* MBEDTLS_SSL_HAVE_AES */
#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY) && defined(MBEDTLS_MD_CAN_SHA256)
#endif /* PSA_WANT_KEY_TYPE_AES || MBEDTLS_SSL_HAVE_AES */
#if defined(PSA_WANT_ALG_CHACHA20_POLY1305) \
&& (defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256))
#define MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256
#endif

Expand Down Expand Up @@ -501,16 +506,16 @@ int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
#endif

#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
defined(MBEDTLS_SSL_HAVE_CBC) && defined(MBEDTLS_SSL_HAVE_AES)
defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
const unsigned char *iv,
size_t iv_len,
const unsigned char *input,
size_t ilen,
unsigned char *output,
size_t *olen);
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_HAVE_CBC &&
MBEDTLS_SSL_HAVE_AES */
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
PSA_WANT_KEY_TYPE_AES */

int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
mbedtls_ssl_transform *t_out,
Expand Down
43 changes: 25 additions & 18 deletions tests/src/certs.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ const size_t mbedtls_test_cli_crt_ec_len =
* Dispatch between SHA-1 and SHA-256
*/

#if defined(MBEDTLS_MD_CAN_SHA256)
#if defined(MBEDTLS_MD_CAN_SHA256) || defined(PSA_WANT_ALG_SHA_256)
#define TEST_CA_CRT_RSA TEST_CA_CRT_RSA_SHA256
#define TEST_SRV_CRT_RSA TEST_SRV_CRT_RSA_SHA256
#else
#define TEST_CA_CRT_RSA TEST_CA_CRT_RSA_SHA1
#define TEST_SRV_CRT_RSA TEST_SRV_CRT_RSA_SHA1
#endif /* MBEDTLS_MD_CAN_SHA256 */
#endif /* PSA_WANT_ALG_SHA_256 || MBEDTLS_MD_CAN_SHA256 */

const char mbedtls_test_ca_crt_rsa[] = TEST_CA_CRT_RSA;
const char mbedtls_test_srv_crt_rsa[] = TEST_SRV_CRT_RSA;
Expand Down Expand Up @@ -406,10 +406,12 @@ const size_t mbedtls_test_cli_crt_len =

/* List of CAs in PEM or DER, depending on config */
const char *mbedtls_test_cas[] = {
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA1)
#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_1)
mbedtls_test_ca_crt_rsa_sha1,
#endif
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
#if defined(MBEDTLS_RSA_C) && \
(defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256))
mbedtls_test_ca_crt_rsa_sha256,
#endif
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Expand All @@ -418,10 +420,12 @@ const char *mbedtls_test_cas[] = {
NULL
};
const size_t mbedtls_test_cas_len[] = {
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA1)
#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_1)
sizeof(mbedtls_test_ca_crt_rsa_sha1),
#endif
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
#if defined(MBEDTLS_RSA_C) && \
(defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256))
sizeof(mbedtls_test_ca_crt_rsa_sha256),
#endif
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Expand All @@ -433,12 +437,13 @@ const size_t mbedtls_test_cas_len[] = {
/* List of all available CA certificates in DER format */
const unsigned char *mbedtls_test_cas_der[] = {
#if defined(MBEDTLS_RSA_C)
#if defined(MBEDTLS_MD_CAN_SHA256)
#if (defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256))
mbedtls_test_ca_crt_rsa_sha256_der,
#endif /* MBEDTLS_MD_CAN_SHA256 */
#if defined(MBEDTLS_MD_CAN_SHA1)
#endif /* PSA_WANT_ALG_SHA_256 */
#if defined(PSA_WANT_ALG_SHA_1) || defined(MBEDTLS_MD_CAN_SHA1)
mbedtls_test_ca_crt_rsa_sha1_der,
#endif /* MBEDTLS_MD_CAN_SHA1 */
#endif /* PSA_WANT_ALG_SHA_1 || MBEDTLS_MD_CAN_SHA1 */
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
mbedtls_test_ca_crt_ec_der,
Expand All @@ -448,12 +453,13 @@ const unsigned char *mbedtls_test_cas_der[] = {

const size_t mbedtls_test_cas_der_len[] = {
#if defined(MBEDTLS_RSA_C)
#if defined(MBEDTLS_MD_CAN_SHA256)
#if defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256)
sizeof(mbedtls_test_ca_crt_rsa_sha256_der),
#endif /* MBEDTLS_MD_CAN_SHA256 */
#if defined(MBEDTLS_MD_CAN_SHA1)
#endif /* PSA_WANT_ALG_SHA_256 */
#if defined(PSA_WANT_ALG_SHA_1) || defined(MBEDTLS_MD_CAN_SHA1)
sizeof(mbedtls_test_ca_crt_rsa_sha1_der),
#endif /* MBEDTLS_MD_CAN_SHA1 */
#endif /* PSA_WANT_ALG_SHA_1 || MBEDTLS_MD_CAN_SHA1 */
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
sizeof(mbedtls_test_ca_crt_ec_der),
Expand All @@ -465,12 +471,13 @@ const size_t mbedtls_test_cas_der_len[] = {
#if defined(MBEDTLS_PEM_PARSE_C)
const char mbedtls_test_cas_pem[] =
#if defined(MBEDTLS_RSA_C)
#if defined(MBEDTLS_MD_CAN_SHA256)
#if defined(MBEDTLS_MD_CAN_SHA256) \
|| defined(PSA_WANT_ALG_SHA256)
TEST_CA_CRT_RSA_SHA256_PEM
#endif /* MBEDTLS_MD_CAN_SHA256 */
#if defined(MBEDTLS_MD_CAN_SHA1)
#endif /* PSA_WANT_ALG_SHA_256 */
#if defined(PSA_WANT_ALG_SHA_1) || defined(MBEDTLS_MD_CAN_SHA1)
TEST_CA_CRT_RSA_SHA1_PEM
#endif /* MBEDTLS_MD_CAN_SHA1 */
#endif /* PSA_WANT_ALG_SHA_1 || MBEDTLS_MD_CAN_SHA1 */
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
TEST_CA_CRT_EC_PEM
Expand Down
4 changes: 4 additions & 0 deletions tests/src/drivers/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#include "test/drivers/hash.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_hash.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_hash.h"
#endif
#endif

mbedtls_test_driver_hash_hooks_t
Expand Down
4 changes: 4 additions & 0 deletions tests/src/drivers/test_driver_aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include "mbedtls/constant_time.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_aead.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_aead.h"
#endif
#endif

mbedtls_test_driver_aead_hooks_t
Expand Down
4 changes: 4 additions & 0 deletions tests/src/drivers/test_driver_asymmetric_encryption.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include "test/drivers/key_management.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_rsa.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_rsa.h"
#endif
#endif

#define PSA_RSA_KEY_PAIR_MAX_SIZE \
Expand Down
4 changes: 4 additions & 0 deletions tests/src/drivers/test_driver_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
#include "test/random.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_cipher.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_cipher.h"
#endif
#endif

#include <string.h>
Expand Down
6 changes: 6 additions & 0 deletions tests/src/drivers/test_driver_key_agreement.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
#include <string.h>

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/include/psa/crypto.h"
#include "libtestdriver1/library/psa_crypto_ecp.h"
#include "libtestdriver1/library/psa_crypto_ffdh.h"
#else
#include "libtestdriver1/tf-psa-crypto/include/psa/crypto.h"
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_ecp.h"
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_ffdh.h"
#endif
#endif

mbedtls_test_driver_key_agreement_hooks_t
Expand Down
6 changes: 6 additions & 0 deletions tests/src/drivers/test_driver_key_management.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
#include "test/random.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_ecp.h"
#include "libtestdriver1/library/psa_crypto_rsa.h"
#include "libtestdriver1/library/psa_crypto_ffdh.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_ecp.h"
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_rsa.h"
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_ffdh.h"
#endif
#endif

#include <string.h>
Expand Down
4 changes: 4 additions & 0 deletions tests/src/drivers/test_driver_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#include "test/drivers/mac.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_mac.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_mac.h"
#endif
#endif

mbedtls_test_driver_mac_hooks_t mbedtls_test_driver_mac_hooks =
Expand Down
6 changes: 5 additions & 1 deletion tests/src/drivers/test_driver_pake.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Test driver for MAC entry points.
* Test driver for PAKE entry points.
*/
/* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Expand All @@ -14,7 +14,11 @@
#include "string.h"

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
#if MBEDTLS_VERSION_MAJOR < 4
#include "libtestdriver1/library/psa_crypto_pake.h"
#else
#include "libtestdriver1/tf-psa-crypto/core/psa_crypto_pake.h"
#endif
#endif

mbedtls_test_driver_pake_hooks_t mbedtls_test_driver_pake_hooks =
Expand Down
Loading