diff --git a/demos/timing.c b/demos/timing.c index 81e8f9c..d1a17e4 100644 --- a/demos/timing.c +++ b/demos/timing.c @@ -669,7 +669,7 @@ static const struct { fprintf(stderr, "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); exit(EXIT_FAILURE); } - if ((err = dsa_make_key_ex(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) { + if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) { fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); exit(EXIT_FAILURE); } diff --git a/src/headers/tomcrypt_pk.h b/src/headers/tomcrypt_pk.h index 88c2cab..c24230a 100644 --- a/src/headers/tomcrypt_pk.h +++ b/src/headers/tomcrypt_pk.h @@ -452,7 +452,7 @@ int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_si int dsa_set_key(const unsigned char *pub, unsigned long publen, const unsigned char *priv, unsigned long privlen, dsa_key *key); -int dsa_make_key_ex(prng_state *prng, int wprng, dsa_key *key); +int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key); void dsa_free(dsa_key *key); diff --git a/src/pk/dsa/dsa_generate_key.c b/src/pk/dsa/dsa_generate_key.c new file mode 100644 index 0000000..33f68c7 --- /dev/null +++ b/src/pk/dsa/dsa_generate_key.c @@ -0,0 +1,47 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + */ +#include "tomcrypt.h" + +/** + @file dsa_make_key.c + DSA implementation, generate a DSA key +*/ + +#ifdef LTC_MDSA + +/** + Create a DSA key + @param prng An active PRNG state + @param wprng The index of the PRNG desired + @param key [in/out] Where to store the created key + @return CRYPT_OK if successful. +*/ +int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key) +{ + int err; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(ltc_mp.name != NULL); + + /* so now we have our DH structure, generator g, order q, modulus p + Now we need a random exponent [mod q] and it's power g^x mod p + */ + /* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */ + if ((err = rand_bn_range(key->x, key->q, prng, wprng)) != CRYPT_OK) { return err; } + if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; } + key->type = PK_PRIVATE; + + return CRYPT_OK; +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/pk/dsa/dsa_make_key.c b/src/pk/dsa/dsa_make_key.c index 708eb08..8ac08f8 100644 --- a/src/pk/dsa/dsa_make_key.c +++ b/src/pk/dsa/dsa_make_key.c @@ -15,31 +15,6 @@ #ifdef LTC_MDSA -/** - Create a DSA key - @param prng An active PRNG state - @param wprng The index of the PRNG desired - @param key [in/out] Where to store the created key - @return CRYPT_OK if successful. -*/ -int dsa_make_key_ex(prng_state *prng, int wprng, dsa_key *key) -{ - int err; - - LTC_ARGCHK(key != NULL); - LTC_ARGCHK(ltc_mp.name != NULL); - - /* so now we have our DH structure, generator g, order q, modulus p - Now we need a random exponent [mod q] and it's power g^x mod p - */ - /* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */ - if ((err = rand_bn_range(key->x, key->q, prng, wprng)) != CRYPT_OK) { return err; } - if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; } - key->type = PK_PRIVATE; - - return CRYPT_OK; -} - /** Old-style creation of a DSA key @param prng An active PRNG state @@ -54,7 +29,7 @@ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, int err; if ((err = dsa_generate_pqg(prng, wprng, group_size, modulus_size, key)) != CRYPT_OK) { return err; } - if ((err = dsa_make_key_ex(prng, wprng, key)) != CRYPT_OK) { return err; } + if ((err = dsa_generate_key(prng, wprng, key)) != CRYPT_OK) { return err; } return CRYPT_OK; } diff --git a/src/pk/dsa/dsa_set.c b/src/pk/dsa/dsa_set.c index 3f28b56..5c1e029 100755 --- a/src/pk/dsa/dsa_set.c +++ b/src/pk/dsa/dsa_set.c @@ -118,6 +118,11 @@ int dsa_set_key(const unsigned char *pub, unsigned long publen, int err; LTC_ARGCHK(key != NULL); + LTC_ARGCHK(key->x != NULL); + LTC_ARGCHK(key->y != NULL); + LTC_ARGCHK(key->p != NULL); + LTC_ARGCHK(key->g != NULL); + LTC_ARGCHK(key->q != NULL); LTC_ARGCHK(ltc_mp.name != NULL); if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)pub , publen)) != CRYPT_OK) { goto LBL_ERR; } diff --git a/tests/dsa_test.c b/tests/dsa_test.c index 7c01ee7..d871a29 100644 --- a/tests/dsa_test.c +++ b/tests/dsa_test.c @@ -208,7 +208,7 @@ static int _dsa_compat_test(void) /* try import dsaparam */ DO(dsa_set_pqg_dsaparam(dsaparam_der, sizeof(dsaparam_der), &key)); - DO(dsa_make_key_ex(&yarrow_prng, find_prng("yarrow"), &key)); + DO(dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)); /* verify it */ DO(dsa_verify_key(&key, &stat)); if (stat == 0) { @@ -257,7 +257,7 @@ int dsa_test(void) /* make a random key */ DO(dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), 20, 128, &key)); - DO(dsa_make_key_ex(&yarrow_prng, find_prng("yarrow"), &key)); + DO(dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)); /* verify it */ DO(dsa_verify_key(&key, &stat1));