blob: f4ad115b986d8cc4185ec2f61b922cd46652acdf [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{
114 mbedtls_ct_condition_t not_mask =
115 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
116 mbedtls_ct_condition_t mask =
117 (mbedtls_ct_condition_t) mbedtls_ct_compiler_opaque(condition);
118 return (mbedtls_ct_uint_t) ((mask & if1) | (not_mask & if0));
119}
120
121static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
122{
123 /* Ensure that the compiler cannot optimise the following operations over x and y,
124 * even if it knows the value of x and y.
125 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100126 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100127 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
128 /*
129 * Check if the most significant bits (MSB) of the operands are different.
130 * cond is true iff the MSBs differ.
131 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100132 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100133
134 /*
135 * If the MSB are the same then the difference x-y will be negative (and
136 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
137 *
138 * If the MSB are different, then the operand with the MSB of 1 is the
139 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
140 * the MSB of y is 0.)
141 */
142
143 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100144 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100145
146 // Extract only the MSB of ret
147 ret = ret >> (MBEDTLS_CT_SIZE - 1);
148
149 // Convert to a condition (i.e., all bits set iff non-zero)
150 return mbedtls_ct_bool(ret);
151}
152
153static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
154{
155 /* diff = 0 if x == y, non-zero otherwise */
156 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ y;
157
158 /* all ones if x != y, 0 otherwise */
159 return mbedtls_ct_bool(diff);
160}
161
162static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
163 unsigned char high,
164 unsigned char c,
165 unsigned char t)
166{
167 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
168 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
169
170 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
171 unsigned low_mask = ((unsigned) co - low) >> 8;
172 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
173 unsigned high_mask = ((unsigned) high - co) >> 8;
174
175 return (unsigned char) (~(low_mask | high_mask)) & to;
176}
177
178
179/* ============================================================================
180 * Everything below here is trivial wrapper functions
181 */
182
183static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
184 mbedtls_ct_uint_t y)
185{
186 return ~mbedtls_ct_bool_ne(x, y);
187}
188
189static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
190 size_t if1,
191 size_t if0)
192{
193 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
194}
195
196static inline unsigned mbedtls_ct_uint_if_new(mbedtls_ct_condition_t condition,
197 unsigned if1,
198 unsigned if0)
199{
200 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
201}
202
203#if defined(MBEDTLS_BIGNUM_C)
204
205static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, \
206 mbedtls_mpi_uint if1, \
207 mbedtls_mpi_uint if0)
208{
209 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
210 (mbedtls_ct_uint_t) if1,
211 (mbedtls_ct_uint_t) if0);
212}
213
214#endif
215
216static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1)
217{
218 return (size_t) (mbedtls_ct_compiler_opaque(condition) & if1);
219}
220
221static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
222{
223 return (unsigned) (mbedtls_ct_compiler_opaque(condition) & if1);
224}
225
226#if defined(MBEDTLS_BIGNUM_C)
227
228static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
229 mbedtls_mpi_uint if1)
230{
231 return (mbedtls_mpi_uint) (mbedtls_ct_compiler_opaque(condition) & if1);
232}
233
234#endif /* MBEDTLS_BIGNUM_C */
235
236static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
237 mbedtls_ct_uint_t y)
238{
239 return mbedtls_ct_bool_lt(y, x);
240}
241
242static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
243 mbedtls_ct_uint_t y)
244{
245 return ~mbedtls_ct_bool_lt(x, y);
246}
247
248static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
249 mbedtls_ct_uint_t y)
250{
251 return ~mbedtls_ct_bool_gt(x, y);
252}
253
254static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
255 mbedtls_ct_condition_t y)
256{
257 return (mbedtls_ct_condition_t) (mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y));
258}
259
260static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
261 mbedtls_ct_condition_t y)
262{
263 return (mbedtls_ct_condition_t) (mbedtls_ct_compiler_opaque(x) & mbedtls_ct_compiler_opaque(y));
264}
265
266static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
267 mbedtls_ct_condition_t y)
268{
269 return (mbedtls_ct_condition_t) (mbedtls_ct_compiler_opaque(x) | mbedtls_ct_compiler_opaque(y));
270}
271
272static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
273{
274 return (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(x));
275}
276
277#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */