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 | |
| 26 | #if defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include MBEDTLS_CONFIG_FILE |
| 28 | #else |
| 29 | #include "mbedtls/config.h" |
| 30 | #endif |
| 31 | |
| 32 | /** Helper to define a function as static except when building invasive tests. |
| 33 | * |
| 34 | * If a function is only used inside its own source file and should be |
| 35 | * declared `static` to allow the compiler to optimize for code size, |
| 36 | * but that function has unit tests, define it with |
| 37 | * ``` |
| 38 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 39 | * ``` |
| 40 | * and declare it in a header in the `library/` directory with |
| 41 | * ``` |
| 42 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 43 | * int mbedtls_foo(...); |
| 44 | * #endif |
| 45 | * ``` |
| 46 | */ |
| 47 | #if defined(MBEDTLS_TEST_HOOKS) |
| 48 | #define MBEDTLS_STATIC_TESTABLE |
| 49 | #else |
| 50 | #define MBEDTLS_STATIC_TESTABLE static |
| 51 | #endif |
| 52 | |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 53 | /** Allow library to access its structs' private members. |
| 54 | * |
| 55 | * Although structs defined in header files are publicly available, |
| 56 | * their members are private and should not be accessed by the user. |
| 57 | */ |
| 58 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 59 | |
| 60 | /** Byte Reading Macros |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 61 | * |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame^] | 62 | * Obtain the most significant byte of x using 0xff |
| 63 | * Using MBEDTLS_BYTE_a will shift a*8 bits |
| 64 | * to retrieve the next byte of information |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 65 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 66 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) |
| 67 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 68 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 69 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 70 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame^] | 71 | /** |
| 72 | * 32-bit integer manipulation macros |
| 73 | * |
| 74 | * \brief Using GET- |
| 75 | * From input data, take the most significant bytes |
| 76 | * and concatonate them as you shift along |
| 77 | * Using PUT- |
| 78 | * Read from a 32 bit integer and store each byte |
| 79 | * in memory, offset by a byte each, resulting in |
| 80 | * each byte being adjacent in memory. |
| 81 | * |
| 82 | * \param n 32 bit integer where data is accessed via |
| 83 | * PUT or stored using GET |
| 84 | * \param b const unsigned char array of data to be |
| 85 | * manipulated |
| 86 | * \param i offset in bytes, In the case of UINT32, i |
| 87 | * would increment by 4 every use assuming |
| 88 | * the data is being stored in the same location |
| 89 | */ |
| 90 | |
| 91 | /** |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 92 | * 32-bit integer manipulation macros (big endian) |
| 93 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 94 | #ifndef MBEDTLS_GET_UINT32_BE |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 95 | #define MBEDTLS_GET_UINT32_BE(n,b,i) \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 96 | do { \ |
| 97 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
| 98 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
| 99 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
| 100 | | ( (uint32_t) (b)[(i) + 3] ); \ |
| 101 | } while( 0 ) |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 102 | #endif |
| 103 | |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 104 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 105 | #define MBEDTLS_PUT_UINT32_BE(n,b,i) \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 106 | do { \ |
| 107 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 108 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 109 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 110 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 111 | } while( 0 ) |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 112 | #endif |
| 113 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame^] | 114 | /** |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 115 | * 32-bit integer manipulation macros (little endian) |
| 116 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 117 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 118 | #define MBEDTLS_GET_UINT32_LE(n,b,i) \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 119 | do { \ |
| 120 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
| 121 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
| 122 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
| 123 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
| 124 | } while( 0 ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 125 | #endif |
| 126 | |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 127 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 128 | #define MBEDTLS_PUT_UINT32_LE(n,b,i) \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 129 | do { \ |
| 130 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 131 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 132 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 133 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 134 | } while( 0 ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 135 | #endif |
| 136 | |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 137 | /** |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 138 | * 32-bit integer conversion from bytes (little endian) |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 139 | */ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 140 | #define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \ |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 141 | ( (uint32_t) (data)[offset] \ |
| 142 | | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ |
| 143 | | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ |
| 144 | | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ |
| 145 | ) |
| 146 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame^] | 147 | /** |
| 148 | * 16-bit integer manipulation macros |
| 149 | * |
| 150 | * \brief Using GET- |
| 151 | * From input data, take the most significant bytes |
| 152 | * and concatonate them as you shift along |
| 153 | * Using PUT- |
| 154 | * Read from a 16 bit integer and store each byte |
| 155 | * in memory, offset by a byte each, resulting in |
| 156 | * each byte being adjacent in memory. |
| 157 | * |
| 158 | * \param n 16 bit integer where data is accessed via |
| 159 | * PUT or stored using GET |
| 160 | * \param b const unsigned char array of data to be |
| 161 | * manipulated |
| 162 | * \param i offset in bytes, In the case of UINT16, i |
| 163 | * would increment by 2 every use assuming |
| 164 | * the data is being stored in the same location |
| 165 | */ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 166 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame^] | 167 | /** |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 168 | * 16-bit integer manipulation macros (little endian) |
| 169 | */ |
| 170 | #ifndef MBEDTLS_GET_UINT16_LE |
| 171 | #define MBEDTLS_GET_UINT16_LE( n, b, i ) \ |
| 172 | { \ |
| 173 | (n) = ( (uint16_t) (b)[(i) ] ) \ |
| 174 | | ( (uint16_t) (b)[(i) + 1] << 8 ); \ |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #ifndef MBEDTLS_PUT_UINT16_LE |
| 179 | #define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ |
| 180 | { \ |
| 181 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 182 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 187 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |