secio: threadsafe
clients were already accessing secio in one thread, but it's safer to make sure it _is_ threadsafe
Juan Batiz-Benet committed
Jan 1, 2015 at 19:48 UTC
175af5227f5292588a61457a7f72218f713b5dad
1 file changed
+13
crypto/secio/rw.go
+13
@@ -5,6 +5,7 @@ import (
5
"errors"
6
"fmt"
7
"io"
8
+ "sync"
9
10
"crypto/hmac"
11
@@ -27,6 +28,8 @@ type etmWriter struct {
28
msg msgio.WriteCloser // msgio for knowing where boundaries lie
29
str cipher.Stream // the stream cipher to encrypt with
30
mac HMAC // the mac to authenticate data with
31
+
32
+ sync.Mutex
33
}
34
35
// NewETMWriter Encrypt-Then-MAC
@@ -44,6 +47,8 @@ func (w *etmWriter) Write(b []byte) (int, error) {
47
48
// WriteMsg writes the msg in the passed in buffer.
49
func (w *etmWriter) WriteMsg(b []byte) error {
50
+ w.Lock()
51
+ defer w.Unlock()
52
53
// encrypt.
54
data := w.pool.Get(uint32(len(b))).([]byte)
@@ -83,6 +88,8 @@ type etmReader struct {
88
msg msgio.ReadCloser // msgio for knowing where boundaries lie
89
str cipher.Stream // the stream cipher to encrypt with
90
mac HMAC // the mac to authenticate data with
91
+
92
+ sync.Mutex
93
}
94
95
// NewETMReader Encrypt-Then-MAC
@@ -105,6 +112,9 @@ func (r *etmReader) drainBuf(buf []byte) int {
112
}
113
114
func (r *etmReader) Read(buf []byte) (int, error) {
115
+ r.Lock()
116
+ defer r.Unlock()
117
+
118
// first, check if we have anything in the buffer
119
copied := r.drainBuf(buf)
120
buf = buf[copied:]
@@ -151,6 +161,9 @@ func (r *etmReader) Read(buf []byte) (int, error) {
161
}
162
163
func (r *etmReader) ReadMsg() ([]byte, error) {
164
+ r.Lock()
165
+ defer r.Unlock()
166
+
167
msg, err := r.msg.ReadMsg()
168
if err != nil {
169
return nil, err