blob: 780ce378dece19eda8c458859b5211604f4c8427 [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
Joe Subbiani2194dc42021-07-14 12:31:31 +010028#include <stdint.h>
29
Gilles Peskinec4672fd2019-09-11 13:39:11 +020030/** Helper to define a function as static except when building invasive tests.
31 *
32 * If a function is only used inside its own source file and should be
33 * declared `static` to allow the compiler to optimize for code size,
34 * but that function has unit tests, define it with
35 * ```
36 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
37 * ```
38 * and declare it in a header in the `library/` directory with
39 * ```
40 * #if defined(MBEDTLS_TEST_HOOKS)
41 * int mbedtls_foo(...);
42 * #endif
43 * ```
44 */
45#if defined(MBEDTLS_TEST_HOOKS)
46#define MBEDTLS_STATIC_TESTABLE
47#else
48#define MBEDTLS_STATIC_TESTABLE static
49#endif
50
TRodziewicz7871c2e2021-07-07 17:29:43 +020051#if defined(MBEDTLS_TEST_HOOKS)
52extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
53#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
54 do { \
55 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
56 { \
57 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
58 } \
59 } while( 0 )
60#else
61#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
62#endif /* defined(MBEDTLS_TEST_HOOKS) */
63
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020064/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020065 *
66 * Although structs defined in header files are publicly available,
67 * their members are private and should not be accessed by the user.
68 */
69#define MBEDTLS_ALLOW_PRIVATE_ACCESS
70
Joe Subbiani50dde562021-06-22 15:51:53 +010071/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010072 *
Joe Subbiani9ab18662021-07-21 16:35:48 +010073 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +010074 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +010075 */
Joe Subbiani2194dc42021-07-14 12:31:31 +010076#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +010077#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
78#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
79#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +010080#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
81#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
82#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
83#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +010084
Joe Subbiani394bdd62021-07-07 15:16:56 +010085/**
Joe Subbianif5462d92021-07-13 12:13:19 +010086 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +010087 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +010088 *
Joe Subbiani635231a2021-07-14 11:53:07 +010089 * \param data Base address of the memory to get the four bytes from.
Joe Subbianif5462d92021-07-13 12:13:19 +010090 * \param offset Offset from \p base of the first and most significant
91 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +010092 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +010093 */
94#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +010095#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] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100101 )
102#endif
103
104/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100105 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100106 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100107 * \param n 32 bits unsigned integer to put in memory.
108 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100109 * bits unsigned integer in.
Joe Subbianif5462d92021-07-13 12:13:19 +0100110 * \param offset Offset from \p base where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100111 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100112 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100113#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100114#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
115{ \
116 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
117 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
118 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
119 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
120}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100121#endif
122
Joe Subbiani394bdd62021-07-07 15:16:56 +0100123/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100124 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100125 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100126 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100127 * \param data Base address of the memory to get the four bytes from.
Joe Subbianif5462d92021-07-13 12:13:19 +0100128 * \param offset Offset from \p base of the first and least significant
129 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100130 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100131 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100132#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100133#define MBEDTLS_GET_UINT32_LE( data, offset ) \
134 ( \
135 ( (uint32_t) ( data )[( offset ) ] ) \
136 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
137 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
138 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100139 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100140#endif
141
Joe Subbiani6a506312021-07-07 16:56:29 +0100142/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100143 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100144 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100145 * \param n 32 bits unsigned integer to put in memory.
146 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100147 * bits unsigned integer in.
Joe Subbianif5462d92021-07-13 12:13:19 +0100148 * \param offset Offset from \p base where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100149 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100150 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100151#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100152#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
153{ \
154 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
155 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
156 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
157 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
158}
Joe Subbiani54c61342021-06-23 12:16:47 +0100159#endif
160
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100161/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100162 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100163 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100164 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100165 * \param data Base address of the memory to get the two bytes from.
Joe Subbianif5462d92021-07-13 12:13:19 +0100166 * \param offset Offset from \p base of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100167 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100168 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100169 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100170#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100171#define MBEDTLS_GET_UINT16_LE( data, offset ) \
172 ( \
173 ( (uint16_t) ( data )[( offset ) ] ) \
174 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100175 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100176#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100177
Joe Subbiani394bdd62021-07-07 15:16:56 +0100178/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100179 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100180 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100181 * \param n 16 bits unsigned integer to put in memory.
182 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100183 * bits unsigned integer in.
Joe Subbianif5462d92021-07-13 12:13:19 +0100184 * \param offset Offset from \p base where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100185 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100186 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100187#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100188#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
189{ \
190 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
191 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100192}
193#endif
194
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100195/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100196 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100197 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100198 *
199 * \param data Base address of the memory to get the two bytes from.
200 * \param offset Offset from \p base of the first and most significant
201 * byte of the two bytes to build the 16 bits unsigned
202 * integer from.
203 */
204#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100205#define MBEDTLS_GET_UINT16_BE( data, offset ) \
206 ( \
207 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
208 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100209 )
210#endif
211
212/**
213 * Put in memory a 16 bits unsigned integer in big-endian order.
214 *
215 * \param n 16 bits unsigned integer to put in memory.
216 * \param data Base address of the memory where to put the 16
217 * bits unsigned integer in.
218 * \param offset Offset from \p base where to put the most significant
219 * byte of the 16 bits unsigned integer \p n.
220 */
221#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100222#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
223{ \
224 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
225 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100226}
227#endif
228
229/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100230 * Get the unsigned 64 bits integer corresponding to eight bytes in
231 * big-endian order (MSB first).
232 *
233 * \param data Base address of the memory to get the eight bytes from.
234 * \param offset Offset from \p base of the first and most significant
235 * byte of the eight bytes to build the 64 bits unsigned
236 * integer from.
237 */
238#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100239#define MBEDTLS_GET_UINT64_BE( data, offset ) \
240 ( \
241 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
242 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
243 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
244 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
245 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
246 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
247 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
248 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100249 )
250#endif
251
252/**
253 * Put in memory a 64 bits unsigned integer in big-endian order.
254 *
255 * \param n 64 bits unsigned integer to put in memory.
256 * \param data Base address of the memory where to put the 64
257 * bits unsigned integer in.
258 * \param offset Offset from \p base where to put the most significant
259 * byte of the 64 bits unsigned integer \p n.
260 */
261#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100262#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
263{ \
264 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
265 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
266 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
267 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
268 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
269 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
270 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
271 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100272}
273#endif
274
275/**
276 * Get the unsigned 64 bits integer corresponding to eight bytes in
277 * little-endian order (LSB first).
278 *
279 * \param data Base address of the memory to get the eight bytes from.
280 * \param offset Offset from \p base of the first and least significant
281 * byte of the eight bytes to build the 64 bits unsigned
282 * integer from.
283 */
284#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100285#define MBEDTLS_GET_UINT64_LE( data, offset ) \
286 ( \
287 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
288 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
289 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
290 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
291 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
292 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
293 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
294 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100295 )
296#endif
297
298/**
299 * Put in memory a 64 bits unsigned integer in little-endian order.
300 *
301 * \param n 64 bits unsigned integer to put in memory.
302 * \param data Base address of the memory where to put the 64
303 * bits unsigned integer in.
304 * \param offset Offset from \p base where to put the least significant
305 * byte of the 64 bits unsigned integer \p n.
306 */
307#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100308#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
309{ \
310 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
311 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
312 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
313 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
314 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
315 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
316 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
317 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100318}
319#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100320
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200321#endif /* MBEDTLS_LIBRARY_COMMON_H */