Skip to content

Commit 2be863a

Browse files
committed
document castBoolToInt
1 parent 120f00c commit 2be863a

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

secrethandshake/conn_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,18 @@ func TestAuthWrong(t *testing.T) {
130130

131131
go func() {
132132
err := Server(serverState, rwServer)
133-
ch <- err
133+
expErr := ErrProtocol{1}
134+
if err != expErr {
135+
ch <- errors.Wrap(err, "server failed differntly then expected")
136+
} else {
137+
ch <- nil
138+
}
134139
wServer.Close()
135140
}()
136141

137142
go func() {
138143
err := wrongClient(clientState, rwClient)
139-
ch <- err
144+
ch <- errors.Wrap(err, "client failed")
140145
wClient.Close()
141146
}()
142147

@@ -190,9 +195,9 @@ func wrongClient(state *State, conn io.ReadWriter) (err error) {
190195

191196
// recv authentication vector? shouldn't get it
192197
boxedSig := make([]byte, ServerAuthLength)
193-
_, err = io.ReadFull(conn, boxedSig)
194-
if err != io.ErrUnexpectedEOF {
195-
return errors.Errorf("wrongClient: expected unepexcted EOF, got %s", err)
198+
n, err = io.ReadFull(conn, boxedSig)
199+
if err != io.EOF || n != 0 {
200+
return errors.Errorf("wrongClient: expected unepexcted EOF, got %s %d", err, n)
196201
}
197202

198203
return nil

secrethandshake/state.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ package secrethandshake
1111

1212
import (
1313
"bytes"
14-
"unsafe" // ☢ ☣
15-
1614
"crypto/hmac"
1715
"crypto/rand"
1816
"crypto/sha256"
@@ -28,6 +26,13 @@ import (
2826
"golang.org/x/crypto/nacl/box"
2927
)
3028

29+
func init() {
30+
err := testMemoryLayoutAssumption()
31+
if err != nil {
32+
panic(err)
33+
}
34+
}
35+
3136
// State is the state each peer holds during the handshake
3237
type State struct {
3338
appKey, secHash []byte
@@ -167,8 +172,6 @@ func (s *State) createClientAuth() []byte {
167172
return out
168173
}
169174

170-
var nullHello [ed25519.SignatureSize + ed25519.PublicKeySize]byte
171-
172175
// verifyClientAuth returns whether a buffer contains a valid clientAuth message
173176
func (s *State) verifyClientAuth(data []byte) bool {
174177
// this branch is okay because there are no secrets involved
@@ -193,16 +196,13 @@ func (s *State) verifyClientAuth(data []byte) bool {
193196
nonce [24]byte // always 0?
194197
sig [ed25519.SignatureSize]byte
195198
public [ed25519.PublicKeySize]byte
199+
openOk bool
196200
)
197201

198-
_, openOk := box.OpenAfterPrecomputation(s.hello[:0], data, &nonce, &s.secret2)
199-
// if !openOk && hello == nil {
200-
// fmt.Println("warning: nil hello")
201-
// }
202+
_, openOk = box.OpenAfterPrecomputation(s.hello[:0], data, &nonce, &s.secret2)
202203

203-
// subtle API requires an int containing 0 or 1, we only have bool.
204-
// we can't branch because openOk is secret.
205-
okInt := int(*((*byte)(unsafe.Pointer(&openOk))))
204+
// see unsafecast_test.go
205+
okInt := castBoolToInt(&openOk)
206206

207207
subtle.ConstantTimeCopy(okInt, sig[:], s.hello[:ed25519.SignatureSize])
208208
subtle.ConstantTimeCopy(okInt, public[:], s.hello[ed25519.SignatureSize:ed25519.SignatureSize+ed25519.PublicKeySize])

secrethandshake/unsafecast.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package secrethandshake
2+
3+
import (
4+
"errors"
5+
"unsafe" // ☢ ☣
6+
)
7+
8+
// the idea here is that the memory backed by bool
9+
// will be on byte and no other values than 0x00 and 0x01
10+
func castBoolToInt(bptr *bool) int {
11+
uptr := unsafe.Pointer(bptr)
12+
bytePtr := (*byte)(uptr)
13+
14+
return int(*bytePtr)
15+
}
16+
17+
func testMemoryLayoutAssumption() error {
18+
var boolTrue, boolFalse bool
19+
boolTrue = true
20+
21+
okTrue := castBoolToInt(&boolTrue)
22+
if okTrue != 1 {
23+
return errors.New("expected bool to int cast failed on true")
24+
}
25+
26+
okFalse := castBoolToInt(&boolFalse)
27+
if okFalse != 0 {
28+
return errors.New("expected bool to int cast failed on false")
29+
}
30+
return nil
31+
}

secrethandshake/unsafecast_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package secrethandshake
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUnsafeBoolCase(t *testing.T) {
8+
if err := testMemoryLayoutAssumption(); err != nil {
9+
t.Fatal(err)
10+
}
11+
}

0 commit comments

Comments
 (0)