gabor-mezei-arm | d112534 | 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 | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 21 | #include "constant_time.h" |
| 22 | |
| 23 | /* constant-time buffer comparison */ |
| 24 | int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n ) |
| 25 | { |
| 26 | size_t i; |
| 27 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 28 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
| 29 | volatile unsigned char diff = 0; |
| 30 | |
| 31 | for( i = 0; i < n; i++ ) |
| 32 | { |
| 33 | /* Read volatile data in order before computing diff. |
| 34 | * This avoids IAR compiler warning: |
| 35 | * 'the order of volatile accesses is undefined ..' */ |
| 36 | unsigned char x = A[i], y = B[i]; |
| 37 | diff |= x ^ y; |
| 38 | } |
| 39 | |
| 40 | return( diff ); |
| 41 | } |
| 42 | |
| 43 | /* Compare the contents of two buffers in constant time. |
| 44 | * Returns 0 if the contents are bitwise identical, otherwise returns |
| 45 | * a non-zero value. |
| 46 | * This is currently only used by GCM and ChaCha20+Poly1305. |
| 47 | */ |
| 48 | int mbedtls_constant_time_memcmp( const void *v1, const void *v2, |
| 49 | size_t len ) |
| 50 | { |
| 51 | const unsigned char *p1 = (const unsigned char*) v1; |
| 52 | const unsigned char *p2 = (const unsigned char*) v2; |
| 53 | size_t i; |
| 54 | unsigned char diff; |
| 55 | |
| 56 | for( diff = 0, i = 0; i < len; i++ ) |
| 57 | diff |= p1[i] ^ p2[i]; |
| 58 | |
| 59 | return( (int)diff ); |
| 60 | } |
| 61 | |
| 62 | /* constant-time buffer comparison */ |
| 63 | unsigned char mbedtls_nist_kw_safer_memcmp( const void *a, const void *b, size_t n ) |
| 64 | { |
| 65 | size_t i; |
| 66 | volatile const unsigned char *A = (volatile const unsigned char *) a; |
| 67 | volatile const unsigned char *B = (volatile const unsigned char *) b; |
| 68 | volatile unsigned char diff = 0; |
| 69 | |
| 70 | for( i = 0; i < n; i++ ) |
| 71 | { |
| 72 | /* Read volatile data in order before computing diff. |
| 73 | * This avoids IAR compiler warning: |
| 74 | * 'the order of volatile accesses is undefined ..' */ |
| 75 | unsigned char x = A[i], y = B[i]; |
| 76 | diff |= x ^ y; |
| 77 | } |
| 78 | |
| 79 | return( diff ); |
| 80 | } |
| 81 | |
| 82 | /* constant-time buffer comparison */ |
| 83 | int mbedtls_safer_memcmp( const void *a, const void *b, size_t n ) |
| 84 | { |
| 85 | size_t i; |
| 86 | const unsigned char *A = (const unsigned char *) a; |
| 87 | const unsigned char *B = (const unsigned char *) b; |
| 88 | unsigned char diff = 0; |
| 89 | |
| 90 | for( i = 0; i < n; i++ ) |
| 91 | diff |= A[i] ^ B[i]; |
| 92 | |
| 93 | return( diff ); |
| 94 | } |
gabor-mezei-arm | 340948e | 2021-09-27 11:40:03 +0200 | [diff] [blame] | 95 | |
| 96 | /** Turn zero-or-nonzero into zero-or-all-bits-one, without branches. |
| 97 | * |
| 98 | * \param value The value to analyze. |
| 99 | * \return Zero if \p value is zero, otherwise all-bits-one. |
| 100 | */ |
| 101 | unsigned mbedtls_cf_uint_mask( unsigned value ) |
| 102 | { |
| 103 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 104 | * well-defined and precisely what we want to do here */ |
| 105 | #if defined(_MSC_VER) |
| 106 | #pragma warning( push ) |
| 107 | #pragma warning( disable : 4146 ) |
| 108 | #endif |
| 109 | return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 110 | #if defined(_MSC_VER) |
| 111 | #pragma warning( pop ) |
| 112 | #endif |
| 113 | } |
gabor-mezei-arm | 3733bf8 | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Turn a bit into a mask: |
| 117 | * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1 |
| 118 | * - if bit == 0, return the all-bits 0 mask, aka 0 |
| 119 | * |
| 120 | * This function can be used to write constant-time code by replacing branches |
| 121 | * with bit operations using masks. |
| 122 | * |
| 123 | * This function is implemented without using comparison operators, as those |
| 124 | * might be translated to branches by some compilers on some platforms. |
| 125 | */ |
| 126 | size_t mbedtls_cf_size_mask( size_t bit ) |
| 127 | { |
| 128 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 129 | * but this is well-defined and precisely what we want to do here. */ |
| 130 | #if defined(_MSC_VER) |
| 131 | #pragma warning( push ) |
| 132 | #pragma warning( disable : 4146 ) |
| 133 | #endif |
| 134 | return -bit; |
| 135 | #if defined(_MSC_VER) |
| 136 | #pragma warning( pop ) |
| 137 | #endif |
| 138 | } |
gabor-mezei-arm | c76227d | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Constant-flow mask generation for "less than" comparison: |
| 142 | * - if x < y, return all bits 1, that is (size_t) -1 |
| 143 | * - otherwise, return all bits 0, that is 0 |
| 144 | * |
| 145 | * This function can be used to write constant-time code by replacing branches |
| 146 | * with bit operations using masks. |
| 147 | * |
| 148 | * This function is implemented without using comparison operators, as those |
| 149 | * might be translated to branches by some compilers on some platforms. |
| 150 | */ |
| 151 | size_t mbedtls_cf_size_mask_lt( size_t x, size_t y ) |
| 152 | { |
| 153 | /* This has the most significant bit set if and only if x < y */ |
| 154 | const size_t sub = x - y; |
| 155 | |
| 156 | /* sub1 = (x < y) ? 1 : 0 */ |
| 157 | const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 ); |
| 158 | |
| 159 | /* mask = (x < y) ? 0xff... : 0x00... */ |
| 160 | const size_t mask = mbedtls_cf_size_mask( sub1 ); |
| 161 | |
| 162 | return( mask ); |
| 163 | } |
gabor-mezei-arm | 16fc57b | 2021-09-27 11:58:31 +0200 | [diff] [blame^] | 164 | |
| 165 | /* |
| 166 | * Constant-flow mask generation for "greater or equal" comparison: |
| 167 | * - if x >= y, return all bits 1, that is (size_t) -1 |
| 168 | * - otherwise, return all bits 0, that is 0 |
| 169 | * |
| 170 | * This function can be used to write constant-time code by replacing branches |
| 171 | * with bit operations using masks. |
| 172 | * |
| 173 | * This function is implemented without using comparison operators, as those |
| 174 | * might be translated to branches by some compilers on some platforms. |
| 175 | */ |
| 176 | size_t mbedtls_cf_size_mask_ge( size_t x, size_t y ) |
| 177 | { |
| 178 | return( ~mbedtls_cf_size_mask_lt( x, y ) ); |
| 179 | } |