refactor(crypto): implement dynamic salt and support multiple keys/algorithms #6363
Open
DharunMR wants to merge 2 commits intomindersec:mainfrom
Open
refactor(crypto): implement dynamic salt and support multiple keys/algorithms #6363DharunMR wants to merge 2 commits intomindersec:mainfrom
DharunMR wants to merge 2 commits intomindersec:mainfrom
Conversation
…support key rotation Signed-off-by: DharunMR <maddharun56@gmail.com>
Signed-off-by: DharunMR <maddharun56@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR completes a major architectural upgrade to the cryptography engine by supporting multiple keys/algorithms and generating dynamic salts per secret. During the implementation of these features, several critical bugs and redundancies in the AES-GCM and AES-CFB implementations were identified and fixed
Dynamic Routing: EncryptedData now stores Algorithm and KeyVersion metadata. engine.go reads this metadata during decryption to dynamically route the payload to the correct algorithm (GCM vs CFB) and fetch the specific key version from the keystore.
Seamless Rotation: This allows administrators to rotate default keys in the configuration; new secrets will use the new key, while old secrets will still decrypt perfectly using the stored KeyVersion.
Engin Driven Entropy: engine.go now securely generates a fresh 16-byte random salt (rand.Read) for every encryption request and stores it securely in the DB record.
CFB Upgraded: aes256cfb.go now accepts this dynamic salt for Argon2 key derivation, drastically improving cryptographic security against pre-computed attacks compared to the old hardcoded legacySalt.
Resolved "Double-Storage": Previously, GCM prepended the 12-byte nonce to the ciphertext, while engine.go was also storing that exact same salt in the database. Encrypt now passes nil to gcm.Seal for the destination array, removing the redundant prepended nonce and reducing the database footprint per secret.
Safely Handled Keystore Newlines: Legacy CFB local keystores contain trailing newlines (\n). Passing these raw strings to Go's strict Base64 decoder caused GCM to panic. decodeKey now utilizes strings.TrimSpace(string(key)) before Base64 decoding, preventing crashes while maintaining absolute backward compatibility for CFB.