Added support for MODP_CUSTOM to gmp plugin

This commit is contained in:
Martin Willi 2010-09-02 19:23:37 +02:00
parent 0abd558a65
commit 08d8b9405b
3 changed files with 52 additions and 17 deletions

View File

@ -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;
}

View File

@ -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_ @}*/

View File

@ -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,