-
Notifications
You must be signed in to change notification settings - Fork 26
feat(secret): encrypt secret metadata at rest #277
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
Open
yagmurcicekdagi
wants to merge
2
commits into
spiffe:main
Choose a base branch
from
yagmurcicekdagi:feat/secret-metadata-encryption
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+322
−46
Open
Changes from all commits
Commits
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
118 changes: 118 additions & 0 deletions
118
app/nexus/internal/state/backend/sqlite/persist/field_nonce.go
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,118 @@ | ||
| // \\ SPIKE: Secure your secrets with SPIFFE. — https://spike.ist/ | ||
| // \\\\\ Copyright 2024-present SPIKE contributors. | ||
| // \\\\\\\ SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package persist | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| sdkErrors "github.com/spiffe/spike-sdk-go/errors" | ||
| ) | ||
|
|
||
| const ( | ||
| nonceFieldSecretMetadataCurrentVersion string = "secret_metadata.current_version" | ||
| nonceFieldSecretMetadataOldestVersion string = "secret_metadata.oldest_version" | ||
| nonceFieldSecretMetadataCreatedTime string = "secret_metadata.created_time" | ||
| nonceFieldSecretMetadataUpdatedTime string = "secret_metadata.updated_time" | ||
| nonceFieldSecretMetadataMaxVersions string = "secret_metadata.max_versions" | ||
| ) | ||
|
|
||
| // fieldNonceSalts are fixed per-field salts (must match AES-GCM nonce size). | ||
| // A per-field nonce is derived as baseNonce XOR salt. | ||
| var fieldNonceSalts = map[string][]byte{ | ||
| nonceFieldSecretMetadataCurrentVersion: []byte("current_ver_"), // 12 bytes | ||
| nonceFieldSecretMetadataOldestVersion: []byte("oldest_ver__"), // 12 bytes | ||
| nonceFieldSecretMetadataCreatedTime: []byte("created_tim_"), // 12 bytes | ||
| nonceFieldSecretMetadataUpdatedTime: []byte("updated_tim_"), // 12 bytes | ||
| nonceFieldSecretMetadataMaxVersions: []byte("max_versions"), // 12 bytes | ||
| } | ||
|
|
||
| // deriveFieldNonce derives a per-field AES-GCM nonce from a base nonce by | ||
| // XOR'ing it with a fixed, field-specific salt. This enables using a single | ||
| // per-row base nonce while still ensuring each encrypted field uses a distinct | ||
| // nonce. | ||
| // | ||
| // Parameters: | ||
| // - baseNonce: The base nonce to derive from. Its length must match the | ||
| // cipher's required nonce size and the salt length for the given field. | ||
| // - field: The field identifier used to select the derivation salt. | ||
| // | ||
| // Returns: | ||
| // - []byte: The derived nonce for the given field. | ||
| // - *sdkErrors.SDKError: An error if the field is unknown | ||
| // (ErrEntityInvalid) or if the nonce size does not match | ||
| // (ErrCryptoNonceSizeMismatch). Returns nil on success. | ||
| func deriveFieldNonce(baseNonce []byte, field string) ([]byte, *sdkErrors.SDKError) { | ||
| salt, ok := fieldNonceSalts[field] | ||
| if !ok { | ||
| failErr := *sdkErrors.ErrEntityInvalid.Clone() | ||
| failErr.Msg = fmt.Sprintf("unknown nonce derivation field: %q", field) | ||
| return nil, &failErr | ||
| } | ||
|
|
||
| if len(baseNonce) != len(salt) { | ||
| failErr := *sdkErrors.ErrCryptoNonceSizeMismatch.Clone() | ||
| failErr.Msg = fmt.Sprintf( | ||
| "invalid nonce size for field %q: got %d, want %d", | ||
| field, len(baseNonce), len(salt), | ||
| ) | ||
| return nil, &failErr | ||
| } | ||
|
|
||
| derived := make([]byte, len(baseNonce)) | ||
| for i := range baseNonce { | ||
| derived[i] = baseNonce[i] ^ salt[i] | ||
| } | ||
| return derived, nil | ||
| } | ||
|
|
||
| // encryptWithDerivedNonce encrypts data using a nonce derived from the | ||
| // provided base nonce and field identifier. The derived nonce is produced by | ||
| // applying the field-specific XOR salt in deriveFieldNonce. | ||
| // | ||
| // Parameters: | ||
| // - s: The DataStore containing the AES-GCM cipher for encryption. | ||
| // - baseNonce: The base nonce used to derive the per-field nonce. | ||
| // - field: The field identifier used to select the derivation salt. | ||
| // - data: The plaintext data to encrypt. | ||
| // | ||
| // Returns: | ||
| // - []byte: The encrypted ciphertext. | ||
| // - *sdkErrors.SDKError: An error if nonce derivation fails or if encryption | ||
| // fails. Returns nil on success. | ||
| func encryptWithDerivedNonce( | ||
| s *DataStore, baseNonce []byte, field string, data []byte, | ||
| ) ([]byte, *sdkErrors.SDKError) { | ||
| derivedNonce, deriveErr := deriveFieldNonce(baseNonce, field) | ||
| if deriveErr != nil { | ||
| return nil, deriveErr | ||
| } | ||
|
|
||
| return encryptWithNonce(s, derivedNonce, data) | ||
| } | ||
|
|
||
| // decryptWithDerivedNonce decrypts ciphertext using a nonce derived from the | ||
| // provided base nonce and field identifier. The derived nonce is produced by | ||
| // applying the field-specific XOR salt in deriveFieldNonce. | ||
| // | ||
| // Parameters: | ||
| // - s: The DataStore containing the AES-GCM cipher for decryption. | ||
| // - baseNonce: The base nonce used to derive the per-field nonce. | ||
| // - field: The field identifier used to select the derivation salt. | ||
| // - ciphertext: The encrypted data to decrypt. | ||
| // | ||
| // Returns: | ||
| // - []byte: The decrypted plaintext. | ||
| // - *sdkErrors.SDKError: An error if nonce derivation fails or if decryption | ||
| // fails. Returns nil on success. | ||
| func decryptWithDerivedNonce( | ||
| s *DataStore, baseNonce []byte, field string, ciphertext []byte, | ||
| ) ([]byte, *sdkErrors.SDKError) { | ||
| derivedNonce, deriveErr := deriveFieldNonce(baseNonce, field) | ||
| if deriveErr != nil { | ||
| return nil, deriveErr | ||
| } | ||
|
|
||
| return s.decrypt(ciphertext, derivedNonce) | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a single nonce per field would be cryptographically weak and it can cause warnings in security audits.
While I do realize that the data here itself is not super-sensitive, auditors typically want to "check boxes", rather than having a pragmatic perspective on security.
My suggestion is as follows:
Derive per-field nonces from the stored base nonce without changing the schema:
One related question would be "what if an attacker knows the per-field key" (since they have access to the source code, as the code is public).
Kerckhoffs's principle states that "A cryptosystem should be secure even if everything except the key is public knowledge".
In AES-GCM:
The nonce is typically stored in plaintext right next to the ciphertext (which we are already doing). An attacker seeing the nonce doesn't help them—they still can't decrypt without the key.
The per-field salts being in the source code is fine because:
As an aside, nonces don't need to be secret—they only need to be unique.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
My initial thought was to generate unique nonces per field, but as you mentioned, this would be an overkill, so I opted for the single nonce approach—referring to how policies are encrypted in app/nexus/internal/state/backend/sqlite/persist/policy.go.
I'm also just curious: Is there a specific reason why single nonce per field approach is used in policies? Wouldn't that be cryptographically weak as well?