gmp uses component builder to build public- from private-key

This commit is contained in:
Martin Willi
2009-08-26 11:23:51 +02:00
parent 8380503168
commit 831520d895
2 changed files with 37 additions and 47 deletions
@@ -115,7 +115,6 @@ struct private_gmp_rsa_private_key_t {
extern bool gmp_rsa_public_key_build_id(mpz_t n, mpz_t e,
identification_t **keyid,
identification_t **keyid_info);
extern gmp_rsa_public_key_t *gmp_rsa_public_key_create_from_n_e(mpz_t n, mpz_t e);
/**
* Auxiliary function overwriting private key material with zero bytes
@@ -381,12 +380,47 @@ static identification_t* get_id(private_gmp_rsa_private_key_t *this,
}
}
/**
* Convert a MP integer into a chunk_t
*/
chunk_t gmp_mpz_to_chunk(const mpz_t value)
{
chunk_t n;
n.len = 1 + mpz_sizeinbase(value, 2) / BITS_PER_BYTE;
n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, value);
if (n.ptr == NULL)
{ /* if we have zero in "value", gmp returns NULL */
n.len = 0;
}
return n;
}
/**
* Convert a MP integer into a DER coded ASN.1 object
*/
chunk_t gmp_mpz_to_asn1(const mpz_t value)
{
return asn1_wrap(ASN1_INTEGER, "m", gmp_mpz_to_chunk(value));
}
/**
* Implementation of gmp_rsa_private_key.get_public_key.
*/
static gmp_rsa_public_key_t* get_public_key(private_gmp_rsa_private_key_t *this)
static public_key_t* get_public_key(private_gmp_rsa_private_key_t *this)
{
return gmp_rsa_public_key_create_from_n_e(this->n, this->e);
chunk_t n, e;
public_key_t *public;
n = gmp_mpz_to_chunk(this->n);
e = gmp_mpz_to_chunk(this->e);
public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
BUILD_RSA_MODULUS, n, BUILD_RSA_PUB_EXP, e, BUILD_END);
chunk_free(&n);
chunk_free(&e);
return public;
}
/**
@@ -441,30 +475,6 @@ static bool belongs_to(private_gmp_rsa_private_key_t *this, public_key_t *public
return FALSE;
}
/**
* Convert a MP integer into a chunk_t
*/
chunk_t gmp_mpz_to_chunk(const mpz_t value)
{
chunk_t n;
n.len = 1 + mpz_sizeinbase(value, 2) / BITS_PER_BYTE;
n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, value);
if (n.ptr == NULL)
{ /* if we have zero in "value", gmp returns NULL */
n.len = 0;
}
return n;
}
/**
* Convert a MP integer into a DER coded ASN.1 object
*/
chunk_t gmp_mpz_to_asn1(const mpz_t value)
{
return asn1_wrap(ASN1_INTEGER, "m", gmp_mpz_to_chunk(value));
}
/**
* Implementation of private_key_t.get_encoding.
*/
@@ -568,26 +568,6 @@ bool gmp_rsa_public_key_build_id(mpz_t n, mpz_t e, identification_t **keyid,
return TRUE;
}
/**
* Create a public key from mpz values, used in gmp_rsa_private_key
*/
gmp_rsa_public_key_t *gmp_rsa_public_key_create_from_n_e(mpz_t n, mpz_t e)
{
private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
mpz_init_set(this->n, n);
mpz_init_set(this->e, e);
this->k = (mpz_sizeinbase(this->n, 2) + 7) / BITS_PER_BYTE;
if (!gmp_rsa_public_key_build_id(this->n, this->e,
&this->keyid, &this->keyid_info))
{
destroy(this);
return NULL;
}
return &this->public;
}
/**
* Load a public key from n and e
*/