blob: 2786c97d40e968ac2a308ce3a55829b9136a4ec3 [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. */
David Horstmannceeaeb92023-01-05 15:44:23 +000036#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
Gilles Peskine8fe23a02022-11-23 17:24:37 +010037 !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.
Gilles Peskineff97f332022-11-25 13:34:59 +010066 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
67 * A null pointer is a valid buffer pointer when the size is 0, for example
68 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine01bf6312022-11-23 14:15:57 +010069 *
70 * \param p Pointer to a buffer of at least n bytes.
71 * This may be \p NULL if \p n is zero.
72 * \param n An offset in bytes.
73 * \return Pointer to offset \p n in the buffer \p p.
74 * Note that this is only a valid pointer if the size of the
75 * buffer is at least \p n + 1.
76 */
77static inline unsigned char *mbedtls_buffer_offset(
David Horstmannceeaeb92023-01-05 15:44:23 +000078 unsigned char *p, size_t n)
Gilles Peskine01bf6312022-11-23 14:15:57 +010079{
David Horstmannceeaeb92023-01-05 15:44:23 +000080 return p == NULL ? NULL : p + n;
Gilles Peskine01bf6312022-11-23 14:15:57 +010081}
82
83/** Return an offset into a read-only buffer.
84 *
Gilles Peskineff97f332022-11-25 13:34:59 +010085 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine01bf6312022-11-23 14:15:57 +010086 *
87 * \param p Pointer to a buffer of at least n bytes.
88 * This may be \p NULL if \p n is zero.
89 * \param n An offset in bytes.
90 * \return Pointer to offset \p n in the buffer \p p.
91 * Note that this is only a valid pointer if the size of the
92 * buffer is at least \p n + 1.
93 */
94static inline const unsigned char *mbedtls_buffer_offset_const(
David Horstmannceeaeb92023-01-05 15:44:23 +000095 const unsigned char *p, size_t n)
Gilles Peskine01bf6312022-11-23 14:15:57 +010096{
David Horstmannceeaeb92023-01-05 15:44:23 +000097 return p == NULL ? NULL : p + n;
Gilles Peskine01bf6312022-11-23 14:15:57 +010098}
99
Joe Subbianiba486b02021-06-22 15:51:53 +0100100/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +0100101 *
Joe Subbiani8799e542021-07-21 16:35:48 +0100102 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid3a3f212021-07-21 15:22:47 +0100103 * byte from x, where byte 0 is the least significant byte.
Joe Subbianiba486b02021-06-22 15:51:53 +0100104 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000105#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
106#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
107#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
108#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
109#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
110#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
111#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
112#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Joe Subbiani6b897c92021-07-08 14:59:52 +0100113
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 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +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 most significant
120 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100121 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100122 */
123#ifndef MBEDTLS_GET_UINT32_BE
David Horstmannceeaeb92023-01-05 15:44:23 +0000124#define MBEDTLS_GET_UINT32_BE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100125 ( \
David Horstmannceeaeb92023-01-05 15:44:23 +0000126 ((uint32_t) (data)[(offset)] << 24) \
127 | ((uint32_t) (data)[(offset) + 1] << 16) \
128 | ((uint32_t) (data)[(offset) + 2] << 8) \
129 | ((uint32_t) (data)[(offset) + 3]) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100130 )
131#endif
132
133/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100134 * Put in memory a 32 bits unsigned integer in big-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 most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100140 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100141 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100142#ifndef MBEDTLS_PUT_UINT32_BE
David Horstmannceeaeb92023-01-05 15:44:23 +0000143#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
144 { \
145 (data)[(offset)] = MBEDTLS_BYTE_3(n); \
146 (data)[(offset) + 1] = MBEDTLS_BYTE_2(n); \
147 (data)[(offset) + 2] = MBEDTLS_BYTE_1(n); \
148 (data)[(offset) + 3] = MBEDTLS_BYTE_0(n); \
149 }
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100150#endif
151
Joe Subbiani266476d2021-07-07 15:16:56 +0100152/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100153 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100154 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100155 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100156 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100157 * \param offset Offset from \p base of the first and least significant
158 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100159 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100160 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100161#ifndef MBEDTLS_GET_UINT32_LE
David Horstmannceeaeb92023-01-05 15:44:23 +0000162#define MBEDTLS_GET_UINT32_LE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100163 ( \
David Horstmannceeaeb92023-01-05 15:44:23 +0000164 ((uint32_t) (data)[(offset)]) \
165 | ((uint32_t) (data)[(offset) + 1] << 8) \
166 | ((uint32_t) (data)[(offset) + 2] << 16) \
167 | ((uint32_t) (data)[(offset) + 3] << 24) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100168 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100169#endif
170
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100171/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100172 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100173 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100174 * \param n 32 bits unsigned integer to put in memory.
175 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100176 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100177 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100178 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100179 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100180#ifndef MBEDTLS_PUT_UINT32_LE
David Horstmannceeaeb92023-01-05 15:44:23 +0000181#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
182 { \
183 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
184 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
185 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
186 (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \
187 }
Joe Subbiani4fb75552021-06-23 12:16:47 +0100188#endif
189
Joe Subbiani61f7d732021-06-24 09:06:23 +0100190/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100191 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100192 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100193 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100194 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100195 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100196 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100197 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100198 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100199#ifndef MBEDTLS_GET_UINT16_LE
David Horstmannceeaeb92023-01-05 15:44:23 +0000200#define MBEDTLS_GET_UINT16_LE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100201 ( \
David Horstmannceeaeb92023-01-05 15:44:23 +0000202 ((uint16_t) (data)[(offset)]) \
203 | ((uint16_t) (data)[(offset) + 1] << 8) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100204 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100205#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100206
Joe Subbiani266476d2021-07-07 15:16:56 +0100207/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100208 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100209 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100210 * \param n 16 bits unsigned integer to put in memory.
211 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100212 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100213 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100214 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100215 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100216#ifndef MBEDTLS_PUT_UINT16_LE
David Horstmannceeaeb92023-01-05 15:44:23 +0000217#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
218 { \
219 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
220 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
221 }
Joe Subbiani4530b272021-07-05 15:37:39 +0100222#endif
223
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100224/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100225 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100226 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100227 *
228 * \param data Base address of the memory to get the two bytes from.
229 * \param offset Offset from \p base of the first and most significant
230 * byte of the two bytes to build the 16 bits unsigned
231 * integer from.
232 */
233#ifndef MBEDTLS_GET_UINT16_BE
David Horstmannceeaeb92023-01-05 15:44:23 +0000234#define MBEDTLS_GET_UINT16_BE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100235 ( \
David Horstmannceeaeb92023-01-05 15:44:23 +0000236 ((uint16_t) (data)[(offset)] << 8) \
237 | ((uint16_t) (data)[(offset) + 1]) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100238 )
239#endif
240
241/**
242 * Put in memory a 16 bits unsigned integer in big-endian order.
243 *
244 * \param n 16 bits unsigned integer to put in memory.
245 * \param data Base address of the memory where to put the 16
246 * bits unsigned integer in.
247 * \param offset Offset from \p base where to put the most significant
248 * byte of the 16 bits unsigned integer \p n.
249 */
250#ifndef MBEDTLS_PUT_UINT16_BE
David Horstmannceeaeb92023-01-05 15:44:23 +0000251#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
252 { \
253 (data)[(offset)] = MBEDTLS_BYTE_1(n); \
254 (data)[(offset) + 1] = MBEDTLS_BYTE_0(n); \
255 }
Joe Subbianic54e9082021-07-19 11:56:54 +0100256#endif
257
258/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100259 * Get the unsigned 64 bits integer corresponding to eight bytes in
260 * big-endian order (MSB first).
261 *
262 * \param data Base address of the memory to get the eight bytes from.
263 * \param offset Offset from \p base of the first and most significant
264 * byte of the eight bytes to build the 64 bits unsigned
265 * integer from.
266 */
267#ifndef MBEDTLS_GET_UINT64_BE
David Horstmannceeaeb92023-01-05 15:44:23 +0000268#define MBEDTLS_GET_UINT64_BE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100269 ( \
David Horstmannceeaeb92023-01-05 15:44:23 +0000270 ((uint64_t) (data)[(offset)] << 56) \
271 | ((uint64_t) (data)[(offset) + 1] << 48) \
272 | ((uint64_t) (data)[(offset) + 2] << 40) \
273 | ((uint64_t) (data)[(offset) + 3] << 32) \
274 | ((uint64_t) (data)[(offset) + 4] << 24) \
275 | ((uint64_t) (data)[(offset) + 5] << 16) \
276 | ((uint64_t) (data)[(offset) + 6] << 8) \
277 | ((uint64_t) (data)[(offset) + 7]) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100278 )
279#endif
280
281/**
282 * Put in memory a 64 bits unsigned integer in big-endian order.
283 *
284 * \param n 64 bits unsigned integer to put in memory.
285 * \param data Base address of the memory where to put the 64
286 * bits unsigned integer in.
287 * \param offset Offset from \p base where to put the most significant
288 * byte of the 64 bits unsigned integer \p n.
289 */
290#ifndef MBEDTLS_PUT_UINT64_BE
David Horstmannceeaeb92023-01-05 15:44:23 +0000291#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
292 { \
293 (data)[(offset)] = MBEDTLS_BYTE_7(n); \
294 (data)[(offset) + 1] = MBEDTLS_BYTE_6(n); \
295 (data)[(offset) + 2] = MBEDTLS_BYTE_5(n); \
296 (data)[(offset) + 3] = MBEDTLS_BYTE_4(n); \
297 (data)[(offset) + 4] = MBEDTLS_BYTE_3(n); \
298 (data)[(offset) + 5] = MBEDTLS_BYTE_2(n); \
299 (data)[(offset) + 6] = MBEDTLS_BYTE_1(n); \
300 (data)[(offset) + 7] = MBEDTLS_BYTE_0(n); \
301 }
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100302#endif
303
304/**
305 * Get the unsigned 64 bits integer corresponding to eight bytes in
306 * little-endian order (LSB first).
307 *
308 * \param data Base address of the memory to get the eight bytes from.
309 * \param offset Offset from \p base of the first and least significant
310 * byte of the eight bytes to build the 64 bits unsigned
311 * integer from.
312 */
313#ifndef MBEDTLS_GET_UINT64_LE
David Horstmannceeaeb92023-01-05 15:44:23 +0000314#define MBEDTLS_GET_UINT64_LE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100315 ( \
David Horstmannceeaeb92023-01-05 15:44:23 +0000316 ((uint64_t) (data)[(offset) + 7] << 56) \
317 | ((uint64_t) (data)[(offset) + 6] << 48) \
318 | ((uint64_t) (data)[(offset) + 5] << 40) \
319 | ((uint64_t) (data)[(offset) + 4] << 32) \
320 | ((uint64_t) (data)[(offset) + 3] << 24) \
321 | ((uint64_t) (data)[(offset) + 2] << 16) \
322 | ((uint64_t) (data)[(offset) + 1] << 8) \
323 | ((uint64_t) (data)[(offset)]) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100324 )
325#endif
326
327/**
328 * Put in memory a 64 bits unsigned integer in little-endian order.
329 *
330 * \param n 64 bits unsigned integer to put in memory.
331 * \param data Base address of the memory where to put the 64
332 * bits unsigned integer in.
333 * \param offset Offset from \p base where to put the least significant
334 * byte of the 64 bits unsigned integer \p n.
335 */
336#ifndef MBEDTLS_PUT_UINT64_LE
David Horstmannceeaeb92023-01-05 15:44:23 +0000337#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
338 { \
339 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
340 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
341 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
342 (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \
343 (data)[(offset) + 4] = MBEDTLS_BYTE_4(n); \
344 (data)[(offset) + 5] = MBEDTLS_BYTE_5(n); \
345 (data)[(offset) + 6] = MBEDTLS_BYTE_6(n); \
346 (data)[(offset) + 7] = MBEDTLS_BYTE_7(n); \
347 }
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100348#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100349
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200350#endif /* MBEDTLS_LIBRARY_COMMON_H */