diff --git a/.gitignore b/.gitignore index 97cca67..5691c92 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ *.out .idea + +# go-mod cache +vendor \ No newline at end of file diff --git a/cbc.go b/cbc.go index a060f9e..fd4fdcf 100755 --- a/cbc.go +++ b/cbc.go @@ -2,6 +2,7 @@ package openssl import ( "crypto/cipher" + "errors" ) // CBCEncrypt @@ -11,6 +12,10 @@ func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err encryptData := make([]byte, len(src)) + if len(iv) != block.BlockSize() { + return nil, errors.New("CBCEncrypt: IV length must equal block size") + } + mode := cipher.NewCBCEncrypter(block, iv) mode.CryptBlocks(encryptData, src) @@ -22,6 +27,10 @@ func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err dst := make([]byte, len(src)) + if len(iv) != block.BlockSize() { + return nil, errors.New("CBCDecrypt: IV length must equal block size") + } + mode := cipher.NewCBCDecrypter(block, iv) mode.CryptBlocks(dst, src)