Skip to content

Commit 2852673

Browse files
committed
Fix: more thorough SHA-NI check
Previously, the runtime check for SHA-NI did not verify SSE 4.1 support. This led to SIGILL crashes on older CPUs when encountering the PINSRD instruction during SHA-1/SHA-2 digest calculation.
1 parent c2b5f68 commit 2852673

File tree

6 files changed

+19
-5
lines changed

6 files changed

+19
-5
lines changed

librhash/algorithms.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ rhash_hash_info rhash_hash_info_default[] =
143143
#if defined(RHASH_SSE4_SHANI) && !defined(RHASH_DISABLE_SHANI)
144144
static void table_init_sha_ext(void)
145145
{
146-
if (has_cpu_feature(CPU_FEATURE_SHANI))
146+
/* SHA-NI Implementation uses SHANI, SSE2, SSSE3, SSE4.1 instructions.
147+
* Checking for SSE4.1 requires to check for SSSE3, SSE3 and SSE2. */
148+
if (has_cpu_feature(CPU_FEATURE_SHANI) &&
149+
has_cpu_feature(CPU_FEATURE_SSE2) &&
150+
has_cpu_feature(CPU_FEATURE_SSE3) &&
151+
has_cpu_feature(CPU_FEATURE_SSSE3) &&
152+
has_cpu_feature(CPU_FEATURE_SSE4_1))
147153
{
148154
assert(rhash_hash_info_default[3].init == (pinit_t)rhash_sha1_init);
149155
rhash_hash_info_default[3].update = (pupdate_t)rhash_sha1_ni_update;

librhash/byte_order.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void rhash_u32_mem_swap(unsigned* arr, int length)
255255

256256
static uint64_t get_cpuid_features(void)
257257
{
258-
uint32_t cpu_info[4] = {0};
258+
uint32_t cpu_info[4] = {0}; /* EAX, EBX, EXC, EDX registers */
259259
uint64_t result = 0;
260260
/* Request basic CPU functions */
261261
RHASH_CPUID(1, cpu_info);

librhash/byte_order.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ static RHASH_INLINE uint64_t bswap_64(uint64_t x)
214214
#define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n))))
215215
#define ROTR64(qword, n) ((qword) >> (n) ^ ((qword) << (64 - (n))))
216216

217+
#define CPU_FEATURE_SSE2 (26)
218+
#define CPU_FEATURE_SSE3 (32)
219+
#define CPU_FEATURE_SSSE3 (41)
220+
#define CPU_FEATURE_SSE4_1 (51)
217221
#define CPU_FEATURE_SSE4_2 (52)
218222
#define CPU_FEATURE_SHANI (29)
219223

librhash/crc32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ unsigned rhash_crc32c_table[8][256] = { {
612612
0xe54c35a1, 0xac704886, 0x7734cfef, 0x3e08b2c8, 0xc451b7cc, 0x8d6dcaeb, 0x56294d82, 0x1f1530a5
613613
} };
614614

615-
#ifdef HAS_INTEL_CPUID
615+
#ifdef HAS_GCC_INTEL_CPUID
616616
static unsigned calculate_crc32c_choose_best(unsigned crcinit, unsigned table[8][256], const unsigned char* msg, size_t size);
617617
static unsigned calculate_crc32c_sse42(unsigned crcinit, unsigned table[8][256], const unsigned char* msg, size_t size);
618618
static unsigned (*calculate_crc32c_p)(unsigned crcinit, unsigned table[8][256], const unsigned char* msg, size_t size) = calculate_crc32c_choose_best;

librhash/sha_ni.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# if defined(__GNUC__) && defined(__SHA__)
77
# define RHASH_GCC_SHANI
88
/* SHA extensions are supported by Visual Studio >= 2015 */
9-
# elif (_MSC_VER >= 1900)
9+
# elif (_MSC_VER >= 1900) && (_M_IX86 || _M_AMD64)
1010
# define RHASH_MSVC_SHANI
1111
# else
1212
# define RHASH_DISABLE_SHANI

librhash/test_lib.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,11 @@ static unsigned find_hash(const char* name)
15861586
static void print_cpu_features(void)
15871587
{
15881588
#if !defined(NO_HAS_CPU_FEATURE)
1589-
printf("CPU Features:%s%s\n",
1589+
printf("CPU Features:%s%s%s%s%s%s\n",
1590+
(rhash_has_cpu_feature(CPU_FEATURE_SSE2) ? " SSE2" : ""),
1591+
(rhash_has_cpu_feature(CPU_FEATURE_SSE3) ? " SSE3" : ""),
1592+
(rhash_has_cpu_feature(CPU_FEATURE_SSSE3) ? " SSSE3" : ""),
1593+
(rhash_has_cpu_feature(CPU_FEATURE_SSE4_1) ? " SSE_4.1" : ""),
15901594
(rhash_has_cpu_feature(CPU_FEATURE_SSE4_2) ? " SSE_4.2" : ""),
15911595
(rhash_has_cpu_feature(CPU_FEATURE_SHANI) ? " SHANI" : ""));
15921596
#endif

0 commit comments

Comments
 (0)