libc/string: Add timing_safe functions from OpenBSD.
[dragonfly.git] / lib / libc / string / timingsafe_bcmp.3
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 .Dd April 11, 2019
18 .Dt TIMINGSAFE_BCMP 3
19 .Os
20 .Sh NAME
21 .Nm timingsafe_bcmp ,
22 .Nm timingsafe_memcmp
23 .Nd timing-safe byte sequence comparisons
24 .Sh SYNOPSIS
25 .In string.h
26 .Ft int
27 .Fn timingsafe_bcmp "const void *b1" "const void *b2" "size_t len"
28 .Ft int
29 .Fn timingsafe_memcmp "const void *b1" "const void *b2" "size_t len"
30 .Sh DESCRIPTION
31 The
32 .Fn timingsafe_bcmp
33 and
34 .Fn timingsafe_memcmp
35 functions lexicographically compare the first
36 .Fa len
37 bytes (each interpreted as an
38 .Vt unsigned char )
39 pointed to by
40 .Fa b1
41 and
42 .Fa b2 .
43 .Pp
44 Additionally, their running times are independent of the byte sequences compared,
45 making them safe to use for comparing secret values such as cryptographic MACs.
46 In contrast,
47 .Xr bcmp 3
48 and
49 .Xr memcmp 3
50 may short-circuit after finding the first differing byte.
51 .Sh RETURN VALUES
52 The
53 .Fn timingsafe_bcmp
54 function returns 0 or not zero if the byte sequence pointed to by
55 .Fa b1
56 compares equal to or not equal to (respectively)
57 the byte sequence pointed to by
58 .Fa b2 .
59 .Pp
60 The
61 .Fn timingsafe_memcmp
62 function returns a negative value, 0, or positive value if the byte sequence
63 pointed to by
64 .Fa b1
65 compares less than, equal to, or greater than (respectively)
66 the byte sequence pointed to by
67 .Fa b2 .
68 .Sh SEE ALSO
69 .Xr bcmp 3 ,
70 .Xr memcmp 3
71 .Sh STANDARDS
72 The
73 .Fn timingsafe_bcmp
74 and
75 .Fn timingsafe_memcmp
76 functions are
77 .Ox
78 extensions.
79 .Sh HISTORY
80 The
81 .Fn timingsafe_bcmp
82 function first appeared in
83 .Ox 4.9
84 and
85 .Dx 5.6 .
86 .Pp
87 The
88 .Fn timingsafe_memcmp
89 function first appeared in
90 .Ox
91 /
92 .Dx 5.6 .