Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions library/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const cha
* fall back to the unsafe implementation. */
#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
#endif

#if defined(__has_builtin)
#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
#else
#define MBEDTLS_HAS_BUILTIN(x) 0
#endif

/** Allow library to access its structs' private members.
*
* Although structs defined in header files are publicly available,
Expand Down Expand Up @@ -208,6 +215,11 @@ static inline void mbedtls_xor(unsigned char *r,
return;
}
#endif
#if defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_HAS_BUILTIN(__builtin_constant_p)
if (__builtin_constant_p(n) && n % 16 == 0) {
return;
}
#endif
#elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
/* This codepath probably only makes sense on architectures with 64-bit registers */
for (; (i + 8) <= n; i += 8) {
Expand All @@ -219,6 +231,11 @@ static inline void mbedtls_xor(unsigned char *r,
return;
}
#endif
#if defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_HAS_BUILTIN(__builtin_constant_p)
if (__builtin_constant_p(n) && n % 8 == 0) {
return;
}
#endif
#else
for (; (i + 4) <= n; i += 4) {
uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
Expand All @@ -229,6 +246,11 @@ static inline void mbedtls_xor(unsigned char *r,
return;
}
#endif
#if defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_HAS_BUILTIN(__builtin_constant_p)
if (__builtin_constant_p(n) && n % 4 == 0) {
return;
}
#endif
#endif
#endif
for (; i < n; i++) {
Expand Down Expand Up @@ -367,12 +389,6 @@ static inline void mbedtls_xor_no_simd(unsigned char *r,
struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function
#endif

#if defined(__has_builtin)
#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
#else
#define MBEDTLS_HAS_BUILTIN(x) 0
#endif

/* Define compiler branch hints */
#if MBEDTLS_HAS_BUILTIN(__builtin_expect)
#define MBEDTLS_LIKELY(x) __builtin_expect(!!(x), 1)
Expand Down