OpenCoverage

dh_key.c

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

Generated by Squish Coco 4.2.2