blob: 25d5294e1ad96f31f899ec3375ddebf042f7f0aa [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
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020027
Gilles Peskine42649d92022-11-23 14:15:57 +010028#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010029#include <stdint.h>
30
Gilles Peskinec4672fd2019-09-11 13:39:11 +020031/** Helper to define a function as static except when building invasive tests.
32 *
33 * If a function is only used inside its own source file and should be
34 * declared `static` to allow the compiler to optimize for code size,
35 * but that function has unit tests, define it with
36 * ```
37 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
38 * ```
39 * and declare it in a header in the `library/` directory with
40 * ```
41 * #if defined(MBEDTLS_TEST_HOOKS)
42 * int mbedtls_foo(...);
43 * #endif
44 * ```
45 */
46#if defined(MBEDTLS_TEST_HOOKS)
47#define MBEDTLS_STATIC_TESTABLE
48#else
49#define MBEDTLS_STATIC_TESTABLE static
50#endif
51
TRodziewicz7871c2e2021-07-07 17:29:43 +020052#if defined(MBEDTLS_TEST_HOOKS)
53extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
54#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
55 do { \
56 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
57 { \
58 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
59 } \
60 } while( 0 )
61#else
62#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
63#endif /* defined(MBEDTLS_TEST_HOOKS) */
64
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020065/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020066 *
67 * Although structs defined in header files are publicly available,
68 * their members are private and should not be accessed by the user.
69 */
70#define MBEDTLS_ALLOW_PRIVATE_ACCESS
71
Gilles Peskine42649d92022-11-23 14:15:57 +010072/** Return an offset into a buffer.
73 *
74 * This is just the addition of an offset to a pointer, except that this
75 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +010076 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
77 * A null pointer is a valid buffer pointer when the size is 0, for example
78 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +010079 *
80 * \param p Pointer to a buffer of at least n bytes.
81 * This may be \p NULL if \p n is zero.
82 * \param n An offset in bytes.
83 * \return Pointer to offset \p n in the buffer \p p.
84 * Note that this is only a valid pointer if the size of the
85 * buffer is at least \p n + 1.
86 */
87static inline unsigned char *mbedtls_buffer_offset(
88 unsigned char *p, size_t n )
89{
90 return( p == NULL ? NULL : p + n );
91}
92
93/** Return an offset into a read-only buffer.
94 *
Gilles Peskine7d237782022-11-25 13:34:59 +010095 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +010096 *
97 * \param p Pointer to a buffer of at least n bytes.
98 * This may be \p NULL if \p n is zero.
99 * \param n An offset in bytes.
100 * \return Pointer to offset \p n in the buffer \p p.
101 * Note that this is only a valid pointer if the size of the
102 * buffer is at least \p n + 1.
103 */
104static inline const unsigned char *mbedtls_buffer_offset_const(
105 const unsigned char *p, size_t n )
106{
107 return( p == NULL ? NULL : p + n );
108}
109
Joe Subbiani50dde562021-06-22 15:51:53 +0100110/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100111 *
Joe Subbiani9ab18662021-07-21 16:35:48 +0100112 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +0100113 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +0100114 */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100115#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +0100116#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
117#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
118#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +0100119#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
120#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
121#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
122#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +0100123
Joe Subbiani394bdd62021-07-07 15:16:56 +0100124/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100125 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100126 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100127 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100128 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800129 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100130 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100131 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100132 */
133#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100134#define MBEDTLS_GET_UINT32_BE( data , offset ) \
135 ( \
136 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
137 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
138 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
139 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100140 )
141#endif
142
143/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100144 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100145 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100146 * \param n 32 bits unsigned integer to put in memory.
147 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100148 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800149 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100150 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100151 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100152#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100153#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
154{ \
155 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
156 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
157 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
158 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
159}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100160#endif
161
Joe Subbiani394bdd62021-07-07 15:16:56 +0100162/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100163 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100164 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100165 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100166 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800167 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100168 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100169 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100170 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100171#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100172#define MBEDTLS_GET_UINT32_LE( data, offset ) \
173 ( \
174 ( (uint32_t) ( data )[( offset ) ] ) \
175 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
176 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
177 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100178 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100179#endif
180
Joe Subbiani6a506312021-07-07 16:56:29 +0100181/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100182 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100183 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100184 * \param n 32 bits unsigned integer to put in memory.
185 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100186 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800187 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100188 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100189 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100190#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100191#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
192{ \
193 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
194 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
195 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
196 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
197}
Joe Subbiani54c61342021-06-23 12:16:47 +0100198#endif
199
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100200/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100201 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100202 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100203 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100204 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800205 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100206 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100207 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100208 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100209#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100210#define MBEDTLS_GET_UINT16_LE( data, offset ) \
211 ( \
212 ( (uint16_t) ( data )[( offset ) ] ) \
213 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100214 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100215#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100216
Joe Subbiani394bdd62021-07-07 15:16:56 +0100217/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100218 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100219 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100220 * \param n 16 bits unsigned integer to put in memory.
221 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100222 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800223 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100224 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100225 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100226#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100227#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
228{ \
229 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
230 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100231}
232#endif
233
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100234/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100235 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100236 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100237 *
238 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800239 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100240 * byte of the two bytes to build the 16 bits unsigned
241 * integer from.
242 */
243#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100244#define MBEDTLS_GET_UINT16_BE( data, offset ) \
245 ( \
246 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
247 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100248 )
249#endif
250
251/**
252 * Put in memory a 16 bits unsigned integer in big-endian order.
253 *
254 * \param n 16 bits unsigned integer to put in memory.
255 * \param data Base address of the memory where to put the 16
256 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800257 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100258 * byte of the 16 bits unsigned integer \p n.
259 */
260#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100261#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
262{ \
263 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
264 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100265}
266#endif
267
268/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800269 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800270 * big-endian order (MSB first).
271 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800272 * \param data Base address of the memory to get the three bytes from.
273 * \param offset Offset from \p data of the first and most significant
274 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800275 * integer from.
276 */
277#ifndef MBEDTLS_GET_UINT24_BE
278#define MBEDTLS_GET_UINT24_BE( data , offset ) \
279 ( \
280 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
281 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
282 | ( (uint32_t) ( data )[( offset ) + 2] ) \
283 )
284#endif
285
286/**
287 * Put in memory a 24 bits unsigned integer in big-endian order.
288 *
289 * \param n 24 bits unsigned integer to put in memory.
290 * \param data Base address of the memory where to put the 24
291 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800292 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800293 * byte of the 24 bits unsigned integer \p n.
294 */
295#ifndef MBEDTLS_PUT_UINT24_BE
296#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
297{ \
298 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
299 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
300 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
301}
302#endif
303
304/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800305 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800306 * little-endian order (LSB first).
307 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800308 * \param data Base address of the memory to get the three bytes from.
309 * \param offset Offset from \p data of the first and least significant
310 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800311 * integer from.
312 */
313#ifndef MBEDTLS_GET_UINT24_LE
314#define MBEDTLS_GET_UINT24_LE( data, offset ) \
315 ( \
316 ( (uint32_t) ( data )[( offset ) ] ) \
317 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
318 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
319 )
320#endif
321
322/**
323 * Put in memory a 24 bits unsigned integer in little-endian order.
324 *
325 * \param n 24 bits unsigned integer to put in memory.
326 * \param data Base address of the memory where to put the 24
327 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800328 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800329 * byte of the 24 bits unsigned integer \p n.
330 */
331#ifndef MBEDTLS_PUT_UINT24_LE
332#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
333{ \
334 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
335 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
336 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
337}
338#endif
339
340/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100341 * Get the unsigned 64 bits integer corresponding to eight bytes in
342 * big-endian order (MSB first).
343 *
344 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800345 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100346 * byte of the eight bytes to build the 64 bits unsigned
347 * integer from.
348 */
349#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100350#define MBEDTLS_GET_UINT64_BE( data, offset ) \
351 ( \
352 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
353 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
354 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
355 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
356 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
357 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
358 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
359 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100360 )
361#endif
362
363/**
364 * Put in memory a 64 bits unsigned integer in big-endian order.
365 *
366 * \param n 64 bits unsigned integer to put in memory.
367 * \param data Base address of the memory where to put the 64
368 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800369 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100370 * byte of the 64 bits unsigned integer \p n.
371 */
372#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100373#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
374{ \
375 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
376 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
377 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
378 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
379 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
380 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
381 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
382 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100383}
384#endif
385
386/**
387 * Get the unsigned 64 bits integer corresponding to eight bytes in
388 * little-endian order (LSB first).
389 *
390 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800391 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100392 * byte of the eight bytes to build the 64 bits unsigned
393 * integer from.
394 */
395#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100396#define MBEDTLS_GET_UINT64_LE( data, offset ) \
397 ( \
398 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
399 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
400 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
401 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
402 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
403 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
404 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
405 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100406 )
407#endif
408
409/**
410 * Put in memory a 64 bits unsigned integer in little-endian order.
411 *
412 * \param n 64 bits unsigned integer to put in memory.
413 * \param data Base address of the memory where to put the 64
414 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800415 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100416 * byte of the 64 bits unsigned integer \p n.
417 */
418#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100419#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
420{ \
421 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
422 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
423 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
424 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
425 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
426 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
427 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
428 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100429}
430#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100431
Jerry Yu6c983522021-09-24 12:45:36 +0800432/* Fix MSVC C99 compatible issue
433 * MSVC support __func__ from visual studio 2015( 1900 )
434 * Use MSVC predefine macro to avoid name check fail.
435 */
436#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800437#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800438#endif
439
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200440#endif /* MBEDTLS_LIBRARY_COMMON_H */