-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbcipher.go
More file actions
112 lines (98 loc) · 3.17 KB
/
dbcipher.go
File metadata and controls
112 lines (98 loc) · 3.17 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
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package dbcrypt
import (
"bytes"
"errors"
"fmt"
)
const prefixSeparator = ":"
// Config encapsulates configuration for DBCipher.
type Config struct {
// Default version of the cryptographic algorithm. Useful for testing older historical implementations. Leave empty to use the most recent version.
//
// Supported values:
// - "": use latest version of the cryptographic algorithm (recommended).
// - "v2": use v2 version of the cryptographic algorithm.
// - "v1": use v1 version of the cryptographic algorithm.
//
// See cipher_spec.go for all versions
Version string
// Contains the password used to derive encryption key
Password string //nolint:gosec
// Contains the salt for increasing password entropy
PasswordSalt string
}
// Validate validates the provided config.
func (conf Config) Validate() error {
if conf.Password == "" {
return errors.New("db password is empty")
}
if conf.PasswordSalt == "" {
return errors.New("db password salt is empty")
}
if len(conf.PasswordSalt) < 32 {
return errors.New("db password salt is too short")
}
return nil
}
// DBCipher is cipher designed to perform validated encryption and decryption on database values.
type DBCipher struct {
encryptionCipherSpec *cipherSpec
ciphersSpec *ciphersSpec
}
// NewDBCipher creates a new instance of DBCipher based on the provided Config.
func NewDBCipher(conf Config) (*DBCipher, error) {
if err := conf.Validate(); err != nil {
return nil, err
}
spec, err := newCiphersSpec(conf)
if err != nil {
return nil, fmt.Errorf("error creating crypto ciphers spec: %w", err)
}
encryptionVersion := conf.Version
if encryptionVersion == "" {
encryptionVersion = spec.DefaultVersion
}
encryptionCipherSpec, err := spec.GetByVersion(encryptionVersion)
if err != nil {
return nil, fmt.Errorf("could not get encryption cipher by version: %w", err)
}
c := &DBCipher{
encryptionCipherSpec: encryptionCipherSpec,
ciphersSpec: spec,
}
return c, nil
}
// Encrypt encrypts the provided bytes with DBCipher.
func (c *DBCipher) Encrypt(plaintext []byte) ([]byte, error) {
ciphertext, err := c.encryptionCipherSpec.Cipher.Encrypt(plaintext)
if err != nil {
return nil, err
}
ciphertextWithPrefix := bytes.NewBuffer(nil)
ciphertextWithPrefix.WriteString(c.encryptionCipherSpec.Prefix)
ciphertextWithPrefix.WriteString(prefixSeparator)
ciphertextWithPrefix.Write(ciphertext)
return ciphertextWithPrefix.Bytes(), nil
}
// Decrypt decrypts the provided bytes with DBCipher.
func (c *DBCipher) Decrypt(ciphertextWithPrefix []byte) ([]byte, error) {
if len(ciphertextWithPrefix) == 0 {
return nil, nil
}
prefix, ciphertext, hasSeparator := bytes.Cut(ciphertextWithPrefix, []byte(prefixSeparator))
if !hasSeparator {
return nil, errors.New("invalid encrypted value format")
}
decryptionCipherSpec, err := c.ciphersSpec.GetByPrefix(string(prefix))
if err != nil {
return nil, fmt.Errorf("unknown encrypted value format: %w", err)
}
plaintext, err := decryptionCipherSpec.Cipher.Decrypt(ciphertext)
if err != nil {
return nil, err
}
return plaintext, nil
}