blob: f7a9c9da6d97e3ce47eadff3290d62e5c1be94c3 [file] [log] [blame]
Gilles Peskinec4672fd2019-09-11 13:39:11 +02001/**
2 * \file common.h
3 *
4 * \brief Utility macros for internal use in the library
5 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Gilles Peskinec4672fd2019-09-11 13:39:11 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Gilles Peskinec4672fd2019-09-11 13:39:11 +020021 */
22
23#ifndef MBEDTLS_LIBRARY_COMMON_H
24#define MBEDTLS_LIBRARY_COMMON_H
25
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020027
28/** Helper to define a function as static except when building invasive tests.
29 *
30 * If a function is only used inside its own source file and should be
31 * declared `static` to allow the compiler to optimize for code size,
32 * but that function has unit tests, define it with
33 * ```
34 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
35 * ```
36 * and declare it in a header in the `library/` directory with
37 * ```
38 * #if defined(MBEDTLS_TEST_HOOKS)
39 * int mbedtls_foo(...);
40 * #endif
41 * ```
42 */
43#if defined(MBEDTLS_TEST_HOOKS)
44#define MBEDTLS_STATIC_TESTABLE
45#else
46#define MBEDTLS_STATIC_TESTABLE static
47#endif
48
TRodziewicz7871c2e2021-07-07 17:29:43 +020049#if defined(MBEDTLS_TEST_HOOKS)
50extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
51#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
52 do { \
53 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
54 { \
55 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
56 } \
57 } while( 0 )
58#else
59#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
60#endif /* defined(MBEDTLS_TEST_HOOKS) */
61
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020062/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020063 *
64 * Although structs defined in header files are publicly available,
65 * their members are private and should not be accessed by the user.
66 */
67#define MBEDTLS_ALLOW_PRIVATE_ACCESS
68
Joe Subbiani50dde562021-06-22 15:51:53 +010069/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010070 *
Joe Subbiani394bdd62021-07-07 15:16:56 +010071 * Obtain the most significant byte of x using 0xff
72 * Using MBEDTLS_BYTE_a will shift a*8 bits
73 * to retrieve the next byte of information
Joe Subbiani50dde562021-06-22 15:51:53 +010074 */
Joe Subbiani5ecac212021-06-24 13:00:03 +010075#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
76#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
77#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
78#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani50dde562021-06-22 15:51:53 +010079
Joe Subbianicd84d762021-07-08 14:59:52 +010080#define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) )
81#define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) )
82#define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) )
83#define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) )
84#define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) )
85#define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) )
86#define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) )
87#define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) )
88
Joe Subbiani394bdd62021-07-07 15:16:56 +010089/**
Joe Subbiani635231a2021-07-14 11:53:07 +010090 * Get the unsigned 32 bits integer corresponding to four bytes in
91 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +010092 *
Joe Subbiani635231a2021-07-14 11:53:07 +010093 * \param data Base address of the memory to get the four bytes from.
94 * \param offset Offset from \p base of the first and most significant
95 * byte of the four bytes to build the 32 bits unsigned
96 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +010097 */
98#ifndef MBEDTLS_GET_UINT32_BE
99#define MBEDTLS_GET_UINT32_BE( data , offset ) \
100 ( \
101 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
102 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
103 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
104 | ( (uint32_t) ( data )[( offset ) + 3] ) \
105 )
106#endif
107
108/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100109 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100110 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100111 * \param n 32 bits unsigned integer to put in memory
112 * \param data Base address of the memory where to put the 32
113 * bits unsigned integer in.
114 * \param offset Offset from \p base where to put the most significant
115 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100116 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100117#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani635231a2021-07-14 11:53:07 +0100118#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
119 do { \
120 ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \
121 ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \
122 ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \
123 ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100124 } while( 0 )
Joe Subbiani30d974c2021-06-23 11:49:03 +0100125#endif
126
Joe Subbiani394bdd62021-07-07 15:16:56 +0100127/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100128 * Get the unsigned 32 bits integer corresponding to four bytes in
129 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100130 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100131 * \param data Base address of the memory to get the four bytes from.
132 * \param offset Offset from \p base of the first and least significant
133 * byte of the four bytes to build the 32 bits unsigned
134 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100135 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100136#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani6a506312021-07-07 16:56:29 +0100137#define MBEDTLS_GET_UINT32_LE( data, offset ) \
138 ( \
139 ( (uint32_t) ( data )[( offset ) ] ) \
140 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
141 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
142 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
143 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100144#endif
145
Joe Subbiani6a506312021-07-07 16:56:29 +0100146/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100147 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100148 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100149 * \param n 32 bits unsigned integer to put in memory
150 * \param data Base address of the memory where to put the 32
151 * bits unsigned integer in.
152 * \param offset Offset from \p base where to put the least significant
153 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100154 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100155#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani635231a2021-07-14 11:53:07 +0100156#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
157 do { \
158 ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
159 ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
160 ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
161 ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100162 } while( 0 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100163#endif
164
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100165/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100166 * Get the unsigned 16 bits integer corresponding to four bytes in
167 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100168 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100169 * \param data Base address of the memory to get the four bytes from.
170 * \param offset Offset from \p base of the first and least significant
171 * byte of the four bytes to build the 16 bits unsigned
172 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100173 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100174#ifndef MBEDTLS_GET_UINT16_LE
175#define MBEDTLS_GET_UINT16_LE( data, offset ) \
176 ( \
177 ( (uint16_t) ( data )[( offset ) ] ) \
178 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100179 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100180#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100181
Joe Subbiani394bdd62021-07-07 15:16:56 +0100182/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100183 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100184 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100185 * \param n 16 bits unsigned integer to put in memory
186 * \param data Base address of the memory where to put the 16
187 * bits unsigned integer in.
188 * \param offset Offset from \p base where to put the least significant
189 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100190 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100191#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani635231a2021-07-14 11:53:07 +0100192#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
193{ \
194 ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
195 ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100196}
197#endif
198
199
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200200#endif /* MBEDTLS_LIBRARY_COMMON_H */