Skip to content

Commit

Permalink
fix: return friendly error on incorrect nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
routing-ci committed Nov 1, 2019
1 parent a658664 commit b1b5c44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions common/secure/crypto.go
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"errors"

"golang.org/x/crypto/pbkdf2"
)
Expand Down Expand Up @@ -45,6 +46,10 @@ func (gcm *AesGCM) Encrypt(plainText []byte) (cipherText, nonce []byte, err erro
}

func (gcm *AesGCM) Decrypt(cipherText, nonce []byte) ([]byte, error) {
if len(nonce) != gcm.NonceSize() {
return nil, errors.New("incorrect nonce length")
}

plainText, err := gcm.Open(nil, nonce, cipherText, []byte{})
if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions common/secure/crypto_test.go
Expand Up @@ -137,6 +137,16 @@ var _ = Describe("Crypto", func() {
Expect(decryptedText).ToNot(Equal(plainText))
})
})

Context("when the nonce is an invalid length", func() {
It("does not panic and returns an error", func() {
otherNonce := []byte("0123")
decryptedText, err := aesGcm.Decrypt(cipherText, otherNonce)
Expect(err).To(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("incorrect nonce length"))
Expect(decryptedText).ToNot(Equal(plainText))
})
})
})

Describe("RandomBytes", func() {
Expand Down

1 comment on commit b1b5c44

@adobley
Copy link
Contributor

@adobley adobley commented on b1b5c44 Nov 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My author got unset and I didn't notice. You can blame me if this causes problems.

Please sign in to comment.