dsa-verify.c 2.46 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* dsa-verify.c
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

   The DSA publickey algorithm.

   Copyright (C) 2002, 2003 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33 34

#if HAVE_CONFIG_H
35
# include "config.h"
Niels Möller's avatar
Niels Möller committed
36 37
#endif

38 39
#include <stdlib.h>

Niels Möller's avatar
Niels Möller committed
40 41
#include "dsa.h"

42 43
#include "bignum.h"

Niels Möller's avatar
Niels Möller committed
44
int
45 46
dsa_verify(const struct dsa_params *params,
	   const mpz_t y,
47 48 49
	   size_t digest_size,
	   const uint8_t *digest,
	   const struct dsa_signature *signature)
Niels Möller's avatar
Niels Möller committed
50 51 52 53 54 55
{
  mpz_t w;
  mpz_t tmp;
  mpz_t v;

  int res;
56

Niels Möller's avatar
Niels Möller committed
57
  /* Check that r and s are in the proper range */
58
  if (mpz_sgn(signature->r) <= 0 || mpz_cmp(signature->r, params->q) >= 0)
Niels Möller's avatar
Niels Möller committed
59 60
    return 0;

61
  if (mpz_sgn(signature->s) <= 0 || mpz_cmp(signature->s, params->q) >= 0)
Niels Möller's avatar
Niels Möller committed
62 63 64 65 66 67 68 69
    return 0;

  mpz_init(w);

  /* Compute w = s^-1 (mod q) */

  /* NOTE: In gmp-2, mpz_invert sometimes generates negative inverses,
   * so we need gmp-3 or better. */
70
  if (!mpz_invert(w, signature->s, params->q))
Niels Möller's avatar
Niels Möller committed
71 72 73 74 75 76 77
    {
      mpz_clear(w);
      return 0;
    }

  mpz_init(tmp);
  mpz_init(v);
78 79

  /* The message digest */
80
  _dsa_hash (tmp, mpz_sizeinbase (params->q, 2), digest_size, digest);
Niels Möller's avatar
Niels Möller committed
81 82 83
  
  /* v = g^{w * h (mod q)} (mod p)  */
  mpz_mul(tmp, tmp, w);
84
  mpz_fdiv_r(tmp, tmp, params->q);
Niels Möller's avatar
Niels Möller committed
85

86
  mpz_powm(v, params->g, tmp, params->p);
Niels Möller's avatar
Niels Möller committed
87 88 89

  /* y^{w * r (mod q) } (mod p) */
  mpz_mul(tmp, signature->r, w);
90
  mpz_fdiv_r(tmp, tmp, params->q);
Niels Möller's avatar
Niels Möller committed
91

92
  mpz_powm(tmp, y, tmp, params->p);
Niels Möller's avatar
Niels Möller committed
93 94 95

  /* v = (g^{w * h} * y^{w * r} (mod p) ) (mod q) */
  mpz_mul(v, v, tmp);
96
  mpz_fdiv_r(v, v, params->p);
Niels Möller's avatar
Niels Möller committed
97

98
  mpz_fdiv_r(v, v, params->q);
Niels Möller's avatar
Niels Möller committed
99 100 101 102 103 104 105 106 107

  res = !mpz_cmp(v, signature->r);

  mpz_clear(w);
  mpz_clear(tmp);
  mpz_clear(v);

  return res;
}