mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 01:04:02 +03:00
Small subtle optimizations of faketls
This commit is contained in:
@@ -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:]))
|
||||||
|
|||||||
@@ -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)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user