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" |
| 22 | |
gabor-mezei-arm | 097d4f5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_BIGNUM_C) |
| 24 | #include "mbedtls/bignum.h" |
| 25 | #endif |
| 26 | |
| 27 | |
gabor-mezei-arm | 944c107 | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 28 | /* constant-time buffer comparison */ |
| 29 | int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n ) |
| 30 | { |
| 31 | size_t i; |
| 32 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 33 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
| 34 | volatile unsigned char diff = 0; |
| 35 | |
| 36 | for( i = 0; i < n; i++ ) |
| 37 | { |
| 38 | /* Read volatile data in order before computing diff. |
| 39 | * This avoids IAR compiler warning: |
| 40 | * 'the order of volatile accesses is undefined ..' */ |
| 41 | unsigned char x = A[i], y = B[i]; |
| 42 | diff |= x ^ y; |
| 43 | } |
| 44 | |
| 45 | return( diff ); |
| 46 | } |
| 47 | |
| 48 | /* Compare the contents of two buffers in constant time. |
| 49 | * Returns 0 if the contents are bitwise identical, otherwise returns |
| 50 | * a non-zero value. |
| 51 | * This is currently only used by GCM and ChaCha20+Poly1305. |
| 52 | */ |
| 53 | int mbedtls_constant_time_memcmp( const void *v1, const void *v2, |
| 54 | size_t len ) |
| 55 | { |
| 56 | const unsigned char *p1 = (const unsigned char*) v1; |
| 57 | const unsigned char *p2 = (const unsigned char*) v2; |
| 58 | size_t i; |
| 59 | unsigned char diff; |
| 60 | |
| 61 | for( diff = 0, i = 0; i < len; i++ ) |
| 62 | diff |= p1[i] ^ p2[i]; |
| 63 | |
| 64 | return( (int)diff ); |
| 65 | } |
| 66 | |
| 67 | /* constant-time buffer comparison */ |
| 68 | unsigned char mbedtls_nist_kw_safer_memcmp( const void *a, const void *b, size_t n ) |
| 69 | { |
| 70 | size_t i; |
| 71 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 72 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
| 73 | volatile unsigned char diff = 0; |
| 74 | |
| 75 | for( i = 0; i < n; i++ ) |
| 76 | { |
| 77 | /* Read volatile data in order before computing diff. |
| 78 | * This avoids IAR compiler warning: |
| 79 | * 'the order of volatile accesses is undefined ..' */ |
| 80 | unsigned char x = A[i], y = B[i]; |
| 81 | diff |= x ^ y; |
| 82 | } |
| 83 | |
| 84 | return( diff ); |
| 85 | } |
| 86 | |
| 87 | /* constant-time buffer comparison */ |
| 88 | int mbedtls_safer_memcmp( const void *a, const void *b, size_t n ) |
| 89 | { |
| 90 | size_t i; |
| 91 | const unsigned char *A = (const unsigned char *) a; |
| 92 | const unsigned char *B = (const unsigned char *) b; |
| 93 | unsigned char diff = 0; |
| 94 | |
| 95 | for( i = 0; i < n; i++ ) |
| 96 | diff |= A[i] ^ B[i]; |
| 97 | |
| 98 | return( diff ); |
| 99 | } |
gabor-mezei-arm | c11cac9 | 2021-09-27 11:40:03 +0200 | [diff] [blame] | 100 | |
| 101 | /** Turn zero-or-nonzero into zero-or-all-bits-one, without branches. |
| 102 | * |
| 103 | * \param value The value to analyze. |
| 104 | * \return Zero if \p value is zero, otherwise all-bits-one. |
| 105 | */ |
| 106 | unsigned mbedtls_cf_uint_mask( unsigned value ) |
| 107 | { |
| 108 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 109 | * well-defined and precisely what we want to do here */ |
| 110 | #if defined(_MSC_VER) |
| 111 | #pragma warning( push ) |
| 112 | #pragma warning( disable : 4146 ) |
| 113 | #endif |
| 114 | return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 115 | #if defined(_MSC_VER) |
| 116 | #pragma warning( pop ) |
| 117 | #endif |
| 118 | } |
gabor-mezei-arm | d361ccd | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Turn a bit into a mask: |
| 122 | * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1 |
| 123 | * - if bit == 0, return the all-bits 0 mask, aka 0 |
| 124 | * |
| 125 | * This function can be used to write constant-time code by replacing branches |
| 126 | * with bit operations using masks. |
| 127 | * |
| 128 | * This function is implemented without using comparison operators, as those |
| 129 | * might be translated to branches by some compilers on some platforms. |
| 130 | */ |
| 131 | size_t mbedtls_cf_size_mask( size_t bit ) |
| 132 | { |
| 133 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 134 | * but this is well-defined and precisely what we want to do here. */ |
| 135 | #if defined(_MSC_VER) |
| 136 | #pragma warning( push ) |
| 137 | #pragma warning( disable : 4146 ) |
| 138 | #endif |
| 139 | return -bit; |
| 140 | #if defined(_MSC_VER) |
| 141 | #pragma warning( pop ) |
| 142 | #endif |
| 143 | } |
gabor-mezei-arm | 4d6b146 | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Constant-flow mask generation for "less than" comparison: |
| 147 | * - if x < y, return all bits 1, that is (size_t) -1 |
| 148 | * - otherwise, return all bits 0, that is 0 |
| 149 | * |
| 150 | * This function can be used to write constant-time code by replacing branches |
| 151 | * with bit operations using masks. |
| 152 | * |
| 153 | * This function is implemented without using comparison operators, as those |
| 154 | * might be translated to branches by some compilers on some platforms. |
| 155 | */ |
| 156 | size_t mbedtls_cf_size_mask_lt( size_t x, size_t y ) |
| 157 | { |
| 158 | /* This has the most significant bit set if and only if x < y */ |
| 159 | const size_t sub = x - y; |
| 160 | |
| 161 | /* sub1 = (x < y) ? 1 : 0 */ |
| 162 | const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 ); |
| 163 | |
| 164 | /* mask = (x < y) ? 0xff... : 0x00... */ |
| 165 | const size_t mask = mbedtls_cf_size_mask( sub1 ); |
| 166 | |
| 167 | return( mask ); |
| 168 | } |
gabor-mezei-arm | a2bcabc | 2021-09-27 11:58:31 +0200 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Constant-flow mask generation for "greater or equal" comparison: |
| 172 | * - if x >= y, return all bits 1, that is (size_t) -1 |
| 173 | * - otherwise, return all bits 0, that is 0 |
| 174 | * |
| 175 | * This function can be used to write constant-time code by replacing branches |
| 176 | * with bit operations using masks. |
| 177 | * |
| 178 | * This function is implemented without using comparison operators, as those |
| 179 | * might be translated to branches by some compilers on some platforms. |
| 180 | */ |
| 181 | size_t mbedtls_cf_size_mask_ge( size_t x, size_t y ) |
| 182 | { |
| 183 | return( ~mbedtls_cf_size_mask_lt( x, y ) ); |
| 184 | } |
gabor-mezei-arm | 96584dd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Constant-flow boolean "equal" comparison: |
| 188 | * return x == y |
| 189 | * |
| 190 | * This function can be used to write constant-time code by replacing branches |
| 191 | * with bit operations - it can be used in conjunction with |
| 192 | * mbedtls_ssl_cf_mask_from_bit(). |
| 193 | * |
| 194 | * This function is implemented without using comparison operators, as those |
| 195 | * might be translated to branches by some compilers on some platforms. |
| 196 | */ |
| 197 | size_t mbedtls_cf_size_bool_eq( size_t x, size_t y ) |
| 198 | { |
| 199 | /* diff = 0 if x == y, non-zero otherwise */ |
| 200 | const size_t diff = x ^ y; |
| 201 | |
| 202 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 203 | * but this is well-defined and precisely what we want to do here. */ |
| 204 | #if defined(_MSC_VER) |
| 205 | #pragma warning( push ) |
| 206 | #pragma warning( disable : 4146 ) |
| 207 | #endif |
| 208 | |
| 209 | /* diff_msb's most significant bit is equal to x != y */ |
| 210 | const size_t diff_msb = ( diff | (size_t) -diff ); |
| 211 | |
| 212 | #if defined(_MSC_VER) |
| 213 | #pragma warning( pop ) |
| 214 | #endif |
| 215 | |
| 216 | /* diff1 = (x != y) ? 1 : 0 */ |
| 217 | const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 ); |
| 218 | |
| 219 | return( 1 ^ diff1 ); |
| 220 | } |
gabor-mezei-arm | 9d7bf09 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 221 | |
| 222 | /** Check whether a size is out of bounds, without branches. |
| 223 | * |
| 224 | * This is equivalent to `size > max`, but is likely to be compiled to |
| 225 | * to code using bitwise operation rather than a branch. |
| 226 | * |
| 227 | * \param size Size to check. |
| 228 | * \param max Maximum desired value for \p size. |
| 229 | * \return \c 0 if `size <= max`. |
| 230 | * \return \c 1 if `size > max`. |
| 231 | */ |
| 232 | unsigned mbedtls_cf_size_gt( size_t size, size_t max ) |
| 233 | { |
| 234 | /* Return the sign bit (1 for negative) of (max - size). */ |
| 235 | return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) ); |
| 236 | } |
gabor-mezei-arm | 097d4f5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 237 | |
| 238 | #if defined(MBEDTLS_BIGNUM_C) |
| 239 | |
| 240 | /** Decide if an integer is less than the other, without branches. |
| 241 | * |
| 242 | * \param x First integer. |
| 243 | * \param y Second integer. |
| 244 | * |
| 245 | * \return 1 if \p x is less than \p y, 0 otherwise |
| 246 | */ |
| 247 | unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x, |
| 248 | const mbedtls_mpi_uint y ) |
| 249 | { |
| 250 | mbedtls_mpi_uint ret; |
| 251 | mbedtls_mpi_uint cond; |
| 252 | |
| 253 | /* |
| 254 | * Check if the most significant bits (MSB) of the operands are different. |
| 255 | */ |
| 256 | cond = ( x ^ y ); |
| 257 | /* |
| 258 | * If the MSB are the same then the difference x-y will be negative (and |
| 259 | * have its MSB set to 1 during conversion to unsigned) if and only if x<y. |
| 260 | */ |
| 261 | ret = ( x - y ) & ~cond; |
| 262 | /* |
| 263 | * If the MSB are different, then the operand with the MSB of 1 is the |
| 264 | * bigger. (That is if y has MSB of 1, then x<y is true and it is false if |
| 265 | * the MSB of y is 0.) |
| 266 | */ |
| 267 | ret |= y & cond; |
| 268 | |
| 269 | |
| 270 | ret = ret >> ( sizeof( mbedtls_mpi_uint ) * 8 - 1 ); |
| 271 | |
| 272 | return (unsigned) ret; |
| 273 | } |
| 274 | |
| 275 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 7533253 | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 276 | |
| 277 | /** Choose between two integer values, without branches. |
| 278 | * |
| 279 | * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled |
| 280 | * to code using bitwise operation rather than a branch. |
| 281 | * |
| 282 | * \param cond Condition to test. |
| 283 | * \param if1 Value to use if \p cond is nonzero. |
| 284 | * \param if0 Value to use if \p cond is zero. |
| 285 | * \return \c if1 if \p cond is nonzero, otherwise \c if0. |
| 286 | */ |
| 287 | unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 ) |
| 288 | { |
| 289 | unsigned mask = mbedtls_cf_uint_mask( cond ); |
| 290 | return( ( mask & if1 ) | (~mask & if0 ) ); |
| 291 | } |
gabor-mezei-arm | 5cec8b4 | 2021-09-27 13:03:57 +0200 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * Select between two sign values in constant-time. |
| 295 | * |
| 296 | * This is functionally equivalent to second ? a : b but uses only bit |
| 297 | * operations in order to avoid branches. |
| 298 | * |
| 299 | * \param[in] a The first sign; must be either +1 or -1. |
| 300 | * \param[in] b The second sign; must be either +1 or -1. |
| 301 | * \param[in] second Must be either 1 (return b) or 0 (return a). |
| 302 | * |
| 303 | * \return The selected sign value. |
| 304 | */ |
| 305 | int mbedtls_cf_cond_select_sign( int a, int b, unsigned char second ) |
| 306 | { |
| 307 | /* In order to avoid questions about what we can reasonnably assume about |
| 308 | * the representations of signed integers, move everything to unsigned |
| 309 | * by taking advantage of the fact that a and b are either +1 or -1. */ |
| 310 | unsigned ua = a + 1; |
| 311 | unsigned ub = b + 1; |
| 312 | |
| 313 | /* second was 0 or 1, mask is 0 or 2 as are ua and ub */ |
| 314 | const unsigned mask = second << 1; |
| 315 | |
| 316 | /* select ua or ub */ |
| 317 | unsigned ur = ( ua & ~mask ) | ( ub & mask ); |
| 318 | |
| 319 | /* ur is now 0 or 2, convert back to -1 or +1 */ |
| 320 | return( (int) ur - 1 ); |
| 321 | } |
gabor-mezei-arm | 043192d | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 322 | |
| 323 | #if defined(MBEDTLS_BIGNUM_C) |
| 324 | |
| 325 | /* |
| 326 | * Conditionally assign dest = src, without leaking information |
| 327 | * about whether the assignment was made or not. |
| 328 | * dest and src must be arrays of limbs of size n. |
| 329 | * assign must be 0 or 1. |
| 330 | */ |
| 331 | void mbedtls_cf_mpi_uint_cond_assign( size_t n, |
| 332 | mbedtls_mpi_uint *dest, |
| 333 | const mbedtls_mpi_uint *src, |
| 334 | unsigned char assign ) |
| 335 | { |
| 336 | size_t i; |
| 337 | |
| 338 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 339 | * but this is well-defined and precisely what we want to do here. */ |
| 340 | #if defined(_MSC_VER) |
| 341 | #pragma warning( push ) |
| 342 | #pragma warning( disable : 4146 ) |
| 343 | #endif |
| 344 | |
| 345 | /* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */ |
| 346 | const mbedtls_mpi_uint mask = -assign; |
| 347 | |
| 348 | #if defined(_MSC_VER) |
| 349 | #pragma warning( pop ) |
| 350 | #endif |
| 351 | |
| 352 | for( i = 0; i < n; i++ ) |
| 353 | dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask ); |
| 354 | } |
| 355 | |
| 356 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 7b23c0b | 2021-09-27 13:31:06 +0200 | [diff] [blame^] | 357 | |
| 358 | /** Shift some data towards the left inside a buffer without leaking |
| 359 | * the length of the data through side channels. |
| 360 | * |
| 361 | * `mbedtls_cf_mem_move_to_left(start, total, offset)` is functionally |
| 362 | * equivalent to |
| 363 | * ``` |
| 364 | * memmove(start, start + offset, total - offset); |
| 365 | * memset(start + offset, 0, total - offset); |
| 366 | * ``` |
| 367 | * but it strives to use a memory access pattern (and thus total timing) |
| 368 | * that does not depend on \p offset. This timing independence comes at |
| 369 | * the expense of performance. |
| 370 | * |
| 371 | * \param start Pointer to the start of the buffer. |
| 372 | * \param total Total size of the buffer. |
| 373 | * \param offset Offset from which to copy \p total - \p offset bytes. |
| 374 | */ |
| 375 | void mbedtls_cf_mem_move_to_left( void *start, |
| 376 | size_t total, |
| 377 | size_t offset ) |
| 378 | { |
| 379 | volatile unsigned char *buf = start; |
| 380 | size_t i, n; |
| 381 | if( total == 0 ) |
| 382 | return; |
| 383 | for( i = 0; i < total; i++ ) |
| 384 | { |
| 385 | unsigned no_op = mbedtls_cf_size_gt( total - offset, i ); |
| 386 | /* The first `total - offset` passes are a no-op. The last |
| 387 | * `offset` passes shift the data one byte to the left and |
| 388 | * zero out the last byte. */ |
| 389 | for( n = 0; n < total - 1; n++ ) |
| 390 | { |
| 391 | unsigned char current = buf[n]; |
| 392 | unsigned char next = buf[n+1]; |
| 393 | buf[n] = mbedtls_cf_uint_if( no_op, current, next ); |
| 394 | } |
| 395 | buf[total-1] = mbedtls_cf_uint_if( no_op, buf[total-1], 0 ); |
| 396 | } |
| 397 | } |