sm4: define & use type KeySizeError

This commit is contained in:
Sun Yimin 2025-03-05 10:56:13 +08:00 committed by GitHub
parent 759bb4c0b9
commit 67f187b1d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,7 @@ package sm4
import (
"crypto/cipher"
"fmt"
"strconv"
"github.com/emmansun/gmsm/internal/alias"
)
@ -11,6 +11,12 @@ import (
// BlockSize the sm4 block size in bytes.
const BlockSize = 16
type KeySizeError int
func (k KeySizeError) Error() string {
return "sm4: invalid key size " + strconv.Itoa(int(k))
}
const rounds = 32
// A cipher is an instance of SM4 encryption using a particular key.
@ -19,13 +25,13 @@ type sm4Cipher struct {
dec [rounds]uint32
}
// NewCipher creates and returns a new cipher.Block.
// The key argument should be the SM4 key,
// NewCipher creates and returns a new [cipher.Block] implementation.
// The key argument should be the SM4 key, must be 16 bytes long.
func NewCipher(key []byte) (cipher.Block, error) {
k := len(key)
switch k {
default:
return nil, fmt.Errorf("sm4: invalid key size %d", k)
return nil, KeySizeError(k)
case 16:
break
}