xref: /freebsd/contrib/wpa/src/utils/const_time.h (revision 4bc52338)
1*4bc52338SCy Schubert /*
2*4bc52338SCy Schubert  * Helper functions for constant time operations
3*4bc52338SCy Schubert  * Copyright (c) 2019, The Linux Foundation
4*4bc52338SCy Schubert  *
5*4bc52338SCy Schubert  * This software may be distributed under the terms of the BSD license.
6*4bc52338SCy Schubert  * See README for more details.
7*4bc52338SCy Schubert  *
8*4bc52338SCy Schubert  * These helper functions can be used to implement logic that needs to minimize
9*4bc52338SCy Schubert  * externally visible differences in execution path by avoiding use of branches,
10*4bc52338SCy Schubert  * avoiding early termination or other time differences, and forcing same memory
11*4bc52338SCy Schubert  * access pattern regardless of values.
12*4bc52338SCy Schubert  */
13*4bc52338SCy Schubert 
14*4bc52338SCy Schubert #ifndef CONST_TIME_H
15*4bc52338SCy Schubert #define CONST_TIME_H
16*4bc52338SCy Schubert 
17*4bc52338SCy Schubert 
18*4bc52338SCy Schubert #if defined(__clang__)
19*4bc52338SCy Schubert #define NO_UBSAN_UINT_OVERFLOW \
20*4bc52338SCy Schubert 	__attribute__((no_sanitize("unsigned-integer-overflow")))
21*4bc52338SCy Schubert #else
22*4bc52338SCy Schubert #define NO_UBSAN_UINT_OVERFLOW
23*4bc52338SCy Schubert #endif
24*4bc52338SCy Schubert 
25*4bc52338SCy Schubert 
26*4bc52338SCy Schubert /**
27*4bc52338SCy Schubert  * const_time_fill_msb - Fill all bits with MSB value
28*4bc52338SCy Schubert  * @val: Input value
29*4bc52338SCy Schubert  * Returns: Value with all the bits set to the MSB of the input val
30*4bc52338SCy Schubert  */
const_time_fill_msb(unsigned int val)31*4bc52338SCy Schubert static inline unsigned int const_time_fill_msb(unsigned int val)
32*4bc52338SCy Schubert {
33*4bc52338SCy Schubert 	/* Move the MSB to LSB and multiple by -1 to fill in all bits. */
34*4bc52338SCy Schubert 	return (val >> (sizeof(val) * 8 - 1)) * ~0U;
35*4bc52338SCy Schubert }
36*4bc52338SCy Schubert 
37*4bc52338SCy Schubert 
38*4bc52338SCy Schubert /* Returns: -1 if val is zero; 0 if val is not zero */
const_time_is_zero(unsigned int val)39*4bc52338SCy Schubert static inline unsigned int const_time_is_zero(unsigned int val)
40*4bc52338SCy Schubert 	NO_UBSAN_UINT_OVERFLOW
41*4bc52338SCy Schubert {
42*4bc52338SCy Schubert 	/* Set MSB to 1 for 0 and fill rest of bits with the MSB value */
43*4bc52338SCy Schubert 	return const_time_fill_msb(~val & (val - 1));
44*4bc52338SCy Schubert }
45*4bc52338SCy Schubert 
46*4bc52338SCy Schubert 
47*4bc52338SCy Schubert /* Returns: -1 if a == b; 0 if a != b */
const_time_eq(unsigned int a,unsigned int b)48*4bc52338SCy Schubert static inline unsigned int const_time_eq(unsigned int a, unsigned int b)
49*4bc52338SCy Schubert {
50*4bc52338SCy Schubert 	return const_time_is_zero(a ^ b);
51*4bc52338SCy Schubert }
52*4bc52338SCy Schubert 
53*4bc52338SCy Schubert 
54*4bc52338SCy Schubert /* Returns: -1 if a == b; 0 if a != b */
const_time_eq_u8(unsigned int a,unsigned int b)55*4bc52338SCy Schubert static inline u8 const_time_eq_u8(unsigned int a, unsigned int b)
56*4bc52338SCy Schubert {
57*4bc52338SCy Schubert 	return (u8) const_time_eq(a, b);
58*4bc52338SCy Schubert }
59*4bc52338SCy Schubert 
60*4bc52338SCy Schubert 
61*4bc52338SCy Schubert /**
62*4bc52338SCy Schubert  * const_time_eq_bin - Constant time memory comparison
63*4bc52338SCy Schubert  * @a: First buffer to compare
64*4bc52338SCy Schubert  * @b: Second buffer to compare
65*4bc52338SCy Schubert  * @len: Number of octets to compare
66*4bc52338SCy Schubert  * Returns: -1 if buffers are equal, 0 if not
67*4bc52338SCy Schubert  *
68*4bc52338SCy Schubert  * This function is meant for comparing passwords or hash values where
69*4bc52338SCy Schubert  * difference in execution time or memory access pattern could provide external
70*4bc52338SCy Schubert  * observer information about the location of the difference in the memory
71*4bc52338SCy Schubert  * buffers. The return value does not behave like memcmp(), i.e.,
72*4bc52338SCy Schubert  * const_time_eq_bin() cannot be used to sort items into a defined order. Unlike
73*4bc52338SCy Schubert  * memcmp(), the execution time of const_time_eq_bin() does not depend on the
74*4bc52338SCy Schubert  * contents of the compared memory buffers, but only on the total compared
75*4bc52338SCy Schubert  * length.
76*4bc52338SCy Schubert  */
const_time_eq_bin(const void * a,const void * b,size_t len)77*4bc52338SCy Schubert static inline unsigned int const_time_eq_bin(const void *a, const void *b,
78*4bc52338SCy Schubert 					     size_t len)
79*4bc52338SCy Schubert {
80*4bc52338SCy Schubert 	const u8 *aa = a;
81*4bc52338SCy Schubert 	const u8 *bb = b;
82*4bc52338SCy Schubert 	size_t i;
83*4bc52338SCy Schubert 	u8 res = 0;
84*4bc52338SCy Schubert 
85*4bc52338SCy Schubert 	for (i = 0; i < len; i++)
86*4bc52338SCy Schubert 		res |= aa[i] ^ bb[i];
87*4bc52338SCy Schubert 
88*4bc52338SCy Schubert 	return const_time_is_zero(res);
89*4bc52338SCy Schubert }
90*4bc52338SCy Schubert 
91*4bc52338SCy Schubert 
92*4bc52338SCy Schubert /**
93*4bc52338SCy Schubert  * const_time_select - Constant time unsigned int selection
94*4bc52338SCy Schubert  * @mask: 0 (false) or -1 (true) to identify which value to select
95*4bc52338SCy Schubert  * @true_val: Value to select for the true case
96*4bc52338SCy Schubert  * @false_val: Value to select for the false case
97*4bc52338SCy Schubert  * Returns: true_val if mask == -1, false_val if mask == 0
98*4bc52338SCy Schubert  */
const_time_select(unsigned int mask,unsigned int true_val,unsigned int false_val)99*4bc52338SCy Schubert static inline unsigned int const_time_select(unsigned int mask,
100*4bc52338SCy Schubert 					     unsigned int true_val,
101*4bc52338SCy Schubert 					     unsigned int false_val)
102*4bc52338SCy Schubert {
103*4bc52338SCy Schubert 	return (mask & true_val) | (~mask & false_val);
104*4bc52338SCy Schubert }
105*4bc52338SCy Schubert 
106*4bc52338SCy Schubert 
107*4bc52338SCy Schubert /**
108*4bc52338SCy Schubert  * const_time_select_int - Constant time int selection
109*4bc52338SCy Schubert  * @mask: 0 (false) or -1 (true) to identify which value to select
110*4bc52338SCy Schubert  * @true_val: Value to select for the true case
111*4bc52338SCy Schubert  * @false_val: Value to select for the false case
112*4bc52338SCy Schubert  * Returns: true_val if mask == -1, false_val if mask == 0
113*4bc52338SCy Schubert  */
const_time_select_int(unsigned int mask,int true_val,int false_val)114*4bc52338SCy Schubert static inline int const_time_select_int(unsigned int mask, int true_val,
115*4bc52338SCy Schubert 					int false_val)
116*4bc52338SCy Schubert {
117*4bc52338SCy Schubert 	return (int) const_time_select(mask, (unsigned int) true_val,
118*4bc52338SCy Schubert 				       (unsigned int) false_val);
119*4bc52338SCy Schubert }
120*4bc52338SCy Schubert 
121*4bc52338SCy Schubert 
122*4bc52338SCy Schubert /**
123*4bc52338SCy Schubert  * const_time_select_u8 - Constant time u8 selection
124*4bc52338SCy Schubert  * @mask: 0 (false) or -1 (true) to identify which value to select
125*4bc52338SCy Schubert  * @true_val: Value to select for the true case
126*4bc52338SCy Schubert  * @false_val: Value to select for the false case
127*4bc52338SCy Schubert  * Returns: true_val if mask == -1, false_val if mask == 0
128*4bc52338SCy Schubert  */
const_time_select_u8(u8 mask,u8 true_val,u8 false_val)129*4bc52338SCy Schubert static inline u8 const_time_select_u8(u8 mask, u8 true_val, u8 false_val)
130*4bc52338SCy Schubert {
131*4bc52338SCy Schubert 	return (u8) const_time_select(mask, true_val, false_val);
132*4bc52338SCy Schubert }
133*4bc52338SCy Schubert 
134*4bc52338SCy Schubert 
135*4bc52338SCy Schubert /**
136*4bc52338SCy Schubert  * const_time_select_s8 - Constant time s8 selection
137*4bc52338SCy Schubert  * @mask: 0 (false) or -1 (true) to identify which value to select
138*4bc52338SCy Schubert  * @true_val: Value to select for the true case
139*4bc52338SCy Schubert  * @false_val: Value to select for the false case
140*4bc52338SCy Schubert  * Returns: true_val if mask == -1, false_val if mask == 0
141*4bc52338SCy Schubert  */
const_time_select_s8(u8 mask,s8 true_val,s8 false_val)142*4bc52338SCy Schubert static inline s8 const_time_select_s8(u8 mask, s8 true_val, s8 false_val)
143*4bc52338SCy Schubert {
144*4bc52338SCy Schubert 	return (s8) const_time_select(mask, (unsigned int) true_val,
145*4bc52338SCy Schubert 				      (unsigned int) false_val);
146*4bc52338SCy Schubert }
147*4bc52338SCy Schubert 
148*4bc52338SCy Schubert 
149*4bc52338SCy Schubert /**
150*4bc52338SCy Schubert  * const_time_select_bin - Constant time binary buffer selection copy
151*4bc52338SCy Schubert  * @mask: 0 (false) or -1 (true) to identify which value to copy
152*4bc52338SCy Schubert  * @true_val: Buffer to copy for the true case
153*4bc52338SCy Schubert  * @false_val: Buffer to copy for the false case
154*4bc52338SCy Schubert  * @len: Number of octets to copy
155*4bc52338SCy Schubert  * @dst: Destination buffer for the copy
156*4bc52338SCy Schubert  *
157*4bc52338SCy Schubert  * This function copies the specified buffer into the destination buffer using
158*4bc52338SCy Schubert  * operations with identical memory access pattern regardless of which buffer
159*4bc52338SCy Schubert  * is being copied.
160*4bc52338SCy Schubert  */
const_time_select_bin(u8 mask,const u8 * true_val,const u8 * false_val,size_t len,u8 * dst)161*4bc52338SCy Schubert static inline void const_time_select_bin(u8 mask, const u8 *true_val,
162*4bc52338SCy Schubert 					 const u8 *false_val, size_t len,
163*4bc52338SCy Schubert 					 u8 *dst)
164*4bc52338SCy Schubert {
165*4bc52338SCy Schubert 	size_t i;
166*4bc52338SCy Schubert 
167*4bc52338SCy Schubert 	for (i = 0; i < len; i++)
168*4bc52338SCy Schubert 		dst[i] = const_time_select_u8(mask, true_val[i], false_val[i]);
169*4bc52338SCy Schubert }
170*4bc52338SCy Schubert 
171*4bc52338SCy Schubert 
const_time_memcmp(const void * a,const void * b,size_t len)172*4bc52338SCy Schubert static inline int const_time_memcmp(const void *a, const void *b, size_t len)
173*4bc52338SCy Schubert {
174*4bc52338SCy Schubert 	const u8 *aa = a;
175*4bc52338SCy Schubert 	const u8 *bb = b;
176*4bc52338SCy Schubert 	int diff, res = 0;
177*4bc52338SCy Schubert 	unsigned int mask;
178*4bc52338SCy Schubert 
179*4bc52338SCy Schubert 	if (len == 0)
180*4bc52338SCy Schubert 		return 0;
181*4bc52338SCy Schubert 	do {
182*4bc52338SCy Schubert 		len--;
183*4bc52338SCy Schubert 		diff = (int) aa[len] - (int) bb[len];
184*4bc52338SCy Schubert 		mask = const_time_is_zero((unsigned int) diff);
185*4bc52338SCy Schubert 		res = const_time_select_int(mask, res, diff);
186*4bc52338SCy Schubert 	} while (len);
187*4bc52338SCy Schubert 
188*4bc52338SCy Schubert 	return res;
189*4bc52338SCy Schubert }
190*4bc52338SCy Schubert 
191*4bc52338SCy Schubert #endif /* CONST_TIME_H */
192