1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004-2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert #include <limits.h>
24*a466cc55SCy Schubert #include <isc/string.h>
25*a466cc55SCy Schubert
26*a466cc55SCy Schubert /* Making a portable memcmp that has no internal branches and loops always
27*a466cc55SCy Schubert * once for every byte without early-out shortcut has a few challenges.
28*a466cc55SCy Schubert *
29*a466cc55SCy Schubert * Inspired by 'timingsafe_memcmp()' from the BSD system and
30*a466cc55SCy Schubert * https://github.com/libressl-portable/openbsd/blob/master/src/lib/libc/string/timingsafe_memcmp.c
31*a466cc55SCy Schubert *
32*a466cc55SCy Schubert * Sadly, that one is not portable C: It makes assumptions on the representation
33*a466cc55SCy Schubert * of negative integers and assumes sign-preserving right-shift of negative
34*a466cc55SCy Schubert * signed values. This is a rewrite from scratch that should not suffer from
35*a466cc55SCy Schubert * such issues.
36*a466cc55SCy Schubert *
37*a466cc55SCy Schubert * 2015-12-12, J. Perlinger (perlinger-at-ntp-dot-org)
38*a466cc55SCy Schubert */
39*a466cc55SCy Schubert int
isc_tsmemcmp(const void * p1,const void * p2,size_t nb)40*a466cc55SCy Schubert isc_tsmemcmp(const void *p1, const void *p2, size_t nb) {
41*a466cc55SCy Schubert const unsigned char *ucp1 = p1;
42*a466cc55SCy Schubert const unsigned char *ucp2 = p2;
43*a466cc55SCy Schubert unsigned int isLT = 0u;
44*a466cc55SCy Schubert unsigned int isGT = 0u;
45*a466cc55SCy Schubert volatile unsigned int mask = (1u << CHAR_BIT);
46*a466cc55SCy Schubert
47*a466cc55SCy Schubert for (/*NOP*/; 0 != nb; --nb, ++ucp1, ++ucp2) {
48*a466cc55SCy Schubert isLT |= mask &
49*a466cc55SCy Schubert ((unsigned int)*ucp1 - (unsigned int)*ucp2);
50*a466cc55SCy Schubert isGT |= mask &
51*a466cc55SCy Schubert ((unsigned int)*ucp2 - (unsigned int)*ucp1);
52*a466cc55SCy Schubert mask &= ~(isLT | isGT);
53*a466cc55SCy Schubert }
54*a466cc55SCy Schubert return (int)(isGT >> CHAR_BIT) - (int)(isLT >> CHAR_BIT);
55*a466cc55SCy Schubert }
56