blob: 8570928070f65a8f6ee7f13a783d603fa7961499 [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 Rodgman3d574da2023-07-31 16:54:00 +010040/* Disable asm under Memsan because it confuses Memsan and generates false errors */
41#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
42#define MBEDTLS_CT_NO_ASM
43#elif defined(__has_feature)
44#if __has_feature(memory_sanitizer)
45#define MBEDTLS_CT_NO_ASM
46#endif
47#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +010048
49/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
50#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
Dave Rodgman3d574da2023-07-31 16:54:00 +010051 __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
Dave Rodgman40a41d02023-05-17 11:59:56 +010052#define MBEDTLS_CT_ASM
53#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
54#define MBEDTLS_CT_ARM_ASM
55#elif defined(__aarch64__)
56#define MBEDTLS_CT_AARCH64_ASM
57#endif
58#endif
59
60#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
61
62
63/* ============================================================================
64 * Core const-time primitives
65 */
66
Dave Rodgman2894d002023-06-08 17:52:21 +010067/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010068 * based on its value) after this function is called.
69 *
70 * If we are not using assembly, this will be fairly inefficient, so its use
71 * should be minimised.
72 */
Dave Rodgman2894d002023-06-08 17:52:21 +010073
74#if !defined(MBEDTLS_CT_ASM)
Dave Rodgman58c80f42023-06-12 18:19:46 +010075extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010076#endif
77
Dave Rodgman93cec452023-07-31 12:30:26 +010078/**
79 * \brief Ensure that a value cannot be known at compile time.
80 *
81 * \param x The value to hide from the compiler.
82 * \return The same value that was passed in, such that the compiler
83 * cannot prove its value (even for calls of the form
84 * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
85 *
86 * \note This is mainly used in constructing mbedtls_ct_condition_t
87 * values and performing operations over them, to ensure that
88 * there is no way for the compiler to ever know anything about
89 * the value of an mbedtls_ct_condition_t.
90 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010091static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
92{
93#if defined(MBEDTLS_CT_ASM)
94 asm volatile ("" : [x] "+r" (x) :);
95 return x;
96#else
Dave Rodgman2894d002023-06-08 17:52:21 +010097 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +010098#endif
99}
100
101/* Convert a number into a condition in constant time. */
102static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
103{
104 /*
105 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
106 *
107 * For some platforms / type sizes, we define assembly to assure this.
108 *
109 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
110 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
111 */
112 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
113#if defined(_MSC_VER)
114 /* MSVC has a warning about unary minus on unsigned, but this is
115 * well-defined and precisely what we want to do here */
116#pragma warning( push )
117#pragma warning( disable : 4146 )
118#endif
119 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
120 (MBEDTLS_CT_SIZE - 1));
121#if defined(_MSC_VER)
122#pragma warning( pop )
123#endif
124}
125
126static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
127 mbedtls_ct_uint_t if1,
128 mbedtls_ct_uint_t if0)
129{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100130 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100131 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100132 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100133}
134
135static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
136{
137 /* Ensure that the compiler cannot optimise the following operations over x and y,
138 * even if it knows the value of x and y.
139 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100140 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100141 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
142 /*
143 * Check if the most significant bits (MSB) of the operands are different.
144 * cond is true iff the MSBs differ.
145 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100146 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100147
148 /*
149 * If the MSB are the same then the difference x-y will be negative (and
150 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
151 *
152 * If the MSB are different, then the operand with the MSB of 1 is the
153 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
154 * the MSB of y is 0.)
155 */
156
157 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100158 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100159
160 // Extract only the MSB of ret
161 ret = ret >> (MBEDTLS_CT_SIZE - 1);
162
163 // Convert to a condition (i.e., all bits set iff non-zero)
164 return mbedtls_ct_bool(ret);
165}
166
167static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
168{
169 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100170 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100171
172 /* all ones if x != y, 0 otherwise */
173 return mbedtls_ct_bool(diff);
174}
175
176static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
177 unsigned char high,
178 unsigned char c,
179 unsigned char t)
180{
181 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
182 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
183
184 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
185 unsigned low_mask = ((unsigned) co - low) >> 8;
186 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
187 unsigned high_mask = ((unsigned) high - co) >> 8;
188
189 return (unsigned char) (~(low_mask | high_mask)) & to;
190}
191
192
193/* ============================================================================
194 * Everything below here is trivial wrapper functions
195 */
196
Dave Rodgman40a41d02023-05-17 11:59:56 +0100197static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
198 size_t if1,
199 size_t if0)
200{
201 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
202}
203
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100204static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100205 unsigned if1,
206 unsigned if0)
207{
208 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
209}
210
211#if defined(MBEDTLS_BIGNUM_C)
212
Dave Rodgman585f7f72023-05-17 17:45:33 +0100213static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
214 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100215 mbedtls_mpi_uint if0)
216{
217 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
218 (mbedtls_ct_uint_t) if1,
219 (mbedtls_ct_uint_t) if0);
220}
221
222#endif
223
224static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1)
225{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100226 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100227}
228
229static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
230{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100231 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100232}
233
234#if defined(MBEDTLS_BIGNUM_C)
235
236static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
237 mbedtls_mpi_uint if1)
238{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100239 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100240}
241
242#endif /* MBEDTLS_BIGNUM_C */
243
Dave Rodgman585f7f72023-05-17 17:45:33 +0100244static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
245 mbedtls_ct_uint_t y)
246{
247 return ~mbedtls_ct_bool_ne(x, y);
248}
249
Dave Rodgman40a41d02023-05-17 11:59:56 +0100250static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
251 mbedtls_ct_uint_t y)
252{
253 return mbedtls_ct_bool_lt(y, x);
254}
255
256static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
257 mbedtls_ct_uint_t y)
258{
259 return ~mbedtls_ct_bool_lt(x, y);
260}
261
262static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
263 mbedtls_ct_uint_t y)
264{
265 return ~mbedtls_ct_bool_gt(x, y);
266}
267
268static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
269 mbedtls_ct_condition_t y)
270{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100271 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100272}
273
274static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
275 mbedtls_ct_condition_t y)
276{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100277 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100278}
279
280static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
281 mbedtls_ct_condition_t y)
282{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100283 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100284}
285
286static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
287{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100288 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100289}
290
291#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */