2 * Copyright 2014-2021 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 uint64_t. */
46 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b);
48 /* Returns 0xff..f if a >= b and 0 otherwise. */
49 static ossl_inline unsigned int constant_time_ge(unsigned int a,
51 /* Convenience method for getting an 8-bit mask. */
52 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
55 /* Returns 0xff..f if a == 0 and 0 otherwise. */
56 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
57 /* Convenience method for getting an 8-bit mask. */
58 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
59 /* Convenience method for getting a 32-bit mask. */
60 static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a);
62 /* Returns 0xff..f if a == b and 0 otherwise. */
63 static ossl_inline unsigned int constant_time_eq(unsigned int a,
65 /* Convenience method for getting an 8-bit mask. */
66 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
68 /* Signed integers. */
69 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
70 /* Convenience method for getting an 8-bit mask. */
71 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
74 * Returns (mask & a) | (~mask & b).
76 * When |mask| is all 1s or all 0s (as returned by the methods above),
77 * the select methods return either |a| (if |mask| is nonzero) or |b|
78 * (if |mask| is zero).
80 static ossl_inline unsigned int constant_time_select(unsigned int mask,
83 /* Convenience method for unsigned chars. */
84 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
88 /* Convenience method for uint32_t. */
89 static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
92 /* Convenience method for uint64_t. */
93 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
95 /* Convenience method for signed integers. */
96 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
100 static ossl_inline unsigned int constant_time_msb(unsigned int a)
102 return 0 - (a >> (sizeof(a) * 8 - 1));
106 static ossl_inline uint32_t constant_time_msb_32(uint32_t a)
108 return 0 - (a >> 31);
111 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
113 return 0 - (a >> 63);
116 static ossl_inline size_t constant_time_msb_s(size_t a)
118 return 0 - (a >> (sizeof(a) * 8 - 1));
121 static ossl_inline unsigned int constant_time_lt(unsigned int a,
124 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
127 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
129 return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
132 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
135 return (unsigned char)constant_time_lt(a, b);
138 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
140 return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
143 static ossl_inline unsigned int constant_time_ge(unsigned int a,
146 return ~constant_time_lt(a, b);
149 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
151 return ~constant_time_lt_s(a, b);
154 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
157 return (unsigned char)constant_time_ge(a, b);
160 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
162 return (unsigned char)constant_time_ge_s(a, b);
165 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
167 return constant_time_msb(~a & (a - 1));
170 static ossl_inline size_t constant_time_is_zero_s(size_t a)
172 return constant_time_msb_s(~a & (a - 1));
175 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
177 return (unsigned char)constant_time_is_zero(a);
180 static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)
182 return constant_time_msb_32(~a & (a - 1));
185 static ossl_inline unsigned int constant_time_eq(unsigned int a,
188 return constant_time_is_zero(a ^ b);
191 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
193 return constant_time_is_zero_s(a ^ b);
196 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
199 return (unsigned char)constant_time_eq(a, b);
202 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
204 return (unsigned char)constant_time_eq_s(a, b);
207 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
209 return constant_time_eq((unsigned)(a), (unsigned)(b));
212 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
214 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
218 * Returns the value unmodified, but avoids optimizations.
219 * The barriers prevent the compiler from narrowing down the
220 * possible value range of the mask and ~mask in the select
221 * statements, which avoids the recognition of the select
222 * and turning it into a conditional load or branch.
224 static ossl_inline unsigned int value_barrier(unsigned int a)
226 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
228 __asm__("" : "=r"(r) : "0"(a));
230 volatile unsigned int r = a;
235 /* Convenience method for uint32_t. */
236 static ossl_inline uint32_t value_barrier_32(uint32_t a)
238 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
240 __asm__("" : "=r"(r) : "0"(a));
242 volatile uint32_t r = a;
247 /* Convenience method for uint64_t. */
248 static ossl_inline uint64_t value_barrier_64(uint64_t a)
250 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
252 __asm__("" : "=r"(r) : "0"(a));
254 volatile uint64_t r = a;
259 /* Convenience method for size_t. */
260 static ossl_inline size_t value_barrier_s(size_t a)
262 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
264 __asm__("" : "=r"(r) : "0"(a));
266 volatile size_t r = a;
271 static ossl_inline unsigned int constant_time_select(unsigned int mask,
275 return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
278 static ossl_inline size_t constant_time_select_s(size_t mask,
282 return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
285 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
289 return (unsigned char)constant_time_select(mask, a, b);
292 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
295 return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
298 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
300 return (int)constant_time_select((unsigned)mask, (unsigned)(a),
304 static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
307 return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
310 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
313 return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
317 * mask must be 0xFFFFFFFF or 0x00000000.
326 static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
329 uint32_t xor = *a ^ *b;
337 * mask must be 0xFFFFFFFF or 0x00000000.
346 static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
349 uint64_t xor = *a ^ *b;
357 * mask must be 0xFF or 0x00.
358 * "constant time" is per len.
361 * unsigned char tmp[len];
363 * memcpy(tmp, a, len);
368 static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
376 for (i = 0; i < len; i++) {
385 * table is a two dimensional array of bytes. Each row has rowsize elements.
386 * Copies row number idx into out. rowsize and numrows are not considered
389 static ossl_inline void constant_time_lookup(void *out,
396 const unsigned char *tablec = (const unsigned char *)table;
397 unsigned char *outc = (unsigned char *)out;
400 memset(out, 0, rowsize);
402 /* Note idx may underflow - but that is well defined */
403 for (i = 0; i < numrows; i++, idx--) {
404 mask = (unsigned char)constant_time_is_zero_s(idx);
405 for (j = 0; j < rowsize; j++)
406 *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
411 * Expected usage pattern is to unconditionally set error and then
412 * wipe it if there was no actual error. |clear| is 1 or 0.
414 void err_clear_last_constant_time(int clear);
416 #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */