2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include "internal/cryptlib.h"
19 #include <openssl/bn.h>
20 #include "crypto/dsa.h"
21 #include "dsa_local.h"
23 static int dsa_builtin_keygen(OPENSSL_CTX *libctx, DSA *dsa);
25 int DSA_generate_key(DSA *dsa)
27 if (dsa->meth->dsa_keygen != NULL)
28 return dsa->meth->dsa_keygen(dsa);
29 return dsa_builtin_keygen(NULL, dsa);
32 int dsa_generate_key_ctx(OPENSSL_CTX *libctx, DSA *dsa)
35 if (dsa->meth->dsa_keygen != NULL)
36 return dsa->meth->dsa_keygen(dsa);
38 return dsa_builtin_keygen(libctx, dsa);
41 static int dsa_builtin_keygen(OPENSSL_CTX *libctx, DSA *dsa)
45 BIGNUM *pub_key = NULL, *priv_key = NULL;
47 if ((ctx = BN_CTX_new_ex(libctx)) == NULL)
50 if (dsa->priv_key == NULL) {
51 if ((priv_key = BN_secure_new()) == NULL)
54 priv_key = dsa->priv_key;
57 if (!ffc_generate_private_key(ctx, &dsa->params, BN_num_bits(dsa->params.q),
61 if (dsa->pub_key == NULL) {
62 if ((pub_key = BN_new()) == NULL)
65 pub_key = dsa->pub_key;
69 BIGNUM *prk = BN_new();
73 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
75 /* pub_key = g ^ priv_key mod p */
76 if (!BN_mod_exp(pub_key, dsa->params.g, prk, dsa->params.p, ctx)) {
80 /* We MUST free prk before any further use of priv_key */
84 dsa->priv_key = priv_key;
85 dsa->pub_key = pub_key;
90 if (pub_key != dsa->pub_key)
92 if (priv_key != dsa->priv_key)