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>
20 static const DSA_METHOD *default_DSA_method = NULL;
22 void DSA_set_default_method(const DSA_METHOD *meth)
24 default_DSA_method = meth;
27 const DSA_METHOD *DSA_get_default_method(void)
29 if (!default_DSA_method)
30 default_DSA_method = DSA_OpenSSL();
31 return default_DSA_method;
36 return DSA_new_method(NULL);
39 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
42 * NB: The caller is specifically setting a method, so it's not up to us
43 * to deal with which ENGINE it comes from.
45 const DSA_METHOD *mtmp;
49 #ifndef OPENSSL_NO_ENGINE
50 ENGINE_finish(dsa->engine);
59 const DSA_METHOD *DSA_get_method(DSA *d)
64 DSA *DSA_new_method(ENGINE *engine)
66 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
69 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
74 ret->lock = CRYPTO_THREAD_lock_new();
75 if (ret->lock == NULL) {
76 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
81 ret->meth = DSA_get_default_method();
82 #ifndef OPENSSL_NO_ENGINE
83 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
85 if (!ENGINE_init(engine)) {
86 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
91 ret->engine = ENGINE_get_default_DSA();
93 ret->meth = ENGINE_get_DSA(ret->engine);
94 if (ret->meth == NULL) {
95 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
101 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
103 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
106 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
107 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
116 void DSA_free(DSA *r)
123 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
124 REF_PRINT_COUNT("DSA", r);
127 REF_ASSERT_ISNT(i < 0);
131 #ifndef OPENSSL_NO_ENGINE
132 ENGINE_finish(r->engine);
135 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
137 CRYPTO_THREAD_lock_free(r->lock);
142 BN_clear_free(r->pub_key);
143 BN_clear_free(r->priv_key);
147 int DSA_up_ref(DSA *r)
151 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
154 REF_PRINT_COUNT("DSA", r);
155 REF_ASSERT_ISNT(i < 2);
156 return ((i > 1) ? 1 : 0);
159 int DSA_size(const DSA *r)
163 unsigned char buf[4]; /* 4 bytes looks really small. However,
164 * i2d_ASN1_INTEGER() will not look beyond
165 * the first byte, as long as the second
166 * parameter is NULL. */
168 i = BN_num_bits(r->q);
169 bs.length = (i + 7) / 8;
171 bs.type = V_ASN1_INTEGER;
172 /* If the top bit is set the asn1 encoding is 1 larger. */
175 i = i2d_ASN1_INTEGER(&bs, NULL);
176 i += i; /* r and s */
177 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
181 int DSA_set_ex_data(DSA *d, int idx, void *arg)
183 return (CRYPTO_set_ex_data(&d->ex_data, idx, arg));
186 void *DSA_get_ex_data(DSA *d, int idx)
188 return (CRYPTO_get_ex_data(&d->ex_data, idx));
191 int DSA_security_bits(const DSA *d)
194 return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
198 #ifndef OPENSSL_NO_DH
199 DH *DSA_dup_DH(const DSA *r)
202 * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
203 * optional length, g, optional pub_key, optional priv_key, optional q.
207 BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
214 if (r->p != NULL || r->g != NULL || r->q != NULL) {
215 if (r->p == NULL || r->g == NULL || r->q == NULL) {
216 /* Shouldn't happen */
222 if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))
227 if (r->pub_key != NULL) {
228 pub_key = BN_dup(r->pub_key);
231 if (r->priv_key != NULL) {
232 priv_key = BN_dup(r->priv_key);
233 if (priv_key == NULL)
236 if (!DH_set0_key(ret, pub_key, priv_key))
238 } else if (r->priv_key != NULL) {
239 /* Shouldn't happen */
256 void DSA_get0_pqg(const DSA *d,
257 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
267 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
269 /* If the fields p, q and g in d are NULL, the corresponding input
270 * parameters MUST be non-NULL.
272 if ((d->p == NULL && p == NULL)
273 || (d->q == NULL && q == NULL)
274 || (d->g == NULL && g == NULL))
293 void DSA_get0_key(const DSA *d,
294 const BIGNUM **pub_key, const BIGNUM **priv_key)
297 *pub_key = d->pub_key;
298 if (priv_key != NULL)
299 *priv_key = d->priv_key;
302 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
304 /* If the field pub_key in d is NULL, the corresponding input
305 * parameters MUST be non-NULL. The priv_key field may
308 if (d->pub_key == NULL && pub_key == NULL)
311 if (pub_key != NULL) {
313 d->pub_key = pub_key;
315 if (priv_key != NULL) {
316 BN_free(d->priv_key);
317 d->priv_key = priv_key;
323 void DSA_clear_flags(DSA *d, int flags)
328 int DSA_test_flags(const DSA *d, int flags)
330 return d->flags & flags;
333 void DSA_set_flags(DSA *d, int flags)
338 ENGINE *DSA_get0_engine(DSA *d)
343 int DSA_bits(const DSA *dsa)
345 return BN_num_bits(dsa->p);