diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 00000000..968ba538 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,38 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "*" ] + # Build semver tags as releases. + tags: [ '*' ] + pull_request: + branches: [ "*" ] + # Build semver tags as releases. + tags: [ '*' ] + workflow_dispatch: + +jobs: + + build: + strategy: + fail-fast: false + matrix: + os: ["windows-latest", "ubuntu-latest", "ubuntu-22.04", "ubuntu-20.04", "macos-latest"] + go-version: ["1.24", "1.23", "1.22", "1.21", "1.20", "1.19", "1.18"] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/sm4/sm4.go b/sm4/sm4.go index 0e301deb..1778795b 100644 --- a/sm4/sm4.go +++ b/sm4/sm4.go @@ -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))) } @@ -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 {