Add a rdrand plugin stub detecting availability of RDRAND instructions

This commit is contained in:
Martin Willi 2013-01-04 14:33:45 +01:00
parent 0e0870aef7
commit ed8dc6f132
5 changed files with 187 additions and 0 deletions

View File

@ -112,6 +112,7 @@ ARG_DISBL_SET([sha1], [disable SHA1 software implementation plugin.])
ARG_DISBL_SET([sha2], [disable SHA256/SHA384/SHA512 software implementation plugin.])
ARG_DISBL_SET([fips-prf], [disable FIPS PRF software implementation plugin.])
ARG_DISBL_SET([gmp], [disable GNU MP (libgmp) based crypto implementation plugin.])
ARG_ENABL_SET([rdrand], [enable Intel RDRAND random generator plugin.])
ARG_DISBL_SET([random], [disable RNG implementation on top of /dev/(u)random.])
ARG_DISBL_SET([nonce], [disable nonce generation plugin.])
ARG_DISBL_SET([x509], [disable X509 certificate implementation plugin.])
@ -890,6 +891,7 @@ ADD_PLUGIN([sha1], [s charon openac scepclient pki scripts medsr
ADD_PLUGIN([sha2], [s charon openac scepclient pki scripts medsrv attest nm])
ADD_PLUGIN([md4], [s charon openac manager scepclient pki nm])
ADD_PLUGIN([md5], [s charon openac scepclient pki scripts attest nm])
ADD_PLUGIN([rdrand], [s charon openac scepclient pki scripts medsrv attest nm])
ADD_PLUGIN([random], [s charon openac scepclient pki scripts medsrv attest nm])
ADD_PLUGIN([nonce], [s charon nm])
ADD_PLUGIN([x509], [s charon openac scepclient pki scripts attest nm])
@ -1015,6 +1017,7 @@ AM_CONDITIONAL(USE_SHA1, test x$sha1 = xtrue)
AM_CONDITIONAL(USE_SHA2, test x$sha2 = xtrue)
AM_CONDITIONAL(USE_FIPS_PRF, test x$fips_prf = xtrue)
AM_CONDITIONAL(USE_GMP, test x$gmp = xtrue)
AM_CONDITIONAL(USE_RDRAND, test x$rdrand = xtrue)
AM_CONDITIONAL(USE_RANDOM, test x$random = xtrue)
AM_CONDITIONAL(USE_NONCE, test x$nonce = xtrue)
AM_CONDITIONAL(USE_X509, test x$x509 = xtrue)
@ -1198,6 +1201,7 @@ AC_OUTPUT(
src/libstrongswan/plugins/sha2/Makefile
src/libstrongswan/plugins/fips_prf/Makefile
src/libstrongswan/plugins/gmp/Makefile
src/libstrongswan/plugins/rdrand/Makefile
src/libstrongswan/plugins/random/Makefile
src/libstrongswan/plugins/nonce/Makefile
src/libstrongswan/plugins/hmac/Makefile

View File

@ -204,6 +204,13 @@ if MONOLITHIC
endif
endif
if USE_RDRAND
SUBDIRS += plugins/rdrand
if MONOLITHIC
libstrongswan_la_LIBADD += plugins/rdrand/libstrongswan-rdrand.la
endif
endif
if USE_RANDOM
SUBDIRS += plugins/random
if MONOLITHIC

View File

@ -0,0 +1,15 @@
INCLUDES = -I$(top_srcdir)/src/libstrongswan
AM_CFLAGS = -rdynamic
if MONOLITHIC
noinst_LTLIBRARIES = libstrongswan-rdrand.la
else
plugin_LTLIBRARIES = libstrongswan-rdrand.la
endif
libstrongswan_rdrand_la_SOURCES = \
rdrand_plugin.h rdrand_plugin.c
libstrongswan_rdrand_la_LDFLAGS = -module -avoid-version

View File

@ -0,0 +1,119 @@
/*
* Copyright (C) 2012 Martin Willi
* Copyright (C) 2012 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "rdrand_plugin.h"
#include <stdio.h>
#include <library.h>
#include <utils/debug.h>
typedef struct private_rdrand_plugin_t private_rdrand_plugin_t;
typedef enum cpuid_feature_t cpuid_feature_t;
/**
* private data of rdrand_plugin
*/
struct private_rdrand_plugin_t {
/**
* public functions
*/
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)
{
DBG1(DBG_LIB, "detected RDRAND support on %s CPU", vendor);
return TRUE;
}
}
DBG1(DBG_LIB, "no RDRAND support on %s CPU, disabled", vendor);
return FALSE;
}
METHOD(plugin_t, get_name, char*,
private_rdrand_plugin_t *this)
{
return "rdrand";
}
METHOD(plugin_t, destroy, void,
private_rdrand_plugin_t *this)
{
free(this);
}
/*
* see header file
*/
plugin_t *rdrand_plugin_create()
{
private_rdrand_plugin_t *this;
INIT(this,
.public = {
.plugin = {
.get_name = _get_name,
.reload = (void*)return_false,
.destroy = _destroy,
},
},
);
have_rdrand();
return &this->public.plugin;
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2012 Martin Willi
* Copyright (C) 2012 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup rdrand_p rdrand
* @ingroup plugins
*
* @defgroup rdrand_plugin rdrand_plugin
* @{ @ingroup rdrand_p
*/
#ifndef RDRAND_PLUGIN_H_
#define RDRAND_PLUGIN_H_
#include <plugins/plugin.h>
typedef struct rdrand_plugin_t rdrand_plugin_t;
/**
* Plugin providing random generators based on Intels RDRAND instruction.
*/
struct rdrand_plugin_t {
/**
* implements plugin interface
*/
plugin_t plugin;
};
#endif /** RDRAND_PLUGIN_H_ @}*/