3 * The DSA publickey algorithm.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002, 2003 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 _dsa_verify(const struct dsa_public_key *key,
39 const uint8_t *digest,
40 const struct dsa_signature *signature)
48 if (mpz_sizeinbase(key->q, 2) != 8 * digest_size)
51 /* Check that r and s are in the proper range */
52 if (mpz_sgn(signature->r) <= 0 || mpz_cmp(signature->r, key->q) >= 0)
55 if (mpz_sgn(signature->s) <= 0 || mpz_cmp(signature->s, key->q) >= 0)
60 /* Compute w = s^-1 (mod q) */
62 /* NOTE: In gmp-2, mpz_invert sometimes generates negative inverses,
63 * so we need gmp-3 or better. */
64 if (!mpz_invert(w, signature->s, key->q))
73 /* The message digest */
74 nettle_mpz_set_str_256_u(tmp, digest_size, digest);
76 /* v = g^{w * h (mod q)} (mod p) */
78 mpz_fdiv_r(tmp, tmp, key->q);
80 mpz_powm(v, key->g, tmp, key->p);
82 /* y^{w * r (mod q) } (mod p) */
83 mpz_mul(tmp, signature->r, w);
84 mpz_fdiv_r(tmp, tmp, key->q);
86 mpz_powm(tmp, key->y, tmp, key->p);
88 /* v = (g^{w * h} * y^{w * r} (mod p) ) (mod q) */
90 mpz_fdiv_r(v, v, key->p);
92 mpz_fdiv_r(v, v, key->q);
94 res = !mpz_cmp(v, signature->r);