You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this is is a short piece of code I have written for handling AES GCM encryption and I invoked it with incorrect IV Size
`func AesDecrypt(data []byte, aesKey []byte, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(aesKey)
if err != nil {
return nil, err
}
// Create GCM mode on the cipher block
aesGCM, err := cipher.NewGCM(block)
//here this is needed because aes package panics if size is not right, instead of sending error
if len(iv) != aesGCM.NonceSize() {
return nil, errors.New(constants.PkgErrors.AES)
}
message, err := aesGCM.Open(nil, iv, data, nil)
if err != nil {
return nil, err
}
return message, err
}`
The crypto/cipher package in go stdlib - houses AES GCM encryption logic inside the gcm.go file - shown as follows
func (g *gcm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) { if len(nonce) != g.nonceSize { panic("crypto/cipher: incorrect nonce length given to GCM") } //more code below - refer crypto/cipher/gcm.go }
What did you see happen?
My code panicked instead of returning proper error that IV size is invalid, this prevents me from writing test cases and other code constructs that would otherwise be possible had the package returned an erro
Due to this folks who are not aware of strict restrictions on IV/Nonce perhaps due to some domain knowledge deficient - need to add validations in there code right before invoking the Open() method
Instead if an appropriate error can returned and without panic - the package will be much better for developer experience and application reliability
What did you expect to see?
I expected that the package should return some standard error of roughly the following form
errors.New("crypto/cipher: incorrect nonce length given to GCM")
The text was updated successfully, but these errors were encountered:
Go version
1.23.0
Output of
go env
in your module/workspace:What did you do?
this is is a short piece of code I have written for handling AES GCM encryption and I invoked it with incorrect IV Size
`func AesDecrypt(data []byte, aesKey []byte, iv []byte) ([]byte, error) {
}`
The crypto/cipher package in go stdlib - houses AES GCM encryption logic inside the gcm.go file - shown as follows
func (g *gcm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) { if len(nonce) != g.nonceSize { panic("crypto/cipher: incorrect nonce length given to GCM") } //more code below - refer crypto/cipher/gcm.go }
What did you see happen?
My code panicked instead of returning proper error that IV size is invalid, this prevents me from writing test cases and other code constructs that would otherwise be possible had the package returned an erro
Due to this folks who are not aware of strict restrictions on IV/Nonce perhaps due to some domain knowledge deficient - need to add validations in there code right before invoking the Open() method
Instead if an appropriate error can returned and without panic - the package will be much better for developer experience and application reliability
What did you expect to see?
I expected that the package should return some standard error of roughly the following form
errors.New("crypto/cipher: incorrect nonce length given to GCM")
The text was updated successfully, but these errors were encountered: