public function atestDeterministicSign()
 {
     $f = file_get_contents(__DIR__ . '/../../data/rfc6979.json');
     $json = json_decode($f);
     $math = $this->math;
     $G = $this->G;
     foreach ($json->test as $test) {
         $G = CurveFactory::getGeneratorByName($test->curve);
         // Initialize private key and message hash (decimal)
         $privateKey = new PrivateKey($math, $G, $math->hexDec($test->privKey));
         $messageHash = $this->math->hexDec(hash($test->algorithm, $test->message));
         // Derive K
         $drbg = RandomGeneratorFactory::getHmacRandomGenerator($privateKey, $messageHash, $test->algorithm);
         // K must be correct (from privatekey and message hash)
         $k = $drbg->generate($G->getOrder());
         $this->assertEquals(strtolower($test->expectedK), $math->decHex($k));
         $hashBits = $this->math->baseConvert($messageHash, 10, 2);
         $size = NumberSize::bnNumBits($this->math, $messageHash);
         $messageHash = $this->math->baseConvert(substr($hashBits, 0, $size), 2, 10);
         $signer = new Signer($math);
         $sig = $signer->sign($privateKey, $messageHash, $k);
         // R and S should be correct
         $sR = $this->math->hexDec($test->expectedR);
         $sS = $this->math->hexDec($test->expectedS);
         $this->assertTrue($signer->verify($privateKey->getPublicKey(), $sig, $messageHash));
         $this->assertSame($sR, $sig->getR(), 'r');
         $this->assertSame($sS, $sig->getS(), 's');
     }
 }
Exemple #2
0
 /**
  * @param GeneratorPoint $G
  * @param $hash
  * @return int|string
  */
 public function truncateHash(GeneratorPoint $G, $hash)
 {
     $hexSize = strlen($this->adapter->decHex($hash));
     $hashBits = $this->adapter->baseConvert($hash, 10, 2);
     if (strlen($hashBits) < $hexSize * 4) {
         $hashBits = str_pad($hashBits, $hexSize * 4, '0', STR_PAD_LEFT);
     }
     $messageHash = $this->adapter->baseConvert(substr($hashBits, 0, NumberSize::bnNumBits($this->adapter, $G->getOrder())), 2, 10);
     return $messageHash;
 }
 /**
  * @dataProvider getHmacTestSet
  * @param GeneratorPoint $G
  * @param integer $size
  * @param string $privKey
  * @param string $algo
  * @param string $message
  * @param string $eK expected K hex
  * @param string $eR expected R hex
  * @param string $eS expected S hex
  */
 public function testHmacSignatures(GeneratorPoint $G, $size, $privKey, $algo, $message, $eK, $eR, $eS)
 {
     //echo "Try {$test->curve} / {$test->algorithm} / '{$test->message}'\n";
     $math = $G->getAdapter();
     // Initialize private key and message hash (decimal)
     $privateKey = $G->getPrivateKeyFrom($math->hexDec($privKey));
     $hashHex = hash($algo, $message);
     $messageHash = $math->hexDec($hashHex);
     // Derive K
     $drbg = RandomGeneratorFactory::getHmacRandomGenerator($privateKey, $messageHash, $algo);
     $k = $drbg->generate($G->getOrder());
     $this->assertEquals($k, $math->hexdec($eK), 'k');
     $hexSize = strlen($hashHex);
     $hashBits = $math->baseConvert($messageHash, 10, 2);
     if (strlen($hashBits) < $hexSize * 4) {
         $hashBits = str_pad($hashBits, $hexSize * 4, '0', STR_PAD_LEFT);
     }
     $messageHash = $math->baseConvert(substr($hashBits, 0, NumberSize::bnNumBits($math, $G->getOrder())), 2, 10);
     $signer = new Signer($math);
     $sig = $signer->sign($privateKey, $messageHash, $k);
     // Should be consistent
     $this->assertTrue($signer->verify($privateKey->getPublicKey(), $sig, $messageHash));
     // R and S should be correct
     $sR = $math->hexDec($eR);
     $sS = $math->hexDec($eS);
     $this->assertSame($sR, $sig->getR(), "r {$sR} == " . $sig->getR());
     $this->assertSame($sS, $sig->getS(), "s {$sR} == " . $sig->getS());
 }
 /**
  * @dataProvider getBnNumBitsNumbers
  */
 public function testNumBits(MathAdapterInterface $adapter, $number, $expected)
 {
     $size = NumberSize::bnNumBits($adapter, $adapter->hexDec($number));
     $this->assertEquals($expected, $size);
 }
 private function bitLength($number)
 {
     return NumberSize::bnNumBits($this->math, $number);
 }