/** * @param PrivateKeyInterface $key * @param $hash * @param $randomK * @return Signature */ public function sign(PrivateKeyInterface $key, $hash, $randomK) { $math = $this->adapter; $generator = $key->getPoint(); $modMath = $math->getModularArithmetic($generator->getOrder()); $k = $math->mod($randomK, $generator->getOrder()); $p1 = $generator->mul($k); $r = $p1->getX(); if ($math->cmp($r, 0) == 0) { throw new \RuntimeException("Error: random number R = 0"); } $hash = $this->truncateHash($generator, $hash); $s = $modMath->div($modMath->add($hash, $math->mul($key->getSecret(), $r)), $k); if ($math->cmp($s, 0) == 0) { throw new \RuntimeException("Error: random number S = 0"); } return new Signature($r, $s); }
/** * Construct a HMAC deterministic byte generator. * * @param MathAdapterInterface $math * @param PrivateKeyInterface $privateKey * @param string $messageHash * @param $algo * @internal param string $personalString */ public function __construct(MathAdapterInterface $math, PrivateKeyInterface $privateKey, $messageHash, $algo) { if (!in_array($algo, hash_algos())) { throw new \RuntimeException('HMACDRGB: Hashing algorithm not found'); } $tempHash = hash($algo, 1, true); $vlen = NumberSize::getCeiledByteSize($math, $math->hexdec(bin2hex($tempHash), 1)); // Initialize deterministic vectors $this->V = str_pad('', $vlen, chr(0x1), STR_PAD_LEFT); $this->K = str_pad('', $vlen, chr(0x0), STR_PAD_LEFT); $this->math = $math; $this->algorithm = $algo; $this->generator = $privateKey->getPoint(); // Encode the private key and hash as binary, a seed for the DRBG $entropy = pack("H*", $this->int2octets($privateKey->getSecret()) . $this->int2octets($messageHash)); $this->update($entropy); return $this; }
/** * Pkcs8PrivateKey constructor. * @param GmpMathInterface $math * @param DigestParamsInterface $kdfParams * @param CipherParamsInterface $cipherParams * @param PrivateKeyInterface $privateKey */ public function __construct(GmpMathInterface $math, DigestParamsInterface $kdfParams, CipherParamsInterface $cipherParams, PrivateKeyInterface $privateKey) { $this->kdfParams = $kdfParams; $this->cipherParams = $cipherParams; parent::__construct($math, $privateKey->getPoint(), $privateKey->getSecret()); }
/** * {@inheritDoc} * @see \Mdanter\Ecc\Crypto\EcDH\EcDHInterface::createMultiPartyKey() */ public function createMultiPartyKey() { $this->calculateKey(); return new PublicKey($this->adapter, $this->senderKey->getPoint(), $this->secretKey); }