2019-09-11 11:39:11 +00:00
|
|
|
/**
|
|
|
|
* \file common.h
|
|
|
|
*
|
|
|
|
* \brief Utility macros for internal use in the library
|
|
|
|
*/
|
|
|
|
/*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2019-09-11 11:39:11 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBEDTLS_LIBRARY_COMMON_H
|
|
|
|
#define MBEDTLS_LIBRARY_COMMON_H
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#else
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Helper to define a function as static except when building invasive tests.
|
|
|
|
*
|
|
|
|
* If a function is only used inside its own source file and should be
|
|
|
|
* declared `static` to allow the compiler to optimize for code size,
|
|
|
|
* but that function has unit tests, define it with
|
|
|
|
* ```
|
|
|
|
* MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
|
|
|
|
* ```
|
|
|
|
* and declare it in a header in the `library/` directory with
|
|
|
|
* ```
|
|
|
|
* #if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
* int mbedtls_foo(...);
|
|
|
|
* #endif
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
#define MBEDTLS_STATIC_TESTABLE
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_STATIC_TESTABLE static
|
|
|
|
#endif
|
|
|
|
|
2021-06-22 14:51:53 +00:00
|
|
|
/** Allow library to access its structs' private members.
|
|
|
|
*
|
|
|
|
* Although structs defined in header files are publicly available,
|
|
|
|
* their members are private and should not be accessed by the user.
|
|
|
|
*/
|
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
|
|
|
/** Byte Reading Macros
|
2021-06-24 08:06:23 +00:00
|
|
|
*
|
2021-07-07 14:16:56 +00:00
|
|
|
* Obtain the most significant byte of x using 0xff
|
|
|
|
* Using MBEDTLS_BYTE_a will shift a*8 bits
|
|
|
|
* to retrieve the next byte of information
|
2021-06-22 14:51:53 +00:00
|
|
|
*/
|
2021-06-24 12:00:03 +00:00
|
|
|
#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
|
|
|
|
#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
|
2021-06-22 14:51:53 +00:00
|
|
|
|
2021-07-08 13:59:52 +00:00
|
|
|
#define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) )
|
|
|
|
#define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) )
|
|
|
|
|
2021-07-07 14:16:56 +00:00
|
|
|
/**
|
2021-07-13 11:13:19 +00:00
|
|
|
* Get the unsigned 32 bits integer corresponding to four bytes in
|
2021-07-14 10:53:07 +00:00
|
|
|
* big-endian order (MSB first).
|
2021-07-07 15:56:29 +00:00
|
|
|
*
|
2021-07-14 10:53:07 +00:00
|
|
|
* \param data Base address of the memory to get the four bytes from.
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param offset Offset from \p base of the first and most significant
|
|
|
|
* byte of the four bytes to build the 32 bits unsigned
|
2021-07-14 10:53:07 +00:00
|
|
|
* integer from.
|
2021-07-07 15:56:29 +00:00
|
|
|
*/
|
|
|
|
#ifndef MBEDTLS_GET_UINT32_BE
|
|
|
|
#define MBEDTLS_GET_UINT32_BE( data , offset ) \
|
|
|
|
( \
|
|
|
|
( (uint32_t) ( data )[( offset ) ] << 24 ) \
|
|
|
|
| ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
|
|
|
|
| ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
|
|
|
|
| ( (uint32_t) ( data )[( offset ) + 3] ) \
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2021-07-14 10:53:07 +00:00
|
|
|
* Put in memory a 32 bits unsigned integer in big-endian order.
|
2021-07-07 15:56:29 +00:00
|
|
|
*
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param n 32 bits unsigned integer to put in memory.
|
|
|
|
* \param data Base address of the memory where to put the 32
|
2021-07-14 10:53:07 +00:00
|
|
|
* bits unsigned integer in.
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param offset Offset from \p base where to put the most significant
|
2021-07-14 10:53:07 +00:00
|
|
|
* byte of the 32 bits unsigned integer \p n.
|
2021-07-07 14:16:56 +00:00
|
|
|
*/
|
2021-06-24 12:00:03 +00:00
|
|
|
#ifndef MBEDTLS_PUT_UINT32_BE
|
2021-07-14 10:53:07 +00:00
|
|
|
#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
|
|
|
|
do { \
|
|
|
|
( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \
|
|
|
|
( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \
|
|
|
|
( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \
|
|
|
|
( data )[( offset ) + 3] = (unsigned char) ( (n) ); \
|
2021-06-24 12:00:03 +00:00
|
|
|
} while( 0 )
|
2021-06-23 10:49:03 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-07 14:16:56 +00:00
|
|
|
/**
|
2021-07-13 11:13:19 +00:00
|
|
|
* Get the unsigned 32 bits integer corresponding to four bytes in
|
2021-07-14 10:53:07 +00:00
|
|
|
* little-endian order (LSB first).
|
2021-07-07 15:56:29 +00:00
|
|
|
*
|
2021-07-14 10:53:07 +00:00
|
|
|
* \param data Base address of the memory to get the four bytes from.
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param offset Offset from \p base of the first and least significant
|
|
|
|
* byte of the four bytes to build the 32 bits unsigned
|
2021-07-14 10:53:07 +00:00
|
|
|
* integer from.
|
2021-06-23 11:16:47 +00:00
|
|
|
*/
|
2021-06-24 12:00:03 +00:00
|
|
|
#ifndef MBEDTLS_GET_UINT32_LE
|
2021-07-07 15:56:29 +00:00
|
|
|
#define MBEDTLS_GET_UINT32_LE( data, offset ) \
|
|
|
|
( \
|
|
|
|
( (uint32_t) ( data )[( offset ) ] ) \
|
|
|
|
| ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
|
|
|
|
| ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
|
|
|
|
| ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
|
|
|
|
)
|
2021-06-23 11:16:47 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-07 15:56:29 +00:00
|
|
|
/**
|
2021-07-14 10:53:07 +00:00
|
|
|
* Put in memory a 32 bits unsigned integer in little-endian order.
|
2021-07-07 15:56:29 +00:00
|
|
|
*
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param n 32 bits unsigned integer to put in memory.
|
|
|
|
* \param data Base address of the memory where to put the 32
|
2021-07-14 10:53:07 +00:00
|
|
|
* bits unsigned integer in.
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param offset Offset from \p base where to put the least significant
|
2021-07-14 10:53:07 +00:00
|
|
|
* byte of the 32 bits unsigned integer \p n.
|
2021-07-07 15:56:29 +00:00
|
|
|
*/
|
2021-06-24 12:00:03 +00:00
|
|
|
#ifndef MBEDTLS_PUT_UINT32_LE
|
2021-07-14 10:53:07 +00:00
|
|
|
#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
|
|
|
|
do { \
|
|
|
|
( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
|
|
|
|
( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
|
|
|
|
( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
|
|
|
|
( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
|
2021-06-24 12:00:03 +00:00
|
|
|
} while( 0 )
|
2021-06-23 11:16:47 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-24 08:06:23 +00:00
|
|
|
/**
|
2021-07-14 11:05:51 +00:00
|
|
|
* Get the unsigned 16 bits integer corresponding to two bytes in
|
2021-07-14 10:53:07 +00:00
|
|
|
* little-endian order (LSB first).
|
2021-07-07 15:56:29 +00:00
|
|
|
*
|
2021-07-14 11:05:51 +00:00
|
|
|
* \param data Base address of the memory to get the two bytes from.
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param offset Offset from \p base of the first and least significant
|
2021-07-14 11:05:51 +00:00
|
|
|
* byte of the two bytes to build the 16 bits unsigned
|
2021-07-14 10:53:07 +00:00
|
|
|
* integer from.
|
2021-06-23 10:23:44 +00:00
|
|
|
*/
|
2021-07-07 15:56:29 +00:00
|
|
|
#ifndef MBEDTLS_GET_UINT16_LE
|
|
|
|
#define MBEDTLS_GET_UINT16_LE( data, offset ) \
|
|
|
|
( \
|
|
|
|
( (uint16_t) ( data )[( offset ) ] ) \
|
|
|
|
| ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
|
2021-06-23 10:23:44 +00:00
|
|
|
)
|
2021-07-07 15:56:29 +00:00
|
|
|
#endif
|
2021-06-23 10:23:44 +00:00
|
|
|
|
2021-07-07 14:16:56 +00:00
|
|
|
/**
|
2021-07-14 10:53:07 +00:00
|
|
|
* Put in memory a 16 bits unsigned integer in little-endian order.
|
2021-07-07 15:56:29 +00:00
|
|
|
*
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param n 16 bits unsigned integer to put in memory.
|
|
|
|
* \param data Base address of the memory where to put the 16
|
2021-07-14 10:53:07 +00:00
|
|
|
* bits unsigned integer in.
|
2021-07-13 11:13:19 +00:00
|
|
|
* \param offset Offset from \p base where to put the least significant
|
2021-07-14 10:53:07 +00:00
|
|
|
* byte of the 16 bits unsigned integer \p n.
|
2021-07-07 14:16:56 +00:00
|
|
|
*/
|
2021-07-05 14:37:39 +00:00
|
|
|
#ifndef MBEDTLS_PUT_UINT16_LE
|
2021-07-14 10:53:07 +00:00
|
|
|
#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
|
|
|
|
{ \
|
|
|
|
( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
|
|
|
|
( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
|
2021-07-05 14:37:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-09-11 11:39:11 +00:00
|
|
|
#endif /* MBEDTLS_LIBRARY_COMMON_H */
|