forked from usbarmory/armory-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
470 lines (354 loc) · 9.18 KB
/
crypto.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright (c) F-Secure Corporation
// https://foundry.f-secure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"io"
"math/big"
"sync"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/xts"
"github.com/f-secure-foundry/tamago/dma"
"github.com/f-secure-foundry/tamago/soc/imx6/dcp"
)
const (
// flag to select DCP for on supported block ciphers
DCP = true
// flag to allow DCP, when flagged, for XTS computation
DCPXTS = false
// flag to select DCP for ESSIV computation
DCPIV = false
// key derivation iteration count
PBKDF2_ITER = 4096
// DEK key derivation diversifier
DEK_DIV = "floppyDEK"
// ESSIV key derivation diversifier
ESSIV_DIV = "floppyESSIV"
// SNVS key derivation diversifier
SNVS_DIV = "floppySNVS"
)
// flag to select ESSIV on AES-128 CBC ciphers
var ESSIV bool
// IV buffer
var iv = make([]byte, aes.BlockSize)
// IV encryption IV for ESSIV computation and IV reset
var zero = make([]byte, aes.BlockSize)
var cipherFn func(buf []byte, lba int, blocks int, blockSize int, enc bool, wg *sync.WaitGroup)
func init() {
dcp.Init()
}
func deriveKey(diversifier []byte, index int, export bool) (key []byte, err error) {
if index == BLOCK_KEY {
var armoryLongterm []byte
// We want to diversify block cipher key derivation across different
// pairings, to do so we combine the diversifier with the UA long term
// public key, which is recreated at each pairing.
armoryLongterm, err = keyring.Export(UA_LONGTERM_KEY, false)
if err != nil {
return
}
diversifier = append(diversifier, armoryLongterm...)
// We re-use the ESSIV "salt" (unfortunate name collision here, it's
// not actually the PBKDF2 salt, or a salt at all) as it is random and
// unknown, the PBKDF2 salt is random but known (as it should be).
diversifier = pbkdf2.Key(keyring.salt, diversifier, PBKDF2_ITER, aes.BlockSize, sha256.New)
}
// It is advised to use only deterministic input data for key
// derivation, therefore we use the empty allocated IV before it being
// filled.
iv := make([]byte, aes.BlockSize)
if export {
key, err = dcp.DeriveKey(diversifier, iv, -1)
} else {
_, err = dcp.DeriveKey(diversifier, iv, index)
}
if err != nil {
return
}
if export {
err = dcp.SetKey(index, key)
}
return
}
func setCipher(kind Cipher, diversifier []byte) (err error) {
var dek []byte
// We need to zero out the IV buffer when switching away from ESSIV, as
// it fills its entirety.
ESSIV = false
copy(iv, zero)
switch kind {
case Cipher_AES128_CBC_PLAIN, Cipher_AES128_CBC_ESSIV:
if kind == Cipher_AES128_CBC_ESSIV {
ESSIV = true
}
if DCP {
_, err = deriveKey(diversifier, BLOCK_KEY, false)
cipherFn = cipherDCP
} else {
dek, err = deriveKey(diversifier, BLOCK_KEY, true)
if err != nil {
return
}
keyring.cb, err = aes.NewCipher(dek)
cipherFn = cipherAES
}
if err != nil {
return
}
if ESSIV && !DCPIV {
keyring.cbiv, err = aes.NewCipher(keyring.salt)
}
case Cipher_AES128_XTS_PLAIN, Cipher_AES256_XTS_PLAIN:
var size int
cbxts := aes.NewCipher
if kind == Cipher_AES256_XTS_PLAIN {
size = 32 * 2
} else {
size = 16 * 2
if DCPXTS && DCP {
cbxts = NewDCPCipher
}
}
dek, err = deriveKey(diversifier, BLOCK_KEY, true)
if err != nil {
return
}
dk := pbkdf2.Key(dek, keyring.salt, PBKDF2_ITER, size, sha256.New)
keyring.cbxts, err = xts.NewCipher(cbxts, dk)
if err != nil {
return
}
cipherFn = cipherXTS
case Cipher_NONE:
deriveKey(zero, BLOCK_KEY, false)
keyring.cb = nil
keyring.cbxts = nil
default:
err = errors.New("unsupported cipher")
}
return
}
// equivalent to aes-cbc-essiv:md5
func essiv(buf []byte, iv []byte) (err error) {
if DCPIV {
err = dcp.Encrypt(buf, ESSIV_KEY, iv)
} else {
encrypter := cipher.NewCBCEncrypter(keyring.cbiv, iv)
encrypter.CryptBlocks(buf, buf)
}
return
}
// equivalent to aes-cbc-plain (hw)
func cipherDCP(buf []byte, lba int, blocks int, blockSize int, enc bool, wg *sync.WaitGroup) {
addr, ivs := dma.Reserve(blocks*aes.BlockSize, 4)
defer dma.Release(addr)
for i := 0; i < blocks; i++ {
off := i * aes.BlockSize
// fill unused 64-bits as reserved buffers are not initialized
binary.BigEndian.PutUint64(ivs[off+8:], 0)
binary.BigEndian.PutUint64(ivs[off:], uint64(lba+i))
if ESSIV {
if err := essiv(ivs[off:], zero); err != nil {
panic(err)
}
}
}
err := dcp.CipherChain(buf, ivs, blocks, blockSize, BLOCK_KEY, enc)
if err != nil {
panic(err)
}
if wg != nil {
wg.Done()
}
}
// equivalent to aes-cbc-plain (sw)
func cipherAES(buf []byte, lba int, blocks int, blockSize int, enc bool, wg *sync.WaitGroup) {
for i := 0; i < blocks; i++ {
start := i * blockSize
end := start + blockSize
slice := buf[start:end]
binary.BigEndian.PutUint64(iv, uint64(lba+i))
if ESSIV {
if err := essiv(iv, zero); err != nil {
panic(err)
}
}
var mode cipher.BlockMode
if enc {
mode = cipher.NewCBCEncrypter(keyring.cb, iv)
} else {
mode = cipher.NewCBCDecrypter(keyring.cb, iv)
}
mode.CryptBlocks(slice, slice)
}
if wg != nil {
wg.Done()
}
}
// equivalent to aes-xts-plain64 (sw)
func cipherXTS(buf []byte, lba int, blocks int, blockSize int, enc bool, wg *sync.WaitGroup) {
for i := 0; i < blocks; i++ {
start := i * blockSize
end := start + blockSize
slice := buf[start:end]
if enc {
keyring.cbxts.Encrypt(slice, slice, uint64(lba+i))
} else {
keyring.cbxts.Decrypt(slice, slice, uint64(lba+i))
}
}
if wg != nil {
wg.Done()
}
}
func encryptSNVS(input []byte, length int) (output []byte, err error) {
block, err := aes.NewCipher(keyring.snvs)
if err != nil {
return
}
iv := rng(aes.BlockSize)
// pad to block size, accounting for IV and HMAC length
length -= len(iv) + sha256.Size
if len(input) < length {
pad := make([]byte, length-len(input))
input = append(input, pad...)
}
output = iv
mac := hmac.New(sha256.New, keyring.snvs)
mac.Write(iv)
stream := cipher.NewOFB(block, iv)
output = append(output, make([]byte, len(input))...)
stream.XORKeyStream(output[len(iv):], input)
mac.Write(output[len(iv):])
output = append(output, mac.Sum(nil)...)
return
}
func decryptSNVS(input []byte) (output []byte, err error) {
if len(input) < aes.BlockSize {
return nil, errors.New("invalid length for decrypt")
}
iv := input[0:aes.BlockSize]
input = input[aes.BlockSize:]
block, err := aes.NewCipher(keyring.snvs)
if err != nil {
return
}
mac := hmac.New(sha256.New, keyring.snvs)
mac.Write(iv)
if len(input) < mac.Size() {
return nil, errors.New("invalid length for decrypt")
}
inputMac := input[len(input)-mac.Size():]
mac.Write(input[0 : len(input)-mac.Size()])
if !hmac.Equal(inputMac, mac.Sum(nil)) {
return nil, errors.New("invalid HMAC")
}
stream := cipher.NewOFB(block, iv)
output = make([]byte, len(input)-mac.Size())
stream.XORKeyStream(output, input[0:len(input)-mac.Size()])
return
}
func encryptOFB(plaintext []byte) (ciphertext []byte, err error) {
if !remote.session {
return nil, errors.New("invalid session")
}
block, err := aes.NewCipher(keyring.sessionKey)
if err != nil {
return
}
in := bytes.NewReader(plaintext)
out := new(bytes.Buffer)
iv := rng(aes.BlockSize)
stream := cipher.NewOFB(block, iv)
reader := &cipher.StreamReader{S: stream, R: in}
_, err = io.Copy(out, reader)
if err == nil {
ciphertext = iv
ciphertext = append(ciphertext, out.Bytes()...)
}
return
}
func decryptOFB(ciphertext []byte) (plaintext []byte, err error) {
if !remote.session {
return nil, errors.New("invalid session")
}
if len(ciphertext) < aes.BlockSize {
return nil, errors.New("invalid message")
}
block, err := aes.NewCipher(keyring.sessionKey)
if err != nil {
return
}
iv := ciphertext[0:aes.BlockSize]
in := bytes.NewReader(ciphertext[aes.BlockSize:])
out := new(bytes.Buffer)
stream := cipher.NewOFB(block, iv)
writer := &cipher.StreamWriter{S: stream, W: out}
_, err = io.Copy(writer, in)
if err == nil {
plaintext = out.Bytes()
}
return
}
func signECDSA(data []byte) (sig *Signature, err error) {
var sigKey *ecdsa.PrivateKey
if remote.session {
sigKey = keyring.armoryEphemeral
} else {
sigKey = keyring.ArmoryLongterm
}
h := sha256.New()
h.Write(data)
sum := h.Sum(nil)
r, s, err := ecdsa.Sign(rand.Reader, sigKey, sum)
if err != nil {
return
}
sig = &Signature{
Data: sum,
R: r.Bytes(),
S: s.Bytes(),
}
return
}
func verifyECDSA(data []byte, sig *Signature) (err error) {
var verKey *ecdsa.PublicKey
if remote.session {
verKey = keyring.mobileEphemeral
} else {
verKey = keyring.MobileLongterm
}
h := sha256.New()
h.Write(data)
if !bytes.Equal(sig.Data, h.Sum(nil)) {
return errors.New("signature error, data mismatch")
}
R := big.NewInt(0)
S := big.NewInt(0)
R.SetBytes(sig.R)
S.SetBytes(sig.S)
valid := ecdsa.Verify(verKey, sig.Data, R, S)
if !valid {
return errors.New("signature error, invalid")
}
return
}
func rng(n int) []byte {
buf := make([]byte, n)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
return buf
}