From 3b36bd12f6f2aa7fa271371502f19c31683e2175 Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Fri, 20 May 2016 00:00:37 +0100 Subject: [PATCH 1/2] Adds casts to zeroize functions to allow building as C++ --- library/aes.c | 2 +- library/arc4.c | 2 +- library/asn1parse.c | 2 +- library/blowfish.c | 2 +- library/camellia.c | 2 +- library/ccm.c | 2 +- library/cipher.c | 2 +- library/des.c | 2 +- library/sha1.c | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/library/aes.c b/library/aes.c index ec9313de3..36660306e 100644 --- a/library/aes.c +++ b/library/aes.c @@ -56,7 +56,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } /* diff --git a/library/arc4.c b/library/arc4.c index ff0e993e7..05b33d3fd 100644 --- a/library/arc4.c +++ b/library/arc4.c @@ -49,7 +49,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } void mbedtls_arc4_init( mbedtls_arc4_context *ctx ) diff --git a/library/asn1parse.c b/library/asn1parse.c index b37523def..e59d2509f 100644 --- a/library/asn1parse.c +++ b/library/asn1parse.c @@ -45,7 +45,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } /* diff --git a/library/blowfish.c b/library/blowfish.c index 89be4d122..9003f0dfe 100644 --- a/library/blowfish.c +++ b/library/blowfish.c @@ -41,7 +41,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } /* diff --git a/library/camellia.c b/library/camellia.c index e015ca24b..d50513fd0 100644 --- a/library/camellia.c +++ b/library/camellia.c @@ -50,7 +50,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } /* diff --git a/library/ccm.c b/library/ccm.c index 3463a0b32..13a8fd1a2 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -51,7 +51,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } #define CCM_ENCRYPT 0 diff --git a/library/cipher.c b/library/cipher.c index ccc068503..0dc51520f 100644 --- a/library/cipher.c +++ b/library/cipher.c @@ -51,7 +51,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } static int supported_init = 0; diff --git a/library/des.c b/library/des.c index 61f214af3..09f95cfc3 100644 --- a/library/des.c +++ b/library/des.c @@ -50,7 +50,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } /* diff --git a/library/sha1.c b/library/sha1.c index 8c77cbaa8..2ccf2a2f5 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -49,7 +49,7 @@ /* Implementation that should never be optimized out by the compiler */ static void mbedtls_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; + volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; } /* From 12833ed3c82acee7509587029d755fe10be2fc10 Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Fri, 20 May 2016 00:19:09 +0100 Subject: [PATCH 2/2] Adds additional casts to calloc calls Casts added to allow compilation of the library as C++ --- library/asn1parse.c | 3 ++- library/asn1write.c | 4 +++- library/bignum.c | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/library/asn1parse.c b/library/asn1parse.c index e59d2509f..ffa2f5299 100644 --- a/library/asn1parse.c +++ b/library/asn1parse.c @@ -269,7 +269,8 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p, /* Allocate and assign next pointer */ if( *p < end ) { - cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) ); + cur->next = (mbedtls_asn1_sequence*)mbedtls_calloc( 1, + sizeof( mbedtls_asn1_sequence ) ); if( cur->next == NULL ) return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); diff --git a/library/asn1write.c b/library/asn1write.c index 00ed73c11..027c858e7 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -312,7 +312,9 @@ mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data { // Add new entry if not present yet based on OID // - if( ( cur = mbedtls_calloc( 1, sizeof(mbedtls_asn1_named_data) ) ) == NULL ) + cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1, + sizeof(mbedtls_asn1_named_data) ); + if( cur == NULL ) return( NULL ); cur->oid.len = oid_len; diff --git a/library/bignum.c b/library/bignum.c index 4536a3b86..4c99e04d6 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -120,7 +120,7 @@ int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ) if( X->n < nblimbs ) { - if( ( p = mbedtls_calloc( nblimbs, ciL ) ) == NULL ) + if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL ) return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); if( X->p != NULL ) @@ -158,7 +158,7 @@ int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ) if( i < nblimbs ) i = nblimbs; - if( ( p = mbedtls_calloc( i, ciL ) ) == NULL ) + if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL ) return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); if( X->p != NULL )