mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-22 17:45:38 +01:00
Add mbedtls_platform_memmove() as a secured memcmp()
Signed-off-by: Piotr Nowicki <piotr.nowicki@arm.com>
This commit is contained in:
parent
478b05c34c
commit
5d5841f450
@ -198,6 +198,22 @@ void *mbedtls_platform_memset( void *ptr, int value, size_t num );
|
||||
*/
|
||||
void *mbedtls_platform_memcpy( void *dst, const void *src, size_t num );
|
||||
|
||||
/**
|
||||
* \brief Secure memmove
|
||||
*
|
||||
* This is a constant-time version of memmove(). It is based on
|
||||
* the double use of the mbedtls_platform_memcpy() function secured
|
||||
* against side-channel attacks.
|
||||
*
|
||||
* \param dst Destination buffer where the data is being moved to.
|
||||
* \param src Source buffer where the data is being moved from.
|
||||
* \param num The length of the buffers in bytes.
|
||||
*
|
||||
* \return 0 if the operation was successful or -1 if memory allocation
|
||||
* failed.
|
||||
*/
|
||||
int mbedtls_platform_memmove( void *dst, const void *src, size_t num );
|
||||
|
||||
/**
|
||||
* \brief Secure memcmp
|
||||
*
|
||||
|
@ -558,7 +558,11 @@ static int mpi_write_hlp( mbedtls_mpi *X, int radix,
|
||||
length++;
|
||||
} while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
|
||||
|
||||
memmove( *p, p_end, length );
|
||||
if( 0 != mbedtls_platform_memmove( *p, p_end, length ) )
|
||||
{
|
||||
ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
*p += length;
|
||||
|
||||
cleanup:
|
||||
|
@ -222,7 +222,10 @@ int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx,
|
||||
}
|
||||
|
||||
mbedtls_platform_memcpy( output, NIST_KW_ICV1, KW_SEMIBLOCK_LENGTH );
|
||||
memmove( output + KW_SEMIBLOCK_LENGTH, input, in_len );
|
||||
if( 0 != mbedtls_platform_memmove( output + KW_SEMIBLOCK_LENGTH, input, in_len ) )
|
||||
{
|
||||
return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -343,7 +346,11 @@ static int unwrap( mbedtls_nist_kw_context *ctx,
|
||||
}
|
||||
|
||||
mbedtls_platform_memcpy( A, input, KW_SEMIBLOCK_LENGTH );
|
||||
memmove( output, input + KW_SEMIBLOCK_LENGTH, ( semiblocks - 1 ) * KW_SEMIBLOCK_LENGTH );
|
||||
if( 0 != mbedtls_platform_memmove( output, input + KW_SEMIBLOCK_LENGTH,
|
||||
( semiblocks - 1 ) * KW_SEMIBLOCK_LENGTH ) )
|
||||
{
|
||||
return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
|
||||
}
|
||||
|
||||
/* Calculate intermediate values */
|
||||
for( t = s; t >= 1; t-- )
|
||||
|
10
library/pk.c
10
library/pk.c
@ -629,7 +629,10 @@ static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
|
||||
|
||||
len = n_len;
|
||||
*p -= len;
|
||||
memmove( *p, start, len );
|
||||
if( 0 != mbedtls_platform_memmove( *p, start, len ) )
|
||||
{
|
||||
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
|
||||
}
|
||||
|
||||
/* ASN.1 DER encoding requires minimal length, so skip leading 0s.
|
||||
* Neither r nor s should be 0, but as a failsafe measure, still detect
|
||||
@ -691,8 +694,11 @@ static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
|
||||
*--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
|
||||
len += 2;
|
||||
|
||||
if( 0 != mbedtls_platform_memmove( sig, p, len ) )
|
||||
{
|
||||
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
|
||||
}
|
||||
ret = 0;
|
||||
memmove( sig, p, len );
|
||||
*sig_len = len;
|
||||
|
||||
return( ret );
|
||||
|
@ -38,6 +38,12 @@
|
||||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/threading.h"
|
||||
|
||||
#if !defined(MBEDTLS_PLATFORM_C)
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
|
||||
#include "mbedtls/entropy_poll.h"
|
||||
#endif
|
||||
@ -121,6 +127,23 @@ void *mbedtls_platform_memcpy( void *dst, const void *src, size_t num )
|
||||
return( memcpy( (void *) dst, (void *) src, start_offset ) );
|
||||
}
|
||||
|
||||
int mbedtls_platform_memmove( void *dst, const void *src, size_t num )
|
||||
{
|
||||
/* The buffers can have a common part, so we cannot do a copy from a random
|
||||
* location. By using a temporary buffer we can do so, but the cost of it
|
||||
* is using more memory and longer transfer time. */
|
||||
void *tmp = mbedtls_calloc( 1, num );
|
||||
if( tmp != NULL )
|
||||
{
|
||||
mbedtls_platform_memcpy( tmp, src, num );
|
||||
mbedtls_platform_memcpy( dst, tmp, num );
|
||||
mbedtls_free( tmp );
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num )
|
||||
{
|
||||
volatile const unsigned char *A = (volatile const unsigned char *) buf1;
|
||||
|
Loading…
Reference in New Issue
Block a user