rdrand: Reuse CPU feature detection to check for RDRAND instructions
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
|
||||
#include <library.h>
|
||||
#include <utils/debug.h>
|
||||
#include <utils/cpu_feature.h>
|
||||
|
||||
typedef struct private_rdrand_plugin_t private_rdrand_plugin_t;
|
||||
typedef enum cpuid_feature_t cpuid_feature_t;
|
||||
@@ -35,56 +36,6 @@ struct private_rdrand_plugin_t {
|
||||
rdrand_plugin_t public;
|
||||
};
|
||||
|
||||
/**
|
||||
* CPU feature flags, returned via cpuid(1)
|
||||
*/
|
||||
enum cpuid_feature_t {
|
||||
CPUID_RDRAND = (1<<30),
|
||||
};
|
||||
|
||||
/**
|
||||
* Get cpuid for info, return eax, ebx, ecx and edx.
|
||||
* -fPIC requires to save ebx on IA-32.
|
||||
*/
|
||||
static void cpuid(u_int op, u_int *a, u_int *b, u_int *c, u_int *d)
|
||||
{
|
||||
#ifdef __x86_64__
|
||||
asm("cpuid" : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) : "a" (op));
|
||||
#else /* __i386__ */
|
||||
asm("pushl %%ebx;"
|
||||
"cpuid;"
|
||||
"movl %%ebx, %1;"
|
||||
"popl %%ebx;"
|
||||
: "=a" (*a), "=r" (*b), "=c" (*c), "=d" (*d) : "a" (op));
|
||||
#endif /* __x86_64__ / __i386__*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have RDRAND instruction
|
||||
*/
|
||||
static bool have_rdrand()
|
||||
{
|
||||
char vendor[3 * sizeof(u_int32_t) + 1];
|
||||
u_int a, b, c, d;
|
||||
|
||||
cpuid(0, &a, &b, &c, &d);
|
||||
/* VendorID string is in b-d-c (yes, in this order) */
|
||||
snprintf(vendor, sizeof(vendor), "%.4s%.4s%.4s", &b, &d, &c);
|
||||
|
||||
/* check if we have an Intel CPU */
|
||||
if (streq(vendor, "GenuineIntel"))
|
||||
{
|
||||
cpuid(1, &a, &b, &c, &d);
|
||||
if (c & CPUID_RDRAND)
|
||||
{
|
||||
DBG2(DBG_LIB, "detected RDRAND support on %s CPU", vendor);
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
DBG2(DBG_LIB, "no RDRAND support on %s CPU, disabled", vendor);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
METHOD(plugin_t, get_name, char*,
|
||||
private_rdrand_plugin_t *this)
|
||||
{
|
||||
@@ -102,10 +53,12 @@ METHOD(plugin_t, get_features, int,
|
||||
PLUGIN_DEPENDS(CRYPTER, ENCR_AES_CBC, 16),
|
||||
};
|
||||
*features = f;
|
||||
if (have_rdrand())
|
||||
if (cpu_feature_available(CPU_FEATURE_RDRAND))
|
||||
{
|
||||
DBG2(DBG_LIB, "detected RDRAND support, enabled");
|
||||
return countof(f);
|
||||
}
|
||||
DBG2(DBG_LIB, "no RDRAND support detected, disabled");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user