blob: b2ef73f700f2a904ab7a6691f38dc2b9fa191bf7 [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 Rodgman08691672023-07-28 16:17:57 +010040#include "../tests/include/test/constant_flow.h"
Dave Rodgman40a41d02023-05-17 11:59:56 +010041
42/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
43#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
Dave Rodgman983448e2023-07-28 17:30:52 +010044 __ARMCC_VERSION >= 6000000)
Dave Rodgman40a41d02023-05-17 11:59:56 +010045#define MBEDTLS_CT_ASM
46#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
47#define MBEDTLS_CT_ARM_ASM
48#elif defined(__aarch64__)
49#define MBEDTLS_CT_AARCH64_ASM
50#endif
51#endif
52
53#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
54
55
56/* ============================================================================
57 * Core const-time primitives
58 */
59
Dave Rodgman2894d002023-06-08 17:52:21 +010060/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010061 * based on its value) after this function is called.
62 *
63 * If we are not using assembly, this will be fairly inefficient, so its use
64 * should be minimised.
65 */
Dave Rodgman2894d002023-06-08 17:52:21 +010066
67#if !defined(MBEDTLS_CT_ASM)
Dave Rodgman58c80f42023-06-12 18:19:46 +010068extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010069#endif
70
Dave Rodgman93cec452023-07-31 12:30:26 +010071/**
72 * \brief Ensure that a value cannot be known at compile time.
73 *
74 * \param x The value to hide from the compiler.
75 * \return The same value that was passed in, such that the compiler
76 * cannot prove its value (even for calls of the form
77 * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
78 *
79 * \note This is mainly used in constructing mbedtls_ct_condition_t
80 * values and performing operations over them, to ensure that
81 * there is no way for the compiler to ever know anything about
82 * the value of an mbedtls_ct_condition_t.
83 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010084static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
85{
86#if defined(MBEDTLS_CT_ASM)
Dave Rodgman08691672023-07-28 16:17:57 +010087 /* Prevent false positives from Memsan - otherwise it will report the asm as
88 * accessing secret data. */
Dave Rodgman2d28c462023-07-28 18:22:56 +010089 TEST_CF_SAVE_SECRET(x);
Dave Rodgman08691672023-07-28 16:17:57 +010090
Dave Rodgman40a41d02023-05-17 11:59:56 +010091 asm volatile ("" : [x] "+r" (x) :);
Dave Rodgman08691672023-07-28 16:17:57 +010092
Dave Rodgman2d28c462023-07-28 18:22:56 +010093 /* Mark the return value as secret (if it was previously marked secret).
94 * This is needed so that code of the form:
Dave Rodgman08691672023-07-28 16:17:57 +010095 *
96 * if (mbedtls_ct_compiler_opaque(secret)) { ... }
97 *
98 * will fail const-flow tests.
99 */
Dave Rodgman2d28c462023-07-28 18:22:56 +0100100 TEST_CF_RESTORE_SECRET(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100101 return x;
102#else
Dave Rodgman2894d002023-06-08 17:52:21 +0100103 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +0100104#endif
105}
106
107/* Convert a number into a condition in constant time. */
108static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
109{
110 /*
111 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
112 *
113 * For some platforms / type sizes, we define assembly to assure this.
114 *
115 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
116 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
117 */
118 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
119#if defined(_MSC_VER)
120 /* MSVC has a warning about unary minus on unsigned, but this is
121 * well-defined and precisely what we want to do here */
122#pragma warning( push )
123#pragma warning( disable : 4146 )
124#endif
125 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
126 (MBEDTLS_CT_SIZE - 1));
127#if defined(_MSC_VER)
128#pragma warning( pop )
129#endif
130}
131
132static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
133 mbedtls_ct_uint_t if1,
134 mbedtls_ct_uint_t if0)
135{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100136 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100137 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100138 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100139}
140
141static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
142{
143 /* Ensure that the compiler cannot optimise the following operations over x and y,
144 * even if it knows the value of x and y.
145 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100146 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100147 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
148 /*
149 * Check if the most significant bits (MSB) of the operands are different.
150 * cond is true iff the MSBs differ.
151 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100152 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100153
154 /*
155 * If the MSB are the same then the difference x-y will be negative (and
156 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
157 *
158 * If the MSB are different, then the operand with the MSB of 1 is the
159 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
160 * the MSB of y is 0.)
161 */
162
163 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100164 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100165
166 // Extract only the MSB of ret
167 ret = ret >> (MBEDTLS_CT_SIZE - 1);
168
169 // Convert to a condition (i.e., all bits set iff non-zero)
170 return mbedtls_ct_bool(ret);
171}
172
173static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
174{
175 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100176 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100177
178 /* all ones if x != y, 0 otherwise */
179 return mbedtls_ct_bool(diff);
180}
181
182static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
183 unsigned char high,
184 unsigned char c,
185 unsigned char t)
186{
187 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
188 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
189
190 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
191 unsigned low_mask = ((unsigned) co - low) >> 8;
192 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
193 unsigned high_mask = ((unsigned) high - co) >> 8;
194
195 return (unsigned char) (~(low_mask | high_mask)) & to;
196}
197
198
199/* ============================================================================
200 * Everything below here is trivial wrapper functions
201 */
202
Dave Rodgman40a41d02023-05-17 11:59:56 +0100203static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
204 size_t if1,
205 size_t if0)
206{
207 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
208}
209
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100210static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100211 unsigned if1,
212 unsigned if0)
213{
214 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
215}
216
217#if defined(MBEDTLS_BIGNUM_C)
218
Dave Rodgman585f7f72023-05-17 17:45:33 +0100219static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
220 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100221 mbedtls_mpi_uint if0)
222{
223 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
224 (mbedtls_ct_uint_t) if1,
225 (mbedtls_ct_uint_t) if0);
226}
227
228#endif
229
230static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1)
231{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100232 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100233}
234
235static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
236{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100237 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100238}
239
240#if defined(MBEDTLS_BIGNUM_C)
241
242static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
243 mbedtls_mpi_uint if1)
244{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100245 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100246}
247
248#endif /* MBEDTLS_BIGNUM_C */
249
Dave Rodgman585f7f72023-05-17 17:45:33 +0100250static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
251 mbedtls_ct_uint_t y)
252{
253 return ~mbedtls_ct_bool_ne(x, y);
254}
255
Dave Rodgman40a41d02023-05-17 11:59:56 +0100256static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
257 mbedtls_ct_uint_t y)
258{
259 return mbedtls_ct_bool_lt(y, x);
260}
261
262static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
263 mbedtls_ct_uint_t y)
264{
265 return ~mbedtls_ct_bool_lt(x, y);
266}
267
268static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
269 mbedtls_ct_uint_t y)
270{
271 return ~mbedtls_ct_bool_gt(x, y);
272}
273
274static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(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_and(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_or(mbedtls_ct_condition_t x,
287 mbedtls_ct_condition_t y)
288{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100289 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100290}
291
292static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
293{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100294 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100295}
296
297#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */