blob: 6bb1f2c44eead86b93d53eac011babc1b7daf409 [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/** Allow library to access its structs' private members.
56 *
57 * Although structs defined in header files are publicly available,
58 * their members are private and should not be accessed by the user.
59 */
60#define MBEDTLS_ALLOW_PRIVATE_ACCESS
61
62/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010063 *
Joe Subbiani266476d2021-07-07 15:16:56 +010064 * Obtain the most significant byte of x using 0xff
65 * Using MBEDTLS_BYTE_a will shift a*8 bits
66 * to retrieve the next byte of information
Joe Subbianiba486b02021-06-22 15:51:53 +010067 */
Joe Subbianic045dc12021-07-14 12:31:31 +010068#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani2bbafda2021-06-24 13:00:03 +010069#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
70#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
71#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianic045dc12021-07-14 12:31:31 +010072#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
73#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
74#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
75#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbiani6b897c92021-07-08 14:59:52 +010076
Joe Subbiani266476d2021-07-07 15:16:56 +010077/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +010078 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +010079 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +010080 *
Joe Subbiani0a65d532021-07-14 11:53:07 +010081 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +010082 * \param offset Offset from \p base of the first and most significant
83 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +010084 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010085 */
86#ifndef MBEDTLS_GET_UINT32_BE
87#define MBEDTLS_GET_UINT32_BE( data , offset ) \
88 ( \
89 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
90 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
91 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
92 | ( (uint32_t) ( data )[( offset ) + 3] ) \
93 )
94#endif
95
96/**
Joe Subbiani0a65d532021-07-14 11:53:07 +010097 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +010098 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +010099 * \param n 32 bits unsigned integer to put in memory.
100 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100101 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100102 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100103 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100104 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100105#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani0a65d532021-07-14 11:53:07 +0100106#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
107 do { \
108 ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \
109 ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \
110 ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \
111 ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100112 } while( 0 )
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100113#endif
114
Joe Subbiani266476d2021-07-07 15:16:56 +0100115/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100116 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100117 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100118 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100119 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100120 * \param offset Offset from \p base of the first and least significant
121 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100122 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100123 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100124#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100125#define MBEDTLS_GET_UINT32_LE( data, offset ) \
126 ( \
127 ( (uint32_t) ( data )[( offset ) ] ) \
128 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
129 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
130 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
131 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100132#endif
133
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100134/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100135 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100136 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100137 * \param n 32 bits unsigned integer to put in memory.
138 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100139 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100140 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100141 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100142 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100143#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani0a65d532021-07-14 11:53:07 +0100144#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
145 do { \
146 ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
147 ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
148 ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
149 ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100150 } while( 0 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100151#endif
152
Joe Subbiani61f7d732021-06-24 09:06:23 +0100153/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100154 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100155 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100156 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100157 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100158 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100159 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100160 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100161 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100162#ifndef MBEDTLS_GET_UINT16_LE
163#define MBEDTLS_GET_UINT16_LE( data, offset ) \
164 ( \
165 ( (uint16_t) ( data )[( offset ) ] ) \
166 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100167 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100168#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100169
Joe Subbiani266476d2021-07-07 15:16:56 +0100170/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100171 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100172 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100173 * \param n 16 bits unsigned integer to put in memory.
174 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100175 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100176 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100177 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100178 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100179#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani0a65d532021-07-14 11:53:07 +0100180#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
181{ \
182 ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
183 ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
Joe Subbiani4530b272021-07-05 15:37:39 +0100184}
185#endif
186
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100187/**
188 * Get the unsigned 64 bits integer corresponding to eight bytes in
189 * big-endian order (MSB first).
190 *
191 * \param data Base address of the memory to get the eight bytes from.
192 * \param offset Offset from \p base of the first and most significant
193 * byte of the eight bytes to build the 64 bits unsigned
194 * integer from.
195 */
196#ifndef MBEDTLS_GET_UINT64_BE
197#define MBEDTLS_GET_UINT64_BE( data, offset ) \
198 ( \
199 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
200 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
201 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
202 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
203 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
204 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
205 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
206 | ( (uint64_t) ( data )[( offset ) + 7] ) \
207 )
208#endif
209
210/**
211 * Put in memory a 64 bits unsigned integer in big-endian order.
212 *
213 * \param n 64 bits unsigned integer to put in memory.
214 * \param data Base address of the memory where to put the 64
215 * bits unsigned integer in.
216 * \param offset Offset from \p base where to put the most significant
217 * byte of the 64 bits unsigned integer \p n.
218 */
219#ifndef MBEDTLS_PUT_UINT64_BE
220#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
221{ \
222 ( data )[( offset ) ] = (unsigned char) ( (n) >> 56 ); \
223 ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 48 ); \
224 ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 40 ); \
225 ( data )[( offset ) + 3] = (unsigned char) ( (n) >> 32 ); \
226 ( data )[( offset ) + 4] = (unsigned char) ( (n) >> 24 ); \
227 ( data )[( offset ) + 5] = (unsigned char) ( (n) >> 16 ); \
228 ( data )[( offset ) + 6] = (unsigned char) ( (n) >> 8 ); \
229 ( data )[( offset ) + 7] = (unsigned char) ( (n) ); \
230}
231#endif
232
233/**
234 * Get the unsigned 64 bits integer corresponding to eight bytes in
235 * little-endian order (LSB first).
236 *
237 * \param data Base address of the memory to get the eight bytes from.
238 * \param offset Offset from \p base of the first and least significant
239 * byte of the eight bytes to build the 64 bits unsigned
240 * integer from.
241 */
242#ifndef MBEDTLS_GET_UINT64_LE
243#define MBEDTLS_GET_UINT64_LE( data, offset ) \
244 ( \
245 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
246 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
247 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
248 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
249 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
250 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
251 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
252 | ( (uint64_t) ( data )[( offset ) ] ) \
253 )
254#endif
255
256/**
257 * Put in memory a 64 bits unsigned integer in little-endian order.
258 *
259 * \param n 64 bits unsigned integer to put in memory.
260 * \param data Base address of the memory where to put the 64
261 * bits unsigned integer in.
262 * \param offset Offset from \p base where to put the least significant
263 * byte of the 64 bits unsigned integer \p n.
264 */
265#ifndef MBEDTLS_PUT_UINT64_LE
266#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
267{ \
268 ( data )[( offset ) + 7] = (unsigned char) ( (n) >> 56 ); \
269 ( data )[( offset ) + 6] = (unsigned char) ( (n) >> 48 ); \
270 ( data )[( offset ) + 5] = (unsigned char) ( (n) >> 40 ); \
271 ( data )[( offset ) + 4] = (unsigned char) ( (n) >> 32 ); \
272 ( data )[( offset ) + 3] = (unsigned char) ( (n) >> 24 ); \
273 ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 16 ); \
274 ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 8 ); \
275 ( data )[( offset ) ] = (unsigned char) ( (n) ); \
276}
277#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100278
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200279#endif /* MBEDTLS_LIBRARY_COMMON_H */