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