-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
keys.go
75 lines (58 loc) · 1.61 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package obfuscated2
import (
"crypto/cipher"
"encoding/binary"
"io"
"github.com/go-faster/errors"
"github.com/gotd/td/internal/crypto"
)
type keys struct {
header []byte
encrypt cipher.Stream
decrypt cipher.Stream
}
func (k *keys) createStreams(init, secret []byte) error {
// preallocate 256 bit key + 16 byte secret
const keyLength = 32 + 16
encryptKey := append(make([]byte, 0, keyLength), init[8:40]...)
encryptIV := append(make([]byte, 0, 16), init[40:56]...)
initRev := getDecryptInit(init)
decryptKey := append(make([]byte, 0, keyLength), initRev[:32]...)
decryptIV := append(make([]byte, 0, 16), initRev[32:48]...)
if len(secret) > 0 {
if len(secret) < 16 {
return errors.Errorf("invalid secret size %d", len(secret))
}
secret = secret[0:16]
encryptKey = crypto.SHA256(encryptKey, secret)
decryptKey = crypto.SHA256(decryptKey, secret)
}
var err error
k.encrypt, err = createCTR(encryptKey, encryptIV)
if err != nil {
return err
}
k.decrypt, err = createCTR(decryptKey, decryptIV)
if err != nil {
return err
}
return nil
}
func generateKeys(randSource io.Reader, protocol [4]byte, secret []byte, dc int) (keys, error) {
init, err := generateInit(randSource)
if err != nil {
return keys{}, err
}
var k keys
if err := k.createStreams(init[:], secret); err != nil {
return keys{}, err
}
copy(init[56:60], protocol[:])
binary.LittleEndian.PutUint16(init[60:62], uint16(dc))
var encryptedInit [64]byte
k.encrypt.XORKeyStream(encryptedInit[:], init[:])
k.header = make([]byte, 64)
copy(k.header, init[0:56])
copy(k.header[56:], encryptedInit[56:56+8])
return k, nil
}