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