blob: 149cf75068e6e6e5183fa25237cbad4170d13118 [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
40
41/* Disable asm under Memsan because it confuses Memsan and generates false errors */
42#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
43#define MBEDTLS_CT_NO_ASM
44#elif defined(__has_feature)
45#if __has_feature(memory_sanitizer)
46#define MBEDTLS_CT_NO_ASM
47#endif
48#endif
49
50/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
51#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
52 __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
53#define MBEDTLS_CT_ASM
54#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
55#define MBEDTLS_CT_ARM_ASM
56#elif defined(__aarch64__)
57#define MBEDTLS_CT_AARCH64_ASM
58#endif
59#endif
60
61#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
62
63
64/* ============================================================================
65 * Core const-time primitives
66 */
67
68/** Ensure that the compiler cannot know the value of x (i.e., cannot optimise
69 * based on its value) after this function is called.
70 *
71 * If we are not using assembly, this will be fairly inefficient, so its use
72 * should be minimised.
73 */
74static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
75{
76#if defined(MBEDTLS_CT_ASM)
77 asm volatile ("" : [x] "+r" (x) :);
78 return x;
79#else
80 volatile mbedtls_ct_uint_t result = x;
81 return result;
82#endif
83}
84
85/* Convert a number into a condition in constant time. */
86static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
87{
88 /*
89 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
90 *
91 * For some platforms / type sizes, we define assembly to assure this.
92 *
93 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
94 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
95 */
96 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
97#if defined(_MSC_VER)
98 /* MSVC has a warning about unary minus on unsigned, but this is
99 * well-defined and precisely what we want to do here */
100#pragma warning( push )
101#pragma warning( disable : 4146 )
102#endif
103 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
104 (MBEDTLS_CT_SIZE - 1));
105#if defined(_MSC_VER)
106#pragma warning( pop )
107#endif
108}
109
110static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
111 mbedtls_ct_uint_t if1,
112 mbedtls_ct_uint_t if0)
113{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100114 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100115 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100116 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100117}
118
119static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
120{
121 /* Ensure that the compiler cannot optimise the following operations over x and y,
122 * even if it knows the value of x and y.
123 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100124 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100125 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
126 /*
127 * Check if the most significant bits (MSB) of the operands are different.
128 * cond is true iff the MSBs differ.
129 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100130 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100131
132 /*
133 * If the MSB are the same then the difference x-y will be negative (and
134 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
135 *
136 * If the MSB are different, then the operand with the MSB of 1 is the
137 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
138 * the MSB of y is 0.)
139 */
140
141 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100142 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100143
144 // Extract only the MSB of ret
145 ret = ret >> (MBEDTLS_CT_SIZE - 1);
146
147 // Convert to a condition (i.e., all bits set iff non-zero)
148 return mbedtls_ct_bool(ret);
149}
150
151static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
152{
153 /* diff = 0 if x == y, non-zero otherwise */
154 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ y;
155
156 /* all ones if x != y, 0 otherwise */
157 return mbedtls_ct_bool(diff);
158}
159
160static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
161 unsigned char high,
162 unsigned char c,
163 unsigned char t)
164{
165 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
166 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
167
168 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
169 unsigned low_mask = ((unsigned) co - low) >> 8;
170 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
171 unsigned high_mask = ((unsigned) high - co) >> 8;
172
173 return (unsigned char) (~(low_mask | high_mask)) & to;
174}
175
176
177/* ============================================================================
178 * Everything below here is trivial wrapper functions
179 */
180
181static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
182 mbedtls_ct_uint_t y)
183{
184 return ~mbedtls_ct_bool_ne(x, y);
185}
186
187static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
188 size_t if1,
189 size_t if0)
190{
191 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
192}
193
194static inline unsigned mbedtls_ct_uint_if_new(mbedtls_ct_condition_t condition,
195 unsigned if1,
196 unsigned if0)
197{
198 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
199}
200
201#if defined(MBEDTLS_BIGNUM_C)
202
203static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, \
204 mbedtls_mpi_uint if1, \
205 mbedtls_mpi_uint if0)
206{
207 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
208 (mbedtls_ct_uint_t) if1,
209 (mbedtls_ct_uint_t) if0);
210}
211
212#endif
213
214static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1)
215{
216 return (size_t) (mbedtls_ct_compiler_opaque(condition) & if1);
217}
218
219static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
220{
221 return (unsigned) (mbedtls_ct_compiler_opaque(condition) & if1);
222}
223
224#if defined(MBEDTLS_BIGNUM_C)
225
226static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
227 mbedtls_mpi_uint if1)
228{
229 return (mbedtls_mpi_uint) (mbedtls_ct_compiler_opaque(condition) & if1);
230}
231
232#endif /* MBEDTLS_BIGNUM_C */
233
234static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
235 mbedtls_ct_uint_t y)
236{
237 return mbedtls_ct_bool_lt(y, x);
238}
239
240static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
241 mbedtls_ct_uint_t y)
242{
243 return ~mbedtls_ct_bool_lt(x, y);
244}
245
246static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
247 mbedtls_ct_uint_t y)
248{
249 return ~mbedtls_ct_bool_gt(x, y);
250}
251
252static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
253 mbedtls_ct_condition_t y)
254{
255 return (mbedtls_ct_condition_t) (mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y));
256}
257
258static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
259 mbedtls_ct_condition_t y)
260{
261 return (mbedtls_ct_condition_t) (mbedtls_ct_compiler_opaque(x) & mbedtls_ct_compiler_opaque(y));
262}
263
264static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
265 mbedtls_ct_condition_t y)
266{
267 return (mbedtls_ct_condition_t) (mbedtls_ct_compiler_opaque(x) | mbedtls_ct_compiler_opaque(y));
268}
269
270static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
271{
272 return (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(x));
273}
274
275#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */