gabor-mezei-arm | 9055972 | 2021-07-12 16:31:22 +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 | |
| 20 | #include "common.h" |
gabor-mezei-arm | 944c107 | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 21 | #include "constant_time.h" |
gabor-mezei-arm | cb4317b | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 22 | #include "mbedtls/error.h" |
gabor-mezei-arm | 944c107 | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 23 | |
gabor-mezei-arm | 097d4f5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_BIGNUM_C) |
| 25 | #include "mbedtls/bignum.h" |
| 26 | #endif |
| 27 | |
gabor-mezei-arm | cb4317b | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_SSL_TLS_C) |
| 29 | #include "mbedtls/ssl_internal.h" |
| 30 | #endif |
| 31 | |
gabor-mezei-arm | f52941e | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 32 | #include <string.h> |
gabor-mezei-arm | 097d4f5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 33 | |
gabor-mezei-arm | 378e7eb | 2021-07-19 15:19:19 +0200 | [diff] [blame^] | 34 | int mbedtls_cf_memcmp( const void *a, |
| 35 | const void *b, |
| 36 | size_t n ) |
gabor-mezei-arm | 944c107 | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 37 | { |
| 38 | size_t i; |
| 39 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 40 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
| 41 | volatile unsigned char diff = 0; |
| 42 | |
| 43 | for( i = 0; i < n; i++ ) |
| 44 | { |
| 45 | /* Read volatile data in order before computing diff. |
| 46 | * This avoids IAR compiler warning: |
| 47 | * 'the order of volatile accesses is undefined ..' */ |
| 48 | unsigned char x = A[i], y = B[i]; |
| 49 | diff |= x ^ y; |
| 50 | } |
| 51 | |
gabor-mezei-arm | 944c107 | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 52 | return( (int)diff ); |
| 53 | } |
| 54 | |
gabor-mezei-arm | c11cac9 | 2021-09-27 11:40:03 +0200 | [diff] [blame] | 55 | /** Turn zero-or-nonzero into zero-or-all-bits-one, without branches. |
| 56 | * |
| 57 | * \param value The value to analyze. |
| 58 | * \return Zero if \p value is zero, otherwise all-bits-one. |
| 59 | */ |
| 60 | unsigned mbedtls_cf_uint_mask( unsigned value ) |
| 61 | { |
| 62 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 63 | * well-defined and precisely what we want to do here */ |
| 64 | #if defined(_MSC_VER) |
| 65 | #pragma warning( push ) |
| 66 | #pragma warning( disable : 4146 ) |
| 67 | #endif |
| 68 | return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 69 | #if defined(_MSC_VER) |
| 70 | #pragma warning( pop ) |
| 71 | #endif |
| 72 | } |
gabor-mezei-arm | d361ccd | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Turn a bit into a mask: |
| 76 | * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1 |
| 77 | * - if bit == 0, return the all-bits 0 mask, aka 0 |
| 78 | * |
| 79 | * This function can be used to write constant-time code by replacing branches |
| 80 | * with bit operations using masks. |
| 81 | * |
| 82 | * This function is implemented without using comparison operators, as those |
| 83 | * might be translated to branches by some compilers on some platforms. |
| 84 | */ |
| 85 | size_t mbedtls_cf_size_mask( size_t bit ) |
| 86 | { |
| 87 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 88 | * but this is well-defined and precisely what we want to do here. */ |
| 89 | #if defined(_MSC_VER) |
| 90 | #pragma warning( push ) |
| 91 | #pragma warning( disable : 4146 ) |
| 92 | #endif |
| 93 | return -bit; |
| 94 | #if defined(_MSC_VER) |
| 95 | #pragma warning( pop ) |
| 96 | #endif |
| 97 | } |
gabor-mezei-arm | 4d6b146 | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Constant-flow mask generation for "less than" comparison: |
| 101 | * - if x < y, return all bits 1, that is (size_t) -1 |
| 102 | * - otherwise, return all bits 0, that is 0 |
| 103 | * |
| 104 | * This function can be used to write constant-time code by replacing branches |
| 105 | * with bit operations using masks. |
| 106 | * |
| 107 | * This function is implemented without using comparison operators, as those |
| 108 | * might be translated to branches by some compilers on some platforms. |
| 109 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 110 | size_t mbedtls_cf_size_mask_lt( size_t x, |
| 111 | size_t y ) |
gabor-mezei-arm | 4d6b146 | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 112 | { |
| 113 | /* This has the most significant bit set if and only if x < y */ |
| 114 | const size_t sub = x - y; |
| 115 | |
| 116 | /* sub1 = (x < y) ? 1 : 0 */ |
| 117 | const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 ); |
| 118 | |
| 119 | /* mask = (x < y) ? 0xff... : 0x00... */ |
| 120 | const size_t mask = mbedtls_cf_size_mask( sub1 ); |
| 121 | |
| 122 | return( mask ); |
| 123 | } |
gabor-mezei-arm | a2bcabc | 2021-09-27 11:58:31 +0200 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Constant-flow mask generation for "greater or equal" comparison: |
| 127 | * - if x >= y, return all bits 1, that is (size_t) -1 |
| 128 | * - otherwise, return all bits 0, that is 0 |
| 129 | * |
| 130 | * This function can be used to write constant-time code by replacing branches |
| 131 | * with bit operations using masks. |
| 132 | * |
| 133 | * This function is implemented without using comparison operators, as those |
| 134 | * might be translated to branches by some compilers on some platforms. |
| 135 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 136 | size_t mbedtls_cf_size_mask_ge( size_t x, |
| 137 | size_t y ) |
gabor-mezei-arm | a2bcabc | 2021-09-27 11:58:31 +0200 | [diff] [blame] | 138 | { |
| 139 | return( ~mbedtls_cf_size_mask_lt( x, y ) ); |
| 140 | } |
gabor-mezei-arm | 96584dd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Constant-flow boolean "equal" comparison: |
| 144 | * return x == y |
| 145 | * |
| 146 | * This function can be used to write constant-time code by replacing branches |
| 147 | * with bit operations - it can be used in conjunction with |
| 148 | * mbedtls_ssl_cf_mask_from_bit(). |
| 149 | * |
| 150 | * This function is implemented without using comparison operators, as those |
| 151 | * might be translated to branches by some compilers on some platforms. |
| 152 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 153 | size_t mbedtls_cf_size_bool_eq( size_t x, |
| 154 | size_t y ) |
gabor-mezei-arm | 96584dd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 155 | { |
| 156 | /* diff = 0 if x == y, non-zero otherwise */ |
| 157 | const size_t diff = x ^ y; |
| 158 | |
| 159 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 160 | * but this is well-defined and precisely what we want to do here. */ |
| 161 | #if defined(_MSC_VER) |
| 162 | #pragma warning( push ) |
| 163 | #pragma warning( disable : 4146 ) |
| 164 | #endif |
| 165 | |
| 166 | /* diff_msb's most significant bit is equal to x != y */ |
| 167 | const size_t diff_msb = ( diff | (size_t) -diff ); |
| 168 | |
| 169 | #if defined(_MSC_VER) |
| 170 | #pragma warning( pop ) |
| 171 | #endif |
| 172 | |
| 173 | /* diff1 = (x != y) ? 1 : 0 */ |
| 174 | const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 ); |
| 175 | |
| 176 | return( 1 ^ diff1 ); |
| 177 | } |
gabor-mezei-arm | 9d7bf09 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 178 | |
| 179 | /** Check whether a size is out of bounds, without branches. |
| 180 | * |
| 181 | * This is equivalent to `size > max`, but is likely to be compiled to |
| 182 | * to code using bitwise operation rather than a branch. |
| 183 | * |
| 184 | * \param size Size to check. |
| 185 | * \param max Maximum desired value for \p size. |
| 186 | * \return \c 0 if `size <= max`. |
| 187 | * \return \c 1 if `size > max`. |
| 188 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 189 | unsigned mbedtls_cf_size_gt( size_t size, |
| 190 | size_t max ) |
gabor-mezei-arm | 9d7bf09 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 191 | { |
| 192 | /* Return the sign bit (1 for negative) of (max - size). */ |
| 193 | return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) ); |
| 194 | } |
gabor-mezei-arm | 097d4f5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 195 | |
| 196 | #if defined(MBEDTLS_BIGNUM_C) |
| 197 | |
| 198 | /** Decide if an integer is less than the other, without branches. |
| 199 | * |
| 200 | * \param x First integer. |
| 201 | * \param y Second integer. |
| 202 | * |
| 203 | * \return 1 if \p x is less than \p y, 0 otherwise |
| 204 | */ |
| 205 | unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x, |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 206 | const mbedtls_mpi_uint y ) |
gabor-mezei-arm | 097d4f5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 207 | { |
| 208 | mbedtls_mpi_uint ret; |
| 209 | mbedtls_mpi_uint cond; |
| 210 | |
| 211 | /* |
| 212 | * Check if the most significant bits (MSB) of the operands are different. |
| 213 | */ |
| 214 | cond = ( x ^ y ); |
| 215 | /* |
| 216 | * If the MSB are the same then the difference x-y will be negative (and |
| 217 | * have its MSB set to 1 during conversion to unsigned) if and only if x<y. |
| 218 | */ |
| 219 | ret = ( x - y ) & ~cond; |
| 220 | /* |
| 221 | * If the MSB are different, then the operand with the MSB of 1 is the |
| 222 | * bigger. (That is if y has MSB of 1, then x<y is true and it is false if |
| 223 | * the MSB of y is 0.) |
| 224 | */ |
| 225 | ret |= y & cond; |
| 226 | |
| 227 | |
| 228 | ret = ret >> ( sizeof( mbedtls_mpi_uint ) * 8 - 1 ); |
| 229 | |
| 230 | return (unsigned) ret; |
| 231 | } |
| 232 | |
| 233 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 7533253 | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 234 | |
| 235 | /** Choose between two integer values, without branches. |
| 236 | * |
| 237 | * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled |
| 238 | * to code using bitwise operation rather than a branch. |
| 239 | * |
| 240 | * \param cond Condition to test. |
| 241 | * \param if1 Value to use if \p cond is nonzero. |
| 242 | * \param if0 Value to use if \p cond is zero. |
| 243 | * \return \c if1 if \p cond is nonzero, otherwise \c if0. |
| 244 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 245 | unsigned mbedtls_cf_uint_if( unsigned cond, |
| 246 | unsigned if1, |
| 247 | unsigned if0 ) |
gabor-mezei-arm | 7533253 | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 248 | { |
| 249 | unsigned mask = mbedtls_cf_uint_mask( cond ); |
| 250 | return( ( mask & if1 ) | (~mask & if0 ) ); |
| 251 | } |
gabor-mezei-arm | 5cec8b4 | 2021-09-27 13:03:57 +0200 | [diff] [blame] | 252 | |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 253 | size_t mbedtls_cf_size_if( unsigned cond, |
| 254 | size_t if1, |
| 255 | size_t if0 ) |
gabor-mezei-arm | bc3a288 | 2021-09-27 15:47:00 +0200 | [diff] [blame] | 256 | { |
| 257 | size_t mask = mbedtls_cf_size_mask( cond ); |
| 258 | return( ( mask & if1 ) | (~mask & if0 ) ); |
| 259 | } |
| 260 | |
gabor-mezei-arm | 5cec8b4 | 2021-09-27 13:03:57 +0200 | [diff] [blame] | 261 | /** |
| 262 | * Select between two sign values in constant-time. |
| 263 | * |
| 264 | * This is functionally equivalent to second ? a : b but uses only bit |
| 265 | * operations in order to avoid branches. |
| 266 | * |
| 267 | * \param[in] a The first sign; must be either +1 or -1. |
| 268 | * \param[in] b The second sign; must be either +1 or -1. |
| 269 | * \param[in] second Must be either 1 (return b) or 0 (return a). |
| 270 | * |
| 271 | * \return The selected sign value. |
| 272 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 273 | int mbedtls_cf_cond_select_sign( int a, |
| 274 | int b, |
| 275 | unsigned char second ) |
gabor-mezei-arm | 5cec8b4 | 2021-09-27 13:03:57 +0200 | [diff] [blame] | 276 | { |
| 277 | /* In order to avoid questions about what we can reasonnably assume about |
| 278 | * the representations of signed integers, move everything to unsigned |
| 279 | * by taking advantage of the fact that a and b are either +1 or -1. */ |
| 280 | unsigned ua = a + 1; |
| 281 | unsigned ub = b + 1; |
| 282 | |
| 283 | /* second was 0 or 1, mask is 0 or 2 as are ua and ub */ |
| 284 | const unsigned mask = second << 1; |
| 285 | |
| 286 | /* select ua or ub */ |
| 287 | unsigned ur = ( ua & ~mask ) | ( ub & mask ); |
| 288 | |
| 289 | /* ur is now 0 or 2, convert back to -1 or +1 */ |
| 290 | return( (int) ur - 1 ); |
| 291 | } |
gabor-mezei-arm | 043192d | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 292 | |
| 293 | #if defined(MBEDTLS_BIGNUM_C) |
| 294 | |
| 295 | /* |
| 296 | * Conditionally assign dest = src, without leaking information |
| 297 | * about whether the assignment was made or not. |
| 298 | * dest and src must be arrays of limbs of size n. |
| 299 | * assign must be 0 or 1. |
| 300 | */ |
| 301 | void mbedtls_cf_mpi_uint_cond_assign( size_t n, |
| 302 | mbedtls_mpi_uint *dest, |
| 303 | const mbedtls_mpi_uint *src, |
| 304 | unsigned char assign ) |
| 305 | { |
| 306 | size_t i; |
| 307 | |
| 308 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 309 | * but this is well-defined and precisely what we want to do here. */ |
| 310 | #if defined(_MSC_VER) |
| 311 | #pragma warning( push ) |
| 312 | #pragma warning( disable : 4146 ) |
| 313 | #endif |
| 314 | |
| 315 | /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */ |
| 316 | const mbedtls_mpi_uint mask = -assign; |
| 317 | |
| 318 | #if defined(_MSC_VER) |
| 319 | #pragma warning( pop ) |
| 320 | #endif |
| 321 | |
| 322 | for( i = 0; i < n; i++ ) |
| 323 | dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask ); |
| 324 | } |
| 325 | |
| 326 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 7b23c0b | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 327 | |
| 328 | /** Shift some data towards the left inside a buffer without leaking |
| 329 | * the length of the data through side channels. |
| 330 | * |
| 331 | * `mbedtls_cf_mem_move_to_left(start, total, offset)` is functionally |
| 332 | * equivalent to |
| 333 | * ``` |
| 334 | * memmove(start, start + offset, total - offset); |
| 335 | * memset(start + offset, 0, total - offset); |
| 336 | * ``` |
| 337 | * but it strives to use a memory access pattern (and thus total timing) |
| 338 | * that does not depend on \p offset. This timing independence comes at |
| 339 | * the expense of performance. |
| 340 | * |
| 341 | * \param start Pointer to the start of the buffer. |
| 342 | * \param total Total size of the buffer. |
| 343 | * \param offset Offset from which to copy \p total - \p offset bytes. |
| 344 | */ |
| 345 | void mbedtls_cf_mem_move_to_left( void *start, |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 346 | size_t total, |
| 347 | size_t offset ) |
gabor-mezei-arm | 7b23c0b | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 348 | { |
| 349 | volatile unsigned char *buf = start; |
| 350 | size_t i, n; |
| 351 | if( total == 0 ) |
| 352 | return; |
| 353 | for( i = 0; i < total; i++ ) |
| 354 | { |
| 355 | unsigned no_op = mbedtls_cf_size_gt( total - offset, i ); |
| 356 | /* The first `total - offset` passes are a no-op. The last |
| 357 | * `offset` passes shift the data one byte to the left and |
| 358 | * zero out the last byte. */ |
| 359 | for( n = 0; n < total - 1; n++ ) |
| 360 | { |
| 361 | unsigned char current = buf[n]; |
| 362 | unsigned char next = buf[n+1]; |
| 363 | buf[n] = mbedtls_cf_uint_if( no_op, current, next ); |
| 364 | } |
| 365 | buf[total-1] = mbedtls_cf_uint_if( no_op, buf[total-1], 0 ); |
| 366 | } |
| 367 | } |
gabor-mezei-arm | ee06feb | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 368 | |
| 369 | /* |
| 370 | * Constant-flow conditional memcpy: |
| 371 | * - if c1 == c2, equivalent to memcpy(dst, src, len), |
| 372 | * - otherwise, a no-op, |
| 373 | * but with execution flow independent of the values of c1 and c2. |
| 374 | * |
| 375 | * This function is implemented without using comparison operators, as those |
| 376 | * might be translated to branches by some compilers on some platforms. |
| 377 | */ |
| 378 | void mbedtls_cf_memcpy_if_eq( unsigned char *dst, |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 379 | const unsigned char *src, |
| 380 | size_t len, |
| 381 | size_t c1, |
| 382 | size_t c2 ) |
gabor-mezei-arm | ee06feb | 2021-09-27 13:34:25 +0200 | [diff] [blame] | 383 | { |
| 384 | /* mask = c1 == c2 ? 0xff : 0x00 */ |
| 385 | const size_t equal = mbedtls_cf_size_bool_eq( c1, c2 ); |
| 386 | const unsigned char mask = (unsigned char) mbedtls_cf_size_mask( equal ); |
| 387 | |
| 388 | /* dst[i] = c1 == c2 ? src[i] : dst[i] */ |
| 389 | for( size_t i = 0; i < len; i++ ) |
| 390 | dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask ); |
| 391 | } |
gabor-mezei-arm | 0f7b9e4 | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * Constant-flow memcpy from variable position in buffer. |
| 395 | * - functionally equivalent to memcpy(dst, src + offset_secret, len) |
| 396 | * - but with execution flow independent from the value of offset_secret. |
| 397 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 398 | void mbedtls_cf_memcpy_offset( unsigned char *dst, |
| 399 | const unsigned char *src_base, |
| 400 | size_t offset_secret, |
| 401 | size_t offset_min, |
| 402 | size_t offset_max, |
| 403 | size_t len ) |
gabor-mezei-arm | 0f7b9e4 | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 404 | { |
| 405 | size_t offset; |
| 406 | |
| 407 | for( offset = offset_min; offset <= offset_max; offset++ ) |
| 408 | { |
| 409 | mbedtls_cf_memcpy_if_eq( dst, src_base + offset, len, |
| 410 | offset, offset_secret ); |
| 411 | } |
| 412 | } |
gabor-mezei-arm | cb4317b | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 413 | |
| 414 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
| 415 | |
| 416 | /* |
| 417 | * Compute HMAC of variable-length data with constant flow. |
| 418 | * |
| 419 | * Only works with MD-5, SHA-1, SHA-256 and SHA-384. |
| 420 | * (Otherwise, computation of block_size needs to be adapted.) |
| 421 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 422 | int mbedtls_cf_hmac( mbedtls_md_context_t *ctx, |
| 423 | const unsigned char *add_data, |
| 424 | size_t add_data_len, |
| 425 | const unsigned char *data, |
| 426 | size_t data_len_secret, |
| 427 | size_t min_data_len, |
| 428 | size_t max_data_len, |
| 429 | unsigned char *output ) |
gabor-mezei-arm | cb4317b | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 430 | { |
| 431 | /* |
| 432 | * This function breaks the HMAC abstraction and uses the md_clone() |
| 433 | * extension to the MD API in order to get constant-flow behaviour. |
| 434 | * |
| 435 | * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means |
| 436 | * concatenation, and okey/ikey are the XOR of the key with some fixed bit |
| 437 | * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx. |
| 438 | * |
| 439 | * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to |
| 440 | * minlen, then cloning the context, and for each byte up to maxlen |
| 441 | * finishing up the hash computation, keeping only the correct result. |
| 442 | * |
| 443 | * Then we only need to compute HASH(okey + inner_hash) and we're done. |
| 444 | */ |
| 445 | const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info ); |
| 446 | /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5, |
| 447 | * all of which have the same block size except SHA-384. */ |
| 448 | const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64; |
| 449 | const unsigned char * const ikey = ctx->hmac_ctx; |
| 450 | const unsigned char * const okey = ikey + block_size; |
| 451 | const size_t hash_size = mbedtls_md_get_size( ctx->md_info ); |
| 452 | |
| 453 | unsigned char aux_out[MBEDTLS_MD_MAX_SIZE]; |
| 454 | mbedtls_md_context_t aux; |
| 455 | size_t offset; |
| 456 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 457 | |
| 458 | mbedtls_md_init( &aux ); |
| 459 | |
| 460 | #define MD_CHK( func_call ) \ |
| 461 | do { \ |
| 462 | ret = (func_call); \ |
| 463 | if( ret != 0 ) \ |
| 464 | goto cleanup; \ |
| 465 | } while( 0 ) |
| 466 | |
| 467 | MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) ); |
| 468 | |
| 469 | /* After hmac_start() of hmac_reset(), ikey has already been hashed, |
| 470 | * so we can start directly with the message */ |
| 471 | MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) ); |
| 472 | MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) ); |
| 473 | |
| 474 | /* For each possible length, compute the hash up to that point */ |
| 475 | for( offset = min_data_len; offset <= max_data_len; offset++ ) |
| 476 | { |
| 477 | MD_CHK( mbedtls_md_clone( &aux, ctx ) ); |
| 478 | MD_CHK( mbedtls_md_finish( &aux, aux_out ) ); |
| 479 | /* Keep only the correct inner_hash in the output buffer */ |
| 480 | mbedtls_cf_memcpy_if_eq( output, aux_out, hash_size, |
| 481 | offset, data_len_secret ); |
| 482 | |
| 483 | if( offset < max_data_len ) |
| 484 | MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) ); |
| 485 | } |
| 486 | |
| 487 | /* The context needs to finish() before it starts() again */ |
| 488 | MD_CHK( mbedtls_md_finish( ctx, aux_out ) ); |
| 489 | |
| 490 | /* Now compute HASH(okey + inner_hash) */ |
| 491 | MD_CHK( mbedtls_md_starts( ctx ) ); |
| 492 | MD_CHK( mbedtls_md_update( ctx, okey, block_size ) ); |
| 493 | MD_CHK( mbedtls_md_update( ctx, output, hash_size ) ); |
| 494 | MD_CHK( mbedtls_md_finish( ctx, output ) ); |
| 495 | |
| 496 | /* Done, get ready for next time */ |
| 497 | MD_CHK( mbedtls_md_hmac_reset( ctx ) ); |
| 498 | |
| 499 | #undef MD_CHK |
| 500 | |
| 501 | cleanup: |
| 502 | mbedtls_md_free( &aux ); |
| 503 | return( ret ); |
| 504 | } |
| 505 | |
| 506 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
gabor-mezei-arm | b8caeee | 2021-09-27 15:33:35 +0200 | [diff] [blame] | 507 | |
| 508 | #if defined(MBEDTLS_BIGNUM_C) |
| 509 | |
| 510 | #define MPI_VALIDATE_RET( cond ) \ |
| 511 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA ) |
| 512 | |
| 513 | /* |
| 514 | * Conditionally assign X = Y, without leaking information |
| 515 | * about whether the assignment was made or not. |
| 516 | * (Leaking information about the respective sizes of X and Y is ok however.) |
| 517 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 518 | int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, |
| 519 | const mbedtls_mpi *Y, |
| 520 | unsigned char assign ) |
gabor-mezei-arm | b8caeee | 2021-09-27 15:33:35 +0200 | [diff] [blame] | 521 | { |
| 522 | int ret = 0; |
| 523 | size_t i; |
| 524 | mbedtls_mpi_uint limb_mask; |
| 525 | MPI_VALIDATE_RET( X != NULL ); |
| 526 | MPI_VALIDATE_RET( Y != NULL ); |
| 527 | |
| 528 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 529 | * but this is well-defined and precisely what we want to do here. */ |
| 530 | #if defined(_MSC_VER) |
| 531 | #pragma warning( push ) |
| 532 | #pragma warning( disable : 4146 ) |
| 533 | #endif |
| 534 | |
| 535 | /* make sure assign is 0 or 1 in a time-constant manner */ |
| 536 | assign = (assign | (unsigned char)-assign) >> (sizeof( assign ) * 8 - 1); |
| 537 | /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */ |
| 538 | limb_mask = -assign; |
| 539 | |
| 540 | #if defined(_MSC_VER) |
| 541 | #pragma warning( pop ) |
| 542 | #endif |
| 543 | |
| 544 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); |
| 545 | |
| 546 | X->s = mbedtls_cf_cond_select_sign( X->s, Y->s, assign ); |
| 547 | |
| 548 | mbedtls_cf_mpi_uint_cond_assign( Y->n, X->p, Y->p, assign ); |
| 549 | |
| 550 | for( i = Y->n; i < X->n; i++ ) |
| 551 | X->p[i] &= ~limb_mask; |
| 552 | |
| 553 | cleanup: |
| 554 | return( ret ); |
| 555 | } |
| 556 | |
gabor-mezei-arm | 58fc8a6 | 2021-09-27 15:37:50 +0200 | [diff] [blame] | 557 | /* |
| 558 | * Conditionally swap X and Y, without leaking information |
| 559 | * about whether the swap was made or not. |
| 560 | * Here it is not ok to simply swap the pointers, which whould lead to |
| 561 | * different memory access patterns when X and Y are used afterwards. |
| 562 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 563 | int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, |
| 564 | mbedtls_mpi *Y, |
| 565 | unsigned char swap ) |
gabor-mezei-arm | 58fc8a6 | 2021-09-27 15:37:50 +0200 | [diff] [blame] | 566 | { |
| 567 | int ret, s; |
| 568 | size_t i; |
| 569 | mbedtls_mpi_uint limb_mask; |
| 570 | mbedtls_mpi_uint tmp; |
| 571 | MPI_VALIDATE_RET( X != NULL ); |
| 572 | MPI_VALIDATE_RET( Y != NULL ); |
| 573 | |
| 574 | if( X == Y ) |
| 575 | return( 0 ); |
| 576 | |
| 577 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 578 | * but this is well-defined and precisely what we want to do here. */ |
| 579 | #if defined(_MSC_VER) |
| 580 | #pragma warning( push ) |
| 581 | #pragma warning( disable : 4146 ) |
| 582 | #endif |
| 583 | |
| 584 | /* make sure swap is 0 or 1 in a time-constant manner */ |
| 585 | swap = (swap | (unsigned char)-swap) >> (sizeof( swap ) * 8 - 1); |
| 586 | /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */ |
| 587 | limb_mask = -swap; |
| 588 | |
| 589 | #if defined(_MSC_VER) |
| 590 | #pragma warning( pop ) |
| 591 | #endif |
| 592 | |
| 593 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) ); |
| 594 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) ); |
| 595 | |
| 596 | s = X->s; |
| 597 | X->s = mbedtls_cf_cond_select_sign( X->s, Y->s, swap ); |
| 598 | Y->s = mbedtls_cf_cond_select_sign( Y->s, s, swap ); |
| 599 | |
| 600 | |
| 601 | for( i = 0; i < X->n; i++ ) |
| 602 | { |
| 603 | tmp = X->p[i]; |
| 604 | X->p[i] = ( X->p[i] & ~limb_mask ) | ( Y->p[i] & limb_mask ); |
| 605 | Y->p[i] = ( Y->p[i] & ~limb_mask ) | ( tmp & limb_mask ); |
| 606 | } |
| 607 | |
| 608 | cleanup: |
| 609 | return( ret ); |
| 610 | } |
| 611 | |
gabor-mezei-arm | b10301d | 2021-09-27 15:41:30 +0200 | [diff] [blame] | 612 | /* |
| 613 | * Compare signed values in constant time |
| 614 | */ |
gabor-mezei-arm | 04087df | 2021-09-27 16:29:52 +0200 | [diff] [blame] | 615 | int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, |
| 616 | const mbedtls_mpi *Y, |
| 617 | unsigned *ret ) |
gabor-mezei-arm | b10301d | 2021-09-27 15:41:30 +0200 | [diff] [blame] | 618 | { |
| 619 | size_t i; |
| 620 | /* The value of any of these variables is either 0 or 1 at all times. */ |
| 621 | unsigned cond, done, X_is_negative, Y_is_negative; |
| 622 | |
| 623 | MPI_VALIDATE_RET( X != NULL ); |
| 624 | MPI_VALIDATE_RET( Y != NULL ); |
| 625 | MPI_VALIDATE_RET( ret != NULL ); |
| 626 | |
| 627 | if( X->n != Y->n ) |
| 628 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 629 | |
| 630 | /* |
| 631 | * Set sign_N to 1 if N >= 0, 0 if N < 0. |
| 632 | * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0. |
| 633 | */ |
| 634 | X_is_negative = ( X->s & 2 ) >> 1; |
| 635 | Y_is_negative = ( Y->s & 2 ) >> 1; |
| 636 | |
| 637 | /* |
| 638 | * If the signs are different, then the positive operand is the bigger. |
| 639 | * That is if X is negative (X_is_negative == 1), then X < Y is true and it |
| 640 | * is false if X is positive (X_is_negative == 0). |
| 641 | */ |
| 642 | cond = ( X_is_negative ^ Y_is_negative ); |
| 643 | *ret = cond & X_is_negative; |
| 644 | |
| 645 | /* |
| 646 | * This is a constant-time function. We might have the result, but we still |
| 647 | * need to go through the loop. Record if we have the result already. |
| 648 | */ |
| 649 | done = cond; |
| 650 | |
| 651 | for( i = X->n; i > 0; i-- ) |
| 652 | { |
| 653 | /* |
| 654 | * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both |
| 655 | * X and Y are negative. |
| 656 | * |
| 657 | * Again even if we can make a decision, we just mark the result and |
| 658 | * the fact that we are done and continue looping. |
| 659 | */ |
| 660 | cond = mbedtls_cf_mpi_uint_lt( Y->p[i - 1], X->p[i - 1] ); |
| 661 | *ret |= cond & ( 1 - done ) & X_is_negative; |
| 662 | done |= cond; |
| 663 | |
| 664 | /* |
| 665 | * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both |
| 666 | * X and Y are positive. |
| 667 | * |
| 668 | * Again even if we can make a decision, we just mark the result and |
| 669 | * the fact that we are done and continue looping. |
| 670 | */ |
| 671 | cond = mbedtls_cf_mpi_uint_lt( X->p[i - 1], Y->p[i - 1] ); |
| 672 | *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative ); |
| 673 | done |= cond; |
| 674 | } |
| 675 | |
| 676 | return( 0 ); |
| 677 | } |
| 678 | |
gabor-mezei-arm | b8caeee | 2021-09-27 15:33:35 +0200 | [diff] [blame] | 679 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | f52941e | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 680 | |
| 681 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 682 | |
| 683 | int mbedtls_cf_rsaes_pkcs1_v15_unpadding( int mode, |
| 684 | size_t ilen, |
| 685 | size_t *olen, |
| 686 | unsigned char *output, |
| 687 | size_t output_max_len, |
| 688 | unsigned char *buf ) |
| 689 | { |
| 690 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 691 | size_t i, plaintext_max_size; |
| 692 | |
| 693 | /* The following variables take sensitive values: their value must |
| 694 | * not leak into the observable behavior of the function other than |
| 695 | * the designated outputs (output, olen, return value). Otherwise |
| 696 | * this would open the execution of the function to |
| 697 | * side-channel-based variants of the Bleichenbacher padding oracle |
| 698 | * attack. Potential side channels include overall timing, memory |
| 699 | * access patterns (especially visible to an adversary who has access |
| 700 | * to a shared memory cache), and branches (especially visible to |
| 701 | * an adversary who has access to a shared code cache or to a shared |
| 702 | * branch predictor). */ |
| 703 | size_t pad_count = 0; |
| 704 | unsigned bad = 0; |
| 705 | unsigned char pad_done = 0; |
| 706 | size_t plaintext_size = 0; |
| 707 | unsigned output_too_large; |
| 708 | |
| 709 | plaintext_max_size = mbedtls_cf_size_if( output_max_len > ilen - 11, |
| 710 | ilen - 11, |
| 711 | output_max_len ); |
| 712 | |
| 713 | /* Check and get padding length in constant time and constant |
| 714 | * memory trace. The first byte must be 0. */ |
| 715 | bad |= buf[0]; |
| 716 | |
| 717 | if( mode == MBEDTLS_RSA_PRIVATE ) |
| 718 | { |
| 719 | /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00 |
| 720 | * where PS must be at least 8 nonzero bytes. */ |
| 721 | bad |= buf[1] ^ MBEDTLS_RSA_CRYPT; |
| 722 | |
| 723 | /* Read the whole buffer. Set pad_done to nonzero if we find |
| 724 | * the 0x00 byte and remember the padding length in pad_count. */ |
| 725 | for( i = 2; i < ilen; i++ ) |
| 726 | { |
| 727 | pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1; |
| 728 | pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
| 729 | } |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00 |
| 734 | * where PS must be at least 8 bytes with the value 0xFF. */ |
| 735 | bad |= buf[1] ^ MBEDTLS_RSA_SIGN; |
| 736 | |
| 737 | /* Read the whole buffer. Set pad_done to nonzero if we find |
| 738 | * the 0x00 byte and remember the padding length in pad_count. |
| 739 | * If there's a non-0xff byte in the padding, the padding is bad. */ |
| 740 | for( i = 2; i < ilen; i++ ) |
| 741 | { |
| 742 | pad_done |= mbedtls_cf_uint_if( buf[i], 0, 1 ); |
| 743 | pad_count += mbedtls_cf_uint_if( pad_done, 0, 1 ); |
| 744 | bad |= mbedtls_cf_uint_if( pad_done, 0, buf[i] ^ 0xFF ); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | /* If pad_done is still zero, there's no data, only unfinished padding. */ |
| 749 | bad |= mbedtls_cf_uint_if( pad_done, 0, 1 ); |
| 750 | |
| 751 | /* There must be at least 8 bytes of padding. */ |
| 752 | bad |= mbedtls_cf_size_gt( 8, pad_count ); |
| 753 | |
| 754 | /* If the padding is valid, set plaintext_size to the number of |
| 755 | * remaining bytes after stripping the padding. If the padding |
| 756 | * is invalid, avoid leaking this fact through the size of the |
| 757 | * output: use the maximum message size that fits in the output |
| 758 | * buffer. Do it without branches to avoid leaking the padding |
| 759 | * validity through timing. RSA keys are small enough that all the |
| 760 | * size_t values involved fit in unsigned int. */ |
| 761 | plaintext_size = mbedtls_cf_uint_if( |
| 762 | bad, (unsigned) plaintext_max_size, |
| 763 | (unsigned) ( ilen - pad_count - 3 ) ); |
| 764 | |
| 765 | /* Set output_too_large to 0 if the plaintext fits in the output |
| 766 | * buffer and to 1 otherwise. */ |
| 767 | output_too_large = mbedtls_cf_size_gt( plaintext_size, |
| 768 | plaintext_max_size ); |
| 769 | |
| 770 | /* Set ret without branches to avoid timing attacks. Return: |
| 771 | * - INVALID_PADDING if the padding is bad (bad != 0). |
| 772 | * - OUTPUT_TOO_LARGE if the padding is good but the decrypted |
| 773 | * plaintext does not fit in the output buffer. |
| 774 | * - 0 if the padding is correct. */ |
| 775 | ret = - (int) mbedtls_cf_uint_if( |
| 776 | bad, - MBEDTLS_ERR_RSA_INVALID_PADDING, |
| 777 | mbedtls_cf_uint_if( output_too_large, |
| 778 | - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE, |
| 779 | 0 ) ); |
| 780 | |
| 781 | /* If the padding is bad or the plaintext is too large, zero the |
| 782 | * data that we're about to copy to the output buffer. |
| 783 | * We need to copy the same amount of data |
| 784 | * from the same buffer whether the padding is good or not to |
| 785 | * avoid leaking the padding validity through overall timing or |
| 786 | * through memory or cache access patterns. */ |
| 787 | bad = mbedtls_cf_uint_mask( bad | output_too_large ); |
| 788 | for( i = 11; i < ilen; i++ ) |
| 789 | buf[i] &= ~bad; |
| 790 | |
| 791 | /* If the plaintext is too large, truncate it to the buffer size. |
| 792 | * Copy anyway to avoid revealing the length through timing, because |
| 793 | * revealing the length is as bad as revealing the padding validity |
| 794 | * for a Bleichenbacher attack. */ |
| 795 | plaintext_size = mbedtls_cf_uint_if( output_too_large, |
| 796 | (unsigned) plaintext_max_size, |
| 797 | (unsigned) plaintext_size ); |
| 798 | |
| 799 | /* Move the plaintext to the leftmost position where it can start in |
| 800 | * the working buffer, i.e. make it start plaintext_max_size from |
| 801 | * the end of the buffer. Do this with a memory access trace that |
| 802 | * does not depend on the plaintext size. After this move, the |
| 803 | * starting location of the plaintext is no longer sensitive |
| 804 | * information. */ |
| 805 | mbedtls_cf_mem_move_to_left( buf + ilen - plaintext_max_size, |
| 806 | plaintext_max_size, |
| 807 | plaintext_max_size - plaintext_size ); |
| 808 | |
| 809 | /* Finally copy the decrypted plaintext plus trailing zeros into the output |
| 810 | * buffer. If output_max_len is 0, then output may be an invalid pointer |
| 811 | * and the result of memcpy() would be undefined; prevent undefined |
| 812 | * behavior making sure to depend only on output_max_len (the size of the |
| 813 | * user-provided output buffer), which is independent from plaintext |
| 814 | * length, validity of padding, success of the decryption, and other |
| 815 | * secrets. */ |
| 816 | if( output_max_len != 0 ) |
| 817 | memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size ); |
| 818 | |
| 819 | /* Report the amount of data we copied to the output buffer. In case |
| 820 | * of errors (bad padding or output too large), the value of *olen |
| 821 | * when this function returns is not specified. Making it equivalent |
| 822 | * to the good case limits the risks of leaking the padding validity. */ |
| 823 | *olen = plaintext_size; |
| 824 | |
| 825 | return( ret ); |
| 826 | } |
| 827 | |
| 828 | #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */ |