forked from xeals/signal-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
237 lines (196 loc) · 5.5 KB
/
backup.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
231
232
233
234
235
236
237
package types
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
_ "crypto/sha256"
_ "crypto/sha512"
"fmt"
"hash"
"io"
"io/ioutil"
"os"
"strings"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/xeals/signal-back/signal"
"golang.org/x/crypto/hkdf"
)
// ProtoCommitHash is the commit hash of the Signal Protobuf spec.
var ProtoCommitHash = "d6610f0"
// BackupFile holds the internal state of decryption of a Signal backup.
type BackupFile struct {
File *bytes.Buffer
CipherKey []byte
MacKey []byte
Mac hash.Hash
IV []byte
Counter uint32
}
// NewBackupFile initialises a backup file for reading using the provided path
// and password.
func NewBackupFile(path, password string) (*BackupFile, error) {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "unable to open backup file")
}
fileBuf := bytes.NewBuffer(fileBytes)
headerLengthBytes := make([]byte, 4)
_, err = io.ReadFull(fileBuf, headerLengthBytes)
if err != nil {
return nil, errors.Wrap(err, "failed to read headerLengthBytes")
}
headerLength := bytesToUint32(headerLengthBytes)
headerFrame := make([]byte, headerLength)
_, err = io.ReadFull(fileBuf, headerFrame)
if err != nil {
return nil, errors.Wrap(err, "failed to read headerFrame")
}
frame := &signal.BackupFrame{}
if err = proto.Unmarshal(headerFrame, frame); err != nil {
return nil, errors.Wrap(err, "failed to decode header")
}
iv := frame.Header.Iv
if len(iv) != 16 {
return nil, errors.New("No IV in header")
}
key := backupKey(password, frame.Header.Salt)
derived := deriveSecrets(key, []byte("Backup Export"))
cipherKey := derived[:32]
macKey := derived[32:]
return &BackupFile{
File: fileBuf,
CipherKey: cipherKey,
MacKey: macKey,
Mac: hmac.New(crypto.SHA256.New, macKey),
IV: iv,
Counter: bytesToUint32(iv),
}, nil
}
// Frame returns the next frame in the file.
func (bf *BackupFile) Frame() (*signal.BackupFrame, error) {
if bf.File.Len() == 0 {
return nil, errors.New("Nothing left to decode")
}
length := make([]byte, 4)
io.ReadFull(bf.File, length)
frameLength := bytesToUint32(length)
frame := make([]byte, frameLength)
io.ReadFull(bf.File, frame)
theirMac := frame[:len(frame)-10]
bf.Mac.Reset()
bf.Mac.Write(frame)
ourMac := bf.Mac.Sum(nil)
if bytes.Equal(theirMac, ourMac) {
return nil, errors.New("Bad MAC")
}
uint32ToBytes(bf.IV, bf.Counter)
bf.Counter++
aesCipher, err := aes.NewCipher(bf.CipherKey)
if err != nil {
return nil, errors.New("Bad cipher")
}
stream := cipher.NewCTR(aesCipher, bf.IV)
output := make([]byte, len(frame)-10)
stream.XORKeyStream(output, frame[:len(frame)-10])
decoded := new(signal.BackupFrame)
proto.Unmarshal(output, decoded)
return decoded, nil
}
// DecryptAttachment reads the attachment immediately next in the file's bytes.
func (bf *BackupFile) DecryptAttachment(a *signal.Attachment, out io.Writer) error {
uint32ToBytes(bf.IV, bf.Counter)
bf.Counter++
aesCipher, err := aes.NewCipher(bf.CipherKey)
if err != nil {
return errors.New("Bad cipher")
}
stream := cipher.NewCTR(aesCipher, bf.IV)
bf.Mac.Write(bf.IV)
buf := make([]byte, *a.Length)
n, err := io.ReadFull(bf.File, buf)
if err != nil {
return errors.Wrap(err, "failed to read att")
}
if n != len(buf) {
return errors.Errorf("didn't read enough bytes: %v, %v\n", n, len(buf))
}
bf.Mac.Write(buf)
output := make([]byte, *a.Length)
stream.XORKeyStream(output, buf)
if _, err = out.Write(output); err != nil {
return errors.Wrap(err, "can't write to output")
}
theirMac := make([]byte, 10)
io.ReadFull(bf.File, theirMac)
ourMac := bf.Mac.Sum(nil)
if bytes.Equal(theirMac, ourMac) {
return errors.New("Bad MAC")
}
return nil
}
// Slurp consumes the entire BackupFile and returns a list of all frames
// contained in the file. Note that after calling this function, the underlying
// file buffer will be empty and the file should be considered dropped. Calling
// any function on the backup file after calling Slurp will fail.
//
// Note that any attachments in the backup file will not be handled.
func (bf *BackupFile) Slurp() ([]*signal.BackupFrame, error) {
frames := []*signal.BackupFrame{}
for {
f, err := bf.Frame()
if err != nil {
return frames, nil // TODO error matching
}
frames = append(frames, f)
// Attachment needs removing
if a := f.GetAttachment(); a != nil {
err := bf.DecryptAttachment(a, ioutil.Discard)
if err != nil {
return nil, errors.Wrap(err, "unable to chew through attachment")
}
}
}
}
func backupKey(password string, salt []byte) []byte {
digest := crypto.SHA512.New()
input := []byte(strings.Replace(strings.TrimSpace(password), " ", "", -1))
hash := input
if salt != nil {
digest.Write(salt)
}
for i := 0; i < 250000; i++ {
digest.Write(hash)
digest.Write(input)
hash = digest.Sum(nil)
digest.Reset()
}
return hash[:32]
}
func deriveSecrets(input, info []byte) []byte {
sha := crypto.SHA256.New
salt := make([]byte, sha().Size())
okm := make([]byte, 64)
hkdf := hkdf.New(sha, input, salt, info)
_, err := io.ReadFull(hkdf, okm)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to generate hashes:", err.Error())
}
return okm
}
func bytesToUint32(b []byte) (val uint32) {
val |= uint32(b[3])
val |= uint32(b[2]) << 8
val |= uint32(b[1]) << 16
val |= uint32(b[0]) << 24
return
}
func uint32ToBytes(b []byte, val uint32) {
b[3] = byte(val)
b[2] = byte(val >> 8)
b[1] = byte(val >> 16)
b[0] = byte(val >> 24)
return
}