From a798e5d5cc1113fe16eb4a9a7ae3c7b2d4beeced Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Thu, 16 Jul 2020 09:57:17 -0400 Subject: [PATCH 1/3] Introduce additional buffer size checks to pk.c Signed-off-by: Andrzej Kurek --- library/pk.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/library/pk.c b/library/pk.c index b92eb14fe..3bb91b284 100644 --- a/library/pk.c +++ b/library/pk.c @@ -548,6 +548,7 @@ static size_t uecc_eckey_get_bitlen( const void *ctx ) return( (size_t) ( NUM_ECC_BYTES * 8 ) ); } +/* This function compares public keys of two keypairs */ static int uecc_eckey_check_pair( const void *pub, const void *prv ) { const mbedtls_uecc_keypair *uecc_pub = @@ -621,13 +622,12 @@ static int uecc_eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, static int asn1_write_mpibuf( unsigned char **p, unsigned char *start, size_t n_len ) { - size_t len = 0; + size_t len = n_len; int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; - if( (size_t)( *p - start ) < n_len ) + if( (size_t)( *p - start ) < len ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - len = n_len; *p -= len; ret = mbedtls_platform_memmove( *p, start, len ); if( ret != 0 ) @@ -659,6 +659,9 @@ static int asn1_write_mpibuf( unsigned char **p, unsigned char *start, len += 1; } + if( ( *p - start ) < 2 ) + return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + /* The ASN.1 length encoding is just a single Byte containing the length, * as we assume that the total buffer length is smaller than 128 Bytes. */ *--(*p) = len; @@ -674,7 +677,7 @@ static int asn1_write_mpibuf( unsigned char **p, unsigned char *start, * * [in/out] sig: the signature pre- and post-transcoding * [in/out] sig_len: signature length pre- and post-transcoding - * [int] buf_len: the available size the in/out buffer + * [in] buf_len: the available size the in/out buffer * * Warning: buf_len must be smaller than 128 Bytes. */ @@ -689,6 +692,9 @@ static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len, MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) ); MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) ); + if( p - sig < 2 ) + return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + /* The ASN.1 length encoding is just a single Byte containing the length, * as we assume that the total buffer length is smaller than 128 Bytes. */ *--p = len; From c3b69edd2f84a121325a466748469c799d409e18 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Thu, 16 Jul 2020 11:33:34 -0400 Subject: [PATCH 2/3] Increase hamming distance by changing UECC_SUCCESS from 0 to FFAAAA Signed-off-by: Andrzej Kurek --- include/tinycrypt/ecc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/tinycrypt/ecc.h b/include/tinycrypt/ecc.h index 57aa5087d..4c20729ac 100644 --- a/include/tinycrypt/ecc.h +++ b/include/tinycrypt/ecc.h @@ -85,7 +85,7 @@ extern "C" { /* Return values for functions, chosen with large Hamming distances between * them (especially to SUCESS) to mitigate the impact of fault injection * attacks flipping a low number of bits. */ -#define UECC_SUCCESS 0 +#define UECC_SUCCESS 0x00FFAAAA #define UECC_FAILURE 0x75555555 #define UECC_FAULT_DETECTED 0x7aaaaaaa From 340396964513018694cc4c83317dc9b2997d641e Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 17 Jul 2020 05:49:47 -0400 Subject: [PATCH 3/3] Add a comment regarding remaining space check Signed-off-by: Andrzej Kurek --- library/pk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/library/pk.c b/library/pk.c index 3bb91b284..fea75767b 100644 --- a/library/pk.c +++ b/library/pk.c @@ -659,6 +659,7 @@ static int asn1_write_mpibuf( unsigned char **p, unsigned char *start, len += 1; } + /* Ensure that there is still space for len and ASN1_INTEGER */ if( ( *p - start ) < 2 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );