blob: 53598228b4d581f954fcf239ac83b2010241583e [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.
76 *
77 * \param p Pointer to a buffer of at least n bytes.
78 * This may be \p NULL if \p n is zero.
79 * \param n An offset in bytes.
80 * \return Pointer to offset \p n in the buffer \p p.
81 * Note that this is only a valid pointer if the size of the
82 * buffer is at least \p n + 1.
83 */
84static inline unsigned char *mbedtls_buffer_offset(
85 unsigned char *p, size_t n )
86{
87 return( p == NULL ? NULL : p + n );
88}
89
90/** Return an offset into a read-only buffer.
91 *
92 * This is just the addition of an offset to a pointer, except that this
93 * function also accepts an offset of 0 into a buffer whose pointer is null.
94 *
95 * \param p Pointer to a buffer of at least n bytes.
96 * This may be \p NULL if \p n is zero.
97 * \param n An offset in bytes.
98 * \return Pointer to offset \p n in the buffer \p p.
99 * Note that this is only a valid pointer if the size of the
100 * buffer is at least \p n + 1.
101 */
102static inline const unsigned char *mbedtls_buffer_offset_const(
103 const unsigned char *p, size_t n )
104{
105 return( p == NULL ? NULL : p + n );
106}
107
Joe Subbiani50dde562021-06-22 15:51:53 +0100108/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100109 *
Joe Subbiani9ab18662021-07-21 16:35:48 +0100110 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +0100111 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +0100112 */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100113#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +0100114#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
115#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
116#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +0100117#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
118#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
119#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
120#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +0100121
Joe Subbiani394bdd62021-07-07 15:16:56 +0100122/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100123 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100124 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100125 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100126 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800127 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100128 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100129 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100130 */
131#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100132#define MBEDTLS_GET_UINT32_BE( data , offset ) \
133 ( \
134 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
135 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
136 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
137 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100138 )
139#endif
140
141/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100142 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100143 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100144 * \param n 32 bits unsigned integer to put in memory.
145 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100146 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800147 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100148 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100149 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100150#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100151#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
152{ \
153 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
154 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
155 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
156 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
157}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100158#endif
159
Joe Subbiani394bdd62021-07-07 15:16:56 +0100160/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100161 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100162 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100163 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100164 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800165 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100166 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100167 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100168 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100169#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100170#define MBEDTLS_GET_UINT32_LE( data, offset ) \
171 ( \
172 ( (uint32_t) ( data )[( offset ) ] ) \
173 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
174 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
175 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100176 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100177#endif
178
Joe Subbiani6a506312021-07-07 16:56:29 +0100179/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100180 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100181 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100182 * \param n 32 bits unsigned integer to put in memory.
183 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100184 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800185 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100186 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100187 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100188#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100189#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
190{ \
191 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
192 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
193 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
194 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
195}
Joe Subbiani54c61342021-06-23 12:16:47 +0100196#endif
197
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100198/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100199 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100200 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100201 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100202 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800203 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100204 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100205 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100206 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100207#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100208#define MBEDTLS_GET_UINT16_LE( data, offset ) \
209 ( \
210 ( (uint16_t) ( data )[( offset ) ] ) \
211 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100212 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100213#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100214
Joe Subbiani394bdd62021-07-07 15:16:56 +0100215/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100216 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100217 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100218 * \param n 16 bits unsigned integer to put in memory.
219 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100220 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800221 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100222 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100223 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100224#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100225#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
226{ \
227 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
228 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100229}
230#endif
231
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100232/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100233 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100234 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100235 *
236 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800237 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100238 * byte of the two bytes to build the 16 bits unsigned
239 * integer from.
240 */
241#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100242#define MBEDTLS_GET_UINT16_BE( data, offset ) \
243 ( \
244 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
245 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100246 )
247#endif
248
249/**
250 * Put in memory a 16 bits unsigned integer in big-endian order.
251 *
252 * \param n 16 bits unsigned integer to put in memory.
253 * \param data Base address of the memory where to put the 16
254 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800255 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100256 * byte of the 16 bits unsigned integer \p n.
257 */
258#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100259#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
260{ \
261 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
262 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100263}
264#endif
265
266/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800267 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800268 * big-endian order (MSB first).
269 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800270 * \param data Base address of the memory to get the three bytes from.
271 * \param offset Offset from \p data of the first and most significant
272 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800273 * integer from.
274 */
275#ifndef MBEDTLS_GET_UINT24_BE
276#define MBEDTLS_GET_UINT24_BE( data , offset ) \
277 ( \
278 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
279 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
280 | ( (uint32_t) ( data )[( offset ) + 2] ) \
281 )
282#endif
283
284/**
285 * Put in memory a 24 bits unsigned integer in big-endian order.
286 *
287 * \param n 24 bits unsigned integer to put in memory.
288 * \param data Base address of the memory where to put the 24
289 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800290 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800291 * byte of the 24 bits unsigned integer \p n.
292 */
293#ifndef MBEDTLS_PUT_UINT24_BE
294#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
295{ \
296 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
297 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
298 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
299}
300#endif
301
302/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800303 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800304 * little-endian order (LSB first).
305 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800306 * \param data Base address of the memory to get the three bytes from.
307 * \param offset Offset from \p data of the first and least significant
308 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800309 * integer from.
310 */
311#ifndef MBEDTLS_GET_UINT24_LE
312#define MBEDTLS_GET_UINT24_LE( data, offset ) \
313 ( \
314 ( (uint32_t) ( data )[( offset ) ] ) \
315 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
316 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
317 )
318#endif
319
320/**
321 * Put in memory a 24 bits unsigned integer in little-endian order.
322 *
323 * \param n 24 bits unsigned integer to put in memory.
324 * \param data Base address of the memory where to put the 24
325 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800326 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800327 * byte of the 24 bits unsigned integer \p n.
328 */
329#ifndef MBEDTLS_PUT_UINT24_LE
330#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
331{ \
332 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
333 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
334 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
335}
336#endif
337
338/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100339 * Get the unsigned 64 bits integer corresponding to eight bytes in
340 * big-endian order (MSB first).
341 *
342 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800343 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100344 * byte of the eight bytes to build the 64 bits unsigned
345 * integer from.
346 */
347#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100348#define MBEDTLS_GET_UINT64_BE( data, offset ) \
349 ( \
350 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
351 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
352 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
353 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
354 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
355 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
356 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
357 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100358 )
359#endif
360
361/**
362 * Put in memory a 64 bits unsigned integer in big-endian order.
363 *
364 * \param n 64 bits unsigned integer to put in memory.
365 * \param data Base address of the memory where to put the 64
366 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800367 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100368 * byte of the 64 bits unsigned integer \p n.
369 */
370#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100371#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
372{ \
373 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
374 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
375 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
376 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
377 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
378 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
379 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
380 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100381}
382#endif
383
384/**
385 * Get the unsigned 64 bits integer corresponding to eight bytes in
386 * little-endian order (LSB first).
387 *
388 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800389 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100390 * byte of the eight bytes to build the 64 bits unsigned
391 * integer from.
392 */
393#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100394#define MBEDTLS_GET_UINT64_LE( data, offset ) \
395 ( \
396 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
397 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
398 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
399 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
400 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
401 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
402 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
403 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100404 )
405#endif
406
407/**
408 * Put in memory a 64 bits unsigned integer in little-endian order.
409 *
410 * \param n 64 bits unsigned integer to put in memory.
411 * \param data Base address of the memory where to put the 64
412 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800413 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100414 * byte of the 64 bits unsigned integer \p n.
415 */
416#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100417#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
418{ \
419 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
420 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
421 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
422 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
423 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
424 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
425 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
426 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100427}
428#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100429
Jerry Yu6c983522021-09-24 12:45:36 +0800430/* Fix MSVC C99 compatible issue
431 * MSVC support __func__ from visual studio 2015( 1900 )
432 * Use MSVC predefine macro to avoid name check fail.
433 */
434#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800435#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800436#endif
437
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200438#endif /* MBEDTLS_LIBRARY_COMMON_H */