Mercurial > dropbear
comparison libtomcrypt/src/pk/dsa/dsa_verify_hash.c @ 285:1b9e69c058d2
propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 08 Mar 2006 13:23:58 +0000 |
parents | |
children | 0cbe8f6dbf9e |
comparison
equal
deleted
inserted
replaced
281:997e6f7dc01e | 285:1b9e69c058d2 |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 * | |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | |
10 */ | |
11 #include "tomcrypt.h" | |
12 | |
13 /** | |
14 @file dsa_verify_hash.c | |
15 DSA implementation, verify a signature, Tom St Denis | |
16 */ | |
17 | |
18 | |
19 #ifdef MDSA | |
20 | |
21 /** | |
22 Verify a DSA signature | |
23 @param r DSA "r" parameter | |
24 @param s DSA "s" parameter | |
25 @param hash The hash that was signed | |
26 @param hashlen The length of the hash that was signed | |
27 @param stat [out] The result of the signature verification, 1==valid, 0==invalid | |
28 @param key The corresponding public DH key | |
29 @return CRYPT_OK if successful (even if the signature is invalid) | |
30 */ | |
31 int dsa_verify_hash_raw( mp_int *r, mp_int *s, | |
32 const unsigned char *hash, unsigned long hashlen, | |
33 int *stat, dsa_key *key) | |
34 { | |
35 mp_int w, v, u1, u2; | |
36 int err; | |
37 | |
38 LTC_ARGCHK(r != NULL); | |
39 LTC_ARGCHK(s != NULL); | |
40 LTC_ARGCHK(stat != NULL); | |
41 LTC_ARGCHK(key != NULL); | |
42 | |
43 /* default to invalid signature */ | |
44 *stat = 0; | |
45 | |
46 /* init our variables */ | |
47 if ((err = mp_init_multi(&w, &v, &u1, &u2, NULL)) != MP_OKAY) { | |
48 return mpi_to_ltc_error(err); | |
49 } | |
50 | |
51 /* neither r or s can be null or >q*/ | |
52 if (mp_iszero(r) == MP_YES || mp_iszero(s) == MP_YES || mp_cmp(r, &key->q) != MP_LT || mp_cmp(s, &key->q) != MP_LT) { | |
53 err = CRYPT_INVALID_PACKET; | |
54 goto done; | |
55 } | |
56 | |
57 /* w = 1/s mod q */ | |
58 if ((err = mp_invmod(s, &key->q, &w)) != MP_OKAY) { goto error; } | |
59 | |
60 /* u1 = m * w mod q */ | |
61 if ((err = mp_read_unsigned_bin(&u1, (unsigned char *)hash, hashlen)) != MP_OKAY) { goto error; } | |
62 if ((err = mp_mulmod(&u1, &w, &key->q, &u1)) != MP_OKAY) { goto error; } | |
63 | |
64 /* u2 = r*w mod q */ | |
65 if ((err = mp_mulmod(r, &w, &key->q, &u2)) != MP_OKAY) { goto error; } | |
66 | |
67 /* v = g^u1 * y^u2 mod p mod q */ | |
68 if ((err = mp_exptmod(&key->g, &u1, &key->p, &u1)) != MP_OKAY) { goto error; } | |
69 if ((err = mp_exptmod(&key->y, &u2, &key->p, &u2)) != MP_OKAY) { goto error; } | |
70 if ((err = mp_mulmod(&u1, &u2, &key->p, &v)) != MP_OKAY) { goto error; } | |
71 if ((err = mp_mod(&v, &key->q, &v)) != MP_OKAY) { goto error; } | |
72 | |
73 /* if r = v then we're set */ | |
74 if (mp_cmp(r, &v) == MP_EQ) { | |
75 *stat = 1; | |
76 } | |
77 | |
78 err = CRYPT_OK; | |
79 goto done; | |
80 | |
81 error : err = mpi_to_ltc_error(err); | |
82 done : mp_clear_multi(&w, &v, &u1, &u2, NULL); | |
83 return err; | |
84 } | |
85 | |
86 /** | |
87 Verify a DSA signature | |
88 @param sig The signature | |
89 @param siglen The length of the signature (octets) | |
90 @param hash The hash that was signed | |
91 @param hashlen The length of the hash that was signed | |
92 @param stat [out] The result of the signature verification, 1==valid, 0==invalid | |
93 @param key The corresponding public DH key | |
94 @return CRYPT_OK if successful (even if the signature is invalid) | |
95 */ | |
96 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
97 const unsigned char *hash, unsigned long hashlen, | |
98 int *stat, dsa_key *key) | |
99 { | |
100 int err; | |
101 mp_int r, s; | |
102 | |
103 if ((err = mp_init_multi(&r, &s, NULL)) != CRYPT_OK) { | |
104 return CRYPT_MEM; | |
105 } | |
106 | |
107 /* decode the sequence */ | |
108 if ((err = der_decode_sequence_multi(sig, siglen, | |
109 LTC_ASN1_INTEGER, 1UL, &r, | |
110 LTC_ASN1_INTEGER, 1UL, &s, | |
111 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { | |
112 goto LBL_ERR; | |
113 } | |
114 | |
115 /* do the op */ | |
116 err = dsa_verify_hash_raw(&r, &s, hash, hashlen, stat, key); | |
117 | |
118 LBL_ERR: | |
119 mp_clear_multi(&r, &s, NULL); | |
120 return err; | |
121 } | |
122 | |
123 #endif | |
124 | |
125 | |
126 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_verify_hash.c,v $ */ | |
127 /* $Revision: 1.8 $ */ | |
128 /* $Date: 2005/05/15 21:48:59 $ */ |