xref: /minix3/lib/libc/string/memcmp.3 (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
12fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993
22fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
32fe8fb19SBen Gras.\"
42fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by
52fe8fb19SBen Gras.\" Chris Torek and the American National Standards Committee X3,
62fe8fb19SBen Gras.\" on Information Processing Systems.
72fe8fb19SBen Gras.\"
82fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
102fe8fb19SBen Gras.\" are met:
112fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
172fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
182fe8fb19SBen Gras.\"    without specific prior written permission.
192fe8fb19SBen Gras.\"
202fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
212fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
242fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302fe8fb19SBen Gras.\" SUCH DAMAGE.
312fe8fb19SBen Gras.\"
322fe8fb19SBen Gras.\"     from: @(#)memcmp.3	8.1 (Berkeley) 6/4/93
33*84d9c625SLionel Sambuc.\"	$NetBSD: memcmp.3,v 1.11 2013/06/24 04:21:20 riastradh Exp $
342fe8fb19SBen Gras.\"
35*84d9c625SLionel Sambuc.Dd June 23, 2013
362fe8fb19SBen Gras.Dt MEMCMP 3
372fe8fb19SBen Gras.Os
382fe8fb19SBen Gras.Sh NAME
392fe8fb19SBen Gras.Nm memcmp
402fe8fb19SBen Gras.Nd compare byte string
412fe8fb19SBen Gras.Sh LIBRARY
422fe8fb19SBen Gras.Lb libc
432fe8fb19SBen Gras.Sh SYNOPSIS
442fe8fb19SBen Gras.In string.h
452fe8fb19SBen Gras.Ft int
462fe8fb19SBen Gras.Fn memcmp "const void *b1" "const void *b2" "size_t len"
472fe8fb19SBen Gras.Sh DESCRIPTION
482fe8fb19SBen GrasThe
492fe8fb19SBen Gras.Fn memcmp
502fe8fb19SBen Grasfunction
512fe8fb19SBen Grascompares byte string
522fe8fb19SBen Gras.Fa b1
532fe8fb19SBen Grasagainst byte string
542fe8fb19SBen Gras.Fa b2 .
552fe8fb19SBen GrasBoth strings are assumed to be
562fe8fb19SBen Gras.Fa len
572fe8fb19SBen Grasbytes long.
582fe8fb19SBen Gras.Sh RETURN VALUES
592fe8fb19SBen GrasThe
602fe8fb19SBen Gras.Fn memcmp
612fe8fb19SBen Grasfunction
622fe8fb19SBen Grasreturns zero if the two strings are identical,
632fe8fb19SBen Grasotherwise returns the difference between the first two differing bytes
642fe8fb19SBen Gras(treated as unsigned char values, so that
652fe8fb19SBen Gras.Sq Li \e200
662fe8fb19SBen Grasis greater than
672fe8fb19SBen Gras.Sq Li \&\e0 ,
682fe8fb19SBen Grasfor example).
692fe8fb19SBen GrasZero-length strings are always identical.
70*84d9c625SLionel Sambuc.Pp
71*84d9c625SLionel SambucDo not use
72*84d9c625SLionel Sambuc.Fn memcmp
73*84d9c625SLionel Sambucto compare cryptographic secrets, because the time it takes varies
74*84d9c625SLionel Sambucdepending on how many bytes are the same, and thus leaks information
75*84d9c625SLionel Sambucabout the two strings by a timing side channel.
76*84d9c625SLionel SambucTo compare secrets, hashes, message authentication codes, etc., use
77*84d9c625SLionel Sambuc.Xr consttime_memequal 3
78*84d9c625SLionel Sambucinstead.
792fe8fb19SBen Gras.Sh SEE ALSO
802fe8fb19SBen Gras.Xr bcmp 3 ,
81*84d9c625SLionel Sambuc.Xr consttime_memequal 3 ,
822fe8fb19SBen Gras.Xr strcasecmp 3 ,
832fe8fb19SBen Gras.Xr strcmp 3 ,
842fe8fb19SBen Gras.Xr strcoll 3 ,
852fe8fb19SBen Gras.Xr strxfrm 3
862fe8fb19SBen Gras.Sh STANDARDS
872fe8fb19SBen GrasThe
882fe8fb19SBen Gras.Fn memcmp
892fe8fb19SBen Grasfunction
902fe8fb19SBen Grasconforms to
912fe8fb19SBen Gras.St -ansiC .
92