blob: 7226ae1bcd2fb01a662065fb49266e7cba1d50c0 [file] [log] [blame]
Gabor Mezeie24dea82021-10-19 12:22:25 +02001/**
2 * Constant-time functions
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gabor Mezeie24dea82021-10-19 12:22:25 +02006 */
7
8#ifndef MBEDTLS_CONSTANT_TIME_H
9#define MBEDTLS_CONSTANT_TIME_H
10
Gabor Mezeie24dea82021-10-19 12:22:25 +020011#include <stddef.h>
12
13
14/** Constant-time buffer comparison without branches.
15 *
Gabor Mezeidbe0f892021-11-03 16:13:32 +010016 * This is equivalent to the standard memcmp function, but is likely to be
Gabor Mezeie24dea82021-10-19 12:22:25 +020017 * compiled to code using bitwise operation rather than a branch.
18 *
19 * This function can be used to write constant-time code by replacing branches
20 * with bit operations using masks.
21 *
22 * \param a Pointer to the first buffer.
23 * \param b Pointer to the second buffer.
24 * \param n The number of bytes to compare in the buffer.
25 *
26 * \return Zero if the content of the two buffer is the same,
27 * otherwise non-zero.
28 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029int mbedtls_ct_memcmp(const void *a,
30 const void *b,
31 size_t n);
Gabor Mezeie24dea82021-10-19 12:22:25 +020032
33#endif /* MBEDTLS_CONSTANT_TIME_H */