-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
72 lines (60 loc) · 1.73 KB
/
writer.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
package blobcrypt
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
)
const (
defaultBufferSize = 16384
)
// Writer encrypts the contents of an underlying io.ReadSeeker.
type Writer struct {
Source io.ReadSeeker
Key []byte
}
// NewWriter creates a writer that encrypts source using key.
func NewWriter(source io.ReadSeeker, key []byte) (*Writer, error) {
if len(key) != sha256.Size {
return nil, fmt.Errorf("Key size is incorrect")
}
return &Writer{Source: source, Key: key}, nil
}
// Encrypt encrypts the contents of the receiver to the output stream.
// On successful return, Writer's HMAC will be set to the HMAC of the output.
func (w *Writer) Encrypt(output io.Writer) ([]byte, error) {
blockCipher, err := aes.NewCipher(w.Key)
if err != nil {
return nil, err
}
iv := shaSlice256(w.Key)
hmacKey := shaSlice256(iv)
// Configure a cancelable context, ensuring goroutines won't be leaked on early return.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cipherStream := CipherStream{
Source: w.Source,
Cipher: cipher.NewCTR(blockCipher, iv[:blockCipher.BlockSize()]),
}
// Encrypt input file in parallel with output, and calculate HMAC as we go.
mac := hmac.New(sha512.New, hmacKey)
for buf := range cipherStream.Stream(ctx) {
// According to documentation, Hash.Write never returns an error.
mac.Write(buf)
if _, err := output.Write(buf); err != nil {
return nil, err
}
}
// If cipherStream exited abnormally due to a read error, return it
if err := cipherStream.Error; err != nil {
return nil, err
}
// Otherwise, write the HMAC suffix
hmacFinal := mac.Sum(nil)
_, err = output.Write(hmacFinal)
return hmacFinal, err
}