Small subtle optimizations of faketls

This commit is contained in:
9seconds
2021-04-06 11:09:37 +03:00
parent a3bae795c4
commit 54a7c6a2a5
2 changed files with 9 additions and 9 deletions
+4 -8
View File
@@ -3,6 +3,7 @@ package faketls
import ( import (
"crypto/hmac" "crypto/hmac"
"crypto/sha256" "crypto/sha256"
"crypto/subtle"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"time" "time"
@@ -39,10 +40,7 @@ func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
} }
copy(hello.Random[:], handshake[ClientHelloRandomOffset:]) copy(hello.Random[:], handshake[ClientHelloRandomOffset:])
copy(handshake[ClientHelloRandomOffset:], clientHelloEmptyRandom)
for i := ClientHelloRandomOffset; i < ClientHelloRandomOffset+RandomLen; i++ {
handshake[i] = 0
}
rec := record.AcquireRecord() rec := record.AcquireRecord()
defer record.ReleaseRecord(rec) defer record.ReleaseRecord(rec)
@@ -62,10 +60,8 @@ func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
computedRandom[i] ^= hello.Random[i] computedRandom[i] ^= hello.Random[i]
} }
for i := 0; i < RandomLen-4; i++ { if subtle.ConstantTimeCompare(clientHelloEmptyRandom[:RandomLen-4], computedRandom[:RandomLen-4]) != 1 {
if computedRandom[i] != 0 { return hello, ErrBadDigest
return hello, ErrBadDigest
}
} }
timestamp := int64(binary.LittleEndian.Uint32(computedRandom[RandomLen-4:])) timestamp := int64(binary.LittleEndian.Uint32(computedRandom[RandomLen-4:]))
+5 -1
View File
@@ -1,6 +1,9 @@
package faketls package faketls
import "errors" import (
"bytes"
"errors"
)
const ( const (
RandomLen = 32 RandomLen = 32
@@ -34,4 +37,5 @@ var (
0x00, 0x1d, // x25519 curve 0x00, 0x1d, // x25519 curve
0x00, 0x20, // 32 bytes of key 0x00, 0x20, // 32 bytes of key
} }
clientHelloEmptyRandom = bytes.Repeat([]byte{0}, RandomLen)
) )