-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcipher.go
More file actions
125 lines (102 loc) · 3.11 KB
/
cipher.go
File metadata and controls
125 lines (102 loc) · 3.11 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
// SPDX-FileCopyrightText: 2025 Greenbone AG <https://greenbone.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package dbcrypt
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"fmt"
"golang.org/x/crypto/argon2"
)
type dbCipher interface {
Encrypt(plaintext []byte) ([]byte, error)
Decrypt(ciphertext []byte) ([]byte, error)
}
type dbCipherGcmAes struct {
key []byte
}
func newDbCipherGcmAes(key []byte) dbCipher {
return dbCipherGcmAes{key: key}
}
func newDbCipherGcmAesWithoutKdf(password, passwordSalt string) dbCipher {
// Historically "v1" uses key truncation to 32 bytes. It needs to be preserved for backward compatibility.
key := make([]byte, 32)
copy(key, []byte(password+passwordSalt))
return newDbCipherGcmAes(key)
}
func newDbCipherGcmAesWithArgon2idKdf(password, passwordSalt string) dbCipher {
// "v2" uses proper KDF (argon2id) to get the key.
key := argon2.IDKey([]byte(password), []byte(passwordSalt), 1, 64*1024, 4, 32)
return newDbCipherGcmAes(key)
}
func (c dbCipherGcmAes) Encrypt(plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(c.key)
if err != nil {
return nil, fmt.Errorf("error creating AES cipher: %w", err)
}
gcm, err := cipher.NewGCMWithRandomNonce(block)
if err != nil {
return nil, fmt.Errorf("error encrypting plaintext: %w", err)
}
ciphertext := gcm.Seal(nil, nil, []byte(plaintext), nil)
return ciphertext, nil
}
func (c dbCipherGcmAes) Decrypt(ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(c.key)
if err != nil {
return nil, fmt.Errorf("error creating AES cipher: %w", err)
}
gcm, err := cipher.NewGCMWithRandomNonce(block)
if err != nil {
return nil, fmt.Errorf("error decrypting ciphertext: %w", err)
}
plaintext, err := gcm.Open(nil, nil, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("error decrypting ciphertext: %w", err)
}
return plaintext, nil
}
type dbCipherHexEncode struct {
impl dbCipher
}
func newDbCipherHexEncode(impl dbCipher) dbCipher {
return dbCipherHexEncode{impl: impl}
}
func (c dbCipherHexEncode) Encrypt(plaintext []byte) ([]byte, error) {
ciphertext, err := c.impl.Encrypt(plaintext)
if err != nil {
return nil, err
}
encoded := hex.AppendEncode(nil, ciphertext)
return encoded, nil
}
func (c dbCipherHexEncode) Decrypt(encoded []byte) ([]byte, error) {
ciphertext, err := hex.AppendDecode(nil, encoded)
if err != nil {
return nil, fmt.Errorf("error decoding ciphertext: %w", err)
}
return c.impl.Decrypt(ciphertext)
}
type dbCipherBase64Encode struct {
impl dbCipher
}
func newDbCipherBase64Encode(impl dbCipher) dbCipher {
return dbCipherBase64Encode{impl: impl}
}
func (c dbCipherBase64Encode) Encrypt(plaintext []byte) ([]byte, error) {
ciphertext, err := c.impl.Encrypt(plaintext)
if err != nil {
return nil, err
}
encoded := base64.StdEncoding.AppendEncode(nil, ciphertext)
return encoded, nil
}
func (c dbCipherBase64Encode) Decrypt(encoded []byte) ([]byte, error) {
ciphertext, err := base64.StdEncoding.AppendDecode(nil, encoded)
if err != nil {
return nil, fmt.Errorf("error decoding ciphertext: %w", err)
}
return c.impl.Decrypt(ciphertext)
}