blob: 4ecc0162bf129167a5f60f8f19cf9c984b686a69 [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 Subbiani394bdd62021-07-07 15:16:56 +010080/**
Joe Subbiani6a506312021-07-07 16:56:29 +010081 * 32-bit integer manipulation GET macros (big endian)
Joe Subbiani394bdd62021-07-07 15:16:56 +010082 *
Joe Subbiani6a506312021-07-07 16:56:29 +010083 * \brief Use this to assign an unsigned 32 bit integer
84 * by taking data stored adjacent in memory that
85 * can be accessed via on offset
86 * Big Endian is used when wanting to
87 * transmit the most signifcant bits first
Joe Subbiani394bdd62021-07-07 15:16:56 +010088 *
Joe Subbiani6a506312021-07-07 16:56:29 +010089 * \param data The data used to translate to a 32 bit
90 * integer
91 * \param offset the shift in bytes to access the next byte
92 * of data
93 */
94#ifndef MBEDTLS_GET_UINT32_BE
95#define MBEDTLS_GET_UINT32_BE( data , offset ) \
96 ( \
97 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
98 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
99 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
100 | ( (uint32_t) ( data )[( offset ) + 3] ) \
101 )
102#endif
103
104/**
105 * 32-bit integer manipulation PUT macros (big endian)
106 *
107 * \brief Read from a 32 bit integer and store each byte
108 * in memory, offset by a specified amount, resulting
109 * in each byte being adjacent in memory.
110 * Big Endian is used when wanting to
111 * transmit the most signifcant bits first
112 *
113 * \param n 32 bit integer where data is accessed
Joe Subbiani394bdd62021-07-07 15:16:56 +0100114 * \param b const unsigned char array of data to be
115 * manipulated
116 * \param i offset in bytes, In the case of UINT32, i
117 * would increment by 4 every use assuming
118 * the data is being stored in the same location
119 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100120#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100121#define MBEDTLS_PUT_UINT32_BE(n,b,i) \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100122 do { \
123 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
124 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
125 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
126 (b)[(i) + 3] = (unsigned char) ( (n) ); \
127 } while( 0 )
Joe Subbiani30d974c2021-06-23 11:49:03 +0100128#endif
129
Joe Subbiani394bdd62021-07-07 15:16:56 +0100130/**
Joe Subbiani6a506312021-07-07 16:56:29 +0100131 * 32-bit integer manipulation GET macros (little endian)
132 *
133 * \brief Use this to assign an unsigned 32 bit integer
134 * by taking data stored adjacent in memory that
135 * can be accessed via on offset
136 * Little Endian is used when wanting to
137 * transmit the least signifcant bits first
138 *
139 * \param data The data used to translate to a 32 bit
140 * integer
141 * \param offset the shift in bytes to access the next byte
142 * of data
Joe Subbiani54c61342021-06-23 12:16:47 +0100143 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100144#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani6a506312021-07-07 16:56:29 +0100145#define MBEDTLS_GET_UINT32_LE( data, offset ) \
146 ( \
147 ( (uint32_t) ( data )[( offset ) ] ) \
148 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
149 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
150 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
151 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100152#endif
153
Joe Subbiani6a506312021-07-07 16:56:29 +0100154/**
155 * 32-bit integer manipulation PUT macros (little endian)
156 *
157 * \brief Read from a 32 bit integer and store each byte
158 * in memory, offset by a specified amount, resulting
159 * in each byte being adjacent in memory.
160 * Little Endian is used when wanting to
161 * transmit the least signifcant bits first
162 *
163 * \param n 32 bit integer where data is accessed
164 * \param b const unsigned char array of data to be
165 * manipulated
166 * \param i offset in bytes, In the case of UINT32, i
167 * would increment by 4 every use assuming
168 * the data is being stored in the same location
169 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100170#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100171#define MBEDTLS_PUT_UINT32_LE(n,b,i) \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100172 do { \
173 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
174 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
175 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
176 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
177 } while( 0 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100178#endif
179
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100180/**
Joe Subbiani6a506312021-07-07 16:56:29 +0100181 * 16-bit integer manipulation GET macros (little endian)
182 *
183 * \brief Use this to assign an unsigned 16 bit integer
184 * by taking data stored adjacent in memory that
185 * can be accessed via on offset
186 * Little Endian is used when wanting to
187 * transmit the least signifcant bits first
188 *
189 * \param data The data used to translate to a 16 bit
190 * integer
191 * \param offset the shit in bytes to access the next byte
192 * of data
Joe Subbiani3b394502021-06-23 11:23:44 +0100193 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100194#ifndef MBEDTLS_GET_UINT16_LE
195#define MBEDTLS_GET_UINT16_LE( data, offset ) \
196 ( \
197 ( (uint16_t) ( data )[( offset ) ] ) \
198 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100199 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100200#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100201
Joe Subbiani394bdd62021-07-07 15:16:56 +0100202/**
Joe Subbiani6a506312021-07-07 16:56:29 +0100203 * 16-bit integer manipulation PUT macros (little endian)
Joe Subbiani394bdd62021-07-07 15:16:56 +0100204 *
Joe Subbiani6a506312021-07-07 16:56:29 +0100205 * \brief Read from a 16 bit integer and store each byte
206 * in memory, offset by a specified amount, resulting
207 * in each byte being adjacent in memory.
208 * Little Endian is used when wanting to
209 * transmit the least signifcant bits first
Joe Subbiani394bdd62021-07-07 15:16:56 +0100210 *
Joe Subbiani6a506312021-07-07 16:56:29 +0100211 * \param n 16 bit integer where data is accessed
Joe Subbiani394bdd62021-07-07 15:16:56 +0100212 * \param b const unsigned char array of data to be
213 * manipulated
214 * \param i offset in bytes, In the case of UINT16, i
215 * would increment by 2 every use assuming
216 * the data is being stored in the same location
217 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100218#ifndef MBEDTLS_PUT_UINT16_LE
219#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \
220{ \
221 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
222 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
223}
224#endif
225
226
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200227#endif /* MBEDTLS_LIBRARY_COMMON_H */