-
Notifications
You must be signed in to change notification settings - Fork 21
/
zuc256_mac.go
219 lines (169 loc) · 4.43 KB
/
zuc256_mac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package zuc
const ZUC256_KEY_SIZE = 32
const ZUC256_IV_SIZE = 23
const ZUC256_MAC32_SIZE = 4
const ZUC256_MAC64_SIZE = 8
const ZUC256_MAC128_SIZE = 16
const ZUC256_MIN_MAC_SIZE = ZUC256_MAC32_SIZE
const ZUC256_MAX_MAC_SIZE = ZUC256_MAC128_SIZE
type Zuc256Mac struct {
LFSR [16]uint32
R1 uint32
R2 uint32
T [4]uint32
K0 [4]uint32
buf [4]byte
buflen int
macbits int32
}
func NewZuc256Mac(key []byte, iv []byte, macbits int32) *Zuc256Mac {
if l := len(key); l != 32 {
panic(KeySizeError(l))
}
if l := len(iv); l != 23 {
panic(IVSizeError(l))
}
m := new(Zuc256Mac)
m.init(key, iv, macbits)
return m
}
func (this *Zuc256Mac) Size() int {
return int(this.macbits / 8)
}
func (this *Zuc256Mac) BlockSize() int {
return ZUC256_IV_SIZE
}
func (this *Zuc256Mac) Reset() {
this.LFSR = [16]uint32{}
this.T = [4]uint32{}
this.buf = [4]byte{}
this.buflen = 0
}
// key = 32
// iv = 23
func (this *Zuc256Mac) init(key []byte, iv []byte, macbits int32) {
if macbits < 32 {
macbits = 32
} else if macbits > 64 {
macbits = 128
}
state := NewZuc256StateWithMacbits(key, iv, macbits)
state.GenerateKeystream(int(macbits/32), this.T[:])
state.GenerateKeystream(int(macbits/32), this.K0[:])
this.LFSR = state.LFSR
this.R1 = state.R1
this.R2 = state.R2
this.macbits = (macbits/32) * 32
}
func (this *Zuc256Mac) generateKeyword() uint32 {
ss := &Zuc256State{
LFSR: this.LFSR,
R1: this.R1,
R2: this.R2,
}
K1 := ss.GenerateKeyword()
this.LFSR = ss.LFSR
this.R1 = ss.R1
this.R2 = ss.R2
return K1
}
func (this *Zuc256Mac) Write(data []byte) (nn int, err error) {
var K1, M uint32
var n int = int(this.macbits / 32)
var i, j int
nn = len(data)
if nn == 0 {
return
}
var dataLen int = len(data)
if this.buflen > 0 {
var num int = len(this.buf) - this.buflen
if (dataLen < num) {
copy(this.buf[this.buflen:], data)
this.buflen += dataLen
return
}
copy(this.buf[this.buflen:], data[:num])
M = GETU32(this.buf[:])
this.buflen = 0
K1 = this.generateKeyword()
for i = 0; i < 32; i++ {
if (M & 0x80000000) > 0 {
for j = 0; j < n; j++ {
this.T[j] ^= this.K0[j]
}
}
M <<= 1
for j = 0; j < n - 1; j++ {
this.K0[j] = (this.K0[j] << 1) | (this.K0[j + 1] >> 31)
}
this.K0[j] = (this.K0[j] << 1) | (K1 >> 31)
K1 <<= 1
}
data = data[num:]
dataLen -= num
}
for dataLen >= 4 {
M = GETU32(data)
K1 = this.generateKeyword()
for i = 0; i < 32; i++ {
if (M & 0x80000000) > 0 {
for j = 0; j < n; j++ {
this.T[j] ^= this.K0[j]
}
}
M <<= 1
for j = 0; j < n - 1; j++ {
this.K0[j] = (this.K0[j] << 1) | (this.K0[j + 1] >> 31)
}
this.K0[j] = (this.K0[j] << 1) | (K1 >> 31)
K1 <<= 1
}
data = data[4:]
dataLen -= 4
}
if dataLen > 0 {
copy(this.buf[:], data)
this.buflen = dataLen
}
return
}
func (this *Zuc256Mac) Sum(data []byte, nbits int) []byte {
var K1, M uint32
var n int = int(this.macbits/32)
var i, j int
var mac []byte = make([]byte, n * 4)
if len(data) == 0 {
nbits = 0
}
if nbits >= 8 {
this.Write(data[:nbits/8])
data = data[nbits/8:]
nbits %= 8
}
if nbits > 0 {
this.buf[this.buflen] = data[0]
}
if this.buflen > 0 || nbits > 0 {
M = GETU32(this.buf[:])
K1 = this.generateKeyword()
for i = 0; i < this.buflen * 8 + nbits; i++ {
if (M & 0x80000000) > 0 {
for j = 0; j < n; j++ {
this.T[j] ^= this.K0[j]
}
}
M <<= 1
for j = 0; j < n - 1; j++ {
this.K0[j] = (this.K0[j] << 1) | (this.K0[j + 1] >> 31)
}
this.K0[j] = (this.K0[j] << 1) | (K1 >> 31)
K1 <<= 1
}
}
for j = 0; j < n; j++ {
this.T[j] ^= this.K0[j]
PUTU32(mac[j * 4:], this.T[j])
}
return mac
}