From 8287e6b078749249c298af86841883e54ebc8d59 Mon Sep 17 00:00:00 2001 From: Ronald Cron Date: Fri, 12 Mar 2021 10:35:18 +0100 Subject: [PATCH] psa: cipher: Add utility functions Isolate the Mbed TLS cipher driver interfaces. Do the actual cipher operations in utility functions that are just called by the interface functions. The utility functions are intended to be also called by the cipher test driver interface functions (to be introduced subsequently) and allow to test the case where cipher operations are fully accelerated with no fallback (component test_psa_crypto_config_basic of all.sh). Signed-off-by: Ronald Cron --- library/psa_crypto_cipher.c | 111 +++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 27 deletions(-) diff --git a/library/psa_crypto_cipher.c b/library/psa_crypto_cipher.c index e86aa9548..147ce815d 100644 --- a/library/psa_crypto_cipher.c +++ b/library/psa_crypto_cipher.c @@ -119,7 +119,7 @@ exit: return( mbedtls_to_psa_error( ret ) ); } -psa_status_t mbedtls_psa_cipher_encrypt_setup( +static psa_status_t cipher_encrypt_setup( mbedtls_psa_cipher_operation_t *operation, const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, @@ -130,7 +130,7 @@ psa_status_t mbedtls_psa_cipher_encrypt_setup( alg, MBEDTLS_ENCRYPT ) ); } -psa_status_t mbedtls_psa_cipher_decrypt_setup( +static psa_status_t cipher_decrypt_setup( mbedtls_psa_cipher_operation_t *operation, const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, @@ -141,7 +141,18 @@ psa_status_t mbedtls_psa_cipher_decrypt_setup( alg, MBEDTLS_DECRYPT ) ); } -psa_status_t mbedtls_psa_cipher_generate_iv( +static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, + const uint8_t *iv, size_t iv_length ) +{ + if( iv_length != operation->iv_size ) + return( PSA_ERROR_INVALID_ARGUMENT ); + + return( mbedtls_to_psa_error( + mbedtls_cipher_set_iv( &operation->cipher, + iv, iv_length ) ) ); +} + +static psa_status_t cipher_generate_iv( mbedtls_psa_cipher_operation_t *operation, uint8_t *iv, size_t iv_size, size_t *iv_length ) { @@ -157,19 +168,7 @@ psa_status_t mbedtls_psa_cipher_generate_iv( *iv_length = operation->iv_size; - return( mbedtls_psa_cipher_set_iv( operation, iv, *iv_length ) ); -} - -psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, - const uint8_t *iv, - size_t iv_length ) -{ - if( iv_length != operation->iv_size ) - return( PSA_ERROR_INVALID_ARGUMENT ); - - return( mbedtls_to_psa_error( - mbedtls_cipher_set_iv( &operation->cipher, - iv, iv_length ) ) ); + return( cipher_set_iv( operation, iv, *iv_length ) ); } /* Process input for which the algorithm is set to ECB mode. This requires @@ -260,12 +259,12 @@ exit: return( status ); } -psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation, - const uint8_t *input, - size_t input_length, - uint8_t *output, - size_t output_size, - size_t *output_length ) +static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation, + const uint8_t *input, + size_t input_length, + uint8_t *output, + size_t output_size, + size_t *output_length ) { psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; size_t expected_output_size; @@ -310,10 +309,10 @@ psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operatio return( status ); } -psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation, - uint8_t *output, - size_t output_size, - size_t *output_length ) +static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation, + uint8_t *output, + size_t output_size, + size_t *output_length ) { psa_status_t status = PSA_ERROR_GENERIC_ERROR; uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; @@ -349,7 +348,7 @@ exit: return( status ); } -psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation ) +static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation ) { /* Sanity check (shouldn't happen: operation->alg should * always have been initialized to a valid value). */ @@ -361,4 +360,62 @@ psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation return( PSA_SUCCESS ); } +psa_status_t mbedtls_psa_cipher_encrypt_setup( + mbedtls_psa_cipher_operation_t *operation, + const psa_key_attributes_t *attributes, + const uint8_t *key_buffer, size_t key_buffer_size, + psa_algorithm_t alg ) +{ + return( cipher_encrypt_setup( + operation, attributes, key_buffer, key_buffer_size, alg ) ); +} + +psa_status_t mbedtls_psa_cipher_decrypt_setup( + mbedtls_psa_cipher_operation_t *operation, + const psa_key_attributes_t *attributes, + const uint8_t *key_buffer, size_t key_buffer_size, + psa_algorithm_t alg ) +{ + return( cipher_decrypt_setup( + operation, attributes, key_buffer, key_buffer_size, alg ) ); +} + +psa_status_t mbedtls_psa_cipher_generate_iv( + mbedtls_psa_cipher_operation_t *operation, + uint8_t *iv, size_t iv_size, size_t *iv_length ) +{ + return( cipher_generate_iv( operation, iv, iv_size, iv_length ) ); +} + +psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, + const uint8_t *iv, + size_t iv_length ) +{ + return( cipher_set_iv( operation, iv, iv_length ) ); +} + +psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation, + const uint8_t *input, + size_t input_length, + uint8_t *output, + size_t output_size, + size_t *output_length ) +{ + return( cipher_update( operation, input, input_length, + output, output_size, output_length ) ); +} + +psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation, + uint8_t *output, + size_t output_size, + size_t *output_length ) +{ + return( cipher_finish( operation, output, output_size, output_length ) ); +} + +psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation ) +{ + return( cipher_abort( operation ) ); +} + #endif /* MBEDTLS_PSA_CRYPTO_C */