Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright The Mbed TLS Contributors |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * \file common.h |
| 22 | * |
| 23 | * \brief Common functions and macros used by MPS |
| 24 | */ |
| 25 | |
| 26 | #ifndef MBEDTLS_MPS_COMMON_H |
| 27 | #define MBEDTLS_MPS_COMMON_H |
| 28 | |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | |
Hanno Becker | 6ed183c | 2021-01-12 06:42:16 +0000 | [diff] [blame] | 31 | /** |
| 32 | * \name SECTION: MPS Configuration |
| 33 | * |
| 34 | * \{ |
| 35 | */ |
| 36 | |
| 37 | /*! This flag enables/disables assertions on the internal state of MPS. |
| 38 | * |
| 39 | * Assertions are sanity checks that should never trigger when MPS |
| 40 | * is used within the bounds of its API and preconditions. |
| 41 | * |
| 42 | * Enabling this increases security by limiting the scope of |
| 43 | * potential bugs, but comes at the cost of increased code size. |
| 44 | * |
| 45 | * Note: So far, there is no guiding principle as to what |
| 46 | * expected conditions merit an assertion, and which don't. |
| 47 | * |
| 48 | * Comment this to disable assertions. |
| 49 | */ |
| 50 | #define MBEDTLS_MPS_ENABLE_ASSERTIONS |
| 51 | |
Hanno Becker | 1ae9f75 | 2021-01-12 06:43:17 +0000 | [diff] [blame] | 52 | /*! This flag controls whether tracing for MPS should be enabled. */ |
Hanno Becker | c809ff6 | 2021-01-12 06:54:04 +0000 | [diff] [blame] | 53 | //#define MBEDTLS_MPS_TRACE |
Hanno Becker | 1ae9f75 | 2021-01-12 06:43:17 +0000 | [diff] [blame] | 54 | |
Hanno Becker | 75ac1f7 | 2021-01-12 07:25:26 +0000 | [diff] [blame] | 55 | #if defined(MBEDTLS_MPS_ENABLE_ASSERTIONS) |
| 56 | |
| 57 | #define MBEDTLS_MPS_ASSERT_RAW( cond, string ) \ |
| 58 | do \ |
| 59 | { \ |
| 60 | if( !(cond) ) \ |
| 61 | { \ |
| 62 | TRACE( trace_error, string ); \ |
| 63 | RETURN( MBEDTLS_ERR_MPS_INTERNAL_ERROR ); \ |
| 64 | } \ |
| 65 | } while( 0 ) |
| 66 | |
| 67 | #else /* MBEDTLS_MPS_ENABLE_ASSERTIONS */ |
| 68 | |
| 69 | #define MBEDTLS_MPS_ASSERT_RAW( cond, string ) do {} while( 0 ) |
| 70 | |
| 71 | #endif /* MBEDTLS_MPS_ENABLE_ASSERTIONS */ |
| 72 | |
Hanno Becker | 6ed183c | 2021-01-12 06:42:16 +0000 | [diff] [blame] | 73 | /* \} name SECTION: MPS Configuration */ |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 74 | |
Hanno Becker | d2f9f53 | 2021-01-12 07:11:11 +0000 | [diff] [blame] | 75 | /** |
| 76 | * \name SECTION: Common types |
| 77 | * |
| 78 | * Various common types used throughout MPS. |
| 79 | * \{ |
| 80 | */ |
| 81 | |
| 82 | /** \brief The type of buffer sizes and offsets used in MPS structures. |
| 83 | * |
| 84 | * This is an unsigned integer type that should be large enough to |
| 85 | * hold the length of any buffer resp. message processed by MPS. |
| 86 | * |
| 87 | * The reason to pick a value as small as possible here is |
| 88 | * to reduce the size of MPS structures. |
| 89 | * |
| 90 | * \warning Care has to be taken when using a narrower type |
| 91 | * than ::mbedtls_mps_size_t here because of |
| 92 | * potential truncation during conversion. |
| 93 | * |
| 94 | * \warning Handshake messages in TLS may be up to 2^24 ~ 16Mb in size. |
| 95 | * If mbedtls_mps_[opt_]stored_size_t is smaller than that, the |
| 96 | * maximum handshake message is restricted accordingly. |
| 97 | * |
| 98 | * For now, we use the default type of size_t throughout, and the use of |
| 99 | * smaller types or different types for ::mbedtls_mps_size_t and |
| 100 | * ::mbedtls_mps_stored_size_t is not yet supported. |
| 101 | * |
| 102 | */ |
| 103 | typedef size_t mbedtls_mps_stored_size_t; |
| 104 | #define MBEDTLS_MPS_SIZE_MAX ( (mbedtls_mps_size_t) -1 ) |
| 105 | |
| 106 | /** \brief The type of buffer sizes and offsets used in the MPS API |
| 107 | * and implementation. |
| 108 | * |
| 109 | * This must be at least as wide as ::mbedtls_stored_size_t but |
| 110 | * may be chosen to be strictly larger if more suitable for the |
| 111 | * target architecture. |
| 112 | * |
| 113 | * For example, in a test build for ARM Thumb, using uint_fast16_t |
| 114 | * instead of uint16_t reduced the code size from 1060 Byte to 962 Byte, |
| 115 | * so almost 10%. |
| 116 | */ |
| 117 | typedef size_t mbedtls_mps_size_t; |
| 118 | |
| 119 | #if (mbedtls_mps_size_t) -1 > (mbedtls_mps_stored_size_t) -1 |
| 120 | #error "Misconfiguration of mbedtls_mps_size_t and mbedtls_mps_stored_size_t." |
| 121 | #endif |
| 122 | |
| 123 | /* \} SECTION: Common types */ |
| 124 | |
| 125 | |
Hanno Becker | 108fc84 | 2021-01-12 06:39:43 +0000 | [diff] [blame] | 126 | #endif /* MBEDTLS_MPS_COMMON_H */ |