]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_lib.c
2 * Copyright 1995-2017 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 "internal/refcount.h"
15 #include <openssl/bn.h>
17 #include <openssl/asn1.h>
18 #include <openssl/engine.h>
19 #include <openssl/dh.h>
23 return DSA_new_method(NULL
);
26 int DSA_set_method(DSA
*dsa
, const DSA_METHOD
*meth
)
29 * NB: The caller is specifically setting a method, so it's not up to us
30 * to deal with which ENGINE it comes from.
32 const DSA_METHOD
*mtmp
;
36 #ifndef OPENSSL_NO_ENGINE
37 ENGINE_finish(dsa
->engine
);
46 const DSA_METHOD
*DSA_get_method(DSA
*d
)
51 DSA
*DSA_new_method(ENGINE
*engine
)
53 DSA
*ret
= OPENSSL_zalloc(sizeof(*ret
));
56 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
61 ret
->lock
= CRYPTO_THREAD_lock_new();
62 if (ret
->lock
== NULL
) {
63 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
68 ret
->meth
= DSA_get_default_method();
69 #ifndef OPENSSL_NO_ENGINE
70 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
; /* early default init */
72 if (!ENGINE_init(engine
)) {
73 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
78 ret
->engine
= ENGINE_get_default_DSA();
80 ret
->meth
= ENGINE_get_DSA(ret
->engine
);
81 if (ret
->meth
== NULL
) {
82 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
88 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
;
90 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA
, ret
, &ret
->ex_data
))
93 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
94 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_INIT_FAIL
);
103 void DSA_free(DSA
*r
)
110 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
111 REF_PRINT_COUNT("DSA", r
);
114 REF_ASSERT_ISNT(i
< 0);
118 #ifndef OPENSSL_NO_ENGINE
119 ENGINE_finish(r
->engine
);
122 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA
, r
, &r
->ex_data
);
124 CRYPTO_THREAD_lock_free(r
->lock
);
129 BN_clear_free(r
->pub_key
);
130 BN_clear_free(r
->priv_key
);
134 int DSA_up_ref(DSA
*r
)
138 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
141 REF_PRINT_COUNT("DSA", r
);
142 REF_ASSERT_ISNT(i
< 2);
143 return ((i
> 1) ? 1 : 0);
146 int DSA_size(const DSA
*r
)
150 unsigned char buf
[4]; /* 4 bytes looks really small. However,
151 * i2d_ASN1_INTEGER() will not look beyond
152 * the first byte, as long as the second
153 * parameter is NULL. */
155 i
= BN_num_bits(r
->q
);
156 bs
.length
= (i
+ 7) / 8;
158 bs
.type
= V_ASN1_INTEGER
;
159 /* If the top bit is set the asn1 encoding is 1 larger. */
162 i
= i2d_ASN1_INTEGER(&bs
, NULL
);
163 i
+= i
; /* r and s */
164 ret
= ASN1_object_size(1, i
, V_ASN1_SEQUENCE
);
168 int DSA_set_ex_data(DSA
*d
, int idx
, void *arg
)
170 return (CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
));
173 void *DSA_get_ex_data(DSA
*d
, int idx
)
175 return (CRYPTO_get_ex_data(&d
->ex_data
, idx
));
178 int DSA_security_bits(const DSA
*d
)
181 return BN_security_bits(BN_num_bits(d
->p
), BN_num_bits(d
->q
));
185 #ifndef OPENSSL_NO_DH
186 DH
*DSA_dup_DH(const DSA
*r
)
189 * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
190 * optional length, g, optional pub_key, optional priv_key, optional q.
194 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
, *pub_key
= NULL
, *priv_key
= NULL
;
201 if (r
->p
!= NULL
|| r
->g
!= NULL
|| r
->q
!= NULL
) {
202 if (r
->p
== NULL
|| r
->g
== NULL
|| r
->q
== NULL
) {
203 /* Shouldn't happen */
209 if (p
== NULL
|| g
== NULL
|| q
== NULL
|| !DH_set0_pqg(ret
, p
, q
, g
))
214 if (r
->pub_key
!= NULL
) {
215 pub_key
= BN_dup(r
->pub_key
);
218 if (r
->priv_key
!= NULL
) {
219 priv_key
= BN_dup(r
->priv_key
);
220 if (priv_key
== NULL
)
223 if (!DH_set0_key(ret
, pub_key
, priv_key
))
225 } else if (r
->priv_key
!= NULL
) {
226 /* Shouldn't happen */
243 void DSA_get0_pqg(const DSA
*d
,
244 const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
254 int DSA_set0_pqg(DSA
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
256 /* If the fields p, q and g in d are NULL, the corresponding input
257 * parameters MUST be non-NULL.
259 if ((d
->p
== NULL
&& p
== NULL
)
260 || (d
->q
== NULL
&& q
== NULL
)
261 || (d
->g
== NULL
&& g
== NULL
))
280 void DSA_get0_key(const DSA
*d
,
281 const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
284 *pub_key
= d
->pub_key
;
285 if (priv_key
!= NULL
)
286 *priv_key
= d
->priv_key
;
289 int DSA_set0_key(DSA
*d
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
291 /* If the field pub_key in d is NULL, the corresponding input
292 * parameters MUST be non-NULL. The priv_key field may
295 if (d
->pub_key
== NULL
&& pub_key
== NULL
)
298 if (pub_key
!= NULL
) {
300 d
->pub_key
= pub_key
;
302 if (priv_key
!= NULL
) {
303 BN_free(d
->priv_key
);
304 d
->priv_key
= priv_key
;
310 void DSA_clear_flags(DSA
*d
, int flags
)
315 int DSA_test_flags(const DSA
*d
, int flags
)
317 return d
->flags
& flags
;
320 void DSA_set_flags(DSA
*d
, int flags
)
325 ENGINE
*DSA_get0_engine(DSA
*d
)
330 int DSA_bits(const DSA
*dsa
)
332 return BN_num_bits(dsa
->p
);