Path: blob/master/libs/tomcrypt/src/pk/dsa/dsa_verify_key.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file dsa_verify_key.c12DSA implementation, verify a key, Tom St Denis13*/1415#ifdef LTC_MDSA1617/**18Validate a DSA key1920Yeah, this function should've been called dsa_validate_key()21in the first place and for compat-reasons we keep it22as it was (for now).2324@param key The key to validate25@param stat [out] Result of test, 1==valid, 0==invalid26@return CRYPT_OK if successful27*/28int dsa_verify_key(dsa_key *key, int *stat)29{30int err;3132err = dsa_int_validate_primes(key, stat);33if (err != CRYPT_OK || *stat == 0) return err;3435err = dsa_int_validate_pqg(key, stat);36if (err != CRYPT_OK || *stat == 0) return err;3738return dsa_int_validate_xy(key, stat);39}4041/**42Non-complex part (no primality testing) of the validation43of DSA params (p, q, g)4445@param key The key to validate46@param stat [out] Result of test, 1==valid, 0==invalid47@return CRYPT_OK if successful48*/49int dsa_int_validate_pqg(dsa_key *key, int *stat)50{51void *tmp1, *tmp2;52int err;5354LTC_ARGCHK(key != NULL);55LTC_ARGCHK(stat != NULL);56*stat = 0;5758/* check q-order */59if ( key->qord >= LTC_MDSA_MAX_GROUP || key->qord <= 15 ||60(unsigned long)key->qord >= mp_unsigned_bin_size(key->p) ||61(mp_unsigned_bin_size(key->p) - key->qord) >= LTC_MDSA_DELTA ) {62return CRYPT_OK;63}6465/* FIPS 186-4 chapter 4.1: 1 < g < p */66if (mp_cmp_d(key->g, 1) != LTC_MP_GT || mp_cmp(key->g, key->p) != LTC_MP_LT) {67return CRYPT_OK;68}6970if ((err = mp_init_multi(&tmp1, &tmp2, NULL)) != CRYPT_OK) { return err; }7172/* FIPS 186-4 chapter 4.1: q is a divisor of (p - 1) */73if ((err = mp_sub_d(key->p, 1, tmp1)) != CRYPT_OK) { goto error; }74if ((err = mp_div(tmp1, key->q, tmp1, tmp2)) != CRYPT_OK) { goto error; }75if (mp_iszero(tmp2) != LTC_MP_YES) {76err = CRYPT_OK;77goto error;78}7980/* FIPS 186-4 chapter 4.1: g is a generator of a subgroup of order q in81* the multiplicative group of GF(p) - so we make sure that g^q mod p = 182*/83if ((err = mp_exptmod(key->g, key->q, key->p, tmp1)) != CRYPT_OK) { goto error; }84if (mp_cmp_d(tmp1, 1) != LTC_MP_EQ) {85err = CRYPT_OK;86goto error;87}8889err = CRYPT_OK;90*stat = 1;91error:92mp_clear_multi(tmp2, tmp1, NULL);93return err;94}9596/**97Primality testing of DSA params p and q9899@param key The key to validate100@param stat [out] Result of test, 1==valid, 0==invalid101@return CRYPT_OK if successful102*/103int dsa_int_validate_primes(dsa_key *key, int *stat)104{105int err, res;106107*stat = 0;108LTC_ARGCHK(key != NULL);109LTC_ARGCHK(stat != NULL);110111/* key->q prime? */112if ((err = mp_prime_is_prime(key->q, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {113return err;114}115if (res == LTC_MP_NO) {116return CRYPT_OK;117}118119/* key->p prime? */120if ((err = mp_prime_is_prime(key->p, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {121return err;122}123if (res == LTC_MP_NO) {124return CRYPT_OK;125}126127*stat = 1;128return CRYPT_OK;129}130131/**132Validation of a DSA key (x and y values)133134@param key The key to validate135@param stat [out] Result of test, 1==valid, 0==invalid136@return CRYPT_OK if successful137*/138int dsa_int_validate_xy(dsa_key *key, int *stat)139{140void *tmp;141int err;142143*stat = 0;144LTC_ARGCHK(key != NULL);145LTC_ARGCHK(stat != NULL);146147/* 1 < y < p-1 */148if ((err = mp_init(&tmp)) != CRYPT_OK) {149return err;150}151if ((err = mp_sub_d(key->p, 1, tmp)) != CRYPT_OK) {152goto error;153}154if (mp_cmp_d(key->y, 1) != LTC_MP_GT || mp_cmp(key->y, tmp) != LTC_MP_LT) {155err = CRYPT_OK;156goto error;157}158159if (key->type == PK_PRIVATE) {160/* FIPS 186-4 chapter 4.1: 0 < x < q */161if (mp_cmp_d(key->x, 0) != LTC_MP_GT || mp_cmp(key->x, key->q) != LTC_MP_LT) {162err = CRYPT_OK;163goto error;164}165/* FIPS 186-4 chapter 4.1: y = g^x mod p */166if ((err = mp_exptmod(key->g, key->x, key->p, tmp)) != CRYPT_OK) {167goto error;168}169if (mp_cmp(tmp, key->y) != LTC_MP_EQ) {170err = CRYPT_OK;171goto error;172}173}174else {175/* with just a public key we cannot test y = g^x mod p therefore we176* only test that y^q mod p = 1, which makes sure y is in g^x mod p177*/178if ((err = mp_exptmod(key->y, key->q, key->p, tmp)) != CRYPT_OK) {179goto error;180}181if (mp_cmp_d(tmp, 1) != LTC_MP_EQ) {182err = CRYPT_OK;183goto error;184}185}186187err = CRYPT_OK;188*stat = 1;189error:190mp_clear(tmp);191return err;192}193194#endif195196197