From 2d8fbe09e9f7622df7c7cc0116781a5d65613f9f Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 28 Sep 2023 18:14:14 -0700 Subject: [PATCH] Import currently-unused utilities in crypto/internal.h Bring these in as they were in 4a0393fcf37d7dbd090a5bb2293601a9ec7605da. The next merge will modify these. --- crypto/internal.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crypto/internal.h b/crypto/internal.h index 96f1c74b97..c1844f1dbb 100644 --- a/crypto/internal.h +++ b/crypto/internal.h @@ -146,6 +146,20 @@ typedef __int128_t int128_t; typedef __uint128_t uint128_t; #endif +// Pointer utility functions. + +// buffers_alias returns one if |a| and |b| alias and zero otherwise. +static inline int buffers_alias(const uint8_t *a, size_t a_len, + const uint8_t *b, size_t b_len) { + // Cast |a| and |b| to integers. In C, pointer comparisons between unrelated + // objects are undefined whereas pointer to integer conversions are merely + // implementation-defined. We assume the implementation defined it in a sane + // way. + uintptr_t a_u = (uintptr_t)a; + uintptr_t b_u = (uintptr_t)b; + return a_u + a_len > b_u && b_u + b_len > a_u; +} + // Constant-time utility functions. // @@ -260,6 +274,13 @@ static inline crypto_word_t constant_time_select_w(crypto_word_t mask, return (value_barrier_w(mask) & a) | (value_barrier_w(~mask) & b); } +// constant_time_select_8 acts like |constant_time_select| but operates on +// 8-bit values. +static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a, + uint8_t b) { + return (uint8_t)(constant_time_select_w(mask, a, b)); +} + #if defined(BORINGSSL_CONSTANT_TIME_VALIDATION) // CONSTTIME_SECRET takes a pointer and a number of bytes and marks that region