diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2023-06-20 01:51:29 +0200 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2023-06-20 01:51:29 +0200 |
commit | 530b0fe326d8a9623e27e081b51512cdf1d5b5d7 (patch) | |
tree | dfe2b901bf10f9acd416c22f61b475b379e52b44 /prog/ž/test.php | |
parent | download changes to website update (diff) | |
download | r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.tar r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.tar.gz r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.tar.bz2 r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.tar.lz r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.tar.xz r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.tar.zst r-530b0fe326d8a9623e27e081b51512cdf1d5b5d7.zip |
Diffstat (limited to 'prog/ž/test.php')
-rwxr-xr-x | prog/ž/test.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/prog/ž/test.php b/prog/ž/test.php new file mode 100755 index 0000000..dc3ab50 --- /dev/null +++ b/prog/ž/test.php @@ -0,0 +1,72 @@ +#!/usr/bin/php +<?php + +require "vendor/autoload.php"; + +use Mdanter\Ecc\Crypto\Signature\SignHasher; +use Mdanter\Ecc\Crypto\Key\PublicKey; +use Mdanter\Ecc\Primitives\Point; +use Mdanter\Ecc\EccFactory; +use Mdanter\Ecc\Crypto\Signature\Signer; +use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer; +use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer; +use Mdanter\Ecc\Math; +use Mdanter\Ecc\Math\GmpMath; + +// ECDSA domain is defined by curve/generator/hash algorithm, +// which a verifier must be aware of. + +$adapter = EccFactory::getAdapter(); +$generator = EccFactory::getNistCurves()->generator384(); +$useDerandomizedSignatures = true; +$algorithm = 'sha384'; +$derSerializer = new DerPrivateKeySerializer($adapter); + +## generate der key +$private = $generator->createPrivateKey(); +echo "privkey: " . $private->getSecret() . PHP_EOL; +$der = $derSerializer->serialize($private); +$math = new GmpMath(); +// echo bin2hex($math->intToString($private->getSecret())) . PHP_EOL; +// echo bin2hex($der) . PHP_EOL; + +## You'll be restoring from a key, as opposed to generating one. +$key = $derSerializer->parse($der); + +$document = 'I am writing today...'; + +$hasher = new SignHasher($algorithm, $adapter); +$hash = $hasher->makeHash($document, $generator); + +echo "message: $document" . PHP_EOL; +echo "hash: $hash" . PHP_EOL; + +# Derandomized signatures are not necessary, but is avoids +# the risk of a low entropy RNG, causing accidental reuse +# of a k value for a different message, which leaks the +# private key. +if ($useDerandomizedSignatures) { + $random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getHmacRandomGenerator($key, $hash, $algorithm); +} else { + $random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getRandomGenerator(); +} +$randomK = $random->generate($generator->getOrder()); + +$signer = new Signer($adapter); +$signature = $signer->sign($key, $hash, $randomK); + +# $serializer = new DerSignatureSerializer(); +# $serializedSig = $serializer->serialize($signature); +# echo base64_encode($serializedSig) . PHP_EOL; + +echo "signature: r=" . $signature->getR() . " s=" . $signature->getS() . PHP_EOL; + +$pubkey = $key->getPublicKey(); +$x = $pubkey->getPoint()->getX(); +$y = $pubkey->getPoint()->getY(); + +echo "public key: x=" . $x . " y=" . $y . PHP_EOL; + +$publickey = new PublicKey($adapter, $generator, new Point($adapter, EccFactory::getNistCurves()->curve384(), $x, $y)); + +echo "signature check " . ($signer->verify($publickey, $signature, $hash) ? "passed" : "failed") . PHP_EOL; |