-
Notifications
You must be signed in to change notification settings - Fork 1
change: introduce new implementation of dbcrypt package #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c0916ce
change: introduce new implementation of dbcrypt package
aad275c
change: refine new implementation of dbcrypt package
845f26a
change: refine new implementation of dbcrypt package
85bed57
change: refine new implementation of dbcrypt package
9773f7b
Merge branch 'main' into AT-2841-new-dbcrypt
9875c17
change: add slices supprt in models for dbcrypt package
b3ced4c
change: refactor model travelsal in dbcrypt package
f57659d
change: imporve error messages in dbcrypt package
134efa8
change: imporve database model handling in dbcrypt package
25bc939
change: imporve database model handling in dbcrypt package
febb8d6
Merge branch 'main' into AT-2841-new-dbcrypt
c3b8d26
change: refine new implementation of dbcrypt package
b1dc428
change: fix failing tests in dbcrypt package
c89312c
change: add licence headers to dbcrypt package
7a9b6b1
change: remove unneded comments
992192d
Merge branch 'main' into AT-2841-new-dbcrypt
stefanTolksdorf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // SPDX-FileCopyrightText: 2025 Greenbone AG <https://greenbone.net> | ||
| // | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| package dbcrypt | ||
|
|
||
| import ( | ||
| "crypto/aes" | ||
| "crypto/cipher" | ||
| "crypto/rand" | ||
| "encoding/base64" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "golang.org/x/crypto/argon2" | ||
| ) | ||
|
|
||
| type dbCipher interface { | ||
| Prefix() string | ||
| Encrypt(plaintext []byte) ([]byte, error) | ||
| Decrypt(ciphertext []byte) ([]byte, error) | ||
| } | ||
|
|
||
| type dbCipherV1 struct { | ||
| key []byte | ||
| } | ||
|
|
||
| func newDbCipherV1(conf Config) (dbCipher, error) { | ||
| // Historically "v1" uses key truncation to 32 bytes. It needs to be preserved for backward compatibility. | ||
| key := []byte(conf.Password + conf.PasswordSalt)[:32] | ||
| return dbCipherV1{key: key}, nil | ||
| } | ||
|
|
||
| func (c dbCipherV1) Prefix() string { | ||
| return "ENC" | ||
| } | ||
|
|
||
| func (c dbCipherV1) Encrypt(plaintext []byte) ([]byte, error) { | ||
| block, err := aes.NewCipher(c.key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| gcm, err := cipher.NewGCM(block) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| iv := make([]byte, gcm.NonceSize()) | ||
| if _, err := io.ReadFull(rand.Reader, iv); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ciphertext := gcm.Seal(nil, iv, []byte(plaintext), nil) | ||
| ciphertextWithIv := append(iv, ciphertext...) | ||
| encoded := hex.AppendEncode(nil, ciphertextWithIv) | ||
| return encoded, nil | ||
| } | ||
|
|
||
| func (c dbCipherV1) Decrypt(encoded []byte) ([]byte, error) { | ||
| ciphertextWithIv, err := hex.AppendDecode(nil, encoded) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error decoding ciphertext: %w", err) | ||
| } | ||
|
|
||
| if len(ciphertextWithIv) < aes.BlockSize+1 { | ||
| return nil, fmt.Errorf("ciphertext too short") | ||
| } | ||
|
|
||
| block, err := aes.NewCipher(c.key) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating AES cipher: %w", err) | ||
| } | ||
|
|
||
| gcm, err := cipher.NewGCM(block) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| iv := ciphertextWithIv[:gcm.NonceSize()] | ||
| ciphertext := ciphertextWithIv[gcm.NonceSize():] | ||
|
|
||
| plaintext, err := gcm.Open(nil, iv, ciphertext, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error decrypting ciphertext: %w", err) | ||
| } | ||
|
|
||
| return plaintext, nil | ||
| } | ||
|
|
||
| type dbCipherV2 struct { | ||
| key []byte | ||
| } | ||
|
|
||
| func newDbCipherV2(conf Config) (dbCipher, error) { | ||
| // "v2" uses proper KDF (argon2id) to get the key | ||
| key := argon2.IDKey([]byte(conf.Password), []byte(conf.PasswordSalt), 1, 64*1024, 4, 32) | ||
| return dbCipherV2{key: key}, nil | ||
| } | ||
|
|
||
| func (c dbCipherV2) Prefix() string { | ||
| return "ENCV2" | ||
| } | ||
|
|
||
| func (c dbCipherV2) Encrypt(plaintext []byte) ([]byte, error) { | ||
| block, err := aes.NewCipher(c.key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| gcm, err := cipher.NewGCMWithRandomNonce(block) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ciphertext := gcm.Seal(nil, nil, []byte(plaintext), nil) | ||
| encoded := base64.StdEncoding.AppendEncode(nil, ciphertext) | ||
| return encoded, nil | ||
| } | ||
|
|
||
| func (c dbCipherV2) Decrypt(encoded []byte) ([]byte, error) { | ||
| ciphertext, err := base64.StdEncoding.AppendDecode(nil, encoded) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error decoding ciphertext: %w", err) | ||
| } | ||
|
|
||
| if len(ciphertext) < aes.BlockSize+1 { | ||
|
badarghal marked this conversation as resolved.
Outdated
|
||
| return nil, fmt.Errorf("ciphertext too short") | ||
| } | ||
|
|
||
| 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, err | ||
| } | ||
|
|
||
| plaintext, err := gcm.Open(nil, nil, ciphertext, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error decrypting ciphertext: %w", err) | ||
| } | ||
|
|
||
| return plaintext, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.