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);
35 * Returns the given value with the MSB copied to all the other
36 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
37 * However, this is not ensured by the C standard so you may need to
38 * replace this with something else on odd CPUs.
40 static ossl_inline unsigned int constant_time_msb(unsigned int a);
41 /* Convenience method for uint64_t. */
42 static ossl_inline uint64_t constant_time_msb_64(uint64_t a);
44 /* Returns 0xff..f if a < b and 0 otherwise. */
45 static ossl_inline unsigned int constant_time_lt(unsigned int a,
47 /* Convenience method for getting an 8-bit mask. */
48 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
50 /* Convenience method for uint64_t. */
51 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b);
53 /* Returns 0xff..f if a >= b and 0 otherwise. */
54 static ossl_inline unsigned int constant_time_ge(unsigned int a,
56 /* Convenience method for getting an 8-bit mask. */
57 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
60 /* Returns 0xff..f if a == 0 and 0 otherwise. */
61 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
62 /* Convenience method for getting an 8-bit mask. */
63 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int 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,
90 /* Convenience method for uint64_t. */
91 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
93 /* Convenience method for signed integers. */
94 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
98 static ossl_inline unsigned int constant_time_msb(unsigned int a)
100 return 0 - (a >> (sizeof(a) * 8 - 1));
103 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
105 return 0 - (a >> 63);
108 static ossl_inline size_t constant_time_msb_s(size_t a)
110 return 0 - (a >> (sizeof(a) * 8 - 1));
113 static ossl_inline unsigned int constant_time_lt(unsigned int a,
116 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
119 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
121 return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
124 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
127 return (unsigned char)(constant_time_lt(a, b));
130 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
132 return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
135 static ossl_inline unsigned int constant_time_ge(unsigned int a,
138 return ~constant_time_lt(a, b);
141 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
143 return ~constant_time_lt_s(a, b);
146 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
149 return (unsigned char)(constant_time_ge(a, b));
152 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
154 return (unsigned char)(constant_time_ge_s(a, b));
157 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
159 return constant_time_msb(~a & (a - 1));
162 static ossl_inline size_t constant_time_is_zero_s(size_t a)
164 return constant_time_msb_s(~a & (a - 1));
167 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
169 return (unsigned char)(constant_time_is_zero(a));
172 static ossl_inline unsigned int constant_time_eq(unsigned int a,
175 return constant_time_is_zero(a ^ b);
178 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
180 return constant_time_is_zero_s(a ^ b);
183 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
186 return (unsigned char)(constant_time_eq(a, b));
189 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
191 return (unsigned char)(constant_time_eq_s(a, b));
194 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
196 return constant_time_eq((unsigned)(a), (unsigned)(b));
199 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
201 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
204 static ossl_inline unsigned int constant_time_select(unsigned int mask,
208 return (mask & a) | (~mask & b);
211 static ossl_inline size_t constant_time_select_s(size_t mask,
215 return (mask & a) | (~mask & b);
218 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
222 return (unsigned char)(constant_time_select(mask, a, b));
225 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
228 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
231 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
233 return (int)(constant_time_select((unsigned)mask, (unsigned)(a),
237 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
240 return (mask & a) | (~mask & b);
247 #endif /* HEADER_CONSTANT_TIME_LOCL_H */