Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Constant-time functions |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef MBEDTLS_CONSTANT_TIME_H |
| 9 | #define MBEDTLS_CONSTANT_TIME_H |
| 10 | |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | |
| 13 | |
| 14 | /** Constant-time buffer comparison without branches. |
| 15 | * |
Gabor Mezei | dbe0f89 | 2021-11-03 16:13:32 +0100 | [diff] [blame] | 16 | * This is equivalent to the standard memcmp function, but is likely to be |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 17 | * 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | int mbedtls_ct_memcmp(const void *a, |
| 30 | const void *b, |
| 31 | size_t n); |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 32 | |
| 33 | #endif /* MBEDTLS_CONSTANT_TIME_H */ |