Skip to content

Commit cf3e2cd

Browse files
authored
zuc: expose methods to support encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
1 parent 62ee2eb commit cf3e2cd

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

zuc/eea.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,18 @@ func NewCipherWithBucketSize(key, iv []byte, bucketSize int) (cipher.SeekableStr
5353
func NewEEACipherWithBucketSize(key []byte, count, bearer, direction uint32, bucketSize int) (cipher.SeekableStream, error) {
5454
return zuc.NewEEACipherWithBucketSize(key, count, bearer, direction, bucketSize)
5555
}
56+
57+
// NewEmptyEEACipher creates and returns a new empty ZUC-EEA cipher instance.
58+
// This function initializes an empty eea struct that can be used for
59+
// unmarshaling a previously saved state using the UnmarshalBinary method.
60+
// The returned cipher instance is not ready for encryption or decryption.
61+
func NewEmptyEEACipher() cipher.SeekableStream {
62+
return zuc.NewEmptyCipher()
63+
}
64+
65+
// UnmarshalEEACipher reconstructs a ZUC cipher instance from a serialized byte slice.
66+
// It attempts to deserialize the provided data into a seekable stream cipher
67+
// that can be used for encryption/decryption operations.
68+
func UnmarshalEEACipher(data []byte) (cipher.SeekableStream, error) {
69+
return zuc.UnmarshalCipher(data)
70+
}

zuc/eea_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package zuc
33
import (
44
"bytes"
55
"crypto/cipher"
6+
"encoding"
67
"encoding/hex"
78
"testing"
89

@@ -113,9 +114,12 @@ func TestXORStreamAt(t *testing.T) {
113114
t.Errorf("expected=%x, result=%x\n", expected[32:64], dst[32:64])
114115
}
115116
}
117+
data, _ := c.(encoding.BinaryMarshaler).MarshalBinary()
118+
c2 := NewEmptyEEACipher()
119+
c2.(encoding.BinaryUnmarshaler).UnmarshalBinary(data)
116120
for i := 1; i < 4; i++ {
117121
c.XORKeyStreamAt(dst[:i], src[:i], 0)
118-
c.XORKeyStreamAt(dst[32:64], src[32:64], 32)
122+
c2.XORKeyStreamAt(dst[32:64], src[32:64], 32)
119123
if !bytes.Equal(dst[32:64], expected[32:64]) {
120124
t.Errorf("expected=%x, result=%x\n", expected[32:64], dst[32:64])
121125
}
@@ -128,8 +132,10 @@ func TestXORStreamAt(t *testing.T) {
128132
if !bytes.Equal(dst[3:16], expected[3:16]) {
129133
t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16])
130134
}
135+
data, _ := c.(encoding.BinaryMarshaler).MarshalBinary()
136+
c2, _ := UnmarshalEEACipher(data)
131137
c.XORKeyStreamAt(dst[:1], src[:1], 0)
132-
c.XORKeyStreamAt(dst[4:16], src[4:16], 4)
138+
c2.XORKeyStreamAt(dst[4:16], src[4:16], 4)
133139
if !bytes.Equal(dst[4:16], expected[4:16]) {
134140
t.Errorf("expected=%x, result=%x\n", expected[3:16], dst[3:16])
135141
}

0 commit comments

Comments
 (0)