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
10 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
13 #include "internal/cryptlib.h"
14 #include <openssl/bn.h>
16 #include <openssl/asn1.h>
17 #include <openssl/engine.h>
18 #include <openssl/dh.h>
22 return DSA_new_method(NULL);
25 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
28 * NB: The caller is specifically setting a method, so it's not up to us
29 * to deal with which ENGINE it comes from.
31 const DSA_METHOD *mtmp;
35 #ifndef OPENSSL_NO_ENGINE
36 ENGINE_finish(dsa->engine);
45 const DSA_METHOD *DSA_get_method(DSA *d)
50 DSA *DSA_new_method(ENGINE *engine)
52 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
55 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
60 ret->lock = CRYPTO_THREAD_lock_new();
61 if (ret->lock == NULL) {
62 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
67 ret->meth = DSA_get_default_method();
68 #ifndef OPENSSL_NO_ENGINE
69 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
71 if (!ENGINE_init(engine)) {
72 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
77 ret->engine = ENGINE_get_default_DSA();
79 ret->meth = ENGINE_get_DSA(ret->engine);
80 if (ret->meth == NULL) {
81 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
87 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
89 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
92 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
93 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
102 void DSA_free(DSA *r)
109 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
110 REF_PRINT_COUNT("DSA", r);
113 REF_ASSERT_ISNT(i < 0);
117 #ifndef OPENSSL_NO_ENGINE
118 ENGINE_finish(r->engine);
121 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
123 CRYPTO_THREAD_lock_free(r->lock);
128 BN_clear_free(r->pub_key);
129 BN_clear_free(r->priv_key);
133 int DSA_up_ref(DSA *r)
137 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
140 REF_PRINT_COUNT("DSA", r);
141 REF_ASSERT_ISNT(i < 2);
142 return ((i > 1) ? 1 : 0);
145 int DSA_size(const DSA *r)
149 unsigned char buf[4]; /* 4 bytes looks really small. However,
150 * i2d_ASN1_INTEGER() will not look beyond
151 * the first byte, as long as the second
152 * parameter is NULL. */
154 i = BN_num_bits(r->q);
155 bs.length = (i + 7) / 8;
157 bs.type = V_ASN1_INTEGER;
158 /* If the top bit is set the asn1 encoding is 1 larger. */
161 i = i2d_ASN1_INTEGER(&bs, NULL);
162 i += i; /* r and s */
163 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
167 int DSA_set_ex_data(DSA *d, int idx, void *arg)
169 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
172 void *DSA_get_ex_data(DSA *d, int idx)
174 return (CRYPTO_get_ex_data(&d->ex_data, idx));
177 int DSA_security_bits(const DSA *d)
180 return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
184 #ifndef OPENSSL_NO_DH
185 DH *DSA_dup_DH(const DSA *r)
188 * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
189 * optional length, g, optional pub_key, optional priv_key, optional q.
193 BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
200 if (r->p != NULL || r->g != NULL || r->q != NULL) {
201 if (r->p == NULL || r->g == NULL || r->q == NULL) {
202 /* Shouldn't happen */
208 if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
213 if (r->pub_key != NULL) {
214 pub_key = BN_dup(r->pub_key);
217 if (r->priv_key != NULL) {
218 priv_key = BN_dup(r->priv_key);
219 if (priv_key == NULL)
222 if (!DH_set0_key(ret, pub_key, priv_key))
224 } else if (r->priv_key != NULL) {
225 /* Shouldn't happen */
242 void DSA_get0_pqg(const DSA *d,
243 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
253 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
255 /* If the fields p, q and g in d are NULL, the corresponding input
256 * parameters MUST be non-NULL.
258 if ((d->p == NULL && p == NULL)
259 || (d->q == NULL && q == NULL)
260 || (d->g == NULL && g == NULL))
279 void DSA_get0_key(const DSA *d,
280 const BIGNUM **pub_key, const BIGNUM **priv_key)
283 *pub_key = d->pub_key;
284 if (priv_key != NULL)
285 *priv_key = d->priv_key;
288 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
290 /* If the field pub_key in d is NULL, the corresponding input
291 * parameters MUST be non-NULL. The priv_key field may
294 if (d->pub_key == NULL && pub_key == NULL)
297 if (pub_key != NULL) {
299 d->pub_key = pub_key;
301 if (priv_key != NULL) {
302 BN_free(d->priv_key);
303 d->priv_key = priv_key;
309 void DSA_clear_flags(DSA *d, int flags)
314 int DSA_test_flags(const DSA *d, int flags)
316 return d->flags & flags;
319 void DSA_set_flags(DSA *d, int flags)
324 ENGINE *DSA_get0_engine(DSA *d)
329 int DSA_bits(const DSA *dsa)
331 return BN_num_bits(dsa->p);