2016-05-17 18:51:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:51:26 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2015-01-27 17:34:45 +00:00
|
|
|
#include <openssl/bn.h>
|
2016-03-30 14:21:39 +00:00
|
|
|
#include "dsa_locl.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2003-01-15 02:01:55 +00:00
|
|
|
static int dsa_builtin_keygen(DSA *dsa);
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int DSA_generate_key(DSA *dsa)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (dsa->meth->dsa_keygen)
|
|
|
|
return dsa->meth->dsa_keygen(dsa);
|
|
|
|
return dsa_builtin_keygen(dsa);
|
|
|
|
}
|
2003-01-15 02:01:55 +00:00
|
|
|
|
|
|
|
static int dsa_builtin_keygen(DSA *dsa)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BIGNUM *pub_key = NULL, *priv_key = NULL;
|
|
|
|
|
|
|
|
if ((ctx = BN_CTX_new()) == NULL)
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dsa->priv_key == NULL) {
|
2015-04-24 20:39:40 +00:00
|
|
|
if ((priv_key = BN_secure_new()) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
} else
|
|
|
|
priv_key = dsa->priv_key;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
do
|
2017-08-02 18:00:52 +00:00
|
|
|
if (!BN_priv_rand_range(priv_key, dsa->q))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
while (BN_is_zero(priv_key)) ;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dsa->pub_key == NULL) {
|
|
|
|
if ((pub_key = BN_new()) == NULL)
|
|
|
|
goto err;
|
|
|
|
} else
|
|
|
|
pub_key = dsa->pub_key;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-05-26 09:55:11 +00:00
|
|
|
BIGNUM *prk = BN_new();
|
2005-05-16 01:43:31 +00:00
|
|
|
|
2016-05-26 09:55:11 +00:00
|
|
|
if (prk == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!BN_mod_exp(pub_key, dsa->g, prk, dsa->p, ctx)) {
|
2016-05-26 09:55:11 +00:00
|
|
|
BN_free(prk);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-26 09:55:11 +00:00
|
|
|
/* We MUST free prk before any further use of priv_key */
|
|
|
|
BN_free(prk);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
dsa->priv_key = priv_key;
|
|
|
|
dsa->pub_key = pub_key;
|
|
|
|
ok = 1;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2015-05-01 01:37:06 +00:00
|
|
|
if (pub_key != dsa->pub_key)
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_free(pub_key);
|
2015-05-01 01:37:06 +00:00
|
|
|
if (priv_key != dsa->priv_key)
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_free(priv_key);
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_CTX_free(ctx);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ok;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|