zuc: expose methods to support encoding.BinaryMarshaler and encoding.BinaryUnmarshaler

This commit is contained in:
Sun Yimin 2025-09-29 15:49:43 +08:00 committed by GitHub
parent 62ee2eb20e
commit cf3e2cd375
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -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)
}

View File

@ -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])
}