psa: Call cipher operations software implementations as a driver

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index 7ecf32e..22dce5d 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -3488,20 +3488,10 @@
         return( PSA_ERROR_BAD_STATE );
     }
 
-    if( operation->mbedtls_in_use == 0 )
-    {
-        status = psa_driver_wrapper_cipher_generate_iv( operation,
-                                                        iv,
-                                                        iv_size,
-                                                        iv_length );
-    }
-    else
-    {
-        status = mbedtls_psa_cipher_generate_iv( operation,
-                                                 iv,
-                                                 iv_size,
-                                                 iv_length );
-    }
+    status = psa_driver_wrapper_cipher_generate_iv( operation,
+                                                    iv,
+                                                    iv_size,
+                                                    iv_length );
 
     if( status == PSA_SUCCESS )
         operation->iv_set = 1;
@@ -3527,16 +3517,9 @@
         return( PSA_ERROR_BAD_STATE );
     }
 
-    if( operation->mbedtls_in_use == 0 )
-    {
-        status = psa_driver_wrapper_cipher_set_iv( operation,
-                                                   iv,
-                                                   iv_length );
-    }
-    else
-    {
-        status = mbedtls_psa_cipher_set_iv( operation, iv, iv_length );
-    }
+    status = psa_driver_wrapper_cipher_set_iv( operation,
+                                               iv,
+                                               iv_length );
 
     if( status == PSA_SUCCESS )
         operation->iv_set = 1;
@@ -3563,25 +3546,12 @@
         return( PSA_ERROR_BAD_STATE );
     }
 
-    if( operation->mbedtls_in_use == 0 )
-    {
-        status = psa_driver_wrapper_cipher_update( operation,
-                                                   input,
-                                                   input_length,
-                                                   output,
-                                                   output_size,
-                                                   output_length );
-    }
-    else
-    {
-        status = mbedtls_psa_cipher_update( operation,
-                                            input,
-                                            input_length,
-                                            output,
-                                            output_size,
-                                            output_length );
-    }
-
+    status = psa_driver_wrapper_cipher_update( operation,
+                                               input,
+                                               input_length,
+                                               output,
+                                               output_size,
+                                               output_length );
     if( status != PSA_SUCCESS )
         psa_cipher_abort( operation );
 
@@ -3604,21 +3574,10 @@
         return( PSA_ERROR_BAD_STATE );
     }
 
-    if( operation->mbedtls_in_use == 0 )
-    {
-        status = psa_driver_wrapper_cipher_finish( operation,
-                                                   output,
-                                                   output_size,
-                                                   output_length );
-    }
-    else
-    {
-        status = mbedtls_psa_cipher_finish( operation,
-                                            output,
-                                            output_size,
-                                            output_length );
-    }
-    
+    status = psa_driver_wrapper_cipher_finish( operation,
+                                               output,
+                                               output_size,
+                                               output_length );
     if( status == PSA_SUCCESS )
         return( psa_cipher_abort( operation ) );
     else
@@ -3645,10 +3604,7 @@
     if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
         return( PSA_ERROR_BAD_STATE );
 
-    if( operation->mbedtls_in_use == 0 )
-        psa_driver_wrapper_cipher_abort( operation );
-    else
-        mbedtls_psa_cipher_abort( operation );
+    psa_driver_wrapper_cipher_abort( operation );
 
     operation->alg = 0;
     operation->key_set = 0;
diff --git a/library/psa_crypto_driver_wrappers.c b/library/psa_crypto_driver_wrappers.c
index f8a1c52..883b944 100644
--- a/library/psa_crypto_driver_wrappers.c
+++ b/library/psa_crypto_driver_wrappers.c
@@ -895,7 +895,13 @@
     size_t iv_size,
     size_t *iv_length )
 {
-#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
+    if( operation->mbedtls_in_use )
+        return( mbedtls_psa_cipher_generate_iv( operation,
+                                                iv,
+                                                iv_size,
+                                                iv_length ) );
+
+#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
     switch( operation->ctx.driver.id )
     {
 #if defined(PSA_CRYPTO_DRIVER_TEST)
@@ -906,6 +912,7 @@
                         iv_size,
                         iv_length ) );
 #endif /* PSA_CRYPTO_DRIVER_TEST */
+
 #if defined(PSA_CRYPTO_DRIVER_TEST)
         case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
             return( test_opaque_cipher_generate_iv(
@@ -914,18 +921,10 @@
                         iv_size,
                         iv_length ) );
 #endif /* PSA_CRYPTO_DRIVER_TEST */
-        default:
-            /* Key is attached to a driver not known to us */
-            return( PSA_ERROR_INVALID_ARGUMENT );
     }
-#else /* PSA_CRYPTO_DRIVER_PRESENT */
-    (void) operation;
-    (void) iv;
-    (void) iv_size;
-    (void) iv_length;
+#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
 
-    return( PSA_ERROR_NOT_SUPPORTED );
-#endif /* PSA_CRYPTO_DRIVER_PRESENT */
+    return( PSA_ERROR_INVALID_ARGUMENT );
 }
 
 psa_status_t psa_driver_wrapper_cipher_set_iv(
@@ -933,7 +932,12 @@
     const uint8_t *iv,
     size_t iv_length )
 {
-#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
+    if( operation->mbedtls_in_use )
+        return( mbedtls_psa_cipher_set_iv( operation,
+                                           iv,
+                                           iv_length ) );
+
+#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
     switch( operation->ctx.driver.id )
     {
 #if defined(PSA_CRYPTO_DRIVER_TEST)
@@ -948,17 +952,10 @@
                                                iv,
                                                iv_length ) );
 #endif /* PSA_CRYPTO_DRIVER_TEST */
-        default:
-            /* Key is attached to a driver not known to us */
-            return( PSA_ERROR_INVALID_ARGUMENT );
     }
-#else /* PSA_CRYPTO_DRIVER_PRESENT */
-    (void) operation;
-    (void) iv;
-    (void) iv_length;
+#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
 
-    return( PSA_ERROR_NOT_SUPPORTED );
-#endif /* PSA_CRYPTO_DRIVER_PRESENT */
+    return( PSA_ERROR_INVALID_ARGUMENT );
 }
 
 psa_status_t psa_driver_wrapper_cipher_update(
@@ -969,7 +966,15 @@
     size_t output_size,
     size_t *output_length )
 {
-#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
+    if( operation->mbedtls_in_use )
+        return( mbedtls_psa_cipher_update( operation,
+                                           input,
+                                           input_length,
+                                           output,
+                                           output_size,
+                                           output_length ) );
+
+#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
     switch( operation->ctx.driver.id )
     {
 #if defined(PSA_CRYPTO_DRIVER_TEST)
@@ -990,20 +995,10 @@
                                                output_size,
                                                output_length ) );
 #endif /* PSA_CRYPTO_DRIVER_TEST */
-        default:
-            /* Key is attached to a driver not known to us */
-            return( PSA_ERROR_INVALID_ARGUMENT );
     }
-#else /* PSA_CRYPTO_DRIVER_PRESENT */
-    (void) operation;
-    (void) input;
-    (void) input_length;
-    (void) output;
-    (void) output_length;
-    (void) output_size;
+#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
 
-    return( PSA_ERROR_NOT_SUPPORTED );
-#endif /* PSA_CRYPTO_DRIVER_PRESENT */
+    return( PSA_ERROR_INVALID_ARGUMENT );
 }
 
 psa_status_t psa_driver_wrapper_cipher_finish(
@@ -1012,7 +1007,13 @@
     size_t output_size,
     size_t *output_length )
 {
-#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
+    if( operation->mbedtls_in_use )
+        return( mbedtls_psa_cipher_finish( operation,
+                                           output,
+                                           output_size,
+                                           output_length ) );
+
+#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
     switch( operation->ctx.driver.id )
     {
 #if defined(PSA_CRYPTO_DRIVER_TEST)
@@ -1029,25 +1030,20 @@
                                                output_size,
                                                output_length ) );
 #endif /* PSA_CRYPTO_DRIVER_TEST */
-        default:
-            /* Key is attached to a driver not known to us */
-            return( PSA_ERROR_INVALID_ARGUMENT );
     }
-#else /* PSA_CRYPTO_DRIVER_PRESENT */
-    (void) operation;
-    (void) output;
-    (void) output_size;
-    (void) output_length;
+#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
 
-    return( PSA_ERROR_NOT_SUPPORTED );
-#endif /* PSA_CRYPTO_DRIVER_PRESENT */
+    return( PSA_ERROR_INVALID_ARGUMENT );
 }
 
 psa_status_t psa_driver_wrapper_cipher_abort(
     psa_cipher_operation_t *operation )
 {
-#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
-    psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
+    if( operation->mbedtls_in_use )
+        return( mbedtls_psa_cipher_abort( operation ) );
+
+#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
+    psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
     psa_operation_driver_context_t *driver_context = &operation->ctx.driver;
 
     /* The object has (apparently) been initialized but it is not in use. It's
@@ -1081,15 +1077,10 @@
 
             return( status );
 #endif /* PSA_CRYPTO_DRIVER_TEST */
-        default:
-            /* Operation is attached to a driver not known to us */
-            return( PSA_ERROR_INVALID_ARGUMENT );
     }
-#else /* PSA_CRYPTO_DRIVER_PRESENT */
-    (void)operation;
+#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
 
-    return( PSA_ERROR_NOT_SUPPORTED );
-#endif /* PSA_CRYPTO_DRIVER_PRESENT */
+    return( PSA_ERROR_INVALID_ARGUMENT );
 }
 
 /*