Просмотр исходного кода

Small subtle optimizations of faketls

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
54a7c6a2a5
2 измененных файлов: 9 добавлений и 9 удалений
  1. 4
    8
      mtglib/internal/faketls/client_hello.go
  2. 5
    1
      mtglib/internal/faketls/init.go

+ 4
- 8
mtglib/internal/faketls/client_hello.go Просмотреть файл

3
 import (
3
 import (
4
 	"crypto/hmac"
4
 	"crypto/hmac"
5
 	"crypto/sha256"
5
 	"crypto/sha256"
6
+	"crypto/subtle"
6
 	"encoding/binary"
7
 	"encoding/binary"
7
 	"fmt"
8
 	"fmt"
8
 	"time"
9
 	"time"
39
 	}
40
 	}
40
 
41
 
41
 	copy(hello.Random[:], handshake[ClientHelloRandomOffset:])
42
 	copy(hello.Random[:], handshake[ClientHelloRandomOffset:])
42
-
43
-	for i := ClientHelloRandomOffset; i < ClientHelloRandomOffset+RandomLen; i++ {
44
-		handshake[i] = 0
45
-	}
43
+	copy(handshake[ClientHelloRandomOffset:], clientHelloEmptyRandom)
46
 
44
 
47
 	rec := record.AcquireRecord()
45
 	rec := record.AcquireRecord()
48
 	defer record.ReleaseRecord(rec)
46
 	defer record.ReleaseRecord(rec)
62
 		computedRandom[i] ^= hello.Random[i]
60
 		computedRandom[i] ^= hello.Random[i]
63
 	}
61
 	}
64
 
62
 
65
-	for i := 0; i < RandomLen-4; i++ {
66
-		if computedRandom[i] != 0 {
67
-			return hello, ErrBadDigest
68
-		}
63
+	if subtle.ConstantTimeCompare(clientHelloEmptyRandom[:RandomLen-4], computedRandom[:RandomLen-4]) != 1 {
64
+		return hello, ErrBadDigest
69
 	}
65
 	}
70
 
66
 
71
 	timestamp := int64(binary.LittleEndian.Uint32(computedRandom[RandomLen-4:]))
67
 	timestamp := int64(binary.LittleEndian.Uint32(computedRandom[RandomLen-4:]))

+ 5
- 1
mtglib/internal/faketls/init.go Просмотреть файл

1
 package faketls
1
 package faketls
2
 
2
 
3
-import "errors"
3
+import (
4
+	"bytes"
5
+	"errors"
6
+)
4
 
7
 
5
 const (
8
 const (
6
 	RandomLen = 32
9
 	RandomLen = 32
34
 		0x00, 0x1d, // x25519 curve
37
 		0x00, 0x1d, // x25519 curve
35
 		0x00, 0x20, // 32 bytes of key
38
 		0x00, 0x20, // 32 bytes of key
36
 	}
39
 	}
40
+	clientHelloEmptyRandom = bytes.Repeat([]byte{0}, RandomLen)
37
 )
41
 )

Загрузка…
Отмена
Сохранить