/** Import DSA public or private key-part from raw numbers NB: The p, q & g parts must be set beforehand @param in The key-part to import, either public or private. @param inlen The key-part's length @param type Which type of key (PK_PRIVATE or PK_PUBLIC) @param key [out] the destination for the imported key @return CRYPT_OK if successful. */ int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key) { int err, stat = 0; LTC_ARGCHK(key != NULL); LTC_ARGCHK(key->x != NULL); LTC_ARGCHK(key->y != NULL); LTC_ARGCHK(key->p != NULL); LTC_ARGCHK(key->g != NULL); LTC_ARGCHK(key->q != NULL); LTC_ARGCHK(ltc_mp.name != NULL); if (type == PK_PRIVATE) { key->type = PK_PRIVATE; if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; } } else { key->type = PK_PUBLIC; if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } } if ((err = dsa_int_validate_xy(key, &stat)) != CRYPT_OK) { goto LBL_ERR; } if (stat == 0) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } return CRYPT_OK; LBL_ERR: dsa_free(key); return err; }
/** Validate a DSA key Yeah, this function should've been called dsa_validate_key() in the first place and for compat-reasons we keep it as it was (for now). @param key The key to validate @param stat [out] Result of test, 1==valid, 0==invalid @return CRYPT_OK if successful */ int dsa_verify_key(dsa_key *key, int *stat) { int err; err = dsa_int_validate_primes(key, stat); if (err != CRYPT_OK || *stat == 0) return err; err = dsa_int_validate_pqg(key, stat); if (err != CRYPT_OK || *stat == 0) return err; return dsa_int_validate_xy(key, stat); }