curve25519: Add a plugin providing Curve25519 DH using backend drivers

This commit is contained in:
Martin Willi 2014-08-08 16:22:56 +02:00 committed by Andreas Steffen
parent 2ac95123bb
commit 7f9bfacd5a
9 changed files with 473 additions and 0 deletions

View File

@ -133,6 +133,7 @@ ARG_DISBL_SET([fips-prf], [disable FIPS PRF software implementation plugin
ARG_ENABL_SET([gcm], [enables the GCM AEAD wrapper crypto plugin.])
ARG_ENABL_SET([gcrypt], [enables the libgcrypt plugin.])
ARG_DISBL_SET([gmp], [disable GNU MP (libgmp) based crypto implementation plugin.])
ARG_ENABL_SET([curve25519], [enable Curve25519 Diffie-Hellman plugin.])
ARG_DISBL_SET([hmac], [disable HMAC crypto implementation plugin.])
ARG_ENABL_SET([md4], [enable MD4 software implementation plugin.])
ARG_DISBL_SET([md5], [disable MD5 software implementation plugin.])
@ -1357,6 +1358,7 @@ ADD_PLUGIN([gcrypt], [s charon scepclient pki scripts manager meds
ADD_PLUGIN([af-alg], [s charon scepclient pki scripts medsrv attest nm cmd aikgen])
ADD_PLUGIN([fips-prf], [s charon nm cmd])
ADD_PLUGIN([gmp], [s charon scepclient pki scripts manager medsrv attest nm cmd aikgen])
ADD_PLUGIN([curve25519], [s charon scripts nm cmd])
ADD_PLUGIN([agent], [s charon nm cmd])
ADD_PLUGIN([keychain], [s charon cmd])
ADD_PLUGIN([chapoly], [s charon scripts nm cmd])
@ -1491,6 +1493,7 @@ AM_CONDITIONAL(USE_SHA3, test x$sha3 = xtrue)
AM_CONDITIONAL(USE_MGF1, test x$mgf1 = xtrue)
AM_CONDITIONAL(USE_FIPS_PRF, test x$fips_prf = xtrue)
AM_CONDITIONAL(USE_GMP, test x$gmp = xtrue)
AM_CONDITIONAL(USE_CURVE25519, test x$curve25519 = xtrue)
AM_CONDITIONAL(USE_RDRAND, test x$rdrand = xtrue)
AM_CONDITIONAL(USE_AESNI, test x$aesni = xtrue)
AM_CONDITIONAL(USE_RANDOM, test x$random = xtrue)
@ -1748,6 +1751,7 @@ AC_CONFIG_FILES([
src/libstrongswan/plugins/mgf1/Makefile
src/libstrongswan/plugins/fips_prf/Makefile
src/libstrongswan/plugins/gmp/Makefile
src/libstrongswan/plugins/curve25519/Makefile
src/libstrongswan/plugins/rdrand/Makefile
src/libstrongswan/plugins/aesni/Makefile
src/libstrongswan/plugins/random/Makefile

View File

@ -313,6 +313,13 @@ if MONOLITHIC
endif
endif
if USE_CURVE25519
SUBDIRS += plugins/curve25519
if MONOLITHIC
libstrongswan_la_LIBADD += plugins/curve25519/libstrongswan-curve25519.la
endif
endif
if USE_RDRAND
SUBDIRS += plugins/rdrand
if MONOLITHIC

View File

@ -0,0 +1,18 @@
AM_CPPFLAGS = \
-I$(top_srcdir)/src/libstrongswan
AM_CFLAGS = \
$(PLUGIN_CFLAGS)
if MONOLITHIC
noinst_LTLIBRARIES = libstrongswan-curve25519.la
else
plugin_LTLIBRARIES = libstrongswan-curve25519.la
endif
libstrongswan_curve25519_la_SOURCES = \
curve25519_dh.h curve25519_dh.c \
curve25519_drv.h curve25519_drv.c \
curve25519_plugin.h curve25519_plugin.c
libstrongswan_curve25519_la_LDFLAGS = -module -avoid-version

View File

@ -0,0 +1,174 @@
/*
* Copyright (C) 2015 Martin Willi
* Copyright (C) 2015 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 <string.h>
#include <stdint.h>
#include "curve25519_dh.h"
#include "curve25519_drv.h"
typedef struct private_curve25519_dh_t private_curve25519_dh_t;
/**
* Private data of an curve25519_dh_t object.
*/
struct private_curve25519_dh_t {
/**
* Public curve25519_dh_t interface.
*/
curve25519_dh_t public;
/**
* Shared key, if computed
*/
u_char shared[CURVE25519_KEY_SIZE];
/**
* TRUE if shared secret is computed
*/
bool computed;
/**
* Curve25519 backend
*/
curve25519_drv_t *drv;
};
/**
* Generate a valid Curve25519 key
*/
static bool generate_key(private_curve25519_dh_t *this)
{
u_char key[CURVE25519_KEY_SIZE];
rng_t *rng;
rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
if (!rng)
{
DBG1(DBG_LIB, "no RNG found for quality %N",
rng_quality_names, RNG_STRONG);
return FALSE;
}
if (!rng->get_bytes(rng, CURVE25519_KEY_SIZE, key))
{
rng->destroy(rng);
return FALSE;
}
rng->destroy(rng);
return this->drv->set_key(this->drv, key);
}
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_curve25519_dh_t *this, chunk_t value)
{
if (value.len == CURVE25519_KEY_SIZE)
{
if (this->drv->curve25519(this->drv, value.ptr, this->shared))
{
this->computed = TRUE;
return TRUE;
}
}
return FALSE;
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
private_curve25519_dh_t *this, chunk_t *value)
{
u_char basepoint[CURVE25519_KEY_SIZE] = { 9 };
*value = chunk_alloc(CURVE25519_KEY_SIZE);
if (this->drv->curve25519(this->drv, basepoint, value->ptr))
{
return TRUE;
}
free(value->ptr);
return FALSE;
}
METHOD(diffie_hellman_t, set_private_value, bool,
private_curve25519_dh_t *this, chunk_t value)
{
if (value.len != CURVE25519_KEY_SIZE)
{
return FALSE;
}
return this->drv->set_key(this->drv, value.ptr);
}
METHOD(diffie_hellman_t, get_shared_secret, bool,
private_curve25519_dh_t *this, chunk_t *secret)
{
if (!this->computed)
{
return FALSE;
}
*secret = chunk_clone(chunk_from_thing(this->shared));
return TRUE;
}
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
private_curve25519_dh_t *this)
{
return CURVE_25519;
}
METHOD(diffie_hellman_t, destroy, void,
private_curve25519_dh_t *this)
{
this->drv->destroy(this->drv);
free(this);
}
/*
* Described in header.
*/
curve25519_dh_t *curve25519_dh_create(diffie_hellman_group_t group)
{
private_curve25519_dh_t *this;
if (group != CURVE_25519)
{
return FALSE;
}
INIT(this,
.public = {
.dh = {
.get_shared_secret = _get_shared_secret,
.set_other_public_value = _set_other_public_value,
.get_my_public_value = _get_my_public_value,
.set_private_value = _set_private_value,
.get_dh_group = _get_dh_group,
.destroy = _destroy,
},
},
.drv = curve25519_drv_probe(),
);
if (!this->drv)
{
free(this);
return NULL;
}
if (!generate_key(this))
{
destroy(this);
return NULL;
}
return &this->public;
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 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 curve25519_dh curve25519_dh
* @{ @ingroup curve25519_p
*/
#ifndef CURVE25519_DH_H_
#define CURVE25519_DH_H_
typedef struct curve25519_dh_t curve25519_dh_t;
#include <library.h>
/**
* Diffie-Hellman implementation using Curve25519.
*/
struct curve25519_dh_t {
/**
* Implements diffie_hellman_t interface.
*/
diffie_hellman_t dh;
};
/**
* Creates a new curve25519_dh_t object.
*
* @param group DH group, CURVE_25519
* @return curve25519_dh_t object, NULL on error
*/
curve25519_dh_t *curve25519_dh_create(diffie_hellman_group_t group);
#endif /** CURVE25519_DH_H_ @}*/

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2015 Martin Willi
* Copyright (C) 2015 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 "curve25519_drv.h"
typedef curve25519_drv_t*(*curve25519_drv_create)();
/**
* See header.
*/
curve25519_drv_t *curve25519_drv_probe()
{
curve25519_drv_create drivers[] = {
};
curve25519_drv_t *driver;
int i;
for (i = 0; i < countof(drivers); i++)
{
driver = drivers[i]();
if (driver)
{
return driver;
}
}
return NULL;
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (C) 2015 Martin Willi
* Copyright (C) 2015 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 curve25519_drv curve25519_drv
* @{ @ingroup curve25519
*/
#ifndef CURVE25519_DRV_H_
#define CURVE25519_DRV_H_
typedef struct curve25519_drv_t curve25519_drv_t;
#include <library.h>
/**
* Private key size of Curve25519
*/
#define CURVE25519_KEY_SIZE 32
/**
* Backend driver abstraction for Curve25519.
*/
struct curve25519_drv_t {
/**
* Set the private key.
*
* @param key 32 byte private key, clamped
* @return TRUE if key set
*/
bool (*set_key)(curve25519_drv_t *this, u_char *key);
/**
* Calculate Curve25519 for the set key.
*
* @param in input data, 32 bytes
* @param out output data, 32 bytes
* @return TRUE if calculated
*/
bool (*curve25519)(curve25519_drv_t *this, u_char *in, u_char *out);
/**
* Destroy a curve25519_drv_t.
*/
void (*destroy)(curve25519_drv_t *this);
};
/**
* Create a curve25519_drv instance.
*/
curve25519_drv_t *curve25519_drv_probe();
#endif /** CURVE25519_DRV_H_ @}*/

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 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 "curve25519_plugin.h"
#include "curve25519_dh.h"
#include <library.h>
typedef struct private_curve25519_plugin_t private_curve25519_plugin_t;
/**
* private data of curve25519_plugin
*/
struct private_curve25519_plugin_t {
/**
* public functions
*/
curve25519_plugin_t public;
};
METHOD(plugin_t, get_name, char*,
private_curve25519_plugin_t *this)
{
return "curve25519";
}
METHOD(plugin_t, get_features, int,
private_curve25519_plugin_t *this, plugin_feature_t *features[])
{
static plugin_feature_t f[] = {
PLUGIN_REGISTER(DH, curve25519_dh_create),
PLUGIN_PROVIDE(DH, CURVE_25519),
PLUGIN_DEPENDS(RNG, RNG_STRONG),
};
*features = f;
return countof(f);
}
METHOD(plugin_t, destroy, void,
private_curve25519_plugin_t *this)
{
free(this);
}
/*
* see header file
*/
plugin_t *curve25519_plugin_create()
{
private_curve25519_plugin_t *this;
INIT(this,
.public = {
.plugin = {
.get_name = _get_name,
.get_features = _get_features,
.destroy = _destroy,
},
},
);
return &this->public.plugin;
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2014 Martin Willi
* Copyright (C) 2014 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 curve25519_p curve25519
* @ingroup plugins
*
* @defgroup curve25519_plugin curve25519_plugin
* @{ @ingroup curve25519_p
*/
#ifndef CURVE25519_PLUGIN_H_
#define CURVE25519_PLUGIN_H_
#include <plugins/plugin.h>
typedef struct curve25519_plugin_t curve25519_plugin_t;
/**
* Plugin providing a Curve25519 DH implementation
*/
struct curve25519_plugin_t {
/**
* implements plugin interface
*/
plugin_t plugin;
};
#endif /** CURVE25519_PLUGIN_H_ @}*/