blob: b513c6a9d48e6f08b9e5ddb5299ced039f83c082 [file] [log] [blame]
gabor-mezei-armd1125342021-07-12 16:31:22 +02001/**
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-armdb9a38c2021-09-27 11:28:54 +020021#include "constant_time.h"
22
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020023#if defined(MBEDTLS_BIGNUM_C)
24#include "mbedtls/bignum.h"
25#endif
26
27
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020028/* constant-time buffer comparison */
29int 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 */
53int 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 */
68unsigned 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 */
88int 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-arm340948e2021-09-27 11:40:03 +0200100
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 */
106unsigned 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-arm3733bf82021-09-27 11:49:42 +0200119
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 */
131size_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-armc76227d2021-09-27 11:53:54 +0200144
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 */
156size_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-arm16fc57b2021-09-27 11:58:31 +0200169
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 */
181size_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-arm8d1d5fd2021-09-27 12:15:19 +0200185
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 */
197size_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-arm5a854422021-09-27 12:25:07 +0200221
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 */
232unsigned 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-arm3f90fd52021-09-27 12:55:33 +0200237
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 */
247unsigned 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 */