blob: c9e6a83ab1ed5a4912a2986d6b7b280fae318fb0 [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
Dave Rodgman08691672023-07-28 16:17:57 +010040#include "../tests/include/test/constant_flow.h"
Dave Rodgman40a41d02023-05-17 11:59:56 +010041
42/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
43#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
Dave Rodgman983448e2023-07-28 17:30:52 +010044 __ARMCC_VERSION >= 6000000)
Dave Rodgman40a41d02023-05-17 11:59:56 +010045#define MBEDTLS_CT_ASM
46#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
47#define MBEDTLS_CT_ARM_ASM
48#elif defined(__aarch64__)
49#define MBEDTLS_CT_AARCH64_ASM
50#endif
51#endif
52
53#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
54
55
56/* ============================================================================
57 * Core const-time primitives
58 */
59
Dave Rodgman2894d002023-06-08 17:52:21 +010060/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010061 * based on its value) after this function is called.
62 *
63 * If we are not using assembly, this will be fairly inefficient, so its use
64 * should be minimised.
65 */
Dave Rodgman2894d002023-06-08 17:52:21 +010066
67#if !defined(MBEDTLS_CT_ASM)
Dave Rodgman58c80f42023-06-12 18:19:46 +010068extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010069#endif
70
Dave Rodgman40a41d02023-05-17 11:59:56 +010071static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
72{
73#if defined(MBEDTLS_CT_ASM)
Dave Rodgman08691672023-07-28 16:17:57 +010074 /* Prevent false positives from Memsan - otherwise it will report the asm as
75 * accessing secret data. */
76 TEST_CF_PUBLIC(&x, sizeof(x));
77
Dave Rodgman40a41d02023-05-17 11:59:56 +010078 asm volatile ("" : [x] "+r" (x) :);
Dave Rodgman08691672023-07-28 16:17:57 +010079
80 /* Mark the return value as secret. This is needed so that code of the form:
81 *
82 * if (mbedtls_ct_compiler_opaque(secret)) { ... }
83 *
84 * will fail const-flow tests.
85 */
86 TEST_CF_SECRET(&x, sizeof(x));
Dave Rodgman40a41d02023-05-17 11:59:56 +010087 return x;
88#else
Dave Rodgman2894d002023-06-08 17:52:21 +010089 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +010090#endif
91}
92
93/* Convert a number into a condition in constant time. */
94static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
95{
96 /*
97 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
98 *
99 * For some platforms / type sizes, we define assembly to assure this.
100 *
101 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
102 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
103 */
104 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
105#if defined(_MSC_VER)
106 /* MSVC has a warning about unary minus on unsigned, but this is
107 * well-defined and precisely what we want to do here */
108#pragma warning( push )
109#pragma warning( disable : 4146 )
110#endif
111 return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >>
112 (MBEDTLS_CT_SIZE - 1));
113#if defined(_MSC_VER)
114#pragma warning( pop )
115#endif
116}
117
118static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
119 mbedtls_ct_uint_t if1,
120 mbedtls_ct_uint_t if0)
121{
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100122 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100123 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100124 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100125}
126
127static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
128{
129 /* Ensure that the compiler cannot optimise the following operations over x and y,
130 * even if it knows the value of x and y.
131 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100132 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100133 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
134 /*
135 * Check if the most significant bits (MSB) of the operands are different.
136 * cond is true iff the MSBs differ.
137 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100138 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100139
140 /*
141 * If the MSB are the same then the difference x-y will be negative (and
142 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
143 *
144 * If the MSB are different, then the operand with the MSB of 1 is the
145 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
146 * the MSB of y is 0.)
147 */
148
149 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100150 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100151
152 // Extract only the MSB of ret
153 ret = ret >> (MBEDTLS_CT_SIZE - 1);
154
155 // Convert to a condition (i.e., all bits set iff non-zero)
156 return mbedtls_ct_bool(ret);
157}
158
159static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
160{
161 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100162 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100163
164 /* all ones if x != y, 0 otherwise */
165 return mbedtls_ct_bool(diff);
166}
167
168static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
169 unsigned char high,
170 unsigned char c,
171 unsigned char t)
172{
173 const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c);
174 const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t);
175
176 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
177 unsigned low_mask = ((unsigned) co - low) >> 8;
178 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
179 unsigned high_mask = ((unsigned) high - co) >> 8;
180
181 return (unsigned char) (~(low_mask | high_mask)) & to;
182}
183
184
185/* ============================================================================
186 * Everything below here is trivial wrapper functions
187 */
188
Dave Rodgman40a41d02023-05-17 11:59:56 +0100189static 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
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100196static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100197 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
Dave Rodgman585f7f72023-05-17 17:45:33 +0100205static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
206 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100207 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{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100218 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100219}
220
221static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1)
222{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100223 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100224}
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{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100231 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100232}
233
234#endif /* MBEDTLS_BIGNUM_C */
235
Dave Rodgman585f7f72023-05-17 17:45:33 +0100236static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
237 mbedtls_ct_uint_t y)
238{
239 return ~mbedtls_ct_bool_ne(x, y);
240}
241
Dave Rodgman40a41d02023-05-17 11:59:56 +0100242static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
243 mbedtls_ct_uint_t y)
244{
245 return mbedtls_ct_bool_lt(y, x);
246}
247
248static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
249 mbedtls_ct_uint_t y)
250{
251 return ~mbedtls_ct_bool_lt(x, y);
252}
253
254static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
255 mbedtls_ct_uint_t y)
256{
257 return ~mbedtls_ct_bool_gt(x, y);
258}
259
260static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
261 mbedtls_ct_condition_t y)
262{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100263 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100264}
265
266static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
267 mbedtls_ct_condition_t y)
268{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100269 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100270}
271
272static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
273 mbedtls_ct_condition_t y)
274{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100275 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100276}
277
278static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
279{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100280 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100281}
282
283#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */