Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file common.h |
| 3 | * |
| 4 | * \brief Utility macros for internal use in the library |
| 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 8 | * 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 Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef MBEDTLS_LIBRARY_COMMON_H |
| 24 | #define MBEDTLS_LIBRARY_COMMON_H |
| 25 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 27 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame^] | 29 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 30 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 31 | /** 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 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_TEST_HOOKS) |
| 53 | extern 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 Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 65 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 66 | * |
| 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 | |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 72 | /** Byte Reading Macros |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 73 | * |
Joe Subbiani | 9ab1866 | 2021-07-21 16:35:48 +0100 | [diff] [blame] | 74 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
Joe Subbiani | d068785 | 2021-07-21 15:22:47 +0100 | [diff] [blame] | 75 | * byte from x, where byte 0 is the least significant byte. |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 76 | */ |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 77 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 78 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 79 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 80 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 81 | #define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) ) |
| 82 | #define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) ) |
| 83 | #define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) ) |
| 84 | #define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) ) |
Joe Subbiani | cd84d76 | 2021-07-08 14:59:52 +0100 | [diff] [blame] | 85 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 86 | /** |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 87 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 88 | * big-endian order (MSB first). |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 89 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 90 | * \param data Base address of the memory to get the four bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 91 | * \param offset Offset from \p data of the first and most significant |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 92 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 93 | * integer from. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 94 | */ |
| 95 | #ifndef MBEDTLS_GET_UINT32_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 96 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 97 | ( \ |
| 98 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 99 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 100 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 101 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 102 | ) |
| 103 | #endif |
| 104 | |
| 105 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 106 | * Put in memory a 32 bits unsigned integer in big-endian order. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 107 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 108 | * \param n 32 bits unsigned integer to put in memory. |
| 109 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 110 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 111 | * \param offset Offset from \p data where to put the most significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 112 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 113 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 114 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 115 | #define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ |
| 116 | { \ |
| 117 | ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \ |
| 118 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \ |
| 119 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \ |
| 120 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \ |
| 121 | } |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 122 | #endif |
| 123 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 124 | /** |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 125 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 126 | * little-endian order (LSB first). |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 127 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 128 | * \param data Base address of the memory to get the four bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 129 | * \param offset Offset from \p data of the first and least significant |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 130 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 131 | * integer from. |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 132 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 133 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 134 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 135 | ( \ |
| 136 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 137 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 138 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 139 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 140 | ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 141 | #endif |
| 142 | |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 143 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 144 | * Put in memory a 32 bits unsigned integer in little-endian order. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 145 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 146 | * \param n 32 bits unsigned integer to put in memory. |
| 147 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 148 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 149 | * \param offset Offset from \p data where to put the least significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 150 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 151 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 152 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 153 | #define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ |
| 154 | { \ |
| 155 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 156 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 157 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 158 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ |
| 159 | } |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 160 | #endif |
| 161 | |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 162 | /** |
Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 163 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 164 | * little-endian order (LSB first). |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 165 | * |
Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 166 | * \param data Base address of the memory to get the two bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 167 | * \param offset Offset from \p data of the first and least significant |
Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 168 | * byte of the two bytes to build the 16 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 169 | * integer from. |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 170 | */ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 171 | #ifndef MBEDTLS_GET_UINT16_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 172 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 173 | ( \ |
| 174 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 175 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 176 | ) |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 177 | #endif |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 178 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 179 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 180 | * Put in memory a 16 bits unsigned integer in little-endian order. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 181 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 182 | * \param n 16 bits unsigned integer to put in memory. |
| 183 | * \param data Base address of the memory where to put the 16 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 184 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 185 | * \param offset Offset from \p data where to put the least significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 186 | * byte of the 16 bits unsigned integer \p n. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 187 | */ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 188 | #ifndef MBEDTLS_PUT_UINT16_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 189 | #define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ |
| 190 | { \ |
| 191 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 192 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 193 | } |
| 194 | #endif |
| 195 | |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 196 | /** |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 197 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 198 | * big-endian order (MSB first). |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 199 | * |
| 200 | * \param data Base address of the memory to get the two bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 201 | * \param offset Offset from \p data of the first and most significant |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 202 | * byte of the two bytes to build the 16 bits unsigned |
| 203 | * integer from. |
| 204 | */ |
| 205 | #ifndef MBEDTLS_GET_UINT16_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 206 | #define MBEDTLS_GET_UINT16_BE( data, offset ) \ |
| 207 | ( \ |
| 208 | ( (uint16_t) ( data )[( offset ) ] << 8 ) \ |
| 209 | | ( (uint16_t) ( data )[( offset ) + 1] ) \ |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 210 | ) |
| 211 | #endif |
| 212 | |
| 213 | /** |
| 214 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 215 | * |
| 216 | * \param n 16 bits unsigned integer to put in memory. |
| 217 | * \param data Base address of the memory where to put the 16 |
| 218 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 219 | * \param offset Offset from \p data where to put the most significant |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 220 | * byte of the 16 bits unsigned integer \p n. |
| 221 | */ |
| 222 | #ifndef MBEDTLS_PUT_UINT16_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 223 | #define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \ |
| 224 | { \ |
| 225 | ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \ |
| 226 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \ |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 227 | } |
| 228 | #endif |
| 229 | |
| 230 | /** |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 231 | * Get the unsigned 24 bits integer corresponding to three bytes in |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 232 | * big-endian order (MSB first). |
| 233 | * |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 234 | * \param data Base address of the memory to get the three bytes from. |
| 235 | * \param offset Offset from \p data of the first and most significant |
| 236 | * byte of the three bytes to build the 24 bits unsigned |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 237 | * integer from. |
| 238 | */ |
| 239 | #ifndef MBEDTLS_GET_UINT24_BE |
| 240 | #define MBEDTLS_GET_UINT24_BE( data , offset ) \ |
| 241 | ( \ |
| 242 | ( (uint32_t) ( data )[( offset ) ] << 16 ) \ |
| 243 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 244 | | ( (uint32_t) ( data )[( offset ) + 2] ) \ |
| 245 | ) |
| 246 | #endif |
| 247 | |
| 248 | /** |
| 249 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 250 | * |
| 251 | * \param n 24 bits unsigned integer to put in memory. |
| 252 | * \param data Base address of the memory where to put the 24 |
| 253 | * bits unsigned integer in. |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 254 | * \param offset Offset from \p data where to put the most significant |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 255 | * byte of the 24 bits unsigned integer \p n. |
| 256 | */ |
| 257 | #ifndef MBEDTLS_PUT_UINT24_BE |
| 258 | #define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \ |
| 259 | { \ |
| 260 | ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \ |
| 261 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 262 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \ |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | /** |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 267 | * Get the unsigned 24 bits integer corresponding to three bytes in |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 268 | * little-endian order (LSB first). |
| 269 | * |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 270 | * \param data Base address of the memory to get the three bytes from. |
| 271 | * \param offset Offset from \p data of the first and least significant |
| 272 | * byte of the three bytes to build the 24 bits unsigned |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 273 | * integer from. |
| 274 | */ |
| 275 | #ifndef MBEDTLS_GET_UINT24_LE |
| 276 | #define MBEDTLS_GET_UINT24_LE( data, offset ) \ |
| 277 | ( \ |
| 278 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 279 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 280 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 281 | ) |
| 282 | #endif |
| 283 | |
| 284 | /** |
| 285 | * Put in memory a 24 bits unsigned integer in little-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 Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 290 | * \param offset Offset from \p data where to put the least significant |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 291 | * byte of the 24 bits unsigned integer \p n. |
| 292 | */ |
| 293 | #ifndef MBEDTLS_PUT_UINT24_LE |
| 294 | #define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \ |
| 295 | { \ |
| 296 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 297 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 298 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | /** |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 303 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 304 | * big-endian order (MSB first). |
| 305 | * |
| 306 | * \param data Base address of the memory to get the eight bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 307 | * \param offset Offset from \p data of the first and most significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 308 | * byte of the eight bytes to build the 64 bits unsigned |
| 309 | * integer from. |
| 310 | */ |
| 311 | #ifndef MBEDTLS_GET_UINT64_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 312 | #define MBEDTLS_GET_UINT64_BE( data, offset ) \ |
| 313 | ( \ |
| 314 | ( (uint64_t) ( data )[( offset ) ] << 56 ) \ |
| 315 | | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \ |
| 316 | | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \ |
| 317 | | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \ |
| 318 | | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \ |
| 319 | | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \ |
| 320 | | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \ |
| 321 | | ( (uint64_t) ( data )[( offset ) + 7] ) \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 322 | ) |
| 323 | #endif |
| 324 | |
| 325 | /** |
| 326 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 327 | * |
| 328 | * \param n 64 bits unsigned integer to put in memory. |
| 329 | * \param data Base address of the memory where to put the 64 |
| 330 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 331 | * \param offset Offset from \p data where to put the most significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 332 | * byte of the 64 bits unsigned integer \p n. |
| 333 | */ |
| 334 | #ifndef MBEDTLS_PUT_UINT64_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 335 | #define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \ |
| 336 | { \ |
| 337 | ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \ |
| 338 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \ |
| 339 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \ |
| 340 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \ |
| 341 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \ |
| 342 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \ |
| 343 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \ |
| 344 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 345 | } |
| 346 | #endif |
| 347 | |
| 348 | /** |
| 349 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 350 | * little-endian order (LSB first). |
| 351 | * |
| 352 | * \param data Base address of the memory to get the eight bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 353 | * \param offset Offset from \p data of the first and least significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 354 | * byte of the eight bytes to build the 64 bits unsigned |
| 355 | * integer from. |
| 356 | */ |
| 357 | #ifndef MBEDTLS_GET_UINT64_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 358 | #define MBEDTLS_GET_UINT64_LE( data, offset ) \ |
| 359 | ( \ |
| 360 | ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \ |
| 361 | | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \ |
| 362 | | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \ |
| 363 | | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \ |
| 364 | | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \ |
| 365 | | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \ |
| 366 | | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \ |
| 367 | | ( (uint64_t) ( data )[( offset ) ] ) \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 368 | ) |
| 369 | #endif |
| 370 | |
| 371 | /** |
| 372 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 373 | * |
| 374 | * \param n 64 bits unsigned integer to put in memory. |
| 375 | * \param data Base address of the memory where to put the 64 |
| 376 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 377 | * \param offset Offset from \p data where to put the least significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 378 | * byte of the 64 bits unsigned integer \p n. |
| 379 | */ |
| 380 | #ifndef MBEDTLS_PUT_UINT64_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 381 | #define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \ |
| 382 | { \ |
| 383 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 384 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 385 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 386 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ |
| 387 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \ |
| 388 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \ |
| 389 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \ |
| 390 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 391 | } |
| 392 | #endif |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 393 | |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame^] | 394 | /** |
| 395 | * Perform a fast block XOR operation, such that |
| 396 | * r[i] = a[i] ^ b[i] where 0 <= i < n |
| 397 | * |
| 398 | * \param r Pointer to result (buffer of at least \p n bytes). \p r |
| 399 | * may be equal to either \p a or \p b, but behaviour when |
| 400 | * it overlaps in other ways is undefined. |
| 401 | * \param a Pointer to input (buffer of at least \p n bytes) |
| 402 | * \param b Pointer to input (buffer of at least \p n bytes) |
| 403 | * \param n Number of bytes to process. |
| 404 | */ |
| 405 | static inline void mbedtls_xor( unsigned char* r, unsigned char const *a, unsigned char const *b, size_t n ) |
| 406 | { |
| 407 | uint32_t *a32 = (uint32_t*)a; |
| 408 | uint32_t *b32 = (uint32_t*)b; |
| 409 | uint32_t *r32 = (uint32_t*)r; |
| 410 | for ( size_t i = 0; i < (n >> 2); i++ ) |
| 411 | { |
| 412 | r32[i] = a32[i] ^ b32[i]; |
| 413 | } |
| 414 | for ( size_t i = n - (n % 4) ; i < n; i++ ) |
| 415 | { |
| 416 | r[i] = a[i] ^ b[i]; |
| 417 | } |
| 418 | } |
| 419 | |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 420 | /* Fix MSVC C99 compatible issue |
| 421 | * MSVC support __func__ from visual studio 2015( 1900 ) |
| 422 | * Use MSVC predefine macro to avoid name check fail. |
| 423 | */ |
| 424 | #if (defined(_MSC_VER) && ( _MSC_VER <= 1900 )) |
Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 425 | #define /*no-check-names*/ __func__ __FUNCTION__ |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 426 | #endif |
| 427 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 428 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |