Antonio de Angelis | 8bb9851 | 2024-01-16 14:13:36 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Constant-time functions |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef MBEDTLS_CONSTANT_TIME_H |
| 10 | #define MBEDTLS_CONSTANT_TIME_H |
| 11 | |
| 12 | #include <stddef.h> |
| 13 | |
| 14 | /** Constant-time buffer comparison without branches. |
| 15 | * |
| 16 | * This is equivalent to the standard memcmp function, but is likely to be |
| 17 | * compiled to code using bitwise operations rather than a branch, such that |
| 18 | * the time taken is constant w.r.t. the data pointed to by \p a and \p b, |
| 19 | * and w.r.t. whether \p a and \p b are equal or not. It is not constant-time |
| 20 | * w.r.t. \p n . |
| 21 | * |
| 22 | * This function can be used to write constant-time code by replacing branches |
| 23 | * with bit operations using masks. |
| 24 | * |
| 25 | * \param a Pointer to the first buffer, containing at least \p n bytes. May not be NULL. |
| 26 | * \param b Pointer to the second buffer, containing at least \p n bytes. May not be NULL. |
| 27 | * \param n The number of bytes to compare. |
| 28 | * |
| 29 | * \return Zero if the contents of the two buffers are the same, |
| 30 | * otherwise non-zero. |
| 31 | */ |
| 32 | int mbedtls_ct_memcmp(const void *a, |
| 33 | const void *b, |
| 34 | size_t n); |
| 35 | |
| 36 | #endif /* MBEDTLS_CONSTANT_TIME_H */ |