Fix reseeding issues of the public RAND_DRBG
[openssl.git] / include / internal / constant_time_locl.h
1 /*
2  * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #ifndef HEADER_CONSTANT_TIME_LOCL_H
11 # define HEADER_CONSTANT_TIME_LOCL_H
12
13 # include <stdlib.h>
14 # include <openssl/e_os2.h>              /* For 'ossl_inline' */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 /*-
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,
24  *      if (a < b) {
25  *        c = a;
26  *      } else {
27  *        c = b;
28  *      }
29  * can be written as
30  *      unsigned int lt = constant_time_lt(a, b);
31  *      c = constant_time_select(lt, a, b);
32  */
33
34 /*
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.
39  */
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);
43
44 /* Returns 0xff..f if a < b and 0 otherwise. */
45 static ossl_inline unsigned int constant_time_lt(unsigned int a,
46                                                  unsigned int b);
47 /* Convenience method for getting an 8-bit mask. */
48 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
49                                                     unsigned int b);
50 /* Convenience method for uint64_t. */
51 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b);
52
53 /* Returns 0xff..f if a >= b and 0 otherwise. */
54 static ossl_inline unsigned int constant_time_ge(unsigned int a,
55                                                  unsigned int b);
56 /* Convenience method for getting an 8-bit mask. */
57 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
58                                                     unsigned int b);
59
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);
64
65 /* Returns 0xff..f if a == b and 0 otherwise. */
66 static ossl_inline unsigned int constant_time_eq(unsigned int a,
67                                                  unsigned int b);
68 /* Convenience method for getting an 8-bit mask. */
69 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
70                                                     unsigned int b);
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);
75
76 /*-
77  * Returns (mask & a) | (~mask & b).
78  *
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).
82  */
83 static ossl_inline unsigned int constant_time_select(unsigned int mask,
84                                                      unsigned int a,
85                                                      unsigned int b);
86 /* Convenience method for unsigned chars. */
87 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
88                                                         unsigned char a,
89                                                         unsigned char b);
90 /* Convenience method for uint64_t. */
91 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
92                                                     uint64_t b);
93 /* Convenience method for signed integers. */
94 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
95                                                 int b);
96
97
98 static ossl_inline unsigned int constant_time_msb(unsigned int a)
99 {
100     return 0 - (a >> (sizeof(a) * 8 - 1));
101 }
102
103 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
104 {
105     return 0 - (a >> 63);
106 }
107
108 static ossl_inline size_t constant_time_msb_s(size_t a)
109 {
110     return 0 - (a >> (sizeof(a) * 8 - 1));
111 }
112
113 static ossl_inline unsigned int constant_time_lt(unsigned int a,
114                                                  unsigned int b)
115 {
116     return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
117 }
118
119 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
120 {
121     return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
122 }
123
124 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
125                                                     unsigned int b)
126 {
127     return (unsigned char)(constant_time_lt(a, b));
128 }
129
130 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
131 {
132     return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
133 }
134
135 static ossl_inline unsigned int constant_time_ge(unsigned int a,
136                                                  unsigned int b)
137 {
138     return ~constant_time_lt(a, b);
139 }
140
141 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
142 {
143     return ~constant_time_lt_s(a, b);
144 }
145
146 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
147                                                     unsigned int b)
148 {
149     return (unsigned char)(constant_time_ge(a, b));
150 }
151
152 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
153 {
154     return (unsigned char)(constant_time_ge_s(a, b));
155 }
156
157 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
158 {
159     return constant_time_msb(~a & (a - 1));
160 }
161
162 static ossl_inline size_t constant_time_is_zero_s(size_t a)
163 {
164     return constant_time_msb_s(~a & (a - 1));
165 }
166
167 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
168 {
169     return (unsigned char)(constant_time_is_zero(a));
170 }
171
172 static ossl_inline unsigned int constant_time_eq(unsigned int a,
173                                                  unsigned int b)
174 {
175     return constant_time_is_zero(a ^ b);
176 }
177
178 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
179 {
180     return constant_time_is_zero_s(a ^ b);
181 }
182
183 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
184                                                     unsigned int b)
185 {
186     return (unsigned char)(constant_time_eq(a, b));
187 }
188
189 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
190 {
191     return (unsigned char)(constant_time_eq_s(a, b));
192 }
193
194 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
195 {
196     return constant_time_eq((unsigned)(a), (unsigned)(b));
197 }
198
199 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
200 {
201     return constant_time_eq_8((unsigned)(a), (unsigned)(b));
202 }
203
204 static ossl_inline unsigned int constant_time_select(unsigned int mask,
205                                                      unsigned int a,
206                                                      unsigned int b)
207 {
208     return (mask & a) | (~mask & b);
209 }
210
211 static ossl_inline size_t constant_time_select_s(size_t mask,
212                                                  size_t a,
213                                                  size_t b)
214 {
215     return (mask & a) | (~mask & b);
216 }
217
218 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
219                                                         unsigned char a,
220                                                         unsigned char b)
221 {
222     return (unsigned char)(constant_time_select(mask, a, b));
223 }
224
225 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
226                                                 int b)
227 {
228     return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
229 }
230
231 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
232 {
233     return (int)(constant_time_select((unsigned)mask, (unsigned)(a),
234                                       (unsigned)(b)));
235 }
236
237 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
238                                                     uint64_t b)
239 {
240     return (mask & a) | (~mask & b);
241 }
242
243 #ifdef __cplusplus
244 }
245 #endif
246
247 #endif                          /* HEADER_CONSTANT_TIME_LOCL_H */