mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-08 17:42:09 +03:00
Fix NULL+0 undefined behavior in ECB encryption and decryption
psa_cipher_encrypt() and psa_cipher_decrypt() sometimes add a zero offset to a null pointer when the cipher does not use an IV. This is undefined behavior, although it works as naively expected on most platforms. This can cause a crash with modern Clang+ASan (depending on compiler optimizations). Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
@@ -516,10 +516,10 @@ psa_status_t mbedtls_psa_cipher_encrypt(
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
|
||||
status = mbedtls_psa_cipher_finish( &operation,
|
||||
output + update_output_length,
|
||||
output_size - update_output_length,
|
||||
&finish_output_length );
|
||||
status = mbedtls_psa_cipher_finish(
|
||||
&operation,
|
||||
mbedtls_buffer_offset( output, update_output_length ),
|
||||
output_size - update_output_length, &finish_output_length );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
|
||||
@@ -563,17 +563,20 @@ psa_status_t mbedtls_psa_cipher_decrypt(
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = mbedtls_psa_cipher_update( &operation, input + operation.iv_length,
|
||||
input_length - operation.iv_length,
|
||||
output, output_size, &olength );
|
||||
status = mbedtls_psa_cipher_update(
|
||||
&operation,
|
||||
mbedtls_buffer_offset_const( input, operation.iv_length ),
|
||||
input_length - operation.iv_length,
|
||||
output, output_size, &olength );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
|
||||
accumulated_length = olength;
|
||||
|
||||
status = mbedtls_psa_cipher_finish( &operation, output + accumulated_length,
|
||||
output_size - accumulated_length,
|
||||
&olength );
|
||||
status = mbedtls_psa_cipher_finish(
|
||||
&operation,
|
||||
mbedtls_buffer_offset( output, accumulated_length ),
|
||||
output_size - accumulated_length, &olength );
|
||||
if( status != PSA_SUCCESS )
|
||||
goto exit;
|
||||
|
||||
|
Reference in New Issue
Block a user