/src/openssl111/crypto/dsa/dsa_key.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2016 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 <time.h> | 
| 12 |  | #include "internal/cryptlib.h" | 
| 13 |  | #include <openssl/bn.h> | 
| 14 |  | #include "dsa_local.h" | 
| 15 |  |  | 
| 16 |  | static int dsa_builtin_keygen(DSA *dsa); | 
| 17 |  |  | 
| 18 |  | int DSA_generate_key(DSA *dsa) | 
| 19 | 0 | { | 
| 20 | 0 |     if (dsa->meth->dsa_keygen) | 
| 21 | 0 |         return dsa->meth->dsa_keygen(dsa); | 
| 22 | 0 |     return dsa_builtin_keygen(dsa); | 
| 23 | 0 | } | 
| 24 |  |  | 
| 25 |  | static int dsa_builtin_keygen(DSA *dsa) | 
| 26 | 0 | { | 
| 27 | 0 |     int ok = 0; | 
| 28 | 0 |     BN_CTX *ctx = NULL; | 
| 29 | 0 |     BIGNUM *pub_key = NULL, *priv_key = NULL; | 
| 30 |  | 
 | 
| 31 | 0 |     if ((ctx = BN_CTX_new()) == NULL) | 
| 32 | 0 |         goto err; | 
| 33 |  |  | 
| 34 | 0 |     if (dsa->priv_key == NULL) { | 
| 35 | 0 |         if ((priv_key = BN_secure_new()) == NULL) | 
| 36 | 0 |             goto err; | 
| 37 | 0 |     } else | 
| 38 | 0 |         priv_key = dsa->priv_key; | 
| 39 |  |  | 
| 40 | 0 |     do | 
| 41 | 0 |         if (!BN_priv_rand_range(priv_key, dsa->q)) | 
| 42 | 0 |             goto err; | 
| 43 | 0 |     while (BN_is_zero(priv_key)) ; | 
| 44 |  |  | 
| 45 | 0 |     if (dsa->pub_key == NULL) { | 
| 46 | 0 |         if ((pub_key = BN_new()) == NULL) | 
| 47 | 0 |             goto err; | 
| 48 | 0 |     } else | 
| 49 | 0 |         pub_key = dsa->pub_key; | 
| 50 |  |  | 
| 51 | 0 |     { | 
| 52 | 0 |         BIGNUM *prk = BN_new(); | 
| 53 |  | 
 | 
| 54 | 0 |         if (prk == NULL) | 
| 55 | 0 |             goto err; | 
| 56 | 0 |         BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | 
| 57 |  | 
 | 
| 58 | 0 |         if (!BN_mod_exp(pub_key, dsa->g, prk, dsa->p, ctx)) { | 
| 59 | 0 |             BN_free(prk); | 
| 60 | 0 |             goto err; | 
| 61 | 0 |         } | 
| 62 |  |         /* We MUST free prk before any further use of priv_key */ | 
| 63 | 0 |         BN_free(prk); | 
| 64 | 0 |     } | 
| 65 |  |  | 
| 66 | 0 |     dsa->priv_key = priv_key; | 
| 67 | 0 |     dsa->pub_key = pub_key; | 
| 68 | 0 |     ok = 1; | 
| 69 |  | 
 | 
| 70 | 0 |  err: | 
| 71 | 0 |     if (pub_key != dsa->pub_key) | 
| 72 | 0 |         BN_free(pub_key); | 
| 73 | 0 |     if (priv_key != dsa->priv_key) | 
| 74 | 0 |         BN_free(priv_key); | 
| 75 | 0 |     BN_CTX_free(ctx); | 
| 76 | 0 |     return ok; | 
| 77 | 0 | } |