constant_time_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "mbedtls/ssl_internal.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_BASE64_C)
  146. /** Given a value in the range 0..63, return the corresponding Base64 digit.
  147. *
  148. * The implementation assumes that letters are consecutive (e.g. ASCII
  149. * but not EBCDIC).
  150. *
  151. * \param value A value in the range 0..63.
  152. *
  153. * \return A base64 digit converted from \p value.
  154. */
  155. unsigned char mbedtls_ct_base64_enc_char( unsigned char value );
  156. /** Given a Base64 digit, return its value.
  157. *
  158. * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'),
  159. * return -1.
  160. *
  161. * The implementation assumes that letters are consecutive (e.g. ASCII
  162. * but not EBCDIC).
  163. *
  164. * \param c A base64 digit.
  165. *
  166. * \return The value of the base64 digit \p c.
  167. */
  168. signed char mbedtls_ct_base64_dec_value( unsigned char c );
  169. #endif /* MBEDTLS_BASE64_C */
  170. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
  171. /** Conditional memcpy without branches.
  172. *
  173. * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
  174. * to be compiled to code using bitwise operation rather than a branch.
  175. *
  176. * \param dest The pointer to conditionally copy to.
  177. * \param src The pointer to copy from. Shouldn't overlap with \p dest.
  178. * \param len The number of bytes to copy.
  179. * \param c1 The first value to analyze in the condition.
  180. * \param c2 The second value to analyze in the condition.
  181. */
  182. void mbedtls_ct_memcpy_if_eq( unsigned char *dest,
  183. const unsigned char *src,
  184. size_t len,
  185. size_t c1, size_t c2 );
  186. /** Copy data from a secret position with constant flow.
  187. *
  188. * This function copies \p len bytes from \p src_base + \p offset_secret to \p
  189. * dst, with a code flow and memory access pattern that does not depend on \p
  190. * offset_secret, but only on \p offset_min, \p offset_max and \p len.
  191. * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
  192. *
  193. * \param dest The destination buffer. This must point to a writable
  194. * buffer of at least \p len bytes.
  195. * \param src The base of the source buffer. This must point to a
  196. * readable buffer of at least \p offset_max + \p len
  197. * bytes. Shouldn't overlap with \p dest.
  198. * \param offset The offset in the source buffer from which to copy.
  199. * This must be no less than \p offset_min and no greater
  200. * than \p offset_max.
  201. * \param offset_min The minimal value of \p offset.
  202. * \param offset_max The maximal value of \p offset.
  203. * \param len The number of bytes to copy.
  204. */
  205. void mbedtls_ct_memcpy_offset( unsigned char *dest,
  206. const unsigned char *src,
  207. size_t offset,
  208. size_t offset_min,
  209. size_t offset_max,
  210. size_t len );
  211. /** Compute the HMAC of variable-length data with constant flow.
  212. *
  213. * This function computes the HMAC of the concatenation of \p add_data and \p
  214. * data, and does with a code flow and memory access pattern that does not
  215. * depend on \p data_len_secret, but only on \p min_data_len and \p
  216. * max_data_len. In particular, this function always reads exactly \p
  217. * max_data_len bytes from \p data.
  218. *
  219. * \param ctx The HMAC context. It must have keys configured
  220. * with mbedtls_md_hmac_starts() and use one of the
  221. * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
  222. * It is reset using mbedtls_md_hmac_reset() after
  223. * the computation is complete to prepare for the
  224. * next computation.
  225. * \param add_data The first part of the message whose HMAC is being
  226. * calculated. This must point to a readable buffer
  227. * of \p add_data_len bytes.
  228. * \param add_data_len The length of \p add_data in bytes.
  229. * \param data The buffer containing the second part of the
  230. * message. This must point to a readable buffer
  231. * of \p max_data_len bytes.
  232. * \param data_len_secret The length of the data to process in \p data.
  233. * This must be no less than \p min_data_len and no
  234. * greater than \p max_data_len.
  235. * \param min_data_len The minimal length of the second part of the
  236. * message, read from \p data.
  237. * \param max_data_len The maximal length of the second part of the
  238. * message, read from \p data.
  239. * \param output The HMAC will be written here. This must point to
  240. * a writable buffer of sufficient size to hold the
  241. * HMAC value.
  242. *
  243. * \retval 0 on success.
  244. * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
  245. * The hardware accelerator failed.
  246. */
  247. int mbedtls_ct_hmac( mbedtls_md_context_t *ctx,
  248. const unsigned char *add_data,
  249. size_t add_data_len,
  250. const unsigned char *data,
  251. size_t data_len_secret,
  252. size_t min_data_len,
  253. size_t max_data_len,
  254. unsigned char *output );
  255. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
  256. #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
  257. /** This function performs the unpadding part of a PKCS#1 v1.5 decryption
  258. * operation (EME-PKCS1-v1_5 decoding).
  259. *
  260. * \note The return value from this function is a sensitive value
  261. * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
  262. * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
  263. * is often a situation that an attacker can provoke and leaking which
  264. * one is the result is precisely the information the attacker wants.
  265. *
  266. * \param mode The mode of operation. This must be either
  267. * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
  268. * \param input The input buffer which is the payload inside PKCS#1v1.5
  269. * encryption padding, called the "encoded message EM"
  270. * by the terminology.
  271. * \param ilen The length of the payload in the \p input buffer.
  272. * \param output The buffer for the payload, called "message M" by the
  273. * PKCS#1 terminology. This must be a writable buffer of
  274. * length \p output_max_len bytes.
  275. * \param olen The address at which to store the length of
  276. * the payload. This must not be \c NULL.
  277. * \param output_max_len The length in bytes of the output buffer \p output.
  278. *
  279. * \return \c 0 on success.
  280. * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
  281. * The output buffer is too small for the unpadded payload.
  282. * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
  283. * The input doesn't contain properly formatted padding.
  284. */
  285. int mbedtls_ct_rsaes_pkcs1_v15_unpadding( int mode,
  286. unsigned char *input,
  287. size_t ilen,
  288. unsigned char *output,
  289. size_t output_max_len,
  290. size_t *olen );
  291. #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
  292. #endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */