Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame^] | 3 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 4 | * |
Gilles Peskine | e820c0a | 2023-08-03 17:45:20 +0200 | [diff] [blame] | 5 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /** |
Hanno Becker | 61d7eed | 2021-03-05 05:09:37 +0000 | [diff] [blame] | 9 | * \file mps_common.h |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 10 | * |
| 11 | * \brief Common functions and macros used by MPS |
| 12 | */ |
| 13 | |
| 14 | #ifndef MBEDTLS_MPS_COMMON_H |
| 15 | #define MBEDTLS_MPS_COMMON_H |
| 16 | |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 17 | #include "mps_error.h" |
| 18 | |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | |
Hanno Becker | 6ed183c | 2021-01-12 06:42:16 +0000 | [diff] [blame] | 21 | /** |
| 22 | * \name SECTION: MPS Configuration |
| 23 | * |
| 24 | * \{ |
| 25 | */ |
| 26 | |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 27 | /*! This flag controls whether the MPS-internal components |
| 28 | * (reader, writer, Layer 1-3) perform validation of the |
| 29 | * expected abstract state at the entry of API calls. |
| 30 | * |
| 31 | * Context: All MPS API functions impose assumptions/preconditions on the |
| 32 | * context on which they operate. For example, every structure has a notion of |
| 33 | * state integrity which is established by `xxx_init()` and preserved by any |
| 34 | * calls to the MPS API which satisfy their preconditions and either succeed, |
| 35 | * or fail with an error code which is explicitly documented to not corrupt |
| 36 | * structure integrity (such as WANT_READ and WANT_WRITE); |
| 37 | * apart from `xxx_init()` any function assumes state integrity as a |
| 38 | * precondition (but usually more). If any of the preconditions is violated, |
| 39 | * the function's behavior is entirely undefined. |
| 40 | * In addition to state integrity, all MPS structures have a more refined |
| 41 | * notion of abstract state that the API operates on. For example, all layers |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 42 | * have a notion of 'abstract read state' which indicates if incoming data has |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 43 | * been passed to the user, e.g. through mps_l2_read_start() for Layer 2 |
| 44 | * or mps_l3_read() in Layer 3. After such a call, it doesn't make sense to |
| 45 | * call these reading functions again until the incoming data has been |
| 46 | * explicitly 'consumed', e.g. through mps_l2_read_consume() for Layer 2 or |
| 47 | * mps_l3_read_consume() on Layer 3. However, even if it doesn't make sense, |
| 48 | * it's a design choice whether the API should fail gracefully on such |
| 49 | * non-sensical calls or not, and that's what this option is about: |
| 50 | * |
| 51 | * This option determines whether the expected abstract state |
Hanno Becker | 6e3484e | 2021-02-22 15:09:03 +0000 | [diff] [blame] | 52 | * is part of the API preconditions or not: If the option is set, |
| 53 | * then the abstract state is not part of the precondition and is |
| 54 | * thus required to be validated by the implementation. If an unexpected |
| 55 | * abstract state is encountered, the implementation must fail gracefully |
| 56 | * with error #MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED. |
| 57 | * Conversely, if this option is not set, then the expected abstract state |
| 58 | * is included in the preconditions of the respective API calls, and |
| 59 | * an implementation's behaviour is undefined if the abstract state is |
| 60 | * not as expected. |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 61 | * |
| 62 | * For example: Enabling this makes mps_l2_read_done() fail if |
| 63 | * no incoming record is currently open; disabling this would |
| 64 | * lead to undefined behavior in this case. |
| 65 | * |
| 66 | * Comment this to remove state validation. |
| 67 | */ |
| 68 | #define MBEDTLS_MPS_STATE_VALIDATION |
| 69 | |
Hanno Becker | 6ed183c | 2021-01-12 06:42:16 +0000 | [diff] [blame] | 70 | /*! This flag enables/disables assertions on the internal state of MPS. |
| 71 | * |
| 72 | * Assertions are sanity checks that should never trigger when MPS |
| 73 | * is used within the bounds of its API and preconditions. |
| 74 | * |
| 75 | * Enabling this increases security by limiting the scope of |
| 76 | * potential bugs, but comes at the cost of increased code size. |
| 77 | * |
| 78 | * Note: So far, there is no guiding principle as to what |
| 79 | * expected conditions merit an assertion, and which don't. |
| 80 | * |
| 81 | * Comment this to disable assertions. |
| 82 | */ |
| 83 | #define MBEDTLS_MPS_ENABLE_ASSERTIONS |
| 84 | |
Hanno Becker | 1ae9f75 | 2021-01-12 06:43:17 +0000 | [diff] [blame] | 85 | /*! This flag controls whether tracing for MPS should be enabled. */ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 86 | //#define MBEDTLS_MPS_ENABLE_TRACE |
Hanno Becker | 1ae9f75 | 2021-01-12 06:43:17 +0000 | [diff] [blame] | 87 | |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 88 | #if defined(MBEDTLS_MPS_STATE_VALIDATION) |
| 89 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | #define MBEDTLS_MPS_STATE_VALIDATE_RAW(cond, string) \ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 91 | do \ |
| 92 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | if (!(cond)) \ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 94 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_ERROR, string); \ |
| 96 | MBEDTLS_MPS_TRACE_RETURN(MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED); \ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 97 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | } while (0) |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 99 | |
| 100 | #else /* MBEDTLS_MPS_STATE_VALIDATION */ |
| 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | #define MBEDTLS_MPS_STATE_VALIDATE_RAW(cond, string) \ |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 103 | do \ |
| 104 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | (cond); \ |
| 106 | } while (0) |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 107 | |
| 108 | #endif /* MBEDTLS_MPS_STATE_VALIDATION */ |
| 109 | |
Hanno Becker | 75ac1f7 | 2021-01-12 07:25:26 +0000 | [diff] [blame] | 110 | #if defined(MBEDTLS_MPS_ENABLE_ASSERTIONS) |
| 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | #define MBEDTLS_MPS_ASSERT_RAW(cond, string) \ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 113 | do \ |
| 114 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | if (!(cond)) \ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 116 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_ERROR, string); \ |
| 118 | MBEDTLS_MPS_TRACE_RETURN(MBEDTLS_ERR_MPS_INTERNAL_ERROR); \ |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 119 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | } while (0) |
Hanno Becker | 75ac1f7 | 2021-01-12 07:25:26 +0000 | [diff] [blame] | 121 | |
| 122 | #else /* MBEDTLS_MPS_ENABLE_ASSERTIONS */ |
| 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | #define MBEDTLS_MPS_ASSERT_RAW(cond, string) do {} while (0) |
Hanno Becker | 75ac1f7 | 2021-01-12 07:25:26 +0000 | [diff] [blame] | 125 | |
| 126 | #endif /* MBEDTLS_MPS_ENABLE_ASSERTIONS */ |
| 127 | |
Hanno Becker | ac267f3 | 2021-01-12 07:25:41 +0000 | [diff] [blame] | 128 | |
Hanno Becker | 6ed183c | 2021-01-12 06:42:16 +0000 | [diff] [blame] | 129 | /* \} name SECTION: MPS Configuration */ |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 130 | |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 131 | /** |
| 132 | * \name SECTION: Common types |
| 133 | * |
| 134 | * Various common types used throughout MPS. |
| 135 | * \{ |
| 136 | */ |
| 137 | |
| 138 | /** \brief The type of buffer sizes and offsets used in MPS structures. |
| 139 | * |
| 140 | * This is an unsigned integer type that should be large enough to |
Hanno Becker | 46101c7 | 2021-02-22 15:11:15 +0000 | [diff] [blame] | 141 | * hold the length of any buffer or message processed by MPS. |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 142 | * |
| 143 | * The reason to pick a value as small as possible here is |
| 144 | * to reduce the size of MPS structures. |
| 145 | * |
| 146 | * \warning Care has to be taken when using a narrower type |
| 147 | * than ::mbedtls_mps_size_t here because of |
| 148 | * potential truncation during conversion. |
| 149 | * |
| 150 | * \warning Handshake messages in TLS may be up to 2^24 ~ 16Mb in size. |
| 151 | * If mbedtls_mps_[opt_]stored_size_t is smaller than that, the |
| 152 | * maximum handshake message is restricted accordingly. |
| 153 | * |
| 154 | * For now, we use the default type of size_t throughout, and the use of |
| 155 | * smaller types or different types for ::mbedtls_mps_size_t and |
| 156 | * ::mbedtls_mps_stored_size_t is not yet supported. |
| 157 | * |
| 158 | */ |
| 159 | typedef size_t mbedtls_mps_stored_size_t; |
Simon | 5205fb4 | 2022-09-05 23:15:07 +0200 | [diff] [blame] | 160 | #define MBEDTLS_MPS_STORED_SIZE_MAX (SIZE_MAX) |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 161 | |
| 162 | /** \brief The type of buffer sizes and offsets used in the MPS API |
| 163 | * and implementation. |
| 164 | * |
| 165 | * This must be at least as wide as ::mbedtls_stored_size_t but |
| 166 | * may be chosen to be strictly larger if more suitable for the |
| 167 | * target architecture. |
| 168 | * |
| 169 | * For example, in a test build for ARM Thumb, using uint_fast16_t |
| 170 | * instead of uint16_t reduced the code size from 1060 Byte to 962 Byte, |
| 171 | * so almost 10%. |
| 172 | */ |
| 173 | typedef size_t mbedtls_mps_size_t; |
Simon | 5205fb4 | 2022-09-05 23:15:07 +0200 | [diff] [blame] | 174 | #define MBEDTLS_MPS_SIZE_MAX (SIZE_MAX) |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 175 | |
Hanno Becker | 4a079c5 | 2021-02-22 15:13:28 +0000 | [diff] [blame] | 176 | #if MBEDTLS_MPS_STORED_SIZE_MAX > MBEDTLS_MPS_SIZE_MAX |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 177 | #error "Misconfiguration of mbedtls_mps_size_t and mbedtls_mps_stored_size_t." |
| 178 | #endif |
| 179 | |
| 180 | /* \} SECTION: Common types */ |
| 181 | |
| 182 | |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 183 | #endif /* MBEDTLS_MPS_COMMON_H */ |