1 /* $NetBSD: consttime_memequal.c,v 1.6 2015/03/18 20:11:35 riastradh Exp $ */
4 * Written by Matthias Drochner <drochner@NetBSD.org>.
8 #if !defined(_KERNEL) && !defined(_STANDALONE)
12 __weak_alias(consttime_memequal
,_consttime_memequal
)
15 #include <lib/libkern/libkern.h>
19 consttime_memequal(const void *b1
, const void *b2
, size_t len
)
21 const unsigned char *c1
= b1
, *c2
= b2
;
28 * Map 0 to 1 and [1, 256) to 0 using only constant-time
31 * This is not simply `!res' because although many CPUs support
32 * branchless conditional moves and many compilers will take
33 * advantage of them, certain compilers generate branches on
34 * certain CPUs for `!res'.
36 return (1 & ((res
- 1) >> 8));