tests: driver wrapper: Add hash dispatch testing

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
diff --git a/tests/suites/test_suite_psa_crypto_driver_wrappers.data b/tests/suites/test_suite_psa_crypto_driver_wrappers.data
index 2513883..64e6023 100644
--- a/tests/suites/test_suite_psa_crypto_driver_wrappers.data
+++ b/tests/suites/test_suite_psa_crypto_driver_wrappers.data
@@ -267,3 +267,43 @@
 
 PSA opaque driver builtin pubkey export: not a public key
 builtin_pubkey_export:MBEDTLS_PSA_KEY_ID_BUILTIN_MIN:PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1):256:PSA_ALG_ECDSA(PSA_ALG_ANY_HASH):"0485f64d89f00be66c88dd937efd6d7c445648dcb701150b8a9509295850f41c1931e571fb8f8c78317a20b380e866584bbc2516c3d2702d792f131a922095fd6c":PSA_ERROR_INVALID_ARGUMENT
+
+Hash compute: SHA-256, computed by the driver
+depends_on:PSA_WANT_ALG_SHA_256
+hash_compute:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_SUCCESS:PSA_SUCCESS
+
+Hash compute: SHA-256, fallback
+depends_on:MBEDTLS_PSA_BUILTIN_ALG_SHA_256
+hash_compute:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_NOT_SUPPORTED:PSA_SUCCESS
+
+Hash compute: SHA-256, no fallback
+depends_on:!MBEDTLS_PSA_BUILTIN_ALG_SHA_256
+hash_compute:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_NOT_SUPPORTED:PSA_ERROR_NOT_SUPPORTED
+
+Hash compute: SHA-256, INSUFFICIENT_MEMORY
+depends_on:MBEDTLS_PSA_BUILTIN_ALG_SHA_256
+hash_compute:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_INSUFFICIENT_MEMORY::PSA_ERROR_INSUFFICIENT_MEMORY
+
+Hash multi-part: SHA-256, computed by the driver
+depends_on:PSA_WANT_ALG_SHA_256
+hash_multipart:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_SUCCESS:PSA_SUCCESS
+
+Hash multi-part: SHA-256, fallback
+depends_on:MBEDTLS_PSA_BUILTIN_ALG_SHA_256
+hash_multipart:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_NOT_SUPPORTED:PSA_SUCCESS
+
+Hash multi-part: SHA-256, no fallback
+depends_on:!MBEDTLS_PSA_BUILTIN_ALG_SHA_256
+hash_multipart:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_NOT_SUPPORTED:PSA_ERROR_NOT_SUPPORTED
+
+Hash multi-part: SHA-256, INSUFFICIENT_MEMORY
+depends_on:MBEDTLS_PSA_BUILTIN_ALG_SHA_256
+hash_compute:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_INSUFFICIENT_MEMORY::PSA_ERROR_INSUFFICIENT_MEMORY
+
+Hash clone: SHA-256, clone successful
+depends_on:PSA_WANT_ALG_SHA_256
+hash_clone:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_SUCCESS
+
+Hash clone: SHA-256, clone failure
+depends_on:PSA_WANT_ALG_SHA_256
+hash_clone:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803":PSA_ERROR_NOT_SUPPORTED
diff --git a/tests/suites/test_suite_psa_crypto_driver_wrappers.function b/tests/suites/test_suite_psa_crypto_driver_wrappers.function
index 8ccce06..f56c68b 100644
--- a/tests/suites/test_suite_psa_crypto_driver_wrappers.function
+++ b/tests/suites/test_suite_psa_crypto_driver_wrappers.function
@@ -1046,3 +1046,201 @@
     PSA_DONE( );
 }
 /* END_CASE */
+
+/* BEGIN_CASE */
+void hash_compute( int alg_arg,
+                   data_t *input, data_t *hash,
+                   int forced_status_arg,
+                   int expected_status_arg )
+{
+    psa_algorithm_t alg = alg_arg;
+    psa_status_t forced_status = forced_status_arg;
+    psa_status_t expected_status = expected_status_arg;
+    unsigned char *output = NULL;
+    size_t output_length;
+
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+    test_driver_hash_hooks.forced_status = forced_status;
+
+    PSA_ASSERT( psa_crypto_init( ) );
+    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
+
+    TEST_EQUAL( psa_hash_compute( alg, input->x, input->len,
+                                  output, PSA_HASH_LENGTH( alg ),
+                                  &output_length ), expected_status );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 1 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+    if( expected_status == PSA_SUCCESS )
+    {
+        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
+    }
+
+exit:
+    mbedtls_free( output );
+    PSA_DONE( );
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void hash_multipart( int alg_arg,
+                     data_t *input, data_t *hash,
+                     int forced_status_arg,
+                     int expected_status_arg )
+{
+    psa_algorithm_t alg = alg_arg;
+    psa_status_t forced_status = forced_status_arg;
+    psa_status_t expected_status = expected_status_arg;
+    unsigned char *output = NULL;
+    psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
+    size_t output_length;
+
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
+
+    PSA_ASSERT( psa_crypto_init( ) );
+
+    /*
+     * Case 1: Force the driver return status for setup.
+     */
+    test_driver_hash_hooks.forced_status = forced_status;
+    TEST_EQUAL( psa_hash_setup( &operation, alg ), expected_status );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 1 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+    if( expected_status == PSA_SUCCESS )
+    {
+        PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
+        TEST_EQUAL( test_driver_hash_hooks.hits,
+                    forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 2 );
+        TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+        PSA_ASSERT( psa_hash_finish( &operation,
+                                     output, PSA_HASH_LENGTH( alg ),
+                                     &output_length ) );
+        TEST_EQUAL( test_driver_hash_hooks.hits,
+                    forced_status == PSA_ERROR_NOT_SUPPORTED ? 1 : 4 );
+        TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
+    }
+
+    /*
+     * Case 2: Force the driver return status for update.
+     */
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+    PSA_ASSERT( psa_hash_setup( &operation, alg ) );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 1 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, PSA_SUCCESS );
+
+    test_driver_hash_hooks.forced_status = forced_status;
+    TEST_EQUAL( psa_hash_update( &operation, input->x, input->len ),
+                                 forced_status );
+    TEST_EQUAL( test_driver_hash_hooks.hits,
+                forced_status != PSA_SUCCESS ? 3 : 2 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+    if( forced_status == PSA_SUCCESS )
+    {
+        PSA_ASSERT( psa_hash_finish( &operation,
+                                     output, PSA_HASH_LENGTH( alg ),
+                                     &output_length ) );
+        TEST_EQUAL( test_driver_hash_hooks.hits, 4 );
+        TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
+    }
+
+    /*
+     * Case 3: Force the driver return status for finish.
+     */
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+    PSA_ASSERT( psa_hash_setup( &operation, alg ) );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 1 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, PSA_SUCCESS );
+
+    PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 2 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, PSA_SUCCESS );
+
+    test_driver_hash_hooks.forced_status = forced_status;
+    TEST_EQUAL( psa_hash_finish( &operation,
+                                 output, PSA_HASH_LENGTH( alg ),
+                                 &output_length ), forced_status );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 4 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+    if( forced_status == PSA_SUCCESS )
+    {
+        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
+    }
+
+exit:
+    psa_hash_abort( &operation );
+    mbedtls_free( output );
+    PSA_DONE( );
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void hash_clone( int alg_arg,
+                 data_t *input, data_t *hash,
+                 int forced_status_arg )
+{
+    psa_algorithm_t alg = alg_arg;
+    psa_status_t forced_status = forced_status_arg;
+    unsigned char *output = NULL;
+    psa_hash_operation_t source_operation = PSA_HASH_OPERATION_INIT;
+    psa_hash_operation_t target_operation = PSA_HASH_OPERATION_INIT;
+    size_t output_length;
+
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+    ASSERT_ALLOC( output, PSA_HASH_LENGTH( alg ) );
+
+    PSA_ASSERT( psa_crypto_init( ) );
+
+    /*
+     * Clone none active operation, the driver shouldn't be called.
+     */
+    TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ),
+                PSA_ERROR_BAD_STATE );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 0 );
+
+    PSA_ASSERT( psa_hash_setup( &source_operation, alg ) );
+    TEST_EQUAL( test_driver_hash_hooks.hits, 1 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, PSA_SUCCESS );
+
+    test_driver_hash_hooks.forced_status = forced_status;
+    TEST_EQUAL( psa_hash_clone( &source_operation, &target_operation ),
+                forced_status );
+    TEST_EQUAL( test_driver_hash_hooks.hits,
+                forced_status == PSA_SUCCESS ? 2 : 3 );
+    TEST_EQUAL( test_driver_hash_hooks.driver_status, forced_status );
+
+    if( forced_status == PSA_SUCCESS )
+    {
+        test_driver_hash_hooks = test_driver_hash_hooks_init();
+        PSA_ASSERT( psa_hash_update( &target_operation,
+                                     input->x, input->len ) );
+        TEST_EQUAL( test_driver_hash_hooks.hits, 1 );
+        TEST_EQUAL( test_driver_hash_hooks.driver_status, PSA_SUCCESS );
+
+        PSA_ASSERT( psa_hash_finish( &target_operation,
+                                     output, PSA_HASH_LENGTH( alg ),
+                                     &output_length ) );
+        TEST_EQUAL( test_driver_hash_hooks.hits, 3 );
+        TEST_EQUAL( test_driver_hash_hooks.driver_status, PSA_SUCCESS );
+
+        ASSERT_COMPARE( output, output_length, hash->x, hash->len );
+    }
+
+exit:
+    psa_hash_abort( &source_operation );
+    psa_hash_abort( &target_operation );
+    mbedtls_free( output );
+    PSA_DONE( );
+    test_driver_hash_hooks = test_driver_hash_hooks_init();
+}
+/* END_CASE */