2 * Copyright 2014-2018 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
15 # include <openssl/e_os2.h> /* For 'ossl_inline' */
18 * The boolean methods return a bitmask of all ones (0xff...f) for true
19 * and 0 for false. This is useful for choosing a value based on the result
20 * of a conditional in constant time. For example,
27 * unsigned int lt = constant_time_lt(a, b);
28 * c = constant_time_select(lt, a, b);
31 /* Returns the given value with the MSB copied to all the other bits. */
32 static ossl_inline unsigned int constant_time_msb(unsigned int a);
33 /* Convenience method for uint32_t. */
34 static ossl_inline uint32_t constant_time_msb_32(uint32_t a);
35 /* Convenience method for uint64_t. */
36 static ossl_inline uint64_t constant_time_msb_64(uint64_t a);
38 /* Returns 0xff..f if a < b and 0 otherwise. */
39 static ossl_inline unsigned int constant_time_lt(unsigned int a,
41 /* Convenience method for getting an 8-bit mask. */
42 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
44 /* Convenience method for uint64_t. */
45 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b);
47 /* Returns 0xff..f if a >= b and 0 otherwise. */
48 static ossl_inline unsigned int constant_time_ge(unsigned int a,
50 /* Convenience method for getting an 8-bit mask. */
51 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
54 /* Returns 0xff..f if a == 0 and 0 otherwise. */
55 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
56 /* Convenience method for getting an 8-bit mask. */
57 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
58 /* Convenience method for getting a 32-bit mask. */
59 static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a);
61 /* Returns 0xff..f if a == b and 0 otherwise. */
62 static ossl_inline unsigned int constant_time_eq(unsigned int a,
64 /* Convenience method for getting an 8-bit mask. */
65 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
67 /* Signed integers. */
68 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
69 /* Convenience method for getting an 8-bit mask. */
70 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
73 * Returns (mask & a) | (~mask & b).
75 * When |mask| is all 1s or all 0s (as returned by the methods above),
76 * the select methods return either |a| (if |mask| is nonzero) or |b|
77 * (if |mask| is zero).
79 static ossl_inline unsigned int constant_time_select(unsigned int mask,
82 /* Convenience method for unsigned chars. */
83 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
87 /* Convenience method for uint32_t. */
88 static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
91 /* Convenience method for uint64_t. */
92 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
94 /* Convenience method for signed integers. */
95 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
99 static ossl_inline unsigned int constant_time_msb(unsigned int a)
101 return 0 - (a >> (sizeof(a) * 8 - 1));
105 static ossl_inline uint32_t constant_time_msb_32(uint32_t a)
107 return 0 - (a >> 31);
110 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
112 return 0 - (a >> 63);
115 static ossl_inline size_t constant_time_msb_s(size_t a)
117 return 0 - (a >> (sizeof(a) * 8 - 1));
120 static ossl_inline unsigned int constant_time_lt(unsigned int a,
123 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
126 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
128 return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
131 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
134 return (unsigned char)constant_time_lt(a, b);
137 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
139 return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
142 static ossl_inline unsigned int constant_time_ge(unsigned int a,
145 return ~constant_time_lt(a, b);
148 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
150 return ~constant_time_lt_s(a, b);
153 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
156 return (unsigned char)constant_time_ge(a, b);
159 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
161 return (unsigned char)constant_time_ge_s(a, b);
164 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
166 return constant_time_msb(~a & (a - 1));
169 static ossl_inline size_t constant_time_is_zero_s(size_t a)
171 return constant_time_msb_s(~a & (a - 1));
174 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
176 return (unsigned char)constant_time_is_zero(a);
179 static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)
181 return constant_time_msb_32(~a & (a - 1));
184 static ossl_inline unsigned int constant_time_eq(unsigned int a,
187 return constant_time_is_zero(a ^ b);
190 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
192 return constant_time_is_zero_s(a ^ b);
195 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
198 return (unsigned char)constant_time_eq(a, b);
201 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
203 return (unsigned char)constant_time_eq_s(a, b);
206 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
208 return constant_time_eq((unsigned)(a), (unsigned)(b));
211 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
213 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
217 * Returns the value unmodified, but avoids optimizations.
218 * The barriers prevent the compiler from narrowing down the
219 * possible value range of the mask and ~mask in the select
220 * statements, which avoids the recognition of the select
221 * and turning it into a conditional load or branch.
223 static ossl_inline unsigned int value_barrier(unsigned int a)
225 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
227 __asm__("" : "=r"(r) : "0"(a));
229 volatile unsigned int r = a;
234 /* Convenience method for uint32_t. */
235 static ossl_inline uint32_t value_barrier_32(uint32_t a)
237 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
239 __asm__("" : "=r"(r) : "0"(a));
241 volatile uint32_t r = a;
246 /* Convenience method for uint64_t. */
247 static ossl_inline uint64_t value_barrier_64(uint64_t a)
249 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
251 __asm__("" : "=r"(r) : "0"(a));
253 volatile uint64_t r = a;
258 /* Convenience method for size_t. */
259 static ossl_inline size_t value_barrier_s(size_t a)
261 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
263 __asm__("" : "=r"(r) : "0"(a));
265 volatile size_t r = a;
270 static ossl_inline unsigned int constant_time_select(unsigned int mask,
274 return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
277 static ossl_inline size_t constant_time_select_s(size_t mask,
281 return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
284 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
288 return (unsigned char)constant_time_select(mask, a, b);
291 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
294 return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
297 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
299 return (int)constant_time_select((unsigned)mask, (unsigned)(a),
303 static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
306 return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
309 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
312 return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
316 * mask must be 0xFFFFFFFF or 0x00000000.
325 static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
328 uint32_t xor = *a ^ *b;
336 * mask must be 0xFFFFFFFF or 0x00000000.
345 static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
348 uint64_t xor = *a ^ *b;
356 * mask must be 0xFF or 0x00.
357 * "constant time" is per len.
360 * unsigned char tmp[len];
362 * memcpy(tmp, a, len);
367 static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
375 for (i = 0; i < len; i++) {
384 * table is a two dimensional array of bytes. Each row has rowsize elements.
385 * Copies row number idx into out. rowsize and numrows are not considered
388 static ossl_inline void constant_time_lookup(void *out,
395 const unsigned char *tablec = (const unsigned char *)table;
396 unsigned char *outc = (unsigned char *)out;
399 memset(out, 0, rowsize);
401 /* Note idx may underflow - but that is well defined */
402 for (i = 0; i < numrows; i++, idx--) {
403 mask = (unsigned char)constant_time_is_zero_s(idx);
404 for (j = 0; j < rowsize; j++)
405 *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
410 * Expected usage pattern is to unconditionally set error and then
411 * wipe it if there was no actual error. |clear| is 1 or 0.
413 void err_clear_last_constant_time(int clear);
415 #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */