15 log "github.com/sirupsen/logrus"
16 "github.com/bytom/bytom/crypto/ed25519/chainkd"
17 "golang.org/x/crypto/nacl/box"
18 "golang.org/x/crypto/nacl/secretbox"
19 "golang.org/x/crypto/ripemd160"
21 "github.com/tendermint/go-wire"
22 cmn "github.com/tendermint/tmlibs/common"
26 dataLenSize = 2 // uint16 to describe the length, is <= dataMaxSize
28 totalFrameSize = dataMaxSize + dataLenSize
29 sealedFrameSize = totalFrameSize + secretbox.Overhead
30 authSigMsgSize = 100 // fixed size (length prefixed) byte arrays
33 type authSigMessage struct {
38 // SecretConnection implements net.Conn
39 type SecretConnection struct {
40 conn io.ReadWriteCloser
44 remPubKey ed25519.PublicKey
45 shrSecret *[32]byte // shared secret
48 // MakeSecretConnection performs handshake and returns a new authenticated SecretConnection.
49 func MakeSecretConnection(conn io.ReadWriteCloser, locPrivKey chainkd.XPrv) (*SecretConnection, error) {
50 locPubKey := locPrivKey.XPub().PublicKey()
52 // Generate ephemeral keys for perfect forward secrecy.
53 locEphPub, locEphPriv := genEphKeys()
55 // Write local ephemeral pubkey and receive one too.
56 // NOTE: every 32-byte string is accepted as a Curve25519 public key
57 // (see DJB's Curve25519 paper: http://cr.yp.to/ecdh/curve25519-20060209.pdf)
58 remEphPub, err := shareEphPubKey(conn, locEphPub)
63 // Compute common shared secret.
64 shrSecret := computeSharedSecret(remEphPub, locEphPriv)
66 // Sort by lexical order.
67 loEphPub, hiEphPub := sort32(locEphPub, remEphPub)
69 // Generate nonces to use for secretbox.
70 recvNonce, sendNonce := genNonces(loEphPub, hiEphPub, locEphPub == loEphPub)
72 // Generate common challenge to sign.
73 challenge := genChallenge(loEphPub, hiEphPub)
75 // Construct SecretConnection.
76 sc := &SecretConnection{
84 // Sign the challenge bytes for authentication.
85 locSignature := signChallenge(challenge, locPrivKey)
87 // Share (in secret) each other's pubkey & challenge signature
88 authSigMsg, err := shareAuthSignature(sc, locPubKey, locSignature)
93 remPubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
94 if !ed25519.Verify(remPubKey, challenge[:], remSignature) {
95 return nil, errors.New("Challenge verification failed")
98 sc.remPubKey = remPubKey
102 // CONTRACT: data smaller than dataMaxSize is read atomically.
103 func (sc *SecretConnection) Read(data []byte) (n int, err error) {
104 if 0 < len(sc.recvBuffer) {
105 n_ := copy(data, sc.recvBuffer)
106 sc.recvBuffer = sc.recvBuffer[n_:]
110 sealedFrame := make([]byte, sealedFrameSize)
111 if _, err = io.ReadFull(sc.conn, sealedFrame); err != nil {
116 frame := make([]byte, totalFrameSize)
117 if _, ok := secretbox.Open(frame[:0], sealedFrame, sc.recvNonce, sc.shrSecret); !ok {
118 return n, errors.New("Failed to decrypt SecretConnection")
121 incr2Nonce(sc.recvNonce)
122 chunkLength := binary.BigEndian.Uint16(frame) // read the first two bytes
123 if chunkLength > dataMaxSize {
124 return 0, errors.New("chunkLength is greater than dataMaxSize")
127 chunk := frame[dataLenSize : dataLenSize+chunkLength]
128 n = copy(data, chunk)
129 sc.recvBuffer = chunk[n:]
133 // RemotePubKey returns authenticated remote pubkey
134 func (sc *SecretConnection) RemotePubKey() ed25519.PublicKey {
138 // Writes encrypted frames of `sealedFrameSize`
139 // CONTRACT: data smaller than dataMaxSize is read atomically.
140 func (sc *SecretConnection) Write(data []byte) (n int, err error) {
143 frame := make([]byte, totalFrameSize)
144 if dataMaxSize < len(data) {
145 chunk = data[:dataMaxSize]
146 data = data[dataMaxSize:]
151 binary.BigEndian.PutUint16(frame, uint16(len(chunk)))
152 copy(frame[dataLenSize:], chunk)
155 sealedFrame := make([]byte, sealedFrameSize)
156 secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret)
157 incr2Nonce(sc.sendNonce)
159 if _, err := sc.conn.Write(sealedFrame); err != nil {
168 // Close implements net.Conn
169 func (sc *SecretConnection) Close() error { return sc.conn.Close() }
171 // LocalAddr implements net.Conn
172 func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
174 // RemoteAddr implements net.Conn
175 func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
177 // SetDeadline implements net.Conn
178 func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
180 // SetReadDeadline implements net.Conn
181 func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
182 return sc.conn.(net.Conn).SetReadDeadline(t)
185 // SetWriteDeadline implements net.Conn
186 func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
187 return sc.conn.(net.Conn).SetWriteDeadline(t)
190 func computeSharedSecret(remPubKey, locPrivKey *[32]byte) (shrSecret *[32]byte) {
191 shrSecret = new([32]byte)
192 box.Precompute(shrSecret, remPubKey, locPrivKey)
196 func genChallenge(loPubKey, hiPubKey *[32]byte) (challenge *[32]byte) {
197 return hash32(append(loPubKey[:], hiPubKey[:]...))
200 // increment nonce big-endian by 2 with wraparound.
201 func incr2Nonce(nonce *[24]byte) {
206 // increment nonce big-endian by 1 with wraparound.
207 func incrNonce(nonce *[24]byte) {
208 for i := 23; 0 <= i; i-- {
216 func genEphKeys() (ephPub, ephPriv *[32]byte) {
218 ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
220 log.Panic("Could not generate ephemeral keypairs")
225 func genNonces(loPubKey, hiPubKey *[32]byte, locIsLo bool) (*[24]byte, *[24]byte) {
226 nonce1 := hash24(append(loPubKey[:], hiPubKey[:]...))
227 nonce2 := new([24]byte)
228 copy(nonce2[:], nonce1[:])
229 nonce2[len(nonce2)-1] ^= 0x01
231 return nonce1, nonce2
233 return nonce2, nonce1
236 func signChallenge(challenge *[32]byte, locPrivKey chainkd.XPrv) []byte {
237 return locPrivKey.Sign(challenge[:])
240 func shareAuthSignature(sc *SecretConnection, pubKey, signature []byte) (*authSigMessage, error) {
241 var recvMsg authSigMessage
243 wTask := func(i int) (res interface{}, err error, abort bool) {
244 msgBytes := wire.BinaryBytes(authSigMessage{pubKey, signature})
245 _, err = sc.Write(msgBytes)
246 return nil, err, false
249 rTask := func(i int) (res interface{}, err error, abort bool) {
250 readBuffer := make([]byte, authSigMsgSize)
251 _, err = io.ReadFull(sc, readBuffer)
253 return nil, err, false
256 n := int(0) // not used.
257 recvMsg = wire.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), authSigMsgSize, &n, &err).(authSigMessage)
258 return nil, err, false
261 trs, ok := cmn.Parallel(wTask, rTask)
263 return nil, errors.New("Parallel task run failed")
266 for i := 0; i < 2; i++ {
267 res, ok := trs.LatestResult(i)
269 return nil, fmt.Errorf("Task %d did not complete", i)
272 if res.Error != nil {
273 return nil, fmt.Errorf("Task %d should not has error but god %v", i, res.Error)
280 func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
283 func(i int) (res interface{}, err error, abort bool) {
284 _, err = conn.Write(locEphPub[:])
285 return nil, err, false
287 func(i int) (res interface{}, err error, abort bool) {
288 remEphPub = new([32]byte)
289 _, err = io.ReadFull(conn, remEphPub[:])
290 return nil, err, false
301 return remEphPub, nil
304 func sort32(foo, bar *[32]byte) (*[32]byte, *[32]byte) {
305 if bytes.Compare(foo[:], bar[:]) < 0 {
312 func hash32(input []byte) (res *[32]byte) {
313 hasher := sha256.New()
314 hasher.Write(input) // does not error
315 resSlice := hasher.Sum(nil)
317 copy(res[:], resSlice)
321 // We only fill in the first 20 bytes with ripemd160
322 func hash24(input []byte) (res *[24]byte) {
323 hasher := ripemd160.New()
324 hasher.Write(input) // does not error
325 resSlice := hasher.Sum(nil)
327 copy(res[:], resSlice)