clean-up a bit around DSA

* comments
* dsa_test()
* order of alloc/free of key parts
This commit is contained in:
Steffen Jaeckel 2017-06-28 16:07:32 +02:00
parent 1cce065676
commit 35e0c5fc71
8 changed files with 34 additions and 28 deletions

View File

@ -129,7 +129,7 @@ int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
int rsa_set_key(const unsigned char *N, unsigned long Nlen,
const unsigned char *e, unsigned long elen,
const unsigned char *d, unsigned long dlen, /* is NULL for public keys */
const unsigned char *d, unsigned long dlen,
rsa_key *key);
int rsa_set_factors(const unsigned char *p, unsigned long plen,
const unsigned char *q, unsigned long qlen,

View File

@ -10,15 +10,15 @@
/**
@file radix_to_bin.c
Convert an MPI from a specific radix to binary data.
Convert data from a specific radix to binary.
Steffen Jaeckel
*/
/**
Convert an MPI from a specific radix to binary data
Convert data from a specific radix to binary
@param in The input
@param radix The radix of the input
@param radix The radix of the input 2..64
@param out The output buffer
@param len [in/out] The length of the output buffer

View File

@ -22,7 +22,7 @@
void dsa_free(dsa_key *key)
{
LTC_ARGCHKVD(key != NULL);
mp_cleanup_multi(&key->g, &key->q, &key->p, &key->x, &key->y, NULL);
mp_cleanup_multi(&key->y, &key->x, &key->q, &key->g, &key->p, NULL);
key->type = key->qord = 0;
}

View File

@ -225,7 +225,7 @@ int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_si
LTC_ARGCHK(ltc_mp.name != NULL);
/* init mp_ints */
if ((err = mp_init_multi(&key->g, &key->q, &key->p, &key->x, &key->y, NULL)) != CRYPT_OK) {
if ((err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL)) != CRYPT_OK) {
return err;
}
/* generate params */

View File

@ -125,7 +125,7 @@ LBL_OK:
return CRYPT_OK;
LBL_ERR:
mp_clear_multi(key->p, key->g, key->q, key->x, key->y, NULL);
dsa_free(key);
return err;
}

View File

@ -12,7 +12,7 @@
#ifdef LTC_MDSA
/**
Import DSA public or private key from raw numbers
Import DSA's p, q & g from raw numbers
@param p DSA's p in binary representation
@param q DSA's q in binary representation
@param g DSA's g in binary representation

View File

@ -25,7 +25,7 @@
*/
int rsa_set_key(const unsigned char *N, unsigned long Nlen,
const unsigned char *e, unsigned long elen,
const unsigned char *d, unsigned long dlen, /* is NULL for public keys */
const unsigned char *d, unsigned long dlen,
rsa_key *key)
{
int err;

View File

@ -112,21 +112,27 @@ static int _dsa_compat_test(void)
x = sizeof(tmp);
DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
DO((x == sizeof(openssl_priv_dsa))?CRYPT_OK:CRYPT_ERROR);
DO((memcmp(tmp, openssl_priv_dsa, sizeof(openssl_priv_dsa)) == 0)?CRYPT_OK:CRYPT_ERROR);
if (compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
"DSA private export failed from dsa_import(priv_key)\n", 0)) {
return CRYPT_FAIL_TESTVECTOR;
}
x = sizeof(tmp);
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
DO((x == sizeof(openssl_pub_dsa))?CRYPT_OK:CRYPT_ERROR);
DO((memcmp(tmp, openssl_pub_dsa, sizeof(openssl_pub_dsa)) == 0)?CRYPT_OK:CRYPT_ERROR);
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
"DSA public export failed from dsa_import(priv_key)\n", 0)) {
return CRYPT_FAIL_TESTVECTOR;
}
dsa_free(&key);
DO(dsa_import(openssl_pub_dsa, sizeof(openssl_pub_dsa), &key));
x = sizeof(tmp);
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
DO((x == sizeof(openssl_pub_dsa))?CRYPT_OK:CRYPT_ERROR);
DO((memcmp(tmp, openssl_pub_dsa, sizeof(openssl_pub_dsa)) == 0)?CRYPT_OK:CRYPT_ERROR);
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
"DSA public export failed from dsa_import(pub_key)\n", 0)) {
return CRYPT_FAIL_TESTVECTOR;
}
dsa_free(&key);
/* try import private key from raw hexadecimal numbers */
@ -148,9 +154,9 @@ static int _dsa_compat_test(void)
&key));
len = sizeof(buf);
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
if (len != sizeof(openssl_priv_dsa) || memcmp(buf, openssl_priv_dsa, len)) {
fprintf(stderr, "DSA private export failed to match dsa_import_radix(16, ..)\n");
return 1;
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
"DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", 0)) {
return CRYPT_FAIL_TESTVECTOR;
}
dsa_free(&key);
@ -164,13 +170,13 @@ static int _dsa_compat_test(void)
&key));
len = sizeof(buf);
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
if (len != sizeof(openssl_pub_dsa) || memcmp(buf, openssl_pub_dsa, len)) {
fprintf(stderr, "DSA public export failed to match dsa_import_radix(16, ..)\n");
return 1;
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
"DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", 0)) {
return CRYPT_FAIL_TESTVECTOR;
}
dsa_free(&key);
return 0;
return CRYPT_OK;
}
int dsa_test(void)
@ -181,7 +187,7 @@ int dsa_test(void)
dsa_key key = LTC_DSA_KEY_INITIALIZER;
dsa_key key2 = LTC_DSA_KEY_INITIALIZER;
_dsa_compat_test();
DO(_dsa_compat_test());
/* make a random key */
DO(dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), 20, 128, &key));