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
13 # include <openssl/e_os2.h> /* For 'ossl_inline' */
20 * The boolean methods return a bitmask of all ones (0xff...f) for true
21 * and 0 for false. This is useful for choosing a value based on the result
22 * of a conditional in constant time. For example,
32 * unsigned int lt = constant_time_lt(a, b);
33 * c = constant_time_select(lt, a, b);
37 * Returns the given value with the MSB copied to all the other
38 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
39 * However, this is not ensured by the C standard so you may need to
40 * replace this with something else on odd CPUs.
42 static ossl_inline unsigned int constant_time_msb(unsigned int a);
45 * Returns 0xff..f if a < b and 0 otherwise.
47 static ossl_inline unsigned int constant_time_lt(unsigned int a,
49 /* Convenience method for getting an 8-bit mask. */
50 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
54 * Returns 0xff..f if a >= b and 0 otherwise.
56 static ossl_inline unsigned int constant_time_ge(unsigned int a,
58 /* Convenience method for getting an 8-bit mask. */
59 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
63 * Returns 0xff..f if a == 0 and 0 otherwise.
65 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
66 /* Convenience method for getting an 8-bit mask. */
67 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
70 * Returns 0xff..f if a == b and 0 otherwise.
72 static ossl_inline unsigned int constant_time_eq(unsigned int a,
74 /* Convenience method for getting an 8-bit mask. */
75 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
77 /* Signed integers. */
78 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
79 /* Convenience method for getting an 8-bit mask. */
80 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
83 * Returns (mask & a) | (~mask & b).
85 * When |mask| is all 1s or all 0s (as returned by the methods above),
86 * the select methods return either |a| (if |mask| is nonzero) or |b|
87 * (if |mask| is zero).
89 static ossl_inline unsigned int constant_time_select(unsigned int mask,
92 /* Convenience method for unsigned chars. */
93 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
96 /* Convenience method for signed integers. */
97 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));
105 static ossl_inline unsigned int constant_time_lt(unsigned int a,
108 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
111 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
114 return (unsigned char)(constant_time_lt(a, b));
117 static ossl_inline unsigned int constant_time_ge(unsigned int a,
120 return ~constant_time_lt(a, b);
123 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
126 return (unsigned char)(constant_time_ge(a, b));
129 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
131 return constant_time_msb(~a & (a - 1));
134 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
136 return (unsigned char)(constant_time_is_zero(a));
139 static ossl_inline unsigned int constant_time_eq(unsigned int a,
142 return constant_time_is_zero(a ^ b);
145 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
148 return (unsigned char)(constant_time_eq(a, b));
151 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
153 return constant_time_eq((unsigned)(a), (unsigned)(b));
156 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
158 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
161 static ossl_inline unsigned int constant_time_select(unsigned int mask,
165 return (mask & a) | (~mask & b);
168 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
172 return (unsigned char)(constant_time_select(mask, a, b));
175 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
178 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
185 #endif /* HEADER_CONSTANT_TIME_LOCL_H */