Use the NLIMBS macro rather than try and calculate the number of limbs
[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 /* Returns the given value with the MSB copied to all the other bits. */
35 static ossl_inline unsigned int constant_time_msb(unsigned int a);
36 /* Convenience method for uint64_t. */
37 static ossl_inline uint64_t constant_time_msb_64(uint64_t a);
38
39 /* Returns 0xff..f if a < b and 0 otherwise. */
40 static ossl_inline unsigned int constant_time_lt(unsigned int a,
41                                                  unsigned int b);
42 /* Convenience method for getting an 8-bit mask. */
43 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
44                                                     unsigned int b);
45 /* Convenience method for uint64_t. */
46 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b);
47
48 /* Returns 0xff..f if a >= b and 0 otherwise. */
49 static ossl_inline unsigned int constant_time_ge(unsigned int a,
50                                                  unsigned int b);
51 /* Convenience method for getting an 8-bit mask. */
52 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
53                                                     unsigned int b);
54
55 /* Returns 0xff..f if a == 0 and 0 otherwise. */
56 static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
57 /* Convenience method for getting an 8-bit mask. */
58 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
59
60 /* Returns 0xff..f if a == b and 0 otherwise. */
61 static ossl_inline unsigned int constant_time_eq(unsigned int a,
62                                                  unsigned int b);
63 /* Convenience method for getting an 8-bit mask. */
64 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
65                                                     unsigned int b);
66 /* Signed integers. */
67 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
68 /* Convenience method for getting an 8-bit mask. */
69 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
70
71 /*-
72  * Returns (mask & a) | (~mask & b).
73  *
74  * When |mask| is all 1s or all 0s (as returned by the methods above),
75  * the select methods return either |a| (if |mask| is nonzero) or |b|
76  * (if |mask| is zero).
77  */
78 static ossl_inline unsigned int constant_time_select(unsigned int mask,
79                                                      unsigned int a,
80                                                      unsigned int b);
81 /* Convenience method for unsigned chars. */
82 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
83                                                         unsigned char a,
84                                                         unsigned char b);
85 /* Convenience method for uint64_t. */
86 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
87                                                     uint64_t b);
88 /* Convenience method for signed integers. */
89 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
90                                                 int b);
91
92
93 static ossl_inline unsigned int constant_time_msb(unsigned int a)
94 {
95     return 0 - (a >> (sizeof(a) * 8 - 1));
96 }
97
98 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
99 {
100     return 0 - (a >> 63);
101 }
102
103 static ossl_inline size_t constant_time_msb_s(size_t a)
104 {
105     return 0 - (a >> (sizeof(a) * 8 - 1));
106 }
107
108 static ossl_inline unsigned int constant_time_lt(unsigned int a,
109                                                  unsigned int b)
110 {
111     return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
112 }
113
114 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
115 {
116     return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
117 }
118
119 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
120                                                     unsigned int b)
121 {
122     return (unsigned char)constant_time_lt(a, b);
123 }
124
125 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
126 {
127     return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
128 }
129
130 static ossl_inline unsigned int constant_time_ge(unsigned int a,
131                                                  unsigned int b)
132 {
133     return ~constant_time_lt(a, b);
134 }
135
136 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
137 {
138     return ~constant_time_lt_s(a, b);
139 }
140
141 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
142                                                     unsigned int b)
143 {
144     return (unsigned char)constant_time_ge(a, b);
145 }
146
147 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
148 {
149     return (unsigned char)constant_time_ge_s(a, b);
150 }
151
152 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
153 {
154     return constant_time_msb(~a & (a - 1));
155 }
156
157 static ossl_inline size_t constant_time_is_zero_s(size_t a)
158 {
159     return constant_time_msb_s(~a & (a - 1));
160 }
161
162 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
163 {
164     return (unsigned char)constant_time_is_zero(a);
165 }
166
167 static ossl_inline unsigned int constant_time_eq(unsigned int a,
168                                                  unsigned int b)
169 {
170     return constant_time_is_zero(a ^ b);
171 }
172
173 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
174 {
175     return constant_time_is_zero_s(a ^ b);
176 }
177
178 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
179                                                     unsigned int b)
180 {
181     return (unsigned char)constant_time_eq(a, b);
182 }
183
184 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
185 {
186     return (unsigned char)constant_time_eq_s(a, b);
187 }
188
189 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
190 {
191     return constant_time_eq((unsigned)(a), (unsigned)(b));
192 }
193
194 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
195 {
196     return constant_time_eq_8((unsigned)(a), (unsigned)(b));
197 }
198
199 static ossl_inline unsigned int constant_time_select(unsigned int mask,
200                                                      unsigned int a,
201                                                      unsigned int b)
202 {
203     return (mask & a) | (~mask & b);
204 }
205
206 static ossl_inline size_t constant_time_select_s(size_t mask,
207                                                  size_t a,
208                                                  size_t b)
209 {
210     return (mask & a) | (~mask & b);
211 }
212
213 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
214                                                         unsigned char a,
215                                                         unsigned char b)
216 {
217     return (unsigned char)constant_time_select(mask, a, b);
218 }
219
220 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
221                                                 int b)
222 {
223     return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
224 }
225
226 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
227 {
228     return (int)constant_time_select((unsigned)mask, (unsigned)(a),
229                                       (unsigned)(b));
230 }
231
232 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
233                                                     uint64_t b)
234 {
235     return (mask & a) | (~mask & b);
236 }
237
238 #ifdef __cplusplus
239 }
240 #endif
241
242 #endif                          /* HEADER_CONSTANT_TIME_LOCL_H */