Skip to content
0.2.0-dev Download ZIP
514 lines ·19.9 KB Raw
1/**
2 * Constant-time functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#ifndef TF_PSA_CRYPTO_CONSTANT_TIME_IMPL_H
9#define TF_PSA_CRYPTO_CONSTANT_TIME_IMPL_H
10
11#include <stddef.h>
12
13#include "tf_psa_crypto_common.h"
14
15/*
16 * To improve readability of constant_time_internal.h, the static inline
17 * definitions are here, and constant_time_internal.h has only the declarations.
18 *
19 * This results in duplicate declarations of the form:
20 * static inline void f(); // from constant_time_internal.h
21 * static inline void f() { ... } // from constant_time_impl.h
22 * when constant_time_internal.h is included.
23 *
24 * This appears to behave as if the declaration-without-definition was not present
25 * (except for warnings if gcc -Wredundant-decls or similar is used).
26 *
27 * Disable -Wredundant-decls so that gcc does not warn about this. This is re-enabled
28 * at the bottom of this file.
29 */
30#if defined(MBEDTLS_COMPILER_IS_GCC) && (__GNUC__ > 4)
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wredundant-decls"
33#endif
34
35/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
36#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
37 __ARMCC_VERSION >= 6000000)
38#define MBEDTLS_CT_ASM
39#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
40#define MBEDTLS_CT_ARM_ASM
41#elif defined(__aarch64__)
42#define MBEDTLS_CT_AARCH64_ASM
43#elif defined(__amd64__) || defined(__x86_64__)
44#define MBEDTLS_CT_X86_64_ASM
45#elif defined(__i386__)
46#define MBEDTLS_CT_X86_ASM
47#endif
48#endif
49
50#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
51
52
53/* ============================================================================
54 * Core const-time primitives
55 */
56
57/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
58 * based on its value) after this function is called.
59 *
60 * If we are not using assembly, this will be fairly inefficient, so its use
61 * should be minimised.
62 */
63
64#if !defined(MBEDTLS_CT_ASM)
65extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
66#endif
67
68/**
69 * \brief Ensure that a value cannot be known at compile time.
70 *
71 * \param x The value to hide from the compiler.
72 * \return The same value that was passed in, such that the compiler
73 * cannot prove its value (even for calls of the form
74 * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
75 *
76 * \note This is mainly used in constructing mbedtls_ct_condition_t
77 * values and performing operations over them, to ensure that
78 * there is no way for the compiler to ever know anything about
79 * the value of an mbedtls_ct_condition_t.
80 */
81static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
82{
83#if defined(MBEDTLS_CT_ASM)
84 asm volatile ("" : [x] "+r" (x) :);
85 return x;
86#else
87 return x ^ mbedtls_ct_zero;
88#endif
89}
90
91/*
92 * Selecting unified syntax is needed for gcc, and harmless on clang.
93 *
94 * This is needed because on Thumb 1, condition flags are always set, so
95 * e.g. "negs" is supported but "neg" is not (on Thumb 2, both exist).
96 *
97 * Under Thumb 1 unified syntax, only the "negs" form is accepted, and
98 * under divided syntax, only the "neg" form is accepted. clang only
99 * supports unified syntax.
100 *
101 * On Thumb 2 and Arm, both compilers are happy with the "s" suffix,
102 * although we don't actually care about setting the flags.
103 *
104 * For old versions of gcc (see #8516 for details), restore divided
105 * syntax afterwards - otherwise old versions of gcc seem to apply
106 * unified syntax globally, which breaks other asm code.
107 */
108#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__thumb__) && !defined(__thumb2__) && \
109 (__GNUC__ < 11) && !defined(__ARM_ARCH_2__)
110#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
111#else
112#define RESTORE_ASM_SYNTAX
113#endif
114
115/* Convert a number into a condition in constant time. */
116static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
117{
118 /*
119 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
120 *
121 * For some platforms / type sizes, we define assembly to assure this.
122 *
123 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
124 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
125 */
126#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
127 mbedtls_ct_uint_t s;
128 asm volatile ("neg %x[s], %x[x] \n\t"
129 "orr %x[x], %x[s], %x[x] \n\t"
130 "asr %x[x], %x[x], 63 \n\t"
131 :
132 [s] "=&r" (s),
133 [x] "+&r" (x)
134 :
135 :
136 );
137 return (mbedtls_ct_condition_t) x;
138#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
139 uint32_t s;
140 asm volatile (".syntax unified \n\t"
141 "negs %[s], %[x] \n\t"
142 "orrs %[x], %[x], %[s] \n\t"
143 "asrs %[x], %[x], #31 \n\t"
144 RESTORE_ASM_SYNTAX
145 :
146 [s] "=&l" (s),
147 [x] "+&l" (x)
148 :
149 :
150 "cc" /* clobbers flag bits */
151 );
152 return (mbedtls_ct_condition_t) x;
153#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
154 uint64_t s;
155 asm volatile ("mov %[x], %[s] \n\t"
156 "neg %[s] \n\t"
157 "or %[x], %[s] \n\t"
158 "sar $63, %[s] \n\t"
159 :
160 [s] "=&a" (s)
161 :
162 [x] "D" (x)
163 :
164 );
165 return (mbedtls_ct_condition_t) s;
166#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
167 uint32_t s;
168 asm volatile ("mov %[x], %[s] \n\t"
169 "neg %[s] \n\t"
170 "or %[s], %[x] \n\t"
171 "sar $31, %[x] \n\t"
172 :
173 [s] "=&c" (s),
174 [x] "+&a" (x)
175 :
176 :
177 );
178 return (mbedtls_ct_condition_t) x;
179#else
180 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
181#if defined(_MSC_VER)
182 /* MSVC has a warning about unary minus on unsigned, but this is
183 * well-defined and precisely what we want to do here */
184#pragma warning( push )
185#pragma warning( disable : 4146 )
186#endif
187 // y is negative (i.e., top bit set) iff x is non-zero
188 mbedtls_ct_int_t y = (-xo) | -(xo >> 1);
189
190 // extract only the sign bit of y so that y == 1 (if x is non-zero) or 0 (if x is zero)
191 y = (((mbedtls_ct_uint_t) y) >> (MBEDTLS_CT_SIZE - 1));
192
193 // -y has all bits set (if x is non-zero), or all bits clear (if x is zero)
194 return (mbedtls_ct_condition_t) (-y);
195#if defined(_MSC_VER)
196#pragma warning( pop )
197#endif
198#endif
199}
200
201static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
202 mbedtls_ct_uint_t if1,
203 mbedtls_ct_uint_t if0)
204{
205#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
206 asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
207 "mvn %x[condition], %x[condition] \n\t"
208 "and %x[condition], %x[condition], %x[if0] \n\t"
209 "orr %x[condition], %x[if1], %x[condition]"
210 :
211 [condition] "+&r" (condition),
212 [if1] "+&r" (if1)
213 :
214 [if0] "r" (if0)
215 :
216 );
217 return (mbedtls_ct_uint_t) condition;
218#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
219 asm volatile (".syntax unified \n\t"
220 "ands %[if1], %[if1], %[condition] \n\t"
221 "mvns %[condition], %[condition] \n\t"
222 "ands %[condition], %[condition], %[if0] \n\t"
223 "orrs %[condition], %[if1], %[condition] \n\t"
224 RESTORE_ASM_SYNTAX
225 :
226 [condition] "+&l" (condition),
227 [if1] "+&l" (if1)
228 :
229 [if0] "l" (if0)
230 :
231 "cc"
232 );
233 return (mbedtls_ct_uint_t) condition;
234#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
235 asm volatile ("and %[condition], %[if1] \n\t"
236 "not %[condition] \n\t"
237 "and %[condition], %[if0] \n\t"
238 "or %[if1], %[if0] \n\t"
239 :
240 [condition] "+&D" (condition),
241 [if1] "+&S" (if1),
242 [if0] "+&a" (if0)
243 :
244 :
245 );
246 return if0;
247#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
248 asm volatile ("and %[condition], %[if1] \n\t"
249 "not %[condition] \n\t"
250 "and %[if0], %[condition] \n\t"
251 "or %[condition], %[if1] \n\t"
252 :
253 [condition] "+&c" (condition),
254 [if1] "+&a" (if1)
255 :
256 [if0] "b" (if0)
257 :
258 );
259 return if1;
260#else
261 mbedtls_ct_condition_t not_cond =
262 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
263 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
264#endif
265}
266
267static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
268{
269#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
270 uint64_t s1;
271 asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
272 "sub %x[x], %x[x], %x[y] \n\t"
273 "bic %x[x], %x[x], %x[s1] \n\t"
274 "and %x[s1], %x[s1], %x[y] \n\t"
275 "orr %x[s1], %x[x], %x[s1] \n\t"
276 "asr %x[x], %x[s1], 63"
277 :
278 [s1] "=&r" (s1),
279 [x] "+&r" (x)
280 :
281 [y] "r" (y)
282 :
283 );
284 return (mbedtls_ct_condition_t) x;
285#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
286 uint32_t s1;
287 asm volatile (
288 ".syntax unified \n\t"
289#if defined(__thumb__) && !defined(__thumb2__)
290 "movs %[s1], %[x] \n\t"
291 "eors %[s1], %[s1], %[y] \n\t"
292#else
293 "eors %[s1], %[x], %[y] \n\t"
294#endif
295 "subs %[x], %[x], %[y] \n\t"
296 "bics %[x], %[x], %[s1] \n\t"
297 "ands %[y], %[s1], %[y] \n\t"
298 "orrs %[x], %[x], %[y] \n\t"
299 "asrs %[x], %[x], #31 \n\t"
300 RESTORE_ASM_SYNTAX
301 :
302 [s1] "=&l" (s1),
303 [x] "+&l" (x),
304 [y] "+&l" (y)
305 :
306 :
307 "cc"
308 );
309 return (mbedtls_ct_condition_t) x;
310#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
311 uint64_t s;
312 asm volatile ("mov %[x], %[s] \n\t"
313 "xor %[y], %[s] \n\t"
314 "sub %[y], %[x] \n\t"
315 "and %[s], %[y] \n\t"
316 "not %[s] \n\t"
317 "and %[s], %[x] \n\t"
318 "or %[y], %[x] \n\t"
319 "sar $63, %[x] \n\t"
320 :
321 [s] "=&a" (s),
322 [x] "+&D" (x),
323 [y] "+&S" (y)
324 :
325 :
326 );
327 return (mbedtls_ct_condition_t) x;
328#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
329 uint32_t s;
330 asm volatile ("mov %[x], %[s] \n\t"
331 "xor %[y], %[s] \n\t"
332 "sub %[y], %[x] \n\t"
333 "and %[s], %[y] \n\t"
334 "not %[s] \n\t"
335 "and %[s], %[x] \n\t"
336 "or %[y], %[x] \n\t"
337 "sar $31, %[x] \n\t"
338 :
339 [s] "=&b" (s),
340 [x] "+&a" (x),
341 [y] "+&c" (y)
342 :
343 :
344 );
345 return (mbedtls_ct_condition_t) x;
346#else
347 /* Ensure that the compiler cannot optimise the following operations over x and y,
348 * even if it knows the value of x and y.
349 */
350 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
351 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
352 /*
353 * Check if the most significant bits (MSB) of the operands are different.
354 * cond is true iff the MSBs differ.
355 */
356 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
357
358 /*
359 * If the MSB are the same then the difference x-y will be negative (and
360 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
361 *
362 * If the MSB are different, then the operand with the MSB of 1 is the
363 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
364 * the MSB of y is 0.)
365 */
366
367 // Select either y, or x - y
368 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
369
370 // Extract only the MSB of ret
371 ret = ret >> (MBEDTLS_CT_SIZE - 1);
372
373 // Convert to a condition (i.e., all bits set iff non-zero)
374 return mbedtls_ct_bool(ret);
375#endif
376}
377
378static inline mbedtls_ct_condition_t mbedtls_ct_uint_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
379{
380 /* diff = 0 if x == y, non-zero otherwise */
381 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
382
383 /* all ones if x != y, 0 otherwise */
384 return mbedtls_ct_bool(diff);
385}
386
387static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
388 unsigned char high,
389 unsigned char c,
390 unsigned char t)
391{
392 const unsigned char co = (unsigned char) mbedtls_ct_compiler_opaque(c);
393 const unsigned char to = (unsigned char) mbedtls_ct_compiler_opaque(t);
394
395 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
396 unsigned low_mask = ((unsigned) co - low) >> 8;
397 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
398 unsigned high_mask = ((unsigned) high - co) >> 8;
399
400 return (unsigned char) (~(low_mask | high_mask)) & to;
401}
402
403/* ============================================================================
404 * Everything below here is trivial wrapper functions
405 */
406
407static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
408 size_t if1,
409 size_t if0)
410{
411 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
412}
413
414static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
415 unsigned if1,
416 unsigned if0)
417{
418 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
419}
420
421static inline mbedtls_ct_condition_t mbedtls_ct_bool_if(mbedtls_ct_condition_t condition,
422 mbedtls_ct_condition_t if1,
423 mbedtls_ct_condition_t if0)
424{
425 return (mbedtls_ct_condition_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1,
426 (mbedtls_ct_uint_t) if0);
427}
428
429static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
430{
431 return (size_t) (condition & if1);
432}
433
434static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
435{
436 return (unsigned) (condition & if1);
437}
438
439static inline mbedtls_ct_condition_t mbedtls_ct_bool_if_else_0(mbedtls_ct_condition_t condition,
440 mbedtls_ct_condition_t if1)
441{
442 return (mbedtls_ct_condition_t) (condition & if1);
443}
444
445static inline int mbedtls_ct_error_if(mbedtls_ct_condition_t condition, int if1, int if0)
446{
447 /* Coverting int -> uint -> int here is safe, because we require if1 and if0 to be
448 * in the range -32767..0, and we require 32-bit int and uint types.
449 *
450 * This means that (0 <= -if0 < INT_MAX), so negating if0 is safe, and similarly for
451 * converting back to int.
452 */
453 return -((int) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) (-if1),
454 (mbedtls_ct_uint_t) (-if0)));
455}
456
457static inline int mbedtls_ct_error_if_else_0(mbedtls_ct_condition_t condition, int if1)
458{
459 return -((int) (condition & (-if1)));
460}
461
462static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
463 mbedtls_ct_uint_t y)
464{
465 return ~mbedtls_ct_uint_ne(x, y);
466}
467
468static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
469 mbedtls_ct_uint_t y)
470{
471 return mbedtls_ct_uint_lt(y, x);
472}
473
474static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
475 mbedtls_ct_uint_t y)
476{
477 return ~mbedtls_ct_uint_lt(x, y);
478}
479
480static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
481 mbedtls_ct_uint_t y)
482{
483 return ~mbedtls_ct_uint_gt(x, y);
484}
485
486static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_condition_t x,
487 mbedtls_ct_condition_t y)
488{
489 return (mbedtls_ct_condition_t) (x ^ y);
490}
491
492static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
493 mbedtls_ct_condition_t y)
494{
495 return (mbedtls_ct_condition_t) (x & y);
496}
497
498static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
499 mbedtls_ct_condition_t y)
500{
501 return (mbedtls_ct_condition_t) (x | y);
502}
503
504static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
505{
506 return (mbedtls_ct_condition_t) (~x);
507}
508
509#if defined(MBEDTLS_COMPILER_IS_GCC) && (__GNUC__ > 4)
510/* Restore warnings for -Wredundant-decls on gcc */
511 #pragma GCC diagnostic pop
512#endif
513
514#endif /* TF_PSA_CRYPTO_CONSTANT_TIME_IMPL_H */