blob: 0169325bb97d2b740fd82728adeeb9800381ad8d [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
Gilles Peskine01bf6312022-11-23 14:15:57 +010032#include <stddef.h>
Joe Subbianic045dc12021-07-14 12:31:31 +010033#include <stdint.h>
34
Gilles Peskine8fe23a02022-11-23 17:24:37 +010035/* Define `inline` on some non-C99-compliant compilers. */
36#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
37 !defined(inline) && !defined(__cplusplus)
38#define inline __inline
39#endif
40
Gilles Peskinec4672fd2019-09-11 13:39:11 +020041/** Helper to define a function as static except when building invasive tests.
42 *
43 * If a function is only used inside its own source file and should be
44 * declared `static` to allow the compiler to optimize for code size,
45 * but that function has unit tests, define it with
46 * ```
47 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
48 * ```
49 * and declare it in a header in the `library/` directory with
50 * ```
51 * #if defined(MBEDTLS_TEST_HOOKS)
52 * int mbedtls_foo(...);
53 * #endif
54 * ```
55 */
56#if defined(MBEDTLS_TEST_HOOKS)
57#define MBEDTLS_STATIC_TESTABLE
58#else
59#define MBEDTLS_STATIC_TESTABLE static
60#endif
61
Gilles Peskine01bf6312022-11-23 14:15:57 +010062/** Return an offset into a buffer.
63 *
64 * This is just the addition of an offset to a pointer, except that this
65 * function also accepts an offset of 0 into a buffer whose pointer is null.
66 *
67 * \param p Pointer to a buffer of at least n bytes.
68 * This may be \p NULL if \p n is zero.
69 * \param n An offset in bytes.
70 * \return Pointer to offset \p n in the buffer \p p.
71 * Note that this is only a valid pointer if the size of the
72 * buffer is at least \p n + 1.
73 */
74static inline unsigned char *mbedtls_buffer_offset(
75 unsigned char *p, size_t n )
76{
77 return( p == NULL ? NULL : p + n );
78}
79
80/** Return an offset into a read-only buffer.
81 *
82 * This is just the addition of an offset to a pointer, except that this
83 * function also accepts an offset of 0 into a buffer whose pointer is null.
84 *
85 * \param p Pointer to a buffer of at least n bytes.
86 * This may be \p NULL if \p n is zero.
87 * \param n An offset in bytes.
88 * \return Pointer to offset \p n in the buffer \p p.
89 * Note that this is only a valid pointer if the size of the
90 * buffer is at least \p n + 1.
91 */
92static inline const unsigned char *mbedtls_buffer_offset_const(
93 const unsigned char *p, size_t n )
94{
95 return( p == NULL ? NULL : p + n );
96}
97
Joe Subbianiba486b02021-06-22 15:51:53 +010098/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010099 *
Joe Subbiani8799e542021-07-21 16:35:48 +0100100 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid3a3f212021-07-21 15:22:47 +0100101 * byte from x, where byte 0 is the least significant byte.
Joe Subbianiba486b02021-06-22 15:51:53 +0100102 */
Joe Subbianic045dc12021-07-14 12:31:31 +0100103#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100104#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
105#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
106#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianic045dc12021-07-14 12:31:31 +0100107#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
108#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
109#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
110#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbiani6b897c92021-07-08 14:59:52 +0100111
Joe Subbiani266476d2021-07-07 15:16:56 +0100112/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100113 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100114 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +0100115 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100116 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100117 * \param offset Offset from \p base of the first and most significant
118 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100119 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100120 */
121#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100122#define MBEDTLS_GET_UINT32_BE( data , offset ) \
123 ( \
124 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
125 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
126 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
127 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100128 )
129#endif
130
131/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100132 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100133 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100134 * \param n 32 bits unsigned integer to put in memory.
135 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100136 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100137 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100138 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100139 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100140#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100141#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
142{ \
143 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
144 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
145 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
146 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
147}
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100148#endif
149
Joe Subbiani266476d2021-07-07 15:16:56 +0100150/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100151 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100152 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100153 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100154 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100155 * \param offset Offset from \p base of the first and least significant
156 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100157 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100158 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100159#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100160#define MBEDTLS_GET_UINT32_LE( data, offset ) \
161 ( \
162 ( (uint32_t) ( data )[( offset ) ] ) \
163 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
164 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
165 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100166 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100167#endif
168
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100169/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100170 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100171 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100172 * \param n 32 bits unsigned integer to put in memory.
173 * \param data Base address of the memory where to put the 32
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 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100177 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100178#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100179#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
180{ \
181 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
182 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
183 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
184 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
185}
Joe Subbiani4fb75552021-06-23 12:16:47 +0100186#endif
187
Joe Subbiani61f7d732021-06-24 09:06:23 +0100188/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100189 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100190 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100191 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100192 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100193 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100194 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100195 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100196 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100197#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100198#define MBEDTLS_GET_UINT16_LE( data, offset ) \
199 ( \
200 ( (uint16_t) ( data )[( offset ) ] ) \
201 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100202 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100203#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100204
Joe Subbiani266476d2021-07-07 15:16:56 +0100205/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100206 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100207 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100208 * \param n 16 bits unsigned integer to put in memory.
209 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100210 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100211 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100212 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100213 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100214#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100215#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
216{ \
217 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
218 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100219}
220#endif
221
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100222/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100223 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100224 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100225 *
226 * \param data Base address of the memory to get the two bytes from.
227 * \param offset Offset from \p base of the first and most significant
228 * byte of the two bytes to build the 16 bits unsigned
229 * integer from.
230 */
231#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100232#define MBEDTLS_GET_UINT16_BE( data, offset ) \
233 ( \
234 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
235 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100236 )
237#endif
238
239/**
240 * Put in memory a 16 bits unsigned integer in big-endian order.
241 *
242 * \param n 16 bits unsigned integer to put in memory.
243 * \param data Base address of the memory where to put the 16
244 * bits unsigned integer in.
245 * \param offset Offset from \p base where to put the most significant
246 * byte of the 16 bits unsigned integer \p n.
247 */
248#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100249#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
250{ \
251 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
252 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbianic54e9082021-07-19 11:56:54 +0100253}
254#endif
255
256/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100257 * Get the unsigned 64 bits integer corresponding to eight bytes in
258 * big-endian order (MSB first).
259 *
260 * \param data Base address of the memory to get the eight bytes from.
261 * \param offset Offset from \p base of the first and most significant
262 * byte of the eight bytes to build the 64 bits unsigned
263 * integer from.
264 */
265#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100266#define MBEDTLS_GET_UINT64_BE( data, offset ) \
267 ( \
268 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
269 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
270 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
271 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
272 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
273 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
274 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
275 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100276 )
277#endif
278
279/**
280 * Put in memory a 64 bits unsigned integer in big-endian order.
281 *
282 * \param n 64 bits unsigned integer to put in memory.
283 * \param data Base address of the memory where to put the 64
284 * bits unsigned integer in.
285 * \param offset Offset from \p base where to put the most significant
286 * byte of the 64 bits unsigned integer \p n.
287 */
288#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100289#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
290{ \
291 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
292 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
293 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
294 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
295 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
296 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
297 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
298 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100299}
300#endif
301
302/**
303 * Get the unsigned 64 bits integer corresponding to eight bytes in
304 * little-endian order (LSB first).
305 *
306 * \param data Base address of the memory to get the eight bytes from.
307 * \param offset Offset from \p base of the first and least significant
308 * byte of the eight bytes to build the 64 bits unsigned
309 * integer from.
310 */
311#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100312#define MBEDTLS_GET_UINT64_LE( data, offset ) \
313 ( \
314 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
315 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
316 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
317 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
318 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
319 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
320 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
321 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100322 )
323#endif
324
325/**
326 * Put in memory a 64 bits unsigned integer in little-endian order.
327 *
328 * \param n 64 bits unsigned integer to put in memory.
329 * \param data Base address of the memory where to put the 64
330 * bits unsigned integer in.
331 * \param offset Offset from \p base where to put the least significant
332 * byte of the 64 bits unsigned integer \p n.
333 */
334#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100335#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
336{ \
337 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
338 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
339 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
340 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
341 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
342 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
343 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
344 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100345}
346#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100347
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200348#endif /* MBEDTLS_LIBRARY_COMMON_H */