Add generate nonce test
Signed-off-by: Paul Elliott <paul.elliott@arm.com>
diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function
index e420158..35b9760 100644
--- a/tests/suites/test_suite_psa_crypto.function
+++ b/tests/suites/test_suite_psa_crypto.function
@@ -3176,8 +3176,6 @@
size_t key_bits = 0;
size_t tag_length = 0;
size_t tag_size = 0;
- size_t nonce_length = 0;
- uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
uint32_t part_offset = 0;
size_t part_length = 0;
@@ -3228,17 +3226,7 @@
PSA_ASSERT( status );
- if( nonce->len == 0 )
- {
- PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
- sizeof( nonce_buffer ),
- &nonce_length ) );
- }
- else
- {
- nonce_length = nonce->len;
- PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
- }
+ PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
if( operation.alg == PSA_ALG_GCM )
@@ -3450,17 +3438,8 @@
goto exit;
}
- if( nonce->len == 0 )
- {
- status = psa_aead_generate_nonce( &operation, nonce_buffer,
- sizeof( nonce_buffer ),
- &nonce_length );
- }
- else
- {
- nonce_length = nonce->len;
- status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
- }
+ nonce_length = nonce->len;
+ status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
if( status != PSA_SUCCESS )
{
@@ -3797,8 +3776,6 @@
size_t output_length = 0;
size_t key_bits = 0;
size_t tag_length = 0;
- size_t nonce_length = 0;
- uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
uint32_t part_offset = 0;
size_t part_length = 0;
size_t output_part_length = 0;
@@ -3849,17 +3826,7 @@
goto exit;
}
- if( nonce->len == 0 )
- {
- status = psa_aead_generate_nonce( &operation, nonce_buffer,
- sizeof( nonce_buffer ),
- &nonce_length );
- }
- else
- {
- nonce_length = nonce->len;
- status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
- }
+ status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
if( status != PSA_SUCCESS )
{
@@ -4023,6 +3990,63 @@
/* END_CASE */
/* BEGIN_CASE */
+void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
+ int alg_arg,
+ int nonce_len,
+ int expected_result_arg )
+{
+
+ mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
+ psa_key_type_t key_type = key_type_arg;
+ psa_algorithm_t alg = alg_arg;
+ psa_aead_operation_t operation;
+ uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
+ psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
+ psa_status_t status = PSA_ERROR_GENERIC_ERROR;
+ size_t nonce_generated_len = 0;
+
+ PSA_ASSERT( psa_crypto_init( ) );
+
+ psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
+ psa_set_key_algorithm( & attributes, alg );
+ psa_set_key_type( & attributes, key_type );
+
+ PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
+ &key ) );
+
+ PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
+
+ operation = psa_aead_operation_init( );
+
+ status = psa_aead_encrypt_setup( &operation, key, alg );
+
+ /* If the operation is not supported, just skip and not fail in case the
+ * encryption involves a common limitation of cryptography hardwares and
+ * an alternative implementation. */
+ if( status == PSA_ERROR_NOT_SUPPORTED )
+ {
+ MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
+ MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
+ }
+
+ PSA_ASSERT( status );
+
+ TEST_ASSERT( nonce_len < PSA_AEAD_NONCE_MAX_SIZE );
+
+ status = psa_aead_generate_nonce( &operation, nonce_buffer,
+ nonce_len,
+ &nonce_generated_len );
+
+ TEST_ASSERT( status == expected_result_arg );
+
+exit:
+ psa_destroy_key( key );
+ psa_aead_abort( &operation );
+ PSA_DONE( );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
void signature_size( int type_arg,
int bits,
int alg_arg,