Skip to content

Commit e2b6d07

Browse files
authored
Merge pull request #3 from PlatONnetwork/juice-develop-fix-sm-handshake
fix: p2p handshake by sm2
2 parents b39de46 + cd9240e commit e2b6d07

File tree

10 files changed

+107
-7
lines changed

10 files changed

+107
-7
lines changed

crypto/ecies/params.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"hash"
4444

4545
ethcrypto "github.com/PlatONnetwork/PlatON-Go/crypto"
46+
"github.com/PlatONnetwork/PlatON-Go/crypto/sm"
4647
)
4748

4849
var (
@@ -72,6 +73,14 @@ type ECIESParams struct {
7273
// * ECIES using AES256 and HMAC-SHA-512-64
7374

7475
var (
76+
ECIES_SM2_SM3 = &ECIESParams{
77+
Hash: sm.NewSM3,
78+
hashAlgo: crypto.SHA256,
79+
Cipher: aes.NewCipher,
80+
BlockSize: aes.BlockSize,
81+
KeyLen: 16,
82+
}
83+
7584
ECIES_AES128_SHA256 = &ECIESParams{
7685
Hash: sha256.New,
7786
hashAlgo: crypto.SHA256,
@@ -114,6 +123,7 @@ var (
114123
)
115124

116125
var paramsFromCurve = map[elliptic.Curve]*ECIESParams{
126+
sm.S256(): ECIES_SM2_SM3,
117127
ethcrypto.S256(): ECIES_AES128_SHA256,
118128
elliptic.P256(): ECIES_AES128_SHA256,
119129
elliptic.P384(): ECIES_AES192_SHA384,

crypto/sm/curve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
9999
return x3.Cmp(y2) == 0
100100
}
101101

102-
//TODO: double check if the function is okay
102+
// TODO: double check if the function is okay
103103
// affineFromJacobian reverses the Jacobian transform. See the comment at the
104104
// top of the file.
105105
func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {

crypto/vrf/vrf_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package vrf
1818

1919
import (
2020
"crypto/ecdsa"
21+
"crypto/elliptic"
2122
"crypto/rand"
2223
"io"
2324
"testing"
2425
)
2526

2627
func TestVrf(t *testing.T) {
28+
curve := elliptic.P256()
2729
for i := 0; i < 10; i++ {
2830
sk, err := ecdsa.GenerateKey(curve, rand.Reader)
2931
if nil != err {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ require (
6868
)
6969

7070
require (
71+
github.com/cespare/xxhash v1.1.0
7172
github.com/golang/mock v1.6.0
7273
github.com/herumi/bls-eth-go-binary v1.32.1
7374
github.com/supranational/blst v0.3.11

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
12
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
23
github.com/PlatONnetwork/wagon v0.6.1-0.20201026015350-67507c2a7b96 h1:BA5xEQQrv82VdaxUoAZeGi/G/UQ3z6z+eNe2rijhhpg=
34
github.com/PlatONnetwork/wagon v0.6.1-0.20201026015350-67507c2a7b96/go.mod h1:zPWloKR2Ep7uqrhyLyE483NCxlAlQnbPsQUJXWN6bVM=
@@ -251,6 +252,7 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
251252
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
252253
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
253254
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
255+
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
254256
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
255257
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg=
256258
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=

miner/worker_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ package miner
1818

1919
import (
2020
"errors"
21-
"github.com/PlatONnetwork/PlatON-Go/sdk"
22-
"github.com/PlatONnetwork/PlatON-Go/sdk/simapp"
2321
"math/big"
2422
"math/rand"
2523
"testing"
2624
"time"
2725

26+
"github.com/PlatONnetwork/PlatON-Go/sdk"
27+
"github.com/PlatONnetwork/PlatON-Go/sdk/simapp"
28+
2829
"github.com/PlatONnetwork/PlatON-Go/core/rawdb"
2930

3031
"github.com/PlatONnetwork/PlatON-Go/core/cbfttypes"
@@ -547,6 +548,11 @@ func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine co
547548
type EmptyBackend struct {
548549
}
549550

551+
func (e EmptyBackend) ChainConfig() *params.ChainConfig {
552+
//TODO implement me
553+
panic("implement me")
554+
}
555+
550556
func (e EmptyBackend) ChainId() (*big.Int, error) {
551557
//TODO implement me
552558
panic("implement me")

p2p/discover/v5wire/crypto.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"golang.org/x/crypto/hkdf"
2929

30+
"github.com/PlatONnetwork/PlatON-Go/common"
3031
"github.com/PlatONnetwork/PlatON-Go/common/math"
3132
"github.com/PlatONnetwork/PlatON-Go/crypto"
3233
"github.com/PlatONnetwork/PlatON-Go/p2p/enode"
@@ -92,7 +93,12 @@ func makeIDSignature(hash hash.Hash, key *ecdsa.PrivateKey, challenge, ephkey []
9293
// s256raw is an unparsed secp256k1 public key ENR entry.
9394
type s256raw []byte
9495

95-
func (s256raw) ENRKey() string { return "secp256k1" }
96+
func (s256raw) ENRKey() string {
97+
if common.SignAlgo == common.SMSignAlgo {
98+
return "sm2"
99+
}
100+
return "secp256k1"
101+
}
96102

97103
// verifyIDSignature checks that signature over idnonce was made by the given node.
98104
func verifyIDSignature(hash hash.Hash, sig []byte, n *enode.Node, challenge, ephkey []byte, destID enode.ID) error {

p2p/enode/idscheme.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323

24+
"github.com/PlatONnetwork/PlatON-Go/common"
2425
"github.com/PlatONnetwork/PlatON-Go/common/math"
2526
"github.com/PlatONnetwork/PlatON-Go/crypto"
2627
"github.com/PlatONnetwork/PlatON-Go/p2p/enr"
@@ -100,7 +101,12 @@ func (V4ID) NodeAddr(r *enr.Record) []byte {
100101
// Secp256k1 is the "secp256k1" key, which holds a public key.
101102
type Secp256k1 ecdsa.PublicKey
102103

103-
func (v Secp256k1) ENRKey() string { return "secp256k1" }
104+
func (v Secp256k1) ENRKey() string {
105+
if common.GetSignAlgo() == common.SMSignAlgo {
106+
return "sm2"
107+
}
108+
return "secp256k1"
109+
}
104110

105111
// EncodeRLP implements rlp.Encoder.
106112
func (v Secp256k1) EncodeRLP(w io.Writer) error {
@@ -124,7 +130,12 @@ func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error {
124130
// s256raw is an unparsed secp256k1 public key entry.
125131
type s256raw []byte
126132

127-
func (s256raw) ENRKey() string { return "secp256k1" }
133+
func (s256raw) ENRKey() string {
134+
if common.GetSignAlgo() == common.SMSignAlgo {
135+
return "sm2"
136+
}
137+
return "secp256k1"
138+
}
128139

129140
// v4CompatID is a weaker and insecure version of the "v4" scheme which only checks for the
130141
// presence of a secp256k1 public key, but doesn't verify the signature.

p2p/enode/idscheme_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
2828

29+
"github.com/PlatONnetwork/PlatON-Go/common"
2930
"github.com/PlatONnetwork/PlatON-Go/crypto"
3031
"github.com/PlatONnetwork/PlatON-Go/p2p/enr"
3132
"github.com/PlatONnetwork/PlatON-Go/rlp"
@@ -34,9 +35,23 @@ import (
3435
var (
3536
privkey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
3637
pubkey = &privkey.PublicKey
38+
//smPrivkey, _ = crypto.HexToECDSA("b517045a9e4cf82daf5d6c21d9d32d12e11b1c850b2d0448670d5aee8a3bcbcc")
39+
smPrivkey = func() func() *ecdsa.PrivateKey {
40+
return func() *ecdsa.PrivateKey {
41+
p, _ := crypto.HexToECDSA("b517045a9e4cf82daf5d6c21d9d32d12e11b1c850b2d0448670d5aee8a3bcbcc")
42+
return p
43+
}
44+
}()
45+
//smPubkey = &smPrivkey().PublicKey
46+
smPubkey = func() func() *ecdsa.PublicKey {
47+
return func() *ecdsa.PublicKey {
48+
return &smPrivkey().PublicKey
49+
}
50+
}()
3751
)
3852

3953
func TestEmptyNodeID(t *testing.T) {
54+
common.SetSignAlgo(common.SMSignAlgo)
4055
var r enr.Record
4156
if addr := ValidSchemes.NodeAddr(&r); addr != nil {
4257
t.Errorf("wrong address on empty record: got %v, want %v", addr, nil)
@@ -73,3 +88,45 @@ func TestGetSetSecp256k1(t *testing.T) {
7388
require.NoError(t, r.Load(&pk))
7489
assert.EqualValues(t, pubkey, &pk)
7590
}
91+
92+
// for sm test
93+
func TestEmptyNodeIDForSm(t *testing.T) {
94+
common.SetSignAlgo(common.SMSignAlgo)
95+
var r enr.Record
96+
if addr := ValidSchemes.NodeAddr(&r); addr != nil {
97+
t.Errorf("wrong address on empty record: got %v, want %v", addr, nil)
98+
}
99+
100+
require.NoError(t, SignV4(&r, smPrivkey()))
101+
expected := "a1e1c33bebe596bda273fd5eb1b1b932a8640ac8524bbd3d9f0863b106b8667d"
102+
assert.Equal(t, expected, hex.EncodeToString(ValidSchemes.NodeAddr(&r)))
103+
}
104+
105+
// Checks that failure to sign leaves the record unmodified.
106+
func TestSignErrorForSm(t *testing.T) {
107+
common.SetSignAlgo(common.SMSignAlgo)
108+
invalidKey := &ecdsa.PrivateKey{D: new(big.Int), PublicKey: *(smPubkey())}
109+
110+
var r enr.Record
111+
emptyEnc, _ := rlp.EncodeToBytes(&r)
112+
if err := SignV4(&r, invalidKey); err == nil {
113+
t.Fatal("expected error from SignV4")
114+
}
115+
newEnc, _ := rlp.EncodeToBytes(&r)
116+
if !bytes.Equal(newEnc, emptyEnc) {
117+
t.Fatal("record modified even though signing failed")
118+
}
119+
}
120+
121+
// TestGetSetSecp256k1 tests encoding/decoding and setting/getting of the Secp256k1 key.
122+
func TestGetSetSecp256k1ForSm(t *testing.T) {
123+
common.SetSignAlgo(common.SMSignAlgo)
124+
var r enr.Record
125+
if err := SignV4(&r, smPrivkey()); err != nil {
126+
t.Fatal(err)
127+
}
128+
129+
var pk Secp256k1
130+
require.NoError(t, r.Load(&pk))
131+
assert.EqualValues(t, smPubkey(), &pk)
132+
}

sdk/simapp/app.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ type SimApp struct {
3838
validators *cbfttypes.Validators
3939
}
4040

41-
func (app *SimApp) TxPoolParam(ctx sdk.Context, param sdk.TxPoolParam) uint64 {
41+
func (app *SimApp) SystemParam(ctx sdk.SystemParamContext, param sdk.SystemParam) uint64 {
42+
//TODO implement me
43+
panic("implement me")
44+
}
45+
46+
func (app *SimApp) TxPoolParam(ctx sdk.Context, param sdk.TxPoolApp) uint64 {
4247
//TODO implement me
4348
return 0
4449
}

0 commit comments

Comments
 (0)