xref: /minix3/common/lib/libc/string/consttime_memequal.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1 /* $NetBSD: consttime_memequal.c,v 1.4 2013/08/28 19:31:14 riastradh Exp $ */
2 
3 #if !defined(_KERNEL) && !defined(_STANDALONE)
4 #include "namespace.h"
5 #include <string.h>
6 #ifdef __weak_alias
7 __weak_alias(consttime_memequal,_consttime_memequal)
8 #endif
9 #else
10 #include <lib/libkern/libkern.h>
11 #endif
12 
13 int
14 consttime_memequal(const void *b1, const void *b2, size_t len)
15 {
16 	const char *c1 = b1, *c2 = b2;
17 	int res = 0;
18 
19 	while (len --)
20 		res |= *c1++ ^ *c2++;
21 
22 	/*
23 	 * If the compiler for your favourite architecture generates a
24 	 * conditional branch for `!res', it will be a data-dependent
25 	 * branch, in which case this should be replaced by
26 	 *
27 	 *	return (1 - (1 & ((res - 1) >> 8)));
28 	 *
29 	 * or rewritten in assembly.
30 	 */
31 	return !res;
32 }
33