Skip to content

Add signature validation tool + fix checkpointing padding. #824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/scala/io/iohk/ethereum/App.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ case class ECDSASignature(r: BigInt, s: BigInt, v: Byte) {
import ECDSASignature.RLength

def bigInt2Bytes(b: BigInt) =
ByteString(ByteUtils.bigIntToBytes(b, RLength))
ByteUtils.padLeft(ByteString(b.toByteArray).takeRight(RLength), RLength, 0)
Copy link
Contributor

@aakoshh aakoshh Dec 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the looks of it ByteUtils.bigIntToBytes should have been doing the same thing, except it was lacking padding on the left. Unfortunately it doesn't have any docstrings or tests, so it's difficult to tell what it's supposed to be doing. I know that some private keys generated by Secp256k1 are 33 bytes and some are 32 when converted to a BigInt, which is why I had to change serialization here. If this change made a difference in your case, does it mean you had a BigInt which had a different length? Do you have examples? Should ByteUtils.bigIntToBytes also do padding? Could you add unit tests?


bigInt2Bytes(r) ++ bigInt2Bytes(s) :+ v
}
Expand Down
57 changes: 57 additions & 0 deletions src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.iohk.ethereum.crypto

import akka.util.ByteString
import io.iohk.ethereum.crypto
import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits
import io.iohk.ethereum.security.SecureRandomBuilder
import io.iohk.ethereum.utils.ByteStringUtils

import scala.util.{Failure, Success, Try}

// scalastyle:off regex
object SignatureValidator extends App with SecureRandomBuilder with JsonMethodsImplicits {

args match {
case Array(pk, sig, msgHash) =>
Try {
val signature = ECDSASignature.fromBytes(ByteStringUtils.string2hash(sig))
val msg = ByteStringUtils.string2hash(msgHash)

signature.flatMap(_.publicKey(msg))
} match {
case Failure(exception) =>
System.err.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) {
System.err.println(
s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is the same as given one"
)
} else {
System.err.println(s"Recovered public key [${ByteStringUtils
.hash2string(pk)}] is different than given [${ByteStringUtils.hash2string(publicKey)}]")
sys.exit(1)
}
case None =>
System.err.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)
System.err.println(
s"Bad Input. Example usage: [signature-validator publicKey signature message_hash]. Example: [signature-validator $pkStr $sigStr $hashStr]"
)
sys.exit(1)
}
}
5 changes: 5 additions & 0 deletions src/universal/bin/signatureValidator
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR/..
./bin/mantis -- signature-validator "$@"