Path: blob/master/libs/tomcrypt/src/pk/dsa/dsa_generate_key.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file dsa_make_key.c12DSA implementation, generate a DSA key13*/1415#ifdef LTC_MDSA1617/**18Create a DSA key19@param prng An active PRNG state20@param wprng The index of the PRNG desired21@param key [in/out] Where to store the created key22@return CRYPT_OK if successful.23*/24int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key)25{26int err;2728LTC_ARGCHK(key != NULL);29LTC_ARGCHK(ltc_mp.name != NULL);3031/* so now we have our DH structure, generator g, order q, modulus p32Now we need a random exponent [mod q] and it's power g^x mod p33*/34/* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */35if ((err = rand_bn_upto(key->x, key->q, prng, wprng)) != CRYPT_OK) { return err; }36if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; }37key->type = PK_PRIVATE;3839return CRYPT_OK;40}4142#endif434445