blob: c36615680b38ac355e9d5ff50d246577a1418487 [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 Peskinec4672fd2019-09-11 13:39:11 +020035/** Helper to define a function as static except when building invasive tests.
36 *
37 * If a function is only used inside its own source file and should be
38 * declared `static` to allow the compiler to optimize for code size,
39 * but that function has unit tests, define it with
40 * ```
41 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
42 * ```
43 * and declare it in a header in the `library/` directory with
44 * ```
45 * #if defined(MBEDTLS_TEST_HOOKS)
46 * int mbedtls_foo(...);
47 * #endif
48 * ```
49 */
50#if defined(MBEDTLS_TEST_HOOKS)
51#define MBEDTLS_STATIC_TESTABLE
52#else
53#define MBEDTLS_STATIC_TESTABLE static
54#endif
55
Gilles Peskine01bf6312022-11-23 14:15:57 +010056/** Return an offset into a buffer.
57 *
58 * This is just the addition of an offset to a pointer, except that this
59 * function also accepts an offset of 0 into a buffer whose pointer is null.
60 *
61 * \param p Pointer to a buffer of at least n bytes.
62 * This may be \p NULL if \p n is zero.
63 * \param n An offset in bytes.
64 * \return Pointer to offset \p n in the buffer \p p.
65 * Note that this is only a valid pointer if the size of the
66 * buffer is at least \p n + 1.
67 */
68static inline unsigned char *mbedtls_buffer_offset(
69 unsigned char *p, size_t n )
70{
71 return( p == NULL ? NULL : p + n );
72}
73
74/** Return an offset into a read-only buffer.
75 *
76 * This is just the addition of an offset to a pointer, except that this
77 * function also accepts an offset of 0 into a buffer whose pointer is null.
78 *
79 * \param p Pointer to a buffer of at least n bytes.
80 * This may be \p NULL if \p n is zero.
81 * \param n An offset in bytes.
82 * \return Pointer to offset \p n in the buffer \p p.
83 * Note that this is only a valid pointer if the size of the
84 * buffer is at least \p n + 1.
85 */
86static inline const unsigned char *mbedtls_buffer_offset_const(
87 const unsigned char *p, size_t n )
88{
89 return( p == NULL ? NULL : p + n );
90}
91
Joe Subbianiba486b02021-06-22 15:51:53 +010092/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010093 *
Joe Subbiani8799e542021-07-21 16:35:48 +010094 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid3a3f212021-07-21 15:22:47 +010095 * byte from x, where byte 0 is the least significant byte.
Joe Subbianiba486b02021-06-22 15:51:53 +010096 */
Joe Subbianic045dc12021-07-14 12:31:31 +010097#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani2bbafda2021-06-24 13:00:03 +010098#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
99#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
100#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianic045dc12021-07-14 12:31:31 +0100101#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
102#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
103#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
104#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbiani6b897c92021-07-08 14:59:52 +0100105
Joe Subbiani266476d2021-07-07 15:16:56 +0100106/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100107 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100108 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +0100109 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100110 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100111 * \param offset Offset from \p base of the first and most significant
112 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100113 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100114 */
115#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100116#define MBEDTLS_GET_UINT32_BE( data , offset ) \
117 ( \
118 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
119 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
120 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
121 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100122 )
123#endif
124
125/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100126 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100127 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100128 * \param n 32 bits unsigned integer to put in memory.
129 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100130 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100131 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100132 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100133 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100134#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100135#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
136{ \
137 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
138 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
139 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
140 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
141}
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100142#endif
143
Joe Subbiani266476d2021-07-07 15:16:56 +0100144/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100145 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100146 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100147 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100148 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100149 * \param offset Offset from \p base of the first and least significant
150 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100151 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100152 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100153#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100154#define MBEDTLS_GET_UINT32_LE( data, offset ) \
155 ( \
156 ( (uint32_t) ( data )[( offset ) ] ) \
157 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
158 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
159 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100160 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100161#endif
162
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100163/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100164 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100165 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100166 * \param n 32 bits unsigned integer to put in memory.
167 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100168 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100169 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100170 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100171 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100172#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100173#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
174{ \
175 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
176 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
177 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
178 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
179}
Joe Subbiani4fb75552021-06-23 12:16:47 +0100180#endif
181
Joe Subbiani61f7d732021-06-24 09:06:23 +0100182/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100183 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100184 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100185 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100186 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100187 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100188 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100189 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100190 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100191#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100192#define MBEDTLS_GET_UINT16_LE( data, offset ) \
193 ( \
194 ( (uint16_t) ( data )[( offset ) ] ) \
195 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100196 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100197#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100198
Joe Subbiani266476d2021-07-07 15:16:56 +0100199/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100200 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100201 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100202 * \param n 16 bits unsigned integer to put in memory.
203 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100204 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100205 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100206 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100207 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100208#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100209#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
210{ \
211 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
212 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100213}
214#endif
215
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100216/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100217 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100218 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100219 *
220 * \param data Base address of the memory to get the two bytes from.
221 * \param offset Offset from \p base of the first and most significant
222 * byte of the two bytes to build the 16 bits unsigned
223 * integer from.
224 */
225#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100226#define MBEDTLS_GET_UINT16_BE( data, offset ) \
227 ( \
228 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
229 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100230 )
231#endif
232
233/**
234 * Put in memory a 16 bits unsigned integer in big-endian order.
235 *
236 * \param n 16 bits unsigned integer to put in memory.
237 * \param data Base address of the memory where to put the 16
238 * bits unsigned integer in.
239 * \param offset Offset from \p base where to put the most significant
240 * byte of the 16 bits unsigned integer \p n.
241 */
242#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100243#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
244{ \
245 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
246 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbianic54e9082021-07-19 11:56:54 +0100247}
248#endif
249
250/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100251 * Get the unsigned 64 bits integer corresponding to eight bytes in
252 * big-endian order (MSB first).
253 *
254 * \param data Base address of the memory to get the eight bytes from.
255 * \param offset Offset from \p base of the first and most significant
256 * byte of the eight bytes to build the 64 bits unsigned
257 * integer from.
258 */
259#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100260#define MBEDTLS_GET_UINT64_BE( data, offset ) \
261 ( \
262 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
263 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
264 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
265 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
266 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
267 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
268 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
269 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100270 )
271#endif
272
273/**
274 * Put in memory a 64 bits unsigned integer in big-endian order.
275 *
276 * \param n 64 bits unsigned integer to put in memory.
277 * \param data Base address of the memory where to put the 64
278 * bits unsigned integer in.
279 * \param offset Offset from \p base where to put the most significant
280 * byte of the 64 bits unsigned integer \p n.
281 */
282#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100283#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
284{ \
285 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
286 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
287 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
288 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
289 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
290 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
291 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
292 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100293}
294#endif
295
296/**
297 * Get the unsigned 64 bits integer corresponding to eight bytes in
298 * little-endian order (LSB first).
299 *
300 * \param data Base address of the memory to get the eight bytes from.
301 * \param offset Offset from \p base of the first and least significant
302 * byte of the eight bytes to build the 64 bits unsigned
303 * integer from.
304 */
305#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100306#define MBEDTLS_GET_UINT64_LE( data, offset ) \
307 ( \
308 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
309 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
310 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
311 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
312 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
313 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
314 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
315 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100316 )
317#endif
318
319/**
320 * Put in memory a 64 bits unsigned integer in little-endian order.
321 *
322 * \param n 64 bits unsigned integer to put in memory.
323 * \param data Base address of the memory where to put the 64
324 * bits unsigned integer in.
325 * \param offset Offset from \p base where to put the least significant
326 * byte of the 64 bits unsigned integer \p n.
327 */
328#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100329#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
330{ \
331 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
332 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
333 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
334 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
335 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
336 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
337 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
338 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100339}
340#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100341
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200342#endif /* MBEDTLS_LIBRARY_COMMON_H */