forked from hashicorp/otto
/
encrypt.go
112 lines (93 loc) · 2.42 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
package otto
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
"golang.org/x/crypto/scrypt"
)
const (
cryptPrefixV0 = "v0:"
cryptKeySaltLen = 32
)
// cryptWrite is a helper to encrypt data and then write it to a file.
// Encryption is done by using bcrypt as a KDF followed by AES-GCM.
func cryptWrite(dst string, password string, plaintext []byte) error {
keySalt := make([]byte, cryptKeySaltLen)
if _, err := rand.Read(keySalt); err != nil {
return err
}
key, err := scrypt.Key([]byte(password), keySalt, 16384, 8, 1, 32)
if err != nil {
return err
}
aesCipher, err := aes.NewCipher(key)
if err != nil {
return err
}
gcm, err := cipher.NewGCM(aesCipher)
if err != nil {
return err
}
// Compute random nonce
nonce := make([]byte, gcm.NonceSize())
if _, err := rand.Read(nonce); err != nil {
return err
}
// Encrypt and tag with GCM
out := gcm.Seal(nil, nonce, plaintext, nil)
ciphertext := make([]byte, 0, len("v0:")+len(nonce)+len(out))
ciphertext = append(ciphertext, []byte("v0:")...)
ciphertext = append(ciphertext, keySalt...)
ciphertext = append(ciphertext, nonce...)
ciphertext = append(ciphertext, out...)
out = nil
// Create the file for writing, making sure it is opened as 0600
// for a little additional security.
f, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, bytes.NewReader(ciphertext))
return err
}
func cryptRead(path string, password string) ([]byte, error) {
// Read the contents of the path first
ciphertext, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
// Verify that the data looks valid
if !bytes.HasPrefix(ciphertext, []byte(cryptPrefixV0)) {
return nil, fmt.Errorf("corrupt encrypted data")
}
// Read our key salt
ciphertext = ciphertext[len(cryptPrefixV0):]
keySalt := ciphertext[:32]
ciphertext = ciphertext[32:]
// Derive the key
key, err := scrypt.Key([]byte(password), keySalt, 16384, 8, 1, 32)
if err != nil {
return nil, err
}
// Setup the cipher
aesCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Setup the GCM AEAD
gcm, err := cipher.NewGCM(aesCipher)
if err != nil {
return nil, err
}
// Get the nonce and ciphertext out
nonce := ciphertext[:gcm.NonceSize()]
ciphertext = ciphertext[gcm.NonceSize():]
// Decrypt
return gcm.Open(nil, nonce, ciphertext, nil)
}