Dispatch hashing calls through the driver wrapper layer

Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
diff --git a/library/psa_crypto_driver_wrappers.c b/library/psa_crypto_driver_wrappers.c
index 6cf23ce..72077ac 100644
--- a/library/psa_crypto_driver_wrappers.c
+++ b/library/psa_crypto_driver_wrappers.c
@@ -38,14 +38,17 @@
 
 /* Repeat above block for each JSON-declared driver during autogeneration */
 
-/* Auto-generated values depending on which drivers are registered. ID 0 is
- * reserved for unallocated operations. */
+/* Auto-generated values depending on which drivers are registered.
+ * ID 0 is reserved for unallocated operations.
+ * ID 1 is reserved for the Mbed TLS software driver. */
 #if defined(PSA_CRYPTO_DRIVER_TEST)
-#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (1)
-#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (2)
+#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
+#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
 #endif /* PSA_CRYPTO_DRIVER_TEST */
 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
 
+#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
+
 /* Support the 'old' SE interface when asked to */
 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
 /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
@@ -56,6 +59,9 @@
 #include "psa_crypto_se.h"
 #endif
 
+/* Include software fallback when present */
+#include "psa_crypto_hash.h"
+
 /* Start delegation functions */
 psa_status_t psa_driver_wrapper_sign_hash(
     const psa_key_attributes_t *attributes,
@@ -1066,4 +1072,207 @@
 #endif /* PSA_CRYPTO_DRIVER_PRESENT */
 }
 
+/*
+ * Hashing functions
+ */
+psa_status_t psa_driver_wrapper_hash_compute(
+    psa_algorithm_t alg,
+    const uint8_t *input,
+    size_t input_length,
+    uint8_t *hash,
+    size_t hash_size,
+    size_t *hash_length)
+{
+    psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
+
+    /* Try accelerators first */
+
+    /* If software fallback is compiled in, try fallback */
+#if defined(MBEDTLS_PSA_BUILTIN_HASH)
+    status = mbedtls_psa_hash_compute( alg, input, input_length,
+                                       hash, hash_size, hash_length );
+    if( status != PSA_ERROR_NOT_SUPPORTED )
+        return( status );
+#endif
+    if( status == PSA_ERROR_NOT_SUPPORTED )
+    {
+        (void) alg;
+        (void) input;
+        (void) input_length;
+        (void) hash;
+        (void) hash_size;
+        (void) hash_length;
+
+        return( PSA_ERROR_NOT_SUPPORTED );
+    }
+    return( status );
+}
+
+psa_status_t psa_driver_wrapper_hash_setup(
+    psa_operation_driver_context_t *operation,
+    psa_algorithm_t alg )
+{
+    psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
+
+    /* A context must be freshly initialized before it can be set up. */
+    if( operation->id != 0 || operation->ctx != NULL )
+        return( PSA_ERROR_BAD_STATE );
+
+    /* Try setup on accelerators first */
+
+    /* If software fallback is compiled in, try fallback */
+#if defined(MBEDTLS_PSA_BUILTIN_HASH)
+    operation->ctx = mbedtls_calloc( 1, sizeof(mbedtls_psa_hash_operation_t) );
+    status = mbedtls_psa_hash_setup( operation->ctx, alg );
+    if( status == PSA_SUCCESS )
+    {
+        operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
+    }
+    else
+    {
+        mbedtls_free( operation->ctx );
+        operation->ctx = NULL;
+        operation->id = 0;
+    }
+
+    if( status != PSA_ERROR_NOT_SUPPORTED )
+        return( status );
+#endif
+    /* Nothing left to try if we fall through here */
+    (void) status;
+    (void) operation;
+    (void) alg;
+    return( PSA_ERROR_NOT_SUPPORTED );
+}
+
+psa_status_t psa_driver_wrapper_hash_clone(
+    const psa_operation_driver_context_t *source_operation,
+    psa_operation_driver_context_t *target_operation )
+{
+    psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
+
+    if( source_operation->ctx == NULL || source_operation->id == 0 )
+        return( PSA_ERROR_BAD_STATE );
+    if( target_operation->ctx != NULL || target_operation->id != 0 )
+        return( PSA_ERROR_BAD_STATE );
+
+    switch( source_operation->id )
+    {
+#if defined(MBEDTLS_PSA_BUILTIN_HASH)
+        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
+            target_operation->ctx = mbedtls_calloc( 1, sizeof(mbedtls_psa_hash_operation_t) );
+            if( target_operation->ctx == NULL )
+            {
+                status = PSA_ERROR_INSUFFICIENT_MEMORY;
+                break;
+            }
+            target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
+            status = mbedtls_psa_hash_clone( source_operation->ctx,
+                                             target_operation->ctx );
+            break;
+#endif
+        default:
+            (void) status;
+            (void) source_operation;
+            (void) target_operation;
+            return( PSA_ERROR_BAD_STATE );
+    }
+
+    if( status != PSA_SUCCESS )
+        psa_driver_wrapper_hash_abort( target_operation );
+    return( status );
+}
+
+psa_status_t psa_driver_wrapper_hash_update(
+    psa_operation_driver_context_t *operation,
+    const uint8_t *input,
+    size_t input_length )
+{
+    psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
+
+    if( operation->ctx == NULL || operation->id == 0 )
+        return( PSA_ERROR_BAD_STATE );
+
+    switch( operation->id )
+    {
+#if defined(MBEDTLS_PSA_BUILTIN_HASH)
+        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
+            status = mbedtls_psa_hash_update( operation->ctx,
+                                              input, input_length );
+            break;
+#endif
+        default:
+            (void) status;
+            (void) operation;
+            (void) input;
+            (void) input_length;
+            return( PSA_ERROR_BAD_STATE );
+    }
+
+    if( status != PSA_SUCCESS )
+        psa_driver_wrapper_hash_abort( operation );
+    return( status );
+}
+
+psa_status_t psa_driver_wrapper_hash_finish(
+    psa_operation_driver_context_t *operation,
+    uint8_t *hash,
+    size_t hash_size,
+    size_t *hash_length )
+{
+    psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
+
+    if( operation->ctx == NULL || operation->id == 0 )
+        return( PSA_ERROR_BAD_STATE );
+
+    switch( operation->id )
+    {
+#if defined(MBEDTLS_PSA_BUILTIN_HASH)
+        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
+            status = mbedtls_psa_hash_finish( operation->ctx,
+                                              hash, hash_size, hash_length );
+            break;
+#endif
+        default:
+            (void) status;
+            (void) operation;
+            (void) hash;
+            (void) hash_size;
+            (void) hash_length;
+            return( PSA_ERROR_BAD_STATE );
+    }
+
+    psa_driver_wrapper_hash_abort( operation );
+    return( status );
+}
+
+psa_status_t psa_driver_wrapper_hash_abort(
+    psa_operation_driver_context_t *operation )
+{
+    psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
+
+    switch( operation->id )
+    {
+        case 0:
+            if( operation->ctx == NULL )
+                return( PSA_SUCCESS );
+            else
+                return( PSA_ERROR_BAD_STATE );
+#if defined(MBEDTLS_PSA_BUILTIN_HASH)
+        case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
+            if( operation->ctx != NULL )
+            {
+                status = mbedtls_psa_hash_abort( operation->ctx );
+                mbedtls_free( operation->ctx );
+                operation->ctx = NULL;
+            }
+            operation->id = 0;
+            return( PSA_SUCCESS );
+#endif
+        default:
+            (void) status;
+            return( PSA_ERROR_BAD_STATE );
+    }
+}
+
 /* End of automatically generated file. */