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