mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-27 12:46:18 +08:00
sm4: define & use type KeySizeError
This commit is contained in:
parent
759bb4c0b9
commit
67f187b1d3
@ -3,7 +3,7 @@ package sm4
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"fmt"
|
"strconv"
|
||||||
|
|
||||||
"github.com/emmansun/gmsm/internal/alias"
|
"github.com/emmansun/gmsm/internal/alias"
|
||||||
)
|
)
|
||||||
@ -11,6 +11,12 @@ import (
|
|||||||
// BlockSize the sm4 block size in bytes.
|
// BlockSize the sm4 block size in bytes.
|
||||||
const BlockSize = 16
|
const BlockSize = 16
|
||||||
|
|
||||||
|
type KeySizeError int
|
||||||
|
|
||||||
|
func (k KeySizeError) Error() string {
|
||||||
|
return "sm4: invalid key size " + strconv.Itoa(int(k))
|
||||||
|
}
|
||||||
|
|
||||||
const rounds = 32
|
const rounds = 32
|
||||||
|
|
||||||
// A cipher is an instance of SM4 encryption using a particular key.
|
// A cipher is an instance of SM4 encryption using a particular key.
|
||||||
@ -19,13 +25,13 @@ type sm4Cipher struct {
|
|||||||
dec [rounds]uint32
|
dec [rounds]uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCipher creates and returns a new cipher.Block.
|
// NewCipher creates and returns a new [cipher.Block] implementation.
|
||||||
// The key argument should be the SM4 key,
|
// The key argument should be the SM4 key, must be 16 bytes long.
|
||||||
func NewCipher(key []byte) (cipher.Block, error) {
|
func NewCipher(key []byte) (cipher.Block, error) {
|
||||||
k := len(key)
|
k := len(key)
|
||||||
switch k {
|
switch k {
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("sm4: invalid key size %d", k)
|
return nil, KeySizeError(k)
|
||||||
case 16:
|
case 16:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user