diff --git a/library/alignment.h b/library/alignment.h index de1ab91487..3539c9175e 100644 --- a/library/alignment.h +++ b/library/alignment.h @@ -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 diff --git a/library/platform_util.c b/library/platform_util.c index 9c18dd502a..2b674f62e7 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -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 );