blob: 11bb9912e9d02eb7dd24b2f63935f773a01832e0 [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 Subbiani9231d5f2021-07-07 16:56:29 +010081 * 32-bit integer manipulation GET macros (big endian)
Joe Subbiani266476d2021-07-07 15:16:56 +010082 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +010083 * \brief Use this to assign an unsigned 32 bit integer
84 * by taking data stored adjacent in memory that
85 * can be accessed via on offset
86 * Big Endian is used when wanting to
87 * transmit the most signifcant bits first
Joe Subbiani266476d2021-07-07 15:16:56 +010088 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +010089 * \param data The data used to translate to a 32 bit
90 * integer
91 * \param offset the shift in bytes to access the next byte
92 * of data
93 */
94#ifndef MBEDTLS_GET_UINT32_BE
95#define MBEDTLS_GET_UINT32_BE( data , offset ) \
96 ( \
97 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
98 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
99 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
100 | ( (uint32_t) ( data )[( offset ) + 3] ) \
101 )
102#endif
103
104/**
105 * 32-bit integer manipulation PUT macros (big endian)
106 *
107 * \brief Read from a 32 bit integer and store each byte
108 * in memory, offset by a specified amount, resulting
109 * in each byte being adjacent in memory.
110 * Big Endian is used when wanting to
111 * transmit the most signifcant bits first
112 *
113 * \param n 32 bit integer where data is accessed
Joe Subbiani266476d2021-07-07 15:16:56 +0100114 * \param b const unsigned char array of data to be
115 * manipulated
116 * \param i offset in bytes, In the case of UINT32, i
117 * would increment by 4 every use assuming
118 * the data is being stored in the same location
119 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100120#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani4530b272021-07-05 15:37:39 +0100121#define MBEDTLS_PUT_UINT32_BE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100122 do { \
123 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
124 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
125 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
126 (b)[(i) + 3] = (unsigned char) ( (n) ); \
127 } while( 0 )
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100128#endif
129
Joe Subbiani266476d2021-07-07 15:16:56 +0100130/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100131 * 32-bit integer manipulation GET macros (little endian)
132 *
133 * \brief Use this to assign an unsigned 32 bit integer
134 * by taking data stored adjacent in memory that
135 * can be accessed via on offset
136 * Little Endian is used when wanting to
137 * transmit the least signifcant bits first
138 *
139 * \param data The data used to translate to a 32 bit
140 * integer
141 * \param offset the shift in bytes to access the next byte
142 * of data
Joe Subbiani4fb75552021-06-23 12:16:47 +0100143 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100144#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100145#define MBEDTLS_GET_UINT32_LE( data, offset ) \
146 ( \
147 ( (uint32_t) ( data )[( offset ) ] ) \
148 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
149 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
150 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
151 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100152#endif
153
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100154/**
155 * 32-bit integer manipulation PUT macros (little endian)
156 *
157 * \brief Read from a 32 bit integer and store each byte
158 * in memory, offset by a specified amount, resulting
159 * in each byte being adjacent in memory.
160 * Little Endian is used when wanting to
161 * transmit the least signifcant bits first
162 *
163 * \param n 32 bit integer where data is accessed
164 * \param b const unsigned char array of data to be
165 * manipulated
166 * \param i offset in bytes, In the case of UINT32, i
167 * would increment by 4 every use assuming
168 * the data is being stored in the same location
169 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100170#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani4530b272021-07-05 15:37:39 +0100171#define MBEDTLS_PUT_UINT32_LE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100172 do { \
173 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
174 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
175 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
176 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
177 } while( 0 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100178#endif
179
Joe Subbiani61f7d732021-06-24 09:06:23 +0100180/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100181 * 16-bit integer manipulation GET macros (little endian)
182 *
183 * \brief Use this to assign an unsigned 16 bit integer
184 * by taking data stored adjacent in memory that
185 * can be accessed via on offset
186 * Little Endian is used when wanting to
187 * transmit the least signifcant bits first
188 *
189 * \param data The data used to translate to a 16 bit
190 * integer
191 * \param offset the shit in bytes to access the next byte
192 * of data
Joe Subbiani927488e2021-06-23 11:23:44 +0100193 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100194#ifndef MBEDTLS_GET_UINT16_LE
195#define MBEDTLS_GET_UINT16_LE( data, offset ) \
196 ( \
197 ( (uint16_t) ( data )[( offset ) ] ) \
198 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100199 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100200#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100201
Joe Subbiani266476d2021-07-07 15:16:56 +0100202/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100203 * 16-bit integer manipulation PUT macros (little endian)
Joe Subbiani266476d2021-07-07 15:16:56 +0100204 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100205 * \brief Read from a 16 bit integer and store each byte
206 * in memory, offset by a specified amount, resulting
207 * in each byte being adjacent in memory.
208 * Little Endian is used when wanting to
209 * transmit the least signifcant bits first
Joe Subbiani266476d2021-07-07 15:16:56 +0100210 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100211 * \param n 16 bit integer where data is accessed
Joe Subbiani266476d2021-07-07 15:16:56 +0100212 * \param b const unsigned char array of data to be
213 * manipulated
214 * \param i offset in bytes, In the case of UINT16, i
215 * would increment by 2 every use assuming
216 * the data is being stored in the same location
217 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100218#ifndef MBEDTLS_PUT_UINT16_LE
219#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \
220{ \
221 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
222 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
223}
224#endif
225
226
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200227#endif /* MBEDTLS_LIBRARY_COMMON_H */