xref: /openbsd-src/lib/libcrypto/dsa/dsa_lib.c (revision f84b1df5a16cdd762c93854218de246e79975d3b)
1 /* $OpenBSD: dsa_lib.c,v 1.34 2022/01/14 08:29:06 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60 
61 #include <stdio.h>
62 
63 #include <openssl/opensslconf.h>
64 
65 #include <openssl/asn1.h>
66 #include <openssl/bn.h>
67 #include <openssl/dsa.h>
68 #include <openssl/err.h>
69 
70 #ifndef OPENSSL_NO_DH
71 #include <openssl/dh.h>
72 #endif
73 #ifndef OPENSSL_NO_ENGINE
74 #include <openssl/engine.h>
75 #endif
76 
77 #include "dh_local.h"
78 #include "dsa_locl.h"
79 
80 static const DSA_METHOD *default_DSA_method = NULL;
81 
82 void
83 DSA_set_default_method(const DSA_METHOD *meth)
84 {
85 	default_DSA_method = meth;
86 }
87 
88 const DSA_METHOD *
89 DSA_get_default_method(void)
90 {
91 	if (!default_DSA_method)
92 		default_DSA_method = DSA_OpenSSL();
93 	return default_DSA_method;
94 }
95 
96 DSA *
97 DSA_new(void)
98 {
99 	return DSA_new_method(NULL);
100 }
101 
102 int
103 DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
104 {
105 	/*
106 	 * NB: The caller is specifically setting a method, so it's not up to us
107 	 * to deal with which ENGINE it comes from.
108 	 */
109         const DSA_METHOD *mtmp;
110         mtmp = dsa->meth;
111         if (mtmp->finish)
112 		mtmp->finish(dsa);
113 #ifndef OPENSSL_NO_ENGINE
114 	ENGINE_finish(dsa->engine);
115 	dsa->engine = NULL;
116 #endif
117         dsa->meth = meth;
118         if (meth->init)
119 		meth->init(dsa);
120         return 1;
121 }
122 
123 DSA *
124 DSA_new_method(ENGINE *engine)
125 {
126 	DSA *ret;
127 
128 	ret = malloc(sizeof(DSA));
129 	if (ret == NULL) {
130 		DSAerror(ERR_R_MALLOC_FAILURE);
131 		return NULL;
132 	}
133 	ret->meth = DSA_get_default_method();
134 #ifndef OPENSSL_NO_ENGINE
135 	if (engine) {
136 		if (!ENGINE_init(engine)) {
137 			DSAerror(ERR_R_ENGINE_LIB);
138 			free(ret);
139 			return NULL;
140 		}
141 		ret->engine = engine;
142 	} else
143 		ret->engine = ENGINE_get_default_DSA();
144 	if (ret->engine) {
145 		ret->meth = ENGINE_get_DSA(ret->engine);
146 		if (ret->meth == NULL) {
147 			DSAerror(ERR_R_ENGINE_LIB);
148 			ENGINE_finish(ret->engine);
149 			free(ret);
150 			return NULL;
151 		}
152 	}
153 #endif
154 
155 	ret->pad = 0;
156 	ret->version = 0;
157 	ret->p = NULL;
158 	ret->q = NULL;
159 	ret->g = NULL;
160 
161 	ret->pub_key = NULL;
162 	ret->priv_key = NULL;
163 
164 	ret->kinv = NULL;
165 	ret->r = NULL;
166 	ret->method_mont_p = NULL;
167 
168 	ret->references = 1;
169 	ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
170 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
171 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
172 #ifndef OPENSSL_NO_ENGINE
173 		ENGINE_finish(ret->engine);
174 #endif
175 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
176 		free(ret);
177 		ret = NULL;
178 	}
179 
180 	return ret;
181 }
182 
183 void
184 DSA_free(DSA *r)
185 {
186 	int i;
187 
188 	if (r == NULL)
189 		return;
190 
191 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA);
192 	if (i > 0)
193 		return;
194 
195 	if (r->meth->finish)
196 		r->meth->finish(r);
197 #ifndef OPENSSL_NO_ENGINE
198 	ENGINE_finish(r->engine);
199 #endif
200 
201 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
202 
203 	BN_clear_free(r->p);
204 	BN_clear_free(r->q);
205 	BN_clear_free(r->g);
206 	BN_clear_free(r->pub_key);
207 	BN_clear_free(r->priv_key);
208 	BN_clear_free(r->kinv);
209 	BN_clear_free(r->r);
210 	free(r);
211 }
212 
213 int
214 DSA_up_ref(DSA *r)
215 {
216 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
217 	return i > 1 ? 1 : 0;
218 }
219 
220 int
221 DSA_size(const DSA *r)
222 {
223 	int ret, i;
224 	ASN1_INTEGER bs;
225 	unsigned char buf[4];	/* 4 bytes looks really small.
226 				   However, i2d_ASN1_INTEGER() will not look
227 				   beyond the first byte, as long as the second
228 				   parameter is NULL. */
229 
230 	i = BN_num_bits(r->q);
231 	bs.length = (i + 7) / 8;
232 	bs.data = buf;
233 	bs.type = V_ASN1_INTEGER;
234 	/* If the top bit is set the asn1 encoding is 1 larger. */
235 	buf[0] = 0xff;
236 
237 	i = i2d_ASN1_INTEGER(&bs, NULL);
238 	i += i; /* r and s */
239 	ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
240 	return ret;
241 }
242 
243 int
244 DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
245     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
246 {
247 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
248 	    new_func, dup_func, free_func);
249 }
250 
251 int
252 DSA_set_ex_data(DSA *d, int idx, void *arg)
253 {
254 	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
255 }
256 
257 void *
258 DSA_get_ex_data(DSA *d, int idx)
259 {
260 	return CRYPTO_get_ex_data(&d->ex_data, idx);
261 }
262 
263 #ifndef OPENSSL_NO_DH
264 DH *
265 DSA_dup_DH(const DSA *r)
266 {
267 	/*
268 	 * DSA has p, q, g, optional pub_key, optional priv_key.
269 	 * DH has p, optional length, g, optional pub_key, optional priv_key,
270 	 * optional q.
271 	 */
272 	DH *ret = NULL;
273 
274 	if (r == NULL)
275 		goto err;
276 	ret = DH_new();
277 	if (ret == NULL)
278 		goto err;
279 	if (r->p != NULL)
280 		if ((ret->p = BN_dup(r->p)) == NULL)
281 			goto err;
282 	if (r->q != NULL) {
283 		ret->length = BN_num_bits(r->q);
284 		if ((ret->q = BN_dup(r->q)) == NULL)
285 			goto err;
286 	}
287 	if (r->g != NULL)
288 		if ((ret->g = BN_dup(r->g)) == NULL)
289 			goto err;
290 	if (r->pub_key != NULL)
291 		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
292 			goto err;
293 	if (r->priv_key != NULL)
294 		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
295 			goto err;
296 
297 	return ret;
298 
299 err:
300 	DH_free(ret);
301 	return NULL;
302 }
303 #endif
304 
305 void
306 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
307 {
308 	if (p != NULL)
309 		*p = d->p;
310 	if (q != NULL)
311 		*q = d->q;
312 	if (g != NULL)
313 		*g = d->g;
314 }
315 
316 int
317 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
318 {
319 	if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
320 	    (d->g == NULL && g == NULL))
321 		return 0;
322 
323 	if (p != NULL) {
324 		BN_free(d->p);
325 		d->p = p;
326 	}
327 	if (q != NULL) {
328 		BN_free(d->q);
329 		d->q = q;
330 	}
331 	if (g != NULL) {
332 		BN_free(d->g);
333 		d->g = g;
334 	}
335 
336 	return 1;
337 }
338 
339 void
340 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
341 {
342 	if (pub_key != NULL)
343 		*pub_key = d->pub_key;
344 	if (priv_key != NULL)
345 		*priv_key = d->priv_key;
346 }
347 
348 int
349 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
350 {
351 	if (d->pub_key == NULL && pub_key == NULL)
352 		return 0;
353 
354 	if (pub_key != NULL) {
355 		BN_free(d->pub_key);
356 		d->pub_key = pub_key;
357 	}
358 	if (priv_key != NULL) {
359 		BN_free(d->priv_key);
360 		d->priv_key = priv_key;
361 	}
362 
363 	return 1;
364 }
365 
366 const BIGNUM *
367 DSA_get0_p(const DSA *d)
368 {
369 	return d->p;
370 }
371 
372 const BIGNUM *
373 DSA_get0_q(const DSA *d)
374 {
375 	return d->q;
376 }
377 
378 const BIGNUM *
379 DSA_get0_g(const DSA *d)
380 {
381 	return d->g;
382 }
383 
384 const BIGNUM *
385 DSA_get0_pub_key(const DSA *d)
386 {
387 	return d->pub_key;
388 }
389 
390 const BIGNUM *
391 DSA_get0_priv_key(const DSA *d)
392 {
393 	return d->priv_key;
394 }
395 
396 void
397 DSA_clear_flags(DSA *d, int flags)
398 {
399 	d->flags &= ~flags;
400 }
401 
402 int
403 DSA_test_flags(const DSA *d, int flags)
404 {
405 	return d->flags & flags;
406 }
407 
408 void
409 DSA_set_flags(DSA *d, int flags)
410 {
411 	d->flags |= flags;
412 }
413 
414 ENGINE *
415 DSA_get0_engine(DSA *d)
416 {
417 	return d->engine;
418 }
419 
420 int
421 DSA_bits(const DSA *dsa)
422 {
423 	return BN_num_bits(dsa->p);
424 }
425