OSDN Git Service

Modify the p2p module encryption library (#125)
[bytom/vapor.git] / p2p / connection / secret_connection.go
1 package connection
2
3 import (
4         "bytes"
5         crand "crypto/rand"
6         "crypto/sha256"
7         "encoding/binary"
8         "errors"
9         "io"
10         "net"
11         "time"
12
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"
19
20         "github.com/vapor/p2p/signlib"
21 )
22
23 const (
24         dataLenSize     = 2 // uint16 to describe the length, is <= dataMaxSize
25         dataMaxSize     = 1024
26         totalFrameSize  = dataMaxSize + dataLenSize
27         sealedFrameSize = totalFrameSize + secretbox.Overhead
28 )
29
30 type authSigMessage struct {
31         Key []byte
32         Sig []byte
33 }
34
35 // SecretConnection implements net.Conn
36 type SecretConnection struct {
37         conn       io.ReadWriteCloser
38         recvBuffer []byte
39         recvNonce  *[24]byte
40         sendNonce  *[24]byte
41         remPubKey  signlib.PubKey
42         shrSecret  *[32]byte // shared secret
43 }
44
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()
49
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)
54         if err != nil {
55                 return nil, err
56         }
57
58         // Compute common shared secret.
59         shrSecret := computeSharedSecret(remEphPub, locEphPriv)
60
61         // Sort by lexical order.
62         loEphPub, hiEphPub := sort32(locEphPub, remEphPub)
63
64         // Generate nonces to use for secretbox.
65         recvNonce, sendNonce := genNonces(loEphPub, hiEphPub, locEphPub == loEphPub)
66
67         // Generate common challenge to sign.
68         challenge := genChallenge(loEphPub, hiEphPub)
69
70         // Construct SecretConnection.
71         sc := &SecretConnection{
72                 conn:       conn,
73                 recvBuffer: nil,
74                 recvNonce:  recvNonce,
75                 sendNonce:  sendNonce,
76                 shrSecret:  shrSecret,
77         }
78
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)
83         if err != nil {
84                 return nil, err
85         }
86         pubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
87         remPubKey, err := signlib.NewPubKey(pubKey)
88         if err != nil {
89                 return nil, err
90         }
91
92         if !remPubKey.Verify(challenge[:], remSignature[:]) {
93                 return nil, errors.New("Challenge verification failed")
94         }
95
96         sc.remPubKey = remPubKey
97         return sc, nil
98 }
99
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_:]
105                 return
106         }
107
108         sealedFrame := make([]byte, sealedFrameSize)
109         if _, err = io.ReadFull(sc.conn, sealedFrame); err != nil {
110                 return
111         }
112
113         // decrypt the frame
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")
117         }
118
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")
123         }
124
125         chunk := frame[dataLenSize : dataLenSize+chunkLength]
126         n = copy(data, chunk)
127         sc.recvBuffer = chunk[n:]
128         return
129 }
130
131 // RemotePubKey returns authenticated remote pubkey
132 func (sc *SecretConnection) RemotePubKey() signlib.PubKey {
133         return sc.remPubKey
134 }
135
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) {
139         for 0 < len(data) {
140                 var chunk []byte
141                 frame := make([]byte, totalFrameSize)
142                 if dataMaxSize < len(data) {
143                         chunk = data[:dataMaxSize]
144                         data = data[dataMaxSize:]
145                 } else {
146                         chunk = data
147                         data = nil
148                 }
149                 binary.BigEndian.PutUint16(frame, uint16(len(chunk)))
150                 copy(frame[dataLenSize:], chunk)
151
152                 // encrypt the frame
153                 sealedFrame := make([]byte, sealedFrameSize)
154                 secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret)
155                 incr2Nonce(sc.sendNonce)
156
157                 if _, err := sc.conn.Write(sealedFrame); err != nil {
158                         return n, err
159                 }
160
161                 n += len(chunk)
162         }
163         return
164 }
165
166 // Close implements net.Conn
167 func (sc *SecretConnection) Close() error { return sc.conn.Close() }
168
169 // LocalAddr implements net.Conn
170 func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
171
172 // RemoteAddr implements net.Conn
173 func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
174
175 // SetDeadline implements net.Conn
176 func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
177
178 // SetReadDeadline implements net.Conn
179 func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
180         return sc.conn.(net.Conn).SetReadDeadline(t)
181 }
182
183 // SetWriteDeadline implements net.Conn
184 func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
185         return sc.conn.(net.Conn).SetWriteDeadline(t)
186 }
187
188 func computeSharedSecret(remPubKey, locPrivKey *[32]byte) (shrSecret *[32]byte) {
189         shrSecret = new([32]byte)
190         box.Precompute(shrSecret, remPubKey, locPrivKey)
191         return
192 }
193
194 func genChallenge(loPubKey, hiPubKey *[32]byte) (challenge *[32]byte) {
195         return hash32(append(loPubKey[:], hiPubKey[:]...))
196 }
197
198 // increment nonce big-endian by 2 with wraparound.
199 func incr2Nonce(nonce *[24]byte) {
200         incrNonce(nonce)
201         incrNonce(nonce)
202 }
203
204 // increment nonce big-endian by 1 with wraparound.
205 func incrNonce(nonce *[24]byte) {
206         for i := 23; 0 <= i; i-- {
207                 nonce[i]++
208                 if nonce[i] != 0 {
209                         return
210                 }
211         }
212 }
213
214 func genEphKeys() (ephPub, ephPriv *[32]byte) {
215         var err error
216         ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
217         if err != nil {
218                 log.Panic("Could not generate ephemeral keypairs")
219         }
220         return
221 }
222
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
228         if locIsLo {
229                 return nonce1, nonce2
230         }
231         return nonce2, nonce1
232 }
233
234 func signChallenge(challenge *[32]byte, locPrivKey signlib.PrivKey) []byte {
235         return locPrivKey.Sign(challenge[:])
236 }
237
238 func shareAuthSignature(sc *SecretConnection, pubKey []byte, signature []byte) (*authSigMessage, error) {
239         var recvMsg authSigMessage
240         var err1, err2 error
241
242         cmn.Parallel(
243                 func() {
244                         msgBytes := wire.BinaryBytes(authSigMessage{pubKey, signature})
245                         _, err1 = sc.Write(msgBytes)
246                 },
247                 func() {
248                         readBuffer := make([]byte, signlib.AuthSigMsgSize)
249                         _, err2 = io.ReadFull(sc, readBuffer)
250                         if err2 != nil {
251                                 return
252                         }
253                         n := int(0) // not used.
254                         recvMsg = wire.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), signlib.AuthSigMsgSize, &n, &err2).(authSigMessage)
255                 },
256         )
257         if err1 != nil {
258                 return nil, err1
259         }
260         if err2 != nil {
261                 return nil, err2
262         }
263         return &recvMsg, nil
264 }
265
266 func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
267         var err1, err2 error
268
269         cmn.Parallel(
270                 func() {
271                         _, err1 = conn.Write(locEphPub[:])
272                 },
273                 func() {
274                         remEphPub = new([32]byte)
275                         _, err2 = io.ReadFull(conn, remEphPub[:])
276                 },
277         )
278
279         if err1 != nil {
280                 return nil, err1
281         }
282         if err2 != nil {
283                 return nil, err2
284         }
285         return remEphPub, nil
286 }
287
288 func sort32(foo, bar *[32]byte) (*[32]byte, *[32]byte) {
289         if bytes.Compare(foo[:], bar[:]) < 0 {
290                 return foo, bar
291         }
292         return bar, foo
293 }
294
295 // sha256
296 func hash32(input []byte) (res *[32]byte) {
297         hasher := sha256.New()
298         hasher.Write(input) // does not error
299         resSlice := hasher.Sum(nil)
300         res = new([32]byte)
301         copy(res[:], resSlice)
302         return
303 }
304
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)
310         res = new([24]byte)
311         copy(res[:], resSlice)
312         return
313 }