comparison src/pk/dsa/dsa_verify_key.c @ 280:59400faa4b44 libtomcrypt-orig libtomcrypt-1.05

Re-import libtomcrypt 1.05 for cleaner propagating. From crypt-1.05.tar.bz2, SHA1 of 88250202bb51570dc64f7e8f1c943cda9479258f
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 12:58:00 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
-1:000000000000 280:59400faa4b44
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 $ */