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,
33 * unsigned int lt = constant_time_lt(a, b);
34 * c = constant_time_select(lt, a, b);
38 * Returns the given value with the MSB copied to all the other
39 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
40 * However, this is not ensured by the C standard so you may need to
41 * replace this with something else on odd CPUs.
43 static ossl_inline unsigned int constant_time_msb(unsigned int a);
46 * Returns 0xff..f if a < b and 0 otherwise.
48 static ossl_inline unsigned int constant_time_lt(unsigned int a,
50 /* Convenience method for getting an 8-bit mask. */
51 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
55 * Returns 0xff..f if a >= b and 0 otherwise.
57 static ossl_inline unsigned int constant_time_ge(unsigned int a,
59 /* Convenience method for getting an 8-bit mask. */
60 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
64 * Returns 0xff..f if a == 0 and 0 otherwise.
66 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
67 /* Convenience method for getting an 8-bit mask. */
68 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
71 * Returns 0xff..f if a == b and 0 otherwise.
73 static ossl_inline unsigned int constant_time_eq(unsigned int a,
75 /* Convenience method for getting an 8-bit mask. */
76 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
78 /* Signed integers. */
79 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
80 /* Convenience method for getting an 8-bit mask. */
81 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
84 * Returns (mask & a) | (~mask & b).
86 * When |mask| is all 1s or all 0s (as returned by the methods above),
87 * the select methods return either |a| (if |mask| is nonzero) or |b|
88 * (if |mask| is zero).
90 static ossl_inline unsigned int constant_time_select(unsigned int mask,
93 /* Convenience method for unsigned chars. */
94 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
97 /* Convenience method for signed integers. */
98 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
101 static ossl_inline unsigned int constant_time_msb(unsigned int a)
103 return 0 - (a >> (sizeof(a) * 8 - 1));
106 static ossl_inline size_t constant_time_msb_s(size_t a)
108 return 0 - (a >> (sizeof(a) * 8 - 1));
111 static ossl_inline unsigned int constant_time_lt(unsigned int a,
114 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
117 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
119 return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
122 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
125 return (unsigned char)(constant_time_lt(a, b));
128 static ossl_inline unsigned int constant_time_ge(unsigned int a,
131 return ~constant_time_lt(a, b);
134 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
136 return ~constant_time_lt_s(a, b);
139 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
142 return (unsigned char)(constant_time_ge(a, b));
145 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
147 return (unsigned char)(constant_time_ge_s(a, b));
150 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
152 return constant_time_msb(~a & (a - 1));
155 static ossl_inline size_t constant_time_is_zero_s(size_t a)
157 return constant_time_msb_s(~a & (a - 1));
160 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
162 return (unsigned char)(constant_time_is_zero(a));
165 static ossl_inline unsigned int constant_time_eq(unsigned int a,
168 return constant_time_is_zero(a ^ b);
171 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
173 return constant_time_is_zero_s(a ^ b);
176 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
179 return (unsigned char)(constant_time_eq(a, b));
182 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
184 return (unsigned char)(constant_time_eq_s(a, b));
187 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
189 return constant_time_eq((unsigned)(a), (unsigned)(b));
192 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
194 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
197 static ossl_inline unsigned int constant_time_select(unsigned int mask,
201 return (mask & a) | (~mask & b);
204 static ossl_inline size_t constant_time_select_s(size_t mask,
208 return (mask & a) | (~mask & b);
211 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
215 return (unsigned char)(constant_time_select(mask, a, b));
218 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
221 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
224 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
226 return (int)(constant_time_select((unsigned)mask, (unsigned)(a),
234 #endif /* HEADER_CONSTANT_TIME_LOCL_H */