comparison libtomcrypt/src/pk/dsa/dsa_verify_key.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_key.c
15 DSA implementation, verify a key, Tom St Denis
16 */
17
18 #ifdef MDSA
19
20 /**
21 Verify a DSA key for validity
22 @param key The key to verify
23 @param stat [out] Result of test, 1==valid, 0==invalid
24 @return CRYPT_OK if successful
25 */
26 int dsa_verify_key(dsa_key *key, int *stat)
27 {
28 mp_int tmp, tmp2;
29 int res, err;
30
31 LTC_ARGCHK(key != NULL);
32 LTC_ARGCHK(stat != NULL);
33
34 /* default to an invalid key */
35 *stat = 0;
36
37 /* first make sure key->q and key->p are prime */
38 if ((err = is_prime(&key->q, &res)) != CRYPT_OK) {
39 return err;
40 }
41 if (res == 0) {
42 return CRYPT_OK;
43 }
44
45
46 if ((err = is_prime(&key->p, &res)) != CRYPT_OK) {
47 return err;
48 }
49 if (res == 0) {
50 return CRYPT_OK;
51 }
52
53 /* now make sure that g is not -1, 0 or 1 and <p */
54 if (mp_cmp_d(&key->g, 0) == MP_EQ || mp_cmp_d(&key->g, 1) == MP_EQ) {
55 return CRYPT_OK;
56 }
57 if ((err = mp_init_multi(&tmp, &tmp2, NULL)) != MP_OKAY) { goto error; }
58 if ((err = mp_sub_d(&key->p, 1, &tmp)) != MP_OKAY) { goto error; }
59 if (mp_cmp(&tmp, &key->g) == MP_EQ || mp_cmp(&key->g, &key->p) != MP_LT) {
60 err = CRYPT_OK;
61 goto done;
62 }
63
64 /* 1 < y < p-1 */
65 if (!(mp_cmp_d(&key->y, 1) == MP_GT && mp_cmp(&key->y, &tmp) == MP_LT)) {
66 err = CRYPT_OK;
67 goto done;
68 }
69
70 /* now we have to make sure that g^q = 1, and that p-1/q gives 0 remainder */
71 if ((err = mp_div(&tmp, &key->q, &tmp, &tmp2)) != MP_OKAY) { goto error; }
72 if (mp_iszero(&tmp2) != MP_YES) {
73 err = CRYPT_OK;
74 goto done;
75 }
76
77 if ((err = mp_exptmod(&key->g, &key->q, &key->p, &tmp)) != MP_OKAY) { goto error; }
78 if (mp_cmp_d(&tmp, 1) != MP_EQ) {
79 err = CRYPT_OK;
80 goto done;
81 }
82
83 /* now we have to make sure that y^q = 1, this makes sure y \in g^x mod p */
84 if ((err = mp_exptmod(&key->y, &key->q, &key->p, &tmp)) != MP_OKAY) { goto error; }
85 if (mp_cmp_d(&tmp, 1) != MP_EQ) {
86 err = CRYPT_OK;
87 goto done;
88 }
89
90 /* at this point we are out of tests ;-( */
91 err = CRYPT_OK;
92 *stat = 1;
93 goto done;
94 error: err = mpi_to_ltc_error(err);
95 done : mp_clear_multi(&tmp, &tmp2, NULL);
96 return err;
97 }
98 #endif
99
100 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_verify_key.c,v $ */
101 /* $Revision: 1.3 $ */
102 /* $Date: 2005/05/05 14:35:59 $ */