mirror of
https://github.com/emmansun/gmsm.git
synced 2025-10-14 07:10:45 +08:00
zuc: expose methods to support encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
This commit is contained in:
parent
62ee2eb20e
commit
cf3e2cd375
15
zuc/eea.go
15
zuc/eea.go
@ -53,3 +53,18 @@ func NewCipherWithBucketSize(key, iv []byte, bucketSize int) (cipher.SeekableStr
|
|||||||
func NewEEACipherWithBucketSize(key []byte, count, bearer, direction uint32, bucketSize int) (cipher.SeekableStream, error) {
|
func NewEEACipherWithBucketSize(key []byte, count, bearer, direction uint32, bucketSize int) (cipher.SeekableStream, error) {
|
||||||
return zuc.NewEEACipherWithBucketSize(key, count, bearer, direction, bucketSize)
|
return zuc.NewEEACipherWithBucketSize(key, count, bearer, direction, bucketSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEmptyEEACipher creates and returns a new empty ZUC-EEA cipher instance.
|
||||||
|
// This function initializes an empty eea struct that can be used for
|
||||||
|
// unmarshaling a previously saved state using the UnmarshalBinary method.
|
||||||
|
// The returned cipher instance is not ready for encryption or decryption.
|
||||||
|
func NewEmptyEEACipher() cipher.SeekableStream {
|
||||||
|
return zuc.NewEmptyCipher()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalEEACipher reconstructs a ZUC cipher instance from a serialized byte slice.
|
||||||
|
// It attempts to deserialize the provided data into a seekable stream cipher
|
||||||
|
// that can be used for encryption/decryption operations.
|
||||||
|
func UnmarshalEEACipher(data []byte) (cipher.SeekableStream, error) {
|
||||||
|
return zuc.UnmarshalCipher(data)
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package zuc
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"encoding"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -113,9 +114,12 @@ func TestXORStreamAt(t *testing.T) {
|
|||||||
t.Errorf("expected=%x, result=%x\n", expected[32:64], dst[32:64])
|
t.Errorf("expected=%x, result=%x\n", expected[32:64], dst[32:64])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
data, _ := c.(encoding.BinaryMarshaler).MarshalBinary()
|
||||||
|
c2 := NewEmptyEEACipher()
|
||||||
|
c2.(encoding.BinaryUnmarshaler).UnmarshalBinary(data)
|
||||||
for i := 1; i < 4; i++ {
|
for i := 1; i < 4; i++ {
|
||||||
c.XORKeyStreamAt(dst[:i], src[:i], 0)
|
c.XORKeyStreamAt(dst[:i], src[:i], 0)
|
||||||
c.XORKeyStreamAt(dst[32:64], src[32:64], 32)
|
c2.XORKeyStreamAt(dst[32:64], src[32:64], 32)
|
||||||
if !bytes.Equal(dst[32:64], expected[32:64]) {
|
if !bytes.Equal(dst[32:64], expected[32:64]) {
|
||||||
t.Errorf("expected=%x, result=%x\n", expected[32:64], dst[32:64])
|
t.Errorf("expected=%x, result=%x\n", expected[32:64], dst[32:64])
|
||||||
}
|
}
|
||||||
@ -128,8 +132,10 @@ func TestXORStreamAt(t *testing.T) {
|
|||||||
if !bytes.Equal(dst[3:16], expected[3:16]) {
|
if !bytes.Equal(dst[3:16], expected[3:16]) {
|
||||||
t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16])
|
t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16])
|
||||||
}
|
}
|
||||||
|
data, _ := c.(encoding.BinaryMarshaler).MarshalBinary()
|
||||||
|
c2, _ := UnmarshalEEACipher(data)
|
||||||
c.XORKeyStreamAt(dst[:1], src[:1], 0)
|
c.XORKeyStreamAt(dst[:1], src[:1], 0)
|
||||||
c.XORKeyStreamAt(dst[4:16], src[4:16], 4)
|
c2.XORKeyStreamAt(dst[4:16], src[4:16], 4)
|
||||||
if !bytes.Equal(dst[4:16], expected[4:16]) {
|
if !bytes.Equal(dst[4:16], expected[4:16]) {
|
||||||
t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16])
|
t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16])
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user