2 * Utilities for constant-time cryptography.
4 * Author: Emilia Kasper (emilia@openssl.org)
5 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
7 * ====================================================================
8 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * "This product includes cryptographic software written by
21 * Eric Young (eay@cryptsoft.com)"
22 * The word 'cryptographic' can be left out if the rouines from the library
23 * being used are not cryptographic related :-).
24 * 4. If you include any Windows specific code (or a derivative thereof) from
25 * the apps directory (application code) you must include an acknowledgement:
26 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
28 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * The licence and distribution terms for any publically available version or
41 * derivative of this code cannot be changed. i.e. this code cannot simply be
42 * copied and put under another distribution licence
43 * [including the GNU Public Licence.]
46 #ifndef HEADER_CONSTANT_TIME_LOCL_H
47 # define HEADER_CONSTANT_TIME_LOCL_H
49 # include <openssl/e_os2.h> /* For 'ossl_inline' */
56 * The boolean methods return a bitmask of all ones (0xff...f) for true
57 * and 0 for false. This is useful for choosing a value based on the result
58 * of a conditional in constant time. For example,
68 * unsigned int lt = constant_time_lt(a, b);
69 * c = constant_time_select(lt, a, b);
73 * Returns the given value with the MSB copied to all the other
74 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
75 * However, this is not ensured by the C standard so you may need to
76 * replace this with something else on odd CPUs.
78 static ossl_inline unsigned int constant_time_msb(unsigned int a);
81 * Returns 0xff..f if a < b and 0 otherwise.
83 static ossl_inline unsigned int constant_time_lt(unsigned int a,
85 /* Convenience method for getting an 8-bit mask. */
86 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
90 * Returns 0xff..f if a >= b and 0 otherwise.
92 static ossl_inline unsigned int constant_time_ge(unsigned int a,
94 /* Convenience method for getting an 8-bit mask. */
95 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
99 * Returns 0xff..f if a == 0 and 0 otherwise.
101 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
102 /* Convenience method for getting an 8-bit mask. */
103 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
106 * Returns 0xff..f if a == b and 0 otherwise.
108 static ossl_inline unsigned int constant_time_eq(unsigned int a,
110 /* Convenience method for getting an 8-bit mask. */
111 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
113 /* Signed integers. */
114 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
115 /* Convenience method for getting an 8-bit mask. */
116 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
119 * Returns (mask & a) | (~mask & b).
121 * When |mask| is all 1s or all 0s (as returned by the methods above),
122 * the select methods return either |a| (if |mask| is nonzero) or |b|
123 * (if |mask| is zero).
125 static ossl_inline unsigned int constant_time_select(unsigned int mask,
128 /* Convenience method for unsigned chars. */
129 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
132 /* Convenience method for signed integers. */
133 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
136 static ossl_inline unsigned int constant_time_msb(unsigned int a)
138 return 0 - (a >> (sizeof(a) * 8 - 1));
141 static ossl_inline unsigned int constant_time_lt(unsigned int a,
144 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
147 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
150 return (unsigned char)(constant_time_lt(a, b));
153 static ossl_inline unsigned int constant_time_ge(unsigned int a,
156 return ~constant_time_lt(a, b);
159 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
162 return (unsigned char)(constant_time_ge(a, b));
165 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
167 return constant_time_msb(~a & (a - 1));
170 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
172 return (unsigned char)(constant_time_is_zero(a));
175 static ossl_inline unsigned int constant_time_eq(unsigned int a,
178 return constant_time_is_zero(a ^ b);
181 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
184 return (unsigned char)(constant_time_eq(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 unsigned char constant_time_select_8(unsigned char mask,
208 return (unsigned char)(constant_time_select(mask, a, b));
211 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
214 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
221 #endif /* HEADER_CONSTANT_TIME_LOCL_H */