diff --git a/sm4/cipher.go b/sm4/cipher.go index 1b0ecee..6ce0611 100644 --- a/sm4/cipher.go +++ b/sm4/cipher.go @@ -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 }