blob: 95a30c91d1180bf860b85b765187e6ec768c9638 [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
32/** Helper to define a function as static except when building invasive tests.
33 *
34 * If a function is only used inside its own source file and should be
35 * declared `static` to allow the compiler to optimize for code size,
36 * but that function has unit tests, define it with
37 * ```
38 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
39 * ```
40 * and declare it in a header in the `library/` directory with
41 * ```
42 * #if defined(MBEDTLS_TEST_HOOKS)
43 * int mbedtls_foo(...);
44 * #endif
45 * ```
46 */
47#if defined(MBEDTLS_TEST_HOOKS)
48#define MBEDTLS_STATIC_TESTABLE
49#else
50#define MBEDTLS_STATIC_TESTABLE static
51#endif
52
Joe Subbianiba486b02021-06-22 15:51:53 +010053/** Allow library to access its structs' private members.
54 *
55 * Although structs defined in header files are publicly available,
56 * their members are private and should not be accessed by the user.
57 */
58#define MBEDTLS_ALLOW_PRIVATE_ACCESS
59
60/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010061 *
Joe Subbiani266476d2021-07-07 15:16:56 +010062 * Obtain the most significant byte of x using 0xff
63 * Using MBEDTLS_BYTE_a will shift a*8 bits
64 * to retrieve the next byte of information
Joe Subbianiba486b02021-06-22 15:51:53 +010065 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +010066#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
67#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
68#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
69#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianiba486b02021-06-22 15:51:53 +010070
Joe Subbiani6b897c92021-07-08 14:59:52 +010071#define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) )
72#define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) )
73#define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) )
74#define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) )
75#define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) )
76#define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) )
77#define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) )
78#define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) )
79
Joe Subbiani266476d2021-07-07 15:16:56 +010080/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +010081 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +010082 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +010083 *
Joe Subbiani0a65d532021-07-14 11:53:07 +010084 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +010085 * \param offset Offset from \p base of the first and most significant
86 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +010087 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010088 */
89#ifndef MBEDTLS_GET_UINT32_BE
90#define MBEDTLS_GET_UINT32_BE( data , offset ) \
91 ( \
92 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
93 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
94 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
95 | ( (uint32_t) ( data )[( offset ) + 3] ) \
96 )
97#endif
98
99/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100100 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100101 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100102 * \param n 32 bits unsigned integer to put in memory.
103 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100104 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100105 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100106 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100107 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100108#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani0a65d532021-07-14 11:53:07 +0100109#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
110 do { \
111 ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \
112 ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \
113 ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \
114 ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100115 } while( 0 )
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100116#endif
117
Joe Subbiani266476d2021-07-07 15:16:56 +0100118/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100119 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100120 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100121 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100122 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100123 * \param offset Offset from \p base of the first and least significant
124 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100125 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100126 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100127#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100128#define MBEDTLS_GET_UINT32_LE( data, offset ) \
129 ( \
130 ( (uint32_t) ( data )[( offset ) ] ) \
131 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
132 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
133 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
134 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100135#endif
136
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100137/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100138 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100139 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100140 * \param n 32 bits unsigned integer to put in memory.
141 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100142 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100143 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100144 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100145 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100146#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani0a65d532021-07-14 11:53:07 +0100147#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
148 do { \
149 ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
150 ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
151 ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
152 ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100153 } while( 0 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100154#endif
155
Joe Subbiani61f7d732021-06-24 09:06:23 +0100156/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100157 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100158 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100159 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100160 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100161 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100162 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100163 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100164 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100165#ifndef MBEDTLS_GET_UINT16_LE
166#define MBEDTLS_GET_UINT16_LE( data, offset ) \
167 ( \
168 ( (uint16_t) ( data )[( offset ) ] ) \
169 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100170 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100171#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100172
Joe Subbiani266476d2021-07-07 15:16:56 +0100173/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100174 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100175 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100176 * \param n 16 bits unsigned integer to put in memory.
177 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100178 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100179 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100180 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100181 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100182#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani0a65d532021-07-14 11:53:07 +0100183#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
184{ \
185 ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
186 ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100187}
188#endif
189
190
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200191#endif /* MBEDTLS_LIBRARY_COMMON_H */