Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/controlplane/pkg/biz/attestationstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func decrypt(ciphertext []byte, passphrase string) ([]byte, error) {
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)

// Too short to hold the magic prefix: a truncated or tampered row, reported like
// any other value that does not decrypt to the magic prefix.
if len(ciphertext) < len(magic) {
return nil, NewErrInvalidPassphrase()
}

// Check if the decrypted data starts with the magic string
if string(ciphertext[:len(magic)]) != magic {
// A mismatch means the state was encrypted with a different passphrase,
Expand Down
31 changes: 31 additions & 0 deletions app/controlplane/pkg/biz/attestationstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,34 @@ func TestErrInvalidPassphraseIsDiscoverableWhenWrapped(t *testing.T) {
wrapped := fmt.Errorf("failed to decrypt attestation state: %w", NewErrInvalidPassphrase())
assert.True(t, IsErrInvalidPassphrase(wrapped))
}

// Truncated stored values (salt + IV + fewer bytes than the magic prefix) must return an error, not panic.
func TestDecryptShortInput(t *testing.T) {
testCases := []struct {
name string
size int
}{
{name: "salt and IV only", size: saltSize + 16},
{name: "one byte after IV", size: saltSize + 16 + 1},
{name: "one byte short of magic", size: saltSize + 16 + len(magic) - 1},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var err error
require.NotPanics(t, func() {
_, err = decrypt(make([]byte, tc.size), "passphrase")
})
assert.True(t, IsErrInvalidPassphrase(err))
})
}
}

func TestEncryptDecryptRoundTrip(t *testing.T) {
enc, err := encrypt([]byte("data"), "passphrase")
require.NoError(t, err)

got, err := decrypt(enc, "passphrase")
require.NoError(t, err)
assert.Equal(t, []byte("data"), got)
}
Loading