From 96b0173cec455571d89bb57d2c8b7c47500d9277 Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Fri, 16 Jul 2021 17:00:26 +0100 Subject: [PATCH] Add common nonce checking to oneshot encrypt Signed-off-by: Paul Elliott --- library/psa_crypto_aead.c | 40 +++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/library/psa_crypto_aead.c b/library/psa_crypto_aead.c index f2096ce3f2..9ac26467f1 100644 --- a/library/psa_crypto_aead.c +++ b/library/psa_crypto_aead.c @@ -136,6 +136,22 @@ static psa_status_t psa_aead_setup( return( PSA_SUCCESS ); } +/* Perform common nonce length checks */ +static psa_status_t mbedtls_aead_check_nonce_length( + mbedtls_psa_aead_operation_t *operation, + size_t nonce_length ) +{ +#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) + if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) + { + if( nonce_length != 12 ) + return( PSA_ERROR_NOT_SUPPORTED ); + } +#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ + + return PSA_SUCCESS; +} + psa_status_t mbedtls_psa_aead_encrypt( const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, @@ -164,6 +180,13 @@ psa_status_t mbedtls_psa_aead_encrypt( } tag = ciphertext + plaintext_length; + if( mbedtls_aead_check_nonce_length( &operation, nonce_length ) + != PSA_SUCCESS ) + { + status = PSA_ERROR_NOT_SUPPORTED; + goto exit; + } + #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) if( operation.alg == PSA_ALG_CCM ) { @@ -195,7 +218,7 @@ psa_status_t mbedtls_psa_aead_encrypt( #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) if( operation.alg == PSA_ALG_CHACHA20_POLY1305 ) { - if( nonce_length != 12 || operation.tag_length != 16 ) + if( operation.tag_length != 16 ) { status = PSA_ERROR_NOT_SUPPORTED; goto exit; @@ -247,21 +270,6 @@ static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, return( PSA_SUCCESS ); } -static psa_status_t mbedtls_aead_check_nonce_length( - mbedtls_psa_aead_operation_t *operation, - size_t nonce_length ) -{ -#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) - if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) - { - if( nonce_length != 12 ) - return( PSA_ERROR_NOT_SUPPORTED ); - } -#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ - - return PSA_SUCCESS; -} - psa_status_t mbedtls_psa_aead_decrypt( const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size,