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

rename function name PKCS5UnPadding to PKCS7UnPadding #3124

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
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
37 changes: 31 additions & 6 deletions crypto/gaes/gaes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"fmt"

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
Expand Down Expand Up @@ -41,7 +42,7 @@ func EncryptCBC(plainText []byte, key []byte, iv ...[]byte) ([]byte, error) {
return nil, err
}
blockSize := block.BlockSize()
plainText = PKCS5Padding(plainText, blockSize)
plainText = PKCS7Padding(plainText, blockSize)
ivValue := ([]byte)(nil)
if len(iv) > 0 {
ivValue = iv[0]
Expand Down Expand Up @@ -80,23 +81,47 @@ func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) {
blockModel := cipher.NewCBCDecrypter(block, ivValue)
plainText := make([]byte, len(cipherText))
blockModel.CryptBlocks(plainText, cipherText)
plainText, e := PKCS5UnPadding(plainText, blockSize)
plainText, e := PKCS7UnPadding(plainText, blockSize)
if e != nil {
return nil, e
}
return plainText, nil
}

func PKCS5Padding(src []byte, blockSize int) []byte {
// PKCS5Padding applies PKCS#5 padding to the source byte slice to match the given block size.
//
// If the block size is not provided, it defaults to 8.
func PKCS5Padding(src []byte, blockSize ...int) []byte {
hailaz marked this conversation as resolved.
Show resolved Hide resolved
blockSizeTemp := 8
if len(blockSize) > 0 {
blockSizeTemp = blockSize[0]
}
return PKCS7Padding(src, blockSizeTemp)
}

// PKCS5UnPadding removes PKCS#5 padding from the source byte slice based on the given block size.
//
// If the block size is not provided, it defaults to 8.
func PKCS5UnPadding(src []byte, blockSize ...int) ([]byte, error) {
hailaz marked this conversation as resolved.
Show resolved Hide resolved
blockSizeTemp := 8
if len(blockSize) > 0 {
blockSizeTemp = blockSize[0]
}
return PKCS7UnPadding(src, blockSizeTemp)
}

// PKCS7Padding applies PKCS#7 padding to the source byte slice to match the given block size.
func PKCS7Padding(src []byte, blockSize int) []byte {
hailaz marked this conversation as resolved.
Show resolved Hide resolved
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}

func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) {
// PKCS7UnPadding removes PKCS#7 padding from the source byte slice based on the given block size.
func PKCS7UnPadding(src []byte, blockSize int) ([]byte, error) {
hailaz marked this conversation as resolved.
Show resolved Hide resolved
length := len(src)
if blockSize <= 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid blocklen")
return nil, gerror.NewCode(gcode.CodeInvalidParameter, fmt.Sprintf("invalid blockSize: %d", blockSize))
}

if length%blockSize != 0 || length == 0 {
Expand All @@ -105,7 +130,7 @@ func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) {

unpadding := int(src[length-1])
if unpadding > blockSize || unpadding == 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid padding")
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid unpadding")
}

padding := src[length-unpadding:]
Expand Down
20 changes: 19 additions & 1 deletion crypto/gaes/gaes_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestDecrypt(t *testing.T) {
t.Assert(decrypt, content)

decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv)
t.Assert(err, "invalid padding")
t.Assert(err, "invalid unpadding")
})
}

Expand Down Expand Up @@ -128,6 +128,24 @@ func TestPKCS5UnPaddingErr(t *testing.T) {
_, err = gaes.PKCS5UnPadding(key_32_err, 32)
t.AssertNE(err, nil)
})

gtest.C(t, func(t *gtest.T) {
// PKCS7UnPadding blockSize zero
_, err := gaes.PKCS7UnPadding(content, 0)
t.AssertNE(err, nil)

// PKCS7UnPadding src len zero
_, err = gaes.PKCS7UnPadding([]byte(""), 16)
t.AssertNE(err, nil)

// PKCS7UnPadding src len > blockSize
_, err = gaes.PKCS7UnPadding(key_17, 16)
t.AssertNE(err, nil)

// PKCS7UnPadding src len > blockSize
_, err = gaes.PKCS7UnPadding(key_32_err, 32)
t.AssertNE(err, nil)
})
}

func TestEncryptCFB(t *testing.T) {
Expand Down
Loading