xref: /openbsd-src/sys/lib/libkern/timingsafe_bcmp.c (revision b5be37d2a6fbef83b90ca99497343fa8dbb0f3fc)
1*b5be37d2Sderaadt /*	$OpenBSD: timingsafe_bcmp.c,v 1.2 2014/06/10 04:16:57 deraadt Exp $	*/
2bee02badSmatthew /*
3bee02badSmatthew  * Copyright (c) 2010 Damien Miller.  All rights reserved.
4bee02badSmatthew  *
5bee02badSmatthew  * Permission to use, copy, modify, and distribute this software for any
6bee02badSmatthew  * purpose with or without fee is hereby granted, provided that the above
7bee02badSmatthew  * copyright notice and this permission notice appear in all copies.
8bee02badSmatthew  *
9bee02badSmatthew  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10bee02badSmatthew  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11bee02badSmatthew  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12bee02badSmatthew  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13bee02badSmatthew  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14bee02badSmatthew  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15bee02badSmatthew  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16bee02badSmatthew  */
17bee02badSmatthew 
18bee02badSmatthew #include <lib/libkern/libkern.h>
19bee02badSmatthew 
20bee02badSmatthew int
timingsafe_bcmp(const void * b1,const void * b2,size_t n)21bee02badSmatthew timingsafe_bcmp(const void *b1, const void *b2, size_t n)
22bee02badSmatthew {
23bee02badSmatthew 	const unsigned char *p1 = b1, *p2 = b2;
24bee02badSmatthew 	int ret = 0;
25bee02badSmatthew 
26bee02badSmatthew 	for (; n > 0; n--)
27bee02badSmatthew 		ret |= *p1++ ^ *p2++;
28bee02badSmatthew 	return (ret != 0);
29bee02badSmatthew }
30