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 | |
| 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 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_TEST_HOOKS) |
| 50 | extern 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 Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 62 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 63 | * |
| 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 Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 69 | /** Byte Reading Macros |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 70 | * |
| 71 | * To tidy up code and save horizontal and vertical space, use byte |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 72 | * reading macros to cast |
| 73 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 74 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) |
| 75 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 76 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 77 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 78 | |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 79 | /* |
| 80 | * 32-bit integer manipulation macros (big endian) |
| 81 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 82 | #ifndef MBEDTLS_GET_UINT32_BE |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame^] | 83 | #define MBEDTLS_GET_UINT32_BE(n,b,i) \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 84 | do { \ |
| 85 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
| 86 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
| 87 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
| 88 | | ( (uint32_t) (b)[(i) + 3] ); \ |
| 89 | } while( 0 ) |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 90 | #endif |
| 91 | |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 92 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame^] | 93 | #define MBEDTLS_PUT_UINT32_BE(n,b,i) \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 94 | do { \ |
| 95 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 96 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 97 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 98 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 99 | } while( 0 ) |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 100 | #endif |
| 101 | |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 102 | /* |
| 103 | * 32-bit integer manipulation macros (little endian) |
| 104 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 105 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame^] | 106 | #define MBEDTLS_GET_UINT32_LE(n,b,i) \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 107 | do { \ |
| 108 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
| 109 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
| 110 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
| 111 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
| 112 | } while( 0 ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 113 | #endif |
| 114 | |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 115 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame^] | 116 | #define MBEDTLS_PUT_UINT32_LE(n,b,i) \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 117 | do { \ |
| 118 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 119 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 120 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 121 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 122 | } while( 0 ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 123 | #endif |
| 124 | |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 125 | /** |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 126 | * 32-bit integer conversion from bytes (little endian) |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 127 | */ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame^] | 128 | #define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \ |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 129 | ( (uint32_t) (data)[offset] \ |
| 130 | | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ |
| 131 | | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ |
| 132 | | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ |
| 133 | ) |
| 134 | |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame^] | 135 | |
| 136 | /* |
| 137 | * 16-bit integer manipulation macros (little endian) |
| 138 | */ |
| 139 | #ifndef MBEDTLS_GET_UINT16_LE |
| 140 | #define MBEDTLS_GET_UINT16_LE( n, b, i ) \ |
| 141 | { \ |
| 142 | (n) = ( (uint16_t) (b)[(i) ] ) \ |
| 143 | | ( (uint16_t) (b)[(i) + 1] << 8 ); \ |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | #ifndef MBEDTLS_PUT_UINT16_LE |
| 148 | #define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ |
| 149 | { \ |
| 150 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 151 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 156 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |