1 /*
2  * Written by Matthias Drochner <drochner@NetBSD.org>.
3  * Public domain.
4  */
5 
6 #ifndef CONSTTIME_MEMEQUAL_H
7 #define CONSTTIME_MEMEQUAL_H
8 inline static int
9 consttime_memequal(const void *b1, const void *b2, size_t len)
10 {
11 	const unsigned char *c1 = b1, *c2 = b2;
12 	unsigned int res = 0;
13 
14 	while (len--)
15 		res |= *c1++ ^ *c2++;
16 
17 	/*
18 	 * Map 0 to 1 and [1, 256) to 0 using only constant-time
19 	 * arithmetic.
20 	 *
21 	 * This is not simply `!res' because although many CPUs support
22 	 * branchless conditional moves and many compilers will take
23 	 * advantage of them, certain compilers generate branches on
24 	 * certain CPUs for `!res'.
25 	 */
26 	return (1 & ((res - 1) >> 8));
27 }
28 #endif /* CONSTTIME_MEMEQUAL_H */
29