From dbba3227c114856ac581abee00b5c64986e73c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=9Al=C4=85ski?= Date: Mon, 30 Nov 2020 10:20:15 +0100 Subject: [PATCH] Added signature validator tool --- src/main/scala/io/iohk/ethereum/App.scala | 6 ++- .../ethereum/crypto/SignatureValidator.scala | 48 +++++++++++++++++++ src/universal/bin/signatureValidator | 5 ++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala create mode 100755 src/universal/bin/signatureValidator diff --git a/src/main/scala/io/iohk/ethereum/App.scala b/src/main/scala/io/iohk/ethereum/App.scala index 6cd85a0288..3afe8b4419 100644 --- a/src/main/scala/io/iohk/ethereum/App.scala +++ b/src/main/scala/io/iohk/ethereum/App.scala @@ -1,7 +1,7 @@ package io.iohk.ethereum import io.iohk.ethereum.cli.CliLauncher -import io.iohk.ethereum.crypto.EcKeyGen +import io.iohk.ethereum.crypto.{EcKeyGen, SignatureValidator} import io.iohk.ethereum.extvm.VmServerApp import io.iohk.ethereum.faucet.Faucet import io.iohk.ethereum.mallet.main.Mallet @@ -19,6 +19,7 @@ object App extends Logger { val faucet = "faucet" val ecKeyGen = "eckeygen" val cli = "cli" + val sigValidator = "signature-validator" args.headOption match { case None => Mantis.main(args) @@ -33,12 +34,13 @@ object App extends Logger { case Some(`mallet`) => Mallet.main(args.tail) case Some(`faucet`) => Faucet.main(args.tail) case Some(`ecKeyGen`) => EcKeyGen.main(args.tail) + case Some(`sigValidator`) => SignatureValidator.main(args.tail) case Some(`cli`) => CliLauncher.main(args.tail) case Some(unknown) => log.error( s"Unrecognised launcher option, " + s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, " + - s"$mallet, $faucet, $vmServer, $ecKeyGen or $cli" + s"$mallet, $faucet, $vmServer, $ecKeyGen, $sigValidator or $cli" ) } diff --git a/src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala b/src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala new file mode 100644 index 0000000000..073aa69f49 --- /dev/null +++ b/src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala @@ -0,0 +1,48 @@ +package io.iohk.ethereum.crypto + +import akka.util.ByteString +import io.iohk.ethereum.crypto +import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits +import io.iohk.ethereum.nodebuilder.SecureRandomBuilder +import io.iohk.ethereum.utils.ByteStringUtils + +import scala.util.{Failure, Success, Try} + +object SignatureValidator extends App with SecureRandomBuilder with JsonMethodsImplicits { + + args match { + case Array(pk, sig, msgHash) => + Try { + val signature = ECDSASignature.fromBytes(ByteString(decode(sig))) + val msg = ByteStringUtils.string2hash(msgHash) + + signature.flatMap(_.publicKey(msg)) + } match { + case Failure(exception) => + println(s"Can't recover public key from signature [$sig] and msg [$msgHash]: ERROR: ${exception.getMessage}") + sys.exit(1) + case Success(recoveredPk) => + val publicKey = ByteStringUtils.string2hash(pk) + recoveredPk match { + case Some(pk) => + if(pk == publicKey) { + println(s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is the same as given one") + } else { + println(s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is different than given [${ByteStringUtils.hash2string(publicKey)}]") + sys.exit(1) + } + case None => + println(s"Can't recover public key from signature [$sig] and msg [$msgHash]") + sys.exit(1) + } + } + case _ => + val keyPair = crypto.generateKeyPair(secureRandom) + val pkStr = ByteStringUtils.hash2string(ByteString(crypto.pubKeyFromKeyPair(keyPair))) + val hash = kec256(Array(1.toByte)) + val hashStr = ByteStringUtils.hash2string(ByteString(hash)) + val signature = ECDSASignature.sign(hash, keyPair) + val sigStr = ByteStringUtils.hash2string(signature.toBytes) + println(s"Bad Input. Example usage: [signature-validator publicKey signature message_hash]. Example: [signature-validator $pkStr $sigStr $hashStr]") + } +} diff --git a/src/universal/bin/signatureValidator b/src/universal/bin/signatureValidator new file mode 100755 index 0000000000..f09bc762e2 --- /dev/null +++ b/src/universal/bin/signatureValidator @@ -0,0 +1,5 @@ +#!/bin/bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $DIR/.. +./bin/mantis -- signature-validator "$@"