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