rlpx_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // Copyright 2015 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 p2p
  17. import (
  18. "bytes"
  19. "crypto/ecdsa"
  20. "crypto/rand"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "io/ioutil"
  25. "net"
  26. "reflect"
  27. "strings"
  28. "sync"
  29. "testing"
  30. "time"
  31. "github.com/davecgh/go-spew/spew"
  32. "github.com/ethereum/go-ethereum/crypto"
  33. "github.com/ethereum/go-ethereum/crypto/ecies"
  34. "github.com/ethereum/go-ethereum/p2p/simulations/pipes"
  35. "github.com/ethereum/go-ethereum/rlp"
  36. "golang.org/x/crypto/sha3"
  37. )
  38. func TestSharedSecret(t *testing.T) {
  39. prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
  40. pub0 := &prv0.PublicKey
  41. prv1, _ := crypto.GenerateKey()
  42. pub1 := &prv1.PublicKey
  43. ss0, err := ecies.ImportECDSA(prv0).GenerateShared(ecies.ImportECDSAPublic(pub1), sskLen, sskLen)
  44. if err != nil {
  45. return
  46. }
  47. ss1, err := ecies.ImportECDSA(prv1).GenerateShared(ecies.ImportECDSAPublic(pub0), sskLen, sskLen)
  48. if err != nil {
  49. return
  50. }
  51. t.Logf("Secret:\n%v %x\n%v %x", len(ss0), ss0, len(ss0), ss1)
  52. if !bytes.Equal(ss0, ss1) {
  53. t.Errorf("dont match :(")
  54. }
  55. }
  56. func TestEncHandshake(t *testing.T) {
  57. for i := 0; i < 10; i++ {
  58. start := time.Now()
  59. if err := testEncHandshake(nil); err != nil {
  60. t.Fatalf("i=%d %v", i, err)
  61. }
  62. t.Logf("(without token) %d %v\n", i+1, time.Since(start))
  63. }
  64. for i := 0; i < 10; i++ {
  65. tok := make([]byte, shaLen)
  66. rand.Reader.Read(tok)
  67. start := time.Now()
  68. if err := testEncHandshake(tok); err != nil {
  69. t.Fatalf("i=%d %v", i, err)
  70. }
  71. t.Logf("(with token) %d %v\n", i+1, time.Since(start))
  72. }
  73. }
  74. func testEncHandshake(token []byte) error {
  75. type result struct {
  76. side string
  77. pubkey *ecdsa.PublicKey
  78. err error
  79. }
  80. var (
  81. prv0, _ = crypto.GenerateKey()
  82. prv1, _ = crypto.GenerateKey()
  83. fd0, fd1 = net.Pipe()
  84. c0, c1 = newRLPX(fd0).(*rlpx), newRLPX(fd1).(*rlpx)
  85. output = make(chan result)
  86. )
  87. go func() {
  88. r := result{side: "initiator"}
  89. defer func() { output <- r }()
  90. defer fd0.Close()
  91. r.pubkey, r.err = c0.doEncHandshake(prv0, &prv1.PublicKey)
  92. if r.err != nil {
  93. return
  94. }
  95. if !reflect.DeepEqual(r.pubkey, &prv1.PublicKey) {
  96. r.err = fmt.Errorf("remote pubkey mismatch: got %v, want: %v", r.pubkey, &prv1.PublicKey)
  97. }
  98. }()
  99. go func() {
  100. r := result{side: "receiver"}
  101. defer func() { output <- r }()
  102. defer fd1.Close()
  103. r.pubkey, r.err = c1.doEncHandshake(prv1, nil)
  104. if r.err != nil {
  105. return
  106. }
  107. if !reflect.DeepEqual(r.pubkey, &prv0.PublicKey) {
  108. r.err = fmt.Errorf("remote ID mismatch: got %v, want: %v", r.pubkey, &prv0.PublicKey)
  109. }
  110. }()
  111. // wait for results from both sides
  112. r1, r2 := <-output, <-output
  113. if r1.err != nil {
  114. return fmt.Errorf("%s side error: %v", r1.side, r1.err)
  115. }
  116. if r2.err != nil {
  117. return fmt.Errorf("%s side error: %v", r2.side, r2.err)
  118. }
  119. // compare derived secrets
  120. if !reflect.DeepEqual(c0.rw.egressMAC, c1.rw.ingressMAC) {
  121. return fmt.Errorf("egress mac mismatch:\n c0.rw: %#v\n c1.rw: %#v", c0.rw.egressMAC, c1.rw.ingressMAC)
  122. }
  123. if !reflect.DeepEqual(c0.rw.ingressMAC, c1.rw.egressMAC) {
  124. return fmt.Errorf("ingress mac mismatch:\n c0.rw: %#v\n c1.rw: %#v", c0.rw.ingressMAC, c1.rw.egressMAC)
  125. }
  126. if !reflect.DeepEqual(c0.rw.enc, c1.rw.enc) {
  127. return fmt.Errorf("enc cipher mismatch:\n c0.rw: %#v\n c1.rw: %#v", c0.rw.enc, c1.rw.enc)
  128. }
  129. if !reflect.DeepEqual(c0.rw.dec, c1.rw.dec) {
  130. return fmt.Errorf("dec cipher mismatch:\n c0.rw: %#v\n c1.rw: %#v", c0.rw.dec, c1.rw.dec)
  131. }
  132. return nil
  133. }
  134. func TestProtocolHandshake(t *testing.T) {
  135. var (
  136. prv0, _ = crypto.GenerateKey()
  137. pub0 = crypto.FromECDSAPub(&prv0.PublicKey)[1:]
  138. hs0 = &protoHandshake{Version: 3, ID: pub0, Caps: []Cap{{"a", 0}, {"b", 2}}}
  139. prv1, _ = crypto.GenerateKey()
  140. pub1 = crypto.FromECDSAPub(&prv1.PublicKey)[1:]
  141. hs1 = &protoHandshake{Version: 3, ID: pub1, Caps: []Cap{{"c", 1}, {"d", 3}}}
  142. wg sync.WaitGroup
  143. )
  144. fd0, fd1, err := pipes.TCPPipe()
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. wg.Add(2)
  149. go func() {
  150. defer wg.Done()
  151. defer fd0.Close()
  152. rlpx := newRLPX(fd0)
  153. rpubkey, err := rlpx.doEncHandshake(prv0, &prv1.PublicKey)
  154. if err != nil {
  155. t.Errorf("dial side enc handshake failed: %v", err)
  156. return
  157. }
  158. if !reflect.DeepEqual(rpubkey, &prv1.PublicKey) {
  159. t.Errorf("dial side remote pubkey mismatch: got %v, want %v", rpubkey, &prv1.PublicKey)
  160. return
  161. }
  162. phs, err := rlpx.doProtoHandshake(hs0)
  163. if err != nil {
  164. t.Errorf("dial side proto handshake error: %v", err)
  165. return
  166. }
  167. phs.Rest = nil
  168. if !reflect.DeepEqual(phs, hs1) {
  169. t.Errorf("dial side proto handshake mismatch:\ngot: %s\nwant: %s\n", spew.Sdump(phs), spew.Sdump(hs1))
  170. return
  171. }
  172. rlpx.close(DiscQuitting)
  173. }()
  174. go func() {
  175. defer wg.Done()
  176. defer fd1.Close()
  177. rlpx := newRLPX(fd1)
  178. rpubkey, err := rlpx.doEncHandshake(prv1, nil)
  179. if err != nil {
  180. t.Errorf("listen side enc handshake failed: %v", err)
  181. return
  182. }
  183. if !reflect.DeepEqual(rpubkey, &prv0.PublicKey) {
  184. t.Errorf("listen side remote pubkey mismatch: got %v, want %v", rpubkey, &prv0.PublicKey)
  185. return
  186. }
  187. phs, err := rlpx.doProtoHandshake(hs1)
  188. if err != nil {
  189. t.Errorf("listen side proto handshake error: %v", err)
  190. return
  191. }
  192. phs.Rest = nil
  193. if !reflect.DeepEqual(phs, hs0) {
  194. t.Errorf("listen side proto handshake mismatch:\ngot: %s\nwant: %s\n", spew.Sdump(phs), spew.Sdump(hs0))
  195. return
  196. }
  197. if err := ExpectMsg(rlpx, discMsg, []DiscReason{DiscQuitting}); err != nil {
  198. t.Errorf("error receiving disconnect: %v", err)
  199. }
  200. }()
  201. wg.Wait()
  202. }
  203. func TestProtocolHandshakeErrors(t *testing.T) {
  204. tests := []struct {
  205. code uint64
  206. msg interface{}
  207. err error
  208. }{
  209. {
  210. code: discMsg,
  211. msg: []DiscReason{DiscQuitting},
  212. err: DiscQuitting,
  213. },
  214. {
  215. code: 0x989898,
  216. msg: []byte{1},
  217. err: errors.New("expected handshake, got 989898"),
  218. },
  219. {
  220. code: handshakeMsg,
  221. msg: make([]byte, baseProtocolMaxMsgSize+2),
  222. err: errors.New("message too big"),
  223. },
  224. {
  225. code: handshakeMsg,
  226. msg: []byte{1, 2, 3},
  227. err: newPeerError(errInvalidMsg, "(code 0) (size 4) rlp: expected input list for p2p.protoHandshake"),
  228. },
  229. {
  230. code: handshakeMsg,
  231. msg: &protoHandshake{Version: 3},
  232. err: DiscInvalidIdentity,
  233. },
  234. }
  235. for i, test := range tests {
  236. p1, p2 := MsgPipe()
  237. go Send(p1, test.code, test.msg)
  238. _, err := readProtocolHandshake(p2)
  239. if !reflect.DeepEqual(err, test.err) {
  240. t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
  241. }
  242. }
  243. }
  244. func TestRLPXFrameFake(t *testing.T) {
  245. buf := new(bytes.Buffer)
  246. hash := fakeHash([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
  247. rw := newRLPXFrameRW(buf, secrets{
  248. AES: crypto.Keccak256(),
  249. MAC: crypto.Keccak256(),
  250. IngressMAC: hash,
  251. EgressMAC: hash,
  252. })
  253. golden := unhex(`
  254. 00828ddae471818bb0bfa6b551d1cb42
  255. 01010101010101010101010101010101
  256. ba628a4ba590cb43f7848f41c4382885
  257. 01010101010101010101010101010101
  258. `)
  259. // Check WriteMsg. This puts a message into the buffer.
  260. if err := Send(rw, 8, []uint{1, 2, 3, 4}); err != nil {
  261. t.Fatalf("WriteMsg error: %v", err)
  262. }
  263. written := buf.Bytes()
  264. if !bytes.Equal(written, golden) {
  265. t.Fatalf("output mismatch:\n got: %x\n want: %x", written, golden)
  266. }
  267. // Check ReadMsg. It reads the message encoded by WriteMsg, which
  268. // is equivalent to the golden message above.
  269. msg, err := rw.ReadMsg()
  270. if err != nil {
  271. t.Fatalf("ReadMsg error: %v", err)
  272. }
  273. if msg.Size != 5 {
  274. t.Errorf("msg size mismatch: got %d, want %d", msg.Size, 5)
  275. }
  276. if msg.Code != 8 {
  277. t.Errorf("msg code mismatch: got %d, want %d", msg.Code, 8)
  278. }
  279. payload, _ := ioutil.ReadAll(msg.Payload)
  280. wantPayload := unhex("C401020304")
  281. if !bytes.Equal(payload, wantPayload) {
  282. t.Errorf("msg payload mismatch:\ngot %x\nwant %x", payload, wantPayload)
  283. }
  284. }
  285. type fakeHash []byte
  286. func (fakeHash) Write(p []byte) (int, error) { return len(p), nil }
  287. func (fakeHash) Reset() {}
  288. func (fakeHash) BlockSize() int { return 0 }
  289. func (h fakeHash) Size() int { return len(h) }
  290. func (h fakeHash) Sum(b []byte) []byte { return append(b, h...) }
  291. func TestRLPXFrameRW(t *testing.T) {
  292. var (
  293. aesSecret = make([]byte, 16)
  294. macSecret = make([]byte, 16)
  295. egressMACinit = make([]byte, 32)
  296. ingressMACinit = make([]byte, 32)
  297. )
  298. for _, s := range [][]byte{aesSecret, macSecret, egressMACinit, ingressMACinit} {
  299. rand.Read(s)
  300. }
  301. conn := new(bytes.Buffer)
  302. s1 := secrets{
  303. AES: aesSecret,
  304. MAC: macSecret,
  305. EgressMAC: sha3.NewLegacyKeccak256(),
  306. IngressMAC: sha3.NewLegacyKeccak256(),
  307. }
  308. s1.EgressMAC.Write(egressMACinit)
  309. s1.IngressMAC.Write(ingressMACinit)
  310. rw1 := newRLPXFrameRW(conn, s1)
  311. s2 := secrets{
  312. AES: aesSecret,
  313. MAC: macSecret,
  314. EgressMAC: sha3.NewLegacyKeccak256(),
  315. IngressMAC: sha3.NewLegacyKeccak256(),
  316. }
  317. s2.EgressMAC.Write(ingressMACinit)
  318. s2.IngressMAC.Write(egressMACinit)
  319. rw2 := newRLPXFrameRW(conn, s2)
  320. // send some messages
  321. for i := 0; i < 10; i++ {
  322. // write message into conn buffer
  323. wmsg := []interface{}{"foo", "bar", strings.Repeat("test", i)}
  324. err := Send(rw1, uint64(i), wmsg)
  325. if err != nil {
  326. t.Fatalf("WriteMsg error (i=%d): %v", i, err)
  327. }
  328. // read message that rw1 just wrote
  329. msg, err := rw2.ReadMsg()
  330. if err != nil {
  331. t.Fatalf("ReadMsg error (i=%d): %v", i, err)
  332. }
  333. if msg.Code != uint64(i) {
  334. t.Fatalf("msg code mismatch: got %d, want %d", msg.Code, i)
  335. }
  336. payload, _ := ioutil.ReadAll(msg.Payload)
  337. wantPayload, _ := rlp.EncodeToBytes(wmsg)
  338. if !bytes.Equal(payload, wantPayload) {
  339. t.Fatalf("msg payload mismatch:\ngot %x\nwant %x", payload, wantPayload)
  340. }
  341. }
  342. }
  343. type handshakeAuthTest struct {
  344. input string
  345. isPlain bool
  346. wantVersion uint
  347. wantRest []rlp.RawValue
  348. }
  349. var eip8HandshakeAuthTests = []handshakeAuthTest{
  350. // (Auth₁) RLPx v4 plain encoding
  351. {
  352. input: `
  353. 048ca79ad18e4b0659fab4853fe5bc58eb83992980f4c9cc147d2aa31532efd29a3d3dc6a3d89eaf
  354. 913150cfc777ce0ce4af2758bf4810235f6e6ceccfee1acc6b22c005e9e3a49d6448610a58e98744
  355. ba3ac0399e82692d67c1f58849050b3024e21a52c9d3b01d871ff5f210817912773e610443a9ef14
  356. 2e91cdba0bd77b5fdf0769b05671fc35f83d83e4d3b0b000c6b2a1b1bba89e0fc51bf4e460df3105
  357. c444f14be226458940d6061c296350937ffd5e3acaceeaaefd3c6f74be8e23e0f45163cc7ebd7622
  358. 0f0128410fd05250273156d548a414444ae2f7dea4dfca2d43c057adb701a715bf59f6fb66b2d1d2
  359. 0f2c703f851cbf5ac47396d9ca65b6260bd141ac4d53e2de585a73d1750780db4c9ee4cd4d225173
  360. a4592ee77e2bd94d0be3691f3b406f9bba9b591fc63facc016bfa8
  361. `,
  362. isPlain: true,
  363. wantVersion: 4,
  364. },
  365. // (Auth₂) EIP-8 encoding
  366. {
  367. input: `
  368. 01b304ab7578555167be8154d5cc456f567d5ba302662433674222360f08d5f1534499d3678b513b
  369. 0fca474f3a514b18e75683032eb63fccb16c156dc6eb2c0b1593f0d84ac74f6e475f1b8d56116b84
  370. 9634a8c458705bf83a626ea0384d4d7341aae591fae42ce6bd5c850bfe0b999a694a49bbbaf3ef6c
  371. da61110601d3b4c02ab6c30437257a6e0117792631a4b47c1d52fc0f8f89caadeb7d02770bf999cc
  372. 147d2df3b62e1ffb2c9d8c125a3984865356266bca11ce7d3a688663a51d82defaa8aad69da39ab6
  373. d5470e81ec5f2a7a47fb865ff7cca21516f9299a07b1bc63ba56c7a1a892112841ca44b6e0034dee
  374. 70c9adabc15d76a54f443593fafdc3b27af8059703f88928e199cb122362a4b35f62386da7caad09
  375. c001edaeb5f8a06d2b26fb6cb93c52a9fca51853b68193916982358fe1e5369e249875bb8d0d0ec3
  376. 6f917bc5e1eafd5896d46bd61ff23f1a863a8a8dcd54c7b109b771c8e61ec9c8908c733c0263440e
  377. 2aa067241aaa433f0bb053c7b31a838504b148f570c0ad62837129e547678c5190341e4f1693956c
  378. 3bf7678318e2d5b5340c9e488eefea198576344afbdf66db5f51204a6961a63ce072c8926c
  379. `,
  380. wantVersion: 4,
  381. wantRest: []rlp.RawValue{},
  382. },
  383. // (Auth₃) RLPx v4 EIP-8 encoding with version 56, additional list elements
  384. {
  385. input: `
  386. 01b8044c6c312173685d1edd268aa95e1d495474c6959bcdd10067ba4c9013df9e40ff45f5bfd6f7
  387. 2471f93a91b493f8e00abc4b80f682973de715d77ba3a005a242eb859f9a211d93a347fa64b597bf
  388. 280a6b88e26299cf263b01b8dfdb712278464fd1c25840b995e84d367d743f66c0e54a586725b7bb
  389. f12acca27170ae3283c1073adda4b6d79f27656993aefccf16e0d0409fe07db2dc398a1b7e8ee93b
  390. cd181485fd332f381d6a050fba4c7641a5112ac1b0b61168d20f01b479e19adf7fdbfa0905f63352
  391. bfc7e23cf3357657455119d879c78d3cf8c8c06375f3f7d4861aa02a122467e069acaf513025ff19
  392. 6641f6d2810ce493f51bee9c966b15c5043505350392b57645385a18c78f14669cc4d960446c1757
  393. 1b7c5d725021babbcd786957f3d17089c084907bda22c2b2675b4378b114c601d858802a55345a15
  394. 116bc61da4193996187ed70d16730e9ae6b3bb8787ebcaea1871d850997ddc08b4f4ea668fbf3740
  395. 7ac044b55be0908ecb94d4ed172ece66fd31bfdadf2b97a8bc690163ee11f5b575a4b44e36e2bfb2
  396. f0fce91676fd64c7773bac6a003f481fddd0bae0a1f31aa27504e2a533af4cef3b623f4791b2cca6
  397. d490
  398. `,
  399. wantVersion: 56,
  400. wantRest: []rlp.RawValue{{0x01}, {0x02}, {0xC2, 0x04, 0x05}},
  401. },
  402. }
  403. type handshakeAckTest struct {
  404. input string
  405. wantVersion uint
  406. wantRest []rlp.RawValue
  407. }
  408. var eip8HandshakeRespTests = []handshakeAckTest{
  409. // (Ack₁) RLPx v4 plain encoding
  410. {
  411. input: `
  412. 049f8abcfa9c0dc65b982e98af921bc0ba6e4243169348a236abe9df5f93aa69d99cadddaa387662
  413. b0ff2c08e9006d5a11a278b1b3331e5aaabf0a32f01281b6f4ede0e09a2d5f585b26513cb794d963
  414. 5a57563921c04a9090b4f14ee42be1a5461049af4ea7a7f49bf4c97a352d39c8d02ee4acc416388c
  415. 1c66cec761d2bc1c72da6ba143477f049c9d2dde846c252c111b904f630ac98e51609b3b1f58168d
  416. dca6505b7196532e5f85b259a20c45e1979491683fee108e9660edbf38f3add489ae73e3dda2c71b
  417. d1497113d5c755e942d1
  418. `,
  419. wantVersion: 4,
  420. },
  421. // (Ack₂) EIP-8 encoding
  422. {
  423. input: `
  424. 01ea0451958701280a56482929d3b0757da8f7fbe5286784beead59d95089c217c9b917788989470
  425. b0e330cc6e4fb383c0340ed85fab836ec9fb8a49672712aeabbdfd1e837c1ff4cace34311cd7f4de
  426. 05d59279e3524ab26ef753a0095637ac88f2b499b9914b5f64e143eae548a1066e14cd2f4bd7f814
  427. c4652f11b254f8a2d0191e2f5546fae6055694aed14d906df79ad3b407d94692694e259191cde171
  428. ad542fc588fa2b7333313d82a9f887332f1dfc36cea03f831cb9a23fea05b33deb999e85489e645f
  429. 6aab1872475d488d7bd6c7c120caf28dbfc5d6833888155ed69d34dbdc39c1f299be1057810f34fb
  430. e754d021bfca14dc989753d61c413d261934e1a9c67ee060a25eefb54e81a4d14baff922180c395d
  431. 3f998d70f46f6b58306f969627ae364497e73fc27f6d17ae45a413d322cb8814276be6ddd13b885b
  432. 201b943213656cde498fa0e9ddc8e0b8f8a53824fbd82254f3e2c17e8eaea009c38b4aa0a3f306e8
  433. 797db43c25d68e86f262e564086f59a2fc60511c42abfb3057c247a8a8fe4fb3ccbadde17514b7ac
  434. 8000cdb6a912778426260c47f38919a91f25f4b5ffb455d6aaaf150f7e5529c100ce62d6d92826a7
  435. 1778d809bdf60232ae21ce8a437eca8223f45ac37f6487452ce626f549b3b5fdee26afd2072e4bc7
  436. 5833c2464c805246155289f4
  437. `,
  438. wantVersion: 4,
  439. wantRest: []rlp.RawValue{},
  440. },
  441. // (Ack₃) EIP-8 encoding with version 57, additional list elements
  442. {
  443. input: `
  444. 01f004076e58aae772bb101ab1a8e64e01ee96e64857ce82b1113817c6cdd52c09d26f7b90981cd7
  445. ae835aeac72e1573b8a0225dd56d157a010846d888dac7464baf53f2ad4e3d584531fa203658fab0
  446. 3a06c9fd5e35737e417bc28c1cbf5e5dfc666de7090f69c3b29754725f84f75382891c561040ea1d
  447. dc0d8f381ed1b9d0d4ad2a0ec021421d847820d6fa0ba66eaf58175f1b235e851c7e2124069fbc20
  448. 2888ddb3ac4d56bcbd1b9b7eab59e78f2e2d400905050f4a92dec1c4bdf797b3fc9b2f8e84a482f3
  449. d800386186712dae00d5c386ec9387a5e9c9a1aca5a573ca91082c7d68421f388e79127a5177d4f8
  450. 590237364fd348c9611fa39f78dcdceee3f390f07991b7b47e1daa3ebcb6ccc9607811cb17ce51f1
  451. c8c2c5098dbdd28fca547b3f58c01a424ac05f869f49c6a34672ea2cbbc558428aa1fe48bbfd6115
  452. 8b1b735a65d99f21e70dbc020bfdface9f724a0d1fb5895db971cc81aa7608baa0920abb0a565c9c
  453. 436e2fd13323428296c86385f2384e408a31e104670df0791d93e743a3a5194ee6b076fb6323ca59
  454. 3011b7348c16cf58f66b9633906ba54a2ee803187344b394f75dd2e663a57b956cb830dd7a908d4f
  455. 39a2336a61ef9fda549180d4ccde21514d117b6c6fd07a9102b5efe710a32af4eeacae2cb3b1dec0
  456. 35b9593b48b9d3ca4c13d245d5f04169b0b1
  457. `,
  458. wantVersion: 57,
  459. wantRest: []rlp.RawValue{{0x06}, {0xC2, 0x07, 0x08}, {0x81, 0xFA}},
  460. },
  461. }
  462. func TestHandshakeForwardCompatibility(t *testing.T) {
  463. var (
  464. keyA, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
  465. keyB, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  466. pubA = crypto.FromECDSAPub(&keyA.PublicKey)[1:]
  467. pubB = crypto.FromECDSAPub(&keyB.PublicKey)[1:]
  468. ephA, _ = crypto.HexToECDSA("869d6ecf5211f1cc60418a13b9d870b22959d0c16f02bec714c960dd2298a32d")
  469. ephB, _ = crypto.HexToECDSA("e238eb8e04fee6511ab04c6dd3c89ce097b11f25d584863ac2b6d5b35b1847e4")
  470. ephPubA = crypto.FromECDSAPub(&ephA.PublicKey)[1:]
  471. ephPubB = crypto.FromECDSAPub(&ephB.PublicKey)[1:]
  472. nonceA = unhex("7e968bba13b6c50e2c4cd7f241cc0d64d1ac25c7f5952df231ac6a2bda8ee5d6")
  473. nonceB = unhex("559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd")
  474. _, _, _, _ = pubA, pubB, ephPubA, ephPubB
  475. authSignature = unhex("299ca6acfd35e3d72d8ba3d1e2b60b5561d5af5218eb5bc182045769eb4226910a301acae3b369fffc4a4899d6b02531e89fd4fe36a2cf0d93607ba470b50f7800")
  476. _ = authSignature
  477. )
  478. makeAuth := func(test handshakeAuthTest) *authMsgV4 {
  479. msg := &authMsgV4{Version: test.wantVersion, Rest: test.wantRest, gotPlain: test.isPlain}
  480. copy(msg.Signature[:], authSignature)
  481. copy(msg.InitiatorPubkey[:], pubA)
  482. copy(msg.Nonce[:], nonceA)
  483. return msg
  484. }
  485. makeAck := func(test handshakeAckTest) *authRespV4 {
  486. msg := &authRespV4{Version: test.wantVersion, Rest: test.wantRest}
  487. copy(msg.RandomPubkey[:], ephPubB)
  488. copy(msg.Nonce[:], nonceB)
  489. return msg
  490. }
  491. // check auth msg parsing
  492. for _, test := range eip8HandshakeAuthTests {
  493. r := bytes.NewReader(unhex(test.input))
  494. msg := new(authMsgV4)
  495. ciphertext, err := readHandshakeMsg(msg, encAuthMsgLen, keyB, r)
  496. if err != nil {
  497. t.Errorf("error for input %x:\n %v", unhex(test.input), err)
  498. continue
  499. }
  500. if !bytes.Equal(ciphertext, unhex(test.input)) {
  501. t.Errorf("wrong ciphertext for input %x:\n %x", unhex(test.input), ciphertext)
  502. }
  503. want := makeAuth(test)
  504. if !reflect.DeepEqual(msg, want) {
  505. t.Errorf("wrong msg for input %x:\ngot %s\nwant %s", unhex(test.input), spew.Sdump(msg), spew.Sdump(want))
  506. }
  507. }
  508. // check auth resp parsing
  509. for _, test := range eip8HandshakeRespTests {
  510. input := unhex(test.input)
  511. r := bytes.NewReader(input)
  512. msg := new(authRespV4)
  513. ciphertext, err := readHandshakeMsg(msg, encAuthRespLen, keyA, r)
  514. if err != nil {
  515. t.Errorf("error for input %x:\n %v", input, err)
  516. continue
  517. }
  518. if !bytes.Equal(ciphertext, input) {
  519. t.Errorf("wrong ciphertext for input %x:\n %x", input, err)
  520. }
  521. want := makeAck(test)
  522. if !reflect.DeepEqual(msg, want) {
  523. t.Errorf("wrong msg for input %x:\ngot %s\nwant %s", input, spew.Sdump(msg), spew.Sdump(want))
  524. }
  525. }
  526. // check derivation for (Auth₂, Ack₂) on recipient side
  527. var (
  528. hs = &encHandshake{
  529. initiator: false,
  530. respNonce: nonceB,
  531. randomPrivKey: ecies.ImportECDSA(ephB),
  532. }
  533. authCiphertext = unhex(eip8HandshakeAuthTests[1].input)
  534. authRespCiphertext = unhex(eip8HandshakeRespTests[1].input)
  535. authMsg = makeAuth(eip8HandshakeAuthTests[1])
  536. wantAES = unhex("80e8632c05fed6fc2a13b0f8d31a3cf645366239170ea067065aba8e28bac487")
  537. wantMAC = unhex("2ea74ec5dae199227dff1af715362700e989d889d7a493cb0639691efb8e5f98")
  538. wantFooIngressHash = unhex("0c7ec6340062cc46f5e9f1e3cf86f8c8c403c5a0964f5df0ebd34a75ddc86db5")
  539. )
  540. if err := hs.handleAuthMsg(authMsg, keyB); err != nil {
  541. t.Fatalf("handleAuthMsg: %v", err)
  542. }
  543. derived, err := hs.secrets(authCiphertext, authRespCiphertext)
  544. if err != nil {
  545. t.Fatalf("secrets: %v", err)
  546. }
  547. if !bytes.Equal(derived.AES, wantAES) {
  548. t.Errorf("aes-secret mismatch:\ngot %x\nwant %x", derived.AES, wantAES)
  549. }
  550. if !bytes.Equal(derived.MAC, wantMAC) {
  551. t.Errorf("mac-secret mismatch:\ngot %x\nwant %x", derived.MAC, wantMAC)
  552. }
  553. io.WriteString(derived.IngressMAC, "foo")
  554. fooIngressHash := derived.IngressMAC.Sum(nil)
  555. if !bytes.Equal(fooIngressHash, wantFooIngressHash) {
  556. t.Errorf("ingress-mac('foo') mismatch:\ngot %x\nwant %x", fooIngressHash, wantFooIngressHash)
  557. }
  558. }