2019-09-11 13:39:11 +02:00
|
|
|
/**
|
|
|
|
* \file common.h
|
|
|
|
*
|
|
|
|
* \brief Utility macros for internal use in the library
|
|
|
|
*/
|
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2019-09-11 13:39:11 +02: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 16:51:53 +02: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 10:06:23 +02:00
|
|
|
*
|
|
|
|
* To tidy up code and save horizontal and vertical space, use byte
|
2021-06-22 16:51:53 +02:00
|
|
|
* reading macros to cast
|
|
|
|
*/
|
2021-06-24 14:00:03 +02: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 16:51:53 +02:00
|
|
|
|
2021-06-23 12:49:03 +02:00
|
|
|
/*
|
|
|
|
* 32-bit integer manipulation macros (big endian)
|
|
|
|
*/
|
2021-06-24 14:00:03 +02:00
|
|
|
#ifndef MBEDTLS_GET_UINT32_BE
|
2021-07-05 16:37:39 +02:00
|
|
|
#define MBEDTLS_GET_UINT32_BE(n,b,i) \
|
2021-06-24 14:00:03 +02:00
|
|
|
do { \
|
|
|
|
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 3] ); \
|
|
|
|
} while( 0 )
|
2021-06-23 12:49:03 +02:00
|
|
|
#endif
|
|
|
|
|
2021-06-24 14:00:03 +02:00
|
|
|
#ifndef MBEDTLS_PUT_UINT32_BE
|
2021-07-05 16:37:39 +02:00
|
|
|
#define MBEDTLS_PUT_UINT32_BE(n,b,i) \
|
2021-06-24 14:00:03 +02:00
|
|
|
do { \
|
|
|
|
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
|
|
|
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
|
|
|
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
|
|
|
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
|
|
|
} while( 0 )
|
2021-06-23 12:49:03 +02:00
|
|
|
#endif
|
|
|
|
|
2021-06-23 13:16:47 +02:00
|
|
|
/*
|
|
|
|
* 32-bit integer manipulation macros (little endian)
|
|
|
|
*/
|
2021-06-24 14:00:03 +02:00
|
|
|
#ifndef MBEDTLS_GET_UINT32_LE
|
2021-07-05 16:37:39 +02:00
|
|
|
#define MBEDTLS_GET_UINT32_LE(n,b,i) \
|
2021-06-24 14:00:03 +02:00
|
|
|
do { \
|
|
|
|
(n) = ( (uint32_t) (b)[(i) ] ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 1] << 8 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 2] << 16 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 3] << 24 ); \
|
|
|
|
} while( 0 )
|
2021-06-23 13:16:47 +02:00
|
|
|
#endif
|
|
|
|
|
2021-06-24 14:00:03 +02:00
|
|
|
#ifndef MBEDTLS_PUT_UINT32_LE
|
2021-07-05 16:37:39 +02:00
|
|
|
#define MBEDTLS_PUT_UINT32_LE(n,b,i) \
|
2021-06-24 14:00:03 +02:00
|
|
|
do { \
|
|
|
|
(b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
|
|
|
|
(b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
|
|
|
|
(b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
|
|
|
|
(b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
|
|
|
|
} while( 0 )
|
2021-06-23 13:16:47 +02:00
|
|
|
#endif
|
|
|
|
|
2021-06-24 10:06:23 +02:00
|
|
|
/**
|
2021-06-23 13:16:47 +02:00
|
|
|
* 32-bit integer conversion from bytes (little endian)
|
2021-06-23 12:23:44 +02:00
|
|
|
*/
|
2021-07-05 16:37:39 +02:00
|
|
|
#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \
|
2021-06-23 12:23:44 +02:00
|
|
|
( (uint32_t) (data)[offset] \
|
|
|
|
| (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \
|
|
|
|
| (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \
|
|
|
|
| (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \
|
|
|
|
)
|
|
|
|
|
2021-07-05 16:37:39 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 16-bit integer manipulation macros (little endian)
|
|
|
|
*/
|
|
|
|
#ifndef MBEDTLS_GET_UINT16_LE
|
|
|
|
#define MBEDTLS_GET_UINT16_LE( n, b, i ) \
|
|
|
|
{ \
|
|
|
|
(n) = ( (uint16_t) (b)[(i) ] ) \
|
|
|
|
| ( (uint16_t) (b)[(i) + 1] << 8 ); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MBEDTLS_PUT_UINT16_LE
|
|
|
|
#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \
|
|
|
|
{ \
|
|
|
|
(b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
|
|
|
|
(b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-09-11 13:39:11 +02:00
|
|
|
#endif /* MBEDTLS_LIBRARY_COMMON_H */
|