botan: Fixes, code style changes plus some refactorings

Some changes rely on newly added FFI functions in Botan's master
branch.
This commit is contained in:
Tobias Brunner
2018-09-12 16:25:00 +02:00
parent 13f113f7a9
commit de2a24310c
19 changed files with 694 additions and 1092 deletions
@@ -70,22 +70,27 @@ struct private_botan_diffie_hellman_t {
* Modulus
*/
botan_mp_t p;
};
/**
* Load a DH private key
*/
bool load_private_key(private_botan_diffie_hellman_t *this, chunk_t value)
{
botan_mp_t xa;
if (chunk_to_botan_mp(value, &xa))
if (!chunk_to_botan_mp(value, &xa))
{
return FALSE;
}
if (botan_privkey_destroy(this->dh_key) ||
botan_privkey_load_dh (&this->dh_key, this->p, this->g, xa))
botan_privkey_load_dh(&this->dh_key, this->p, this->g, xa))
{
botan_mp_destroy(xa);
return FALSE;
}
botan_mp_destroy(xa);
return TRUE;
}
@@ -99,25 +104,29 @@ METHOD(diffie_hellman_t, set_other_public_value, bool,
return FALSE;
}
chunk_clear(&this->shared_secret);
botan_pk_op_key_agreement_create(&op, this->dh_key, "Raw", 0);
/* get shared secret key size */
if (botan_pk_op_key_agreement(op, NULL, &this->shared_secret.len, value.ptr,
value.len, NULL, 0)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
if (botan_pk_op_key_agreement_create(&op, this->dh_key, "Raw", 0))
{
return FALSE;
}
chunk_clear(&this->shared_secret);
if (botan_pk_op_key_agreement_size(op, &this->shared_secret.len))
{
botan_pk_op_key_agreement_destroy(op);
return FALSE;
}
this->shared_secret = chunk_alloc(this->shared_secret.len);
if (botan_pk_op_key_agreement(op, this->shared_secret.ptr,
&this->shared_secret.len, value.ptr,
value.len, NULL, 0))
&this->shared_secret.len, value.ptr,
value.len, NULL, 0))
{
chunk_clear(&this->shared_secret);
botan_pk_op_key_agreement_destroy(op);
return FALSE;
}
botan_pk_op_key_agreement_destroy(op);
return TRUE;
}
@@ -128,36 +137,35 @@ METHOD(diffie_hellman_t, get_my_public_value, bool,
/* get key size of public key first */
if (botan_pk_op_key_agreement_export_public(this->dh_key, NULL, &value->len)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
{
return FALSE;
}
*value = chunk_alloc(value->len);
if (botan_pk_op_key_agreement_export_public(this->dh_key, value->ptr,
&value->len))
&value->len))
{
chunk_clear(value);
return FALSE;
}
return TRUE;
}
METHOD(diffie_hellman_t, set_private_value, bool,
private_botan_diffie_hellman_t *this, chunk_t value)
{
chunk_clear(&this->shared_secret);
return load_private_key(this, value);
}
METHOD(diffie_hellman_t, get_shared_secret, bool,
private_botan_diffie_hellman_t *this, chunk_t *secret)
{
if (this->shared_secret.len == 0)
if (!this->shared_secret.len)
{
return FALSE;
}
*secret = chunk_clone(this->shared_secret);
return TRUE;
}
@@ -181,10 +189,12 @@ METHOD(diffie_hellman_t, destroy, void,
/*
* Generic internal constructor
*/
botan_diffie_hellman_t *create_generic(diffie_hellman_group_t group,
chunk_t g, chunk_t p)
static botan_diffie_hellman_t *create_generic(diffie_hellman_group_t group,
chunk_t g, chunk_t p, size_t exp_len)
{
private_botan_diffie_hellman_t *this;
chunk_t random;
rng_t *rng;
INIT(this,
.public = {
@@ -200,42 +210,52 @@ botan_diffie_hellman_t *create_generic(diffie_hellman_group_t group,
.group = group,
);
if (chunk_to_botan_mp(p, &this->p))
if (!chunk_to_botan_mp(p, &this->p))
{
destroy(this);
return NULL;
}
if (chunk_to_botan_mp(g, &this->g))
if (!chunk_to_botan_mp(g, &this->g))
{
destroy(this);
return NULL;
}
chunk_t random;
rng_t *rng;
rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
if (rng && rng->allocate_bytes(rng, p.len, &random))
if (!rng || !rng->allocate_bytes(rng, p.len, &random))
{
rng->destroy(rng);
if (!load_private_key(this, random))
{
destroy(this);
chunk_clear(&random);
return NULL;
}
DESTROY_IF(rng);
destroy(this);
return NULL;
}
rng->destroy(rng);
if (!load_private_key(this, random))
{
chunk_clear(&random);
destroy(this);
return NULL;
}
chunk_clear(&random);
return &this->public;
}
/*
* Described in header.
*/
botan_diffie_hellman_t *
botan_diffie_hellman_create(diffie_hellman_group_t group)
botan_diffie_hellman_t *botan_diffie_hellman_create(
diffie_hellman_group_t group, ...)
{
diffie_hellman_params_t *params;
chunk_t g, p;
if (group == MODP_CUSTOM)
{
VA_ARGS_GET(group, g, p);
return create_generic(group, g, p);
}
params = diffie_hellman_get_params(group);
if (!params)
{
@@ -244,18 +264,4 @@ botan_diffie_hellman_create(diffie_hellman_group_t group)
return create_generic(group, params->generator, params->prime);
}
/*
* Described in header.
*/
botan_diffie_hellman_t *
botan_diffie_hellman_create_custom(diffie_hellman_group_t group, chunk_t g,
chunk_t p)
{
if (group == MODP_CUSTOM)
{
return create_generic(group, g, p);
}
return NULL;
}
#endif