crypto_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. package v5wire
  17. import (
  18. "bytes"
  19. "crypto/ecdsa"
  20. "crypto/elliptic"
  21. "crypto/sha256"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common/hexutil"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/p2p/enode"
  28. )
  29. func TestVector_ECDH(t *testing.T) {
  30. var (
  31. staticKey = hexPrivkey("0xfb757dc581730490a1d7a00deea65e9b1936924caaea8f44d476014856b68736")
  32. publicKey = hexPubkey(crypto.S256(), "0x039961e4c2356d61bedb83052c115d311acb3a96f5777296dcf297351130266231")
  33. want = hexutil.MustDecode("0x033b11a2a1f214567e1537ce5e509ffd9b21373247f2a3ff6841f4976f53165e7e")
  34. )
  35. result := ecdh(staticKey, publicKey)
  36. check(t, "shared-secret", result, want)
  37. }
  38. func TestVector_KDF(t *testing.T) {
  39. var (
  40. ephKey = hexPrivkey("0xfb757dc581730490a1d7a00deea65e9b1936924caaea8f44d476014856b68736")
  41. cdata = hexutil.MustDecode("0x000000000000000000000000000000006469736376350001010102030405060708090a0b0c00180102030405060708090a0b0c0d0e0f100000000000000000")
  42. net = newHandshakeTest()
  43. )
  44. defer net.close()
  45. destKey := &testKeyB.PublicKey
  46. s := deriveKeys(sha256.New, ephKey, destKey, net.nodeA.id(), net.nodeB.id(), cdata)
  47. t.Logf("ephemeral-key = %#x", ephKey.D)
  48. t.Logf("dest-pubkey = %#x", EncodePubkey(destKey))
  49. t.Logf("node-id-a = %#x", net.nodeA.id().Bytes())
  50. t.Logf("node-id-b = %#x", net.nodeB.id().Bytes())
  51. t.Logf("challenge-data = %#x", cdata)
  52. check(t, "initiator-key", s.writeKey, hexutil.MustDecode("0xdccc82d81bd610f4f76d3ebe97a40571"))
  53. check(t, "recipient-key", s.readKey, hexutil.MustDecode("0xac74bb8773749920b0d3a8881c173ec5"))
  54. }
  55. func TestVector_IDSignature(t *testing.T) {
  56. var (
  57. key = hexPrivkey("0xfb757dc581730490a1d7a00deea65e9b1936924caaea8f44d476014856b68736")
  58. destID = enode.HexID("0xbbbb9d047f0488c0b5a93c1c3f2d8bafc7c8ff337024a55434a0d0555de64db9")
  59. ephkey = hexutil.MustDecode("0x039961e4c2356d61bedb83052c115d311acb3a96f5777296dcf297351130266231")
  60. cdata = hexutil.MustDecode("0x000000000000000000000000000000006469736376350001010102030405060708090a0b0c00180102030405060708090a0b0c0d0e0f100000000000000000")
  61. )
  62. sig, err := makeIDSignature(sha256.New(), key, cdata, ephkey, destID)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. t.Logf("static-key = %#x", key.D)
  67. t.Logf("challenge-data = %#x", cdata)
  68. t.Logf("ephemeral-pubkey = %#x", ephkey)
  69. t.Logf("node-id-B = %#x", destID.Bytes())
  70. expected := "0x94852a1e2318c4e5e9d422c98eaf19d1d90d876b29cd06ca7cb7546d0fff7b484fe86c09a064fe72bdbef73ba8e9c34df0cd2b53e9d65528c2c7f336d5dfc6e6"
  71. check(t, "id-signature", sig, hexutil.MustDecode(expected))
  72. }
  73. func TestDeriveKeys(t *testing.T) {
  74. t.Parallel()
  75. var (
  76. n1 = enode.ID{1}
  77. n2 = enode.ID{2}
  78. cdata = []byte{1, 2, 3, 4}
  79. )
  80. sec1 := deriveKeys(sha256.New, testKeyA, &testKeyB.PublicKey, n1, n2, cdata)
  81. sec2 := deriveKeys(sha256.New, testKeyB, &testKeyA.PublicKey, n1, n2, cdata)
  82. if sec1 == nil || sec2 == nil {
  83. t.Fatal("key agreement failed")
  84. }
  85. if !reflect.DeepEqual(sec1, sec2) {
  86. t.Fatalf("keys not equal:\n %+v\n %+v", sec1, sec2)
  87. }
  88. }
  89. func check(t *testing.T, what string, x, y []byte) {
  90. t.Helper()
  91. if !bytes.Equal(x, y) {
  92. t.Errorf("wrong %s: %#x != %#x", what, x, y)
  93. } else {
  94. t.Logf("%s = %#x", what, x)
  95. }
  96. }
  97. func hexPrivkey(input string) *ecdsa.PrivateKey {
  98. key, err := crypto.HexToECDSA(strings.TrimPrefix(input, "0x"))
  99. if err != nil {
  100. panic(err)
  101. }
  102. return key
  103. }
  104. func hexPubkey(curve elliptic.Curve, input string) *ecdsa.PublicKey {
  105. key, err := DecodePubkey(curve, hexutil.MustDecode(input))
  106. if err != nil {
  107. panic(err)
  108. }
  109. return key
  110. }