1 // Uses nacl's secret_box to encrypt a net.Conn.
2 // It is (meant to be) an implementation of the STS protocol.
3 // Note we do not (yet) assume that a remote peer's pubkey
4 // is known ahead of time, and thus we are technically
5 // still vulnerable to MITM. (TODO!)
6 // See docs/sts-final.pdf for more info
19 "golang.org/x/crypto/nacl/box"
20 "golang.org/x/crypto/nacl/secretbox"
21 "golang.org/x/crypto/ripemd160"
23 "github.com/tendermint/go-crypto"
24 "github.com/tendermint/go-wire"
25 cmn "github.com/tendermint/tmlibs/common"
28 // 2 + 1024 == 1026 total frame size
29 const dataLenSize = 2 // uint16 to describe the length, is <= dataMaxSize
30 const dataMaxSize = 1024
31 const totalFrameSize = dataMaxSize + dataLenSize
32 const sealedFrameSize = totalFrameSize + secretbox.Overhead
33 const authSigMsgSize = (32 + 1) + (64 + 1) // fixed size (length prefixed) byte arrays
35 // Implements net.Conn
36 type SecretConnection struct {
37 conn io.ReadWriteCloser
41 remPubKey crypto.PubKeyEd25519
42 shrSecret *[32]byte // shared secret
45 // Performs handshake and returns a new authenticated SecretConnection.
46 // Returns nil if error in handshake.
47 // Caller should call conn.Close()
48 // See docs/sts-final.pdf for more information.
49 func MakeSecretConnection(conn io.ReadWriteCloser, locPrivKey crypto.PrivKeyEd25519) (*SecretConnection, error) {
51 locPubKey := locPrivKey.PubKey().Unwrap().(crypto.PubKeyEd25519)
53 // Generate ephemeral keys for perfect forward secrecy.
54 locEphPub, locEphPriv := genEphKeys()
56 // Write local ephemeral pubkey and receive one too.
57 // NOTE: every 32-byte string is accepted as a Curve25519 public key
58 // (see DJB's Curve25519 paper: http://cr.yp.to/ecdh/curve25519-20060209.pdf)
59 remEphPub, err := shareEphPubKey(conn, locEphPub)
64 // Compute common shared secret.
65 shrSecret := computeSharedSecret(remEphPub, locEphPriv)
67 // Sort by lexical order.
68 loEphPub, hiEphPub := sort32(locEphPub, remEphPub)
70 // Generate nonces to use for secretbox.
71 recvNonce, sendNonce := genNonces(loEphPub, hiEphPub, locEphPub == loEphPub)
73 // Generate common challenge to sign.
74 challenge := genChallenge(loEphPub, hiEphPub)
76 // Construct SecretConnection.
77 sc := &SecretConnection{
85 // Sign the challenge bytes for authentication.
86 locSignature := signChallenge(challenge, locPrivKey)
88 // Share (in secret) each other's pubkey & challenge signature
89 authSigMsg, err := shareAuthSignature(sc, locPubKey, locSignature)
93 remPubKey, remSignature := authSigMsg.Key, authSigMsg.Sig
94 if !remPubKey.VerifyBytes(challenge[:], remSignature) {
95 return nil, errors.New("Challenge verification failed")
99 sc.remPubKey = remPubKey.Unwrap().(crypto.PubKeyEd25519)
103 // Returns authenticated remote pubkey
104 func (sc *SecretConnection) RemotePubKey() crypto.PubKeyEd25519 {
108 // Writes encrypted frames of `sealedFrameSize`
109 // CONTRACT: data smaller than dataMaxSize is read atomically.
110 func (sc *SecretConnection) Write(data []byte) (n int, err error) {
112 var frame []byte = make([]byte, totalFrameSize)
114 if dataMaxSize < len(data) {
115 chunk = data[:dataMaxSize]
116 data = data[dataMaxSize:]
121 chunkLength := len(chunk)
122 binary.BigEndian.PutUint16(frame, uint16(chunkLength))
123 copy(frame[dataLenSize:], chunk)
126 var sealedFrame = make([]byte, sealedFrameSize)
127 secretbox.Seal(sealedFrame[:0], frame, sc.sendNonce, sc.shrSecret)
128 // fmt.Printf("secretbox.Seal(sealed:%X,sendNonce:%X,shrSecret:%X\n", sealedFrame, sc.sendNonce, sc.shrSecret)
129 incr2Nonce(sc.sendNonce)
132 _, err := sc.conn.Write(sealedFrame)
142 // CONTRACT: data smaller than dataMaxSize is read atomically.
143 func (sc *SecretConnection) Read(data []byte) (n int, err error) {
144 if 0 < len(sc.recvBuffer) {
145 n_ := copy(data, sc.recvBuffer)
146 sc.recvBuffer = sc.recvBuffer[n_:]
150 sealedFrame := make([]byte, sealedFrameSize)
151 _, err = io.ReadFull(sc.conn, sealedFrame)
157 var frame = make([]byte, totalFrameSize)
158 // fmt.Printf("secretbox.Open(sealed:%X,recvNonce:%X,shrSecret:%X\n", sealedFrame, sc.recvNonce, sc.shrSecret)
159 _, ok := secretbox.Open(frame[:0], sealedFrame, sc.recvNonce, sc.shrSecret)
161 return n, errors.New("Failed to decrypt SecretConnection")
163 incr2Nonce(sc.recvNonce)
166 var chunkLength = binary.BigEndian.Uint16(frame) // read the first two bytes
167 if chunkLength > dataMaxSize {
168 return 0, errors.New("chunkLength is greater than dataMaxSize")
170 var chunk = frame[dataLenSize : dataLenSize+chunkLength]
172 n = copy(data, chunk)
173 sc.recvBuffer = chunk[n:]
177 // Implements net.Conn
178 func (sc *SecretConnection) Close() error { return sc.conn.Close() }
179 func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
180 func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
181 func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
182 func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
183 return sc.conn.(net.Conn).SetReadDeadline(t)
185 func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
186 return sc.conn.(net.Conn).SetWriteDeadline(t)
189 func genEphKeys() (ephPub, ephPriv *[32]byte) {
191 ephPub, ephPriv, err = box.GenerateKey(crand.Reader)
193 cmn.PanicCrisis("Could not generate ephemeral keypairs")
198 func shareEphPubKey(conn io.ReadWriteCloser, locEphPub *[32]byte) (remEphPub *[32]byte, err error) {
203 _, err1 = conn.Write(locEphPub[:])
206 remEphPub = new([32]byte)
207 _, err2 = io.ReadFull(conn, remEphPub[:])
218 return remEphPub, nil
221 func computeSharedSecret(remPubKey, locPrivKey *[32]byte) (shrSecret *[32]byte) {
222 shrSecret = new([32]byte)
223 box.Precompute(shrSecret, remPubKey, locPrivKey)
227 func sort32(foo, bar *[32]byte) (lo, hi *[32]byte) {
228 if bytes.Compare(foo[:], bar[:]) < 0 {
238 func genNonces(loPubKey, hiPubKey *[32]byte, locIsLo bool) (recvNonce, sendNonce *[24]byte) {
239 nonce1 := hash24(append(loPubKey[:], hiPubKey[:]...))
240 nonce2 := new([24]byte)
241 copy(nonce2[:], nonce1[:])
242 nonce2[len(nonce2)-1] ^= 0x01
253 func genChallenge(loPubKey, hiPubKey *[32]byte) (challenge *[32]byte) {
254 return hash32(append(loPubKey[:], hiPubKey[:]...))
257 func signChallenge(challenge *[32]byte, locPrivKey crypto.PrivKeyEd25519) (signature crypto.SignatureEd25519) {
258 signature = locPrivKey.Sign(challenge[:]).Unwrap().(crypto.SignatureEd25519)
262 type authSigMessage struct {
267 func shareAuthSignature(sc *SecretConnection, pubKey crypto.PubKeyEd25519, signature crypto.SignatureEd25519) (*authSigMessage, error) {
268 var recvMsg authSigMessage
273 msgBytes := wire.BinaryBytes(authSigMessage{pubKey.Wrap(), signature.Wrap()})
274 _, err1 = sc.Write(msgBytes)
277 readBuffer := make([]byte, authSigMsgSize)
278 _, err2 = io.ReadFull(sc, readBuffer)
282 n := int(0) // not used.
283 recvMsg = wire.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), authSigMsgSize, &n, &err2).(authSigMessage)
296 func verifyChallengeSignature(challenge *[32]byte, remPubKey crypto.PubKeyEd25519, remSignature crypto.SignatureEd25519) bool {
297 return remPubKey.VerifyBytes(challenge[:], remSignature.Wrap())
300 //--------------------------------------------------------------------------------
303 func hash32(input []byte) (res *[32]byte) {
304 hasher := sha256.New()
305 hasher.Write(input) // does not error
306 resSlice := hasher.Sum(nil)
308 copy(res[:], resSlice)
312 // We only fill in the first 20 bytes with ripemd160
313 func hash24(input []byte) (res *[24]byte) {
314 hasher := ripemd160.New()
315 hasher.Write(input) // does not error
316 resSlice := hasher.Sum(nil)
318 copy(res[:], resSlice)
323 func hash20(input []byte) (res *[20]byte) {
324 hasher := ripemd160.New()
325 hasher.Write(input) // does not error
326 resSlice := hasher.Sum(nil)
328 copy(res[:], resSlice)
332 // increment nonce big-endian by 2 with wraparound.
333 func incr2Nonce(nonce *[24]byte) {
338 // increment nonce big-endian by 1 with wraparound.
339 func incrNonce(nonce *[24]byte) {
340 for i := 23; 0 <= i; i-- {