| 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> | 
|  | 29 |  | 
| Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 30 | /** Helper to define a function as static except when building invasive tests. | 
|  | 31 | * | 
|  | 32 | * If a function is only used inside its own source file and should be | 
|  | 33 | * declared `static` to allow the compiler to optimize for code size, | 
|  | 34 | * but that function has unit tests, define it with | 
|  | 35 | * ``` | 
|  | 36 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } | 
|  | 37 | * ``` | 
|  | 38 | * and declare it in a header in the `library/` directory with | 
|  | 39 | * ``` | 
|  | 40 | * #if defined(MBEDTLS_TEST_HOOKS) | 
|  | 41 | * int mbedtls_foo(...); | 
|  | 42 | * #endif | 
|  | 43 | * ``` | 
|  | 44 | */ | 
|  | 45 | #if defined(MBEDTLS_TEST_HOOKS) | 
|  | 46 | #define MBEDTLS_STATIC_TESTABLE | 
|  | 47 | #else | 
|  | 48 | #define MBEDTLS_STATIC_TESTABLE static | 
|  | 49 | #endif | 
|  | 50 |  | 
| TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 51 | #if defined(MBEDTLS_TEST_HOOKS) | 
|  | 52 | extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file ); | 
|  | 53 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \ | 
|  | 54 | do { \ | 
|  | 55 | if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \ | 
|  | 56 | { \ | 
|  | 57 | ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \ | 
|  | 58 | } \ | 
|  | 59 | } while( 0 ) | 
|  | 60 | #else | 
|  | 61 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) | 
|  | 62 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ | 
|  | 63 |  | 
| Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 64 | /** Allow library to access its structs' private members. | 
| Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 65 | * | 
|  | 66 | * Although structs defined in header files are publicly available, | 
|  | 67 | * their members are private and should not be accessed by the user. | 
|  | 68 | */ | 
|  | 69 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS | 
|  | 70 |  | 
| Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 71 | /** Byte Reading Macros | 
| Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 72 | * | 
| Joe Subbiani | 9ab1866 | 2021-07-21 16:35:48 +0100 | [diff] [blame] | 73 | * 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] | 74 | * byte from x, where byte 0 is the least significant byte. | 
| Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 75 | */ | 
| Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 76 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) (   ( x )         & 0xff ) ) | 
| Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 77 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8  ) & 0xff ) ) | 
|  | 78 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) | 
|  | 79 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) | 
| Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 80 | #define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) ) | 
|  | 81 | #define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) ) | 
|  | 82 | #define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) ) | 
|  | 83 | #define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) ) | 
| Joe Subbiani | cd84d76 | 2021-07-08 14:59:52 +0100 | [diff] [blame] | 84 |  | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 85 | /** | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 86 | * Get the unsigned 32 bits integer corresponding to four bytes in | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 87 | * big-endian order (MSB first). | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 88 | * | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 89 | * \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] | 90 | * \param   offset  Offset from \p data of the first and most significant | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 91 | *                  byte of the four bytes to build the 32 bits unsigned | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 92 | *                  integer from. | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 93 | */ | 
|  | 94 | #ifndef MBEDTLS_GET_UINT32_BE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 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]       )         \ | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 101 | ) | 
|  | 102 | #endif | 
|  | 103 |  | 
|  | 104 | /** | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 105 | * Put in memory a 32 bits unsigned integer in big-endian order. | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 106 | * | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 107 | * \param   n       32 bits unsigned integer to put in memory. | 
|  | 108 | * \param   data    Base address of the memory where to put the 32 | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 109 | *                  bits unsigned integer in. | 
| Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 110 | * \param   offset  Offset from \p data where to put the most significant | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 111 | *                  byte of the 32 bits unsigned integer \p n. | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 112 | */ | 
| Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 113 | #ifndef MBEDTLS_PUT_UINT32_BE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 114 | #define MBEDTLS_PUT_UINT32_BE( n, data, offset )                \ | 
|  | 115 | {                                                               \ | 
|  | 116 | ( data )[( offset )    ] = MBEDTLS_BYTE_3( n );             \ | 
|  | 117 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n );             \ | 
|  | 118 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n );             \ | 
|  | 119 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n );             \ | 
|  | 120 | } | 
| Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 121 | #endif | 
|  | 122 |  | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 123 | /** | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 124 | * Get the unsigned 32 bits integer corresponding to four bytes in | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 125 | * little-endian order (LSB first). | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 126 | * | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 127 | * \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] | 128 | * \param   offset  Offset from \p data of the first and least significant | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 129 | *                  byte of the four bytes to build the 32 bits unsigned | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 130 | *                  integer from. | 
| Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 131 | */ | 
| Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 132 | #ifndef MBEDTLS_GET_UINT32_LE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 133 | #define MBEDTLS_GET_UINT32_LE( data, offset )                   \ | 
|  | 134 | (                                                           \ | 
|  | 135 | ( (uint32_t) ( data )[( offset )    ]       )         \ | 
|  | 136 | | ( (uint32_t) ( data )[( offset ) + 1] <<  8 )         \ | 
|  | 137 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 )         \ | 
|  | 138 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 )         \ | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 139 | ) | 
| Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 140 | #endif | 
|  | 141 |  | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 142 | /** | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 143 | * Put in memory a 32 bits unsigned integer in little-endian order. | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 144 | * | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 145 | * \param   n       32 bits unsigned integer to put in memory. | 
|  | 146 | * \param   data    Base address of the memory where to put the 32 | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 147 | *                  bits unsigned integer in. | 
| Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 148 | * \param   offset  Offset from \p data where to put the least significant | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 149 | *                  byte of the 32 bits unsigned integer \p n. | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 150 | */ | 
| Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 151 | #ifndef MBEDTLS_PUT_UINT32_LE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 152 | #define MBEDTLS_PUT_UINT32_LE( n, data, offset )                \ | 
|  | 153 | {                                                               \ | 
|  | 154 | ( data )[( offset )    ] = MBEDTLS_BYTE_0( n );             \ | 
|  | 155 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n );             \ | 
|  | 156 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n );             \ | 
|  | 157 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n );             \ | 
|  | 158 | } | 
| Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 159 | #endif | 
|  | 160 |  | 
| Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 161 | /** | 
| Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 162 | * Get the unsigned 16 bits integer corresponding to two bytes in | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 163 | * little-endian order (LSB first). | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 164 | * | 
| Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 165 | * \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] | 166 | * \param   offset  Offset from \p data of the first and least significant | 
| Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 167 | *                  byte of the two bytes to build the 16 bits unsigned | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 168 | *                  integer from. | 
| Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 169 | */ | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 170 | #ifndef MBEDTLS_GET_UINT16_LE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 171 | #define MBEDTLS_GET_UINT16_LE( data, offset )                   \ | 
|  | 172 | (                                                           \ | 
|  | 173 | ( (uint16_t) ( data )[( offset )    ]       )         \ | 
|  | 174 | | ( (uint16_t) ( data )[( offset ) + 1] <<  8 )         \ | 
| Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 175 | ) | 
| Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 176 | #endif | 
| Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 177 |  | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 178 | /** | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 179 | * Put in memory a 16 bits unsigned integer in little-endian order. | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 180 | * | 
| Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 181 | * \param   n       16 bits unsigned integer to put in memory. | 
|  | 182 | * \param   data    Base address of the memory where to put the 16 | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 183 | *                  bits unsigned integer in. | 
| Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 184 | * \param   offset  Offset from \p data where to put the least significant | 
| Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 185 | *                  byte of the 16 bits unsigned integer \p n. | 
| Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 186 | */ | 
| Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 187 | #ifndef MBEDTLS_PUT_UINT16_LE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 188 | #define MBEDTLS_PUT_UINT16_LE( n, data, offset )                \ | 
|  | 189 | {                                                               \ | 
|  | 190 | ( data )[( offset )    ] = MBEDTLS_BYTE_0( n );             \ | 
|  | 191 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n );             \ | 
| Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 192 | } | 
|  | 193 | #endif | 
|  | 194 |  | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 195 | /** | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 196 | * Get the unsigned 16 bits integer corresponding to two bytes in | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 197 | * big-endian order (MSB first). | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 198 | * | 
|  | 199 | * \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] | 200 | * \param   offset  Offset from \p data of the first and most significant | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 201 | *                  byte of the two bytes to build the 16 bits unsigned | 
|  | 202 | *                  integer from. | 
|  | 203 | */ | 
|  | 204 | #ifndef MBEDTLS_GET_UINT16_BE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 205 | #define MBEDTLS_GET_UINT16_BE( data, offset )                   \ | 
|  | 206 | (                                                           \ | 
|  | 207 | ( (uint16_t) ( data )[( offset )    ] << 8 )          \ | 
|  | 208 | | ( (uint16_t) ( data )[( offset ) + 1]      )          \ | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 209 | ) | 
|  | 210 | #endif | 
|  | 211 |  | 
|  | 212 | /** | 
|  | 213 | * Put in memory a 16 bits unsigned integer in big-endian order. | 
|  | 214 | * | 
|  | 215 | * \param   n       16 bits unsigned integer to put in memory. | 
|  | 216 | * \param   data    Base address of the memory where to put the 16 | 
|  | 217 | *                  bits unsigned integer in. | 
| Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 218 | * \param   offset  Offset from \p data where to put the most significant | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 219 | *                  byte of the 16 bits unsigned integer \p n. | 
|  | 220 | */ | 
|  | 221 | #ifndef MBEDTLS_PUT_UINT16_BE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 222 | #define MBEDTLS_PUT_UINT16_BE( n, data, offset )                \ | 
|  | 223 | {                                                               \ | 
|  | 224 | ( data )[( offset )    ] = MBEDTLS_BYTE_1( n );             \ | 
|  | 225 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n );             \ | 
| Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 226 | } | 
|  | 227 | #endif | 
|  | 228 |  | 
|  | 229 | /** | 
| Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 230 | * Get the unsigned 24 bits integer corresponding to three bytes in | 
| Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 231 | * big-endian order (MSB first). | 
|  | 232 | * | 
| Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 233 | * \param   data    Base address of the memory to get the three bytes from. | 
|  | 234 | * \param   offset  Offset from \p data of the first and most significant | 
|  | 235 | *                  byte of the three bytes to build the 24 bits unsigned | 
| Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 236 | *                  integer from. | 
|  | 237 | */ | 
|  | 238 | #ifndef MBEDTLS_GET_UINT24_BE | 
|  | 239 | #define MBEDTLS_GET_UINT24_BE( data , offset )                  \ | 
|  | 240 | (                                                           \ | 
|  | 241 | ( (uint32_t) ( data )[( offset )    ] << 16 )         \ | 
|  | 242 | | ( (uint32_t) ( data )[( offset ) + 1] << 8  )         \ | 
|  | 243 | | ( (uint32_t) ( data )[( offset ) + 2]       )         \ | 
|  | 244 | ) | 
|  | 245 | #endif | 
|  | 246 |  | 
|  | 247 | /** | 
|  | 248 | * Put in memory a 24 bits unsigned integer in big-endian order. | 
|  | 249 | * | 
|  | 250 | * \param   n       24 bits unsigned integer to put in memory. | 
|  | 251 | * \param   data    Base address of the memory where to put the 24 | 
|  | 252 | *                  bits unsigned integer in. | 
| Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 253 | * \param   offset  Offset from \p data where to put the most significant | 
| Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 254 | *                  byte of the 24 bits unsigned integer \p n. | 
|  | 255 | */ | 
|  | 256 | #ifndef MBEDTLS_PUT_UINT24_BE | 
|  | 257 | #define MBEDTLS_PUT_UINT24_BE( n, data, offset )                \ | 
|  | 258 | {                                                               \ | 
|  | 259 | ( data )[( offset )    ] = MBEDTLS_BYTE_2( n );             \ | 
|  | 260 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n );             \ | 
|  | 261 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n );             \ | 
|  | 262 | } | 
|  | 263 | #endif | 
|  | 264 |  | 
|  | 265 | /** | 
| Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 266 | * Get the unsigned 24 bits integer corresponding to three bytes in | 
| Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 267 | * little-endian order (LSB first). | 
|  | 268 | * | 
| Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 269 | * \param   data    Base address of the memory to get the three bytes from. | 
|  | 270 | * \param   offset  Offset from \p data of the first and least significant | 
|  | 271 | *                  byte of the three bytes to build the 24 bits unsigned | 
| Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 272 | *                  integer from. | 
|  | 273 | */ | 
|  | 274 | #ifndef MBEDTLS_GET_UINT24_LE | 
|  | 275 | #define MBEDTLS_GET_UINT24_LE( data, offset )                   \ | 
|  | 276 | (                                                           \ | 
|  | 277 | ( (uint32_t) ( data )[( offset )    ]       )         \ | 
|  | 278 | | ( (uint32_t) ( data )[( offset ) + 1] <<  8 )         \ | 
|  | 279 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 )         \ | 
|  | 280 | ) | 
|  | 281 | #endif | 
|  | 282 |  | 
|  | 283 | /** | 
|  | 284 | * Put in memory a 24 bits unsigned integer in little-endian order. | 
|  | 285 | * | 
|  | 286 | * \param   n       24 bits unsigned integer to put in memory. | 
|  | 287 | * \param   data    Base address of the memory where to put the 24 | 
|  | 288 | *                  bits unsigned integer in. | 
| Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 289 | * \param   offset  Offset from \p data where to put the least significant | 
| Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 290 | *                  byte of the 24 bits unsigned integer \p n. | 
|  | 291 | */ | 
|  | 292 | #ifndef MBEDTLS_PUT_UINT24_LE | 
|  | 293 | #define MBEDTLS_PUT_UINT24_LE( n, data, offset )                \ | 
|  | 294 | {                                                               \ | 
|  | 295 | ( data )[( offset )    ] = MBEDTLS_BYTE_0( n );             \ | 
|  | 296 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n );             \ | 
|  | 297 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n );             \ | 
|  | 298 | } | 
|  | 299 | #endif | 
|  | 300 |  | 
|  | 301 | /** | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 302 | * Get the unsigned 64 bits integer corresponding to eight bytes in | 
|  | 303 | * big-endian order (MSB first). | 
|  | 304 | * | 
|  | 305 | * \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] | 306 | * \param   offset  Offset from \p data of the first and most significant | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 307 | *                  byte of the eight bytes to build the 64 bits unsigned | 
|  | 308 | *                  integer from. | 
|  | 309 | */ | 
|  | 310 | #ifndef MBEDTLS_GET_UINT64_BE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 311 | #define MBEDTLS_GET_UINT64_BE( data, offset )                   \ | 
|  | 312 | (                                                           \ | 
|  | 313 | ( (uint64_t) ( data )[( offset )    ] << 56 )         \ | 
|  | 314 | | ( (uint64_t) ( data )[( offset ) + 1] << 48 )         \ | 
|  | 315 | | ( (uint64_t) ( data )[( offset ) + 2] << 40 )         \ | 
|  | 316 | | ( (uint64_t) ( data )[( offset ) + 3] << 32 )         \ | 
|  | 317 | | ( (uint64_t) ( data )[( offset ) + 4] << 24 )         \ | 
|  | 318 | | ( (uint64_t) ( data )[( offset ) + 5] << 16 )         \ | 
|  | 319 | | ( (uint64_t) ( data )[( offset ) + 6] <<  8 )         \ | 
|  | 320 | | ( (uint64_t) ( data )[( offset ) + 7]       )         \ | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 321 | ) | 
|  | 322 | #endif | 
|  | 323 |  | 
|  | 324 | /** | 
|  | 325 | * Put in memory a 64 bits unsigned integer in big-endian order. | 
|  | 326 | * | 
|  | 327 | * \param   n       64 bits unsigned integer to put in memory. | 
|  | 328 | * \param   data    Base address of the memory where to put the 64 | 
|  | 329 | *                  bits unsigned integer in. | 
| Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 330 | * \param   offset  Offset from \p data where to put the most significant | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 331 | *                  byte of the 64 bits unsigned integer \p n. | 
|  | 332 | */ | 
|  | 333 | #ifndef MBEDTLS_PUT_UINT64_BE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 334 | #define MBEDTLS_PUT_UINT64_BE( n, data, offset )                \ | 
|  | 335 | {                                                               \ | 
|  | 336 | ( data )[( offset )    ] = MBEDTLS_BYTE_7( n );             \ | 
|  | 337 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n );             \ | 
|  | 338 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n );             \ | 
|  | 339 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n );             \ | 
|  | 340 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n );             \ | 
|  | 341 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n );             \ | 
|  | 342 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n );             \ | 
|  | 343 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n );             \ | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 344 | } | 
|  | 345 | #endif | 
|  | 346 |  | 
|  | 347 | /** | 
|  | 348 | * Get the unsigned 64 bits integer corresponding to eight bytes in | 
|  | 349 | * little-endian order (LSB first). | 
|  | 350 | * | 
|  | 351 | * \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] | 352 | * \param   offset  Offset from \p data of the first and least significant | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 353 | *                  byte of the eight bytes to build the 64 bits unsigned | 
|  | 354 | *                  integer from. | 
|  | 355 | */ | 
|  | 356 | #ifndef MBEDTLS_GET_UINT64_LE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 357 | #define MBEDTLS_GET_UINT64_LE( data, offset )                   \ | 
|  | 358 | (                                                           \ | 
|  | 359 | ( (uint64_t) ( data )[( offset ) + 7] << 56 )         \ | 
|  | 360 | | ( (uint64_t) ( data )[( offset ) + 6] << 48 )         \ | 
|  | 361 | | ( (uint64_t) ( data )[( offset ) + 5] << 40 )         \ | 
|  | 362 | | ( (uint64_t) ( data )[( offset ) + 4] << 32 )         \ | 
|  | 363 | | ( (uint64_t) ( data )[( offset ) + 3] << 24 )         \ | 
|  | 364 | | ( (uint64_t) ( data )[( offset ) + 2] << 16 )         \ | 
|  | 365 | | ( (uint64_t) ( data )[( offset ) + 1] <<  8 )         \ | 
|  | 366 | | ( (uint64_t) ( data )[( offset )    ]       )         \ | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 367 | ) | 
|  | 368 | #endif | 
|  | 369 |  | 
|  | 370 | /** | 
|  | 371 | * Put in memory a 64 bits unsigned integer in little-endian order. | 
|  | 372 | * | 
|  | 373 | * \param   n       64 bits unsigned integer to put in memory. | 
|  | 374 | * \param   data    Base address of the memory where to put the 64 | 
|  | 375 | *                  bits unsigned integer in. | 
| Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 376 | * \param   offset  Offset from \p data where to put the least significant | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 377 | *                  byte of the 64 bits unsigned integer \p n. | 
|  | 378 | */ | 
|  | 379 | #ifndef MBEDTLS_PUT_UINT64_LE | 
| Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 380 | #define MBEDTLS_PUT_UINT64_LE( n, data, offset )                \ | 
|  | 381 | {                                                               \ | 
|  | 382 | ( data )[( offset )    ] = MBEDTLS_BYTE_0( n );             \ | 
|  | 383 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n );             \ | 
|  | 384 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n );             \ | 
|  | 385 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n );             \ | 
|  | 386 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n );             \ | 
|  | 387 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n );             \ | 
|  | 388 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n );             \ | 
|  | 389 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n );             \ | 
| Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 390 | } | 
|  | 391 | #endif | 
| Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 392 |  | 
| Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 393 | /* Fix MSVC C99 compatible issue | 
|  | 394 | *      MSVC support __func__ from visual studio 2015( 1900 ) | 
|  | 395 | *      Use MSVC predefine macro to avoid name check fail. | 
|  | 396 | */ | 
|  | 397 | #if (defined(_MSC_VER) && ( _MSC_VER <= 1900 )) | 
| Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 398 | #define /*no-check-names*/ __func__ __FUNCTION__ | 
| Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 399 | #endif | 
|  | 400 |  | 
| Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 401 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |