func readHandshakeMsg(msg plainDecoder, plainSize int, prv *ecdsa.PrivateKey, r io.Reader) ([]byte, error) { buf := make([]byte, plainSize) if _, err := io.ReadFull(r, buf); err != nil { return buf, err } // Attempt decoding pre-EIP-8 "plain" format. key := ecies.ImportECDSA(prv) if dec, err := key.Decrypt(rand.Reader, buf, nil, nil); err == nil { msg.decodePlain(dec) return buf, nil } // Could be EIP-8 format, try that. prefix := buf[:2] size := binary.BigEndian.Uint16(prefix) if size < uint16(plainSize) { return buf, fmt.Errorf("size underflow, need at least %d bytes", plainSize) } buf = append(buf, make([]byte, size-uint16(plainSize)+2)...) if _, err := io.ReadFull(r, buf[plainSize:]); err != nil { return buf, err } dec, err := key.Decrypt(rand.Reader, buf[2:], nil, prefix) if err != nil { return buf, err } // Can't use rlp.DecodeBytes here because it rejects // trailing data (forward-compatibility). s := rlp.NewStream(bytes.NewReader(dec), 0) return buf, s.Decode(msg) }
func TestSharedSecret(t *testing.T) { prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader) pub0 := &prv0.PublicKey prv1, _ := crypto.GenerateKey() pub1 := &prv1.PublicKey ss0, err := ecies.ImportECDSA(prv0).GenerateShared(ecies.ImportECDSAPublic(pub1), sskLen, sskLen) if err != nil { return } ss1, err := ecies.ImportECDSA(prv1).GenerateShared(ecies.ImportECDSAPublic(pub0), sskLen, sskLen) if err != nil { return } t.Logf("Secret:\n%v %x\n%v %x", len(ss0), ss0, len(ss0), ss1) if !bytes.Equal(ss0, ss1) { t.Errorf("dont match :(") } }
func (h *encHandshake) ecdhShared(prv *ecdsa.PrivateKey) ([]byte, error) { return ecies.ImportECDSA(prv).GenerateShared(h.remotePub, sskLen, sskLen) }
func TestHandshakeForwardCompatibility(t *testing.T) { var ( keyA, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") keyB, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") pubA = crypto.FromECDSAPub(&keyA.PublicKey)[1:] pubB = crypto.FromECDSAPub(&keyB.PublicKey)[1:] ephA, _ = crypto.HexToECDSA("869d6ecf5211f1cc60418a13b9d870b22959d0c16f02bec714c960dd2298a32d") ephB, _ = crypto.HexToECDSA("e238eb8e04fee6511ab04c6dd3c89ce097b11f25d584863ac2b6d5b35b1847e4") ephPubA = crypto.FromECDSAPub(&ephA.PublicKey)[1:] ephPubB = crypto.FromECDSAPub(&ephB.PublicKey)[1:] nonceA = unhex("7e968bba13b6c50e2c4cd7f241cc0d64d1ac25c7f5952df231ac6a2bda8ee5d6") nonceB = unhex("559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd") _, _, _, _ = pubA, pubB, ephPubA, ephPubB authSignature = unhex("299ca6acfd35e3d72d8ba3d1e2b60b5561d5af5218eb5bc182045769eb4226910a301acae3b369fffc4a4899d6b02531e89fd4fe36a2cf0d93607ba470b50f7800") _ = authSignature ) makeAuth := func(test handshakeAuthTest) *authMsgV4 { msg := &authMsgV4{Version: test.wantVersion, Rest: test.wantRest, gotPlain: test.isPlain} copy(msg.Signature[:], authSignature) copy(msg.InitiatorPubkey[:], pubA) copy(msg.Nonce[:], nonceA) return msg } makeAck := func(test handshakeAckTest) *authRespV4 { msg := &authRespV4{Version: test.wantVersion, Rest: test.wantRest} copy(msg.RandomPubkey[:], ephPubB) copy(msg.Nonce[:], nonceB) return msg } // check auth msg parsing for _, test := range eip8HandshakeAuthTests { r := bytes.NewReader(unhex(test.input)) msg := new(authMsgV4) ciphertext, err := readHandshakeMsg(msg, encAuthMsgLen, keyB, r) if err != nil { t.Errorf("error for input %x:\n %v", unhex(test.input), err) continue } if !bytes.Equal(ciphertext, unhex(test.input)) { t.Errorf("wrong ciphertext for input %x:\n %x", unhex(test.input), ciphertext) } want := makeAuth(test) if !reflect.DeepEqual(msg, want) { t.Errorf("wrong msg for input %x:\ngot %s\nwant %s", unhex(test.input), spew.Sdump(msg), spew.Sdump(want)) } } // check auth resp parsing for _, test := range eip8HandshakeRespTests { input := unhex(test.input) r := bytes.NewReader(input) msg := new(authRespV4) ciphertext, err := readHandshakeMsg(msg, encAuthRespLen, keyA, r) if err != nil { t.Errorf("error for input %x:\n %v", input, err) continue } if !bytes.Equal(ciphertext, input) { t.Errorf("wrong ciphertext for input %x:\n %x", input, err) } want := makeAck(test) if !reflect.DeepEqual(msg, want) { t.Errorf("wrong msg for input %x:\ngot %s\nwant %s", input, spew.Sdump(msg), spew.Sdump(want)) } } // check derivation for (Auth₂, Ack₂) on recipient side var ( hs = &encHandshake{ initiator: false, respNonce: nonceB, randomPrivKey: ecies.ImportECDSA(ephB), } authCiphertext = unhex(eip8HandshakeAuthTests[1].input) authRespCiphertext = unhex(eip8HandshakeRespTests[1].input) authMsg = makeAuth(eip8HandshakeAuthTests[1]) wantAES = unhex("80e8632c05fed6fc2a13b0f8d31a3cf645366239170ea067065aba8e28bac487") wantMAC = unhex("2ea74ec5dae199227dff1af715362700e989d889d7a493cb0639691efb8e5f98") wantFooIngressHash = unhex("0c7ec6340062cc46f5e9f1e3cf86f8c8c403c5a0964f5df0ebd34a75ddc86db5") ) if err := hs.handleAuthMsg(authMsg, keyB); err != nil { t.Fatalf("handleAuthMsg: %v", err) } derived, err := hs.secrets(authCiphertext, authRespCiphertext) if err != nil { t.Fatalf("secrets: %v", err) } if !bytes.Equal(derived.AES, wantAES) { t.Errorf("aes-secret mismatch:\ngot %x\nwant %x", derived.AES, wantAES) } if !bytes.Equal(derived.MAC, wantMAC) { t.Errorf("mac-secret mismatch:\ngot %x\nwant %x", derived.MAC, wantMAC) } io.WriteString(derived.IngressMAC, "foo") fooIngressHash := derived.IngressMAC.Sum(nil) if !bytes.Equal(fooIngressHash, wantFooIngressHash) { t.Errorf("ingress-mac('foo') mismatch:\ngot %x\nwant %x", fooIngressHash, wantFooIngressHash) } }
func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) { key := ecies.ImportECDSA(prv) return key.Decrypt(rand.Reader, ct, nil, nil) }