GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/dh/dh_lib.c Lines: 48 83 57.8 %
Date: 2017-11-07 Branches: 12 32 37.5 %

Line Branch Exec Source
1
/* $OpenBSD: dh_lib.c,v 1.22 2017/01/29 17:49:22 beck 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
#include <stdio.h>
60
61
#include <openssl/opensslconf.h>
62
63
#include <openssl/bn.h>
64
#include <openssl/dh.h>
65
#include <openssl/err.h>
66
67
#ifndef OPENSSL_NO_ENGINE
68
#include <openssl/engine.h>
69
#endif
70
71
static const DH_METHOD *default_DH_method = NULL;
72
73
void
74
DH_set_default_method(const DH_METHOD *meth)
75
{
76
	default_DH_method = meth;
77
}
78
79
const DH_METHOD *
80
DH_get_default_method(void)
81
{
82
4708
	if (!default_DH_method)
83
1419
		default_DH_method = DH_OpenSSL();
84
2354
	return default_DH_method;
85
}
86
87
int
88
DH_set_method(DH *dh, const DH_METHOD *meth)
89
{
90
	/*
91
	 * NB: The caller is specifically setting a method, so it's not up to us
92
	 * to deal with which ENGINE it comes from.
93
	 */
94
        const DH_METHOD *mtmp;
95
96
        mtmp = dh->meth;
97
        if (mtmp->finish)
98
		mtmp->finish(dh);
99
#ifndef OPENSSL_NO_ENGINE
100
	if (dh->engine) {
101
		ENGINE_finish(dh->engine);
102
		dh->engine = NULL;
103
	}
104
#endif
105
        dh->meth = meth;
106
        if (meth->init)
107
		meth->init(dh);
108
        return 1;
109
}
110
111
DH *
112
DH_new(void)
113
{
114
4708
	return DH_new_method(NULL);
115
}
116
117
DH *
118
DH_new_method(ENGINE *engine)
119
{
120
	DH *ret;
121
122
4708
	ret = malloc(sizeof(DH));
123
2354
	if (ret == NULL) {
124
		DHerror(ERR_R_MALLOC_FAILURE);
125
		return NULL;
126
	}
127
128
2354
	ret->meth = DH_get_default_method();
129
#ifndef OPENSSL_NO_ENGINE
130
2354
	if (engine) {
131
		if (!ENGINE_init(engine)) {
132
			DHerror(ERR_R_ENGINE_LIB);
133
			free(ret);
134
			return NULL;
135
		}
136
		ret->engine = engine;
137
	} else
138
2354
		ret->engine = ENGINE_get_default_DH();
139
2354
	if(ret->engine) {
140
		ret->meth = ENGINE_get_DH(ret->engine);
141
		if (!ret->meth) {
142
			DHerror(ERR_R_ENGINE_LIB);
143
			ENGINE_finish(ret->engine);
144
			free(ret);
145
			return NULL;
146
		}
147
	}
148
#endif
149
150
2354
	ret->pad = 0;
151
2354
	ret->version = 0;
152
2354
	ret->p = NULL;
153
2354
	ret->g = NULL;
154
2354
	ret->length = 0;
155
2354
	ret->pub_key = NULL;
156
2354
	ret->priv_key = NULL;
157
2354
	ret->q = NULL;
158
2354
	ret->j = NULL;
159
2354
	ret->seed = NULL;
160
2354
	ret->seedlen = 0;
161
2354
	ret->counter = NULL;
162
2354
	ret->method_mont_p=NULL;
163
2354
	ret->references = 1;
164
2354
	ret->flags = ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
165
2354
	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
166

4708
	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
167
#ifndef OPENSSL_NO_ENGINE
168
		if (ret->engine)
169
			ENGINE_finish(ret->engine);
170
#endif
171
		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
172
		free(ret);
173
		ret = NULL;
174
	}
175
2354
	return ret;
176
2354
}
177
178
void
179
DH_free(DH *r)
180
{
181
	int i;
182
183
40356
	if (r == NULL)
184
19006
		return;
185
1172
	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
186
1172
	if (i > 0)
187
		return;
188
189
1172
	if (r->meth->finish)
190
1172
		r->meth->finish(r);
191
#ifndef OPENSSL_NO_ENGINE
192
1172
	if (r->engine)
193
		ENGINE_finish(r->engine);
194
#endif
195
196
1172
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
197
198
1172
	BN_clear_free(r->p);
199
1172
	BN_clear_free(r->g);
200
1172
	BN_clear_free(r->q);
201
1172
	BN_clear_free(r->j);
202
1172
	free(r->seed);
203
1172
	BN_clear_free(r->counter);
204
1172
	BN_clear_free(r->pub_key);
205
1172
	BN_clear_free(r->priv_key);
206
1172
	free(r);
207
21350
}
208
209
int
210
DH_up_ref(DH *r)
211
{
212
	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
213
214
	return i > 1 ? 1 : 0;
215
}
216
217
int
218
DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
219
    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
220
{
221
	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func,
222
	    dup_func, free_func);
223
}
224
225
int
226
DH_set_ex_data(DH *d, int idx, void *arg)
227
{
228
	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
229
}
230
231
void *
232
DH_get_ex_data(DH *d, int idx)
233
{
234
	return CRYPTO_get_ex_data(&d->ex_data, idx);
235
}
236
237
int
238
DH_size(const DH *dh)
239
{
240
552
	return BN_num_bytes(dh->p);
241
}