blob: 3c82bd53faf54adc71791dde1acb2a0995072dac [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 */
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100123#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
124 mbedtls_ct_uint_t s;
125 asm volatile ("neg %x[s], %x[x] \n\t"
126 "orr %x[x], %x[s], %x[x] \n\t"
127 "asr %x[x], %x[x], 63"
128 :
129 [s] "=&r" (s),
130 [x] "+&r" (x)
131 :
132 :
133 );
134 return (mbedtls_ct_condition_t) x;
Dave Rodgmanef252792023-05-13 12:48:02 +0100135#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
136 uint32_t s;
137 asm volatile ("neg %[s], %[x] \n\t"
138 "orr %[x], %[x], %[s] \n\t"
139 "asr %[x], %[x], #31"
140 :
141 [s] "=&l" (s),
142 [x] "+&l" (x)
143 :
144 :
145 );
146 return (mbedtls_ct_condition_t) x;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100147#else
Dave Rodgman40a41d02023-05-17 11:59:56 +0100148 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
149#if defined(_MSC_VER)
150 /* MSVC has a warning about unary minus on unsigned, but this is
151 * well-defined and precisely what we want to do here */
152#pragma warning( push )
153#pragma warning( disable : 4146 )
154#endif
155 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
156 (MBEDTLS_CT_SIZE - 1));
157#if defined(_MSC_VER)
158#pragma warning( pop )
159#endif
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100160#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100161}
162
163static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
164 mbedtls_ct_uint_t if1,
165 mbedtls_ct_uint_t if0)
166{
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100167#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
168 asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
169 "mvn %x[condition], %x[condition] \n\t"
170 "and %x[condition], %x[condition], %x[if0] \n\t"
171 "orr %x[condition], %x[if1], %x[condition]"
172 :
173 [condition] "+&r" (condition),
174 [if1] "+&r" (if1)
175 :
176 [if0] "r" (if0)
177 :
178 );
179 return (mbedtls_ct_uint_t) condition;
Dave Rodgmanef252792023-05-13 12:48:02 +0100180#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
181 asm volatile ("and %[if1], %[if1], %[condition] \n\t"
182 "mvn %[condition], %[condition] \n\t"
183 "and %[condition], %[condition], %[if0] \n\t"
184 "orr %[condition], %[if1], %[condition]"
185 :
186 [condition] "+&l" (condition),
187 [if1] "+&l" (if1)
188 :
189 [if0] "l" (if0)
190 :
191 );
192 return (mbedtls_ct_uint_t) condition;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100193#else
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100194 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100195 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100196 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100197#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100198}
199
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100200static 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 +0100201{
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100202#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
203 uint64_t s1, s2;
204 asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
205 "sub %x[s2], %x[x], %x[y] \n\t"
206 "bic %x[s2], %x[s2], %[s1] \n\t"
207 "and %x[s1], %x[s1], %x[y] \n\t"
208 "orr %x[s1], %x[s2], %x[s1] \n\t"
209 "asr %x[x], %x[s1], 63"
210 : [s1] "=&r" (s1), [s2] "=&r" (s2), [x] "+r" (x)
211 : [y] "r" (y)
212 :
213 );
214 return (mbedtls_ct_condition_t) x;
Dave Rodgmanef252792023-05-13 12:48:02 +0100215#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
216 uint32_t s1;
217 asm volatile (
218#if defined(__thumb__) && !defined(__thumb2__)
219 "mov %[s1], %[x] \n\t"
220 "eor %[s1], %[s1], %[y] \n\t"
221#else
222 "eor %[s1], %[x], %[y] \n\t"
223#endif
224 "sub %[x], %[x], %[y] \n\t"
225 "bic %[x], %[x], %[s1] \n\t"
226 "and %[y], %[s1], %[y] \n\t"
227 "orr %[x], %[x], %[y] \n\t"
228 "asr %[x], %[x], #31"
229 : [s1] "=&l" (s1), [x] "+&l" (x), [y] "+&l" (y)
230 :
231 :
232 );
233 return (mbedtls_ct_condition_t) x;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100234#else
Dave Rodgman40a41d02023-05-17 11:59:56 +0100235 /* Ensure that the compiler cannot optimise the following operations over x and y,
236 * even if it knows the value of x and y.
237 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100238 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100239 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
240 /*
241 * Check if the most significant bits (MSB) of the operands are different.
242 * cond is true iff the MSBs differ.
243 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100244 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100245
246 /*
247 * If the MSB are the same then the difference x-y will be negative (and
248 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
249 *
250 * If the MSB are different, then the operand with the MSB of 1 is the
251 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
252 * the MSB of y is 0.)
253 */
254
255 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100256 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100257
258 // Extract only the MSB of ret
259 ret = ret >> (MBEDTLS_CT_SIZE - 1);
260
261 // Convert to a condition (i.e., all bits set iff non-zero)
262 return mbedtls_ct_bool(ret);
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100263#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100264}
265
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100266static 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 +0100267{
268 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100269 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100270
271 /* all ones if x != y, 0 otherwise */
272 return mbedtls_ct_bool(diff);
273}
274
275static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
276 unsigned char high,
277 unsigned char c,
278 unsigned char t)
279{
280 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
281 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
282
283 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
284 unsigned low_mask = ((unsigned) co - low) >> 8;
285 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
286 unsigned high_mask = ((unsigned) high - co) >> 8;
287
288 return (unsigned char) (~(low_mask | high_mask)) & to;
289}
290
291
292/* ============================================================================
293 * Everything below here is trivial wrapper functions
294 */
295
Dave Rodgman40a41d02023-05-17 11:59:56 +0100296static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
297 size_t if1,
298 size_t if0)
299{
300 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
301}
302
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100303static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100304 unsigned if1,
305 unsigned if0)
306{
307 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
308}
309
310#if defined(MBEDTLS_BIGNUM_C)
311
Dave Rodgman585f7f72023-05-17 17:45:33 +0100312static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
313 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100314 mbedtls_mpi_uint if0)
315{
316 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
317 (mbedtls_ct_uint_t) if1,
318 (mbedtls_ct_uint_t) if0);
319}
320
321#endif
322
Dave Rodgman98ddc012023-08-10 12:11:31 +0100323static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100324{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100325 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100326}
327
Dave Rodgman98ddc012023-08-10 12:11:31 +0100328static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100329{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100330 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100331}
332
333#if defined(MBEDTLS_BIGNUM_C)
334
Dave Rodgman98ddc012023-08-10 12:11:31 +0100335static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
336 mbedtls_mpi_uint if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100337{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100338 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100339}
340
341#endif /* MBEDTLS_BIGNUM_C */
342
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100343static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
Dave Rodgman585f7f72023-05-17 17:45:33 +0100344 mbedtls_ct_uint_t y)
345{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100346 return ~mbedtls_ct_uint_ne(x, y);
Dave Rodgman585f7f72023-05-17 17:45:33 +0100347}
348
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100349static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100350 mbedtls_ct_uint_t y)
351{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100352 return mbedtls_ct_uint_lt(y, x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100353}
354
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100355static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100356 mbedtls_ct_uint_t y)
357{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100358 return ~mbedtls_ct_uint_lt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100359}
360
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100361static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100362 mbedtls_ct_uint_t y)
363{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100364 return ~mbedtls_ct_uint_gt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100365}
366
367static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
368 mbedtls_ct_condition_t y)
369{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100370 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100371}
372
373static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
374 mbedtls_ct_condition_t y)
375{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100376 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100377}
378
379static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
380 mbedtls_ct_condition_t y)
381{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100382 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100383}
384
385static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
386{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100387 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100388}
389
Dave Rodgman205295c2023-08-01 14:10:56 +0100390#ifdef __GNUC__
391 #pragma GCC diagnostic pop
392#endif
393
Dave Rodgman40a41d02023-05-17 11:59:56 +0100394#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */