blob: 91418e58fff9b49ef90cd1fe25fe792636321abb [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)
Dave Rodgman58c80f42023-06-12 18:19:46 +010076extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010077#endif
78
Dave Rodgman40a41d02023-05-17 11:59:56 +010079static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
80{
81#if defined(MBEDTLS_CT_ASM)
82 asm volatile ("" : [x] "+r" (x) :);
83 return x;
84#else
Dave Rodgman2894d002023-06-08 17:52:21 +010085 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +010086#endif
87}
88
89/* Convert a number into a condition in constant time. */
90static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
91{
92 /*
93 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
94 *
95 * For some platforms / type sizes, we define assembly to assure this.
96 *
97 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
98 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
99 */
100 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
101#if defined(_MSC_VER)
102 /* MSVC has a warning about unary minus on unsigned, but this is
103 * well-defined and precisely what we want to do here */
104#pragma warning( push )
105#pragma warning( disable : 4146 )
106#endif
107 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
108 (MBEDTLS_CT_SIZE - 1));
109#if defined(_MSC_VER)
110#pragma warning( pop )
111#endif
112}
113
114static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
115 mbedtls_ct_uint_t if1,
116 mbedtls_ct_uint_t if0)
117{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100118 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100119 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100120 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100121}
122
123static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
124{
125 /* Ensure that the compiler cannot optimise the following operations over x and y,
126 * even if it knows the value of x and y.
127 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100128 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100129 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
130 /*
131 * Check if the most significant bits (MSB) of the operands are different.
132 * cond is true iff the MSBs differ.
133 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100134 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100135
136 /*
137 * If the MSB are the same then the difference x-y will be negative (and
138 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
139 *
140 * If the MSB are different, then the operand with the MSB of 1 is the
141 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
142 * the MSB of y is 0.)
143 */
144
145 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100146 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100147
148 // Extract only the MSB of ret
149 ret = ret >> (MBEDTLS_CT_SIZE - 1);
150
151 // Convert to a condition (i.e., all bits set iff non-zero)
152 return mbedtls_ct_bool(ret);
153}
154
155static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
156{
157 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100158 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100159
160 /* all ones if x != y, 0 otherwise */
161 return mbedtls_ct_bool(diff);
162}
163
164static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
165 unsigned char high,
166 unsigned char c,
167 unsigned char t)
168{
169 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
170 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
171
172 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
173 unsigned low_mask = ((unsigned) co - low) >> 8;
174 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
175 unsigned high_mask = ((unsigned) high - co) >> 8;
176
177 return (unsigned char) (~(low_mask | high_mask)) & to;
178}
179
180
181/* ============================================================================
182 * Everything below here is trivial wrapper functions
183 */
184
Dave Rodgman40a41d02023-05-17 11:59:56 +0100185static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
186 size_t if1,
187 size_t if0)
188{
189 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
190}
191
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100192static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100193 unsigned if1,
194 unsigned if0)
195{
196 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
197}
198
199#if defined(MBEDTLS_BIGNUM_C)
200
Dave Rodgman585f7f72023-05-17 17:45:33 +0100201static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
202 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100203 mbedtls_mpi_uint if0)
204{
205 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
206 (mbedtls_ct_uint_t) if1,
207 (mbedtls_ct_uint_t) if0);
208}
209
210#endif
211
212static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1)
213{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100214 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100215}
216
217static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
218{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100219 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100220}
221
222#if defined(MBEDTLS_BIGNUM_C)
223
224static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
225 mbedtls_mpi_uint if1)
226{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100227 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100228}
229
230#endif /* MBEDTLS_BIGNUM_C */
231
Dave Rodgman585f7f72023-05-17 17:45:33 +0100232static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
233 mbedtls_ct_uint_t y)
234{
235 return ~mbedtls_ct_bool_ne(x, y);
236}
237
Dave Rodgman40a41d02023-05-17 11:59:56 +0100238static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
239 mbedtls_ct_uint_t y)
240{
241 return mbedtls_ct_bool_lt(y, x);
242}
243
244static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
245 mbedtls_ct_uint_t y)
246{
247 return ~mbedtls_ct_bool_lt(x, y);
248}
249
250static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
251 mbedtls_ct_uint_t y)
252{
253 return ~mbedtls_ct_bool_gt(x, y);
254}
255
256static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
257 mbedtls_ct_condition_t y)
258{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100259 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100260}
261
262static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
263 mbedtls_ct_condition_t y)
264{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100265 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100266}
267
268static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(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_not(mbedtls_ct_condition_t x)
275{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100276 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100277}
278
279#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */