-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
629 lines (522 loc) · 16.4 KB
/
encrypt.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package minileap
import (
"bufio"
"crypto/rand"
"fmt"
"hash"
"io"
"os"
"path/filepath"
"strings"
"github.com/cathalgarvey/base58"
"github.com/cryptag/go-minilock/taber"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/ssh/terminal"
)
func init() {
if os.Getenv("DEBUG") == "1" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.FatalLevel)
}
}
const (
NonceLength = 24
// EncryptChunkLength tells us how big a chunk should be encrypted
// at once.
EncryptChunkLength = 100_000
Blake2bHashLength = blake2b.Size
// 104 == 24 + 16 + 64. Does not include IsLastChunkIndicatorLength.
NonceCryptoBlakeOverhead = NonceLength + secretbox.Overhead + Blake2bHashLength
// 2-byte message type (big endian)
EncryptHeaderLength = 2
DecryptHeaderLength = EncryptHeaderLength + NonceCryptoBlakeOverhead
IsLastChunkIndicatorLength = 1
// Use DecryptChunkLength when decrypting
DecryptChunkOverhead = IsLastChunkIndicatorLength + NonceCryptoBlakeOverhead
ValidKeyLength = 32
MiniLeapFileExtension = "minileap"
MiniLeapFileExtensionIncludingDot = "." + MiniLeapFileExtension
EncryptFilenameChunkLength = 256
)
const (
MaxMsgType = uint16(1<<16 - 1) // 65,535
MinFilenameLength = 1
MaxFilenameLength = 255
//
// Message types
//
MessageTypeInvalid = uint16(0)
MessageTypeText = uint16(1)
MessageTypeURL = uint16(2)
MessageTypeCommand = uint16(3)
MessageTypePassphrase = uint16(4)
MessageTypeFileWithFilename = uint16(5)
MessageTypeFileWithFilenameAndPath = uint16(6)
)
var empty32ByteArray = [32]byte{}
func MessageTypeName(msgType uint16) string {
switch msgType {
case MessageTypeInvalid:
return fmt.Sprintf("MessageTypeInvalid (number %v)", msgType)
case MessageTypeText:
return fmt.Sprintf("MessageTypeText (number %v)", msgType)
case MessageTypeURL:
return fmt.Sprintf("MessageTypeURL (number %v)", msgType)
case MessageTypeCommand:
return fmt.Sprintf("MessageTypeCommand (number %v)", msgType)
case MessageTypePassphrase:
return fmt.Sprintf("MessageTypePassphrase (number %v)", msgType)
case MessageTypeFileWithFilename:
return fmt.Sprintf("MessageTypeFileWithFilename (number %v)", msgType)
case MessageTypeFileWithFilenameAndPath:
return fmt.Sprintf("MessageTypeFileWithFilenameAndPath (number %v)", msgType)
}
return fmt.Sprintf("Unknown (number %v)", msgType)
}
var (
ErrInvalidNonce = fmt.Errorf("Invalid nonce")
ErrInvalidKey = fmt.Errorf("Invalid key")
ErrInvalidKeyLength = fmt.Errorf("Invalid key length")
ErrInvalidChunkLength = fmt.Errorf("Header: invalid chunk length")
ErrInvalidMessageType = fmt.Errorf("Header: invalid message type")
ErrChunkDecryptionFailed = fmt.Errorf("Chunk decryption failed")
ErrInvalidChunkHash = fmt.Errorf("Invalid chunk hash")
ErrInvalidFinalHash = fmt.Errorf("Invalid final hash")
ErrInvalidEncryptConfig = fmt.Errorf("Invalid encryption configuration options")
ErrInvalidFilenameLength = fmt.Errorf("Invalid filename length")
ErrInvalidAccountID = fmt.Errorf("Invalid account ID; must be valid base58 and of length 33 after being decoded")
// Don't let an attacker who's sending me a file control which
// directory it ends up in
InvalidFilenameChars = []string{`/`, `\`}
TestKey = &[ValidKeyLength]byte{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1,
}
)
type EncryptionConfig struct {
NoncePrefix []byte // {:constraints [ $(v.len in [0 12 18]) ]}
// These fields are in common with `minileap.Message`
BlobID BlobID
MsgType uint16
OrigFilename string
SavedLocation string
PlainFile io.Writer
Blake hash.Hash
}
func (config *EncryptionConfig) SavedAs() string {
if config == nil {
return "(*EncryptionConfig is nil)"
}
if config.SavedLocation != "" {
return config.SavedLocation
}
return config.OrigFilename
}
func EncryptFile(plainFilename string, key *[32]byte, dest string, forceOverwrite bool) (cipherFilename string, err error) {
if key == nil || *key == empty32ByteArray {
return "", ErrInvalidKey
}
basePlainFilename := filepath.Base(plainFilename)
for _, char := range InvalidFilenameChars {
if strings.Contains(basePlainFilename, char) {
return "", fmt.Errorf("Filename `%s` contains invalid character `%s`!",
basePlainFilename, char)
}
}
if dest == "" {
// Save encrypted file with ".minileap" extension appended
cipherFilename = plainFilename + MiniLeapFileExtensionIncludingDot
} else if exists, err := DirExists(dest); exists && err != nil {
cipherFilename = filepath.Clean(dest) + string(filepath.Separator) + basePlainFilename + MiniLeapFileExtensionIncludingDot
} else {
cipherFilename = dest
}
exists, err := FileExists(cipherFilename)
if err != nil {
return "", err
}
if exists && !forceOverwrite {
return cipherFilename, fmt.Errorf("Encrypted file `%s` already exists and you've chosen not to overwrite existing files!", cipherFilename)
}
plainFile, err := os.Open(plainFilename)
if err != nil {
return cipherFilename, err
}
defer plainFile.Close()
cipherFile, err := os.Create(cipherFilename)
if err != nil {
return cipherFilename, err
}
defer cipherFile.Close()
headerNoncePrefix, blobID, err := NewBlob()
if err != nil {
return cipherFilename, err
}
encConfig := &EncryptionConfig{
NoncePrefix: headerNoncePrefix[:],
BlobID: blobID,
OrigFilename: filepath.Base(plainFilename),
MsgType: MessageTypeFileWithFilename,
}
err = EncryptReaderToWriter(MessageTypeFileWithFilename, plainFile, key, cipherFile, encConfig)
if err != nil {
return cipherFilename, err
}
return cipherFilename, nil
}
func EncryptReaderToWriter(msgType uint16, plainFile io.Reader, key *[32]byte, cipherFile io.Writer, encConfig *EncryptionConfig) error {
if key == nil || *key == empty32ByteArray {
return ErrInvalidKey
}
if msgType == MessageTypeFileWithFilename && (encConfig == nil || encConfig.OrigFilename == "") {
return fmt.Errorf("Must specify original filename when encrypting file: %w",
ErrInvalidEncryptConfig)
}
// if encConfig == nil {
// encConfig = &EncryptionConfig{}
// }
if err := ValidNoncePrefix(encConfig.NoncePrefix); err != nil {
return err
}
blake, err := blake2b.New512(nil)
if err != nil {
return err
}
//
// Header: Generate it, encrypt it, hash it
//
header, err := NewHeader(msgType)
if err != nil {
return err
}
noncePlusEncryptedHeaderPlusHash, err := EncryptAndHashChunk(encConfig.NoncePrefix, header, key, blake)
if err != nil {
return err
}
encConfig.NoncePrefix = nil
log.Debugf("EncryptReaderToWriter: header created, encrypted, and hashed successfully (though not yet written)")
// Write this below, both for efficiency's sake, and to not reveal
// much about the structure of the files we're sending
firstChunkEnc := noncePlusEncryptedHeaderPlusHash
//
// Encrypt filename if we are encrypting a file (and, thus, there is one)
//
if msgType == MessageTypeFileWithFilename {
filenameChunk, err := NewFilenameChunk(encConfig.OrigFilename)
if err != nil {
return err
}
noncePlusFilename, err := EncryptAndHashChunk(encConfig.NoncePrefix, filenameChunk, key, blake)
if err != nil {
return err
}
firstChunkEnc = append(firstChunkEnc, noncePlusFilename...)
// FALL THROUGH
}
//
// Encrypt and hash each chunk
//
isFirstChunkWritten := false
var plainb [EncryptChunkLength]byte
// staged chunk consisting of IsLastChunkByte + plain chunk
var staged []byte
// Closure helper ftw
encryptAndWriteStaged := func() error {
noncePlusEncryptedChunkPlusHash, err := EncryptAndHashChunk(encConfig.NoncePrefix, staged, key, blake)
if err != nil {
return err
}
if !isFirstChunkWritten {
// Write the header chunk + filename chunk (if exists) +
// first data chunk all at once
noncePlusEncryptedChunkPlusHash = append(firstChunkEnc,
noncePlusEncryptedChunkPlusHash...)
}
log.Debugf("Writing chunk; first == %v, length == %v",
!isFirstChunkWritten, len(noncePlusEncryptedChunkPlusHash))
_, err = cipherFile.Write(noncePlusEncryptedChunkPlusHash)
if err != nil {
return err
}
staged = nil
isFirstChunkWritten = true
return nil
}
// Loop till 0 bytes read. Fuck EOF, which only complicates things
for {
n, err := plainFile.Read(plainb[:])
if err != nil && err != io.EOF {
return err
}
if n > 0 {
if len(staged) > 0 {
// We have new data _and_ staged data, so set
// `staged` as _not_ the last chunk, then encrypt
// and write it.
staged[0], err = IsLastChunkBoolToByte(false)
if err != nil {
return err
}
err = encryptAndWriteStaged()
if err != nil {
return err
}
}
// We have new data and no staged data. This may be
// the first iteration, and the current chunk may or
// not be the last one. Let's prepend a dummy value,
// then stage the chunk.
staged = append([]byte{0}, plainb[:n]...)
continue
}
if n == 0 && len(staged) > 0 {
// We have no new data but we _do_ have staged
// data. Therefore what is staged is the last
// chunk. So let's mark it as such, then encrypt
// and write it.
staged[0], err = IsLastChunkBoolToByte(true)
if err != nil {
return err
}
err = encryptAndWriteStaged()
if err != nil {
return err
}
break
}
}
return nil
}
func NewHeader(msgType uint16) ([]byte, error) {
if msgType == MessageTypeInvalid || msgType > MaxMsgType {
return nil, ErrInvalidMessageType
}
// Big endian
header := []byte{
// Message type
byte(msgType >> 8),
byte(msgType),
}
// assert len(header) == 2
return header, nil
}
// NewFilenameChunk returns a []byte of length 256 with the following
// structure: `[ 1 byte: length of original filename || N bytes:
// original filename || 255 - N bytes: random data (padding) ]`
func NewFilenameChunk(origFilename string) ([]byte, error) {
// Must convert to []byte first to correctly support UTF-8;
// len(str) counts runes, not bytes
lenOrigFilename := len([]byte(origFilename))
if lenOrigFilename < MinFilenameLength || lenOrigFilename > MaxFilenameLength {
return nil, fmt.Errorf("origFilename may not be %v: %w",
lenOrigFilename, ErrInvalidFilenameLength)
}
// [ 0 0 0 0 0 ... ]
filenameChunk := make([]byte, EncryptFilenameChunkLength)
// [ 14 0 0 0 0 ... ] where 14 == lenOrigFilename
filenameChunk[0] = byte(lenOrigFilename)
// [ 14 m y f i l e n a m e . t x t 0 0 0 0 0 ... ] where
// origFilename == "myfilename.txt"
n := copy(filenameChunk[1:], []byte(origFilename))
if n != lenOrigFilename {
return nil, fmt.Errorf("Only copied %v bytes of origFilename, not %v!", n, lenOrigFilename)
}
// [ 14 m y f i l e n a m e . t x t R A N D O M D A T A H E R E ... ]
n, err := rand.Reader.Read(filenameChunk[1+lenOrigFilename:])
if err != nil {
return nil, err
}
nWanted := EncryptFilenameChunkLength - lenOrigFilename - 1
if n != nWanted {
return nil, fmt.Errorf("Only read %v random bytes, not %v!", n, nWanted)
}
return filenameChunk, nil
}
// EncryptAndHashChunk encrypts and hashes the given data. Unless you
// are encrypting a miniLeap header or filename, the first argument
// should consist of the data you want to encrypt prefixed by an
// "isLastChunk" byte.
func EncryptAndHashChunk(noncePrefix []byte, isLastChunkBytePlusPlain []byte, key *[ValidKeyLength]byte, blake hash.Hash) ([]byte, error) {
isLastChunkBytePlusPlainLen := len(isLastChunkBytePlusPlain)
if isLastChunkBytePlusPlainLen == 0 {
return nil, fmt.Errorf("Cannot encrypt empty chunk")
}
if key == nil || *key == empty32ByteArray {
return nil, ErrInvalidKey
}
if err := ValidNoncePrefix(noncePrefix); err != nil {
return nil, err
}
nonce, err := RandomNonce()
if err != nil {
return nil, fmt.Errorf("Error generating nonce: %s", err)
}
// Overwrite beginning of `nonce` with (potentially empty) `noncePrefix`
copy((*nonce)[:], noncePrefix)
nonceSlice := (*nonce)[:]
cipherCapacity := isLastChunkBytePlusPlainLen + NonceCryptoBlakeOverhead
// Start with length 0 and append from there so it's hard to
// accidentally access the trailing zeroes belowsef, brosef
cipher := make([]byte, 0, cipherCapacity)
// Note: `blake.Write(...)` never returns error, as per `hash.Hash` spec
blake.Write((*key)[:])
cipher = append(cipher, nonceSlice...)
cipher = secretbox.Seal(cipher, isLastChunkBytePlusPlain, nonce, key)
blake.Write(cipher)
blakeSum := blake.Sum(nil)
cipher = append(cipher, blakeSum...)
blake.Write(blakeSum) // It's almost like... a chain of blocks...
if len(cipher) != cap(cipher) {
return nil, fmt.Errorf("EncryptAndHashChunk: ciphertext is of length "+
"%v but should be %v; something went wrong!", len(cipher), cap(cipher))
}
return cipher, nil
}
// IsLastChunkBoolToByte takes the given bool and turns it into a
// valid indicator that this is the last chunk if isLastChunk is true,
// or an indicator that this is not the last chunk if isLastChunk is
// false. (`isLastChunk == false` results in the returned byte being
// even, and `isLastChunk == true` results in the returned byte being
// odd.)
func IsLastChunkBoolToByte(isLastChunk bool) (byte, error) {
randByte, err := RandByte()
if err != nil {
return 0, err
}
if isLastChunk {
// Ensure odd
return randByte | 1, nil
}
// Ensure even
return randByte & 0xFE, nil
}
func RandByte() (byte, error) {
b := make([]byte, 1)
_, err := rand.Read(b)
if err != nil {
return 0, err
}
return b[0], nil
}
// From https://www.tutorialspoint.com/how-to-check-if-a-file-exists-in-golang
func FileExists(filename string) (bool, error) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return !info.IsDir(), nil
}
func DirExists(dir string) (bool, error) {
info, err := os.Stat(dir)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return info.IsDir(), nil
}
func MustGetFromStdinSecure() string {
input, err := ReadPassword()
if err != nil {
exit(err)
}
fmt.Fprintf(os.Stderr, "\n")
return input
}
func exit(err error) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func ReadPassword() (string, error) {
inputb, err := terminal.ReadPassword(int(os.Stdin.Fd()))
return string(inputb), err
}
func MustGetFromStdinStripped() string {
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
exit(err)
}
return strings.TrimRight(text, "\r\n")
}
// RandomNonce generates and returns a new random nonce. RandomNonce
// guarantees that the returned nonce is not nil and is fully
// populated.
func RandomNonce() (*[NonceLength]byte, error) {
var b [NonceLength]byte
n, err := rand.Reader.Read(b[:])
if err != nil {
return nil, err
}
if n != NonceLength {
return nil, fmt.Errorf("Only read %v random bytes, not %v!",
n, NonceLength)
}
return &b, nil
}
func ConvertNonce(nonce []byte) (goodNonce *[NonceLength]byte, err error) {
if len(nonce) != NonceLength {
return nil, ErrInvalidNonce
}
var b [NonceLength]byte
n := copy(b[:], nonce[:])
if n != NonceLength {
return nil, fmt.Errorf("Error converting nonce; got %v bytes, wanted %v",
n, NonceLength)
}
return &b, nil
}
func ConvertKey(key []byte) (goodKey *[ValidKeyLength]byte, err error) {
if len(key) != ValidKeyLength {
return nil, fmt.Errorf("Invalid key; must be of length %d, has length %d",
ValidKeyLength, len(key))
}
// []byte -> *[ValidKeyLength]byte
var good [ValidKeyLength]byte
n := copy(good[:], key)
if n != ValidKeyLength {
return nil, ErrInvalidKeyLength
}
return &good, nil
}
func Base58DecodeAccountID(accountID string) ([]byte, error) {
signPubKey, err := base58.StdEncoding.Decode([]byte(accountID))
if err != nil {
return nil, err
}
if len(signPubKey) != 32 {
return nil, fmt.Errorf("Decoded account ID's length is %v, should be 32",
len(signPubKey))
}
return signPubKey, nil
}
func AccountIDToCurve25519(accountID string) ([]byte, error) {
signPubKey, err := Base58DecodeAccountID(accountID)
if err != nil {
return nil, err
}
curvePub, err := PublicEd25519ToCurve25519(signPubKey)
if err != nil {
return nil, err
}
return curvePub, nil
}
func MustWipeKeys(keyPair *taber.Keys, keyPairPrivate32 *[32]byte) {
err := keyPair.Wipe()
err2 := taber.WipeKeyArray(keyPairPrivate32)
if err != nil {
log.Fatalf("Error wiping keyPair: %v\n", err)
}
if err2 != nil {
log.Fatalf("Error wiping keyPairPrivate32: %v\n", err2)
}
}