]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/cipher: panic on invalid IV length
authorShane Hansen <shanemhansen@gmail.com>
Wed, 17 Oct 2012 18:29:00 +0000 (14:29 -0400)
committerAdam Langley <agl@golang.org>
Wed, 17 Oct 2012 18:29:00 +0000 (14:29 -0400)
Give better user feedback when invalid IV is used
to construct a cipher.

Fixes #3411

R=golang-dev, agl
CC=golang-dev
https://golang.org/cl/6652053

src/pkg/crypto/cipher/cbc.go
src/pkg/crypto/cipher/cfb.go
src/pkg/crypto/cipher/ctr.go

index a48929cf5d56cb1538fc9460bfa1d63f1c00ecb2..6fab9b42131e364d2c0c2ab73614bfaedeeecc4a 100644 (file)
@@ -33,6 +33,9 @@ type cbcEncrypter cbc
 // mode, using the given Block. The length of iv must be the same as the
 // Block's block size.
 func NewCBCEncrypter(b Block, iv []byte) BlockMode {
+       if len(iv) != b.BlockSize() {
+               panic("cipher.NewCBCEncrypter: IV length must equal block size")
+       }
        return (*cbcEncrypter)(newCBC(b, iv))
 }
 
@@ -58,6 +61,9 @@ type cbcDecrypter cbc
 // mode, using the given Block. The length of iv must be the same as the
 // Block's block size and must match the iv used to encrypt the data.
 func NewCBCDecrypter(b Block, iv []byte) BlockMode {
+       if len(iv) != b.BlockSize() {
+               panic("cipher.NewCBCDecrypter: IV length must equal block size")
+       }
        return (*cbcDecrypter)(newCBC(b, iv))
 }
 
index d14165a865661a0fee40bb5f727856cb869140d4..99006b546d1463c04a911c8fa68b8764b8ba3250 100644 (file)
@@ -17,6 +17,9 @@ type cfb struct {
 // using the given Block. The iv must be the same length as the Block's block
 // size.
 func NewCFBEncrypter(block Block, iv []byte) Stream {
+       if len(iv) != block.BlockSize() {
+               panic("cipher.NewCBFEncrypter: IV length must equal block size")
+       }
        return newCFB(block, iv, false)
 }
 
@@ -24,6 +27,9 @@ func NewCFBEncrypter(block Block, iv []byte) Stream {
 // using the given Block. The iv must be the same length as the Block's block
 // size.
 func NewCFBDecrypter(block Block, iv []byte) Stream {
+       if len(iv) != block.BlockSize() {
+               panic("cipher.NewCBFEncrypter: IV length must equal block size")
+       }
        return newCFB(block, iv, true)
 }
 
index 147b74fc2fd4d7a24c7592410e6aa4b235854a95..d9ee9d82725f46280602e1bb252b9a8b5c36cdd1 100644 (file)
@@ -23,7 +23,7 @@ type ctr struct {
 // counter mode. The length of iv must be the same as the Block's block size.
 func NewCTR(block Block, iv []byte) Stream {
        if len(iv) != block.BlockSize() {
-               panic("cipher.NewCTR: iv length must equal block size")
+               panic("cipher.NewCTR: IV length must equal block size")
        }
 
        return &ctr{