blob: 5115465e885b5725c3ae40b16562c40e5ca64780 [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
28/** Helper to define a function as static except when building invasive tests.
29 *
30 * If a function is only used inside its own source file and should be
31 * declared `static` to allow the compiler to optimize for code size,
32 * but that function has unit tests, define it with
33 * ```
34 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
35 * ```
36 * and declare it in a header in the `library/` directory with
37 * ```
38 * #if defined(MBEDTLS_TEST_HOOKS)
39 * int mbedtls_foo(...);
40 * #endif
41 * ```
42 */
43#if defined(MBEDTLS_TEST_HOOKS)
44#define MBEDTLS_STATIC_TESTABLE
45#else
46#define MBEDTLS_STATIC_TESTABLE static
47#endif
48
TRodziewicz7871c2e2021-07-07 17:29:43 +020049#if defined(MBEDTLS_TEST_HOOKS)
50extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
51#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
52 do { \
53 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
54 { \
55 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
56 } \
57 } while( 0 )
58#else
59#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
60#endif /* defined(MBEDTLS_TEST_HOOKS) */
61
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020062/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020063 *
64 * Although structs defined in header files are publicly available,
65 * their members are private and should not be accessed by the user.
66 */
67#define MBEDTLS_ALLOW_PRIVATE_ACCESS
68
Joe Subbiani50dde562021-06-22 15:51:53 +010069/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010070 *
Joe Subbiani394bdd62021-07-07 15:16:56 +010071 * Obtain the most significant byte of x using 0xff
72 * Using MBEDTLS_BYTE_a will shift a*8 bits
73 * to retrieve the next byte of information
Joe Subbiani50dde562021-06-22 15:51:53 +010074 */
Joe Subbiani5ecac212021-06-24 13:00:03 +010075#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
76#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
77#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
78#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani50dde562021-06-22 15:51:53 +010079
Joe Subbiani394bdd62021-07-07 15:16:56 +010080/**
81 * 32-bit integer manipulation macros
82 *
83 * \brief Using GET-
84 * From input data, take the most significant bytes
85 * and concatonate them as you shift along
86 * Using PUT-
87 * Read from a 32 bit integer and store each byte
88 * in memory, offset by a byte each, resulting in
89 * each byte being adjacent in memory.
90 *
91 * \param n 32 bit integer where data is accessed via
92 * PUT or stored using GET
93 * \param b const unsigned char array of data to be
94 * manipulated
95 * \param i offset in bytes, In the case of UINT32, i
96 * would increment by 4 every use assuming
97 * the data is being stored in the same location
98 */
99
100/**
Joe Subbiani30d974c2021-06-23 11:49:03 +0100101 * 32-bit integer manipulation macros (big endian)
102 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100103#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100104#define MBEDTLS_GET_UINT32_BE(n,b,i) \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100105 do { \
106 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
107 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
108 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
109 | ( (uint32_t) (b)[(i) + 3] ); \
110 } while( 0 )
Joe Subbiani30d974c2021-06-23 11:49:03 +0100111#endif
112
Joe Subbiani5ecac212021-06-24 13:00:03 +0100113#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100114#define MBEDTLS_PUT_UINT32_BE(n,b,i) \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100115 do { \
116 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
117 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
118 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
119 (b)[(i) + 3] = (unsigned char) ( (n) ); \
120 } while( 0 )
Joe Subbiani30d974c2021-06-23 11:49:03 +0100121#endif
122
Joe Subbiani394bdd62021-07-07 15:16:56 +0100123/**
Joe Subbiani54c61342021-06-23 12:16:47 +0100124 * 32-bit integer manipulation macros (little endian)
125 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100126#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100127#define MBEDTLS_GET_UINT32_LE(n,b,i) \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100128 do { \
129 (n) = ( (uint32_t) (b)[(i) ] ) \
130 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
131 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
132 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
133 } while( 0 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100134#endif
135
Joe Subbiani5ecac212021-06-24 13:00:03 +0100136#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100137#define MBEDTLS_PUT_UINT32_LE(n,b,i) \
Joe Subbiani5ecac212021-06-24 13:00:03 +0100138 do { \
139 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
140 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
141 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
142 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
143 } while( 0 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100144#endif
145
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100146/**
Joe Subbiani54c61342021-06-23 12:16:47 +0100147 * 32-bit integer conversion from bytes (little endian)
Joe Subbiani3b394502021-06-23 11:23:44 +0100148 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100149#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100150 ( (uint32_t) (data)[offset] \
151 | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \
152 | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \
153 | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \
154 )
155
Joe Subbiani394bdd62021-07-07 15:16:56 +0100156/**
157 * 16-bit integer manipulation macros
158 *
159 * \brief Using GET-
160 * From input data, take the most significant bytes
161 * and concatonate them as you shift along
162 * Using PUT-
163 * Read from a 16 bit integer and store each byte
164 * in memory, offset by a byte each, resulting in
165 * each byte being adjacent in memory.
166 *
167 * \param n 16 bit integer where data is accessed via
168 * PUT or stored using GET
169 * \param b const unsigned char array of data to be
170 * manipulated
171 * \param i offset in bytes, In the case of UINT16, i
172 * would increment by 2 every use assuming
173 * the data is being stored in the same location
174 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100175
Joe Subbiani394bdd62021-07-07 15:16:56 +0100176/**
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100177 * 16-bit integer manipulation macros (little endian)
178 */
179#ifndef MBEDTLS_GET_UINT16_LE
180#define MBEDTLS_GET_UINT16_LE( n, b, i ) \
181{ \
182 (n) = ( (uint16_t) (b)[(i) ] ) \
183 | ( (uint16_t) (b)[(i) + 1] << 8 ); \
184}
185#endif
186
187#ifndef MBEDTLS_PUT_UINT16_LE
188#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \
189{ \
190 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
191 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
192}
193#endif
194
195
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200196#endif /* MBEDTLS_LIBRARY_COMMON_H */