blob: a3ce1d859266f751054a3a0b7d50f887294a26da [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 *
62 * To tidy up code and save horizontal and vertical space, use byte
Joe Subbianiba486b02021-06-22 15:51:53 +010063 * reading macros to cast
64 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +010065#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
66#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
67#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
68#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbianiba486b02021-06-22 15:51:53 +010069
Joe Subbianiaa5f6a62021-06-23 11:49:03 +010070/*
71 * 32-bit integer manipulation macros (big endian)
72 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +010073#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani4530b272021-07-05 15:37:39 +010074#define MBEDTLS_GET_UINT32_BE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +010075 do { \
76 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
77 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
78 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
79 | ( (uint32_t) (b)[(i) + 3] ); \
80 } while( 0 )
Joe Subbianiaa5f6a62021-06-23 11:49:03 +010081#endif
82
Joe Subbiani2bbafda2021-06-24 13:00:03 +010083#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani4530b272021-07-05 15:37:39 +010084#define MBEDTLS_PUT_UINT32_BE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +010085 do { \
86 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
87 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
88 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
89 (b)[(i) + 3] = (unsigned char) ( (n) ); \
90 } while( 0 )
Joe Subbianiaa5f6a62021-06-23 11:49:03 +010091#endif
92
Joe Subbiani4fb75552021-06-23 12:16:47 +010093/*
94 * 32-bit integer manipulation macros (little endian)
95 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +010096#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani4530b272021-07-05 15:37:39 +010097#define MBEDTLS_GET_UINT32_LE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +010098 do { \
99 (n) = ( (uint32_t) (b)[(i) ] ) \
100 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
101 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
102 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
103 } while( 0 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100104#endif
105
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100106#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani4530b272021-07-05 15:37:39 +0100107#define MBEDTLS_PUT_UINT32_LE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100108 do { \
109 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
110 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
111 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
112 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
113 } while( 0 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100114#endif
115
Joe Subbiani61f7d732021-06-24 09:06:23 +0100116/**
Joe Subbiani4fb75552021-06-23 12:16:47 +0100117 * 32-bit integer conversion from bytes (little endian)
Joe Subbiani927488e2021-06-23 11:23:44 +0100118 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100119#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100120 ( (uint32_t) (data)[offset] \
121 | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \
122 | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \
123 | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \
124 )
125
Joe Subbiani4530b272021-07-05 15:37:39 +0100126
127/*
128 * 16-bit integer manipulation macros (little endian)
129 */
130#ifndef MBEDTLS_GET_UINT16_LE
131#define MBEDTLS_GET_UINT16_LE( n, b, i ) \
132{ \
133 (n) = ( (uint16_t) (b)[(i) ] ) \
134 | ( (uint16_t) (b)[(i) + 1] << 8 ); \
135}
136#endif
137
138#ifndef MBEDTLS_PUT_UINT16_LE
139#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \
140{ \
141 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
142 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
143}
144#endif
145
146
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200147#endif /* MBEDTLS_LIBRARY_COMMON_H */