]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_key.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/bn.h>
15 #include <openssl/rand.h>
17 static int dsa_builtin_keygen(DSA
*dsa
);
19 int DSA_generate_key(DSA
*dsa
)
21 if (dsa
->meth
->dsa_keygen
)
22 return dsa
->meth
->dsa_keygen(dsa
);
23 return dsa_builtin_keygen(dsa
);
26 static int dsa_builtin_keygen(DSA
*dsa
)
30 BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
32 if ((ctx
= BN_CTX_new()) == NULL
)
35 if (dsa
->priv_key
== NULL
) {
36 if ((priv_key
= BN_secure_new()) == NULL
)
39 priv_key
= dsa
->priv_key
;
42 if (!BN_rand_range(priv_key
, dsa
->q
))
44 while (BN_is_zero(priv_key
)) ;
46 if (dsa
->pub_key
== NULL
) {
47 if ((pub_key
= BN_new()) == NULL
)
50 pub_key
= dsa
->pub_key
;
53 BIGNUM
*prk
= BN_new();
57 BN_with_flags(prk
, priv_key
, BN_FLG_CONSTTIME
);
59 if (!BN_mod_exp(pub_key
, dsa
->g
, prk
, dsa
->p
, ctx
)) {
63 /* We MUST free prk before any further use of priv_key */
67 dsa
->priv_key
= priv_key
;
68 dsa
->pub_key
= pub_key
;
72 if (pub_key
!= dsa
->pub_key
)
74 if (priv_key
!= dsa
->priv_key
)