Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ["1.25.x"]
go-version: ["1.26.x"]

steps:
- uses: actions/checkout@v5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ["1.25.x"]
go-version: ["1.26.x"]

steps:
- uses: actions/checkout@v5
Expand Down
5 changes: 5 additions & 0 deletions aescbc/aescbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func Decrypt(key []byte, ciphertext []byte) ([]byte, error) {

iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]

if len(ciphertext) == 0 || len(ciphertext)%aes.BlockSize != 0 {
return nil, fmt.Errorf("ciphertext after IV must be a non-zero multiple of %d bytes but was %d", aes.BlockSize, len(ciphertext))
}

decrypter := cipher.NewCBCDecrypter(block, iv)

plaintextData := make([]byte, len(ciphertext))
Expand Down
33 changes: 33 additions & 0 deletions aescbc/aescbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ func TestDecrypt(t *testing.T) {
assert.Equal(t, []byte("Hello world"), dec)
}

func TestDecryptBlockSizeValidation(t *testing.T) {
key := []byte("12345678901234567890123456789012") // 32 bytes

t.Run("IV only, no data", func(t *testing.T) {
ciphertext := make([]byte, 16) // exactly aes.BlockSize
_, err := aescbc.Decrypt(key, ciphertext)
assert.ErrorContains(t, err, "ciphertext after IV must be a non-zero multiple of 16 bytes but was 0")
})

t.Run("non-block-aligned after IV", func(t *testing.T) {
ciphertext := make([]byte, 17) // aes.BlockSize + 1
_, err := aescbc.Decrypt(key, ciphertext)
assert.ErrorContains(t, err, "ciphertext after IV must be a non-zero multiple of 16 bytes but was 1")
})

t.Run("block-aligned after IV does not panic", func(t *testing.T) {
ciphertext := make([]byte, 32) // aes.BlockSize + aes.BlockSize
assert.NotPanics(t, func() {
// Will fail on padding validation, but must not panic
_, _ = aescbc.Decrypt(key, ciphertext)
})
})

t.Run("round-trip", func(t *testing.T) {
plaintext := []byte("security audit fix")
enc, err := aescbc.Encrypt(rand.Reader, key, plaintext)
require.NoError(t, err)
dec, err := aescbc.Decrypt(key, enc)
require.NoError(t, err)
assert.Equal(t, plaintext, dec)
})
}

func FuzzEncryptAndDecrypt(f *testing.F) {
f.Add(uint64(0), uint64(0), uint64(0), uint64(0), []byte("hello"))
f.Fuzz(func(t *testing.T, u0, u1, u2, u3 uint64, plaintext []byte) {
Expand Down
Loading