github.com/core-coin/go-core/v2@v2.1.9/p2p/discover/v5wire/crypto_test.go (about)

     1  // Copyright 2020 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package v5wire
    18  
    19  import (
    20  	"bytes"
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  
    25  	"golang.org/x/crypto/sha3"
    26  
    27  	"github.com/core-coin/go-core/v2/common/hexutil"
    28  	"github.com/core-coin/go-core/v2/crypto"
    29  	"github.com/core-coin/go-core/v2/p2p/enode"
    30  )
    31  
    32  func TestVector_ECDH(t *testing.T) {
    33  	var (
    34  		staticKey = hexPrivkey("0x1033b1bac4c731e800b6399a357e51cf1b20eec942aac608c90b89553003e2ed3f94bd80613ee9006b1e62b6bb45109d0db9a4833e78363991")
    35  		publicKey = hexPubkey("0x1033b1bac4c731e800b6399a357e51cf1b20eec942aac608c90b89553003e2ed3f94bd80613ee9006b1e62b6bb45109d0db9a4833e78363992")
    36  		want      = hexutil.MustDecode("0x1f361690a5508a7d725bce70d87e284cf1f7cad2b0a1bc1518dac3668c401d4a32944f26f18136203125ceb06266c3c53f766b80beea2659")
    37  	)
    38  	result := ecdh(staticKey, publicKey)
    39  	check(t, "shared-secret", result, want)
    40  }
    41  
    42  func TestVector_KDF(t *testing.T) {
    43  	var (
    44  		ephKey = hexPrivkey("0x1033b1bac4c731e800b6399a357e51cf1b20eec942aac608c90b89553003e2ed3f94bd80613ee9006b1e62b6bb45109d0db9a4833e78363991")
    45  		cdata  = hexutil.MustDecode("0x000000000000000000000000000000006469736376350001010102030405060708090a0b0c00180102030405060708090a0b0c0d0e0f100000000000000000")
    46  		net    = newHandshakeTest()
    47  	)
    48  	defer net.close()
    49  
    50  	s := deriveKeys(sha3.New256, ephKey, testKeyB.PublicKey(), net.nodeA.id(), net.nodeB.id(), cdata)
    51  	t.Logf("ephemeral-key = %#x", ephKey.PrivateKey())
    52  	t.Logf("dest-pubkey = %#x", EncodePubkey(testKeyB.PublicKey()))
    53  	t.Logf("node-id-a = %#x", net.nodeA.id().Bytes())
    54  	t.Logf("node-id-b = %#x", net.nodeB.id().Bytes())
    55  	t.Logf("challenge-data = %#x", cdata)
    56  	check(t, "initiator-key", s.writeKey, hexutil.MustDecode("0x32f7236576f4f16d00850a0bc1849d65"))
    57  	check(t, "recipient-key", s.readKey, hexutil.MustDecode("0xcb0f6a26538541a796211d6b074237d2"))
    58  }
    59  
    60  func TestVector_IDSignature(t *testing.T) {
    61  	var (
    62  		key    = hexPrivkey("0x1033b1bac4c731e800b6399a357e51cf1b20eec942aac608c90b89553003e2ed3f94bd80613ee9006b1e62b6bb45109d0db9a4833e78363991")
    63  		destID = enode.HexID("0xbbbb9d047f0488c0b5a93c1c3f2d8bafc7c8ff337024a55434a0d0555de64db9")
    64  		ephkey = hexutil.MustDecode("0x1033b1bac4c731e800b6399a357e51cf1b20eec942aac608c90b89553003e2ed3f94bd80613ee9006b1e62b6bb45109d0db9a4833e78363992")
    65  		cdata  = hexutil.MustDecode("0x000000000000000000000000000000006469736376350001010102030405060708090a0b0c00180102030405060708090a0b0c0d0e0f100000000000000000")
    66  	)
    67  
    68  	sig, err := makeIDSignature(sha3.New256(), key, cdata, ephkey, destID)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	t.Logf("static-key = %#x", key.PrivateKey())
    73  	t.Logf("challenge-data = %#x", cdata)
    74  	t.Logf("ephemeral-pubkey = %#x", ephkey)
    75  	t.Logf("node-id-B = %#x", destID.Bytes())
    76  	expected := "0x602c459e5ec3401426132f09c525bcf31404b6bb7ff2b0c079bc261a18ab0ce19c54784baed55c5d9b12a0a43c41be3aca422588c546e53d80b4140b4eb4fec32ab6c5e0f0e310f6ad1a3ed8097f98f39958b4699231b209e6d936854a37d5f63cdded03a9a9a75508d6d985a881fe3f32002f65ab658f3b0bc9fbdea48703b9c5c0dc2151c5ae8c4b77b1e5cdaee9fa20748e01960ab51ddb118d1209f73d186f0444921ad72c7c757480"
    77  	check(t, "id-signature", sig, hexutil.MustDecode(expected))
    78  }
    79  
    80  func TestDeriveKeys(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	var (
    84  		n1    = enode.ID{1}
    85  		n2    = enode.ID{2}
    86  		cdata = []byte{1, 2, 3, 4}
    87  	)
    88  	sec1 := deriveKeys(sha3.New256, testKeyA, testKeyB.PublicKey(), n1, n2, cdata)
    89  	sec2 := deriveKeys(sha3.New256, testKeyB, testKeyA.PublicKey(), n1, n2, cdata)
    90  	if sec1 == nil || sec2 == nil {
    91  		t.Fatal("key agreement failed")
    92  	}
    93  	if !reflect.DeepEqual(sec1, sec2) {
    94  		t.Fatalf("keys not equal:\n  %+v\n  %+v", sec1, sec2)
    95  	}
    96  }
    97  
    98  func check(t *testing.T, what string, x, y []byte) {
    99  	t.Helper()
   100  
   101  	if !bytes.Equal(x, y) {
   102  		t.Errorf("wrong %s: %#x != %#x", what, x, y)
   103  	} else {
   104  		t.Logf("%s = %#x", what, x)
   105  	}
   106  }
   107  
   108  func hexPrivkey(input string) *crypto.PrivateKey {
   109  	key, err := crypto.UnmarshalPrivateKeyHex(strings.TrimPrefix(input, "0x"))
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  	return key
   114  }
   115  
   116  func hexPubkey(input string) *crypto.PublicKey {
   117  	key, err := DecodePubkey(hexutil.MustDecode(input))
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  	return key
   122  }