1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13    @file dsa_make_key.c
14    DSA implementation, generate a DSA key
15 */
16 
17 #ifdef LTC_MDSA
18 
19 /**
20   Create a DSA key
21   @param prng          An active PRNG state
22   @param wprng         The index of the PRNG desired
23   @param key           [in/out] Where to store the created key
24   @return CRYPT_OK if successful.
25 */
dsa_generate_key(prng_state * prng,int wprng,dsa_key * key)26 int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key)
27 {
28   int err;
29 
30   LTC_ARGCHK(key         != NULL);
31   LTC_ARGCHK(ltc_mp.name != NULL);
32 
33   /* so now we have our DH structure, generator g, order q, modulus p
34      Now we need a random exponent [mod q] and it's power g^x mod p
35    */
36   /* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */
37   if ((err = rand_bn_upto(key->x, key->q, prng, wprng)) != CRYPT_OK)          { return err; }
38   if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK)            { return err; }
39   key->type = PK_PRIVATE;
40 
41   return CRYPT_OK;
42 }
43 
44 #endif
45 
46 /* ref:         $Format:%D$ */
47 /* git commit:  $Format:%H$ */
48 /* commit time: $Format:%ai$ */
49