blob: c06472418dfb2d1f1e00599a7db33afd3b8e0007 [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/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010056 *
Joe Subbiani8799e542021-07-21 16:35:48 +010057 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid3a3f212021-07-21 15:22:47 +010058 * byte from x, where byte 0 is the least significant byte.
Joe Subbianiba486b02021-06-22 15:51:53 +010059 */
Joe Subbianic045dc12021-07-14 12:31:31 +010060#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani2bbafda2021-06-24 13:00:03 +010061#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
62#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
63#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianic045dc12021-07-14 12:31:31 +010064#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
65#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
66#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
67#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbiani6b897c92021-07-08 14:59:52 +010068
Joe Subbiani266476d2021-07-07 15:16:56 +010069/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +010070 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +010071 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +010072 *
Joe Subbiani0a65d532021-07-14 11:53:07 +010073 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +010074 * \param offset Offset from \p base of the first and most significant
75 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +010076 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010077 */
78#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +010079#define MBEDTLS_GET_UINT32_BE( data , offset ) \
80 ( \
81 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
82 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
83 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
84 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +010085 )
86#endif
87
88/**
Joe Subbiani0a65d532021-07-14 11:53:07 +010089 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010090 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +010091 * \param n 32 bits unsigned integer to put in memory.
92 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +010093 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +010094 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +010095 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +010096 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +010097#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +010098#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
99{ \
100 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
101 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
102 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
103 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
104}
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100105#endif
106
Joe Subbiani266476d2021-07-07 15:16:56 +0100107/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100108 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100109 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100110 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100111 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100112 * \param offset Offset from \p base of the first and least significant
113 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100114 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100115 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100116#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100117#define MBEDTLS_GET_UINT32_LE( data, offset ) \
118 ( \
119 ( (uint32_t) ( data )[( offset ) ] ) \
120 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
121 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
122 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100123 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100124#endif
125
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100126/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100127 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100128 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100129 * \param n 32 bits unsigned integer to put in memory.
130 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100131 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100132 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100133 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100134 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100135#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100136#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
137{ \
138 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
139 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
140 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
141 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
142}
Joe Subbiani4fb75552021-06-23 12:16:47 +0100143#endif
144
Joe Subbiani61f7d732021-06-24 09:06:23 +0100145/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100146 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100147 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100148 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100149 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100150 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100151 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100152 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100153 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100154#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100155#define MBEDTLS_GET_UINT16_LE( data, offset ) \
156 ( \
157 ( (uint16_t) ( data )[( offset ) ] ) \
158 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100159 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100160#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100161
Joe Subbiani266476d2021-07-07 15:16:56 +0100162/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100163 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100164 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100165 * \param n 16 bits unsigned integer to put in memory.
166 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100167 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100168 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100169 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100170 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100171#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100172#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
173{ \
174 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
175 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100176}
177#endif
178
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100179/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100180 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100181 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100182 *
183 * \param data Base address of the memory to get the two bytes from.
184 * \param offset Offset from \p base of the first and most significant
185 * byte of the two bytes to build the 16 bits unsigned
186 * integer from.
187 */
188#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100189#define MBEDTLS_GET_UINT16_BE( data, offset ) \
190 ( \
191 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
192 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100193 )
194#endif
195
196/**
197 * Put in memory a 16 bits unsigned integer in big-endian order.
198 *
199 * \param n 16 bits unsigned integer to put in memory.
200 * \param data Base address of the memory where to put the 16
201 * bits unsigned integer in.
202 * \param offset Offset from \p base where to put the most significant
203 * byte of the 16 bits unsigned integer \p n.
204 */
205#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100206#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
207{ \
208 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
209 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbianic54e9082021-07-19 11:56:54 +0100210}
211#endif
212
213/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100214 * Get the unsigned 64 bits integer corresponding to eight bytes in
215 * big-endian order (MSB first).
216 *
217 * \param data Base address of the memory to get the eight bytes from.
218 * \param offset Offset from \p base of the first and most significant
219 * byte of the eight bytes to build the 64 bits unsigned
220 * integer from.
221 */
222#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100223#define MBEDTLS_GET_UINT64_BE( data, offset ) \
224 ( \
225 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
226 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
227 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
228 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
229 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
230 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
231 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
232 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100233 )
234#endif
235
236/**
237 * Put in memory a 64 bits unsigned integer in big-endian order.
238 *
239 * \param n 64 bits unsigned integer to put in memory.
240 * \param data Base address of the memory where to put the 64
241 * bits unsigned integer in.
242 * \param offset Offset from \p base where to put the most significant
243 * byte of the 64 bits unsigned integer \p n.
244 */
245#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100246#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
247{ \
248 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
249 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
250 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
251 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
252 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
253 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
254 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
255 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100256}
257#endif
258
259/**
260 * Get the unsigned 64 bits integer corresponding to eight bytes in
261 * little-endian order (LSB first).
262 *
263 * \param data Base address of the memory to get the eight bytes from.
264 * \param offset Offset from \p base of the first and least significant
265 * byte of the eight bytes to build the 64 bits unsigned
266 * integer from.
267 */
268#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100269#define MBEDTLS_GET_UINT64_LE( data, offset ) \
270 ( \
271 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
272 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
273 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
274 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
275 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
276 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
277 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
278 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100279 )
280#endif
281
282/**
283 * Put in memory a 64 bits unsigned integer in little-endian order.
284 *
285 * \param n 64 bits unsigned integer to put in memory.
286 * \param data Base address of the memory where to put the 64
287 * bits unsigned integer in.
288 * \param offset Offset from \p base where to put the least significant
289 * byte of the 64 bits unsigned integer \p n.
290 */
291#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100292#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
293{ \
294 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
295 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
296 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
297 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
298 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
299 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
300 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
301 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100302}
303#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100304
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200305#endif /* MBEDTLS_LIBRARY_COMMON_H */