2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
|
|
|
*
|
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.]
|
|
|
|
*
|
|
|
|
* The DSS routines are based on patches supplied by
|
|
|
|
* Steven Schoch <schoch@sheba.arc.nasa.gov>. */
|
|
|
|
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-30 20:53:00 +00:00
|
|
|
#include <openssl/bn.h>
|
2014-09-06 01:04:51 +01:00
|
|
|
#include <openssl/dh.h>
|
2015-10-30 20:53:00 +00:00
|
|
|
#include <openssl/digest.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/engine.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ex_data.h>
|
|
|
|
#include <openssl/mem.h>
|
2015-10-30 20:53:00 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
2015-03-28 07:12:01 +00:00
|
|
|
#include <openssl/thread.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-04-28 22:47:06 +01:00
|
|
|
#include "../fipsmodule/bn/internal.h"
|
2015-04-13 19:04:14 +01:00
|
|
|
#include "../internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-31 01:08:37 +00:00
|
|
|
|
2015-10-30 20:53:00 +00:00
|
|
|
#define OPENSSL_DSA_MAX_MODULUS_BITS 10000
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
|
|
|
|
// Rabin-Miller
|
2015-10-30 20:53:00 +00:00
|
|
|
#define DSS_prime_checks 50
|
2015-04-15 22:29:53 +01:00
|
|
|
|
2017-11-21 17:58:36 +00:00
|
|
|
static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
|
|
|
|
BIGNUM **out_r);
|
|
|
|
|
2015-10-30 20:53:00 +00:00
|
|
|
static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-30 20:53:00 +00:00
|
|
|
DSA *DSA_new(void) {
|
2016-02-07 19:36:04 +00:00
|
|
|
DSA *dsa = OPENSSL_malloc(sizeof(DSA));
|
2014-06-20 20:00:00 +01:00
|
|
|
if (dsa == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
|
2014-06-20 20:00:00 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(dsa, 0, sizeof(DSA));
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
dsa->references = 1;
|
|
|
|
|
2016-06-16 20:13:43 +01:00
|
|
|
CRYPTO_MUTEX_init(&dsa->method_mont_lock);
|
2015-12-05 04:14:35 +00:00
|
|
|
CRYPTO_new_ex_data(&dsa->ex_data);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return dsa;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSA_free(DSA *dsa) {
|
|
|
|
if (dsa == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:49:30 +01:00
|
|
|
if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-15 22:29:53 +01:00
|
|
|
CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-04-22 18:50:28 +01:00
|
|
|
BN_clear_free(dsa->p);
|
|
|
|
BN_clear_free(dsa->q);
|
|
|
|
BN_clear_free(dsa->g);
|
|
|
|
BN_clear_free(dsa->pub_key);
|
|
|
|
BN_clear_free(dsa->priv_key);
|
2015-10-30 20:53:00 +00:00
|
|
|
BN_MONT_CTX_free(dsa->method_mont_p);
|
2016-06-16 20:13:43 +01:00
|
|
|
BN_MONT_CTX_free(dsa->method_mont_q);
|
|
|
|
CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
OPENSSL_free(dsa);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_up_ref(DSA *dsa) {
|
2015-05-15 20:49:30 +01:00
|
|
|
CRYPTO_refcount_inc(&dsa->references);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:04:24 +01:00
|
|
|
void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
|
|
|
|
const BIGNUM **out_priv_key) {
|
|
|
|
if (out_pub_key != NULL) {
|
|
|
|
*out_pub_key = dsa->pub_key;
|
|
|
|
}
|
|
|
|
if (out_priv_key != NULL) {
|
|
|
|
*out_priv_key = dsa->priv_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
|
|
|
|
const BIGNUM **out_g) {
|
|
|
|
if (out_p != NULL) {
|
|
|
|
*out_p = dsa->p;
|
|
|
|
}
|
|
|
|
if (out_q != NULL) {
|
|
|
|
*out_q = dsa->q;
|
|
|
|
}
|
|
|
|
if (out_g != NULL) {
|
|
|
|
*out_g = dsa->g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:48:19 +01:00
|
|
|
int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) {
|
|
|
|
if (dsa->pub_key == NULL && pub_key == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pub_key != NULL) {
|
|
|
|
BN_free(dsa->pub_key);
|
|
|
|
dsa->pub_key = pub_key;
|
|
|
|
}
|
|
|
|
if (priv_key != NULL) {
|
|
|
|
BN_free(dsa->priv_key);
|
|
|
|
dsa->priv_key = priv_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
|
|
|
|
if ((dsa->p == NULL && p == NULL) ||
|
|
|
|
(dsa->q == NULL && q == NULL) ||
|
|
|
|
(dsa->g == NULL && g == NULL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
BN_free(dsa->p);
|
|
|
|
dsa->p = p;
|
|
|
|
}
|
|
|
|
if (q != NULL) {
|
|
|
|
BN_free(dsa->q);
|
|
|
|
dsa->q = q;
|
|
|
|
}
|
|
|
|
if (g != NULL) {
|
|
|
|
BN_free(dsa->g);
|
|
|
|
dsa->g = g;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
|
|
|
|
size_t seed_len, int *out_counter,
|
|
|
|
unsigned long *out_h, BN_GENCB *cb) {
|
2015-10-30 20:53:00 +00:00
|
|
|
int ok = 0;
|
|
|
|
unsigned char seed[SHA256_DIGEST_LENGTH];
|
|
|
|
unsigned char md[SHA256_DIGEST_LENGTH];
|
|
|
|
unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
|
|
|
|
BIGNUM *r0, *W, *X, *c, *test;
|
|
|
|
BIGNUM *g = NULL, *q = NULL, *p = NULL;
|
|
|
|
BN_MONT_CTX *mont = NULL;
|
|
|
|
int k, n = 0, m = 0;
|
|
|
|
unsigned i;
|
|
|
|
int counter = 0;
|
|
|
|
int r = 0;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
unsigned int h = 2;
|
|
|
|
unsigned qsize;
|
|
|
|
const EVP_MD *evpmd;
|
|
|
|
|
|
|
|
evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
|
|
|
|
qsize = EVP_MD_size(evpmd);
|
|
|
|
|
|
|
|
if (bits < 512) {
|
|
|
|
bits = 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits = (bits + 63) / 64 * 64;
|
|
|
|
|
|
|
|
if (seed_in != NULL) {
|
|
|
|
if (seed_len < (size_t)qsize) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (seed_len > (size_t)qsize) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Only consume as much seed as is expected.
|
2015-10-30 20:53:00 +00:00
|
|
|
seed_len = qsize;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(seed, seed_in, seed_len);
|
2015-10-30 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
|
|
|
|
r0 = BN_CTX_get(ctx);
|
|
|
|
g = BN_CTX_get(ctx);
|
|
|
|
W = BN_CTX_get(ctx);
|
|
|
|
q = BN_CTX_get(ctx);
|
|
|
|
X = BN_CTX_get(ctx);
|
|
|
|
c = BN_CTX_get(ctx);
|
|
|
|
p = BN_CTX_get(ctx);
|
|
|
|
test = BN_CTX_get(ctx);
|
|
|
|
|
|
|
|
if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Find q.
|
2015-10-30 20:53:00 +00:00
|
|
|
for (;;) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 1
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_GENCB_call(cb, 0, m++)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int use_random_seed = (seed_in == NULL);
|
|
|
|
if (use_random_seed) {
|
|
|
|
if (!RAND_bytes(seed, qsize)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-18 19:06:02 +01:00
|
|
|
// If we come back through, use random seed next time.
|
2015-10-30 20:53:00 +00:00
|
|
|
seed_in = NULL;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(buf, seed, qsize);
|
|
|
|
OPENSSL_memcpy(buf2, seed, qsize);
|
2017-08-18 19:06:02 +01:00
|
|
|
// precompute "SEED + 1" for step 7:
|
2015-10-30 20:53:00 +00:00
|
|
|
for (i = qsize - 1; i < qsize; i--) {
|
|
|
|
buf[i]++;
|
|
|
|
if (buf[i] != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 2
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
|
|
|
|
!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
for (i = 0; i < qsize; i++) {
|
|
|
|
md[i] ^= buf2[i];
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 3
|
2015-10-30 20:53:00 +00:00
|
|
|
md[0] |= 0x80;
|
|
|
|
md[qsize - 1] |= 0x01;
|
|
|
|
if (!BN_bin2bn(md, qsize, q)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 4
|
2015-10-30 20:53:00 +00:00
|
|
|
r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
|
|
|
|
if (r > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r != 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// do a callback call
|
|
|
|
// step 5
|
2015-10-30 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 6
|
2015-10-30 20:53:00 +00:00
|
|
|
counter = 0;
|
2017-08-18 19:06:02 +01:00
|
|
|
// "offset = 2"
|
2015-10-30 20:53:00 +00:00
|
|
|
|
|
|
|
n = (bits - 1) / 160;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 7
|
2015-10-30 20:53:00 +00:00
|
|
|
BN_zero(W);
|
2017-08-18 19:06:02 +01:00
|
|
|
// now 'buf' contains "SEED + offset - 1"
|
2015-10-30 20:53:00 +00:00
|
|
|
for (k = 0; k <= n; k++) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// obtain "SEED + offset + k" by incrementing:
|
2015-10-30 20:53:00 +00:00
|
|
|
for (i = qsize - 1; i < qsize; i--) {
|
|
|
|
buf[i]++;
|
|
|
|
if (buf[i] != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 8
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_bin2bn(md, qsize, r0) ||
|
|
|
|
!BN_lshift(r0, r0, (qsize << 3) * k) ||
|
|
|
|
!BN_add(W, W, r0)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// more of step 8
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_mask_bits(W, bits - 1) ||
|
|
|
|
!BN_copy(X, W) ||
|
|
|
|
!BN_add(X, X, test)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 9
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_lshift1(r0, q) ||
|
|
|
|
!BN_mod(c, X, r0, ctx) ||
|
|
|
|
!BN_sub(r0, c, BN_value_one()) ||
|
|
|
|
!BN_sub(p, X, r0)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 10
|
2015-10-30 20:53:00 +00:00
|
|
|
if (BN_cmp(p, test) >= 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 11
|
2015-10-30 20:53:00 +00:00
|
|
|
r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
|
|
|
|
if (r > 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
goto end; // found it
|
2015-10-30 20:53:00 +00:00
|
|
|
}
|
|
|
|
if (r != 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 13
|
2015-10-30 20:53:00 +00:00
|
|
|
counter++;
|
2017-08-18 19:06:02 +01:00
|
|
|
// "offset = offset + n + 1"
|
2015-10-30 20:53:00 +00:00
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// step 14
|
2015-10-30 20:53:00 +00:00
|
|
|
if (counter >= 4096) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
if (!BN_GENCB_call(cb, 2, 1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// We now need to generate g
|
|
|
|
// Set r0=(p-1)/q
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_sub(test, p, BN_value_one()) ||
|
|
|
|
!BN_div(r0, NULL, test, q, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:03:26 +00:00
|
|
|
mont = BN_MONT_CTX_new_for_modulus(p, ctx);
|
|
|
|
if (mont == NULL ||
|
|
|
|
!BN_set_word(test, h)) {
|
2015-10-30 20:53:00 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// g=test^r0%p
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!BN_is_one(g)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!BN_add(test, test, BN_value_one())) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
h++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_GENCB_call(cb, 3, 1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (ok) {
|
|
|
|
BN_free(dsa->p);
|
|
|
|
BN_free(dsa->q);
|
|
|
|
BN_free(dsa->g);
|
|
|
|
dsa->p = BN_dup(p);
|
|
|
|
dsa->q = BN_dup(q);
|
|
|
|
dsa->g = BN_dup(g);
|
|
|
|
if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
|
|
|
|
ok = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (out_counter != NULL) {
|
|
|
|
*out_counter = counter;
|
|
|
|
}
|
|
|
|
if (out_h != NULL) {
|
|
|
|
*out_h = h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-10-30 20:53:00 +00:00
|
|
|
|
|
|
|
BN_MONT_CTX_free(mont);
|
|
|
|
|
|
|
|
return ok;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-01-30 06:51:01 +00:00
|
|
|
DSA *DSAparams_dup(const DSA *dsa) {
|
|
|
|
DSA *ret = DSA_new();
|
|
|
|
if (ret == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->p = BN_dup(dsa->p);
|
|
|
|
ret->q = BN_dup(dsa->q);
|
|
|
|
ret->g = BN_dup(dsa->g);
|
|
|
|
if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
|
|
|
|
DSA_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
int DSA_generate_key(DSA *dsa) {
|
2015-10-30 20:53:00 +00:00
|
|
|
int ok = 0;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BIGNUM *pub_key = NULL, *priv_key = NULL;
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv_key = dsa->priv_key;
|
|
|
|
if (priv_key == NULL) {
|
|
|
|
priv_key = BN_new();
|
|
|
|
if (priv_key == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 21:36:58 +01:00
|
|
|
if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2015-10-30 20:53:00 +00:00
|
|
|
|
|
|
|
pub_key = dsa->pub_key;
|
|
|
|
if (pub_key == NULL) {
|
|
|
|
pub_key = BN_new();
|
|
|
|
if (pub_key == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-10-30 20:53:00 +00:00
|
|
|
|
2016-06-16 20:18:38 +01:00
|
|
|
if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
|
|
|
|
dsa->p, ctx) ||
|
2016-12-17 20:25:55 +00:00
|
|
|
!BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx,
|
2016-06-16 20:18:38 +01:00
|
|
|
dsa->method_mont_p)) {
|
2015-10-30 20:53:00 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
dsa->priv_key = priv_key;
|
|
|
|
dsa->pub_key = pub_key;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (dsa->pub_key == NULL) {
|
|
|
|
BN_free(pub_key);
|
|
|
|
}
|
|
|
|
if (dsa->priv_key == NULL) {
|
|
|
|
BN_free(priv_key);
|
|
|
|
}
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
|
|
|
|
return ok;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DSA_SIG *DSA_SIG_new(void) {
|
|
|
|
DSA_SIG *sig;
|
|
|
|
sig = OPENSSL_malloc(sizeof(DSA_SIG));
|
|
|
|
if (!sig) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sig->r = NULL;
|
|
|
|
sig->s = NULL;
|
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSA_SIG_free(DSA_SIG *sig) {
|
|
|
|
if (!sig) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-22 18:50:28 +01:00
|
|
|
BN_free(sig->r);
|
|
|
|
BN_free(sig->s);
|
2014-06-20 20:00:00 +01:00
|
|
|
OPENSSL_free(sig);
|
|
|
|
}
|
|
|
|
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
// mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and
|
|
|
|
// |b| as secret. This function internally uses Montgomery reduction, but
|
|
|
|
// neither inputs nor outputs are in Montgomery form.
|
|
|
|
static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
|
|
|
const BN_MONT_CTX *mont, BN_CTX *ctx) {
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
BIGNUM *tmp = BN_CTX_get(ctx);
|
|
|
|
// |BN_mod_mul_montgomery| removes a factor of R, so we cancel it with a
|
|
|
|
// single |BN_to_montgomery| which adds one factor of R.
|
|
|
|
int ok = tmp != NULL &&
|
|
|
|
BN_to_montgomery(tmp, a, mont, ctx) &&
|
|
|
|
BN_mod_mul_montgomery(r, tmp, b, mont, ctx);
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:58:36 +00:00
|
|
|
DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
|
2015-10-30 20:53:00 +00:00
|
|
|
BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
|
|
|
|
BIGNUM m;
|
|
|
|
BIGNUM xr;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
int reason = ERR_R_BN_LIB;
|
|
|
|
DSA_SIG *ret = NULL;
|
|
|
|
|
|
|
|
BN_init(&m);
|
|
|
|
BN_init(&xr);
|
|
|
|
|
|
|
|
if (!dsa->p || !dsa->q || !dsa->g) {
|
|
|
|
reason = DSA_R_MISSING_PARAMETERS;
|
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-10-30 20:53:00 +00:00
|
|
|
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
// We only support DSA keys that are a multiple of 8 bits. (This is a weaker
|
|
|
|
// check than the one in |DSA_do_check_signature|, which only allows 160-,
|
|
|
|
// 224-, and 256-bit keys.
|
|
|
|
if (BN_num_bits(dsa->q) % 8 != 0) {
|
|
|
|
reason = DSA_R_BAD_Q_VALUE;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-10-30 20:53:00 +00:00
|
|
|
s = BN_new();
|
|
|
|
if (s == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
redo:
|
2017-11-21 17:58:36 +00:00
|
|
|
if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
|
|
|
|
goto err;
|
2015-10-30 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (digest_len > BN_num_bytes(dsa->q)) {
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
// If the digest length is greater than the size of |dsa->q| use the
|
|
|
|
// BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-3, 4.2.
|
|
|
|
// Note the above check that |dsa->q| is a multiple of 8 bits.
|
2015-10-30 20:53:00 +00:00
|
|
|
digest_len = BN_num_bytes(dsa->q);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_bin2bn(digest, digest_len, &m) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
// |m| is bounded by 2^(num_bits(q)), which is slightly looser than q. This
|
|
|
|
// violates |bn_mod_add_consttime| and |mod_mul_consttime|'s preconditions.
|
|
|
|
// (The underlying algorithms could accept looser bounds, but we reduce for
|
|
|
|
// simplicity.)
|
|
|
|
size_t q_width = bn_minimal_width(dsa->q);
|
|
|
|
if (!bn_resize_words(&m, q_width) ||
|
|
|
|
!bn_resize_words(&xr, q_width)) {
|
|
|
|
goto err;
|
2015-10-30 20:53:00 +00:00
|
|
|
}
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
bn_reduce_once_in_place(m.d, 0 /* no carry word */, dsa->q->d,
|
|
|
|
xr.d /* scratch space */, q_width);
|
|
|
|
|
|
|
|
// Compute s = inv(k) (m + xr) mod q. Note |dsa->method_mont_q| is
|
|
|
|
// initialized by |dsa_sign_setup|.
|
|
|
|
if (!mod_mul_consttime(&xr, dsa->priv_key, r, dsa->method_mont_q, ctx) ||
|
|
|
|
!bn_mod_add_consttime(s, &xr, &m, dsa->q, ctx) ||
|
|
|
|
!mod_mul_consttime(s, s, kinv, dsa->method_mont_q, ctx)) {
|
2015-10-30 20:53:00 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Redo if r or s is zero as required by FIPS 186-3: this is
|
|
|
|
// very unlikely.
|
2015-10-30 20:53:00 +00:00
|
|
|
if (BN_is_zero(r) || BN_is_zero(s)) {
|
|
|
|
goto redo;
|
|
|
|
}
|
2015-12-11 20:29:17 +00:00
|
|
|
ret = DSA_SIG_new();
|
|
|
|
if (ret == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2015-10-30 20:53:00 +00:00
|
|
|
ret->r = r;
|
|
|
|
ret->s = s;
|
|
|
|
|
|
|
|
err:
|
2015-12-11 20:29:17 +00:00
|
|
|
if (ret == NULL) {
|
2015-10-30 20:53:00 +00:00
|
|
|
OPENSSL_PUT_ERROR(DSA, reason);
|
|
|
|
BN_free(r);
|
|
|
|
BN_free(s);
|
|
|
|
}
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
BN_clear_free(&m);
|
|
|
|
BN_clear_free(&xr);
|
|
|
|
BN_clear_free(kinv);
|
|
|
|
|
|
|
|
return ret;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
|
|
|
|
const DSA *dsa) {
|
2015-01-11 04:37:17 +00:00
|
|
|
int valid;
|
|
|
|
if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-11 04:37:17 +00:00
|
|
|
return valid;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
|
|
|
|
size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
|
2015-10-30 20:53:00 +00:00
|
|
|
BN_CTX *ctx;
|
|
|
|
BIGNUM u1, u2, t1;
|
|
|
|
int ret = 0;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
*out_valid = 0;
|
|
|
|
|
|
|
|
if (!dsa->p || !dsa->q || !dsa->g) {
|
|
|
|
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = BN_num_bits(dsa->q);
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
// FIPS 186-3 allows only different sizes for q.
|
2015-10-30 20:53:00 +00:00
|
|
|
if (i != 160 && i != 224 && i != 256) {
|
|
|
|
OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
|
|
|
|
OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_init(&u1);
|
|
|
|
BN_init(&u2);
|
|
|
|
BN_init(&t1);
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
|
|
|
|
BN_ucmp(sig->r, dsa->q) >= 0) {
|
|
|
|
ret = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
|
|
|
|
BN_ucmp(sig->s, dsa->q) >= 0) {
|
|
|
|
ret = 1;
|
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Calculate W = inv(S) mod Q
|
|
|
|
// save W in u2
|
2015-10-30 20:53:00 +00:00
|
|
|
if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// save M in u1
|
2015-10-30 20:53:00 +00:00
|
|
|
if (digest_len > (i >> 3)) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// if the digest length is greater than the size of q use the
|
|
|
|
// BN_num_bits(dsa->q) leftmost bits of the digest, see
|
|
|
|
// fips 186-3, 4.2
|
2015-10-30 20:53:00 +00:00
|
|
|
digest_len = (i >> 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// u1 = M * w mod q
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// u2 = r * w mod q
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:11:04 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
|
2016-06-16 20:13:43 +01:00
|
|
|
(CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
|
2016-03-25 20:11:04 +00:00
|
|
|
ctx)) {
|
2015-10-30 20:53:00 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
|
2016-03-25 20:11:04 +00:00
|
|
|
dsa->method_mont_p)) {
|
2015-10-30 20:53:00 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// BN_copy(&u1,&t1);
|
|
|
|
// let u1 = u1 mod q
|
2015-10-30 20:53:00 +00:00
|
|
|
if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// V is now in u1. If the signature is correct, it will be
|
|
|
|
// equal to R.
|
2015-10-30 20:53:00 +00:00
|
|
|
*out_valid = BN_ucmp(&u1, sig->r) == 0;
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (ret != 1) {
|
|
|
|
OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
|
|
|
|
}
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
BN_free(&u1);
|
|
|
|
BN_free(&u2);
|
|
|
|
BN_free(&t1);
|
|
|
|
|
|
|
|
return ret;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
|
2017-11-21 17:58:36 +00:00
|
|
|
uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) {
|
2014-06-20 20:00:00 +01:00
|
|
|
DSA_SIG *s;
|
|
|
|
|
|
|
|
s = DSA_do_sign(digest, digest_len, dsa);
|
|
|
|
if (s == NULL) {
|
|
|
|
*out_siglen = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_siglen = i2d_DSA_SIG(s, &out_sig);
|
|
|
|
DSA_SIG_free(s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
|
|
|
|
const uint8_t *sig, size_t sig_len, const DSA *dsa) {
|
2015-01-11 04:37:17 +00:00
|
|
|
int valid;
|
|
|
|
if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_check_signature(int *out_valid, const uint8_t *digest,
|
|
|
|
size_t digest_len, const uint8_t *sig, size_t sig_len,
|
|
|
|
const DSA *dsa) {
|
2014-06-20 20:00:00 +01:00
|
|
|
DSA_SIG *s = NULL;
|
2015-01-11 04:37:17 +00:00
|
|
|
int ret = 0;
|
2015-01-08 20:26:55 +00:00
|
|
|
uint8_t *der = NULL;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
s = DSA_SIG_new();
|
|
|
|
if (s == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-01-08 20:26:55 +00:00
|
|
|
const uint8_t *sigp = sig;
|
|
|
|
if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Ensure that the signature uses DER and doesn't have trailing garbage.
|
2015-01-08 20:26:55 +00:00
|
|
|
int der_len = i2d_DSA_SIG(s, &der);
|
2016-12-13 06:07:13 +00:00
|
|
|
if (der_len < 0 || (size_t)der_len != sig_len ||
|
|
|
|
OPENSSL_memcmp(sig, der, sig_len)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-01-11 04:37:17 +00:00
|
|
|
ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
err:
|
2015-04-22 18:50:28 +01:00
|
|
|
OPENSSL_free(der);
|
|
|
|
DSA_SIG_free(s);
|
2014-06-20 20:00:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// der_len_len returns the number of bytes needed to represent a length of |len|
|
|
|
|
// in DER.
|
2016-01-02 22:35:47 +00:00
|
|
|
static size_t der_len_len(size_t len) {
|
|
|
|
if (len < 0x80) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
size_t ret = 1;
|
|
|
|
while (len > 0) {
|
|
|
|
ret++;
|
|
|
|
len >>= 8;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-01-02 22:35:47 +00:00
|
|
|
int DSA_size(const DSA *dsa) {
|
|
|
|
size_t order_len = BN_num_bytes(dsa->q);
|
2017-08-18 19:06:02 +01:00
|
|
|
// Compute the maximum length of an |order_len| byte integer. Defensively
|
|
|
|
// assume that the leading 0x00 is included.
|
2016-01-02 22:35:47 +00:00
|
|
|
size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
|
|
|
|
if (integer_len < order_len) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-18 19:06:02 +01:00
|
|
|
// A DSA signature is two INTEGERs.
|
2016-01-02 22:35:47 +00:00
|
|
|
size_t value_len = 2 * integer_len;
|
|
|
|
if (value_len < integer_len) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-18 19:06:02 +01:00
|
|
|
// Add the header.
|
2016-01-02 22:35:47 +00:00
|
|
|
size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
|
|
|
|
if (ret < value_len) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:58:36 +00:00
|
|
|
static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
|
|
|
|
BIGNUM **out_r) {
|
2015-10-30 20:53:00 +00:00
|
|
|
BN_CTX *ctx;
|
2018-01-25 16:39:22 +00:00
|
|
|
BIGNUM k, *kinv = NULL, *r = NULL;
|
2015-10-30 20:53:00 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!dsa->p || !dsa->q || !dsa->g) {
|
|
|
|
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_init(&k);
|
|
|
|
|
|
|
|
ctx = ctx_in;
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = BN_new();
|
2018-01-25 16:39:22 +00:00
|
|
|
kinv = BN_new();
|
|
|
|
if (r == NULL || kinv == NULL ||
|
|
|
|
// Get random k
|
|
|
|
!BN_rand_range_ex(&k, 1, dsa->q) ||
|
|
|
|
!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
|
2016-06-16 20:13:43 +01:00
|
|
|
(CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
|
|
|
|
ctx) ||
|
|
|
|
!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
|
|
|
|
(CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
|
2018-01-25 16:39:22 +00:00
|
|
|
ctx) ||
|
|
|
|
// Compute r = (g^k mod p) mod q
|
|
|
|
!BN_mod_exp_mont_consttime(r, dsa->g, &k, dsa->p, ctx,
|
|
|
|
dsa->method_mont_p) ||
|
Fix some timing leaks in the DSA code.
The DSA code is deprecated and will, hopefully, be removed in the future.
Nonetheless, this is easy enough to fix. It's the analog of the work we'd
already done for ECDSA.
- Document more clearly that we don't care about the DSA code.
- Use the existing constant-time modular addition function rather than
the ad-hoc code.
- Reduce the digest to satisfy modular operations' invariants. (The
underlying algorithms could accept looser bounds, but we reduce for
simplicity.) There's no particular reason to do this in constant time,
but we have the code for it, so we may as well.
- This additionally adds a missing check that num_bits(q) is a multiple
of 8. We otherwise don't compute the right answer. Verification
already rejected all 160-, 224-, and 256-bit keys, and we only
generate DSA parameters where the length of q matches some hash
function's length, so this is unlikely to cause anyone trouble.
- Use Montgomery reduction to perform the modular multiplication. This
could be optimized to save a couple Montgomery reductions as in ECDSA,
but DSA is deprecated, so I haven't bothered optimizing this.
- The reduction from g^k (mod p) to r = g^k (mod p) (mod q) is left
in variable time, but reversing it would require a discrete log
anyway. (The corresponding ECDSA operation is much easier to make
constant-time due to Hasse's theorem, though that's actually still a
TODO. I need to finish lifting EC_FELEM up the stack.)
Thanks to Keegan Ryan from NCC Group for reporting the modular addition issue
(CVE-2018-0495). The remainder is stuff I noticed along the way.
Update-Note: See the num_bits(q) change.
Change-Id: I4f032b041e2aeb09f9737a39f178c24e6a7fa1cb
Reviewed-on: https://boringssl-review.googlesource.com/29145
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-06-07 16:32:15 +01:00
|
|
|
// Note |BN_mod| below is not constant-time and may leak information about
|
|
|
|
// |r|. |dsa->p| may be significantly larger than |dsa->q|, so this is not
|
|
|
|
// easily performed in constant-time with Montgomery reduction.
|
|
|
|
//
|
|
|
|
// However, |r| at this point is g^k (mod p). It is almost the value of
|
|
|
|
// |r| revealed in the signature anyway (g^k (mod p) (mod q)), going from
|
|
|
|
// it to |k| would require computing a discrete log.
|
2018-01-25 16:39:22 +00:00
|
|
|
!BN_mod(r, r, dsa->q, ctx) ||
|
|
|
|
// Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
|
|
|
|
// Theorem.
|
2016-12-17 19:27:16 +00:00
|
|
|
!bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
|
2015-10-30 20:53:00 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_clear_free(*out_kinv);
|
|
|
|
*out_kinv = kinv;
|
|
|
|
kinv = NULL;
|
|
|
|
BN_clear_free(*out_r);
|
|
|
|
*out_r = r;
|
|
|
|
ret = 1;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-30 20:53:00 +00:00
|
|
|
err:
|
|
|
|
if (!ret) {
|
|
|
|
OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
|
|
|
|
if (r != NULL) {
|
|
|
|
BN_clear_free(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx_in == NULL) {
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
BN_clear_free(&k);
|
2016-12-17 19:27:16 +00:00
|
|
|
BN_clear_free(kinv);
|
2015-10-30 20:53:00 +00:00
|
|
|
return ret;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-12-05 04:14:35 +00:00
|
|
|
int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
|
2017-05-14 22:10:18 +01:00
|
|
|
CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
|
2015-04-15 22:29:53 +01:00
|
|
|
int index;
|
2017-05-14 22:10:18 +01:00
|
|
|
if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
|
2015-12-05 04:14:35 +00:00
|
|
|
free_func)) {
|
2015-04-15 22:29:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return index;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 00:09:42 +01:00
|
|
|
int DSA_set_ex_data(DSA *dsa, int idx, void *arg) {
|
|
|
|
return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 00:09:42 +01:00
|
|
|
void *DSA_get_ex_data(const DSA *dsa, int idx) {
|
|
|
|
return CRYPTO_get_ex_data(&dsa->ex_data, idx);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-09-06 01:04:51 +01:00
|
|
|
|
2017-08-01 00:09:42 +01:00
|
|
|
DH *DSA_dup_DH(const DSA *dsa) {
|
|
|
|
if (dsa == NULL) {
|
|
|
|
return NULL;
|
2014-09-06 01:04:51 +01:00
|
|
|
}
|
2017-08-01 00:09:42 +01:00
|
|
|
|
|
|
|
DH *ret = DH_new();
|
2014-09-06 01:04:51 +01:00
|
|
|
if (ret == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2017-08-01 00:09:42 +01:00
|
|
|
if (dsa->q != NULL) {
|
|
|
|
ret->priv_length = BN_num_bits(dsa->q);
|
|
|
|
if ((ret->q = BN_dup(dsa->q)) == NULL) {
|
2014-09-06 01:04:51 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 00:09:42 +01:00
|
|
|
if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) ||
|
|
|
|
(dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) ||
|
|
|
|
(dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) ||
|
|
|
|
(dsa->priv_key != NULL &&
|
|
|
|
(ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) {
|
|
|
|
goto err;
|
2014-09-06 01:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err:
|
2015-04-22 18:50:28 +01:00
|
|
|
DH_free(ret);
|
2014-09-06 01:04:51 +01:00
|
|
|
return NULL;
|
|
|
|
}
|