]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dsa/dsa_lib.c
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
) {
80 ret
->meth
= DSA_get_default_method();
81 #ifndef OPENSSL_NO_ENGINE
82 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
; /* early default init */
84 if (!ENGINE_init(engine
)) {
85 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
90 ret
->engine
= ENGINE_get_default_DSA();
92 ret
->meth
= ENGINE_get_DSA(ret
->engine
);
93 if (ret
->meth
== NULL
) {
94 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_ENGINE_LIB
);
100 ret
->flags
= ret
->meth
->flags
& ~DSA_FLAG_NON_FIPS_ALLOW
;
102 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA
, ret
, &ret
->ex_data
))
105 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
106 DSAerr(DSA_F_DSA_NEW_METHOD
, ERR_R_INIT_FAIL
);
115 void DSA_free(DSA
*r
)
122 CRYPTO_atomic_add(&r
->references
, -1, &i
, r
->lock
);
123 REF_PRINT_COUNT("DSA", r
);
126 REF_ASSERT_ISNT(i
< 0);
130 #ifndef OPENSSL_NO_ENGINE
131 ENGINE_finish(r
->engine
);
134 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA
, r
, &r
->ex_data
);
136 CRYPTO_THREAD_lock_free(r
->lock
);
141 BN_clear_free(r
->pub_key
);
142 BN_clear_free(r
->priv_key
);
146 int DSA_up_ref(DSA
*r
)
150 if (CRYPTO_atomic_add(&r
->references
, 1, &i
, r
->lock
) <= 0)
153 REF_PRINT_COUNT("DSA", r
);
154 REF_ASSERT_ISNT(i
< 2);
155 return ((i
> 1) ? 1 : 0);
158 int DSA_size(const DSA
*r
)
162 unsigned char buf
[4]; /* 4 bytes looks really small. However,
163 * i2d_ASN1_INTEGER() will not look beyond
164 * the first byte, as long as the second
165 * parameter is NULL. */
167 i
= BN_num_bits(r
->q
);
168 bs
.length
= (i
+ 7) / 8;
170 bs
.type
= V_ASN1_INTEGER
;
171 /* If the top bit is set the asn1 encoding is 1 larger. */
174 i
= i2d_ASN1_INTEGER(&bs
, NULL
);
175 i
+= i
; /* r and s */
176 ret
= ASN1_object_size(1, i
, V_ASN1_SEQUENCE
);
180 int DSA_set_ex_data(DSA
*d
, int idx
, void *arg
)
182 return (CRYPTO_set_ex_data(&d
->ex_data
, idx
, arg
));
185 void *DSA_get_ex_data(DSA
*d
, int idx
)
187 return (CRYPTO_get_ex_data(&d
->ex_data
, idx
));
190 int DSA_security_bits(const DSA
*d
)
193 return BN_security_bits(BN_num_bits(d
->p
), BN_num_bits(d
->q
));
197 #ifndef OPENSSL_NO_DH
198 DH
*DSA_dup_DH(const DSA
*r
)
201 * DSA has p, q, g, optional pub_key, optional priv_key. DH has p,
202 * optional length, g, optional pub_key, optional priv_key, optional q.
206 BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
, *pub_key
= NULL
, *priv_key
= NULL
;
213 if (r
->p
!= NULL
|| r
->g
!= NULL
|| r
->q
!= NULL
) {
214 if (r
->p
== NULL
|| r
->g
== NULL
|| r
->q
== NULL
) {
215 /* Shouldn't happen */
221 if (p
== NULL
|| g
== NULL
|| q
== NULL
|| !DH_set0_pqg(ret
, p
, q
, g
))
226 if (r
->pub_key
!= NULL
) {
227 pub_key
= BN_dup(r
->pub_key
);
230 if (r
->priv_key
!= NULL
) {
231 priv_key
= BN_dup(r
->priv_key
);
232 if (priv_key
== NULL
)
235 if (!DH_set0_key(ret
, pub_key
, priv_key
))
237 } else if (r
->priv_key
!= NULL
) {
238 /* Shouldn't happen */
255 void DSA_get0_pqg(const DSA
*d
, BIGNUM
**p
, BIGNUM
**q
, BIGNUM
**g
)
265 int DSA_set0_pqg(DSA
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
267 /* If the fields in d are NULL, the corresponding input
268 * parameters MUST be non-NULL.
270 * It is an error to give the results from get0 on d
271 * as input parameters.
273 if (p
== d
->p
|| q
== d
->q
|| g
== d
->g
)
292 void DSA_get0_key(const DSA
*d
, BIGNUM
**pub_key
, BIGNUM
**priv_key
)
295 *pub_key
= d
->pub_key
;
296 if (priv_key
!= NULL
)
297 *priv_key
= d
->priv_key
;
300 int DSA_set0_key(DSA
*d
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
302 /* If the pub_key in d is NULL, the corresponding input
303 * parameters MUST be non-NULL. The priv_key field may
306 * It is an error to give the results from get0 on d
307 * as input parameters.
309 if (d
->pub_key
== pub_key
310 || (d
->priv_key
!= NULL
&& priv_key
== d
->priv_key
))
313 if (pub_key
!= NULL
) {
315 d
->pub_key
= pub_key
;
317 if (priv_key
!= NULL
) {
318 BN_free(d
->priv_key
);
319 d
->priv_key
= priv_key
;
325 void DSA_clear_flags(DSA
*d
, int flags
)
330 int DSA_test_flags(const DSA
*d
, int flags
)
332 return d
->flags
& flags
;
335 void DSA_set_flags(DSA
*d
, int flags
)
340 ENGINE
*DSA_get0_engine(DSA
*d
)