/** * @see AbstractKeyInstance::deserializeFromObject($obj) */ protected function deserializeFromObject($obj) { $n = new MathBigInteger($obj["n"]); $e = new MathBigInteger($obj["e"]); $array = array("n" => $n, "e" => $e); $this->rsa->loadKey($array, CRYPT_RSA_PUBLIC_FORMAT_RAW); $this->rsa->setPublicKey($array, CRYPT_RSA_PUBLIC_FORMAT_RAW); $this->keysize = RSAKeyPair::_getKeySizeFromRSAKeySize(strlen($n->toBits())); $this->rsa->setHash(RSAKeyPair::$KEYSIZES[$this->keysize]["hashAlg"]); return $this; }
/** * @see AbstractKeyInstance::serializeToObject($obj) */ protected function serializeToObject(&$obj) { $obj["p"] = $this->key_p->toHex(); $obj["q"] = $this->key_q->toHex(); $obj["g"] = $this->key_g->toHex(); $obj["x"] = $this->key_x->toHex(); }
/** * RSAVP1 * * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.2 RFC3447#section-5.2.2}. * * @access private * @param MathBigInteger $s * @return MathBigInteger */ function _rsavp1($s) { if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) { user_error('Signature representative out of range', E_USER_NOTICE); return false; } return $this->_exponentiate($s); }
/** * @see AbstractPublicKey::verify($message, $signature) */ public function verify($message, $signature) { $params = DSAKeyPair::$KEYSIZES[$this->keysize]; $hash_alg = $params["hashAlg"]; $hexlength = $params["q_bitlength"] / 4; // we pre-pad with 0s because encoding may have gotten rid of some $signature = Utils::hex_lpad(bin2hex($signature), $hexlength * 2); // now this should only happen if the signature was longer if (strlen($signature) != $hexlength * 2) { throw new \Exception("problem with r/s combo: " . sizeof($signature) . "/" . $hexlength . " - " . $signature); } $r = new MathBigInteger(substr($signature, 0, $hexlength), 16); $s = new MathBigInteger(substr($signature, $hexlength, $hexlength), 16); // check rangeconstraints if ($r->compare(DSAKeyPair::$zero) < 0 || $r->compare($this->key_q) > 0) { throw new \Exception("problem with r: " . $r->toString()); } if ($s->compare(DSAKeyPair::$zero) < 0 || $s->compare($this->key_q) > 0) { throw new \Exception("problem with s: " . $r->toString()); } return CryptDSA::verify($message, $hash_alg, $r, $s, $this->key_p, $this->key_q, $this->key_g, $this->key_y); }
/** * DSA verify * * @param string $message message * @param string $hash_alg hash algorithm * @param MathBigInteger $r r * @param MathBigInteger $s s * @param MathBigInteger $p p * @param MathBigInteger $q q * @param MathBigInteger $g g * @param MathBigInteger $y public key * @return bool */ public static function verify($message, $hash_alg, $r, $s, $p, $q, $g, $y) { $hash = new CryptHash($hash_alg); $hash_m = new MathBigInteger($hash->hash($message), 256); $w = $s->modInverse($q); $hash_m_mul = $hash_m->multiply($w); $u1_base = $hash_m_mul->divide($q); $u1 = $u1_base[1]; $r_mul = $r->multiply($w); $u2_base = $r_mul->divide($q); $u2 = $u2_base[1]; $g_pow = $g->modPow($u1, $p); $y_pow = $y->modPow($u2, $p); $g_pow_mul = $g_pow->multiply($y_pow); $g_pow_mul_mod_base = $g_pow_mul->divide($p); $g_pow_mul_mod = $g_pow_mul_mod_base[1]; $v_base = $g_pow_mul_mod->divide($q); $v = $v_base[1]; return $v->compare($r) == 0; }