xref: /freebsd/lib/libc/string/timingsafe_bcmp.3 (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1.\"	$OpenBSD: timingsafe_bcmp.3,v 1.2 2014/06/21 20:22:15 tedu Exp $
2.\"
3.\" Copyright (c) 2014 Google Inc.
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.\" $FreeBSD$
18.Dd August 15, 2016
19.Dt TIMINGSAFE_BCMP 3
20.Os
21.Sh NAME
22.Nm timingsafe_bcmp ,
23.Nm timingsafe_memcmp
24.Nd timing-safe byte sequence comparisons
25.Sh SYNOPSIS
26.In string.h
27.Ft int
28.Fn timingsafe_bcmp "const void *b1" "const void *b2" "size_t len"
29.Ft int
30.Fn timingsafe_memcmp "const void *b1" "const void *b2" "size_t len"
31.Sh DESCRIPTION
32The
33.Fn timingsafe_bcmp
34and
35.Fn timingsafe_memcmp
36functions lexicographically compare the first
37.Fa len
38bytes (each interpreted as an
39.Vt unsigned char )
40pointed to by
41.Fa b1
42and
43.Fa b2 .
44.Pp
45Additionally, their running times are independent of the byte sequences compared,
46making them safe to use for comparing secret values such as cryptographic MACs.
47In contrast,
48.Xr bcmp 3
49and
50.Xr memcmp 3
51may short-circuit after finding the first differing byte.
52.Sh RETURN VALUES
53The
54.Fn timingsafe_bcmp
55function returns 0 or not zero if the byte sequence pointed to by
56.Fa b1
57compares equal to or not equal to (respectively)
58the byte sequence pointed to by
59.Fa b2 .
60.Pp
61The
62.Fn timingsafe_memcmp
63function returns a negative value, 0, or positive value if the byte sequence
64pointed to by
65.Fa b1
66compares less than, equal to, or greater than (respectively)
67the byte sequence pointed to by
68.Fa b2 .
69.Sh SEE ALSO
70.Xr bcmp 3 ,
71.Xr memcmp 3
72.Sh STANDARDS
73The
74.Fn timingsafe_bcmp
75and
76.Fn timingsafe_memcmp
77functions are
78.Fx
79extensions.
80.Sh HISTORY
81The
82.Fn timingsafe_bcmp
83function first appeared in
84.Ox 4.9 .
85.Pp
86The
87.Fn timingsafe_memcmp
88function first appeared in
89.Ox 5.6 .
90.Pp
91Both functions first appeared in
92.Fx 12.0 .
93