mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 20:26:19 +08:00
sm4: define & use type KeySizeError
This commit is contained in:
parent
759bb4c0b9
commit
67f187b1d3
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user