/
blob.go
230 lines (182 loc) · 4.92 KB
/
blob.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package spotcontrol
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"golang.org/x/crypto/pbkdf2"
"log"
"math/big"
"os"
)
type BlobInfo struct {
Username string
DecodedBlob string
}
func (b *BlobInfo) saveToFile(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(b)
if err != nil {
return err
}
return nil
}
func blobFromFile(path string) (BlobInfo, error) {
result := BlobInfo{}
file, err := os.Open(path)
if err != nil {
return result, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&result)
if err != nil {
return result, err
}
return result, nil
}
func newBlobInfo(blob64 string, client64 string,
keys privateKeys, deviceId string, username string) (BlobInfo, error) {
partDecoded, err := decodeBlob(blob64, client64, keys)
if err != nil {
return BlobInfo{}, err
}
fullDecoded := decodeBlobSecondary(partDecoded, username,
deviceId)
return BlobInfo{
Username: username,
DecodedBlob: base64.StdEncoding.EncodeToString(fullDecoded),
}, nil
}
func (b *BlobInfo) makeAuthBlob(deviceId string, client64 string, dhKeys privateKeys) (string, error) {
secret := sha1.Sum([]byte(deviceId))
key := blobKey(b.Username, secret[:])
blobBytes, err := base64.StdEncoding.DecodeString(b.DecodedBlob)
if err != nil {
return "", err
}
encoded := encryptBlob(blobBytes, key)
fullEncoded := makeBlob(encoded, dhKeys, client64)
return fullEncoded, nil
}
func blobKey(username string, secret []byte) []byte {
data := pbkdf2.Key(secret, []byte(username), 0x100, 20, sha1.New)[0:20]
hash := sha1.Sum(data)
length := make([]byte, 4)
binary.BigEndian.PutUint32(length, 20)
return append(hash[:], length...)
}
func makeBlob(blobPart []byte, keys privateKeys, publicKey string) string {
part := []byte(base64.StdEncoding.EncodeToString(blobPart))
sharedKey := keys.SharedKey(publicKey)
iv := randomVec(16)
key := sha1.Sum(sharedKey)
base_key := key[:16]
hash := hmac.New(sha1.New, base_key)
hash.Write([]byte("checksum"))
checksum_key := hash.Sum(nil)
hash.Reset()
hash.Write([]byte("encryption"))
encryption_key := hash.Sum(nil)
hash.Reset()
block, _ := aes.NewCipher(encryption_key[0:16])
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(part, part)
macHash := hmac.New(sha1.New, checksum_key)
macHash.Write(part)
mac := macHash.Sum(nil)
part = append(iv, part...)
part = append(part, mac...)
return base64.StdEncoding.EncodeToString(part)
}
func encryptBlob(blob []byte, key []byte) []byte {
block, _ := aes.NewCipher(key)
bs := block.BlockSize()
if len(blob)%bs != 0 {
panic("Need a multiple of the blocksize")
}
l := len(blob)
for i := l - 0x11; i >= 0; i-- {
blob[l-i-1] = blob[l-i-1] ^ blob[l-i-0x11]
}
ciphertext := make([]byte, len(blob))
encoded := ciphertext
for len(blob) > 0 {
block.Encrypt(ciphertext, blob)
ciphertext = ciphertext[bs:]
blob = blob[bs:]
}
return encoded
}
func decodeBlob(blob64 string, client64 string, keys privateKeys) (string, error) {
clientKey, err := base64.StdEncoding.DecodeString(client64)
if err != nil {
return "", err
}
blobBytes, err := base64.StdEncoding.DecodeString(blob64)
if err != nil {
return "", err
}
clientKey_be := new(big.Int)
clientKey_be.SetBytes(clientKey)
sharedKey := powm(clientKey_be, keys.privateKey, keys.prime)
iv := blobBytes[0:16]
encryptedPart := blobBytes[16 : len(blobBytes)-20]
ckSum := blobBytes[len(blobBytes)-20:]
key := sha1.Sum(sharedKey.Bytes())
base_key := key[:16]
hash := hmac.New(sha1.New, base_key)
hash.Write([]byte("checksum"))
checksum_key := hash.Sum(nil)
hash.Reset()
hash.Write([]byte("encryption"))
encryption_key := hash.Sum(nil)
hash.Reset()
macHash := hmac.New(sha1.New, checksum_key)
macHash.Write(encryptedPart)
mac := macHash.Sum(nil)
if !bytes.Equal(mac, ckSum) {
log.Println("add user error, mac doesn't match")
return "", errors.New("mac mismatch")
}
block, _ := aes.NewCipher(encryption_key[0:16])
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(encryptedPart, encryptedPart)
return string(encryptedPart), nil
}
func decodeBlobSecondary(blob64 string, username string, deviceId string) []byte {
blob, _ := base64.StdEncoding.DecodeString(blob64)
secret := sha1.Sum([]byte(deviceId))
key := blobKey(username, secret[:])
data := decryptBlob(blob, key)
return data
}
func decryptBlob(blob []byte, key []byte) []byte {
block, _ := aes.NewCipher(key)
bs := block.BlockSize()
if len(blob)%bs != 0 {
panic("Need a multiple of the blocksize")
}
plaintext := make([]byte, len(blob))
plain := plaintext
for len(blob) > 0 {
block.Decrypt(plaintext, blob)
plaintext = plaintext[bs:]
blob = blob[bs:]
}
l := len(plain)
for i := 0; i < l-0x10; i++ {
plain[l-i-1] = plain[l-i-1] ^ plain[l-i-0x11]
}
return plain
}