diff --git a/zuc/eea.go b/zuc/eea.go index 9875c89..73e46f6 100644 --- a/zuc/eea.go +++ b/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) { 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) +} diff --git a/zuc/eea_test.go b/zuc/eea_test.go index e979096..e3926ec 100644 --- a/zuc/eea_test.go +++ b/zuc/eea_test.go @@ -3,6 +3,7 @@ package zuc import ( "bytes" "crypto/cipher" + "encoding" "encoding/hex" "testing" @@ -113,9 +114,12 @@ func TestXORStreamAt(t *testing.T) { 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++ { 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]) { 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]) { 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[4:16], src[4:16], 4) + c2.XORKeyStreamAt(dst[4:16], src[4:16], 4) if !bytes.Equal(dst[4:16], expected[4:16]) { t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16]) }