Skip to content

Commit

Permalink
Import currently-unused utilities in crypto/internal.h
Browse files Browse the repository at this point in the history
Bring these in as they were in 4a0393f.
The next merge will modify these.
  • Loading branch information
briansmith committed Sep 29, 2023
1 parent 2270dc6 commit 2d8fbe0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2d8fbe0

Please sign in to comment.