OpenCoverage

dh_lib.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/dh/dh_lib.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.-
3 *-
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-
8 */-
9-
10#include <stdio.h>-
11#include "internal/cryptlib.h"-
12#include "internal/refcount.h"-
13#include <openssl/bn.h>-
14#include "dh_locl.h"-
15#include <openssl/engine.h>-
16-
17int DH_set_method(DH *dh, const DH_METHOD *meth)-
18{-
19 /*-
20 * NB: The caller is specifically setting a method, so it's not up to us-
21 * to deal with which ENGINE it comes from.-
22 */-
23 const DH_METHOD *mtmp;-
24 mtmp = dh->meth;-
25 if (mtmp->finish)
mtmp->finishDescription
TRUEnever evaluated
FALSEnever evaluated
0
26 mtmp->finish(dh);
never executed: mtmp->finish(dh);
0
27#ifndef OPENSSL_NO_ENGINE-
28 ENGINE_finish(dh->engine);-
29 dh->engine = NULL;-
30#endif-
31 dh->meth = meth;-
32 if (meth->init)
meth->initDescription
TRUEnever evaluated
FALSEnever evaluated
0
33 meth->init(dh);
never executed: meth->init(dh);
0
34 return 1;
never executed: return 1;
0
35}-
36-
37DH *DH_new(void)-
38{-
39 return DH_new_method(NULL);
executed 12258 times by 1 test: return DH_new_method( ((void *)0) );
Executed by:
  • libcrypto.so.1.1
12258
40}-
41-
42DH *DH_new_method(ENGINE *engine)-
43{-
44 DH *ret = OPENSSL_zalloc(sizeof(*ret));-
45-
46 if (ret == NULL) {
ret == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12258
47 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);-
48 return NULL;
never executed: return ((void *)0) ;
0
49 }-
50-
51 ret->references = 1;-
52 ret->lock = CRYPTO_THREAD_lock_new();-
53 if (ret->lock == NULL) {
ret->lock == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12258
54 DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);-
55 OPENSSL_free(ret);-
56 return NULL;
never executed: return ((void *)0) ;
0
57 }-
58-
59 ret->meth = DH_get_default_method();-
60#ifndef OPENSSL_NO_ENGINE-
61 ret->flags = ret->meth->flags; /* early default init */-
62 if (engine) {
engineDescription
TRUEnever evaluated
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12258
63 if (!ENGINE_init(engine)) {
!ENGINE_init(engine)Description
TRUEnever evaluated
FALSEnever evaluated
0
64 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);-
65 goto err;
never executed: goto err;
0
66 }-
67 ret->engine = engine;-
68 } else
never executed: end of block
0
69 ret->engine = ENGINE_get_default_DH();
executed 12258 times by 1 test: ret->engine = ENGINE_get_default_DH();
Executed by:
  • libcrypto.so.1.1
12258
70 if (ret->engine) {
ret->engineDescription
TRUEnever evaluated
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12258
71 ret->meth = ENGINE_get_DH(ret->engine);-
72 if (ret->meth == NULL) {
ret->meth == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
73 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);-
74 goto err;
never executed: goto err;
0
75 }-
76 }
never executed: end of block
0
77#endif-
78-
79 ret->flags = ret->meth->flags;-
80-
81 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
!CRYPTO_new_ex...&ret->ex_data)Description
TRUEnever evaluated
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12258
82 goto err;
never executed: goto err;
0
83-
84 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
(ret->meth->in... ((void *)0) )Description
TRUEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
!ret->meth->init(ret)Description
TRUEnever evaluated
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12258
85 DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);-
86 goto err;
never executed: goto err;
0
87 }-
88-
89 return ret;
executed 12258 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
12258
90-
91 err:-
92 DH_free(ret);-
93 return NULL;
never executed: return ((void *)0) ;
0
94}-
95-
96void DH_free(DH *r)-
97{-
98 int i;-
99-
100 if (r == NULL)
r == ((void *)0)Description
TRUEevaluated 4649 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12493 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
4649-12493
101 return;
executed 4649 times by 1 test: return;
Executed by:
  • libcrypto.so.1.1
4649
102-
103 CRYPTO_DOWN_REF(&r->references, &i, r->lock);-
104 REF_PRINT_COUNT("DH", r);-
105 if (i > 0)
i > 0Description
TRUEevaluated 235 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
235-12258
106 return;
executed 235 times by 1 test: return;
Executed by:
  • libcrypto.so.1.1
235
107 REF_ASSERT_ISNT(i < 0);-
108-
109 if (r->meth != NULL && r->meth->finish != NULL)
r->meth != ((void *)0)Description
TRUEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
r->meth->finish != ((void *)0)Description
TRUEevaluated 12258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-12258
110 r->meth->finish(r);
executed 12258 times by 1 test: r->meth->finish(r);
Executed by:
  • libcrypto.so.1.1
12258
111#ifndef OPENSSL_NO_ENGINE-
112 ENGINE_finish(r->engine);-
113#endif-
114-
115 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);-
116-
117 CRYPTO_THREAD_lock_free(r->lock);-
118-
119 BN_clear_free(r->p);-
120 BN_clear_free(r->g);-
121 BN_clear_free(r->q);-
122 BN_clear_free(r->j);-
123 OPENSSL_free(r->seed);-
124 BN_clear_free(r->counter);-
125 BN_clear_free(r->pub_key);-
126 BN_clear_free(r->priv_key);-
127 OPENSSL_free(r);-
128}
executed 12258 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
12258
129-
130int DH_up_ref(DH *r)-
131{-
132 int i;-
133-
134 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
CRYPTO_UP_REF(... r->lock) <= 0Description
TRUEnever evaluated
FALSEevaluated 235 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-235
135 return 0;
never executed: return 0;
0
136-
137 REF_PRINT_COUNT("DH", r);-
138 REF_ASSERT_ISNT(i < 2);-
139 return ((i > 1) ? 1 : 0);
executed 235 times by 1 test: return ((i > 1) ? 1 : 0);
Executed by:
  • libcrypto.so.1.1
(i > 1)Description
TRUEevaluated 235 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-235
140}-
141-
142int DH_set_ex_data(DH *d, int idx, void *arg)-
143{-
144 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
never executed: return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
0
145}-
146-
147void *DH_get_ex_data(DH *d, int idx)-
148{-
149 return CRYPTO_get_ex_data(&d->ex_data, idx);
never executed: return CRYPTO_get_ex_data(&d->ex_data, idx);
0
150}-
151-
152int DH_bits(const DH *dh)-
153{-
154 return BN_num_bits(dh->p);
never executed: return BN_num_bits(dh->p);
0
155}-
156-
157int DH_size(const DH *dh)-
158{-
159 return BN_num_bytes(dh->p);
executed 242 times by 1 test: return ((BN_num_bits(dh->p)+7)/8);
Executed by:
  • libcrypto.so.1.1
242
160}-
161-
162int DH_security_bits(const DH *dh)-
163{-
164 int N;-
165 if (dh->q)
dh->qDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 663 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
9-663
166 N = BN_num_bits(dh->q);
executed 9 times by 1 test: N = BN_num_bits(dh->q);
Executed by:
  • libcrypto.so.1.1
9
167 else if (dh->length)
dh->lengthDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 647 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
16-647
168 N = dh->length;
executed 16 times by 1 test: N = dh->length;
Executed by:
  • libcrypto.so.1.1
16
169 else-
170 N = -1;
executed 647 times by 1 test: N = -1;
Executed by:
  • libcrypto.so.1.1
647
171 return BN_security_bits(BN_num_bits(dh->p), N);
executed 672 times by 1 test: return BN_security_bits(BN_num_bits(dh->p), N);
Executed by:
  • libcrypto.so.1.1
672
172}-
173-
174-
175void DH_get0_pqg(const DH *dh,-
176 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)-
177{-
178 if (p != NULL)
p != ((void *)0)Description
TRUEevaluated 63 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-63
179 *p = dh->p;
executed 63 times by 1 test: *p = dh->p;
Executed by:
  • libcrypto.so.1.1
63
180 if (q != NULL)
q != ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 62 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1-62
181 *q = dh->q;
executed 1 time by 1 test: *q = dh->q;
Executed by:
  • libcrypto.so.1.1
1
182 if (g != NULL)
g != ((void *)0)Description
TRUEevaluated 63 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-63
183 *g = dh->g;
executed 63 times by 1 test: *g = dh->g;
Executed by:
  • libcrypto.so.1.1
63
184}
executed 63 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
63
185-
186int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)-
187{-
188 /* If the fields p and g in d are NULL, the corresponding input-
189 * parameters MUST be non-NULL. q may remain NULL.-
190 */-
191 if ((dh->p == NULL && p == NULL)
dh->p == ((void *)0)Description
TRUEevaluated 504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
p == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-504
192 || (dh->g == NULL && g == NULL))
dh->g == ((void *)0)Description
TRUEevaluated 504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
g == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-504
193 return 0;
never executed: return 0;
0
194-
195 if (p != NULL) {
p != ((void *)0)Description
TRUEevaluated 504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-504
196 BN_free(dh->p);-
197 dh->p = p;-
198 }
executed 504 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
504
199 if (q != NULL) {
q != ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 503 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1-503
200 BN_free(dh->q);-
201 dh->q = q;-
202 }
executed 1 time by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1
203 if (g != NULL) {
g != ((void *)0)Description
TRUEevaluated 504 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-504
204 BN_free(dh->g);-
205 dh->g = g;-
206 }
executed 504 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
504
207-
208 if (q != NULL) {
q != ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 503 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1-503
209 dh->length = BN_num_bits(q);-
210 }
executed 1 time by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1
211-
212 return 1;
executed 504 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
504
213}-
214-
215long DH_get_length(const DH *dh)-
216{-
217 return dh->length;
never executed: return dh->length;
0
218}-
219-
220int DH_set_length(DH *dh, long length)-
221{-
222 dh->length = length;-
223 return 1;
executed 3 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
3
224}-
225-
226void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)-
227{-
228 if (pub_key != NULL)
pub_key != ((void *)0)Description
TRUEevaluated 210 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-210
229 *pub_key = dh->pub_key;
executed 210 times by 1 test: *pub_key = dh->pub_key;
Executed by:
  • libcrypto.so.1.1
210
230 if (priv_key != NULL)
priv_key != ((void *)0)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 207 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
3-207
231 *priv_key = dh->priv_key;
executed 3 times by 1 test: *priv_key = dh->priv_key;
Executed by:
  • libcrypto.so.1.1
3
232}
executed 210 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
210
233-
234int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)-
235{-
236 if (pub_key != NULL) {
pub_key != ((void *)0)Description
TRUEevaluated 441 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
2-441
237 BN_free(dh->pub_key);-
238 dh->pub_key = pub_key;-
239 }
executed 441 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
441
240 if (priv_key != NULL) {
priv_key != ((void *)0)Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 435 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
8-435
241 BN_free(dh->priv_key);-
242 dh->priv_key = priv_key;-
243 }
executed 8 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
8
244-
245 return 1;
executed 443 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
443
246}-
247-
248const BIGNUM *DH_get0_p(const DH *dh)-
249{-
250 return dh->p;
executed 1 time by 1 test: return dh->p;
Executed by:
  • libcrypto.so.1.1
1
251}-
252-
253const BIGNUM *DH_get0_q(const DH *dh)-
254{-
255 return dh->q;
executed 1 time by 1 test: return dh->q;
Executed by:
  • libcrypto.so.1.1
1
256}-
257-
258const BIGNUM *DH_get0_g(const DH *dh)-
259{-
260 return dh->g;
executed 1 time by 1 test: return dh->g;
Executed by:
  • libcrypto.so.1.1
1
261}-
262-
263const BIGNUM *DH_get0_priv_key(const DH *dh)-
264{-
265 return dh->priv_key;
executed 2 times by 1 test: return dh->priv_key;
Executed by:
  • libcrypto.so.1.1
2
266}-
267-
268const BIGNUM *DH_get0_pub_key(const DH *dh)-
269{-
270 return dh->pub_key;
executed 2 times by 1 test: return dh->pub_key;
Executed by:
  • libcrypto.so.1.1
2
271}-
272-
273void DH_clear_flags(DH *dh, int flags)-
274{-
275 dh->flags &= ~flags;-
276}
never executed: end of block
0
277-
278int DH_test_flags(const DH *dh, int flags)-
279{-
280 return dh->flags & flags;
never executed: return dh->flags & flags;
0
281}-
282-
283void DH_set_flags(DH *dh, int flags)-
284{-
285 dh->flags |= flags;-
286}
never executed: end of block
0
287-
288ENGINE *DH_get0_engine(DH *dh)-
289{-
290 return dh->engine;
never executed: return dh->engine;
0
291}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2