Check signature for challenge message and reorder the sig-checking clause after lighter checks

pull/55/head
Rongjian Lan 6 years ago
parent 7fe3daf0e3
commit c56930921b
  1. 48
      consensus/consensus_validator.go

@ -4,16 +4,14 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/gob" "encoding/gob"
"github.com/dedis/kyber"
"github.com/dedis/kyber/sign/schnorr"
"harmony-benchmark/attack" "harmony-benchmark/attack"
"harmony-benchmark/blockchain" "harmony-benchmark/blockchain"
"harmony-benchmark/crypto" "harmony-benchmark/crypto"
"harmony-benchmark/p2p" "harmony-benchmark/p2p"
proto_consensus "harmony-benchmark/proto/consensus" proto_consensus "harmony-benchmark/proto/consensus"
"regexp" "harmony-benchmark/utils"
"strconv"
"github.com/dedis/kyber"
"github.com/dedis/kyber/sign/schnorr"
) )
// Validator's consensus message dispatcher // Validator's consensus message dispatcher
@ -70,20 +68,17 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) {
copy(consensus.blockHash[:], blockHash[:]) copy(consensus.blockHash[:], blockHash[:])
// Verify signature // Verify block data
if schnorr.Verify(crypto.Ed25519Curve, consensus.leader.PubKey, payload[:offset-64], signature) != nil { // check leader Id
consensus.Log.Warn("Received message with invalid signature", "leaderKey", consensus.leader.PubKey, "consensus", consensus) myLeaderId := utils.GetUniqueIdFromPeer(consensus.leader)
if leaderId != myLeaderId {
consensus.Log.Warn("Received message from wrong leader", "myLeaderId", myLeaderId, "receivedLeaderId", leaderId, "consensus", consensus)
return return
} }
// Verify block data // Verify signature
// check leader Id if schnorr.Verify(crypto.Ed25519Curve, consensus.leader.PubKey, payload[:offset-64], signature) != nil {
leaderPrivKey := consensus.leader.Ip + consensus.leader.Port consensus.Log.Warn("Received message with invalid signature", "leaderKey", consensus.leader.PubKey, "consensus", consensus)
reg, _ := regexp.Compile("[^0-9]+")
socketId := reg.ReplaceAllString(leaderPrivKey, "")
value, _ := strconv.Atoi(socketId)
if leaderId != uint16(value) {
consensus.Log.Warn("Received message from wrong leader", "myLeaderId", uint16(value), "receivedLeaderId", leaderId, "consensus", consensus)
return return
} }
@ -196,26 +191,25 @@ func (consensus *Consensus) processChallengeMessage(payload []byte) {
//#### END: Read payload data //#### END: Read payload data
// TODO: make use of the data. This is just to avoid the unused variable warning // TODO: make use of the data. This is just to avoid the unused variable warning
_ = consensusId
_ = blockHash
_ = leaderId
_ = aggreCommit _ = aggreCommit
_ = aggreKey _ = aggreKey
_ = challenge _ = challenge
_ = signature _ = signature
// Verify block data and the aggregated signatures
// Update readyByConsensus for attack. // Update readyByConsensus for attack.
attack.GetInstance().UpdateConsensusReady(consensusId) attack.GetInstance().UpdateConsensusReady(consensusId)
// Verify block data and the aggregated signatures
// check leader Id // check leader Id
leaderPrivKey := consensus.leader.Ip + consensus.leader.Port myLeaderId := utils.GetUniqueIdFromPeer(consensus.leader)
reg, _ := regexp.Compile("[^0-9]+") if leaderId != myLeaderId {
socketId := reg.ReplaceAllString(leaderPrivKey, "") consensus.Log.Warn("Received message from wrong leader", "myLeaderId", myLeaderId, "receivedLeaderId", leaderId, "consensus", consensus)
value, _ := strconv.Atoi(socketId) return
if leaderId != uint16(value) { }
consensus.Log.Warn("Received message from wrong leader", "myLeaderId", consensus.consensusId, "receivedLeaderId", consensusId, "consensus", consensus)
// Verify signature
if schnorr.Verify(crypto.Ed25519Curve, consensus.leader.PubKey, payload[:offset-64], signature) != nil {
consensus.Log.Warn("Received message with invalid signature", "leaderKey", consensus.leader.PubKey, "consensus", consensus)
return return
} }

Loading…
Cancel
Save