| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/dh/dh_key.c | 
| Switch to Source code | Preprocessed file | 
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||
| 2 | static int generate_key(DH *dh); | - | ||||||||||||
| 3 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); | - | ||||||||||||
| 4 | static 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); | - | ||||||||||||
| 7 | static int dh_init(DH *dh); | - | ||||||||||||
| 8 | static int dh_finish(DH *dh); | - | ||||||||||||
| 9 | - | |||||||||||||
| 10 | int DH_generate_key(DH *dh) | - | ||||||||||||
| 11 | { | - | ||||||||||||
| 12 |     return executed 232 times by 1 test:   dh->meth->generate_key(dh);return dh->meth->generate_key(dh);Executed by: 
 executed 232 times by 1 test:  return dh->meth->generate_key(dh);Executed by: 
  | 232 | ||||||||||||
| 13 | } | - | ||||||||||||
| 14 | - | |||||||||||||
| 15 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | - | ||||||||||||
| 16 | { | - | ||||||||||||
| 17 |     return executed 233 times by 1 test:   dh->meth->compute_key(key, pub_key, dh);return dh->meth->compute_key(key, pub_key, dh);Executed by: 
 executed 233 times by 1 test:  return dh->meth->compute_key(key, pub_key, dh);Executed by: 
  | 233 | ||||||||||||
| 18 | } | - | ||||||||||||
| 19 | - | |||||||||||||
| 20 | int 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
  | 0-3 | ||||||||||||
| 25 |         return never executed:   rv;return rv;never executed:  return rv; | 0 | ||||||||||||
| 26 | pad = ((BN_num_bits(dh->p)+7)/8) - rv; | - | ||||||||||||
| 27 |     if (pad > 0
  | 1-2 | ||||||||||||
| 28 | memmove(key + pad, key, rv); | - | ||||||||||||
| 29 | memset(key, 0, pad); | - | ||||||||||||
| 30 |     } executed 1 time by 1 test:  end of blockExecuted by: 
  | 1 | ||||||||||||
| 31 |     return executed 3 times by 1 test:   rv + pad;return rv + pad;Executed by: 
 executed 3 times by 1 test:  return rv + pad;Executed by: 
  | 3 | ||||||||||||
| 32 | } | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | static 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 | - | |||||||||||||
| 50 | static const DH_METHOD *default_DH_method = &dh_ossl; | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | const DH_METHOD *DH_OpenSSL(void) | - | ||||||||||||
| 53 | { | - | ||||||||||||
| 54 |     return never executed:   &dh_ossl;return &dh_ossl;never executed:  return &dh_ossl; | 0 | ||||||||||||
| 55 | } | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | void DH_set_default_method(const DH_METHOD *meth) | - | ||||||||||||
| 58 | { | - | ||||||||||||
| 59 | default_DH_method = meth; | - | ||||||||||||
| 60 | } never executed:  end of block | 0 | ||||||||||||
| 61 | - | |||||||||||||
| 62 | const DH_METHOD *DH_get_default_method(void) | - | ||||||||||||
| 63 | { | - | ||||||||||||
| 64 |     return executed 12258 times by 1 test:   default_DH_method;return default_DH_method;Executed by: 
 executed 12258 times by 1 test:  return default_DH_method;Executed by: 
  | 12258 | ||||||||||||
| 65 | } | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | static 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
  | 0-232 | ||||||||||||
| 85 | ERR_put_error(5,(103),(103),__FILE__,86); | - | ||||||||||||
| 86 |         return never executed:   0;return 0;never executed:  return 0; | 0 | ||||||||||||
| 87 | } | - | ||||||||||||
| 88 | - | |||||||||||||
| 89 | ctx = BN_CTX_new(); | - | ||||||||||||
| 90 |     if (ctx == 
  | 0-232 | ||||||||||||
| 91 |               ((void *)0)
  | 0-232 | ||||||||||||
| 92 | ) | - | ||||||||||||
| 93 |         goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 94 | - | |||||||||||||
| 95 |     if (dh->priv_key == 
  | 12-220 | ||||||||||||
| 96 |                        ((void *)0)
  | 12-220 | ||||||||||||
| 97 | ) { | - | ||||||||||||
| 98 | priv_key = BN_secure_new(); | - | ||||||||||||
| 99 |         if (priv_key == 
  | 0-220 | ||||||||||||
| 100 |                        ((void *)0)
  | 0-220 | ||||||||||||
| 101 | ) | - | ||||||||||||
| 102 |             goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 103 | generate_new_key = 1; | - | ||||||||||||
| 104 |     } executed 220 times by 1 test:   elseend of blockExecuted by: 
  | 220 | ||||||||||||
| 105 |         priv_key = dh->priv_key; executed 12 times by 1 test:  priv_key = dh->priv_key;Executed by: 
  | 12 | ||||||||||||
| 106 | - | |||||||||||||
| 107 |     if (dh->pub_key == 
  | 0-232 | ||||||||||||
| 108 |                       ((void *)0)
  | 0-232 | ||||||||||||
| 109 | ) { | - | ||||||||||||
| 110 | pub_key = BN_new(); | - | ||||||||||||
| 111 |         if (pub_key == 
  | 0-232 | ||||||||||||
| 112 |                       ((void *)0)
  | 0-232 | ||||||||||||
| 113 | ) | - | ||||||||||||
| 114 |             goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 115 |     } executed 232 times by 1 test:   elseend of blockExecuted by: 
  | 232 | ||||||||||||
| 116 |         pub_key = dh->pub_key; never executed:  pub_key = dh->pub_key; | 0 | ||||||||||||
| 117 | - | |||||||||||||
| 118 |     if (dh->flags & 0x01
  | 0-232 | ||||||||||||
| 119 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | - | ||||||||||||
| 120 | dh->lock, dh->p, ctx); | - | ||||||||||||
| 121 |         if (!mont
  | 0-232 | ||||||||||||
| 122 |             goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 123 |     } executed 232 times by 1 test:  end of blockExecuted by: 
  | 232 | ||||||||||||
| 124 | - | |||||||||||||
| 125 |     if (generate_new_key
  | 12-220 | ||||||||||||
| 126 |         if (dh->q
  | 2-218 | ||||||||||||
| 127 | do { | - | ||||||||||||
| 128 |                 if (!BN_priv_rand_range(priv_key, dh->q)
  | 0-2 | ||||||||||||
| 129 |                     goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 130 |             } executed 2 times by 1 test:  end of blockExecuted by: 
  | 2 | ||||||||||||
| 131 |             while (BN_is_zero(priv_key)
 
  | 0-2 | ||||||||||||
| 132 |         } executed 2 times by 1 test:   else {end of blockExecuted by: 
  | 2 | ||||||||||||
| 133 | - | |||||||||||||
| 134 |             l = dh->length
  | 11-207 | ||||||||||||
| 135 |             if (!BN_priv_rand(priv_key, l, 0, 0)
  | 0-218 | ||||||||||||
| 136 |                 goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 137 |         } executed 218 times by 1 test:  end of blockExecuted by: 
  | 218 | ||||||||||||
| 138 | } | - | ||||||||||||
| 139 | - | |||||||||||||
| 140 | { | - | ||||||||||||
| 141 | BIGNUM *prk = BN_new(); | - | ||||||||||||
| 142 | - | |||||||||||||
| 143 |         if (prk == 
  | 0-232 | ||||||||||||
| 144 |                   ((void *)0)
  | 0-232 | ||||||||||||
| 145 | ) | - | ||||||||||||
| 146 |             goto never executed:   err;goto 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)
  | 0-232 | ||||||||||||
| 150 | BN_free(prk); | - | ||||||||||||
| 151 |             goto never executed:   err;goto 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: 
  | 232 | ||||||||||||
| 161 |     if (ok != 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
  | 0-232 | ||||||||||||
| 165 |         BN_free(pub_key); never executed:  BN_free(pub_key); | 0 | ||||||||||||
| 166 |     if (priv_key != dh->priv_key
  | 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:   ok;return ok;Executed by: 
 executed 232 times by 1 test:  return ok;Executed by: 
  | 232 | ||||||||||||
| 170 | } | - | ||||||||||||
| 171 | - | |||||||||||||
| 172 | static 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
  | 0-236 | ||||||||||||
| 185 | ERR_put_error(5,(102),(103),__FILE__,170); | - | ||||||||||||
| 186 |         goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 187 | } | - | ||||||||||||
| 188 | - | |||||||||||||
| 189 | ctx = BN_CTX_new(); | - | ||||||||||||
| 190 |     if (ctx == 
  | 0-236 | ||||||||||||
| 191 |               ((void *)0)
  | 0-236 | ||||||||||||
| 192 | ) | - | ||||||||||||
| 193 |         goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 194 | BN_CTX_start(ctx); | - | ||||||||||||
| 195 | tmp = BN_CTX_get(ctx); | - | ||||||||||||
| 196 |     if (tmp == 
  | 0-236 | ||||||||||||
| 197 |               ((void *)0)
  | 0-236 | ||||||||||||
| 198 | ) | - | ||||||||||||
| 199 |         goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 200 | - | |||||||||||||
| 201 |     if (dh->priv_key == 
  | 0-236 | ||||||||||||
| 202 |                        ((void *)0)
  | 0-236 | ||||||||||||
| 203 | ) { | - | ||||||||||||
| 204 | ERR_put_error(5,(102),(100),__FILE__,183); | - | ||||||||||||
| 205 |         goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 206 | } | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 |     if (dh->flags & 0x01
  | 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
  | 0-236 | ||||||||||||
| 213 |             goto never executed:   err;goto err;never executed:  goto err; | 0 | ||||||||||||
| 214 |     } executed 236 times by 1 test:  end of blockExecuted by: 
  | 236 | ||||||||||||
| 215 | - | |||||||||||||
| 216 |     if (!DH_check_pub_key(dh, pub_key, &check_result)
 
  | 0-236 | ||||||||||||
| 217 | ERR_put_error(5,(102),(102),__FILE__,196); | - | ||||||||||||
| 218 |         goto executed 16 times by 1 test:   err;goto err;Executed by: 
 executed 16 times by 1 test:  goto err;Executed by: 
  | 16 | ||||||||||||
| 219 | } | - | ||||||||||||
| 220 | - | |||||||||||||
| 221 |     if (!dh->
  | 0-220 | ||||||||||||
| 222 |         meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)
  | 0-220 | ||||||||||||
| 223 | ERR_put_error(5,(102),(3),__FILE__,202); | - | ||||||||||||
| 224 |         goto never executed:   err;goto 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: 
  | 220 | ||||||||||||
| 229 |     if (ctx != 
  | 0-236 | ||||||||||||
| 230 |               ((void *)0)
  | 0-236 | ||||||||||||
| 231 | ) { | - | ||||||||||||
| 232 | BN_CTX_end(ctx); | - | ||||||||||||
| 233 | BN_CTX_free(ctx); | - | ||||||||||||
| 234 |     } executed 236 times by 1 test:  end of blockExecuted by: 
  | 236 | ||||||||||||
| 235 |     return executed 236 times by 1 test:   ret;return ret;Executed by: 
 executed 236 times by 1 test:  return ret;Executed by: 
  | 236 | ||||||||||||
| 236 | } | - | ||||||||||||
| 237 | - | |||||||||||||
| 238 | static 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:   BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);Executed by: 
 executed 452 times by 1 test:  return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);Executed by: 
  | 452 | ||||||||||||
| 243 | } | - | ||||||||||||
| 244 | - | |||||||||||||
| 245 | static int dh_init(DH *dh) | - | ||||||||||||
| 246 | { | - | ||||||||||||
| 247 | dh->flags |= 0x01; | - | ||||||||||||
| 248 |     return executed 12258 times by 1 test:   1;return 1;Executed by: 
 executed 12258 times by 1 test:  return 1;Executed by: 
  | 12258 | ||||||||||||
| 249 | } | - | ||||||||||||
| 250 | - | |||||||||||||
| 251 | static int dh_finish(DH *dh) | - | ||||||||||||
| 252 | { | - | ||||||||||||
| 253 | BN_MONT_CTX_free(dh->method_mont_p); | - | ||||||||||||
| 254 |     return executed 12258 times by 1 test:   1;return 1;Executed by: 
 executed 12258 times by 1 test:  return 1;Executed by: 
  | 12258 | ||||||||||||
| 255 | } | - | ||||||||||||
| Switch to Source code | Preprocessed file |