blob: fb4194e2702046356238b33df26ed7e13a7345ab [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
26#if defined(MBEDTLS_CONFIG_FILE)
27#include MBEDTLS_CONFIG_FILE
28#else
29#include "mbedtls/config.h"
30#endif
31
Joe Subbianic045dc12021-07-14 12:31:31 +010032#include <stdint.h>
33
Gilles Peskinec4672fd2019-09-11 13:39:11 +020034/** Helper to define a function as static except when building invasive tests.
35 *
36 * If a function is only used inside its own source file and should be
37 * declared `static` to allow the compiler to optimize for code size,
38 * but that function has unit tests, define it with
39 * ```
40 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
41 * ```
42 * and declare it in a header in the `library/` directory with
43 * ```
44 * #if defined(MBEDTLS_TEST_HOOKS)
45 * int mbedtls_foo(...);
46 * #endif
47 * ```
48 */
49#if defined(MBEDTLS_TEST_HOOKS)
50#define MBEDTLS_STATIC_TESTABLE
51#else
52#define MBEDTLS_STATIC_TESTABLE static
53#endif
54
Joe Subbianiba486b02021-06-22 15:51:53 +010055/** Allow library to access its structs' private members.
56 *
57 * Although structs defined in header files are publicly available,
58 * their members are private and should not be accessed by the user.
59 */
60#define MBEDTLS_ALLOW_PRIVATE_ACCESS
61
62/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010063 *
Joe Subbiani266476d2021-07-07 15:16:56 +010064 * Obtain the most significant byte of x using 0xff
65 * Using MBEDTLS_BYTE_a will shift a*8 bits
66 * to retrieve the next byte of information
Joe Subbianiba486b02021-06-22 15:51:53 +010067 */
Joe Subbianic045dc12021-07-14 12:31:31 +010068#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani2bbafda2021-06-24 13:00:03 +010069#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
70#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
71#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianic045dc12021-07-14 12:31:31 +010072#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
73#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
74#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
75#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbiani6b897c92021-07-08 14:59:52 +010076
Joe Subbiani266476d2021-07-07 15:16:56 +010077/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +010078 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +010079 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +010080 *
Joe Subbiani0a65d532021-07-14 11:53:07 +010081 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +010082 * \param offset Offset from \p base of the first and most significant
83 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +010084 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010085 */
86#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +010087#define MBEDTLS_GET_UINT32_BE( data , offset ) \
88 ( \
89 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
90 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
91 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
92 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +010093 )
94#endif
95
96/**
Joe Subbiani0a65d532021-07-14 11:53:07 +010097 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010098 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +010099 * \param n 32 bits unsigned integer to put in memory.
100 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100101 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100102 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100103 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100104 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100105#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100106#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
107{ \
108 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
109 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
110 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
111 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
112}
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100113#endif
114
Joe Subbiani266476d2021-07-07 15:16:56 +0100115/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100116 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100117 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100118 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100119 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100120 * \param offset Offset from \p base of the first and least significant
121 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100122 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100123 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100124#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100125#define MBEDTLS_GET_UINT32_LE( data, offset ) \
126 ( \
127 ( (uint32_t) ( data )[( offset ) ] ) \
128 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
129 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
130 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100131 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100132#endif
133
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100134/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100135 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100136 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100137 * \param n 32 bits unsigned integer to put in memory.
138 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100139 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100140 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100141 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100142 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100143#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100144#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
145{ \
146 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
147 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
148 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
149 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
150}
Joe Subbiani4fb75552021-06-23 12:16:47 +0100151#endif
152
Joe Subbiani61f7d732021-06-24 09:06:23 +0100153/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100154 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100155 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100156 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100157 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100158 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100159 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100160 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100161 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100162#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100163#define MBEDTLS_GET_UINT16_LE( data, offset ) \
164 ( \
165 ( (uint16_t) ( data )[( offset ) ] ) \
166 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100167 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100168#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100169
Joe Subbiani266476d2021-07-07 15:16:56 +0100170/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100171 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100172 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100173 * \param n 16 bits unsigned integer to put in memory.
174 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100175 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100176 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100177 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100178 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100179#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100180#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
181{ \
182 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
183 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100184}
185#endif
186
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100187/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100188 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100189 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100190 *
191 * \param data Base address of the memory to get the two bytes from.
192 * \param offset Offset from \p base of the first and most significant
193 * byte of the two bytes to build the 16 bits unsigned
194 * integer from.
195 */
196#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100197#define MBEDTLS_GET_UINT16_BE( data, offset ) \
198 ( \
199 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
200 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100201 )
202#endif
203
204/**
205 * Put in memory a 16 bits unsigned integer in big-endian order.
206 *
207 * \param n 16 bits unsigned integer to put in memory.
208 * \param data Base address of the memory where to put the 16
209 * bits unsigned integer in.
210 * \param offset Offset from \p base where to put the most significant
211 * byte of the 16 bits unsigned integer \p n.
212 */
213#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100214#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
215{ \
216 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
217 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbianic54e9082021-07-19 11:56:54 +0100218}
219#endif
220
221/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100222 * Get the unsigned 64 bits integer corresponding to eight bytes in
223 * big-endian order (MSB first).
224 *
225 * \param data Base address of the memory to get the eight bytes from.
226 * \param offset Offset from \p base of the first and most significant
227 * byte of the eight bytes to build the 64 bits unsigned
228 * integer from.
229 */
230#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100231#define MBEDTLS_GET_UINT64_BE( data, offset ) \
232 ( \
233 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
234 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
235 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
236 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
237 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
238 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
239 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
240 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100241 )
242#endif
243
244/**
245 * Put in memory a 64 bits unsigned integer in big-endian order.
246 *
247 * \param n 64 bits unsigned integer to put in memory.
248 * \param data Base address of the memory where to put the 64
249 * bits unsigned integer in.
250 * \param offset Offset from \p base where to put the most significant
251 * byte of the 64 bits unsigned integer \p n.
252 */
253#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100254#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
255{ \
256 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
257 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
258 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
259 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
260 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
261 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
262 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
263 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100264}
265#endif
266
267/**
268 * Get the unsigned 64 bits integer corresponding to eight bytes in
269 * little-endian order (LSB first).
270 *
271 * \param data Base address of the memory to get the eight bytes from.
272 * \param offset Offset from \p base of the first and least significant
273 * byte of the eight bytes to build the 64 bits unsigned
274 * integer from.
275 */
276#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100277#define MBEDTLS_GET_UINT64_LE( data, offset ) \
278 ( \
279 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
280 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
281 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
282 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
283 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
284 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
285 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
286 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100287 )
288#endif
289
290/**
291 * Put in memory a 64 bits unsigned integer in little-endian order.
292 *
293 * \param n 64 bits unsigned integer to put in memory.
294 * \param data Base address of the memory where to put the 64
295 * bits unsigned integer in.
296 * \param offset Offset from \p base where to put the least significant
297 * byte of the 64 bits unsigned integer \p n.
298 */
299#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100300#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
301{ \
302 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
303 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
304 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
305 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
306 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
307 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
308 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
309 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100310}
311#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100312
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200313#endif /* MBEDTLS_LIBRARY_COMMON_H */