|
|
@ -35,17 +35,23 @@ const (
|
|
|
|
|
|
|
|
|
|
|
|
// Cipher cipher
|
|
|
|
// Cipher cipher
|
|
|
|
type Cipher struct {
|
|
|
|
type Cipher struct {
|
|
|
|
cipher C.gnutls_cipher_hd_t
|
|
|
|
cipher C.gnutls_cipher_hd_t
|
|
|
|
t int
|
|
|
|
t int
|
|
|
|
|
|
|
|
blockSize int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewCipher create cipher
|
|
|
|
// NewCipher create cipher
|
|
|
|
func NewCipher(t int, key []byte, iv []byte) (*Cipher, error) {
|
|
|
|
func NewCipher(t int, key []byte, iv []byte) (*Cipher, error) {
|
|
|
|
ivSize := C.cipher_get_block_size(C.int(t))
|
|
|
|
keysize := GetCipherKeySize(t)
|
|
|
|
blockSize := C.cipher_get_iv_size(C.int(t))
|
|
|
|
ivSize := GetCipherIVSize(t)
|
|
|
|
if len(key) != int(blockSize) || len(iv) != int(ivSize) {
|
|
|
|
blocksize := GetCipherBlockSize(t)
|
|
|
|
|
|
|
|
//log.Printf("block size: %d, iv size: %d", int(ivSize), int(blockSize))
|
|
|
|
|
|
|
|
if len(key) != int(keysize) {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("wrong key size")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("wrong block/iv size")
|
|
|
|
if len(iv) != int(ivSize) {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("wrong iv size")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ckey := C.CBytes(key)
|
|
|
|
ckey := C.CBytes(key)
|
|
|
@ -59,13 +65,12 @@ func NewCipher(t int, key []byte, iv []byte) (*Cipher, error) {
|
|
|
|
log.Println("new cipher return nil")
|
|
|
|
log.Println("new cipher return nil")
|
|
|
|
return nil, nil
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Cipher{c, t}, nil
|
|
|
|
return &Cipher{c, t, blocksize}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Encrypt encrypt
|
|
|
|
// Encrypt encrypt
|
|
|
|
func (c *Cipher) Encrypt(buf []byte) ([]byte, error) {
|
|
|
|
func (c *Cipher) Encrypt(buf []byte) ([]byte, error) {
|
|
|
|
blockSize := C.cipher_get_iv_size(C.int(c.t))
|
|
|
|
if len(buf)%c.blockSize != 0 {
|
|
|
|
if len(buf)%int(blockSize) != 0 {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("wrong block size")
|
|
|
|
return nil, fmt.Errorf("wrong block size")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -86,8 +91,7 @@ func (c *Cipher) Encrypt(buf []byte) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
|
|
// Decrypt decrypt
|
|
|
|
// Decrypt decrypt
|
|
|
|
func (c *Cipher) Decrypt(buf []byte) ([]byte, error) {
|
|
|
|
func (c *Cipher) Decrypt(buf []byte) ([]byte, error) {
|
|
|
|
blockSize := C.cipher_get_iv_size(C.int(c.t))
|
|
|
|
if len(buf)%c.blockSize != 0 {
|
|
|
|
if len(buf)%int(blockSize) != 0 {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("wrong block size")
|
|
|
|
return nil, fmt.Errorf("wrong block size")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -111,3 +115,18 @@ func (c *Cipher) Close() error {
|
|
|
|
C.gnutls_cipher_deinit(c.cipher)
|
|
|
|
C.gnutls_cipher_deinit(c.cipher)
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetCipherKeySize get the cipher algorithm key length
|
|
|
|
|
|
|
|
func GetCipherKeySize(t int) int {
|
|
|
|
|
|
|
|
return int(C.gnutls_cipher_get_key_size(C.gnutls_cipher_algorithm_t(t)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetCipherIVSize get the cipher algorithm iv length
|
|
|
|
|
|
|
|
func GetCipherIVSize(t int) int {
|
|
|
|
|
|
|
|
return int(C.gnutls_cipher_get_iv_size(C.gnutls_cipher_algorithm_t(t)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetCipherBlockSize get the cipher algorithm block size
|
|
|
|
|
|
|
|
func GetCipherBlockSize(t int) int {
|
|
|
|
|
|
|
|
return int(C.gnutls_cipher_get_block_size(C.gnutls_cipher_algorithm_t(t)))
|
|
|
|
|
|
|
|
}
|
|
|
|