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
12 @file dsa_verify_key.c
13 DSA implementation, verify a key, Tom St Denis
21 Yeah, this function should've been called dsa_validate_key()
22 in the first place and for compat-reasons we keep it
25 @param key The key to validate
26 @param stat [out] Result of test, 1==valid, 0==invalid
27 @return CRYPT_OK if successful
29 int dsa_verify_key(dsa_key *key, int *stat)
33 err = dsa_int_validate_primes(key, stat);
34 if (err != CRYPT_OK || *stat == 0) return err;
36 err = dsa_int_validate_pqg(key, stat);
37 if (err != CRYPT_OK || *stat == 0) return err;
39 return dsa_int_validate_xy(key, stat);
43 Non-complex part (no primality testing) of the validation
44 of DSA params (p, q, g)
46 @param key The key to validate
47 @param stat [out] Result of test, 1==valid, 0==invalid
48 @return CRYPT_OK if successful
50 int dsa_int_validate_pqg(dsa_key *key, int *stat)
55 LTC_ARGCHK(key != NULL);
56 LTC_ARGCHK(stat != NULL);
60 if ( key->qord >= LTC_MDSA_MAX_GROUP || key->qord <= 15 ||
61 (unsigned long)key->qord >= mp_unsigned_bin_size(key->p) ||
62 (mp_unsigned_bin_size(key->p) - key->qord) >= LTC_MDSA_DELTA ) {
66 /* FIPS 186-4 chapter 4.1: 1 < g < p */
67 if (mp_cmp_d(key->g, 1) != LTC_MP_GT || mp_cmp(key->g, key->p) != LTC_MP_LT) {
71 if ((err = mp_init_multi(&tmp1, &tmp2, NULL)) != CRYPT_OK) { return err; }
73 /* FIPS 186-4 chapter 4.1: q is a divisor of (p - 1) */
74 if ((err = mp_sub_d(key->p, 1, tmp1)) != CRYPT_OK) { goto error; }
75 if ((err = mp_div(tmp1, key->q, tmp1, tmp2)) != CRYPT_OK) { goto error; }
76 if (mp_iszero(tmp2) != LTC_MP_YES) {
81 /* FIPS 186-4 chapter 4.1: g is a generator of a subgroup of order q in
82 * the multiplicative group of GF(p) - so we make sure that g^q mod p = 1
84 if ((err = mp_exptmod(key->g, key->q, key->p, tmp1)) != CRYPT_OK) { goto error; }
85 if (mp_cmp_d(tmp1, 1) != LTC_MP_EQ) {
93 mp_clear_multi(tmp2, tmp1, NULL);
98 Primality testing of DSA params p and q
100 @param key The key to validate
101 @param stat [out] Result of test, 1==valid, 0==invalid
102 @return CRYPT_OK if successful
104 int dsa_int_validate_primes(dsa_key *key, int *stat)
109 LTC_ARGCHK(key != NULL);
110 LTC_ARGCHK(stat != NULL);
113 if ((err = mp_prime_is_prime(key->q, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
116 if (res == LTC_MP_NO) {
121 if ((err = mp_prime_is_prime(key->p, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
124 if (res == LTC_MP_NO) {
133 Validation of a DSA key (x and y values)
135 @param key The key to validate
136 @param stat [out] Result of test, 1==valid, 0==invalid
137 @return CRYPT_OK if successful
139 int dsa_int_validate_xy(dsa_key *key, int *stat)
145 LTC_ARGCHK(key != NULL);
146 LTC_ARGCHK(stat != NULL);
149 if ((err = mp_init(&tmp)) != CRYPT_OK) {
152 if ((err = mp_sub_d(key->p, 1, tmp)) != CRYPT_OK) {
155 if (mp_cmp_d(key->y, 1) != LTC_MP_GT || mp_cmp(key->y, tmp) != LTC_MP_LT) {
160 if (key->type == PK_PRIVATE) {
161 /* FIPS 186-4 chapter 4.1: 0 < x < q */
162 if (mp_cmp_d(key->x, 0) != LTC_MP_GT || mp_cmp(key->x, key->q) != LTC_MP_LT) {
166 /* FIPS 186-4 chapter 4.1: y = g^x mod p */
167 if ((err = mp_exptmod(key->g, key->x, key->p, tmp)) != CRYPT_OK) {
170 if (mp_cmp(tmp, key->y) != LTC_MP_EQ) {
176 /* with just a public key we cannot test y = g^x mod p therefore we
177 * only test that y^q mod p = 1, which makes sure y is in g^x mod p
179 if ((err = mp_exptmod(key->y, key->q, key->p, tmp)) != CRYPT_OK) {
182 if (mp_cmp_d(tmp, 1) != LTC_MP_EQ) {
197 /* ref: $Format:%D$ */
198 /* git commit: $Format:%H$ */
199 /* commit time: $Format:%ai$ */