Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to use input iv. 在工具函数SM4Cbc中,允许使用通过形参输入的iv,以支持并行调用。(兼容使用全局变量IV的情况) #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions sm4/sm4.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func SetIV(iv []byte) error {
return nil
}

func Sm4Cbc(key []byte, in []byte, mode bool) (out []byte, err error) {
func Sm4Cbc(key []byte, in []byte, mode bool, ivInput ...[]byte) (out []byte, err error) {
if len(key) != BlockSize {
return nil, errors.New("SM4: invalid key size " + strconv.Itoa(len(key)))
}
Expand All @@ -310,7 +310,14 @@ func Sm4Cbc(key []byte, in []byte, mode bool) (out []byte, err error) {
inData = in
}
iv := make([]byte, BlockSize)
copy(iv, IV)
if ivInput != nil {
if len(ivInput[0]) != BlockSize {
return nil, errors.New("SM4: invalid iv size")
}
copy(iv, ivInput[0])
} else {
copy(iv, IV)
}
out = make([]byte, len(inData))
c, err := NewCipher(key)
if err != nil {
Expand Down