1 /* crypto/constant_time_locl.h */
3 * Utilities for constant-time cryptography.
5 * Author: Emilia Kasper (emilia@openssl.org)
6 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
8 * ====================================================================
9 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * "This product includes cryptographic software written by
22 * Eric Young (eay@cryptsoft.com)"
23 * The word 'cryptographic' can be left out if the rouines from the library
24 * being used are not cryptographic related :-).
25 * 4. If you include any Windows specific code (or a derivative thereof) from
26 * the apps directory (application code) you must include an acknowledgement:
27 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
29 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * The licence and distribution terms for any publically available version or
42 * derivative of this code cannot be changed. i.e. this code cannot simply be
43 * copied and put under another distribution licence
44 * [including the GNU Public Licence.]
47 #ifndef HEADER_CONSTANT_TIME_LOCL_H
48 #define HEADER_CONSTANT_TIME_LOCL_H
50 #include "e_os.h" /* For 'inline' */
57 * The following methods return a bitmask of all ones (0xff...f) for true
58 * and 0 for false. This is useful for choosing a value based on the result
59 * of a conditional in constant time. For example,
69 * unsigned int lt = constant_time_lt(a, b);
70 * c = a & lt | b & ~lt;
74 * Returns the given value with the MSB copied to all the other
75 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
76 * However, this is not ensured by the C standard so you may need to
77 * replace this with something else on odd CPUs.
79 static inline unsigned int constant_time_msb(unsigned int a);
82 * Returns 0xff..f if a < b and 0 otherwise.
84 static inline unsigned int constant_time_lt(unsigned int a, unsigned int b);
85 /* Convenience method for getting an 8-bit mask. */
86 static inline unsigned char constant_time_lt_8(unsigned int a, unsigned int b);
89 * Returns 0xff..f if a >= b and 0 otherwise.
91 static inline unsigned int constant_time_ge(unsigned int a, unsigned int b);
92 /* Convenience method for getting an 8-bit mask. */
93 static inline unsigned char constant_time_ge_8(unsigned int a, unsigned int b);
96 * Returns 0xff..f if a == 0 and 0 otherwise.
98 static inline unsigned int constant_time_is_zero(unsigned int a);
99 /* Convenience method for getting an 8-bit mask. */
100 static inline unsigned char constant_time_is_zero_8(unsigned int a);
104 * Returns 0xff..f if a == b and 0 otherwise.
106 static inline unsigned int constant_time_eq(unsigned int a, unsigned int b);
107 /* Convenience method for getting an 8-bit mask. */
108 static inline unsigned char constant_time_eq_8(unsigned int a, unsigned int b);
110 static inline unsigned int constant_time_msb(unsigned int a)
112 return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1));
115 static inline unsigned int constant_time_lt(unsigned int a, unsigned int b)
118 /* Case 1: msb(a) == msb(b). a < b iff the MSB of a - b is set.*/
119 lt = ~(a ^ b) & (a - b);
120 /* Case 2: msb(a) != msb(b). a < b iff the MSB of b is set. */
122 return constant_time_msb(lt);
125 static inline unsigned char constant_time_lt_8(unsigned int a, unsigned int b)
127 return (unsigned char)(constant_time_lt(a, b));
130 static inline unsigned int constant_time_ge(unsigned int a, unsigned int b)
133 /* Case 1: msb(a) == msb(b). a >= b iff the MSB of a - b is not set.*/
134 ge = ~((a ^ b) | (a - b));
135 /* Case 2: msb(a) != msb(b). a >= b iff the MSB of a is set. */
137 return constant_time_msb(ge);
140 static inline unsigned char constant_time_ge_8(unsigned int a, unsigned int b)
142 return (unsigned char)(constant_time_ge(a, b));
145 static inline unsigned int constant_time_is_zero(unsigned int a)
147 return constant_time_msb(~a & (a - 1));
150 static inline unsigned char constant_time_is_zero_8(unsigned int a)
152 return (unsigned char)(constant_time_is_zero(a));
155 static inline unsigned int constant_time_eq(unsigned int a, unsigned int b)
157 return constant_time_is_zero(a ^ b);
160 static inline unsigned char constant_time_eq_8(unsigned int a, unsigned int b)
162 return (unsigned char)(constant_time_eq(a, b));
169 #endif /* HEADER_CONSTANT_TIME_LOCL_H */