blob: c35bc04269a5c1d70cc2778fbf4ca8682b629c4b [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 Subbianid3a3f212021-07-21 15:22:47 +010064 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
65 * byte from x, where byte 0 is the least significant byte.
Joe Subbianiba486b02021-06-22 15:51:53 +010066 */
Joe Subbianic045dc12021-07-14 12:31:31 +010067#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani2bbafda2021-06-24 13:00:03 +010068#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
69#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
70#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianic045dc12021-07-14 12:31:31 +010071#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
72#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
73#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
74#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbiani6b897c92021-07-08 14:59:52 +010075
Joe Subbiani266476d2021-07-07 15:16:56 +010076/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +010077 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +010078 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +010079 *
Joe Subbiani0a65d532021-07-14 11:53:07 +010080 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +010081 * \param offset Offset from \p base of the first and most significant
82 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +010083 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010084 */
85#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +010086#define MBEDTLS_GET_UINT32_BE( data , offset ) \
87 ( \
88 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
89 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
90 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
91 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +010092 )
93#endif
94
95/**
Joe Subbiani0a65d532021-07-14 11:53:07 +010096 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010097 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +010098 * \param n 32 bits unsigned integer to put in memory.
99 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100100 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100101 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100102 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100103 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100104#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100105#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
106{ \
107 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
108 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
109 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
110 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
111}
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100112#endif
113
Joe Subbiani266476d2021-07-07 15:16:56 +0100114/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100115 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100116 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100117 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100118 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100119 * \param offset Offset from \p base of the first and least significant
120 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100121 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100122 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100123#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100124#define MBEDTLS_GET_UINT32_LE( data, offset ) \
125 ( \
126 ( (uint32_t) ( data )[( offset ) ] ) \
127 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
128 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
129 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100130 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100131#endif
132
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100133/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100134 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100135 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100136 * \param n 32 bits unsigned integer to put in memory.
137 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100138 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100139 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100140 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100141 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100142#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100143#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
144{ \
145 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
146 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
147 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
148 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
149}
Joe Subbiani4fb75552021-06-23 12:16:47 +0100150#endif
151
Joe Subbiani61f7d732021-06-24 09:06:23 +0100152/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100153 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100154 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100155 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100156 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100157 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100158 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100159 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100160 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100161#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100162#define MBEDTLS_GET_UINT16_LE( data, offset ) \
163 ( \
164 ( (uint16_t) ( data )[( offset ) ] ) \
165 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100166 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100167#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100168
Joe Subbiani266476d2021-07-07 15:16:56 +0100169/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100170 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100171 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100172 * \param n 16 bits unsigned integer to put in memory.
173 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100174 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100175 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100176 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100177 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100178#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100179#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
180{ \
181 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
182 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100183}
184#endif
185
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100186/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100187 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100188 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100189 *
190 * \param data Base address of the memory to get the two bytes from.
191 * \param offset Offset from \p base of the first and most significant
192 * byte of the two bytes to build the 16 bits unsigned
193 * integer from.
194 */
195#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100196#define MBEDTLS_GET_UINT16_BE( data, offset ) \
197 ( \
198 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
199 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100200 )
201#endif
202
203/**
204 * Put in memory a 16 bits unsigned integer in big-endian order.
205 *
206 * \param n 16 bits unsigned integer to put in memory.
207 * \param data Base address of the memory where to put the 16
208 * bits unsigned integer in.
209 * \param offset Offset from \p base where to put the most significant
210 * byte of the 16 bits unsigned integer \p n.
211 */
212#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100213#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
214{ \
215 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
216 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbianic54e9082021-07-19 11:56:54 +0100217}
218#endif
219
220/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100221 * Get the unsigned 64 bits integer corresponding to eight bytes in
222 * big-endian order (MSB first).
223 *
224 * \param data Base address of the memory to get the eight bytes from.
225 * \param offset Offset from \p base of the first and most significant
226 * byte of the eight bytes to build the 64 bits unsigned
227 * integer from.
228 */
229#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100230#define MBEDTLS_GET_UINT64_BE( data, offset ) \
231 ( \
232 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
233 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
234 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
235 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
236 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
237 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
238 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
239 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100240 )
241#endif
242
243/**
244 * Put in memory a 64 bits unsigned integer in big-endian order.
245 *
246 * \param n 64 bits unsigned integer to put in memory.
247 * \param data Base address of the memory where to put the 64
248 * bits unsigned integer in.
249 * \param offset Offset from \p base where to put the most significant
250 * byte of the 64 bits unsigned integer \p n.
251 */
252#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100253#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
254{ \
255 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
256 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
257 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
258 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
259 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
260 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
261 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
262 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100263}
264#endif
265
266/**
267 * Get the unsigned 64 bits integer corresponding to eight bytes in
268 * little-endian order (LSB first).
269 *
270 * \param data Base address of the memory to get the eight bytes from.
271 * \param offset Offset from \p base of the first and least significant
272 * byte of the eight bytes to build the 64 bits unsigned
273 * integer from.
274 */
275#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100276#define MBEDTLS_GET_UINT64_LE( data, offset ) \
277 ( \
278 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
279 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
280 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
281 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
282 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
283 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
284 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
285 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100286 )
287#endif
288
289/**
290 * Put in memory a 64 bits unsigned integer in little-endian order.
291 *
292 * \param n 64 bits unsigned integer to put in memory.
293 * \param data Base address of the memory where to put the 64
294 * bits unsigned integer in.
295 * \param offset Offset from \p base where to put the least significant
296 * byte of the 64 bits unsigned integer \p n.
297 */
298#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100299#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
300{ \
301 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
302 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
303 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
304 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
305 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
306 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
307 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
308 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100309}
310#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100311
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200312#endif /* MBEDTLS_LIBRARY_COMMON_H */