| gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 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 |  | 
| Gabor Mezei | 291df7b | 2021-10-19 11:27:17 +0200 | [diff] [blame] | 20 | #ifndef MBEDTLS_CONSTANT_TIME_INTERNAL_H | 
|  | 21 | #define MBEDTLS_CONSTANT_TIME_INTERNAL_H | 
|  | 22 |  | 
| gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 23 | #include "common.h" | 
|  | 24 |  | 
| gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 25 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 26 | #include "mbedtls/bignum.h" | 
|  | 27 | #endif | 
|  | 28 |  | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_SSL_TLS_C) | 
|  | 30 | #include "ssl_misc.h" | 
|  | 31 | #endif | 
|  | 32 |  | 
| gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 33 | #include <stddef.h> | 
|  | 34 |  | 
| gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 35 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 36 | /** Turn a value into a mask: | 
|  | 37 | * - if \p value == 0, return the all-bits 0 mask, aka 0 | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 38 | * - otherwise, return the all-bits 1 mask, aka (unsigned) -1 | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 39 | * | 
|  | 40 | * This function can be used to write constant-time code by replacing branches | 
|  | 41 | * with bit operations using masks. | 
|  | 42 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 43 | * \param value     The value to analyze. | 
|  | 44 | * | 
|  | 45 | * \return          Zero if \p value is zero, otherwise all-bits-one. | 
|  | 46 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | unsigned mbedtls_ct_uint_mask(unsigned value); | 
| gabor-mezei-arm | 3733bf8 | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 48 |  | 
| Andrzej Kurek | 084334c | 2022-09-27 14:19:50 -0400 | [diff] [blame] | 49 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Gabor Mezei | 6a426c9 | 2021-10-20 11:17:43 +0200 | [diff] [blame] | 50 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 51 | /** Turn a value into a mask: | 
|  | 52 | * - if \p value == 0, return the all-bits 0 mask, aka 0 | 
|  | 53 | * - otherwise, return the all-bits 1 mask, aka (size_t) -1 | 
|  | 54 | * | 
|  | 55 | * This function can be used to write constant-time code by replacing branches | 
|  | 56 | * with bit operations using masks. | 
|  | 57 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 58 | * \param value     The value to analyze. | 
|  | 59 | * | 
|  | 60 | * \return          Zero if \p value is zero, otherwise all-bits-one. | 
|  | 61 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | size_t mbedtls_ct_size_mask(size_t value); | 
| gabor-mezei-arm | c76227d | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 63 |  | 
| Andrzej Kurek | 084334c | 2022-09-27 14:19:50 -0400 | [diff] [blame] | 64 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
| Gabor Mezei | 6a426c9 | 2021-10-20 11:17:43 +0200 | [diff] [blame] | 65 |  | 
| gabor-mezei-arm | 9cb5569 | 2021-08-11 15:07:02 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 67 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 68 | /** Turn a value into a mask: | 
|  | 69 | * - if \p value == 0, return the all-bits 0 mask, aka 0 | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 70 | * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1 | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 71 | * | 
|  | 72 | * This function can be used to write constant-time code by replacing branches | 
|  | 73 | * with bit operations using masks. | 
|  | 74 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 75 | * \param value     The value to analyze. | 
|  | 76 | * | 
|  | 77 | * \return          Zero if \p value is zero, otherwise all-bits-one. | 
|  | 78 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value); | 
| gabor-mezei-arm | 9cb5569 | 2021-08-11 15:07:02 +0200 | [diff] [blame] | 80 |  | 
|  | 81 | #endif /* MBEDTLS_BIGNUM_C */ | 
|  | 82 |  | 
| Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 83 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) | 
|  | 84 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 85 | /** Constant-flow mask generation for "greater or equal" comparison: | 
|  | 86 | * - if \p x >= \p y, return all-bits 1, that is (size_t) -1 | 
|  | 87 | * - otherwise, return all bits 0, that is 0 | 
|  | 88 | * | 
|  | 89 | * This function can be used to write constant-time code by replacing branches | 
|  | 90 | * with bit operations using masks. | 
|  | 91 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 92 | * \param x     The first value to analyze. | 
|  | 93 | * \param y     The second value to analyze. | 
|  | 94 | * | 
|  | 95 | * \return      All-bits-one if \p x is greater or equal than \p y, | 
|  | 96 | *              otherwise zero. | 
|  | 97 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | size_t mbedtls_ct_size_mask_ge(size_t x, | 
|  | 99 | size_t y); | 
| gabor-mezei-arm | 8d1d5fd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 100 |  | 
| Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 101 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ | 
|  | 102 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 103 | /** Constant-flow boolean "equal" comparison: | 
|  | 104 | * return x == y | 
|  | 105 | * | 
|  | 106 | * This is equivalent to \p x == \p y, but is likely to be compiled | 
|  | 107 | * to code using bitwise operation rather than a branch. | 
|  | 108 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 109 | * \param x     The first value to analyze. | 
|  | 110 | * \param y     The second value to analyze. | 
|  | 111 | * | 
|  | 112 | * \return      1 if \p x equals to \p y, otherwise 0. | 
|  | 113 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | unsigned mbedtls_ct_size_bool_eq(size_t x, | 
|  | 115 | size_t y); | 
| gabor-mezei-arm | 5a85442 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 116 |  | 
| gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 118 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 119 | /** Decide if an integer is less than the other, without branches. | 
|  | 120 | * | 
|  | 121 | * This is equivalent to \p x < \p y, but is likely to be compiled | 
|  | 122 | * to code using bitwise operation rather than a branch. | 
|  | 123 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 124 | * \param x     The first value to analyze. | 
|  | 125 | * \param y     The second value to analyze. | 
|  | 126 | * | 
|  | 127 | * \return      1 if \p x is less than \p y, otherwise 0. | 
|  | 128 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x, | 
|  | 130 | const mbedtls_mpi_uint y); | 
| gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 131 |  | 
| Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 132 | /** | 
| Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 133 | * \brief          Check if one unsigned MPI is less than another in constant | 
|  | 134 | *                 time. | 
| Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 135 | * | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 136 | * \param A        The left-hand MPI. This must point to an array of limbs | 
|  | 137 | *                 with the same allocated length as \p B. | 
|  | 138 | * \param B        The right-hand MPI. This must point to an array of limbs | 
|  | 139 | *                 with the same allocated length as \p A. | 
| Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 140 | * \param limbs    The number of limbs in \p A and \p B. | 
| Gilles Peskine | 95b5add | 2022-11-09 11:18:38 +0100 | [diff] [blame] | 141 | *                 This must not be 0. | 
| Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 142 | * | 
|  | 143 | * \return         The result of the comparison: | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 144 | *                 \c 1 if \p A is less than \p B. | 
|  | 145 | *                 \c 0 if \p A is greater than or equal to \p B. | 
| Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 146 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | unsigned mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A, | 
|  | 148 | const mbedtls_mpi_uint *B, | 
|  | 149 | size_t limbs); | 
| gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 150 | #endif /* MBEDTLS_BIGNUM_C */ | 
| gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 151 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 152 | /** Choose between two integer values without branches. | 
|  | 153 | * | 
|  | 154 | * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled | 
|  | 155 | * to code using bitwise operation rather than a branch. | 
|  | 156 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 157 | * \param condition     Condition to test. | 
|  | 158 | * \param if1           Value to use if \p condition is nonzero. | 
|  | 159 | * \param if0           Value to use if \p condition is zero. | 
|  | 160 | * | 
|  | 161 | * \return  \c if1 if \p condition is nonzero, otherwise \c if0. | 
|  | 162 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | unsigned mbedtls_ct_uint_if(unsigned condition, | 
|  | 164 | unsigned if1, | 
|  | 165 | unsigned if0); | 
| gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 166 |  | 
| gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 167 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 168 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 169 | /** Conditionally assign a value without branches. | 
|  | 170 | * | 
|  | 171 | * This is equivalent to `if ( condition ) dest = src`, but is likely | 
|  | 172 | * to be compiled to code using bitwise operation rather than a branch. | 
|  | 173 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 174 | * \param n             \p dest and \p src must be arrays of limbs of size n. | 
|  | 175 | * \param dest          The MPI to conditionally assign to. This must point | 
|  | 176 | *                      to an initialized MPI. | 
|  | 177 | * \param src           The MPI to be assigned from. This must point to an | 
|  | 178 | *                      initialized MPI. | 
|  | 179 | * \param condition     Condition to test, must be 0 or 1. | 
|  | 180 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | void mbedtls_ct_mpi_uint_cond_assign(size_t n, | 
|  | 182 | mbedtls_mpi_uint *dest, | 
|  | 183 | const mbedtls_mpi_uint *src, | 
|  | 184 | unsigned char condition); | 
| gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 185 |  | 
|  | 186 | #endif /* MBEDTLS_BIGNUM_C */ | 
| gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 187 |  | 
| Gabor Mezei | 9a4074a | 2021-11-15 16:18:54 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_BASE64_C) | 
|  | 189 |  | 
| Gabor Mezei | a096975 | 2021-11-24 16:26:33 +0100 | [diff] [blame] | 190 | /** Given a value in the range 0..63, return the corresponding Base64 digit. | 
|  | 191 | * | 
|  | 192 | * The implementation assumes that letters are consecutive (e.g. ASCII | 
|  | 193 | * but not EBCDIC). | 
|  | 194 | * | 
|  | 195 | * \param value     A value in the range 0..63. | 
|  | 196 | * | 
|  | 197 | * \return          A base64 digit converted from \p value. | 
|  | 198 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | unsigned char mbedtls_ct_base64_enc_char(unsigned char value); | 
| Gabor Mezei | 9a4074a | 2021-11-15 16:18:54 +0100 | [diff] [blame] | 200 |  | 
| Gabor Mezei | a096975 | 2021-11-24 16:26:33 +0100 | [diff] [blame] | 201 | /** Given a Base64 digit, return its value. | 
|  | 202 | * | 
|  | 203 | * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'), | 
|  | 204 | * return -1. | 
|  | 205 | * | 
|  | 206 | * The implementation assumes that letters are consecutive (e.g. ASCII | 
|  | 207 | * but not EBCDIC). | 
|  | 208 | * | 
|  | 209 | * \param c     A base64 digit. | 
|  | 210 | * | 
|  | 211 | * \return      The value of the base64 digit \p c. | 
|  | 212 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | signed char mbedtls_ct_base64_dec_value(unsigned char c); | 
| Gabor Mezei | 358829a | 2021-11-15 16:22:37 +0100 | [diff] [blame] | 214 |  | 
| Gabor Mezei | 9a4074a | 2021-11-15 16:18:54 +0100 | [diff] [blame] | 215 | #endif /* MBEDTLS_BASE64_C */ | 
|  | 216 |  | 
| Przemek Stekiel | 89ad623 | 2022-09-27 13:36:12 +0200 | [diff] [blame] | 217 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 218 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 219 | /** Conditional memcpy without branches. | 
|  | 220 | * | 
| Gabor Mezei | 642eeb2 | 2021-11-03 16:13:32 +0100 | [diff] [blame] | 221 | * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 222 | * to be compiled to code using bitwise operation rather than a branch. | 
|  | 223 | * | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 224 | * \param dest      The pointer to conditionally copy to. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 225 | * \param src       The pointer to copy from. Shouldn't overlap with \p dest. | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 226 | * \param len       The number of bytes to copy. | 
|  | 227 | * \param c1        The first value to analyze in the condition. | 
|  | 228 | * \param c2        The second value to analyze in the condition. | 
|  | 229 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | void mbedtls_ct_memcpy_if_eq(unsigned char *dest, | 
|  | 231 | const unsigned char *src, | 
|  | 232 | size_t len, | 
|  | 233 | size_t c1, size_t c2); | 
| gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 234 |  | 
|  | 235 | /** Copy data from a secret position with constant flow. | 
|  | 236 | * | 
|  | 237 | * This function copies \p len bytes from \p src_base + \p offset_secret to \p | 
|  | 238 | * dst, with a code flow and memory access pattern that does not depend on \p | 
|  | 239 | * offset_secret, but only on \p offset_min, \p offset_max and \p len. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 240 | * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`. | 
| gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 241 | * | 
| Paul Elliott | 5260ce2 | 2022-05-09 18:15:54 +0100 | [diff] [blame] | 242 | * \note                This function reads from \p dest, but the value that | 
|  | 243 | *                      is read does not influence the result and this | 
|  | 244 | *                      function's behavior is well-defined regardless of the | 
|  | 245 | *                      contents of the buffers. This may result in false | 
|  | 246 | *                      positives from static or dynamic analyzers, especially | 
|  | 247 | *                      if \p dest is not initialized. | 
|  | 248 | * | 
| Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 249 | * \param dest          The destination buffer. This must point to a writable | 
| gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 250 | *                      buffer of at least \p len bytes. | 
| Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 251 | * \param src           The base of the source buffer. This must point to a | 
| gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 252 | *                      readable buffer of at least \p offset_max + \p len | 
| Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 253 | *                      bytes. Shouldn't overlap with \p dest. | 
|  | 254 | * \param offset        The offset in the source buffer from which to copy. | 
| gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 255 | *                      This must be no less than \p offset_min and no greater | 
|  | 256 | *                      than \p offset_max. | 
| Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 257 | * \param offset_min    The minimal value of \p offset. | 
|  | 258 | * \param offset_max    The maximal value of \p offset. | 
| gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 259 | * \param len           The number of bytes to copy. | 
|  | 260 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | void mbedtls_ct_memcpy_offset(unsigned char *dest, | 
|  | 262 | const unsigned char *src, | 
|  | 263 | size_t offset, | 
|  | 264 | size_t offset_min, | 
|  | 265 | size_t offset_max, | 
|  | 266 | size_t len); | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 267 |  | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 268 | /** Compute the HMAC of variable-length data with constant flow. | 
|  | 269 | * | 
|  | 270 | * This function computes the HMAC of the concatenation of \p add_data and \p | 
|  | 271 | * data, and does with a code flow and memory access pattern that does not | 
|  | 272 | * depend on \p data_len_secret, but only on \p min_data_len and \p | 
|  | 273 | * max_data_len. In particular, this function always reads exactly \p | 
|  | 274 | * max_data_len bytes from \p data. | 
|  | 275 | * | 
|  | 276 | * \param ctx               The HMAC context. It must have keys configured | 
|  | 277 | *                          with mbedtls_md_hmac_starts() and use one of the | 
|  | 278 | *                          following hashes: SHA-384, SHA-256, SHA-1 or MD-5. | 
|  | 279 | *                          It is reset using mbedtls_md_hmac_reset() after | 
|  | 280 | *                          the computation is complete to prepare for the | 
|  | 281 | *                          next computation. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 282 | * \param add_data          The first part of the message whose HMAC is being | 
|  | 283 | *                          calculated. This must point to a readable buffer | 
|  | 284 | *                          of \p add_data_len bytes. | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 285 | * \param add_data_len      The length of \p add_data in bytes. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 286 | * \param data              The buffer containing the second part of the | 
|  | 287 | *                          message. This must point to a readable buffer | 
|  | 288 | *                          of \p max_data_len bytes. | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 289 | * \param data_len_secret   The length of the data to process in \p data. | 
|  | 290 | *                          This must be no less than \p min_data_len and no | 
|  | 291 | *                          greater than \p max_data_len. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 292 | * \param min_data_len      The minimal length of the second part of the | 
| Gabor Mezei | 116cd6a | 2021-10-20 11:18:37 +0200 | [diff] [blame] | 293 | *                          message, read from \p data. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 294 | * \param max_data_len      The maximal length of the second part of the | 
| Gabor Mezei | 116cd6a | 2021-10-20 11:18:37 +0200 | [diff] [blame] | 295 | *                          message, read from \p data. | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 296 | * \param output            The HMAC will be written here. This must point to | 
|  | 297 | *                          a writable buffer of sufficient size to hold the | 
|  | 298 | *                          HMAC value. | 
|  | 299 | * | 
|  | 300 | * \retval 0 on success. | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 301 | * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 302 | *         The hardware accelerator failed. | 
|  | 303 | */ | 
| Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 304 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | int mbedtls_ct_hmac(mbedtls_svc_key_id_t key, | 
|  | 306 | psa_algorithm_t alg, | 
|  | 307 | const unsigned char *add_data, | 
|  | 308 | size_t add_data_len, | 
|  | 309 | const unsigned char *data, | 
|  | 310 | size_t data_len_secret, | 
|  | 311 | size_t min_data_len, | 
|  | 312 | size_t max_data_len, | 
|  | 313 | unsigned char *output); | 
| Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 314 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | int mbedtls_ct_hmac(mbedtls_md_context_t *ctx, | 
|  | 316 | const unsigned char *add_data, | 
|  | 317 | size_t add_data_len, | 
|  | 318 | const unsigned char *data, | 
|  | 319 | size_t data_len_secret, | 
|  | 320 | size_t min_data_len, | 
|  | 321 | size_t max_data_len, | 
|  | 322 | unsigned char *output); | 
| Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 323 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 324 |  | 
| Przemek Stekiel | 89ad623 | 2022-09-27 13:36:12 +0200 | [diff] [blame] | 325 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
| gabor-mezei-arm | fdb7118 | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 326 |  | 
|  | 327 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) | 
|  | 328 |  | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 329 | /** This function performs the unpadding part of a PKCS#1 v1.5 decryption | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 330 | *  operation (EME-PKCS1-v1_5 decoding). | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 331 | * | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 332 | * \note The return value from this function is a sensitive value | 
|  | 333 | *       (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen | 
|  | 334 | *       in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING | 
|  | 335 | *       is often a situation that an attacker can provoke and leaking which | 
|  | 336 | *       one is the result is precisely the information the attacker wants. | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 337 | * | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 338 | * \param input          The input buffer which is the payload inside PKCS#1v1.5 | 
|  | 339 | *                       encryption padding, called the "encoded message EM" | 
|  | 340 | *                       by the terminology. | 
|  | 341 | * \param ilen           The length of the payload in the \p input buffer. | 
|  | 342 | * \param output         The buffer for the payload, called "message M" by the | 
|  | 343 | *                       PKCS#1 terminology. This must be a writable buffer of | 
|  | 344 | *                       length \p output_max_len bytes. | 
| Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 345 | * \param olen           The address at which to store the length of | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 346 | *                       the payload. This must not be \c NULL. | 
|  | 347 | * \param output_max_len The length in bytes of the output buffer \p output. | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 348 | * | 
| Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 349 | * \return      \c 0 on success. | 
|  | 350 | * \return      #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE | 
|  | 351 | *              The output buffer is too small for the unpadded payload. | 
|  | 352 | * \return      #MBEDTLS_ERR_RSA_INVALID_PADDING | 
|  | 353 | *              The input doesn't contain properly formatted padding. | 
| gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 354 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input, | 
|  | 356 | size_t ilen, | 
|  | 357 | unsigned char *output, | 
|  | 358 | size_t output_max_len, | 
|  | 359 | size_t *olen); | 
| gabor-mezei-arm | fdb7118 | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 360 |  | 
|  | 361 | #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */ | 
| Gabor Mezei | 291df7b | 2021-10-19 11:27:17 +0200 | [diff] [blame] | 362 |  | 
|  | 363 | #endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */ |