mps_common.h 7.8 KB

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