Add efficent unaligned get/put functions

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2022-11-28 14:44:05 +00:00
parent d98ac8b75e
commit a360e1987a
2 changed files with 60 additions and 0 deletions

View file

@ -28,6 +28,32 @@
#include "mbedtls/build_info.h"
/**
* Read the unsigned 16 bits integer from the given address, which need not
* be aligned.
*
* \param p pointer to 2 bytes of data
* \return Data at the given address
*/
inline uint16_t mbedtls_get_unaligned_uint16( const void *p )
{
uint16_t r;
memcpy( &r, p, sizeof( r ) );
return r;
}
/**
* Write the unsigned 16 bits integer to the given address, which need not
* be aligned.
*
* \param p pointer to 2 bytes of data
* \param x data to write
*/
inline void mbedtls_put_unaligned_uint16( void *p, uint16_t x )
{
memcpy( p, &x, sizeof( x ) );
}
/**
* Read the unsigned 32 bits integer from the given address, which need not
* be aligned.
@ -54,6 +80,32 @@ inline void mbedtls_put_unaligned_uint32( void *p, uint32_t x )
memcpy( p, &x, sizeof( x ) );
}
/**
* Read the unsigned 64 bits integer from the given address, which need not
* be aligned.
*
* \param p pointer to 8 bytes of data
* \return Data at the given address
*/
inline uint64_t mbedtls_get_unaligned_uint64( const void *p )
{
uint64_t r;
memcpy( &r, p, sizeof( r ) );
return r;
}
/**
* Write the unsigned 64 bits integer to the given address, which need not
* be aligned.
*
* \param p pointer to 8 bytes of data
* \param x data to write
*/
inline void mbedtls_put_unaligned_uint64( void *p, uint64_t x )
{
memcpy( p, &x, sizeof( x ) );
}
/** Byte Reading Macros
*
* Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th

View file

@ -149,6 +149,14 @@ void (*mbedtls_test_hook_test_fail)( const char *, int, const char *);
*/
extern inline void mbedtls_xor( unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n );
extern inline uint16_t mbedtls_get_unaligned_uint16( const void *p );
extern inline void mbedtls_put_unaligned_uint16( void *p, uint16_t x );
extern inline uint32_t mbedtls_get_unaligned_uint32( const void *p );
extern inline void mbedtls_put_unaligned_uint32( void *p, uint32_t x );
extern inline uint64_t mbedtls_get_unaligned_uint64( const void *p );
extern inline void mbedtls_put_unaligned_uint64( void *p, uint64_t x );