1 From 6e34f618d37ddbb5854c42e2ad4fca83492fa7b7 Mon Sep 17 00:00:00 2001
2 From: Jouni Malinen <jouni@codeaurora.org>
3 Date: Wed, 27 Feb 2019 18:38:30 +0200
4 Subject: [PATCH 02/14] Add helper functions for constant time operations
6 These functions can be used to help implement constant time operations
7 for various cryptographic operations that must minimize externally
8 observable differences in processing (both in timing and also in
9 internal cache use, etc.).
11 This is related to CVE-2019-9494 and CVE-2019-9495.
13 Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
15 src/utils/const_time.h | 191 +++++++++++++++++++++++++++++++++++++++++++++++++
16 1 file changed, 191 insertions(+)
17 create mode 100644 src/utils/const_time.h
20 +++ b/src/utils/const_time.h
23 + * Helper functions for constant time operations
24 + * Copyright (c) 2019, The Linux Foundation
26 + * This software may be distributed under the terms of the BSD license.
27 + * See README for more details.
29 + * These helper functions can be used to implement logic that needs to minimize
30 + * externally visible differences in execution path by avoiding use of branches,
31 + * avoiding early termination or other time differences, and forcing same memory
32 + * access pattern regardless of values.
39 +#if defined(__clang__)
40 +#define NO_UBSAN_UINT_OVERFLOW \
41 + __attribute__((no_sanitize("unsigned-integer-overflow")))
43 +#define NO_UBSAN_UINT_OVERFLOW
48 + * const_time_fill_msb - Fill all bits with MSB value
50 + * Returns: Value with all the bits set to the MSB of the input val
52 +static inline unsigned int const_time_fill_msb(unsigned int val)
54 + /* Move the MSB to LSB and multiple by -1 to fill in all bits. */
55 + return (val >> (sizeof(val) * 8 - 1)) * ~0U;
59 +/* Returns: -1 if val is zero; 0 if val is not zero */
60 +static inline unsigned int const_time_is_zero(unsigned int val)
61 + NO_UBSAN_UINT_OVERFLOW
63 + /* Set MSB to 1 for 0 and fill rest of bits with the MSB value */
64 + return const_time_fill_msb(~val & (val - 1));
68 +/* Returns: -1 if a == b; 0 if a != b */
69 +static inline unsigned int const_time_eq(unsigned int a, unsigned int b)
71 + return const_time_is_zero(a ^ b);
75 +/* Returns: -1 if a == b; 0 if a != b */
76 +static inline u8 const_time_eq_u8(unsigned int a, unsigned int b)
78 + return (u8) const_time_eq(a, b);
83 + * const_time_eq_bin - Constant time memory comparison
84 + * @a: First buffer to compare
85 + * @b: Second buffer to compare
86 + * @len: Number of octets to compare
87 + * Returns: -1 if buffers are equal, 0 if not
89 + * This function is meant for comparing passwords or hash values where
90 + * difference in execution time or memory access pattern could provide external
91 + * observer information about the location of the difference in the memory
92 + * buffers. The return value does not behave like memcmp(), i.e.,
93 + * const_time_eq_bin() cannot be used to sort items into a defined order. Unlike
94 + * memcmp(), the execution time of const_time_eq_bin() does not depend on the
95 + * contents of the compared memory buffers, but only on the total compared
98 +static inline unsigned int const_time_eq_bin(const void *a, const void *b,
106 + for (i = 0; i < len; i++)
107 + res |= aa[i] ^ bb[i];
109 + return const_time_is_zero(res);
114 + * const_time_select - Constant time unsigned int selection
115 + * @mask: 0 (false) or -1 (true) to identify which value to select
116 + * @true_val: Value to select for the true case
117 + * @false_val: Value to select for the false case
118 + * Returns: true_val if mask == -1, false_val if mask == 0
120 +static inline unsigned int const_time_select(unsigned int mask,
121 + unsigned int true_val,
122 + unsigned int false_val)
124 + return (mask & true_val) | (~mask & false_val);
129 + * const_time_select_int - Constant time int selection
130 + * @mask: 0 (false) or -1 (true) to identify which value to select
131 + * @true_val: Value to select for the true case
132 + * @false_val: Value to select for the false case
133 + * Returns: true_val if mask == -1, false_val if mask == 0
135 +static inline int const_time_select_int(unsigned int mask, int true_val,
138 + return (int) const_time_select(mask, (unsigned int) true_val,
139 + (unsigned int) false_val);
144 + * const_time_select_u8 - Constant time u8 selection
145 + * @mask: 0 (false) or -1 (true) to identify which value to select
146 + * @true_val: Value to select for the true case
147 + * @false_val: Value to select for the false case
148 + * Returns: true_val if mask == -1, false_val if mask == 0
150 +static inline u8 const_time_select_u8(u8 mask, u8 true_val, u8 false_val)
152 + return (u8) const_time_select(mask, true_val, false_val);
157 + * const_time_select_s8 - Constant time s8 selection
158 + * @mask: 0 (false) or -1 (true) to identify which value to select
159 + * @true_val: Value to select for the true case
160 + * @false_val: Value to select for the false case
161 + * Returns: true_val if mask == -1, false_val if mask == 0
163 +static inline s8 const_time_select_s8(u8 mask, s8 true_val, s8 false_val)
165 + return (s8) const_time_select(mask, (unsigned int) true_val,
166 + (unsigned int) false_val);
171 + * const_time_select_bin - Constant time binary buffer selection copy
172 + * @mask: 0 (false) or -1 (true) to identify which value to copy
173 + * @true_val: Buffer to copy for the true case
174 + * @false_val: Buffer to copy for the false case
175 + * @len: Number of octets to copy
176 + * @dst: Destination buffer for the copy
178 + * This function copies the specified buffer into the destination buffer using
179 + * operations with identical memory access pattern regardless of which buffer
182 +static inline void const_time_select_bin(u8 mask, const u8 *true_val,
183 + const u8 *false_val, size_t len,
188 + for (i = 0; i < len; i++)
189 + dst[i] = const_time_select_u8(mask, true_val[i], false_val[i]);
193 +static inline int const_time_memcmp(const void *a, const void *b, size_t len)
204 + diff = (int) aa[len] - (int) bb[len];
205 + mask = const_time_is_zero((unsigned int) diff);
206 + res = const_time_select_int(mask, res, diff);
212 +#endif /* CONST_TIME_H */