blob: c490d8229d90da061126c67074457848e4091659 [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
Dave Rodgman2894d002023-06-08 17:52:21 +010068/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010069 * 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 */
Dave Rodgman2894d002023-06-08 17:52:21 +010074
75#if !defined(MBEDTLS_CT_ASM)
76/*
77* Define an object with the value zero, such that the compiler cannot prove that it
78* has the value zero (because it is volatile, it "may be modified in ways unknown to
79* the implementation").
80*/
81static volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0;
82#endif
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)
87 asm volatile ("" : [x] "+r" (x) :);
88 return x;
89#else
Dave Rodgman2894d002023-06-08 17:52:21 +010090 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +010091#endif
92}
93
94/* Convert a number into a condition in constant time. */
95static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
96{
97 /*
98 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
99 *
100 * For some platforms / type sizes, we define assembly to assure this.
101 *
102 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
103 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
104 */
105 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
106#if defined(_MSC_VER)
107 /* MSVC has a warning about unary minus on unsigned, but this is
108 * well-defined and precisely what we want to do here */
109#pragma warning( push )
110#pragma warning( disable : 4146 )
111#endif
112 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
113 (MBEDTLS_CT_SIZE - 1));
114#if defined(_MSC_VER)
115#pragma warning( pop )
116#endif
117}
118
119static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
120 mbedtls_ct_uint_t if1,
121 mbedtls_ct_uint_t if0)
122{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100123 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100124 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100125 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100126}
127
128static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
129{
130 /* Ensure that the compiler cannot optimise the following operations over x and y,
131 * even if it knows the value of x and y.
132 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100133 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100134 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
135 /*
136 * Check if the most significant bits (MSB) of the operands are different.
137 * cond is true iff the MSBs differ.
138 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100139 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100140
141 /*
142 * If the MSB are the same then the difference x-y will be negative (and
143 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
144 *
145 * If the MSB are different, then the operand with the MSB of 1 is the
146 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
147 * the MSB of y is 0.)
148 */
149
150 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100151 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100152
153 // Extract only the MSB of ret
154 ret = ret >> (MBEDTLS_CT_SIZE - 1);
155
156 // Convert to a condition (i.e., all bits set iff non-zero)
157 return mbedtls_ct_bool(ret);
158}
159
160static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
161{
162 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100163 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100164
165 /* all ones if x != y, 0 otherwise */
166 return mbedtls_ct_bool(diff);
167}
168
169static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
170 unsigned char high,
171 unsigned char c,
172 unsigned char t)
173{
174 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
175 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
176
177 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
178 unsigned low_mask = ((unsigned) co - low) >> 8;
179 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
180 unsigned high_mask = ((unsigned) high - co) >> 8;
181
182 return (unsigned char) (~(low_mask | high_mask)) & to;
183}
184
185
186/* ============================================================================
187 * Everything below here is trivial wrapper functions
188 */
189
Dave Rodgman40a41d02023-05-17 11:59:56 +0100190static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
191 size_t if1,
192 size_t if0)
193{
194 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
195}
196
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100197static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100198 unsigned if1,
199 unsigned if0)
200{
201 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
202}
203
204#if defined(MBEDTLS_BIGNUM_C)
205
Dave Rodgman585f7f72023-05-17 17:45:33 +0100206static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
207 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100208 mbedtls_mpi_uint if0)
209{
210 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
211 (mbedtls_ct_uint_t) if1,
212 (mbedtls_ct_uint_t) if0);
213}
214
215#endif
216
217static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1)
218{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100219 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100220}
221
222static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
223{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100224 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100225}
226
227#if defined(MBEDTLS_BIGNUM_C)
228
229static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
230 mbedtls_mpi_uint if1)
231{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100232 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100233}
234
235#endif /* MBEDTLS_BIGNUM_C */
236
Dave Rodgman585f7f72023-05-17 17:45:33 +0100237static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
238 mbedtls_ct_uint_t y)
239{
240 return ~mbedtls_ct_bool_ne(x, y);
241}
242
Dave Rodgman40a41d02023-05-17 11:59:56 +0100243static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
244 mbedtls_ct_uint_t y)
245{
246 return mbedtls_ct_bool_lt(y, x);
247}
248
249static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
250 mbedtls_ct_uint_t y)
251{
252 return ~mbedtls_ct_bool_lt(x, y);
253}
254
255static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
256 mbedtls_ct_uint_t y)
257{
258 return ~mbedtls_ct_bool_gt(x, y);
259}
260
261static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
262 mbedtls_ct_condition_t y)
263{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100264 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100265}
266
267static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
268 mbedtls_ct_condition_t y)
269{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100270 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100271}
272
273static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
274 mbedtls_ct_condition_t y)
275{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100276 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100277}
278
279static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
280{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100281 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100282}
283
284#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */