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 (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"encoding/binary"
"fmt"
"time"
@@ -39,10 +40,7 @@ func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
}
copy(hello.Random[:], handshake[ClientHelloRandomOffset:])
for i := ClientHelloRandomOffset; i < ClientHelloRandomOffset+RandomLen; i++ {
handshake[i] = 0
}
copy(handshake[ClientHelloRandomOffset:], clientHelloEmptyRandom)
rec := record.AcquireRecord()
defer record.ReleaseRecord(rec)
@@ -62,10 +60,8 @@ func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
computedRandom[i] ^= hello.Random[i]
}
for i := 0; i < RandomLen-4; i++ {
if computedRandom[i] != 0 {
return hello, ErrBadDigest
}
if subtle.ConstantTimeCompare(clientHelloEmptyRandom[:RandomLen-4], computedRandom[:RandomLen-4]) != 1 {
return hello, ErrBadDigest
}
timestamp := int64(binary.LittleEndian.Uint32(computedRandom[RandomLen-4:]))
+5 -1
View File
@@ -1,6 +1,9 @@
package faketls
import "errors"
import (
"bytes"
"errors"
)
const (
RandomLen = 32
@@ -34,4 +37,5 @@ var (
0x00, 0x1d, // x25519 curve
0x00, 0x20, // 32 bytes of key
}
clientHelloEmptyRandom = bytes.Repeat([]byte{0}, RandomLen)
)