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