[dpdk-dev v6 1/2] eal: introduce rte_memeq_timingsafe() based on FreeBSD API
Morten Brørup
mb at smartsharesystems.com
Fri Oct 3 09:35:38 CEST 2025
> From: Kai Ji [mailto:kai.ji at intel.com]
> Sent: Thursday, 2 October 2025 17.32
>
> Bugzilla ID: 1773
> https://bugs.dpdk.org/show_bug.cgi?id=1773
>
> Signed-off-by: Kai Ji <kai.ji at intel.com>
> ---
> lib/eal/include/rte_memory.h | 38 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 38 insertions(+)
>
> diff --git a/lib/eal/include/rte_memory.h
> b/lib/eal/include/rte_memory.h
> index dcc0e69cfe..2d24e091af 100644
> --- a/lib/eal/include/rte_memory.h
> +++ b/lib/eal/include/rte_memory.h
> @@ -746,6 +746,44 @@ __rte_experimental
> void
> rte_memzero_explicit(void *dst, size_t sz);
>
> +/**
> + * @warning
> + * @b EXPERIMENTAL: this API may change without prior notice.
> + *
> + * Timing-safe memory equality comparison.
> + *
> + * This function compares two memory regions in constant time, making
> it
> + * resistant to timing side-channel attacks. The execution time
> depends only
> + * on the length parameter, not on the actual data values being
> compared.
> + *
> + * This is particularly important for cryptographic operations where
> timing
> + * differences could leak information about secret keys, passwords, or
> other
> + * sensitive data.
> + *
> + * @param a
> + * Pointer to the first memory region to compare
> + * @param b
> + * Pointer to the second memory region to compare
> + * @param n
> + * Number of bytes to compare
> + * @return
> + * true if the memory regions are identical, false if they differ
> + */
> +__rte_experimental
> +static inline bool
> +rte_memeq_timingsafe(const void *a, const void *b, size_t n)
> +{
> + const volatile uint8_t *pa = (const volatile uint8_t *)a;
> + const volatile uint8_t *pb = (const volatile uint8_t *)b;
> + uint8_t result = 0;
> + size_t i;
> +
> + for (i = 0; i < n; i++)
> + result |= pa[i] ^ pb[i];
> +
> + return result == UINT8_C(0);
> +}
> +
> #ifdef __cplusplus
> }
> #endif
> --
> 2.34.1
Reviewed-by: Morten Brørup <mb at smartsharesystems.com>
More information about the dev
mailing list