]>
git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/constant_time.h
73219759e0ceeebec6323b40107d5e933ef58e97
2 * Copyright 2014-2025 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_INTERNAL_CONSTANT_TIME_H
11 # define OSSL_INTERNAL_CONSTANT_TIME_H
16 # include <openssl/e_os2.h> /* For 'ossl_inline' */
19 * The boolean methods return a bitmask of all ones (0xff...f) for true
20 * and 0 for false. This is useful for choosing a value based on the result
21 * of a conditional in constant time. For example,
28 * unsigned int lt = constant_time_lt(a, b);
29 * c = constant_time_select(lt, a, b);
32 /* Returns the given value with the MSB copied to all the other bits. */
33 static ossl_inline
unsigned int constant_time_msb(unsigned int a
);
34 /* Convenience method for uint32_t. */
35 static ossl_inline
uint32_t constant_time_msb_32(uint32_t a
);
36 /* Convenience method for uint64_t. */
37 static ossl_inline
uint64_t constant_time_msb_64(uint64_t a
);
39 /* Returns 0xff..f if a < b and 0 otherwise. */
40 static ossl_inline
unsigned int constant_time_lt(unsigned int a
,
42 /* Convenience method for getting an 8-bit mask. */
43 static ossl_inline
unsigned char constant_time_lt_8(unsigned int a
,
45 /* Convenience method for uint32_t. */
46 static ossl_inline
uint32_t constant_time_lt_32(uint32_t a
, uint32_t b
);
48 /* Convenience method for uint64_t. */
49 static ossl_inline
uint64_t constant_time_lt_64(uint64_t a
, uint64_t b
);
51 /* Returns 0xff..f if a >= b and 0 otherwise. */
52 static ossl_inline
unsigned int constant_time_ge(unsigned int a
,
54 /* Convenience method for getting an 8-bit mask. */
55 static ossl_inline
unsigned char constant_time_ge_8(unsigned int a
,
58 /* Returns 0xff..f if a == 0 and 0 otherwise. */
59 static ossl_inline
unsigned int constant_time_is_zero(unsigned int a
);
60 /* Convenience method for getting an 8-bit mask. */
61 static ossl_inline
unsigned char constant_time_is_zero_8(unsigned int a
);
62 /* Convenience method for getting a 32-bit mask. */
63 static ossl_inline
uint32_t constant_time_is_zero_32(uint32_t a
);
65 /* Returns 0xff..f if a == b and 0 otherwise. */
66 static ossl_inline
unsigned int constant_time_eq(unsigned int a
,
68 /* Convenience method for getting an 8-bit mask. */
69 static ossl_inline
unsigned char constant_time_eq_8(unsigned int a
,
71 /* Signed integers. */
72 static ossl_inline
unsigned int constant_time_eq_int(int a
, int b
);
73 /* Convenience method for getting an 8-bit mask. */
74 static ossl_inline
unsigned char constant_time_eq_int_8(int a
, int b
);
77 * Returns (mask & a) | (~mask & b).
79 * When |mask| is all 1s or all 0s (as returned by the methods above),
80 * the select methods return either |a| (if |mask| is nonzero) or |b|
81 * (if |mask| is zero).
83 static ossl_inline
unsigned int constant_time_select(unsigned int mask
,
86 /* Convenience method for unsigned chars. */
87 static ossl_inline
unsigned char constant_time_select_8(unsigned char mask
,
91 /* Convenience method for uint32_t. */
92 static ossl_inline
uint32_t constant_time_select_32(uint32_t mask
, uint32_t a
,
95 /* Convenience method for uint64_t. */
96 static ossl_inline
uint64_t constant_time_select_64(uint64_t mask
, uint64_t a
,
98 /* Convenience method for signed integers. */
99 static ossl_inline
int constant_time_select_int(unsigned int mask
, int a
,
103 static ossl_inline
unsigned int constant_time_msb(unsigned int a
)
105 return 0 - (a
>> (sizeof(a
) * 8 - 1));
109 static ossl_inline
uint32_t constant_time_msb_32(uint32_t a
)
111 return 0 - (a
>> 31);
114 static ossl_inline
uint64_t constant_time_msb_64(uint64_t a
)
116 return 0 - (a
>> 63);
119 static ossl_inline
size_t constant_time_msb_s(size_t a
)
121 return 0 - (a
>> (sizeof(a
) * 8 - 1));
124 static ossl_inline
unsigned int constant_time_lt(unsigned int a
,
127 return constant_time_msb(a
^ ((a
^ b
) | ((a
- b
) ^ b
)));
130 static ossl_inline
size_t constant_time_lt_s(size_t a
, size_t b
)
132 return constant_time_msb_s(a
^ ((a
^ b
) | ((a
- b
) ^ b
)));
135 static ossl_inline
unsigned char constant_time_lt_8(unsigned int a
,
138 return (unsigned char)constant_time_lt(a
, b
);
141 static ossl_inline
uint32_t constant_time_lt_32(uint32_t a
, uint32_t b
)
143 return constant_time_msb_32(a
^ ((a
^ b
) | ((a
- b
) ^ b
)));
146 static ossl_inline
uint64_t constant_time_lt_64(uint64_t a
, uint64_t b
)
148 return constant_time_msb_64(a
^ ((a
^ b
) | ((a
- b
) ^ b
)));
152 static ossl_inline BN_ULONG
value_barrier_bn(BN_ULONG a
)
154 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
156 __asm__("" : "=r"(r
) : "0"(a
));
158 volatile BN_ULONG r
= a
;
163 static ossl_inline BN_ULONG
constant_time_msb_bn(BN_ULONG a
)
165 return 0 - (a
>> (sizeof(a
) * 8 - 1));
168 static ossl_inline BN_ULONG
constant_time_lt_bn(BN_ULONG a
, BN_ULONG b
)
170 return constant_time_msb_bn(a
^ ((a
^ b
) | ((a
- b
) ^ b
)));
173 static ossl_inline BN_ULONG
constant_time_is_zero_bn(BN_ULONG a
)
175 return constant_time_msb_bn(~a
& (a
- 1));
178 static ossl_inline BN_ULONG
constant_time_eq_bn(BN_ULONG a
,
181 return constant_time_is_zero_bn(a
^ b
);
184 static ossl_inline BN_ULONG
constant_time_select_bn(BN_ULONG mask
,
188 return (value_barrier_bn(mask
) & a
) | (value_barrier_bn(~mask
) & b
);
192 static ossl_inline
unsigned int constant_time_ge(unsigned int a
,
195 return ~constant_time_lt(a
, b
);
198 static ossl_inline
size_t constant_time_ge_s(size_t a
, size_t b
)
200 return ~constant_time_lt_s(a
, b
);
203 static ossl_inline
unsigned char constant_time_ge_8(unsigned int a
,
206 return (unsigned char)constant_time_ge(a
, b
);
209 static ossl_inline
unsigned char constant_time_ge_8_s(size_t a
, size_t b
)
211 return (unsigned char)constant_time_ge_s(a
, b
);
214 static ossl_inline
unsigned int constant_time_is_zero(unsigned int a
)
216 return constant_time_msb(~a
& (a
- 1));
219 static ossl_inline
size_t constant_time_is_zero_s(size_t a
)
221 return constant_time_msb_s(~a
& (a
- 1));
224 static ossl_inline
unsigned char constant_time_is_zero_8(unsigned int a
)
226 return (unsigned char)constant_time_is_zero(a
);
229 static ossl_inline
uint32_t constant_time_is_zero_32(uint32_t a
)
231 return constant_time_msb_32(~a
& (a
- 1));
234 static ossl_inline
uint64_t constant_time_is_zero_64(uint64_t a
)
236 return constant_time_msb_64(~a
& (a
- 1));
239 static ossl_inline
unsigned int constant_time_eq(unsigned int a
,
242 return constant_time_is_zero(a
^ b
);
245 static ossl_inline
size_t constant_time_eq_s(size_t a
, size_t b
)
247 return constant_time_is_zero_s(a
^ b
);
250 static ossl_inline
unsigned char constant_time_eq_8(unsigned int a
,
253 return (unsigned char)constant_time_eq(a
, b
);
256 static ossl_inline
unsigned char constant_time_eq_8_s(size_t a
, size_t b
)
258 return (unsigned char)constant_time_eq_s(a
, b
);
261 static ossl_inline
unsigned int constant_time_eq_int(int a
, int b
)
263 return constant_time_eq((unsigned)(a
), (unsigned)(b
));
266 static ossl_inline
unsigned char constant_time_eq_int_8(int a
, int b
)
268 return constant_time_eq_8((unsigned)(a
), (unsigned)(b
));
272 * Returns the value unmodified, but avoids optimizations.
273 * The barriers prevent the compiler from narrowing down the
274 * possible value range of the mask and ~mask in the select
275 * statements, which avoids the recognition of the select
276 * and turning it into a conditional load or branch.
278 static ossl_inline
unsigned int value_barrier(unsigned int a
)
280 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
282 __asm__("" : "=r"(r
) : "0"(a
));
284 volatile unsigned int r
= a
;
289 /* Convenience method for uint32_t. */
290 static ossl_inline
uint32_t value_barrier_32(uint32_t a
)
292 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
294 __asm__("" : "=r"(r
) : "0"(a
));
296 volatile uint32_t r
= a
;
301 /* Convenience method for uint64_t. */
302 static ossl_inline
uint64_t value_barrier_64(uint64_t a
)
304 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
306 __asm__("" : "=r"(r
) : "0"(a
));
308 volatile uint64_t r
= a
;
313 /* Convenience method for size_t. */
314 static ossl_inline
size_t value_barrier_s(size_t a
)
316 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
318 __asm__("" : "=r"(r
) : "0"(a
));
320 volatile size_t r
= a
;
325 /* Convenience method for unsigned char. */
326 static ossl_inline
unsigned char value_barrier_8(unsigned char a
)
328 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
330 __asm__("" : "=r"(r
) : "0"(a
));
332 volatile unsigned char r
= a
;
337 static ossl_inline
unsigned int constant_time_select(unsigned int mask
,
341 return (value_barrier(mask
) & a
) | (value_barrier(~mask
) & b
);
344 static ossl_inline
size_t constant_time_select_s(size_t mask
,
348 return (value_barrier_s(mask
) & a
) | (value_barrier_s(~mask
) & b
);
351 static ossl_inline
unsigned char constant_time_select_8(unsigned char mask
,
355 return (unsigned char)constant_time_select(mask
, a
, b
);
358 static ossl_inline
int constant_time_select_int(unsigned int mask
, int a
,
361 return (int)constant_time_select(mask
, (unsigned)(a
), (unsigned)(b
));
364 static ossl_inline
int constant_time_select_int_s(size_t mask
, int a
, int b
)
366 return (int)constant_time_select((unsigned)mask
, (unsigned)(a
),
370 static ossl_inline
uint32_t constant_time_select_32(uint32_t mask
, uint32_t a
,
373 return (value_barrier_32(mask
) & a
) | (value_barrier_32(~mask
) & b
);
376 static ossl_inline
uint64_t constant_time_select_64(uint64_t mask
, uint64_t a
,
379 return (value_barrier_64(mask
) & a
) | (value_barrier_64(~mask
) & b
);
383 * mask must be 0xFFFFFFFF or 0x00000000.
392 static ossl_inline
void constant_time_cond_swap_32(uint32_t mask
, uint32_t *a
,
395 uint32_t xor = *a
^ *b
;
397 xor &= value_barrier_32(mask
);
403 * mask must be 0xFFFFFFFF or 0x00000000.
412 static ossl_inline
void constant_time_cond_swap_64(uint64_t mask
, uint64_t *a
,
415 uint64_t xor = *a
^ *b
;
417 xor &= value_barrier_64(mask
);
423 * mask must be 0xFF or 0x00.
424 * "constant time" is per len.
427 * unsigned char tmp[len];
429 * memcpy(tmp, a, len);
434 static ossl_inline
void constant_time_cond_swap_buff(unsigned char mask
,
442 for (i
= 0; i
< len
; i
++) {
444 tmp
&= value_barrier_8(mask
);
451 * table is a two dimensional array of bytes. Each row has rowsize elements.
452 * Copies row number idx into out. rowsize and numrows are not considered
455 static ossl_inline
void constant_time_lookup(void *out
,
462 const unsigned char *tablec
= (const unsigned char *)table
;
463 unsigned char *outc
= (unsigned char *)out
;
466 memset(out
, 0, rowsize
);
468 /* Note idx may underflow - but that is well defined */
469 for (i
= 0; i
< numrows
; i
++, idx
--) {
470 mask
= (unsigned char)constant_time_is_zero_s(idx
);
471 for (j
= 0; j
< rowsize
; j
++)
472 *(outc
+ j
) |= constant_time_select_8(mask
, *(tablec
++), 0);
477 * Expected usage pattern is to unconditionally set error and then
478 * wipe it if there was no actual error. |clear| is 1 or 0.
480 void err_clear_last_constant_time(int clear
);
482 #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */