|
1 | 1 | package encryption |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "crypto/aes" |
5 | 6 | "crypto/cipher" |
6 | 7 | "crypto/rand" |
7 | 8 | "crypto/sha256" |
8 | 9 | "encoding/base64" |
9 | 10 | "fmt" |
10 | 11 | "io" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "golang.org/x/crypto/pbkdf2" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + encryptionStringVersion = "v2" |
| 19 | + encryptionBinaryMagic = "AFENC2" |
| 20 | + encryptionSaltSize = 16 |
| 21 | + encryptionKeySize = 32 |
| 22 | + encryptionPBKDF2Rounds = 600000 |
11 | 23 | ) |
12 | 24 |
|
13 | 25 | // EncryptionService provides encryption and decryption for sensitive configuration values |
14 | 26 | type EncryptionService struct { |
15 | | - key []byte |
| 27 | + passphrase []byte |
16 | 28 | } |
17 | 29 |
|
18 | | -// NewEncryptionService creates a new encryption service with a derived key |
| 30 | +// NewEncryptionService creates a new encryption service with a PBKDF2-hardened passphrase. |
19 | 31 | func NewEncryptionService(passphrase string) *EncryptionService { |
20 | | - // Derive a 32-byte key from the passphrase using SHA-256 |
21 | | - hash := sha256.Sum256([]byte(passphrase)) |
22 | 32 | return &EncryptionService{ |
23 | | - key: hash[:], |
| 33 | + passphrase: []byte(passphrase), |
24 | 34 | } |
25 | 35 | } |
26 | 36 |
|
27 | | -// Encrypt encrypts a plaintext string and returns a base64-encoded ciphertext |
28 | | -func (es *EncryptionService) Encrypt(plaintext string) (string, error) { |
29 | | - if plaintext == "" { |
30 | | - return "", nil |
| 37 | +func (es *EncryptionService) deriveKey(salt []byte) []byte { |
| 38 | + return pbkdf2.Key(es.passphrase, salt, encryptionPBKDF2Rounds, encryptionKeySize, sha256.New) |
| 39 | +} |
| 40 | + |
| 41 | +func (es *EncryptionService) encryptRaw(plaintext []byte) ([]byte, error) { |
| 42 | + if len(plaintext) == 0 { |
| 43 | + return nil, nil |
31 | 44 | } |
32 | 45 |
|
33 | | - // Create AES cipher |
34 | | - block, err := aes.NewCipher(es.key) |
| 46 | + salt := make([]byte, encryptionSaltSize) |
| 47 | + if _, err := io.ReadFull(rand.Reader, salt); err != nil { |
| 48 | + return nil, fmt.Errorf("failed to generate salt: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + block, err := aes.NewCipher(es.deriveKey(salt)) |
35 | 52 | if err != nil { |
36 | | - return "", fmt.Errorf("failed to create AES cipher: %w", err) |
| 53 | + return nil, fmt.Errorf("failed to create AES cipher: %w", err) |
37 | 54 | } |
38 | 55 |
|
39 | | - // Create GCM mode |
40 | 56 | gcm, err := cipher.NewGCM(block) |
41 | 57 | if err != nil { |
42 | | - return "", fmt.Errorf("failed to create GCM: %w", err) |
| 58 | + return nil, fmt.Errorf("failed to create GCM: %w", err) |
43 | 59 | } |
44 | 60 |
|
45 | | - // Generate a random nonce |
46 | 61 | nonce := make([]byte, gcm.NonceSize()) |
47 | 62 | if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
48 | | - return "", fmt.Errorf("failed to generate nonce: %w", err) |
| 63 | + return nil, fmt.Errorf("failed to generate nonce: %w", err) |
49 | 64 | } |
50 | 65 |
|
51 | | - // Encrypt the plaintext |
52 | | - ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil) |
53 | | - |
54 | | - // Return base64-encoded ciphertext |
55 | | - return base64.StdEncoding.EncodeToString(ciphertext), nil |
| 66 | + ciphertext := gcm.Seal(nil, nonce, plaintext, nil) |
| 67 | + encoded := make([]byte, 0, len(encryptionBinaryMagic)+len(salt)+len(nonce)+len(ciphertext)) |
| 68 | + encoded = append(encoded, encryptionBinaryMagic...) |
| 69 | + encoded = append(encoded, salt...) |
| 70 | + encoded = append(encoded, nonce...) |
| 71 | + encoded = append(encoded, ciphertext...) |
| 72 | + return encoded, nil |
56 | 73 | } |
57 | 74 |
|
58 | | -// Decrypt decrypts a base64-encoded ciphertext and returns the plaintext |
59 | | -func (es *EncryptionService) Decrypt(ciphertext string) (string, error) { |
60 | | - if ciphertext == "" { |
61 | | - return "", nil |
| 75 | +func (es *EncryptionService) decryptRaw(ciphertext []byte) ([]byte, error) { |
| 76 | + if len(ciphertext) == 0 { |
| 77 | + return nil, nil |
62 | 78 | } |
63 | 79 |
|
64 | | - // Decode base64 |
65 | | - data, err := base64.StdEncoding.DecodeString(ciphertext) |
66 | | - if err != nil { |
67 | | - return "", fmt.Errorf("failed to decode base64: %w", err) |
| 80 | + if !bytes.HasPrefix(ciphertext, []byte(encryptionBinaryMagic)) { |
| 81 | + return nil, fmt.Errorf("unsupported legacy ciphertext format") |
68 | 82 | } |
69 | 83 |
|
70 | | - // Create AES cipher |
71 | | - block, err := aes.NewCipher(es.key) |
| 84 | + data := ciphertext[len(encryptionBinaryMagic):] |
| 85 | + if len(data) < encryptionSaltSize { |
| 86 | + return nil, fmt.Errorf("ciphertext too short") |
| 87 | + } |
| 88 | + |
| 89 | + salt, encryptedData := data[:encryptionSaltSize], data[encryptionSaltSize:] |
| 90 | + |
| 91 | + block, err := aes.NewCipher(es.deriveKey(salt)) |
72 | 92 | if err != nil { |
73 | | - return "", fmt.Errorf("failed to create AES cipher: %w", err) |
| 93 | + return nil, fmt.Errorf("failed to create AES cipher: %w", err) |
74 | 94 | } |
75 | 95 |
|
76 | | - // Create GCM mode |
77 | 96 | gcm, err := cipher.NewGCM(block) |
78 | 97 | if err != nil { |
79 | | - return "", fmt.Errorf("failed to create GCM: %w", err) |
| 98 | + return nil, fmt.Errorf("failed to create GCM: %w", err) |
80 | 99 | } |
81 | 100 |
|
82 | | - // Check minimum length |
83 | 101 | nonceSize := gcm.NonceSize() |
84 | | - if len(data) < nonceSize { |
85 | | - return "", fmt.Errorf("ciphertext too short") |
| 102 | + if len(encryptedData) < nonceSize { |
| 103 | + return nil, fmt.Errorf("ciphertext too short") |
86 | 104 | } |
87 | 105 |
|
88 | | - // Extract nonce and encrypted data |
89 | | - nonce, encryptedData := data[:nonceSize], data[nonceSize:] |
90 | | - |
91 | | - // Decrypt |
92 | | - plaintext, err := gcm.Open(nil, nonce, encryptedData, nil) |
| 106 | + nonce, sealed := encryptedData[:nonceSize], encryptedData[nonceSize:] |
| 107 | + plaintext, err := gcm.Open(nil, nonce, sealed, nil) |
93 | 108 | if err != nil { |
94 | | - return "", fmt.Errorf("failed to decrypt: %w", err) |
| 109 | + return nil, fmt.Errorf("failed to decrypt: %w", err) |
95 | 110 | } |
96 | 111 |
|
97 | | - return string(plaintext), nil |
| 112 | + return plaintext, nil |
98 | 113 | } |
99 | 114 |
|
100 | | -// EncryptBytes encrypts raw bytes and returns the ciphertext as bytes (nonce prepended). |
101 | | -func (es *EncryptionService) EncryptBytes(plaintext []byte) ([]byte, error) { |
102 | | - if len(plaintext) == 0 { |
103 | | - return nil, nil |
| 115 | +// Encrypt encrypts a plaintext string and returns a versioned, base64-encoded ciphertext. |
| 116 | +func (es *EncryptionService) Encrypt(plaintext string) (string, error) { |
| 117 | + if plaintext == "" { |
| 118 | + return "", nil |
104 | 119 | } |
105 | 120 |
|
106 | | - block, err := aes.NewCipher(es.key) |
| 121 | + encoded, err := es.encryptRaw([]byte(plaintext)) |
107 | 122 | if err != nil { |
108 | | - return nil, fmt.Errorf("failed to create AES cipher: %w", err) |
| 123 | + return "", err |
109 | 124 | } |
110 | 125 |
|
111 | | - gcm, err := cipher.NewGCM(block) |
112 | | - if err != nil { |
113 | | - return nil, fmt.Errorf("failed to create GCM: %w", err) |
114 | | - } |
| 126 | + return encryptionStringVersion + ":" + base64.StdEncoding.EncodeToString(encoded), nil |
| 127 | +} |
115 | 128 |
|
116 | | - nonce := make([]byte, gcm.NonceSize()) |
117 | | - if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
118 | | - return nil, fmt.Errorf("failed to generate nonce: %w", err) |
| 129 | +// Decrypt decrypts a base64-encoded ciphertext and returns the plaintext |
| 130 | +func (es *EncryptionService) Decrypt(ciphertext string) (string, error) { |
| 131 | + if ciphertext == "" { |
| 132 | + return "", nil |
119 | 133 | } |
120 | 134 |
|
121 | | - return gcm.Seal(nonce, nonce, plaintext, nil), nil |
122 | | -} |
123 | | - |
124 | | -// DecryptBytes decrypts ciphertext bytes (nonce prepended) and returns the plaintext bytes. |
125 | | -func (es *EncryptionService) DecryptBytes(ciphertext []byte) ([]byte, error) { |
126 | | - if len(ciphertext) == 0 { |
127 | | - return nil, nil |
| 135 | + encoded := ciphertext |
| 136 | + if strings.HasPrefix(ciphertext, encryptionStringVersion+":") { |
| 137 | + encoded = strings.TrimPrefix(ciphertext, encryptionStringVersion+":") |
128 | 138 | } |
129 | 139 |
|
130 | | - block, err := aes.NewCipher(es.key) |
| 140 | + data, err := base64.StdEncoding.DecodeString(encoded) |
131 | 141 | if err != nil { |
132 | | - return nil, fmt.Errorf("failed to create AES cipher: %w", err) |
| 142 | + return "", fmt.Errorf("failed to decode base64: %w", err) |
133 | 143 | } |
134 | 144 |
|
135 | | - gcm, err := cipher.NewGCM(block) |
| 145 | + plaintext, err := es.decryptRaw(data) |
136 | 146 | if err != nil { |
137 | | - return nil, fmt.Errorf("failed to create GCM: %w", err) |
| 147 | + return "", err |
138 | 148 | } |
139 | 149 |
|
140 | | - nonceSize := gcm.NonceSize() |
141 | | - if len(ciphertext) < nonceSize { |
142 | | - return nil, fmt.Errorf("ciphertext too short") |
143 | | - } |
| 150 | + return string(plaintext), nil |
| 151 | +} |
144 | 152 |
|
145 | | - nonce, encryptedData := ciphertext[:nonceSize], ciphertext[nonceSize:] |
146 | | - plaintext, err := gcm.Open(nil, nonce, encryptedData, nil) |
147 | | - if err != nil { |
148 | | - return nil, fmt.Errorf("failed to decrypt: %w", err) |
149 | | - } |
| 153 | +// EncryptBytes encrypts raw bytes and returns the versioned ciphertext bytes. |
| 154 | +func (es *EncryptionService) EncryptBytes(plaintext []byte) ([]byte, error) { |
| 155 | + return es.encryptRaw(plaintext) |
| 156 | +} |
150 | 157 |
|
151 | | - return plaintext, nil |
| 158 | +// DecryptBytes decrypts versioned ciphertext bytes and returns the plaintext bytes. |
| 159 | +func (es *EncryptionService) DecryptBytes(ciphertext []byte) ([]byte, error) { |
| 160 | + return es.decryptRaw(ciphertext) |
152 | 161 | } |
153 | 162 |
|
154 | 163 | // EncryptConfigurationValues encrypts sensitive values in a configuration map |
|
0 commit comments