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