4949#include "mbedtls/pem.h"
5050#endif
5151
52+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
53+ #include "psa/crypto.h"
54+ #include "mbedtls/psa_util.h"
55+ #endif
56+
5257#if defined(MBEDTLS_PLATFORM_C )
5358#include "mbedtls/platform.h"
5459#else
@@ -1892,16 +1897,35 @@ static int x509_crt_check_signature( const mbedtls_x509_crt *child,
18921897 mbedtls_x509_crt * parent ,
18931898 mbedtls_x509_crt_restart_ctx * rs_ctx )
18941899{
1895- const mbedtls_md_info_t * md_info ;
18961900 unsigned char hash [MBEDTLS_MD_MAX_SIZE ];
1897-
1901+ size_t hash_len ;
1902+ #if !defined(MBEDTLS_USE_PSA_CRYPTO )
1903+ const mbedtls_md_info_t * md_info ;
18981904 md_info = mbedtls_md_info_from_type ( child -> sig_md );
1905+ hash_len = mbedtls_md_get_size ( md_info );
1906+
1907+ /* Note: hash errors can happen only after an internal error */
18991908 if ( mbedtls_md ( md_info , child -> tbs .p , child -> tbs .len , hash ) != 0 )
1909+ return ( -1 );
1910+ #else
1911+ psa_hash_operation_t hash_operation ;
1912+ psa_algorithm_t hash_alg = mbedtls_psa_translate_md ( child -> sig_md );
1913+
1914+ if ( psa_hash_setup ( & hash_operation , hash_alg ) != PSA_SUCCESS )
1915+ return ( -1 );
1916+
1917+ if ( psa_hash_update ( & hash_operation , child -> tbs .p , child -> tbs .len )
1918+ != PSA_SUCCESS )
19001919 {
1901- /* Note: this can't happen except after an internal error */
19021920 return ( -1 );
19031921 }
19041922
1923+ if ( psa_hash_finish ( & hash_operation , hash , sizeof ( hash ), & hash_len )
1924+ != PSA_SUCCESS )
1925+ {
1926+ return ( -1 );
1927+ }
1928+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
19051929 /* Skip expensive computation on obvious mismatch */
19061930 if ( ! mbedtls_pk_can_do ( & parent -> pk , child -> sig_pk ) )
19071931 return ( -1 );
@@ -1910,15 +1934,15 @@ static int x509_crt_check_signature( const mbedtls_x509_crt *child,
19101934 if ( rs_ctx != NULL && child -> sig_pk == MBEDTLS_PK_ECDSA )
19111935 {
19121936 return ( mbedtls_pk_verify_restartable ( & parent -> pk ,
1913- child -> sig_md , hash , mbedtls_md_get_size ( md_info ) ,
1937+ child -> sig_md , hash , hash_len ,
19141938 child -> sig .p , child -> sig .len , & rs_ctx -> pk ) );
19151939 }
19161940#else
19171941 (void ) rs_ctx ;
19181942#endif
19191943
19201944 return ( mbedtls_pk_verify_ext ( child -> sig_pk , child -> sig_opts , & parent -> pk ,
1921- child -> sig_md , hash , mbedtls_md_get_size ( md_info ) ,
1945+ child -> sig_md , hash , hash_len ,
19221946 child -> sig .p , child -> sig .len ) );
19231947}
19241948
0 commit comments