Skip to content

Check union initialization in test drivers #168

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

Merged
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
17 changes: 17 additions & 0 deletions tests/include/test/drivers/test_driver_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@

#include "mbedtls/build_info.h"

/* Use the same formatting for error code definitions as the standard
* error values, which must have a specific sequence of tokens for
* interoperability between implementations of different parts of PSA.
* This means no space between the cast and the - operator.
* This contradicts our code style, so we temporarily disable style checking.
*
* *INDENT-OFF*
*/

/** Error code that test drivers return when they detect that an input
* parameter was not initialized properly. This normally indicates a
* bug in the core.
*/
#define PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION ((psa_status_t)-0x0201)

/* *INDENT-ON* */

#endif /* test_driver_common.h */
6 changes: 6 additions & 0 deletions tests/src/drivers/test_driver_aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
mbedtls_test_driver_aead_hooks.driver_status =
mbedtls_test_driver_aead_hooks.forced_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_aead_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
Expand Down Expand Up @@ -186,6 +189,9 @@ psa_status_t mbedtls_test_transparent_aead_decrypt_setup(
if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
mbedtls_test_driver_aead_hooks.driver_status =
mbedtls_test_driver_aead_hooks.forced_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_aead_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
Expand Down
14 changes: 8 additions & 6 deletions tests/src/drivers/test_driver_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,14 @@ psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
{
mbedtls_test_driver_cipher_hooks.hits++;

/* Wiping the entire struct here, instead of member-by-member. This is
* useful for the test suite, since it gives a chance of catching memory
* corruption errors should the core not have allocated (enough) memory for
* our context struct. */
memset(operation, 0, sizeof(*operation));

if (mbedtls_test_driver_cipher_hooks.forced_status != PSA_SUCCESS) {
return mbedtls_test_driver_cipher_hooks.forced_status;
}

if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
return PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
}

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
return libtestdriver1_mbedtls_psa_cipher_encrypt_setup(
Expand All @@ -175,6 +173,10 @@ psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
return mbedtls_test_driver_cipher_hooks.forced_status;
}

if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
return PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
}

#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_CIPHER)
return libtestdriver1_mbedtls_psa_cipher_decrypt_setup(
Expand Down
12 changes: 12 additions & 0 deletions tests/src/drivers/test_driver_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ psa_status_t mbedtls_test_transparent_mac_sign_setup(
if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
mbedtls_test_driver_mac_hooks.driver_status =
mbedtls_test_driver_mac_hooks.forced_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_mac_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
Expand Down Expand Up @@ -120,6 +123,9 @@ psa_status_t mbedtls_test_transparent_mac_verify_setup(
if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
mbedtls_test_driver_mac_hooks.driver_status =
mbedtls_test_driver_mac_hooks.forced_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_mac_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
Expand Down Expand Up @@ -309,6 +315,9 @@ psa_status_t mbedtls_test_opaque_mac_sign_setup(
if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
mbedtls_test_driver_mac_hooks.driver_status =
mbedtls_test_driver_mac_hooks.forced_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_mac_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
(void) operation;
(void) attributes;
Expand All @@ -333,6 +342,9 @@ psa_status_t mbedtls_test_opaque_mac_verify_setup(
if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
mbedtls_test_driver_mac_hooks.driver_status =
mbedtls_test_driver_mac_hooks.forced_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_mac_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
(void) operation;
(void) attributes;
Expand Down
3 changes: 3 additions & 0 deletions tests/src/drivers/test_driver_pake.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ psa_status_t mbedtls_test_transparent_pake_setup(
if (mbedtls_test_driver_pake_hooks.forced_setup_status != PSA_SUCCESS) {
mbedtls_test_driver_pake_hooks.driver_status =
mbedtls_test_driver_pake_hooks.forced_setup_status;
} else if (!MBEDTLS_TEST_OBJECT_IS_ALL_ZERO(operation)) {
mbedtls_test_driver_pake_hooks.driver_status =
PSA_ERROR_TEST_DETECTED_BAD_INITIALIZATION;
} else {
#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_PAKE)
Expand Down