From 82169ad51ae86bf588d4f4b61df6a89718d2c23a Mon Sep 17 00:00:00 2001 From: Joshua Conero Date: Wed, 19 Jan 2022 16:00:45 +0800 Subject: [PATCH] pref(cbc): turn panic into friendly and controllable errors pref: gitignore vendor --- .gitignore | 3 +++ cbc.go | 9 +++++++++ 2 files changed, 12 insertions(+) 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)