/**
  * @param CurveFpInterface $curve
  * @param string           $data
  * @return PointInterface
  */
 public function unserialize(CurveFpInterface $curve, $data)
 {
     if (substr($data, 0, 2) != '04') {
         throw new \InvalidArgumentException('Invalid data: only uncompressed keys are supported.');
     }
     $data = substr($data, 2);
     $dataLength = strlen($data);
     $x = $this->adapter->hexDec(substr($data, 0, $dataLength / 2));
     $y = $this->adapter->hexDec(substr($data, $dataLength / 2));
     return $curve->getPoint($x, $y);
 }
Example #2
0
 /**
  * @dataProvider getB24Params
  * @testdox Test 192-bit generated curve points against ECDSAVS section B2.4 samples
  */
 public function testP192CurveEcdsavsB24SignatureValidityTest(MathAdapterInterface $math, $msg, $Qx, $Qy, $R, $S, $expected)
 {
     $msg = $math->hexDec($msg);
     $Qx = $math->hexDec($Qx);
     $Qy = $math->hexDec($Qy);
     $R = $math->hexDec($R);
     $S = $math->hexDec($S);
     $generator = EccFactory::getNistCurves($math)->generator192();
     $publicKey = $generator->getPublicKeyFrom($Qx, $Qy);
     $signer = new Signer($math);
     $actual = $signer->verify($publicKey, new Signature($R, $S), $math->digestInteger($msg));
     $this->assertEquals($expected, $actual);
 }
Example #3
0
 /**
  * @dataProvider getAdapters
  */
 public function testStrictIntegerReturnValues(MathAdapterInterface $math)
 {
     $x = 10;
     $y = 4;
     $mod = $math->mod($x, $y);
     $this->assertTrue(is_string($mod) && !is_resource($mod));
     $add = $math->add($x, $y);
     $this->assertTrue(is_string($add) && !is_resource($add));
     $sub = $math->sub($add, $y);
     $this->assertTrue(is_string($sub) && !is_resource($sub));
     $mul = $math->mul($x, $y);
     $this->assertTrue(is_string($mul) && !is_resource($mul));
     $div = $math->div($mul, $y);
     $this->assertTrue(is_string($div) && !is_resource($div));
     $pow = $math->pow($x, $y);
     $this->assertTrue(is_string($pow) && !is_resource($div));
     $powmod = $math->powmod($x, $y, $y);
     $this->assertTrue(is_string($powmod) && !is_resource($powmod));
     $bitwiseand = $math->bitwiseAnd($x, $y);
     $this->assertTrue(is_string($bitwiseand) && !is_resource($bitwiseand));
     $hexdec = $math->decHex($x);
     $this->assertTrue(is_string($hexdec) && !is_resource($hexdec));
     $dechex = $math->hexDec($hexdec);
     $this->assertTrue(is_string($dechex) && !is_resource($dechex));
 }
 /**
  * @param string $data
  * {@inheritDoc}
  * @see \Mdanter\Ecc\Serializer\PrivateKeySerializerInterface::parse()
  * @throws \FG\ASN1\Exception\ParserException
  */
 public function parse($data)
 {
     $asnObject = Object::fromBinary($data);
     if (!$asnObject instanceof Sequence || $asnObject->getNumberofChildren() !== 4) {
         throw new \RuntimeException('Invalid data.');
     }
     $children = $asnObject->getChildren();
     $version = $children[0];
     if ($version->getContent() != 1) {
         throw new \RuntimeException('Invalid data: only version 1 (RFC5915) keys are supported.');
     }
     $key = $this->adapter->hexDec($children[1]->getContent());
     $oid = $children[2]->getContent();
     $generator = CurveOidMapper::getGeneratorFromOid($oid);
     return $generator->getPrivateKeyFrom($key);
 }
 /**
  * @dataProvider getDeterministicSign2Data
  */
 public function testDeterministicSign2($curve, $size, $algo, $privKey, $message, $eK, $eR, $eS)
 {
     //echo "Try {$test->curve} / {$test->algorithm} / '{$test->message}'\n";
     $G = CurveFactory::getGeneratorByName($curve);
     // Initialize private key and message hash (decimal)
     $privateKey = $G->getPrivateKeyFrom($this->math->hexDec($privKey));
     $hashHex = hash($algo, $message);
     $messageHash = $this->math->hexDec($hashHex);
     // Derive K
     $drbg = RandomGeneratorFactory::getHmacRandomGenerator($privateKey, $messageHash, $algo);
     $k = $drbg->generate($G->getOrder());
     $this->assertEquals($this->math->hexdec($eK), $k, 'k');
     $hexSize = strlen($hashHex);
     $hashBits = $this->math->baseConvert($messageHash, 10, 2);
     if (strlen($hashBits) < $hexSize * 4) {
         $hashBits = str_pad($hashBits, $hexSize * 4, '0', STR_PAD_LEFT);
     }
     $messageHash = $this->math->baseConvert(substr($hashBits, 0, $size), 2, 10);
     $signer = new Signer($this->math);
     $sig = $signer->sign($privateKey, $messageHash, $k);
     // R and S should be correct
     $sR = $this->math->hexDec($eR);
     $sS = $this->math->hexDec($eS);
     $this->assertTrue($signer->verify($privateKey->getPublicKey(), $sig, $messageHash));
     $this->assertSame($sR, $sig->getR(), 'r');
     $this->assertSame($sS, $sig->getS(), 's');
 }
Example #6
0
 /**
  * Extracts R and S values from given data
  *
  * @param string $value
  *
  * @return \Mdanter\Ecc\Crypto\Signature\Signature
  */
 private function extractSignature(string $value) : \Mdanter\Ecc\Crypto\Signature\Signature
 {
     $length = $this->getSignatureLength();
     $value = unpack('H*', $value)[1];
     return new Signature($this->adapter->hexDec(substr($value, 0, $length)), $this->adapter->hexDec(substr($value, $length)));
 }
Example #7
0
 /**
  * Extracts R and S values from given data
  *
  * @param string $value
  *
  * @return \Mdanter\Ecc\Crypto\Signature\Signature
  */
 private function extractSignature($value)
 {
     $length = $this->getSignatureLength();
     $value = unpack('H*', $value)[1];
     return new Signature($this->adapter->hexDec(substr($value, 0, $length)), $this->adapter->hexDec(substr($value, $length)));
 }
Example #8
0
 /**
  * @param bool $hex
  * @return int|string
  */
 public function getHash($hex = false)
 {
     $hash = hash($this->algo, $this->content, false);
     return $hex ? $hash : $this->adapter->hexDec($hash);
 }
Example #9
0
 /**
  * @return int|string
  */
 public function getInt()
 {
     return $this->math->hexDec($this->getHex());
 }
Example #10
0
 /**
  * @param $value
  *
  * @return int|string
  */
 private function convertHexToDec($value)
 {
     return $this->adapter->hexDec($value);
 }
Example #11
0
 /**
  * @dataProvider getAdapters
  */
 public function testModFunction(MathAdapterInterface $math)
 {
     // $o->compressed, $o->decompressed public key.
     // Check that we can compress a key properly (tests $math->mod())
     foreach ($this->compression_data as $o) {
         $prefix = substr($o->decompressed, 0, 2);
         // will be 04.
         $this->assertEquals('04', $prefix);
         // hex encoded (X,Y) coordinate of ECDSA public key.
         $x = substr($o->decompressed, 2, 64);
         $y = substr($o->decompressed, 66, 64);
         // y % 2 == 0       - true: y is even(02) / false: y is odd(03)
         $mod = $math->mod($math->hexDec($y), 2);
         $compressed = '0' . ($mod == 0 ? '2' : '3') . $x;
         // Check that the mod function reported the parity for the y value.
         $this->assertTrue($compressed === $o->compressed);
     }
 }
Example #12
0
 /**
  * Hash the data, returning a decimal.
  *
  * @param $string - a binary string to hash
  * @return int|string
  */
 public function hashDec($string)
 {
     $hex = $this->hash($string, false);
     return $this->math->hexDec($hex);
 }
 /**
  * @param int|string $max
  * @return int|string
  */
 public function generate($max)
 {
     $bytes = NumberSize::getFlooredByteSize($this->adapter, $max);
     $iv = mcrypt_create_iv($bytes, \MCRYPT_DEV_URANDOM);
     return $this->adapter->hexDec(bin2hex($iv));
 }