blob: fdc68db4e81414ab0755c3c7d8396ac2038745f5 [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 Subbiani266476d2021-07-07 15:16:56 +010071/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +010072 * 32-bit integer manipulation GET macros (big endian)
Joe Subbiani266476d2021-07-07 15:16:56 +010073 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +010074 * \brief Use this to assign an unsigned 32 bit integer
75 * by taking data stored adjacent in memory that
76 * can be accessed via on offset
77 * Big Endian is used when wanting to
78 * transmit the most signifcant bits first
Joe Subbiani266476d2021-07-07 15:16:56 +010079 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +010080 * \param data The data used to translate to a 32 bit
81 * integer
82 * \param offset the shift in bytes to access the next byte
83 * of data
84 */
85#ifndef MBEDTLS_GET_UINT32_BE
86#define MBEDTLS_GET_UINT32_BE( data , offset ) \
87 ( \
88 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
89 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
90 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
91 | ( (uint32_t) ( data )[( offset ) + 3] ) \
92 )
93#endif
94
95/**
96 * 32-bit integer manipulation PUT macros (big endian)
97 *
98 * \brief Read from a 32 bit integer and store each byte
99 * in memory, offset by a specified amount, resulting
100 * in each byte being adjacent in memory.
101 * Big Endian is used when wanting to
102 * transmit the most signifcant bits first
103 *
104 * \param n 32 bit integer where data is accessed
Joe Subbiani266476d2021-07-07 15:16:56 +0100105 * \param b const unsigned char array of data to be
106 * manipulated
107 * \param i offset in bytes, In the case of UINT32, i
108 * would increment by 4 every use assuming
109 * the data is being stored in the same location
110 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100111#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani4530b272021-07-05 15:37:39 +0100112#define MBEDTLS_PUT_UINT32_BE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100113 do { \
114 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
115 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
116 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
117 (b)[(i) + 3] = (unsigned char) ( (n) ); \
118 } while( 0 )
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100119#endif
120
Joe Subbiani266476d2021-07-07 15:16:56 +0100121/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100122 * 32-bit integer manipulation GET macros (little endian)
123 *
124 * \brief Use this to assign an unsigned 32 bit integer
125 * by taking data stored adjacent in memory that
126 * can be accessed via on offset
127 * Little Endian is used when wanting to
128 * transmit the least signifcant bits first
129 *
130 * \param data The data used to translate to a 32 bit
131 * integer
132 * \param offset the shift in bytes to access the next byte
133 * of data
Joe Subbiani4fb75552021-06-23 12:16:47 +0100134 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100135#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100136#define MBEDTLS_GET_UINT32_LE( data, offset ) \
137 ( \
138 ( (uint32_t) ( data )[( offset ) ] ) \
139 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
140 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
141 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
142 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100143#endif
144
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100145/**
146 * 32-bit integer manipulation PUT macros (little endian)
147 *
148 * \brief Read from a 32 bit integer and store each byte
149 * in memory, offset by a specified amount, resulting
150 * in each byte being adjacent in memory.
151 * Little Endian is used when wanting to
152 * transmit the least signifcant bits first
153 *
154 * \param n 32 bit integer where data is accessed
155 * \param b const unsigned char array of data to be
156 * manipulated
157 * \param i offset in bytes, In the case of UINT32, i
158 * would increment by 4 every use assuming
159 * the data is being stored in the same location
160 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100161#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani4530b272021-07-05 15:37:39 +0100162#define MBEDTLS_PUT_UINT32_LE(n,b,i) \
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100163 do { \
164 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
165 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
166 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
167 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
168 } while( 0 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100169#endif
170
Joe Subbiani61f7d732021-06-24 09:06:23 +0100171/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100172 * 16-bit integer manipulation GET macros (little endian)
173 *
174 * \brief Use this to assign an unsigned 16 bit integer
175 * by taking data stored adjacent in memory that
176 * can be accessed via on offset
177 * Little Endian is used when wanting to
178 * transmit the least signifcant bits first
179 *
180 * \param data The data used to translate to a 16 bit
181 * integer
182 * \param offset the shit in bytes to access the next byte
183 * of data
Joe Subbiani927488e2021-06-23 11:23:44 +0100184 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100185#ifndef MBEDTLS_GET_UINT16_LE
186#define MBEDTLS_GET_UINT16_LE( data, offset ) \
187 ( \
188 ( (uint16_t) ( data )[( offset ) ] ) \
189 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100190 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100191#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100192
Joe Subbiani266476d2021-07-07 15:16:56 +0100193/**
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100194 * 16-bit integer manipulation PUT macros (little endian)
Joe Subbiani266476d2021-07-07 15:16:56 +0100195 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100196 * \brief Read from a 16 bit integer and store each byte
197 * in memory, offset by a specified amount, resulting
198 * in each byte being adjacent in memory.
199 * Little Endian is used when wanting to
200 * transmit the least signifcant bits first
Joe Subbiani266476d2021-07-07 15:16:56 +0100201 *
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100202 * \param n 16 bit integer where data is accessed
Joe Subbiani266476d2021-07-07 15:16:56 +0100203 * \param b const unsigned char array of data to be
204 * manipulated
205 * \param i offset in bytes, In the case of UINT16, i
206 * would increment by 2 every use assuming
207 * the data is being stored in the same location
208 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100209#ifndef MBEDTLS_PUT_UINT16_LE
210#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \
211{ \
212 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
213 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
214}
215#endif
216
217
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200218#endif /* MBEDTLS_LIBRARY_COMMON_H */