13 log "github.com/sirupsen/logrus"
14 "github.com/tendermint/go-wire"
15 cmn "github.com/tendermint/tmlibs/common"
16 "golang.org/x/crypto/nacl/box"
17 "golang.org/x/crypto/nacl/secretbox"
18 "golang.org/x/crypto/ripemd160"
20 "github.com/vapor/p2p/signlib"
24 dataLenSize = 2 // uint16 to describe the length, is <= dataMaxSize
26 totalFrameSize = dataMaxSize + dataLenSize
27 sealedFrameSize = totalFrameSize + secretbox.Overhead
30 type authSigMessage struct {
35 // SecretConnection implements net.Conn
36 type SecretConnection struct {
37 conn io.ReadWriteCloser
41 remPubKey signlib.PubKey
42 shrSecret *[32]byte // shared secret
45 // MakeSecretConnection performs handshake and returns a new authenticated SecretConnection.
46 func MakeSecretConnection(conn io.ReadWriteCloser, locPrivKey signlib.PrivKey) (*SecretConnection, error) {
47 // Generate ephemeral keys for perfect forward secrecy.
48 locEphPub, locEphPriv := genEphKeys()
50 // Write local ephemeral pubkey and receive one too.
51 // NOTE: every 32-byte string is accepted as a Curve25519 public key
52 // (see DJB's Curve25519 paper: http://cr.yp.to/ecdh/curve25519-20060209.pdf)
53 remEphPub, err := shareEphPubKey(conn, locEphPub)
58 // Compute common shared secret.
59 shrSecret := computeSharedSecret(remEphPub, locEphPriv)
61 // Sort by lexical order.
62 loEphPub, hiEphPub := sort32(locEphPub, remEphPub)
64 // Generate nonces to use for secretbox.
65 recvNonce, sendNonce := genNonces(loEphPub, hiEphPub, locEphPub == loEphPub)
67 // Generate common challenge to sign.
68 challenge := genChallenge(loEphPub, hiEphPub)
70 // Construct SecretConnection.
71 sc := &SecretConnection{
79 // Sign the challenge bytes for authentication.
80 locSignature := signChallenge(challenge, locPrivKey)
81 // Share (in secret) each other's pubkey & challenge signature
82 authSigMsg, err := shareAuthSignature(sc, locPrivKey.XPub().Bytes(), locSignature)
86 pubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
87 remPubKey, err := signlib.NewPubKey(pubKey)
92 if !remPubKey.Verify(challenge[:], remSignature[:]) {
93 return nil, errors.New("Challenge verification failed")
96 sc.remPubKey = remPubKey
100 // CONTRACT: data smaller than dataMaxSize is read atomically.
101 func (sc *SecretConnection) Read(data []byte) (n int, err error) {
102 if 0 < len(sc.recvBuffer) {
103 n_ := copy(data, sc.recvBuffer)
104 sc.recvBuffer = sc.recvBuffer[n_:]
108 sealedFrame := make([]byte, sealedFrameSize)
109 if _, err = io.ReadFull(sc.conn, sealedFrame); err != nil {
114 frame := make([]byte, totalFrameSize)
115 if _, ok := secretbox.Open(frame[:0], sealedFrame, sc.recvNonce, sc.shrSecret); !ok {
116 return n, errors.New("Failed to decrypt SecretConnection")
119 incr2Nonce(sc.recvNonce)
120 chunkLength := binary.BigEndian.Uint16(frame) // read the first two bytes
121 if chunkLength > dataMaxSize {
122 return 0, errors.New("chunkLength is greater than dataMaxSize")
125 chunk := frame[dataLenSize : dataLenSize+chunkLength]
126 n = copy(data, chunk)
127 sc.recvBuffer = chunk[n:]
131 // RemotePubKey returns authenticated remote pubkey
132 func (sc *SecretConnection) RemotePubKey() signlib.PubKey {
136 // Writes encrypted frames of `sealedFrameSize`
137 // CONTRACT: data smaller than dataMaxSize is read atomically.
138 func (sc *SecretConnection) Write(data []byte) (n int, err error) {
141 frame := make([]byte, totalFrameSize)
142 if dataMaxSize < len(data) {
143 chunk = data[:dataMaxSize]
144 data = data[dataMaxSize:]
149 binary.BigEndian.PutUint16(frame, uint16(len(chunk)))
150 copy(frame[dataLenSize:], chunk)
153 sealedFrame := make([]byte, sealedFrameSize)
154 secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret)
155 incr2Nonce(sc.sendNonce)
157 if _, err := sc.conn.Write(sealedFrame); err != nil {
166 // Close implements net.Conn
167 func (sc *SecretConnection) Close() error { return sc.conn.Close() }
169 // LocalAddr implements net.Conn
170 func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
172 // RemoteAddr implements net.Conn
173 func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
175 // SetDeadline implements net.Conn
176 func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
178 // SetReadDeadline implements net.Conn
179 func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
180 return sc.conn.(net.Conn).SetReadDeadline(t)
183 // SetWriteDeadline implements net.Conn
184 func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
185 return sc.conn.(net.Conn).SetWriteDeadline(t)
188 func computeSharedSecret(remPubKey, locPrivKey *[32]byte) (shrSecret *[32]byte) {
189 shrSecret = new([32]byte)
190 box.Precompute(shrSecret, remPubKey, locPrivKey)
194 func genChallenge(loPubKey, hiPubKey *[32]byte) (challenge *[32]byte) {
195 return hash32(append(loPubKey[:], hiPubKey[:]...))
198 // increment nonce big-endian by 2 with wraparound.
199 func incr2Nonce(nonce *[24]byte) {
204 // increment nonce big-endian by 1 with wraparound.
205 func incrNonce(nonce *[24]byte) {
206 for i := 23; 0 <= i; i-- {
214 func genEphKeys() (ephPub, ephPriv *[32]byte) {
216 ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
218 log.Panic("Could not generate ephemeral keypairs")
223 func genNonces(loPubKey, hiPubKey *[32]byte, locIsLo bool) (*[24]byte, *[24]byte) {
224 nonce1 := hash24(append(loPubKey[:], hiPubKey[:]...))
225 nonce2 := new([24]byte)
226 copy(nonce2[:], nonce1[:])
227 nonce2[len(nonce2)-1] ^= 0x01
229 return nonce1, nonce2
231 return nonce2, nonce1
234 func signChallenge(challenge *[32]byte, locPrivKey signlib.PrivKey) []byte {
235 return locPrivKey.Sign(challenge[:])
238 func shareAuthSignature(sc *SecretConnection, pubKey []byte, signature []byte) (*authSigMessage, error) {
239 var recvMsg authSigMessage
244 msgBytes := wire.BinaryBytes(authSigMessage{pubKey, signature})
245 _, err1 = sc.Write(msgBytes)
248 readBuffer := make([]byte, signlib.AuthSigMsgSize)
249 _, err2 = io.ReadFull(sc, readBuffer)
253 n := int(0) // not used.
254 recvMsg = wire.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), signlib.AuthSigMsgSize, &n, &err2).(authSigMessage)
266 func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
271 _, err1 = conn.Write(locEphPub[:])
274 remEphPub = new([32]byte)
275 _, err2 = io.ReadFull(conn, remEphPub[:])
285 return remEphPub, nil
288 func sort32(foo, bar *[32]byte) (*[32]byte, *[32]byte) {
289 if bytes.Compare(foo[:], bar[:]) < 0 {
296 func hash32(input []byte) (res *[32]byte) {
297 hasher := sha256.New()
298 hasher.Write(input) // does not error
299 resSlice := hasher.Sum(nil)
301 copy(res[:], resSlice)
305 // We only fill in the first 20 bytes with ripemd160
306 func hash24(input []byte) (res *[24]byte) {
307 hasher := ripemd160.New()
308 hasher.Write(input) // does not error
309 resSlice := hasher.Sum(nil)
311 copy(res[:], resSlice)