github.com/jimmyx0x/go-ethereum@v1.10.28/p2p/discover/v5wire/crypto_test.go (about) 1 // Copyright 2020 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package v5wire 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "crypto/elliptic" 23 "crypto/sha256" 24 "reflect" 25 "strings" 26 "testing" 27 28 "github.com/ethereum/go-ethereum/common/hexutil" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/p2p/enode" 31 ) 32 33 func TestVector_ECDH(t *testing.T) { 34 var ( 35 staticKey = hexPrivkey("0xfb757dc581730490a1d7a00deea65e9b1936924caaea8f44d476014856b68736") 36 publicKey = hexPubkey(crypto.S256(), "0x039961e4c2356d61bedb83052c115d311acb3a96f5777296dcf297351130266231") 37 want = hexutil.MustDecode("0x033b11a2a1f214567e1537ce5e509ffd9b21373247f2a3ff6841f4976f53165e7e") 38 ) 39 result := ecdh(staticKey, publicKey) 40 check(t, "shared-secret", result, want) 41 } 42 43 func TestVector_KDF(t *testing.T) { 44 var ( 45 ephKey = hexPrivkey("0xfb757dc581730490a1d7a00deea65e9b1936924caaea8f44d476014856b68736") 46 cdata = hexutil.MustDecode("0x000000000000000000000000000000006469736376350001010102030405060708090a0b0c00180102030405060708090a0b0c0d0e0f100000000000000000") 47 net = newHandshakeTest() 48 ) 49 defer net.close() 50 51 destKey := &testKeyB.PublicKey 52 s := deriveKeys(sha256.New, ephKey, destKey, net.nodeA.id(), net.nodeB.id(), cdata) 53 t.Logf("ephemeral-key = %#x", ephKey.D) 54 t.Logf("dest-pubkey = %#x", EncodePubkey(destKey)) 55 t.Logf("node-id-a = %#x", net.nodeA.id().Bytes()) 56 t.Logf("node-id-b = %#x", net.nodeB.id().Bytes()) 57 t.Logf("challenge-data = %#x", cdata) 58 check(t, "initiator-key", s.writeKey, hexutil.MustDecode("0xdccc82d81bd610f4f76d3ebe97a40571")) 59 check(t, "recipient-key", s.readKey, hexutil.MustDecode("0xac74bb8773749920b0d3a8881c173ec5")) 60 } 61 62 func TestVector_IDSignature(t *testing.T) { 63 var ( 64 key = hexPrivkey("0xfb757dc581730490a1d7a00deea65e9b1936924caaea8f44d476014856b68736") 65 destID = enode.HexID("0xbbbb9d047f0488c0b5a93c1c3f2d8bafc7c8ff337024a55434a0d0555de64db9") 66 ephkey = hexutil.MustDecode("0x039961e4c2356d61bedb83052c115d311acb3a96f5777296dcf297351130266231") 67 cdata = hexutil.MustDecode("0x000000000000000000000000000000006469736376350001010102030405060708090a0b0c00180102030405060708090a0b0c0d0e0f100000000000000000") 68 ) 69 70 sig, err := makeIDSignature(sha256.New(), key, cdata, ephkey, destID) 71 if err != nil { 72 t.Fatal(err) 73 } 74 t.Logf("static-key = %#x", key.D) 75 t.Logf("challenge-data = %#x", cdata) 76 t.Logf("ephemeral-pubkey = %#x", ephkey) 77 t.Logf("node-id-B = %#x", destID.Bytes()) 78 expected := "0x94852a1e2318c4e5e9d422c98eaf19d1d90d876b29cd06ca7cb7546d0fff7b484fe86c09a064fe72bdbef73ba8e9c34df0cd2b53e9d65528c2c7f336d5dfc6e6" 79 check(t, "id-signature", sig, hexutil.MustDecode(expected)) 80 } 81 82 func TestDeriveKeys(t *testing.T) { 83 t.Parallel() 84 85 var ( 86 n1 = enode.ID{1} 87 n2 = enode.ID{2} 88 cdata = []byte{1, 2, 3, 4} 89 ) 90 sec1 := deriveKeys(sha256.New, testKeyA, &testKeyB.PublicKey, n1, n2, cdata) 91 sec2 := deriveKeys(sha256.New, testKeyB, &testKeyA.PublicKey, n1, n2, cdata) 92 if sec1 == nil || sec2 == nil { 93 t.Fatal("key agreement failed") 94 } 95 if !reflect.DeepEqual(sec1, sec2) { 96 t.Fatalf("keys not equal:\n %+v\n %+v", sec1, sec2) 97 } 98 } 99 100 func check(t *testing.T, what string, x, y []byte) { 101 t.Helper() 102 103 if !bytes.Equal(x, y) { 104 t.Errorf("wrong %s: %#x != %#x", what, x, y) 105 } else { 106 t.Logf("%s = %#x", what, x) 107 } 108 } 109 110 func hexPrivkey(input string) *ecdsa.PrivateKey { 111 key, err := crypto.HexToECDSA(strings.TrimPrefix(input, "0x")) 112 if err != nil { 113 panic(err) 114 } 115 return key 116 } 117 118 func hexPubkey(curve elliptic.Curve, input string) *ecdsa.PublicKey { 119 key, err := DecodePubkey(curve, hexutil.MustDecode(input)) 120 if err != nil { 121 panic(err) 122 } 123 return key 124 }