timingsafe_memcmp(3)

timingsafe_memcmp3

Timing-safe byte sequence comparisons

Part of timingsafe_bcmp(3).

Usage

#include <string.h>

int

timingsafe_bcmp(const void *b1, const void *b2, size_t len);

int

timingsafe_memcmp(const void *b1, const void *b2, size_t len);

Overview

The timingsafe_bcmp() and timingsafe_memcmp() functions lexicographically compare the first len bytes (each interpreted as an unsigned char) pointed to by b1 and b2.

Additionally, their running times are independent of the byte sequences compared, making them safe to use for comparing secret values such as cryptographic MACs. In contrast, bcmp(3) and memcmp(3) may short-circuit after finding the first differing byte.

Return values

The timingsafe_bcmp() function returns 0 or not zero if the byte sequence pointed to by b1 compares equal to or not equal to (respectively) the byte sequence pointed to by b2.

The timingsafe_memcmp() function returns a negative value, 0, or positive value if the byte sequence pointed to by b1 compares less than, equal to, or greater than (respectively) the byte sequence pointed to by b2.

Releases

24 releases since FreeBSD 11.1-RELEASE; the text changed 11 times.

  1. 11.1
  2. 11.2
  3. 11.3
  4. 11.4
  5. 12.0
  6. 12.1
  7. 12.2
  8. 12.3
  9. 12.4
  10. 13.0–13.2
  11. 13.3–14.5
  12. 15.0–16.0

Standards

The timingsafe_bcmp() and timingsafe_memcmp() functions are FreeBSD extensions.

History

The timingsafe_bcmp() function first appeared in OpenBSD 4.9.

The timingsafe_memcmp() function first appeared in OpenBSD 5.6.

Both functions first appeared in FreeBSD 11.1.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.