blob: b251a664c05dec41f54db6bc5f9562ce11aca225 [file] [log] [blame]
Dave Rodgman40a41d02023-05-17 11:59:56 +01001/**
2 * Constant-time functions
3 *
4 * For readability, the static inline definitions are here, and
5 * constant_time_internal.h has only the declarations.
6 *
7 * This results in duplicate declarations of the form:
8 * static inline void f() { ... }
9 * static inline void f();
10 * when constant_time_internal.h is included. This appears to behave
11 * exactly as if the declaration-without-definition was not present.
12 *
13 * Copyright The Mbed TLS Contributors
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29#ifndef MBEDTLS_CONSTANT_TIME_IMPL_H
30#define MBEDTLS_CONSTANT_TIME_IMPL_H
31
32#include <stddef.h>
33
34#include "common.h"
35
36#if defined(MBEDTLS_BIGNUM_C)
37#include "mbedtls/bignum.h"
38#endif
39
Dave Rodgman205295c2023-08-01 14:10:56 +010040/* constant_time_impl.h contains all the static inline implementations,
41 * so that constant_time_internal.h is more readable.
42 *
43 * gcc generates warnings about duplicate declarations, so disable this
44 * warning.
45 */
46#ifdef __GNUC__
47 #pragma GCC diagnostic push
48 #pragma GCC diagnostic ignored "-Wredundant-decls"
49#endif
50
Dave Rodgman3d574da2023-07-31 16:54:00 +010051/* Disable asm under Memsan because it confuses Memsan and generates false errors */
52#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
53#define MBEDTLS_CT_NO_ASM
54#elif defined(__has_feature)
55#if __has_feature(memory_sanitizer)
56#define MBEDTLS_CT_NO_ASM
57#endif
58#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +010059
60/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
61#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
Dave Rodgman3d574da2023-07-31 16:54:00 +010062 __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
Dave Rodgman40a41d02023-05-17 11:59:56 +010063#define MBEDTLS_CT_ASM
64#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
65#define MBEDTLS_CT_ARM_ASM
66#elif defined(__aarch64__)
67#define MBEDTLS_CT_AARCH64_ASM
68#endif
69#endif
70
71#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
72
73
74/* ============================================================================
75 * Core const-time primitives
76 */
77
Dave Rodgman2894d002023-06-08 17:52:21 +010078/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010079 * based on its value) after this function is called.
80 *
81 * If we are not using assembly, this will be fairly inefficient, so its use
82 * should be minimised.
83 */
Dave Rodgman2894d002023-06-08 17:52:21 +010084
85#if !defined(MBEDTLS_CT_ASM)
Dave Rodgman58c80f42023-06-12 18:19:46 +010086extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010087#endif
88
Dave Rodgman93cec452023-07-31 12:30:26 +010089/**
90 * \brief Ensure that a value cannot be known at compile time.
91 *
92 * \param x The value to hide from the compiler.
93 * \return The same value that was passed in, such that the compiler
94 * cannot prove its value (even for calls of the form
95 * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
96 *
97 * \note This is mainly used in constructing mbedtls_ct_condition_t
98 * values and performing operations over them, to ensure that
99 * there is no way for the compiler to ever know anything about
100 * the value of an mbedtls_ct_condition_t.
101 */
Dave Rodgman40a41d02023-05-17 11:59:56 +0100102static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
103{
104#if defined(MBEDTLS_CT_ASM)
105 asm volatile ("" : [x] "+r" (x) :);
106 return x;
107#else
Dave Rodgman2894d002023-06-08 17:52:21 +0100108 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +0100109#endif
110}
111
112/* Convert a number into a condition in constant time. */
113static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
114{
115 /*
116 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
117 *
118 * For some platforms / type sizes, we define assembly to assure this.
119 *
120 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
121 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
122 */
123 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
124#if defined(_MSC_VER)
125 /* MSVC has a warning about unary minus on unsigned, but this is
126 * well-defined and precisely what we want to do here */
127#pragma warning( push )
128#pragma warning( disable : 4146 )
129#endif
130 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
131 (MBEDTLS_CT_SIZE - 1));
132#if defined(_MSC_VER)
133#pragma warning( pop )
134#endif
135}
136
137static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
138 mbedtls_ct_uint_t if1,
139 mbedtls_ct_uint_t if0)
140{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100141 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100142 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100143 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100144}
145
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100146static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100147{
148 /* Ensure that the compiler cannot optimise the following operations over x and y,
149 * even if it knows the value of x and y.
150 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100151 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100152 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
153 /*
154 * Check if the most significant bits (MSB) of the operands are different.
155 * cond is true iff the MSBs differ.
156 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100157 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100158
159 /*
160 * If the MSB are the same then the difference x-y will be negative (and
161 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
162 *
163 * If the MSB are different, then the operand with the MSB of 1 is the
164 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
165 * the MSB of y is 0.)
166 */
167
168 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100169 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100170
171 // Extract only the MSB of ret
172 ret = ret >> (MBEDTLS_CT_SIZE - 1);
173
174 // Convert to a condition (i.e., all bits set iff non-zero)
175 return mbedtls_ct_bool(ret);
176}
177
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100178static inline mbedtls_ct_condition_t mbedtls_ct_uint_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100179{
180 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100181 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100182
183 /* all ones if x != y, 0 otherwise */
184 return mbedtls_ct_bool(diff);
185}
186
187static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
188 unsigned char high,
189 unsigned char c,
190 unsigned char t)
191{
Agathiyan Bragadeesh9ebfa7f2023-08-17 10:00:01 +0100192 const unsigned char co = (unsigned char) mbedtls_ct_compiler_opaque(c);
193 const unsigned char to = (unsigned char) mbedtls_ct_compiler_opaque(t);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100194
195 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
196 unsigned low_mask = ((unsigned) co - low) >> 8;
197 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
198 unsigned high_mask = ((unsigned) high - co) >> 8;
199
200 return (unsigned char) (~(low_mask | high_mask)) & to;
201}
202
203
204/* ============================================================================
205 * Everything below here is trivial wrapper functions
206 */
207
Dave Rodgman40a41d02023-05-17 11:59:56 +0100208static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
209 size_t if1,
210 size_t if0)
211{
212 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
213}
214
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100215static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100216 unsigned if1,
217 unsigned if0)
218{
219 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
220}
221
222#if defined(MBEDTLS_BIGNUM_C)
223
Dave Rodgman585f7f72023-05-17 17:45:33 +0100224static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
225 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100226 mbedtls_mpi_uint if0)
227{
228 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
229 (mbedtls_ct_uint_t) if1,
230 (mbedtls_ct_uint_t) if0);
231}
232
233#endif
234
Dave Rodgman98ddc012023-08-10 12:11:31 +0100235static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100236{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100237 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100238}
239
Dave Rodgman98ddc012023-08-10 12:11:31 +0100240static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100241{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100242 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100243}
244
245#if defined(MBEDTLS_BIGNUM_C)
246
Dave Rodgman98ddc012023-08-10 12:11:31 +0100247static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
248 mbedtls_mpi_uint if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100249{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100250 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100251}
252
253#endif /* MBEDTLS_BIGNUM_C */
254
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100255static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
Dave Rodgman585f7f72023-05-17 17:45:33 +0100256 mbedtls_ct_uint_t y)
257{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100258 return ~mbedtls_ct_uint_ne(x, y);
Dave Rodgman585f7f72023-05-17 17:45:33 +0100259}
260
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100261static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100262 mbedtls_ct_uint_t y)
263{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100264 return mbedtls_ct_uint_lt(y, x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100265}
266
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100267static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100268 mbedtls_ct_uint_t y)
269{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100270 return ~mbedtls_ct_uint_lt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100271}
272
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100273static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100274 mbedtls_ct_uint_t y)
275{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100276 return ~mbedtls_ct_uint_gt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100277}
278
279static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
280 mbedtls_ct_condition_t y)
281{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100282 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100283}
284
285static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
286 mbedtls_ct_condition_t y)
287{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100288 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100289}
290
291static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
292 mbedtls_ct_condition_t y)
293{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100294 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100295}
296
297static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
298{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100299 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100300}
301
Dave Rodgman205295c2023-08-01 14:10:56 +0100302#ifdef __GNUC__
303 #pragma GCC diagnostic pop
304#endif
305
Dave Rodgman40a41d02023-05-17 11:59:56 +0100306#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */