1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
14
@file dsa_verify_key.c
15
DSA implementation, verify a key, Tom St Denis
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
26
int dsa_verify_key(dsa_key *key, int *stat)
31
LTC_ARGCHK(key != NULL);
32
LTC_ARGCHK(stat != NULL);
34
/* default to an invalid key */
37
/* first make sure key->q and key->p are prime */
38
if ((err = is_prime(&key->q, &res)) != CRYPT_OK) {
46
if ((err = is_prime(&key->p, &res)) != CRYPT_OK) {
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) {
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) {
65
if (!(mp_cmp_d(&key->y, 1) == MP_GT && mp_cmp(&key->y, &tmp) == MP_LT)) {
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) {
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) {
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) {
90
/* at this point we are out of tests ;-( */
94
error: err = mpi_to_ltc_error(err);
95
done : mp_clear_multi(&tmp, &tmp2, NULL);
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 $ */