Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / dsa-verify.c
bloba96469f65d81f4e2b5d7791f366984f7957cbca8
1 /* dsa-verify.c
3 * The DSA publickey algorithm.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002, 2003 Niels Möller
9 *
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., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <stdlib.h>
32 #include "dsa.h"
34 #include "bignum.h"
36 int
37 _dsa_verify(const struct dsa_public_key *key,
38 unsigned digest_size,
39 const uint8_t *digest,
40 const struct dsa_signature *signature)
42 mpz_t w;
43 mpz_t tmp;
44 mpz_t v;
46 int res;
48 if (mpz_sizeinbase(key->q, 2) != 8 * digest_size)
49 return 0;
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)
53 return 0;
55 if (mpz_sgn(signature->s) <= 0 || mpz_cmp(signature->s, key->q) >= 0)
56 return 0;
58 mpz_init(w);
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))
66 mpz_clear(w);
67 return 0;
70 mpz_init(tmp);
71 mpz_init(v);
73 /* The message digest */
74 nettle_mpz_set_str_256_u(tmp, digest_size, digest);
76 /* v = g^{w * h (mod q)} (mod p) */
77 mpz_mul(tmp, tmp, w);
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) */
89 mpz_mul(v, v, tmp);
90 mpz_fdiv_r(v, v, key->p);
92 mpz_fdiv_r(v, v, key->q);
94 res = !mpz_cmp(v, signature->r);
96 mpz_clear(w);
97 mpz_clear(tmp);
98 mpz_clear(v);
100 return res;