Repository navigation
Expand file tree
/
Copy pathencryption.go
More file actions
289 lines (237 loc) · 7.33 KB
/
Copy pathencryption.go
File metadata and controls
289 lines (237 loc) · 7.33 KB
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
package password
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"fmt"
"golang.org/x/crypto/argon2"
"strings"
"time"
"unicode/utf8"
)
const saltLength = 32
const entropyBlockLength = 24
const paddingBlockLength = 16
const timeFormat = "2006-01-02T15:04:05-07:00"
// HashFunc is a function signature.
// The Hash function will be called for password and secret hashing.
type HashFunc func(data []byte, salt []byte) [32]byte
// Hash will calculate a 32 byte hash from a given byte slice.
// It is used for password and secret hashing.
// You can overwrite it with any function that meets the HashFunc signature.
// By default, it is set to a variant of argon2.Key.
var Hash HashFunc = argon2iHash
func sha256Hash(data []byte, salt []byte) [32]byte {
temp := make([]byte, 0, len(data)+len(salt))
copy(temp, salt)
temp = append(temp, data...)
return sha256.Sum256(temp)
}
const argon2iMemory uint32 = 32
const argon2iTime uint32 = 3
const argon2iThreads uint8 = 4
func argon2iHash(data []byte, salt []byte) [32]byte {
return [32]byte(argon2.Key(data, salt, argon2iTime, argon2iMemory*1024, argon2iThreads, 32))
}
// getHashedPassword returns random salt + hash with base64 encoding.
func getHashedPassword(password string) (string, error) {
salt := make([]byte, saltLength)
_, err := rand.Read(salt)
if err != nil {
return "", err
}
hash := Hash([]byte(password), salt)
hashedPassword := append(salt, hash[:]...)
return base64.StdEncoding.EncodeToString(hashedPassword), nil
}
// compareHashedPassword compares a hashed password A against a password B, by performing the hash operation on B with the same salt from A.
// The input hashedPassword must be base64 encoded.
func compareHashedPassword(hashedPassword string, password string) (bool, error) {
// decode hashed password
data1, err := base64.StdEncoding.DecodeString(hashedPassword)
if err != nil {
return false, err
}
// extract salt
if len(data1) < saltLength {
return false, fmt.Errorf("hashed password is too short")
}
salt := make([]byte, saltLength)
copy(salt, data1[:saltLength])
// hash other password
hash := Hash([]byte(password), salt)
data2 := append(salt, hash[:]...)
// compare
result := subtle.ConstantTimeCompare(data1, data2) == 1
return result, nil
}
// comparePassword compares two passwords with constant time.
func comparePassword(password1 string, password2 string) bool {
hash1 := sha256.Sum256([]byte(password1))
hash2 := sha256.Sum256([]byte(password2))
return subtle.ConstantTimeCompare(hash1[:], hash2[:]) == 1
}
// packData encodes a given id and data string to json with entropy, padding and additional metadata.
func packData(id string, data string) (string, error) {
if !utf8.ValidString(id) {
return "", fmt.Errorf("invalid utf8 character in packData")
}
if !utf8.ValidString(data) {
return "", fmt.Errorf("invalid utf8 character in packData")
}
paddingLength := paddingBlockLength - (len(data) % paddingBlockLength) + 1
entropy := make([]byte, entropyBlockLength)
_, err := rand.Read(entropy)
if err != nil {
return "", err
}
temp := new(bytes.Buffer)
enc := json.NewEncoder(temp)
enc.SetEscapeHTML(false)
enc.SetIndent("", "")
err = enc.Encode(map[string]interface{}{
"id": id,
"data": data,
"padding": strings.Repeat(" ", paddingLength),
"entropy": base64.StdEncoding.EncodeToString(entropy),
"timestamp": time.Now().Format(timeFormat),
})
if err != nil {
return "", err
}
return strings.ReplaceAll(temp.String(), "\n", ""), nil
}
// unpackData decodes a given json string to its id and data representation.
func unpackData(input string) (string, string, error) {
if !utf8.ValidString(input) {
return "", "", fmt.Errorf("invalid utf8 character in unpackData")
}
dec := json.NewDecoder(strings.NewReader(input))
dec.DisallowUnknownFields()
temp := make(map[string]interface{})
err := dec.Decode(&temp)
if err != nil {
return "", "", err
}
id, ok := temp["id"].(string)
if !ok {
return "", "", fmt.Errorf("id field not found in unpackData")
}
data, ok := temp["data"].(string)
if !ok {
return "", "", fmt.Errorf("data field not found in unpackData")
}
_, ok = temp["padding"].(string)
if !ok {
return "", "", fmt.Errorf("padding field not found in unpackData")
}
_, ok = temp["entropy"].(string)
if !ok {
return "", "", fmt.Errorf("entropy field not found in unpackData")
}
_, ok = temp["timestamp"].(string)
if !ok {
return "", "", fmt.Errorf("timestamp field not found in unpackData")
}
return id, data, nil
}
// Encrypt a given text with AES256 and return a base64 representation.
// The secret is hashed with the custom Hash function.
// Galois Counter Mode is used.
// The nonce is stored as a prefix of the ciphertext.
func Encrypt(text string, secret string) (string, error) {
// create salt
salt := make([]byte, saltLength)
_, err := rand.Read(salt)
if err != nil {
return "", err
}
// hash secret
secretHash := Hash([]byte(secret), salt)
// prepare cipher
block, err := aes.NewCipher(secretHash[:])
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
// create nonce
nonce := make([]byte, gcm.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
return "", err
}
// encrypt
encrypted := gcm.Seal(nil, nonce, []byte(text), nil)
saltAndNonce := append(salt, nonce...)
cipherBytes := append(saltAndNonce, encrypted...)
return base64.StdEncoding.EncodeToString(cipherBytes), nil
}
// Decrypt a given ciphertext in base64 representation with AES256.
// The secret is hashed with the custom Hash function.
// Galois Counter Mode is used.
// The nonce is retrieved as a prefix of the ciphertext.
func Decrypt(ciphertext string, secret string) (string, error) {
// extract salt
cipherBytes, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return "", err
}
if len(cipherBytes) < saltLength {
return "", fmt.Errorf("ciphertext is too short")
}
salt := cipherBytes[:saltLength]
cipherBytes = cipherBytes[saltLength:]
// hash secret
secretHash := Hash([]byte(secret), salt)
// prepare cipher
block, err := aes.NewCipher(secretHash[:])
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
// extract nonce
if len(cipherBytes) < gcm.NonceSize() {
return "", fmt.Errorf("ciphertext is too short")
}
nonce := cipherBytes[:gcm.NonceSize()]
msg := cipherBytes[gcm.NonceSize():]
// decrypt
textBytes, err := gcm.Open(nil, nonce, msg, nil)
if err != nil {
return "", err
}
if !utf8.Valid(textBytes) {
return "", fmt.Errorf("invalid utf8 character after decryption")
}
text := string(textBytes)
return text, nil
}
// EncryptOTP returns a One-Time-Pad (OTP) encrypted message and its OTP secret.
func EncryptOTP(text string) ([]byte, []byte) {
secret := make([]byte, len(text))
_, _ = rand.Read(secret)
cipherBytes := make([]byte, len(text))
for i := 0; i < len(text); i++ {
cipherBytes[i] = secret[i] ^ text[i]
}
return cipherBytes, secret
}
// DecryptOTP returns the decrypted message from a One-Time-Pad (OTP) encryption.
func DecryptOTP(cipherBytes []byte, secret []byte) string {
textBytes := make([]byte, min(len(cipherBytes), len(secret)))
for i := 0; i < len(textBytes); i++ {
textBytes[i] = cipherBytes[i] ^ secret[i]
}
return string(textBytes)
}