Skip to content

Commit 9edcc3e

Browse files
Hanno BeckerPatater
authored andcommitted
Merge branch 'iotssl-2597-psa-hashing-x509_CRYPTO' into feature-psa-tls-integration-proposed
2 parents 8295695 + 60ea0fc commit 9edcc3e

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

library/x509_crt.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
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

library/x509write_csr.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
#include "mbedtls/asn1write.h"
3838
#include "mbedtls/platform_util.h"
3939

40+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
41+
#include "psa/crypto.h"
42+
#include "mbedtls/psa_util.h"
43+
#endif
44+
4045
#include <string.h>
4146
#include <stdlib.h>
4247

@@ -136,7 +141,11 @@ int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, s
136141
size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
137142
size_t len = 0;
138143
mbedtls_pk_type_t pk_alg;
139-
144+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
145+
psa_hash_operation_t hash_operation;
146+
size_t hash_len;
147+
psa_algorithm_t hash_alg = mbedtls_psa_translate_md( ctx->md_alg );
148+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
140149
/*
141150
* Prepare data to be signed in tmp_buf
142151
*/
@@ -187,9 +196,23 @@ int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, s
187196

188197
/*
189198
* Prepare signature
199+
* Note: hash errors can happen only after an internal error
190200
*/
191-
mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
201+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
202+
if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
203+
return( MBEDTLS_ERR_X509_FATAL_ERROR );
204+
205+
if( psa_hash_update( &hash_operation, c, len ) != PSA_SUCCESS )
206+
return( MBEDTLS_ERR_X509_FATAL_ERROR );
192207

208+
if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
209+
!= PSA_SUCCESS )
210+
{
211+
return( MBEDTLS_ERR_X509_FATAL_ERROR );
212+
}
213+
#else /* MBEDTLS_USE_PSA_CRYPTO */
214+
mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
215+
#endif
193216
if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
194217
f_rng, p_rng ) ) != 0 )
195218
{

0 commit comments

Comments
 (0)