xref: /openssl/include/internal/constant_time.h (revision 7ed6de99)
1 /*
2  * Copyright 2014-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 OSSL_INTERNAL_CONSTANT_TIME_H
11 # define OSSL_INTERNAL_CONSTANT_TIME_H
12 # pragma once
13 
14 # include <stdlib.h>
15 # include <string.h>
16 # include <openssl/e_os2.h>              /* For 'ossl_inline' */
17 
18 /*-
19  * The boolean methods return a bitmask of all ones (0xff...f) for true
20  * and 0 for false. This is useful for choosing a value based on the result
21  * of a conditional in constant time. For example,
22  *      if (a < b) {
23  *        c = a;
24  *      } else {
25  *        c = b;
26  *      }
27  * can be written as
28  *      unsigned int lt = constant_time_lt(a, b);
29  *      c = constant_time_select(lt, a, b);
30  */
31 
32 /* Returns the given value with the MSB copied to all the other bits. */
33 static ossl_inline unsigned int constant_time_msb(unsigned int a);
34 /* Convenience method for uint32_t. */
35 static ossl_inline uint32_t constant_time_msb_32(uint32_t 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 /* Convenience method for getting a 32-bit mask. */
60 static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a);
61 
62 /* Returns 0xff..f if a == b and 0 otherwise. */
63 static ossl_inline unsigned int constant_time_eq(unsigned int a,
64                                                  unsigned int b);
65 /* Convenience method for getting an 8-bit mask. */
66 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
67                                                     unsigned int b);
68 /* Signed integers. */
69 static ossl_inline unsigned int constant_time_eq_int(int a, int b);
70 /* Convenience method for getting an 8-bit mask. */
71 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
72 
73 /*-
74  * Returns (mask & a) | (~mask & b).
75  *
76  * When |mask| is all 1s or all 0s (as returned by the methods above),
77  * the select methods return either |a| (if |mask| is nonzero) or |b|
78  * (if |mask| is zero).
79  */
80 static ossl_inline unsigned int constant_time_select(unsigned int mask,
81                                                      unsigned int a,
82                                                      unsigned int b);
83 /* Convenience method for unsigned chars. */
84 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
85                                                         unsigned char a,
86                                                         unsigned char b);
87 
88 /* Convenience method for uint32_t. */
89 static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
90                                                     uint32_t b);
91 
92 /* Convenience method for uint64_t. */
93 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
94                                                     uint64_t b);
95 /* Convenience method for signed integers. */
96 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
97                                                 int b);
98 
99 
constant_time_msb(unsigned int a)100 static ossl_inline unsigned int constant_time_msb(unsigned int a)
101 {
102     return 0 - (a >> (sizeof(a) * 8 - 1));
103 }
104 
105 
constant_time_msb_32(uint32_t a)106 static ossl_inline uint32_t constant_time_msb_32(uint32_t a)
107 {
108     return 0 - (a >> 31);
109 }
110 
constant_time_msb_64(uint64_t a)111 static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
112 {
113     return 0 - (a >> 63);
114 }
115 
constant_time_msb_s(size_t a)116 static ossl_inline size_t constant_time_msb_s(size_t a)
117 {
118     return 0 - (a >> (sizeof(a) * 8 - 1));
119 }
120 
constant_time_lt(unsigned int a,unsigned int b)121 static ossl_inline unsigned int constant_time_lt(unsigned int a,
122                                                  unsigned int b)
123 {
124     return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
125 }
126 
constant_time_lt_s(size_t a,size_t b)127 static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
128 {
129     return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
130 }
131 
constant_time_lt_8(unsigned int a,unsigned int b)132 static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
133                                                     unsigned int b)
134 {
135     return (unsigned char)constant_time_lt(a, b);
136 }
137 
constant_time_lt_64(uint64_t a,uint64_t b)138 static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
139 {
140     return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
141 }
142 
143 #ifdef BN_ULONG
value_barrier_bn(BN_ULONG a)144 static ossl_inline BN_ULONG value_barrier_bn(BN_ULONG a)
145 {
146 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
147     BN_ULONG r;
148     __asm__("" : "=r"(r) : "0"(a));
149 #else
150     volatile BN_ULONG r = a;
151 #endif
152     return r;
153 }
154 
constant_time_msb_bn(BN_ULONG a)155 static ossl_inline BN_ULONG constant_time_msb_bn(BN_ULONG a)
156 {
157     return 0 - (a >> (sizeof(a) * 8 - 1));
158 }
159 
constant_time_lt_bn(BN_ULONG a,BN_ULONG b)160 static ossl_inline BN_ULONG constant_time_lt_bn(BN_ULONG a, BN_ULONG b)
161 {
162     return constant_time_msb_bn(a ^ ((a ^ b) | ((a - b) ^ b)));
163 }
164 
constant_time_is_zero_bn(BN_ULONG a)165 static ossl_inline BN_ULONG constant_time_is_zero_bn(BN_ULONG a)
166 {
167     return constant_time_msb_bn(~a & (a - 1));
168 }
169 
constant_time_eq_bn(BN_ULONG a,BN_ULONG b)170 static ossl_inline BN_ULONG constant_time_eq_bn(BN_ULONG a,
171                                                 BN_ULONG b)
172 {
173     return constant_time_is_zero_bn(a ^ b);
174 }
175 
constant_time_select_bn(BN_ULONG mask,BN_ULONG a,BN_ULONG b)176 static ossl_inline BN_ULONG constant_time_select_bn(BN_ULONG mask,
177                                                     BN_ULONG a,
178                                                     BN_ULONG b)
179 {
180     return (value_barrier_bn(mask) & a) | (value_barrier_bn(~mask) & b);
181 }
182 #endif
183 
constant_time_ge(unsigned int a,unsigned int b)184 static ossl_inline unsigned int constant_time_ge(unsigned int a,
185                                                  unsigned int b)
186 {
187     return ~constant_time_lt(a, b);
188 }
189 
constant_time_ge_s(size_t a,size_t b)190 static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
191 {
192     return ~constant_time_lt_s(a, b);
193 }
194 
constant_time_ge_8(unsigned int a,unsigned int b)195 static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
196                                                     unsigned int b)
197 {
198     return (unsigned char)constant_time_ge(a, b);
199 }
200 
constant_time_ge_8_s(size_t a,size_t b)201 static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
202 {
203     return (unsigned char)constant_time_ge_s(a, b);
204 }
205 
constant_time_is_zero(unsigned int a)206 static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
207 {
208     return constant_time_msb(~a & (a - 1));
209 }
210 
constant_time_is_zero_s(size_t a)211 static ossl_inline size_t constant_time_is_zero_s(size_t a)
212 {
213     return constant_time_msb_s(~a & (a - 1));
214 }
215 
constant_time_is_zero_8(unsigned int a)216 static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
217 {
218     return (unsigned char)constant_time_is_zero(a);
219 }
220 
constant_time_is_zero_32(uint32_t a)221 static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)
222 {
223     return constant_time_msb_32(~a & (a - 1));
224 }
225 
constant_time_is_zero_64(uint64_t a)226 static ossl_inline uint64_t constant_time_is_zero_64(uint64_t a)
227 {
228     return constant_time_msb_64(~a & (a - 1));
229 }
230 
constant_time_eq(unsigned int a,unsigned int b)231 static ossl_inline unsigned int constant_time_eq(unsigned int a,
232                                                  unsigned int b)
233 {
234     return constant_time_is_zero(a ^ b);
235 }
236 
constant_time_eq_s(size_t a,size_t b)237 static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
238 {
239     return constant_time_is_zero_s(a ^ b);
240 }
241 
constant_time_eq_8(unsigned int a,unsigned int b)242 static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
243                                                     unsigned int b)
244 {
245     return (unsigned char)constant_time_eq(a, b);
246 }
247 
constant_time_eq_8_s(size_t a,size_t b)248 static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
249 {
250     return (unsigned char)constant_time_eq_s(a, b);
251 }
252 
constant_time_eq_int(int a,int b)253 static ossl_inline unsigned int constant_time_eq_int(int a, int b)
254 {
255     return constant_time_eq((unsigned)(a), (unsigned)(b));
256 }
257 
constant_time_eq_int_8(int a,int b)258 static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
259 {
260     return constant_time_eq_8((unsigned)(a), (unsigned)(b));
261 }
262 
263 /*
264  * Returns the value unmodified, but avoids optimizations.
265  * The barriers prevent the compiler from narrowing down the
266  * possible value range of the mask and ~mask in the select
267  * statements, which avoids the recognition of the select
268  * and turning it into a conditional load or branch.
269  */
value_barrier(unsigned int a)270 static ossl_inline unsigned int value_barrier(unsigned int a)
271 {
272 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
273     unsigned int r;
274     __asm__("" : "=r"(r) : "0"(a));
275 #else
276     volatile unsigned int r = a;
277 #endif
278     return r;
279 }
280 
281 /* Convenience method for uint32_t. */
value_barrier_32(uint32_t a)282 static ossl_inline uint32_t value_barrier_32(uint32_t a)
283 {
284 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
285     uint32_t r;
286     __asm__("" : "=r"(r) : "0"(a));
287 #else
288     volatile uint32_t r = a;
289 #endif
290     return r;
291 }
292 
293 /* Convenience method for uint64_t. */
value_barrier_64(uint64_t a)294 static ossl_inline uint64_t value_barrier_64(uint64_t a)
295 {
296 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
297     uint64_t r;
298     __asm__("" : "=r"(r) : "0"(a));
299 #else
300     volatile uint64_t r = a;
301 #endif
302     return r;
303 }
304 
305 /* Convenience method for size_t. */
value_barrier_s(size_t a)306 static ossl_inline size_t value_barrier_s(size_t a)
307 {
308 #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
309     size_t r;
310     __asm__("" : "=r"(r) : "0"(a));
311 #else
312     volatile size_t r = a;
313 #endif
314     return r;
315 }
316 
constant_time_select(unsigned int mask,unsigned int a,unsigned int b)317 static ossl_inline unsigned int constant_time_select(unsigned int mask,
318                                                      unsigned int a,
319                                                      unsigned int b)
320 {
321     return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
322 }
323 
constant_time_select_s(size_t mask,size_t a,size_t b)324 static ossl_inline size_t constant_time_select_s(size_t mask,
325                                                  size_t a,
326                                                  size_t b)
327 {
328     return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
329 }
330 
constant_time_select_8(unsigned char mask,unsigned char a,unsigned char b)331 static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
332                                                         unsigned char a,
333                                                         unsigned char b)
334 {
335     return (unsigned char)constant_time_select(mask, a, b);
336 }
337 
constant_time_select_int(unsigned int mask,int a,int b)338 static ossl_inline int constant_time_select_int(unsigned int mask, int a,
339                                                 int b)
340 {
341     return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
342 }
343 
constant_time_select_int_s(size_t mask,int a,int b)344 static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
345 {
346     return (int)constant_time_select((unsigned)mask, (unsigned)(a),
347                                       (unsigned)(b));
348 }
349 
constant_time_select_32(uint32_t mask,uint32_t a,uint32_t b)350 static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
351                                                     uint32_t b)
352 {
353     return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
354 }
355 
constant_time_select_64(uint64_t mask,uint64_t a,uint64_t b)356 static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
357                                                     uint64_t b)
358 {
359     return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
360 }
361 
362 /*
363  * mask must be 0xFFFFFFFF or 0x00000000.
364  *
365  * if (mask) {
366  *     uint32_t tmp = *a;
367  *
368  *     *a = *b;
369  *     *b = tmp;
370  * }
371  */
constant_time_cond_swap_32(uint32_t mask,uint32_t * a,uint32_t * b)372 static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
373                                                    uint32_t *b)
374 {
375     uint32_t xor = *a ^ *b;
376 
377     xor &= mask;
378     *a ^= xor;
379     *b ^= xor;
380 }
381 
382 /*
383  * mask must be 0xFFFFFFFF or 0x00000000.
384  *
385  * if (mask) {
386  *     uint64_t tmp = *a;
387  *
388  *     *a = *b;
389  *     *b = tmp;
390  * }
391  */
constant_time_cond_swap_64(uint64_t mask,uint64_t * a,uint64_t * b)392 static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
393                                                    uint64_t *b)
394 {
395     uint64_t xor = *a ^ *b;
396 
397     xor &= mask;
398     *a ^= xor;
399     *b ^= xor;
400 }
401 
402 /*
403  * mask must be 0xFF or 0x00.
404  * "constant time" is per len.
405  *
406  * if (mask) {
407  *     unsigned char tmp[len];
408  *
409  *     memcpy(tmp, a, len);
410  *     memcpy(a, b);
411  *     memcpy(b, tmp);
412  * }
413  */
constant_time_cond_swap_buff(unsigned char mask,unsigned char * a,unsigned char * b,size_t len)414 static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
415                                                      unsigned char *a,
416                                                      unsigned char *b,
417                                                      size_t len)
418 {
419     size_t i;
420     unsigned char tmp;
421 
422     for (i = 0; i < len; i++) {
423         tmp = a[i] ^ b[i];
424         tmp &= mask;
425         a[i] ^= tmp;
426         b[i] ^= tmp;
427     }
428 }
429 
430 /*
431  * table is a two dimensional array of bytes. Each row has rowsize elements.
432  * Copies row number idx into out. rowsize and numrows are not considered
433  * private.
434  */
constant_time_lookup(void * out,const void * table,size_t rowsize,size_t numrows,size_t idx)435 static ossl_inline void constant_time_lookup(void *out,
436                                              const void *table,
437                                              size_t rowsize,
438                                              size_t numrows,
439                                              size_t idx)
440 {
441     size_t i, j;
442     const unsigned char *tablec = (const unsigned char *)table;
443     unsigned char *outc = (unsigned char *)out;
444     unsigned char mask;
445 
446     memset(out, 0, rowsize);
447 
448     /* Note idx may underflow - but that is well defined */
449     for (i = 0; i < numrows; i++, idx--) {
450         mask = (unsigned char)constant_time_is_zero_s(idx);
451         for (j = 0; j < rowsize; j++)
452             *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
453     }
454 }
455 
456 /*
457  * Expected usage pattern is to unconditionally set error and then
458  * wipe it if there was no actual error. |clear| is 1 or 0.
459  */
460 void err_clear_last_constant_time(int clear);
461 
462 #endif                          /* OSSL_INTERNAL_CONSTANT_TIME_H */
463