SHARED_LIBS_LINK_EXTS is no longer used, remove it completely
[oweals/openssl.git] / include / internal / constant_time_locl.h
1 /* crypto/constant_time_locl.h */
2 /*-
3  * Utilities for constant-time cryptography.
4  *
5  * Author: Emilia Kasper (emilia@openssl.org)
6  * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
7  * (Google).
8  * ====================================================================
9  * Copyright (c) 2014 The OpenSSL Project.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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)"
28  *
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
39  * SUCH DAMAGE.
40  *
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.]
45  */
46
47 #ifndef HEADER_CONSTANT_TIME_LOCL_H
48 # define HEADER_CONSTANT_TIME_LOCL_H
49
50 # include <openssl/e_os2.h>              /* For 'ossl_inline' */
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 /*-
57  * The boolean 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,
60  *
61  * if (a < b) {
62  *   c = a;
63  * } else {
64  *   c = b;
65  * }
66  *
67  * can be written as
68  *
69  * unsigned int lt = constant_time_lt(a, b);
70  * c = constant_time_select(lt, a, b);
71  */
72
73 /*
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.
78  */
79 static ossl_inline unsigned int constant_time_msb(unsigned int a);
80
81 /*
82  * Returns 0xff..f if a < b and 0 otherwise.
83  */
84 static ossl_inline unsigned int constant_time_lt(unsigned int a,
85                                                  unsigned int b);
86 /* Convenience method for getting an 8-bit mask. */
87 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
88                                                     unsigned int b);
89
90 /*
91  * Returns 0xff..f if a >= b and 0 otherwise.
92  */
93 static ossl_inline unsigned int constant_time_ge(unsigned int a,
94                                                  unsigned int b);
95 /* Convenience method for getting an 8-bit mask. */
96 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
97                                                     unsigned int b);
98
99 /*
100  * Returns 0xff..f if a == 0 and 0 otherwise.
101  */
102 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
103 /* Convenience method for getting an 8-bit mask. */
104 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
105
106 /*
107  * Returns 0xff..f if a == b and 0 otherwise.
108  */
109 static ossl_inline unsigned int constant_time_eq(unsigned int a,
110                                                  unsigned int b);
111 /* Convenience method for getting an 8-bit mask. */
112 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
113                                                     unsigned int b);
114 /* Signed integers. */
115 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
116 /* Convenience method for getting an 8-bit mask. */
117 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
118
119 /*-
120  * Returns (mask & a) | (~mask & b).
121  *
122  * When |mask| is all 1s or all 0s (as returned by the methods above),
123  * the select methods return either |a| (if |mask| is nonzero) or |b|
124  * (if |mask| is zero).
125  */
126 static ossl_inline unsigned int constant_time_select(unsigned int mask,
127                                                      unsigned int a,
128                                                      unsigned int b);
129 /* Convenience method for unsigned chars. */
130 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
131                                                         unsigned char a,
132                                                         unsigned char b);
133 /* Convenience method for signed integers. */
134 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
135                                                 int b);
136
137 static ossl_inline unsigned int constant_time_msb(unsigned int a)
138 {
139     return 0 - (a >> (sizeof(a) * 8 - 1));
140 }
141
142 static ossl_inline unsigned int constant_time_lt(unsigned int a,
143                                                  unsigned int b)
144 {
145     return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
146 }
147
148 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
149                                                     unsigned int b)
150 {
151     return (unsigned char)(constant_time_lt(a, b));
152 }
153
154 static ossl_inline unsigned int constant_time_ge(unsigned int a,
155                                                  unsigned int b)
156 {
157     return ~constant_time_lt(a, b);
158 }
159
160 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
161                                                     unsigned int b)
162 {
163     return (unsigned char)(constant_time_ge(a, b));
164 }
165
166 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
167 {
168     return constant_time_msb(~a & (a - 1));
169 }
170
171 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
172 {
173     return (unsigned char)(constant_time_is_zero(a));
174 }
175
176 static ossl_inline unsigned int constant_time_eq(unsigned int a,
177                                                  unsigned int b)
178 {
179     return constant_time_is_zero(a ^ b);
180 }
181
182 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
183                                                     unsigned int b)
184 {
185     return (unsigned char)(constant_time_eq(a, b));
186 }
187
188 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
189 {
190     return constant_time_eq((unsigned)(a), (unsigned)(b));
191 }
192
193 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
194 {
195     return constant_time_eq_8((unsigned)(a), (unsigned)(b));
196 }
197
198 static ossl_inline unsigned int constant_time_select(unsigned int mask,
199                                                      unsigned int a,
200                                                      unsigned int b)
201 {
202     return (mask & a) | (~mask & b);
203 }
204
205 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
206                                                         unsigned char a,
207                                                         unsigned char b)
208 {
209     return (unsigned char)(constant_time_select(mask, a, b));
210 }
211
212 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
213                                                 int b)
214 {
215     return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
216 }
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif                          /* HEADER_CONSTANT_TIME_LOCL_H */