/
encrypt.go
177 lines (159 loc) · 4.11 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
package easyecc
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"github.com/go-jose/go-jose/v3"
"golang.org/x/crypto/scrypt"
)
const (
// Key derivation parameters.
deriveKey_N = 16384
deriveKey_r = 8
deriveKey_p = 1
deriveKey_keyLen = 32
)
// encrypt encrypts the content using AES256-GCM algorithm.
func encrypt(key []byte, content []byte) ([]byte, error) {
c, err := aes.NewCipher(key[:32]) // The key must be 32 bytes long.
if err != nil {
return nil, fmt.Errorf("failed to create cipher: %v", err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, fmt.Errorf("failed to create GCM: %v", err)
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, fmt.Errorf("failed to populate nonce: %v", err)
}
return gcm.Seal(nonce, nonce, content, nil), nil
}
// decrypt decrypts the content using AES256-GCM algorithm.
func decrypt(key []byte, content []byte) ([]byte, error) {
c, err := aes.NewCipher(key[:32]) // The key must be 32 bytes long.
if err != nil {
return nil, fmt.Errorf("failed to create cipher: %v", err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, fmt.Errorf("failed to create GCM: %v", err)
}
nonceSize := gcm.NonceSize()
if len(content) < nonceSize {
return nil, fmt.Errorf("invalid content")
}
nonce, ciphertext := content[:nonceSize], content[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("failed to decrypt: %v", err)
}
return plaintext, nil
}
// makeSalt creates random 32 bytes salt.
func makeSalt() ([]byte, error) {
salt := make([]byte, 32)
_, err := rand.Read(salt)
if err != nil {
return nil, err
}
return salt, nil
}
// deriveKey creates symmetric encryption key and salt (both are 32 bytes long)
// from password.
// Key derivation algorithm is described in https://www.tarsnap.com/scrypt/scrypt.pdf.
func deriveKey(password, salt []byte) ([]byte, error) {
key, err := scrypt.Key(password, salt, deriveKey_N, deriveKey_r, deriveKey_p,
deriveKey_keyLen)
if err != nil {
return nil, err
}
return key, nil
}
func encryptJWE(key []byte, content []byte) (string, error) {
encrypter, err := jose.NewEncrypter(jose.A256GCM,
jose.Recipient{Algorithm: jose.DIRECT, Key: key}, nil)
if err != nil {
return "", err
}
object, err := encrypter.Encrypt(content)
if err != nil {
return "", err
}
return object.FullSerialize(), nil
}
func decryptJWE(key []byte, content string) ([]byte, error) {
object, err := jose.ParseEncrypted(content)
if err != nil {
return nil, err
}
decrypted, err := object.Decrypt(key)
if err != nil {
return nil, err
}
return decrypted, nil
}
func addJSONField(content string, name string, value interface{}) (string, error) {
var i interface{}
err := json.Unmarshal([]byte(content), &i)
if err != nil {
return "", err
}
m, ok := i.(map[string]interface{})
if !ok {
return "", fmt.Errorf("invalid content")
}
m[name] = value
b, err := json.Marshal(i)
if err != nil {
return "", err
}
return string(b), nil
}
func encryptWithPassphraseJWE(passphrase string, content []byte) (string, error) {
salt, err := makeSalt()
if err != nil {
return "", err
}
key, err := deriveKey([]byte(passphrase), salt)
if err != nil {
return "", err
}
s, err := encryptJWE(key, content)
if err != nil {
return "", err
}
return addJSONField(s, "x-salt", base64urlEncode(salt))
}
func decryptWithPassphraseJWE(passphrase string, content string) ([]byte, error) {
var i interface{}
err := json.Unmarshal([]byte(content), &i)
if err != nil {
return nil, err
}
m, ok := i.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid content")
}
saltObj, ok := m["x-salt"]
if !ok {
return nil, fmt.Errorf("invalid content")
}
saltStr, ok := saltObj.(string)
if !ok {
return nil, fmt.Errorf("invalid content")
}
salt, err := base64urlDecode(saltStr)
if !ok {
return nil, fmt.Errorf("invalid content")
}
key, err := deriveKey([]byte(passphrase), salt)
if err != nil {
return nil, err
}
b, err := decryptJWE(key, content)
return b, err
}