OpenCoverage

dh_key.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/dh/dh_key.c
Switch to Source codePreprocessed file
LineSourceCount
1-
2static int generate_key(DH *dh);-
3static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);-
4static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,-
5 const BIGNUM *a, const BIGNUM *p,-
6 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);-
7static int dh_init(DH *dh);-
8static int dh_finish(DH *dh);-
9-
10int DH_generate_key(DH *dh)-
11{-
12 return
executed 232 times by 1 test: return dh->meth->generate_key(dh);
Executed by:
  • libcrypto.so.1.1
dh->meth->generate_key(dh);
executed 232 times by 1 test: return dh->meth->generate_key(dh);
Executed by:
  • libcrypto.so.1.1
232
13}-
14-
15int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)-
16{-
17 return
executed 233 times by 1 test: return dh->meth->compute_key(key, pub_key, dh);
Executed by:
  • libcrypto.so.1.1
dh->meth->compute_key(key, pub_key, dh);
executed 233 times by 1 test: return dh->meth->compute_key(key, pub_key, dh);
Executed by:
  • libcrypto.so.1.1
233
18}-
19-
20int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)-
21{-
22 int rv, pad;-
23 rv = dh->meth->compute_key(key, pub_key, dh);-
24 if (rv <= 0
rv <= 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-3
25 return
never executed: return rv;
rv;
never executed: return rv;
0
26 pad = ((BN_num_bits(dh->p)+7)/8) - rv;-
27 if (pad > 0
pad > 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
1-2
28 memmove(key + pad, key, rv);-
29 memset(key, 0, pad);-
30 }
executed 1 time by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1
31 return
executed 3 times by 1 test: return rv + pad;
Executed by:
  • libcrypto.so.1.1
rv + pad;
executed 3 times by 1 test: return rv + pad;
Executed by:
  • libcrypto.so.1.1
3
32}-
33-
34static DH_METHOD dh_ossl = {-
35 "OpenSSL DH Method",-
36 generate_key,-
37 compute_key,-
38 dh_bn_mod_exp,-
39 dh_init,-
40 dh_finish,-
41 0x0400,-
42 -
43 ((void *)0)-
44 ,-
45 -
46 ((void *)0)-
47-
48};-
49-
50static const DH_METHOD *default_DH_method = &dh_ossl;-
51-
52const DH_METHOD *DH_OpenSSL(void)-
53{-
54 return
never executed: return &dh_ossl;
&dh_ossl;
never executed: return &dh_ossl;
0
55}-
56-
57void DH_set_default_method(const DH_METHOD *meth)-
58{-
59 default_DH_method = meth;-
60}
never executed: end of block
0
61-
62const DH_METHOD *DH_get_default_method(void)-
63{-
64 return
executed 12258 times by 1 test: return default_DH_method;
Executed by:
  • libcrypto.so.1.1
default_DH_method;
executed 12258 times by 1 test: return default_DH_method;
Executed by:
  • libcrypto.so.1.1
12258
65}-
66-
67static int generate_key(DH *dh)-
68{-
69 int ok = 0;-
70 int generate_new_key = 0;-
71 unsigned l;-
72 BN_CTX *ctx = -
73 ((void *)0)-
74 ;-
75 BN_MONT_CTX *mont = -
76 ((void *)0)-
77 ;-
78 BIGNUM *pub_key = -
79 ((void *)0)-
80 , *priv_key = -
81 ((void *)0)-
82 ;-
83-
84 if (BN_num_bits(dh->p) > 10000
BN_num_bits(dh->p) > 10000Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-232
85 ERR_put_error(5,(103),(103),__FILE__,86);-
86 return
never executed: return 0;
0;
never executed: return 0;
0
87 }-
88-
89 ctx = BN_CTX_new();-
90 if (ctx ==
ctx == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-232
91 ((void *)0)
ctx == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-232
92 )-
93 goto
never executed: goto err;
err;
never executed: goto err;
0
94-
95 if (dh->priv_key ==
dh->priv_key == ((void *)0)Description
TRUEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12-220
96 ((void *)0)
dh->priv_key == ((void *)0)Description
TRUEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12-220
97 ) {-
98 priv_key = BN_secure_new();-
99 if (priv_key ==
priv_key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-220
100 ((void *)0)
priv_key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-220
101 )-
102 goto
never executed: goto err;
err;
never executed: goto err;
0
103 generate_new_key = 1;-
104 }
executed 220 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
else
220
105 priv_key = dh->priv_key;
executed 12 times by 1 test: priv_key = dh->priv_key;
Executed by:
  • libcrypto.so.1.1
12
106-
107 if (dh->pub_key ==
dh->pub_key == ((void *)0)Description
TRUEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-232
108 ((void *)0)
dh->pub_key == ((void *)0)Description
TRUEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-232
109 ) {-
110 pub_key = BN_new();-
111 if (pub_key ==
pub_key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-232
112 ((void *)0)
pub_key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-232
113 )-
114 goto
never executed: goto err;
err;
never executed: goto err;
0
115 }
executed 232 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
else
232
116 pub_key = dh->pub_key;
never executed: pub_key = dh->pub_key;
0
117-
118 if (dh->flags & 0x01
dh->flags & 0x01Description
TRUEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
) {
0-232
119 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,-
120 dh->lock, dh->p, ctx);-
121 if (!mont
!montDescription
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-232
122 goto
never executed: goto err;
err;
never executed: goto err;
0
123 }
executed 232 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
232
124-
125 if (generate_new_key
generate_new_keyDescription
TRUEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
12-220
126 if (dh->q
dh->qDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 218 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
2-218
127 do {-
128 if (!BN_priv_rand_range(priv_key, dh->q)
!BN_priv_rand_...iv_key, dh->q)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-2
129 goto
never executed: goto err;
err;
never executed: goto err;
0
130 }
executed 2 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
2
131 while (BN_is_zero(priv_key)
BN_is_zero(priv_key)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
|| BN_is_one(priv_key)
BN_is_one(priv_key)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
);
0-2
132 }
executed 2 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
else {
2
133-
134 l = dh->length
dh->lengthDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 207 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
? dh->length : BN_num_bits(dh->p) - 1;
11-207
135 if (!BN_priv_rand(priv_key, l, 0, 0)
!BN_priv_rand(..._key, l, 0, 0)Description
TRUEnever evaluated
FALSEevaluated 218 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-218
136 goto
never executed: goto err;
err;
never executed: goto err;
0
137 }
executed 218 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
218
138 }-
139-
140 {-
141 BIGNUM *prk = BN_new();-
142-
143 if (prk ==
prk == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-232
144 ((void *)0)
prk == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-232
145 )-
146 goto
never executed: goto err;
err;
never executed: goto err;
0
147 BN_with_flags(prk, priv_key, 0x04);-
148-
149 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)
!dh->meth->bn_...>p, ctx, mont)Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-232
150 BN_free(prk);-
151 goto
never executed: goto err;
err;
never executed: goto err;
0
152 }-
153-
154 BN_free(prk);-
155 }-
156-
157 dh->pub_key = pub_key;-
158 dh->priv_key = priv_key;-
159 ok = 1;-
160 err:
code before this statement executed 232 times by 1 test: err:
Executed by:
  • libcrypto.so.1.1
232
161 if (ok != 1
ok != 1Description
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-232
162 ERR_put_error(5,(103),(3),__FILE__,151);
never executed: ERR_put_error(5,(103),(3),__FILE__,151);
0
163-
164 if (pub_key != dh->pub_key
pub_key != dh->pub_keyDescription
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-232
165 BN_free(pub_key);
never executed: BN_free(pub_key);
0
166 if (priv_key != dh->priv_key
priv_key != dh->priv_keyDescription
TRUEnever evaluated
FALSEevaluated 232 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-232
167 BN_free(priv_key);
never executed: BN_free(priv_key);
0
168 BN_CTX_free(ctx);-
169 return
executed 232 times by 1 test: return ok;
Executed by:
  • libcrypto.so.1.1
ok;
executed 232 times by 1 test: return ok;
Executed by:
  • libcrypto.so.1.1
232
170}-
171-
172static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)-
173{-
174 BN_CTX *ctx = -
175 ((void *)0)-
176 ;-
177 BN_MONT_CTX *mont = -
178 ((void *)0)-
179 ;-
180 BIGNUM *tmp;-
181 int ret = -1;-
182 int check_result;-
183-
184 if (BN_num_bits(dh->p) > 10000
BN_num_bits(dh->p) > 10000Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-236
185 ERR_put_error(5,(102),(103),__FILE__,170);-
186 goto
never executed: goto err;
err;
never executed: goto err;
0
187 }-
188-
189 ctx = BN_CTX_new();-
190 if (ctx ==
ctx == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-236
191 ((void *)0)
ctx == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-236
192 )-
193 goto
never executed: goto err;
err;
never executed: goto err;
0
194 BN_CTX_start(ctx);-
195 tmp = BN_CTX_get(ctx);-
196 if (tmp ==
tmp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-236
197 ((void *)0)
tmp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-236
198 )-
199 goto
never executed: goto err;
err;
never executed: goto err;
0
200-
201 if (dh->priv_key ==
dh->priv_key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-236
202 ((void *)0)
dh->priv_key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-236
203 ) {-
204 ERR_put_error(5,(102),(100),__FILE__,183);-
205 goto
never executed: goto err;
err;
never executed: goto err;
0
206 }-
207-
208 if (dh->flags & 0x01
dh->flags & 0x01Description
TRUEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
) {
0-236
209 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,-
210 dh->lock, dh->p, ctx);-
211 BN_set_flags(dh->priv_key, 0x04);-
212 if (!mont
!montDescription
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
)
0-236
213 goto
never executed: goto err;
err;
never executed: goto err;
0
214 }
executed 236 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
236
215-
216 if (!DH_check_pub_key(dh, pub_key, &check_result)
!DH_check_pub_...&check_result)Description
TRUEnever evaluated
FALSEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
|| check_result
check_resultDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-236
217 ERR_put_error(5,(102),(102),__FILE__,196);-
218 goto
executed 16 times by 1 test: goto err;
Executed by:
  • libcrypto.so.1.1
err;
executed 16 times by 1 test: goto err;
Executed by:
  • libcrypto.so.1.1
16
219 }-
220-
221 if (!dh->
!dh-> meth->bn...>p, ctx, mont)Description
TRUEnever evaluated
FALSEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-220
222 meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)
!dh-> meth->bn...>p, ctx, mont)Description
TRUEnever evaluated
FALSEevaluated 220 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
) {
0-220
223 ERR_put_error(5,(102),(3),__FILE__,202);-
224 goto
never executed: goto err;
err;
never executed: goto err;
0
225 }-
226-
227 ret = BN_bn2bin(tmp, key);-
228 err:
code before this statement executed 220 times by 1 test: err:
Executed by:
  • libcrypto.so.1.1
220
229 if (ctx !=
ctx != ((void *)0)Description
TRUEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-236
230 ((void *)0)
ctx != ((void *)0)Description
TRUEevaluated 236 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-236
231 ) {-
232 BN_CTX_end(ctx);-
233 BN_CTX_free(ctx);-
234 }
executed 236 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
236
235 return
executed 236 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
ret;
executed 236 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
236
236}-
237-
238static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,-
239 const BIGNUM *a, const BIGNUM *p,-
240 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)-
241{-
242 return
executed 452 times by 1 test: return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
Executed by:
  • libcrypto.so.1.1
BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
executed 452 times by 1 test: return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
Executed by:
  • libcrypto.so.1.1
452
243}-
244-
245static int dh_init(DH *dh)-
246{-
247 dh->flags |= 0x01;-
248 return
executed 12258 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
1;
executed 12258 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
12258
249}-
250-
251static int dh_finish(DH *dh)-
252{-
253 BN_MONT_CTX_free(dh->method_mont_p);-
254 return
executed 12258 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
1;
executed 12258 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
12258
255}-
Switch to Source codePreprocessed file

Generated by Squish Coco 4.2.2