-
Notifications
You must be signed in to change notification settings - Fork 145
/
k12.go
400 lines (337 loc) · 9.02 KB
/
k12.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// k12 implements the KangarooTwelve XOF.
//
// KangarooTwelve is being standardised at the CFRG working group
// of the IRTF. This package implements draft 10.
//
// https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/10/
package k12
import (
"encoding/binary"
"github.com/cloudflare/circl/internal/sha3"
"github.com/cloudflare/circl/simd/keccakf1600"
)
const chunkSize = 8192 // aka B
// KangarooTwelve splits the message into chunks of 8192 bytes each.
// The first chunk is absorbed directly in a TurboSHAKE128 instance, which
// we call the stalk. The subsequent chunks aren't absorbed directly, but
// instead their hash is absorbed: they're like leafs on a stalk.
// If we have a fast TurboSHAKE128 available, we buffer chunks until we have
// enough to do the parallel TurboSHAKE128. If not, we absorb directly into
// a separate TurboSHAKE128 state.
type State struct {
initialTodo int // Bytes left to absorb for the first chunk.
stalk sha3.State
context []byte // context string "C" provided by the user
// buffer of incoming data so we can do parallel TurboSHAKE128:
// nil when we haven't absorbed the first chunk yet;
// empty if we have, but we do not have a fast parallel TurboSHAKE128;
// and chunkSize*lanes in length if we have.
buf []byte
offset int // offset in buf or bytes written to leaf
// Number of chunk hashes ("CV_i") absorbed into the stalk.
chunk uint
// TurboSHAKE128 instance to compute the leaf in case we don't have
// a fast parallel TurboSHAKE128, viz when lanes == 1.
leaf *sha3.State
lanes uint8 // number of TurboSHAKE128s to compute in parallel
}
// NewDraft10 creates a new instance of Kangaroo12 draft version -10.
func NewDraft10(c []byte) State {
var lanes byte = 1
if keccakf1600.IsEnabledX4() {
lanes = 4
} else if keccakf1600.IsEnabledX2() {
lanes = 2
}
return newDraft10(c, lanes)
}
func newDraft10(c []byte, lanes byte) State {
return State{
initialTodo: chunkSize,
stalk: sha3.NewTurboShake128(0x07),
context: c,
lanes: lanes,
}
}
func (s *State) Reset() {
s.initialTodo = chunkSize
s.stalk.Reset()
s.stalk.SwitchDS(0x07)
s.buf = nil
s.offset = 0
s.chunk = 0
}
func (s *State) Clone() State {
stalk := s.stalk.Clone().(*sha3.State)
ret := State{
initialTodo: s.initialTodo,
stalk: *stalk,
context: s.context,
offset: s.offset,
chunk: s.chunk,
lanes: s.lanes,
}
if s.leaf != nil {
ret.leaf = s.leaf.Clone().(*sha3.State)
}
if s.buf != nil {
ret.buf = make([]byte, len(s.buf))
copy(ret.buf, s.buf)
}
return ret
}
func Draft10Sum(hash []byte, msg []byte, c []byte) {
// TODO Tweak number of lanes depending on the length of the message
s := NewDraft10(c)
_, _ = s.Write(msg)
_, _ = s.Read(hash)
}
func (s *State) Write(p []byte) (int, error) {
written := len(p)
// The first chunk is written directly to the stalk.
if s.initialTodo > 0 {
taken := s.initialTodo
if len(p) < taken {
taken = len(p)
}
headP := p[:taken]
_, _ = s.stalk.Write(headP)
s.initialTodo -= taken
p = p[taken:]
}
if len(p) == 0 {
return written, nil
}
// If this is the first bit of data written after the initial chunk,
// we're out of the fast-path and allocate some buffers.
if s.buf == nil {
if s.lanes != 1 {
s.buf = make([]byte, int(s.lanes)*chunkSize)
} else {
// We create the buffer to signal we're past the first chunk,
// but do not use it.
s.buf = make([]byte, 0)
h := sha3.NewTurboShake128(0x0B)
s.leaf = &h
}
_, _ = s.stalk.Write([]byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
s.stalk.SwitchDS(0x06)
}
// If we're just using one lane, we don't need to cache in a buffer
// for parallel hashing. Instead, we feed directly to TurboSHAKE.
if s.lanes == 1 {
for len(p) > 0 {
// Write to current leaf.
to := chunkSize - s.offset
if len(p) < to {
to = len(p)
}
_, _ = s.leaf.Write(p[:to])
p = p[to:]
s.offset += to
// Did we fill the chunk?
if s.offset == chunkSize {
var cv [32]byte
_, _ = s.leaf.Read(cv[:])
_, _ = s.stalk.Write(cv[:])
s.leaf.Reset()
s.offset = 0
s.chunk++
}
}
return written, nil
}
// If we can't fill all our lanes or the buffer isn't empty, we write the
// data to the buffer.
if s.offset != 0 || len(p) < len(s.buf) {
to := len(s.buf) - s.offset
if len(p) < to {
to = len(p)
}
p2 := p[:to]
p = p[to:]
copy(s.buf[s.offset:], p2)
s.offset += to
}
// Absorb the buffer if we filled it
if s.offset == len(s.buf) {
s.writeX(s.buf)
s.offset = 0
}
// Note that at this point we may assume that s.offset = 0 if len(p) != 0
if len(p) != 0 && s.offset != 0 {
panic("shouldn't happen")
}
// Absorb a bunch of chunks at the same time.
if len(p) >= int(s.lanes)*chunkSize {
p = s.writeX(p)
}
// Put the remainder in the buffer.
if len(p) > 0 {
copy(s.buf, p)
s.offset = len(p)
}
return written, nil
}
// Absorb a multiple of a multiple of lanes * chunkSize.
// Returns the remainder.
func (s *State) writeX(p []byte) []byte {
switch s.lanes {
case 4:
return s.writeX4(p)
default:
return s.writeX2(p)
}
}
func (s *State) writeX4(p []byte) []byte {
for len(p) >= 4*chunkSize {
var x4 keccakf1600.StateX4
a := x4.Initialize(true)
for offset := 0; offset < 48*168; offset += 168 {
for i := 0; i < 21; i++ {
a[i*4] ^= binary.LittleEndian.Uint64(
p[8*i+offset:],
)
a[i*4+1] ^= binary.LittleEndian.Uint64(
p[chunkSize+8*i+offset:],
)
a[i*4+2] ^= binary.LittleEndian.Uint64(
p[chunkSize*2+8*i+offset:],
)
a[i*4+3] ^= binary.LittleEndian.Uint64(
p[chunkSize*3+8*i+offset:],
)
}
x4.Permute()
}
for i := 0; i < 16; i++ {
a[i*4] ^= binary.LittleEndian.Uint64(
p[8*i+48*168:],
)
a[i*4+1] ^= binary.LittleEndian.Uint64(
p[chunkSize+8*i+48*168:],
)
a[i*4+2] ^= binary.LittleEndian.Uint64(
p[chunkSize*2+8*i+48*168:],
)
a[i*4+3] ^= binary.LittleEndian.Uint64(
p[chunkSize*3+8*i+48*168:],
)
}
a[16*4] ^= 0x0b
a[16*4+1] ^= 0x0b
a[16*4+2] ^= 0x0b
a[16*4+3] ^= 0x0b
a[20*4] ^= 0x80 << 56
a[20*4+1] ^= 0x80 << 56
a[20*4+2] ^= 0x80 << 56
a[20*4+3] ^= 0x80 << 56
x4.Permute()
var buf [32 * 4]byte
for i := 0; i < 4; i++ {
binary.LittleEndian.PutUint64(buf[8*i:], a[4*i])
binary.LittleEndian.PutUint64(buf[32+8*i:], a[4*i+1])
binary.LittleEndian.PutUint64(buf[32*2+8*i:], a[4*i+2])
binary.LittleEndian.PutUint64(buf[32*3+8*i:], a[4*i+3])
}
_, _ = s.stalk.Write(buf[:])
p = p[chunkSize*4:]
s.chunk += 4
}
return p
}
func (s *State) writeX2(p []byte) []byte {
// TODO On M2 Pro, 1/3 of the time is spent on this function
// and LittleEndian.Uint64 excluding the actual permutation.
// Rewriting in assembler might be worthwhile.
for len(p) >= 2*chunkSize {
var x2 keccakf1600.StateX2
a := x2.Initialize(true)
for offset := 0; offset < 48*168; offset += 168 {
for i := 0; i < 21; i++ {
a[i*2] ^= binary.LittleEndian.Uint64(
p[8*i+offset:],
)
a[i*2+1] ^= binary.LittleEndian.Uint64(
p[chunkSize+8*i+offset:],
)
}
x2.Permute()
}
for i := 0; i < 16; i++ {
a[i*2] ^= binary.LittleEndian.Uint64(
p[8*i+48*168:],
)
a[i*2+1] ^= binary.LittleEndian.Uint64(
p[chunkSize+8*i+48*168:],
)
}
a[16*2] ^= 0x0b
a[16*2+1] ^= 0x0b
a[20*2] ^= 0x80 << 56
a[20*2+1] ^= 0x80 << 56
x2.Permute()
var buf [32 * 2]byte
for i := 0; i < 4; i++ {
binary.LittleEndian.PutUint64(buf[8*i:], a[2*i])
binary.LittleEndian.PutUint64(buf[32+8*i:], a[2*i+1])
}
_, _ = s.stalk.Write(buf[:])
p = p[chunkSize*2:]
s.chunk += 2
}
return p
}
func (s *State) Read(p []byte) (int, error) {
if s.stalk.IsAbsorbing() {
// Write context string C
_, _ = s.Write(s.context)
// Write length_encode( |C| )
var buf [9]byte
binary.BigEndian.PutUint64(buf[:8], uint64(len(s.context)))
// Find first non-zero digit in big endian encoding of context length
i := 0
for buf[i] == 0 && i < 8 {
i++
}
buf[8] = byte(8 - i) // number of bytes to represent |C|
_, _ = s.Write(buf[i:])
// We need to write the chunk number if we're past the first chunk.
if s.buf != nil {
// Write last remaining chunk(s)
var cv [32]byte
if s.lanes == 1 {
if s.offset != 0 {
_, _ = s.leaf.Read(cv[:])
_, _ = s.stalk.Write(cv[:])
s.chunk++
}
} else {
remainingBuf := s.buf[:s.offset]
for len(remainingBuf) > 0 {
h := sha3.NewTurboShake128(0x0B)
to := chunkSize
if len(remainingBuf) < to {
to = len(remainingBuf)
}
_, _ = h.Write(remainingBuf[:to])
_, _ = h.Read(cv[:])
_, _ = s.stalk.Write(cv[:])
s.chunk++
remainingBuf = remainingBuf[to:]
}
}
// Write length_encode( chunk )
binary.BigEndian.PutUint64(buf[:8], uint64(s.chunk))
// Find first non-zero digit in big endian encoding of number of chunks
i = 0
for buf[i] == 0 && i < 8 {
i++
}
buf[8] = byte(8 - i) // number of bytes to represent number of chunks.
_, _ = s.stalk.Write(buf[i:])
_, _ = s.stalk.Write([]byte{0xff, 0xff})
}
}
return s.stalk.Read(p)
}