Skip to content

Commit

Permalink
tests: Add AEAD transparent test driver hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
  • Loading branch information
ronald-cron-arm committed Apr 7, 2021
1 parent de82281 commit bfe551d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
19 changes: 19 additions & 0 deletions tests/include/test/drivers/aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
#if defined(PSA_CRYPTO_DRIVER_TEST)
#include <psa/crypto_driver_common.h>

typedef struct {
/* If not PSA_SUCCESS, return this error code instead of processing the
* function call. */
psa_status_t forced_status;
/* Count the amount of times AEAD driver functions are called. */
unsigned long hits;
/* Status returned by the last AEAD driver function call. */
psa_status_t driver_status;
} test_driver_aead_hooks_t;

#define TEST_DRIVER_AEAD_INIT { 0, 0, 0 }
static inline test_driver_aead_hooks_t test_driver_aead_hooks_init( void )
{
const test_driver_aead_hooks_t v = TEST_DRIVER_AEAD_INIT;
return( v );
}

extern test_driver_aead_hooks_t test_driver_aead_hooks;

psa_status_t test_transparent_aead_encrypt(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
Expand Down
36 changes: 32 additions & 4 deletions tests/src/drivers/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "test/drivers/aead.h"

test_driver_aead_hooks_t test_driver_aead_hooks = TEST_DRIVER_AEAD_INIT;

psa_status_t test_transparent_aead_encrypt(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
Expand All @@ -37,13 +39,26 @@ psa_status_t test_transparent_aead_encrypt(
const uint8_t *plaintext, size_t plaintext_length,
uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
{
return( mbedtls_psa_aead_encrypt(
test_driver_aead_hooks.hits++;

if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
{
test_driver_aead_hooks.driver_status =
test_driver_aead_hooks.forced_status;
}
else
{
test_driver_aead_hooks.driver_status =
mbedtls_psa_aead_encrypt(
attributes, key_buffer, key_buffer_size,
alg,
nonce, nonce_length,
additional_data, additional_data_length,
plaintext, plaintext_length,
ciphertext, ciphertext_size, ciphertext_length ) );
ciphertext, ciphertext_size, ciphertext_length );
}

return( test_driver_aead_hooks.driver_status );
}

psa_status_t test_transparent_aead_decrypt(
Expand All @@ -55,13 +70,26 @@ psa_status_t test_transparent_aead_decrypt(
const uint8_t *ciphertext, size_t ciphertext_length,
uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
{
return( mbedtls_psa_aead_decrypt(
test_driver_aead_hooks.hits++;

if( test_driver_aead_hooks.forced_status != PSA_SUCCESS )
{
test_driver_aead_hooks.driver_status =
test_driver_aead_hooks.forced_status;
}
else
{
test_driver_aead_hooks.driver_status =
mbedtls_psa_aead_decrypt(
attributes, key_buffer, key_buffer_size,
alg,
nonce, nonce_length,
additional_data, additional_data_length,
ciphertext, ciphertext_length,
plaintext, plaintext_size, plaintext_length ) );
plaintext, plaintext_size, plaintext_length );
}

return( test_driver_aead_hooks.driver_status );
}

#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */

0 comments on commit bfe551d

Please sign in to comment.