2 * Copyright 2020-2021 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"
16 #include <openssl/core_names.h>
17 #include <openssl/err.h>
18 #include "crypto/dsa.h"
21 * The intention with the "backend" source file is to offer backend support
22 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
23 * implementations alike.
26 int ossl_dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[])
28 const OSSL_PARAM *param_priv_key, *param_pub_key;
29 BIGNUM *priv_key = NULL, *pub_key = NULL;
35 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
37 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
39 /* It's ok if neither half is present */
40 if (param_priv_key == NULL && param_pub_key == NULL)
43 if (param_pub_key != NULL && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
45 if (param_priv_key != NULL && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
48 if (!DSA_set0_key(dsa, pub_key, priv_key))
54 BN_clear_free(priv_key);
60 DSA *ossl_dsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
61 OSSL_LIB_CTX *libctx, const char *propq)
63 const unsigned char *p, *pm;
67 const ASN1_STRING *pstr;
68 const X509_ALGOR *palg;
69 ASN1_INTEGER *privkey = NULL;
70 const BIGNUM *dsa_p, *dsa_g;
71 BIGNUM *dsa_pubkey = NULL, *dsa_privkey = NULL;
76 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
78 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
80 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
82 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
88 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
90 /* We have parameters now set private key */
91 if ((dsa_privkey = BN_secure_new()) == NULL
92 || !ASN1_INTEGER_to_BN(privkey, dsa_privkey)) {
93 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
96 /* Calculate public key */
97 if ((dsa_pubkey = BN_new()) == NULL) {
98 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
101 if ((ctx = BN_CTX_new()) == NULL) {
102 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
106 dsa_p = DSA_get0_p(dsa);
107 dsa_g = DSA_get0_g(dsa);
108 BN_set_flags(dsa_privkey, BN_FLG_CONSTTIME);
109 if (!BN_mod_exp(dsa_pubkey, dsa_g, dsa_privkey, dsa_p, ctx)) {
110 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
113 DSA_set0_key(dsa, dsa_pubkey, dsa_privkey);
118 ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
120 BN_free(dsa_privkey);
126 ASN1_STRING_clear_free(privkey);