-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (164 loc) · 4.62 KB
/
main.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
// Read and write asynchronously to encrypted files.
package rwcipher
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"fmt"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/ssh/terminal"
"io"
"io/ioutil"
"os"
)
// Returns a password string
type PasswordReader interface {
ReadPassword() ([]byte, error)
}
type StdInPasswordReader struct {
}
func (spr StdInPasswordReader) ReadPassword() ([]byte, error) {
pwd, error := terminal.ReadPassword(int(os.Stdin.Fd()))
return pwd, error
}
// Read an encrypted file
// enc is the encrypted file path
// dst is the decrypted file path destination
// pr is only used for testing, and should be nil
func Decrypt(enc string, dst string, pr PasswordReader) (err error) {
if pr == nil {
pr = StdInPasswordReader{}
}
pw, err := getPassword(enc, pr)
if err != nil {
return
}
// Read from src
ciphertext, err := ioutil.ReadFile(enc)
if err != nil {
return errors.New(fmt.Sprintf("Error Reading Ciphertext: %v", err))
}
plaintext, err := decBytes(ciphertext, pw)
if err != nil {
return
}
// Write to dst
err = ioutil.WriteFile(dst, plaintext, 0644)
if err != nil {
return errors.New(fmt.Sprintf("Error writing decrypted file to disk: %v", err))
}
return nil
}
// Encrypt a plaintext file with a password from stdin
// plain is the plaintext file path
// dst is the destination filepath for the encrypted file
// pr is only used for testing, and should be nil
func Encrypt(plain string, dst string, pr PasswordReader) (err error) {
if pr == nil {
pr = StdInPasswordReader{}
}
pw, err := getPassword(plain, pr)
if err != nil {
return
}
plaintext, err := ioutil.ReadFile(plain)
if err != nil {
return errors.New(fmt.Sprintf("Error Reading File to Encrypt: %v", err))
}
ciphertext, err := encBytes(plaintext, pw)
if err != nil {
return
}
// Write out
err = ioutil.WriteFile(dst, ciphertext, 0444)
if err != nil {
return errors.New(fmt.Sprintf("Error writing encrypted file to disk: %v", err))
}
return nil
}
func encBytes(plaintext []byte, pw []byte) (ciphertext []byte, err error) {
//Choose a random nonce
nonce := make([]byte, 12)
_, err = io.ReadFull(rand.Reader, nonce)
if err != nil {
return nil, err
}
//Choose a random salt
salt := make([]byte, 16)
_, err = io.ReadFull(rand.Reader, salt)
if err != nil {
return nil, err
}
key := (stretchPasswordToAES256Key(pw, salt))
keyptr := (([32]byte)(key))
ciphertext, err = encryptGCM(plaintext, &keyptr, nonce)
if err != nil {
ciphertext = nil
return
}
// Save the salt for decryption
ciphertext = append(ciphertext, salt...)
return
}
func decBytes(ciphertext []byte, pw []byte) (plaintext []byte, err error) {
// Recover salt from end of file
salt := ciphertext[len(ciphertext)-16:]
ciphertext = ciphertext[:len(ciphertext)-16]
key := (stretchPasswordToAES256Key(pw, salt))
keyptr := (([32]byte)(key))
plaintext, err = decryptGCM(ciphertext, &keyptr)
if err != nil {
return []byte(""), errors.New(fmt.Sprintf("Decryption Failed: %v", err))
}
return
}
func stretchPasswordToAES256Key(pw []byte, salt []byte) []byte {
return argon2.IDKey(pw, salt, 1, 64*1024, 4, 32)
}
// Get password bytes from user
func getPassword(filename string, pr PasswordReader) (pwd []byte, err error) {
fmt.Println("Enter password for " + filename)
pwd, err = pr.ReadPassword()
if err != nil {
return []byte(""), err
}
return pwd, nil
}
// The following is from https://github.com/gtank/cryptopasta/blob/master/encrypt.go
// It is in the public domain
// Encrypt encrypts data using 256-bit AES-GCM. This both hides the content of
// the data and provides a check that it hasn't been altered. Output takes the
// form nonce|ciphertext|tag where '|' indicates concatenation.
func encryptGCM(plaintext []byte, key *[32]byte, nonce []byte) (ciphertext []byte, err error) {
block, err := aes.NewCipher(key[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}
// Decrypt decrypts data using 256-bit AES-GCM. This both hides the content of
// the data and provides a check that it hasn't been altered. Expects input
// form nonce|ciphertext|tag where '|' indicates concatenation.
func decryptGCM(ciphertext []byte, key *[32]byte) (plaintext []byte, err error) {
block, err := aes.NewCipher(key[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(ciphertext) < gcm.NonceSize() {
return nil, errors.New("malformed ciphertext")
}
return gcm.Open(nil,
ciphertext[:gcm.NonceSize()],
ciphertext[gcm.NonceSize():],
nil,
)
}