-
Notifications
You must be signed in to change notification settings - Fork 75
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "$@" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 aBigInt
, which is why I had to change serialization here. If this change made a difference in your case, does it mean you had aBigInt
which had a different length? Do you have examples? ShouldByteUtils.bigIntToBytes
also do padding? Could you add unit tests?