aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libstrongswan/plugins/gmp/gmp_diffie_hellman.c53
-rw-r--r--src/libstrongswan/plugins/gmp/gmp_diffie_hellman.h11
-rw-r--r--src/libstrongswan/plugins/gmp/gmp_plugin.c5
3 files changed, 52 insertions, 17 deletions
diff --git a/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.c b/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.c
index 9f992c61c..e99502b27 100644
--- a/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.c
+++ b/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.c
@@ -189,21 +189,15 @@ METHOD(diffie_hellman_t, destroy, void,
free(this);
}
-/*
- * Described in header.
+/**
+ * Generic internal constructor
*/
-gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group)
+static gmp_diffie_hellman_t *create_generic(diffie_hellman_group_t group,
+ size_t exp_len, chunk_t g, chunk_t p)
{
private_gmp_diffie_hellman_t *this;
- diffie_hellman_params_t *params;
- rng_t *rng;
chunk_t random;
-
- params = diffie_hellman_get_params(group);
- if (!params)
- {
- return NULL;
- }
+ rng_t *rng;
INIT(this,
.public = {
@@ -216,7 +210,7 @@ gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group)
},
},
.group = group,
- .p_len = params->prime.len,
+ .p_len = p.len,
);
mpz_init(this->p);
@@ -225,9 +219,8 @@ gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group)
mpz_init(this->xa);
mpz_init(this->zz);
mpz_init(this->g);
-
- mpz_import(this->p, params->prime.len, 1, 1, 1, 0, params->prime.ptr);
- mpz_import(this->g, params->generator.len, 1, 1, 1, 0, params->generator.ptr);
+ mpz_import(this->g, g.len, 1, 1, 1, 0, g.ptr);
+ mpz_import(this->p, p.len, 1, 1, 1, 0, p.ptr);
rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
if (!rng)
@@ -238,10 +231,10 @@ gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group)
return NULL;
}
- rng->allocate_bytes(rng, params->exp_len, &random);
+ rng->allocate_bytes(rng, exp_len, &random);
rng->destroy(rng);
- if (params->exp_len == this->p_len)
+ if (exp_len == this->p_len)
{
/* achieve bitsof(p)-1 by setting MSB to 0 */
*random.ptr &= 0x7F;
@@ -256,3 +249,29 @@ gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group)
return &this->public;
}
+/*
+ * Described in header.
+ */
+gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group)
+{
+ diffie_hellman_params_t *params;
+
+ params = diffie_hellman_get_params(group);
+ if (!params)
+ {
+ return NULL;
+ }
+ return create_generic(group, params->exp_len,
+ params->generator, params->prime);
+}
+
+
+gmp_diffie_hellman_t *gmp_diffie_hellman_create_custom(
+ diffie_hellman_group_t group, chunk_t g, chunk_t p)
+{
+ if (group == MODP_CUSTOM)
+ {
+ return create_generic(MODP_CUSTOM, p.len, g, p);
+ }
+ return NULL;
+}
diff --git a/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.h b/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.h
index 2a54eebb1..6d73c0863 100644
--- a/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.h
+++ b/src/libstrongswan/plugins/gmp/gmp_diffie_hellman.h
@@ -45,5 +45,16 @@ struct gmp_diffie_hellman_t {
*/
gmp_diffie_hellman_t *gmp_diffie_hellman_create(diffie_hellman_group_t group);
+/**
+ * Creates a new gmp_diffie_hellman_t object for MODP_CUSTOM.
+ *
+ * @param group MODP_CUSTOM
+ * @param g generator
+ * @param p prime
+ * @return gmp_diffie_hellman_t object, NULL if not supported
+ */
+gmp_diffie_hellman_t *gmp_diffie_hellman_create_custom(
+ diffie_hellman_group_t group, chunk_t g, chunk_t p);
+
#endif /** GMP_DIFFIE_HELLMAN_H_ @}*/
diff --git a/src/libstrongswan/plugins/gmp/gmp_plugin.c b/src/libstrongswan/plugins/gmp/gmp_plugin.c
index 5081844e6..08900df2e 100644
--- a/src/libstrongswan/plugins/gmp/gmp_plugin.c
+++ b/src/libstrongswan/plugins/gmp/gmp_plugin.c
@@ -38,6 +38,8 @@ METHOD(plugin_t, destroy, void,
{
lib->crypto->remove_dh(lib->crypto,
(dh_constructor_t)gmp_diffie_hellman_create);
+ lib->crypto->remove_dh(lib->crypto,
+ (dh_constructor_t)gmp_diffie_hellman_create_custom);
lib->creds->remove_builder(lib->creds,
(builder_function_t)gmp_rsa_private_key_gen);
lib->creds->remove_builder(lib->creds,
@@ -85,6 +87,9 @@ plugin_t *gmp_plugin_create()
lib->crypto->add_dh(lib->crypto, MODP_768_BIT,
(dh_constructor_t)gmp_diffie_hellman_create);
+ lib->crypto->add_dh(lib->crypto, MODP_CUSTOM,
+ (dh_constructor_t)gmp_diffie_hellman_create_custom);
+
lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
(builder_function_t)gmp_rsa_private_key_gen);
lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,