public function format(PublicKeyInterface $key) { if (!$key->getCurve() instanceof NamedCurveFp) { throw new \RuntimeException('Not implemented for unnamed curves'); } $sequence = new Sequence(new Sequence(new ObjectIdentifier(DerPublicKeySerializer::X509_ECDSA_OID), CurveOidMapper::getCurveOid($key->getCurve())), new BitString($this->encodePoint($key->getPoint()))); return $sequence->getBinary(); }
/** * @param string $curveName * @param PublicKeyInterface $publicKey * @return string */ public function serialize($curveName, PublicKeyInterface $publicKey) { $ecdsa = 'ecdsa-sha2-' . $curveName; $key = hex2bin($this->pointSerializer->serialize($publicKey->getPoint())); $serialized = pack("N", strlen($ecdsa)) . $ecdsa; $serialized .= pack("N", strlen($curveName)) . $curveName; $serialized .= pack("N", strlen($key)) . $key; return base64_encode($serialized); }
/** * */ private function calculateKey() { $this->checkExchangeState(); if ($this->secretKey === null) { $this->secretKey = $this->recipientKey->getPoint()->mul($this->senderKey->getSecret()); } }
/** * @param PublicKeyInterface $key * @param SignatureInterface $signature * @param $hash * @return bool */ public function verify(PublicKeyInterface $key, SignatureInterface $signature, $hash) { $generator = $key->getGenerator(); $n = $generator->getOrder(); $point = $key->getPoint(); $r = $signature->getR(); $s = $signature->getS(); $math = $this->adapter; if ($math->cmp($r, 1) < 1 || $math->cmp($r, $math->sub($n, 1)) > 0) { return false; } if ($math->cmp($s, 1) < 1 || $math->cmp($s, $math->sub($n, 1)) > 0) { return false; } $modMath = $math->getModularArithmetic($n); $c = $math->inverseMod($s, $n); $u1 = $modMath->mul($hash, $c); $u2 = $modMath->mul($r, $c); $xy = $generator->mul($u1)->add($point->mul($u2)); $v = $math->mod($xy->getX(), $n); return $math->cmp($v, $r) == 0; }
/** * @param OutputInterface $output * @param PublicKeyInterface $key */ public static function dumpPublicKey(OutputInterface $output, PublicKeyInterface $key) { $order = $key->getPoint()->getOrder(); $output->writeln('<comment>Public key information</comment>'); $output->writeln(''); $output->writeln('<info>Curve type</info> : ' . $key->getCurve()->getName()); $output->writeln('<info>X</info> : ' . $key->getPoint()->getX()); $output->writeln('<info>Y</info> : ' . $key->getPoint()->getY()); $output->writeln('<info>Order</info> : ' . (empty($order) ? '<null>' : $key->getPoint()->getOrder())); }
public function getUncompressedKey(PublicKeyInterface $key) { return $this->formatter->encodePoint($key->getPoint()); }