constant_time_internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Constant-time functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifndef MBEDTLS_CONSTANT_TIME_INTERNAL_H
  20. #define MBEDTLS_CONSTANT_TIME_INTERNAL_H
  21. #include "common.h"
  22. #if defined(MBEDTLS_BIGNUM_C)
  23. #include "mbedtls/bignum.h"
  24. #endif
  25. #if defined(MBEDTLS_SSL_TLS_C)
  26. #include "ssl_misc.h"
  27. #endif
  28. #include <stddef.h>
  29. /** Turn a value into a mask:
  30. * - if \p value == 0, return the all-bits 0 mask, aka 0
  31. * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
  32. *
  33. * This function can be used to write constant-time code by replacing branches
  34. * with bit operations using masks.
  35. *
  36. * \param value The value to analyze.
  37. *
  38. * \return Zero if \p value is zero, otherwise all-bits-one.
  39. */
  40. unsigned mbedtls_ct_uint_mask( unsigned value );
  41. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
  42. /** Turn a value into a mask:
  43. * - if \p value == 0, return the all-bits 0 mask, aka 0
  44. * - otherwise, return the all-bits 1 mask, aka (size_t) -1
  45. *
  46. * This function can be used to write constant-time code by replacing branches
  47. * with bit operations using masks.
  48. *
  49. * \param value The value to analyze.
  50. *
  51. * \return Zero if \p value is zero, otherwise all-bits-one.
  52. */
  53. size_t mbedtls_ct_size_mask( size_t value );
  54. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
  55. #if defined(MBEDTLS_BIGNUM_C)
  56. /** Turn a value into a mask:
  57. * - if \p value == 0, return the all-bits 0 mask, aka 0
  58. * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
  59. *
  60. * This function can be used to write constant-time code by replacing branches
  61. * with bit operations using masks.
  62. *
  63. * \param value The value to analyze.
  64. *
  65. * \return Zero if \p value is zero, otherwise all-bits-one.
  66. */
  67. mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask( mbedtls_mpi_uint value );
  68. #endif /* MBEDTLS_BIGNUM_C */
  69. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
  70. /** Constant-flow mask generation for "greater or equal" comparison:
  71. * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
  72. * - otherwise, return all bits 0, that is 0
  73. *
  74. * This function can be used to write constant-time code by replacing branches
  75. * with bit operations using masks.
  76. *
  77. * \param x The first value to analyze.
  78. * \param y The second value to analyze.
  79. *
  80. * \return All-bits-one if \p x is greater or equal than \p y,
  81. * otherwise zero.
  82. */
  83. size_t mbedtls_ct_size_mask_ge( size_t x,
  84. size_t y );
  85. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
  86. /** Constant-flow boolean "equal" comparison:
  87. * return x == y
  88. *
  89. * This is equivalent to \p x == \p y, but is likely to be compiled
  90. * to code using bitwise operation rather than a branch.
  91. *
  92. * \param x The first value to analyze.
  93. * \param y The second value to analyze.
  94. *
  95. * \return 1 if \p x equals to \p y, otherwise 0.
  96. */
  97. unsigned mbedtls_ct_size_bool_eq( size_t x,
  98. size_t y );
  99. #if defined(MBEDTLS_BIGNUM_C)
  100. /** Decide if an integer is less than the other, without branches.
  101. *
  102. * This is equivalent to \p x < \p y, but is likely to be compiled
  103. * to code using bitwise operation rather than a branch.
  104. *
  105. * \param x The first value to analyze.
  106. * \param y The second value to analyze.
  107. *
  108. * \return 1 if \p x is less than \p y, otherwise 0.
  109. */
  110. unsigned mbedtls_ct_mpi_uint_lt( const mbedtls_mpi_uint x,
  111. const mbedtls_mpi_uint y );
  112. #endif /* MBEDTLS_BIGNUM_C */
  113. /** Choose between two integer values without branches.
  114. *
  115. * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
  116. * to code using bitwise operation rather than a branch.
  117. *
  118. * \param condition Condition to test.
  119. * \param if1 Value to use if \p condition is nonzero.
  120. * \param if0 Value to use if \p condition is zero.
  121. *
  122. * \return \c if1 if \p condition is nonzero, otherwise \c if0.
  123. */
  124. unsigned mbedtls_ct_uint_if( unsigned condition,
  125. unsigned if1,
  126. unsigned if0 );
  127. #if defined(MBEDTLS_BIGNUM_C)
  128. /** Conditionally assign a value without branches.
  129. *
  130. * This is equivalent to `if ( condition ) dest = src`, but is likely
  131. * to be compiled to code using bitwise operation rather than a branch.
  132. *
  133. * \param n \p dest and \p src must be arrays of limbs of size n.
  134. * \param dest The MPI to conditionally assign to. This must point
  135. * to an initialized MPI.
  136. * \param src The MPI to be assigned from. This must point to an
  137. * initialized MPI.
  138. * \param condition Condition to test, must be 0 or 1.
  139. */
  140. void mbedtls_ct_mpi_uint_cond_assign( size_t n,
  141. mbedtls_mpi_uint *dest,
  142. const mbedtls_mpi_uint *src,
  143. unsigned char condition );
  144. #endif /* MBEDTLS_BIGNUM_C */
  145. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
  146. /** Conditional memcpy without branches.
  147. *
  148. * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
  149. * to be compiled to code using bitwise operation rather than a branch.
  150. *
  151. * \param dest The pointer to conditionally copy to.
  152. * \param src The pointer to copy from. Shouldn't overlap with \p dest.
  153. * \param len The number of bytes to copy.
  154. * \param c1 The first value to analyze in the condition.
  155. * \param c2 The second value to analyze in the condition.
  156. */
  157. void mbedtls_ct_memcpy_if_eq( unsigned char *dest,
  158. const unsigned char *src,
  159. size_t len,
  160. size_t c1, size_t c2 );
  161. /** Copy data from a secret position with constant flow.
  162. *
  163. * This function copies \p len bytes from \p src_base + \p offset_secret to \p
  164. * dst, with a code flow and memory access pattern that does not depend on \p
  165. * offset_secret, but only on \p offset_min, \p offset_max and \p len.
  166. * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
  167. *
  168. * \param dest The destination buffer. This must point to a writable
  169. * buffer of at least \p len bytes.
  170. * \param src The base of the source buffer. This must point to a
  171. * readable buffer of at least \p offset_max + \p len
  172. * bytes. Shouldn't overlap with \p dest.
  173. * \param offset The offset in the source buffer from which to copy.
  174. * This must be no less than \p offset_min and no greater
  175. * than \p offset_max.
  176. * \param offset_min The minimal value of \p offset.
  177. * \param offset_max The maximal value of \p offset.
  178. * \param len The number of bytes to copy.
  179. */
  180. void mbedtls_ct_memcpy_offset( unsigned char *dest,
  181. const unsigned char *src,
  182. size_t offset,
  183. size_t offset_min,
  184. size_t offset_max,
  185. size_t len );
  186. /** Compute the HMAC of variable-length data with constant flow.
  187. *
  188. * This function computes the HMAC of the concatenation of \p add_data and \p
  189. * data, and does with a code flow and memory access pattern that does not
  190. * depend on \p data_len_secret, but only on \p min_data_len and \p
  191. * max_data_len. In particular, this function always reads exactly \p
  192. * max_data_len bytes from \p data.
  193. *
  194. * \param ctx The HMAC context. It must have keys configured
  195. * with mbedtls_md_hmac_starts() and use one of the
  196. * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
  197. * It is reset using mbedtls_md_hmac_reset() after
  198. * the computation is complete to prepare for the
  199. * next computation.
  200. * \param add_data The first part of the message whose HMAC is being
  201. * calculated. This must point to a readable buffer
  202. * of \p add_data_len bytes.
  203. * \param add_data_len The length of \p add_data in bytes.
  204. * \param data The buffer containing the second part of the
  205. * message. This must point to a readable buffer
  206. * of \p max_data_len bytes.
  207. * \param data_len_secret The length of the data to process in \p data.
  208. * This must be no less than \p min_data_len and no
  209. * greater than \p max_data_len.
  210. * \param min_data_len The minimal length of the second part of the
  211. * message, read from \p data.
  212. * \param max_data_len The maximal length of the second part of the
  213. * message, read from \p data.
  214. * \param output The HMAC will be written here. This must point to
  215. * a writable buffer of sufficient size to hold the
  216. * HMAC value.
  217. *
  218. * \retval 0 on success.
  219. * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
  220. * The hardware accelerator failed.
  221. */
  222. int mbedtls_ct_hmac( mbedtls_md_context_t *ctx,
  223. const unsigned char *add_data,
  224. size_t add_data_len,
  225. const unsigned char *data,
  226. size_t data_len_secret,
  227. size_t min_data_len,
  228. size_t max_data_len,
  229. unsigned char *output );
  230. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
  231. #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
  232. /** This function performs the unpadding part of a PKCS#1 v1.5 decryption
  233. * operation (EME-PKCS1-v1_5 decoding).
  234. *
  235. * \note The return value from this function is a sensitive value
  236. * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
  237. * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
  238. * is often a situation that an attacker can provoke and leaking which
  239. * one is the result is precisely the information the attacker wants.
  240. *
  241. * \param input The input buffer which is the payload inside PKCS#1v1.5
  242. * encryption padding, called the "encoded message EM"
  243. * by the terminology.
  244. * \param ilen The length of the payload in the \p input buffer.
  245. * \param output The buffer for the payload, called "message M" by the
  246. * PKCS#1 terminology. This must be a writable buffer of
  247. * length \p output_max_len bytes.
  248. * \param olen The address at which to store the length of
  249. * the payload. This must not be \c NULL.
  250. * \param output_max_len The length in bytes of the output buffer \p output.
  251. *
  252. * \return \c 0 on success.
  253. * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
  254. * The output buffer is too small for the unpadded payload.
  255. * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
  256. * The input doesn't contain properly formatted padding.
  257. */
  258. int mbedtls_ct_rsaes_pkcs1_v15_unpadding( unsigned char *input,
  259. size_t ilen,
  260. unsigned char *output,
  261. size_t output_max_len,
  262. size_t *olen );
  263. #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
  264. #endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */