-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.go
More file actions
366 lines (336 loc) · 9.43 KB
/
crypt.go
File metadata and controls
366 lines (336 loc) · 9.43 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package crypt
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/goforj/godump"
"io"
"os"
"strings"
)
var jsonMarshal = json.Marshal
// GenerateAppKey generates a random base64 app key prefixed with "base64:".
// @group Key management
// @behavior readonly
//
// Example: generate an AES-256 key
//
// key, _ := crypt.GenerateAppKey()
// godump.Dump(key)
//
// // #string "base64:..."
func GenerateAppKey() (string, error) {
key := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
return "", err
}
encoded := base64.StdEncoding.EncodeToString(key)
return "base64:" + encoded, nil
}
// GetAppKey retrieves the APP_KEY from the environment and parses it.
// @group Key management
// @behavior readonly
//
// Example: read APP_KEY and ensure the correct size
//
// keyStr, _ := crypt.GenerateAppKey()
// _ = os.Setenv("APP_KEY", keyStr)
// key, err := crypt.GetAppKey()
// godump.Dump(len(key), err)
//
// // #int 32
// // #error <nil>
func GetAppKey() ([]byte, error) {
key := os.Getenv("APP_KEY")
if key == "" {
return nil, errors.New("APP_KEY is not set in environment")
}
return ReadAppKey(key)
}
// GetPreviousAppKeys retrieves and parses APP_PREVIOUS_KEYS from the environment.
// Keys are expected to be comma-delimited and prefixed with "base64:".
// @group Key management
// @behavior readonly
//
// Example: parse two previous keys (mixed AES-128/256)
//
// k1, _ := crypt.GenerateAppKey()
// k2, _ := crypt.GenerateAppKey()
// _ = os.Setenv("APP_PREVIOUS_KEYS", k1+", "+k2)
// keys, err := crypt.GetPreviousAppKeys()
// godump.Dump(len(keys), err)
//
// // #int 2
// // #error <nil>
func GetPreviousAppKeys() ([][]byte, error) {
previous := strings.TrimSpace(os.Getenv("APP_PREVIOUS_KEYS"))
if previous == "" {
return nil, nil
}
parts := strings.Split(previous, ",")
keys := make([][]byte, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
key, err := ReadAppKey(part)
if err != nil {
return nil, fmt.Errorf("failed to parse APP_PREVIOUS_KEYS: %w", err)
}
keys = append(keys, key)
}
return keys, nil
}
// ReadAppKey parses a base64 encoded app key with "base64:" prefix.
// Accepts 16-byte keys (AES-128) or 32-byte keys (AES-256) after decoding.
// @group Key management
// @behavior readonly
//
// Example: parse AES-128 and AES-256 keys
//
// key128raw := make([]byte, 16)
// _, _ = rand.Read(key128raw)
// key128str := "base64:" + base64.StdEncoding.EncodeToString(key128raw)
//
// key256str, _ := crypt.GenerateAppKey()
//
// key128, _ := crypt.ReadAppKey(key128str)
// key256, _ := crypt.ReadAppKey(key256str)
// godump.Dump(len(key128), len(key256))
//
// // #int 16
// // #int 32
func ReadAppKey(key string) ([]byte, error) {
const prefix = "base64:"
if len(key) < len(prefix) || key[:len(prefix)] != prefix {
return nil, fmt.Errorf("unsupported or missing key prefix")
}
decoded, err := base64.StdEncoding.DecodeString(key[len(prefix):])
if err != nil {
return nil, err
}
if len(decoded) != 32 && len(decoded) != 16 {
return nil, fmt.Errorf("key must be 16 or 32 bytes after decoding")
}
return decoded, nil
}
// pkcs7Pad pads data to a multiple of blockSize using PKCS#7 semantics.
// @group Padding
// @behavior pure
//
// Example: pad short block
//
// p := crypt.pkcs7Pad([]byte("abc"), 4)
// godump.Dump(p)
//
// // #[]uint8 [
// // 0 => 97 #uint8
// // 1 => 98 #uint8
// // 2 => 99 #uint8
// // 3 => 1 #uint8
// // ]
func pkcs7Pad(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padText...)
}
// pkcs7Unpad removes PKCS#7 padding from data.
// @group Padding
// @behavior readonly
//
// Example: unpad valid data
//
// out, _ := crypt.pkcs7Unpad([]byte{97, 98, 99, 1})
// godump.Dump(string(out))
//
// // #string "abc"
func pkcs7Unpad(data []byte) ([]byte, error) {
if len(data) == 0 {
return nil, errors.New("invalid padding size")
}
padding := data[len(data)-1]
if int(padding) > len(data) || padding == 0 {
return nil, errors.New("invalid padding")
}
for _, b := range data[len(data)-int(padding):] {
if b != padding {
return nil, errors.New("invalid padding")
}
}
return data[:len(data)-int(padding)], nil
}
// EncryptedPayload is the JSON structure wrapped in base64 used for ciphertext.
type EncryptedPayload struct {
IV string `json:"iv"`
Value string `json:"value"`
MAC string `json:"mac"`
}
// Encrypt encrypts a plaintext using the APP_KEY from environment.
// @group Encryption
// @behavior readonly
//
// Example: encrypt with current APP_KEY
//
// keyStr, _ := crypt.GenerateAppKey()
// _ = os.Setenv("APP_KEY", keyStr)
// ciphertext, err := crypt.Encrypt("secret")
// godump.Dump(err == nil, ciphertext != "")
//
// // #bool true
// // #bool true
func Encrypt(plaintext string) (string, error) {
key, err := GetAppKey()
if err != nil {
return "", err
}
return encryptWithKey(key, plaintext)
}
// Decrypt decrypts an encrypted payload using the APP_KEY from environment.
// Falls back to APP_PREVIOUS_KEYS when the current key cannot decrypt.
// @group Encryption
// @behavior readonly
//
// Example: decrypt using current key
//
// keyStr, _ := crypt.GenerateAppKey()
// _ = os.Setenv("APP_KEY", keyStr)
// c, _ := crypt.Encrypt("secret")
// p, _ := crypt.Decrypt(c)
// godump.Dump(p)
//
// // #string "secret"
//
// Example: decrypt ciphertext encrypted with a previous key
//
// oldKeyStr, _ := crypt.GenerateAppKey()
// newKeyStr, _ := crypt.GenerateAppKey()
// _ = os.Setenv("APP_KEY", oldKeyStr)
// oldCipher, _ := crypt.Encrypt("rotated")
// _ = os.Setenv("APP_KEY", newKeyStr)
// _ = os.Setenv("APP_PREVIOUS_KEYS", oldKeyStr)
// plain, err := crypt.Decrypt(oldCipher)
// godump.Dump(plain, err)
//
// // #string "rotated"
// // #error <nil>
func Decrypt(encodedPayload string) (string, error) {
key, err := GetAppKey()
if err != nil {
return "", err
}
previousKeys, err := GetPreviousAppKeys()
if err != nil {
return "", err
}
keys := make([][]byte, 0, 1+len(previousKeys))
keys = append(keys, key)
keys = append(keys, previousKeys...)
var lastErr error
for _, k := range keys {
plain, decErr := decryptWithKey(k, encodedPayload)
if decErr == nil {
return plain, nil
}
lastErr = decErr
}
return "", fmt.Errorf("failed to decrypt with current or previous keys: %w", lastErr)
}
// encryptWithKey encrypts plaintext using the provided AES key (16 or 32 bytes).
// Intended for internal use; prefer Encrypt for env-driven keys.
// @group Encryption
// @behavior readonly
func encryptWithKey(key []byte, plaintext string) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
padded := pkcs7Pad([]byte(plaintext), aes.BlockSize)
ciphertext := make([]byte, len(padded))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, padded)
ivB64 := base64.StdEncoding.EncodeToString(iv)
valB64 := base64.StdEncoding.EncodeToString(ciphertext)
mac := computeHMACSHA256(append(iv, ciphertext...), key)
macB64 := base64.StdEncoding.EncodeToString(mac)
payload := EncryptedPayload{IV: ivB64, Value: valB64, MAC: macB64}
jsonData, err := jsonMarshal(payload)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(jsonData), nil
}
// decryptWithKey attempts to decrypt an encoded payload using the provided AES key.
// Intended for internal use; prefer Decrypt for env-driven keys and rotation.
// @group Encryption
// @behavior readonly
func decryptWithKey(key []byte, encodedPayload string) (string, error) {
jsonBytes, err := base64.StdEncoding.DecodeString(encodedPayload)
if err != nil {
return "", fmt.Errorf("base64 decode failed: %w", err)
}
var payload EncryptedPayload
if err := json.Unmarshal(jsonBytes, &payload); err != nil {
return "", fmt.Errorf("json decode failed: %w", err)
}
iv, err := base64.StdEncoding.DecodeString(payload.IV)
if err != nil {
return "", fmt.Errorf("iv decode failed: %w", err)
}
ciphertext, err := base64.StdEncoding.DecodeString(payload.Value)
if err != nil {
return "", fmt.Errorf("value decode failed: %w", err)
}
mac, err := base64.StdEncoding.DecodeString(payload.MAC)
if err != nil {
return "", fmt.Errorf("mac decode failed: %w", err)
}
expectedMAC := computeHMACSHA256(append(iv, ciphertext...), key)
if !hmac.Equal(expectedMAC, mac) {
return "", errors.New("HMAC validation failed")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
if len(ciphertext)%aes.BlockSize != 0 {
return "", errors.New("ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ciphertext, ciphertext)
unpadded, err := pkcs7Unpad(ciphertext)
if err != nil {
return "", err
}
return string(unpadded), nil
}
// computeHMACSHA256 computes HMAC-SHA256 over data with the given key.
// @group MAC
// @behavior pure
//
// Example: produce deterministic MAC
//
// m := crypt.computeHMACSHA256([]byte("msg"), []byte("key"))
// godump.Dump(len(m))
//
// // #int 32
func computeHMACSHA256(data []byte, key []byte) []byte {
h := hmac.New(sha256.New, key)
h.Write(data)
return h.Sum(nil)
}
// dumpExample is a no-op wrapper to keep the godump import live for doc examples.
func dumpExample(values ...interface{}) {
godump.Dump(values...)
}