Remove building with NOCRYPTO option
[minix.git] / common / lib / libc / string / consttime_memequal.c
blob79263c19df49bd726dac3342a2970fb9dd497e85
1 /* $NetBSD: consttime_memequal.c,v 1.6 2015/03/18 20:11:35 riastradh Exp $ */
3 /*
4 * Written by Matthias Drochner <drochner@NetBSD.org>.
5 * Public domain.
6 */
8 #if !defined(_KERNEL) && !defined(_STANDALONE)
9 #include "namespace.h"
10 #include <string.h>
11 #ifdef __weak_alias
12 __weak_alias(consttime_memequal,_consttime_memequal)
13 #endif
14 #else
15 #include <lib/libkern/libkern.h>
16 #endif
18 int
19 consttime_memequal(const void *b1, const void *b2, size_t len)
21 const unsigned char *c1 = b1, *c2 = b2;
22 unsigned int res = 0;
24 while (len--)
25 res |= *c1++ ^ *c2++;
28 * Map 0 to 1 and [1, 256) to 0 using only constant-time
29 * arithmetic.
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));