2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 HEADER_CONSTANT_TIME_LOCL_H
11 # define HEADER_CONSTANT_TIME_LOCL_H
14 # include <openssl/e_os2.h> /* For 'ossl_inline' */
21 * The boolean methods return a bitmask of all ones (0xff...f) for true
22 * and 0 for false. This is useful for choosing a value based on the result
23 * of a conditional in constant time. For example,
30 * unsigned int lt = constant_time_lt(a, b);
31 * c = constant_time_select(lt, a, b);
34 /* Returns the given value with the MSB copied to all the other bits. */
35 static ossl_inline unsigned int constant_time_msb(unsigned int 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);
60 /* Returns 0xff..f if a == b and 0 otherwise. */
61 static ossl_inline unsigned int constant_time_eq(unsigned int a,
63 /* Convenience method for getting an 8-bit mask. */
64 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
66 /* Signed integers. */
67 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
68 /* Convenience method for getting an 8-bit mask. */
69 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
72 * Returns (mask & a) | (~mask & b).
74 * When |mask| is all 1s or all 0s (as returned by the methods above),
75 * the select methods return either |a| (if |mask| is nonzero) or |b|
76 * (if |mask| is zero).
78 static ossl_inline unsigned int constant_time_select(unsigned int mask,
81 /* Convenience method for unsigned chars. */
82 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
85 /* Convenience method for uint64_t. */
86 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
88 /* Convenience method for signed integers. */
89 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
93 static ossl_inline unsigned int constant_time_msb(unsigned int a)
95 return 0 - (a >> (sizeof(a) * 8 - 1));
98 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
100 return 0 - (a >> 63);
103 static ossl_inline size_t constant_time_msb_s(size_t a)
105 return 0 - (a >> (sizeof(a) * 8 - 1));
108 static ossl_inline unsigned int constant_time_lt(unsigned int a,
111 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
114 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
116 return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
119 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
122 return (unsigned char)constant_time_lt(a, b);
125 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
127 return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
130 static ossl_inline unsigned int constant_time_ge(unsigned int a,
133 return ~constant_time_lt(a, b);
136 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
138 return ~constant_time_lt_s(a, b);
141 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
144 return (unsigned char)constant_time_ge(a, b);
147 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
149 return (unsigned char)constant_time_ge_s(a, b);
152 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
154 return constant_time_msb(~a & (a - 1));
157 static ossl_inline size_t constant_time_is_zero_s(size_t a)
159 return constant_time_msb_s(~a & (a - 1));
162 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
164 return (unsigned char)constant_time_is_zero(a);
167 static ossl_inline unsigned int constant_time_eq(unsigned int a,
170 return constant_time_is_zero(a ^ b);
173 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
175 return constant_time_is_zero_s(a ^ b);
178 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
181 return (unsigned char)constant_time_eq(a, b);
184 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
186 return (unsigned char)constant_time_eq_s(a, b);
189 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
191 return constant_time_eq((unsigned)(a), (unsigned)(b));
194 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
196 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
199 static ossl_inline unsigned int constant_time_select(unsigned int mask,
203 return (mask & a) | (~mask & b);
206 static ossl_inline size_t constant_time_select_s(size_t mask,
210 return (mask & a) | (~mask & b);
213 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
217 return (unsigned char)constant_time_select(mask, a, b);
220 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
223 return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
226 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
228 return (int)constant_time_select((unsigned)mask, (unsigned)(a),
232 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
235 return (mask & a) | (~mask & b);
242 #endif /* HEADER_CONSTANT_TIME_LOCL_H */