-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwr1partition.go
348 lines (326 loc) · 9.73 KB
/
wr1partition.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
package iapi
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/immesys/wave/serdes"
"github.com/immesys/wave/wve"
)
func CalculateWR1Partition(validFrom time.Time, validUntil time.Time, userPrefix [][]byte) ([][]byte, wve.WVE) {
if len(userPrefix) > 12 {
return nil, wve.Err(wve.InvalidParameter, "user prefix partition must be < 12 elements")
}
tiers := WR1PartitionTiers
endNS := validUntil.UnixNano()
endNS -= endNS % tiers[3]
endNS += tiers[3]
startNS := validFrom.UnixNano()
startNS -= startNS % tiers[3]
intstart := make([]int64, 4)
intend := make([]int64, 4)
bytestart := make([][]byte, 4)
byteend := make([][]byte, 4)
for i := 0; i < 4; i++ {
intstart[i] = startNS / tiers[i]
intend[i] = endNS / tiers[i]
bytestart[i] = make([]byte, 2)
binary.BigEndian.PutUint16(bytestart[i], uint16(intstart[i]))
byteend[i] = make([]byte, 2)
binary.BigEndian.PutUint16(byteend[i], uint16(intend[i]))
}
// fmt.Printf("calculated wr1 partition is [%d %d %d %d] [%d %d %d %d]\n",
// intstart[0], intstart[1], intstart[2], intstart[3],
// intend[0], intend[1], intend[2], intend[3])
rv := make([][]byte, 20)
for i := 0; i < 12; i++ {
if i >= len(userPrefix) {
break
}
if userPrefix[i] != nil {
rv[i] = userPrefix[i]
}
}
for i := 0; i < 4; i++ {
rv[12+i] = bytestart[i]
rv[16+i] = byteend[i]
}
return rv, nil
}
//This generates the partitions and calculates the differences to generate the keyring bundle entries, but it does not generate the keys
func CalculateEmptyKeyBundleEntries(startDat time.Time, endDat time.Time, userPrefix [][]byte) ([][][]byte, []serdes.BLS12381OAQUEKeyringBundleEntry, wve.WVE) {
partitions, err := CalculateKeyBundlePartitions(startDat, endDat, userPrefix)
if err != nil {
return nil, nil, err
}
return partitions, CalculateEmptyKeyBundleEntriesFromPartitions(partitions), nil
}
func CalculateEmptyKeyBundleEntriesFromPartitions(partitions [][][]byte) []serdes.BLS12381OAQUEKeyringBundleEntry {
rv := make([]serdes.BLS12381OAQUEKeyringBundleEntry, len(partitions))
current := make([][]byte, 20)
for i := 0; i < len(partitions); i++ {
for idx, p := range partitions[i] {
//Add all the changes
if !bytes.Equal(p, current[idx]) {
cp := make([]byte, len(p))
copy(cp[:], p[:])
rv[i].PartitionChange = append(rv[i].PartitionChange, serdes.PartitionChange{
Index: idx,
Content: cp,
})
current[idx] = cp
}
//Move on to the next partition
}
}
return rv
}
func DecodeKeyBundleEntries(be []serdes.BLS12381OAQUEKeyringBundleEntry) ([][][]byte, wve.WVE) {
rv := make([][][]byte, len(be))
current := make([][]byte, 20)
for i := 0; i < len(rv); i++ {
for _, change := range be[i].PartitionChange {
if change.Index > 20 {
return nil, wve.Err(wve.MalformedObject, "bad partition change record")
}
current[change.Index] = change.Content
}
cp := make([][]byte, 20)
for k := 0; k < 20; k++ {
if len(current[k]) > 0 {
cp[k] = current[k]
}
}
rv[i] = cp
}
return rv, nil
}
//The WR1 recommended partition scheme is
// <userdefined: 12> <beginrange: 4> <endrange: 4>
// Which allows for expiry ranges at a granularity of weeks
// In the worst case this requires a key bundle of 144 keys
// to delegate. Ranges are only allowed to go up to 3 years
// long.
var WR1PartitionTiers []int64 = []int64{int64(64 * 7 * 24 * time.Hour), int64(16 * 7 * 24 * time.Hour), int64(4 * 7 * 24 * time.Hour), int64(7 * 24 * time.Hour)}
func CalculateKeyBundlePartitions(startDat time.Time, endDat time.Time, userPrefix [][]byte) ([][][]byte, wve.WVE) {
//round up the end date by a week
tiers := WR1PartitionTiers
left := [][]int64{}
leftTimeRanges := []DateRange{}
right := [][]int64{}
rightTimeRanges := []DateRange{}
_ = left
_ = right
//The left side, or beginrange, covers all times that permitted ranges can start in. Therefore it must cover (startDate-3years)..(endDate)
endNS := endDat.UnixNano()
endNS -= endNS % tiers[3]
endNS += tiers[3]
startNS := startDat.UnixNano()
startNS -= startNS % tiers[3]
//First calculate the left
cursor := startNS - int64(3*365*24*time.Hour)
cursor -= (cursor % tiers[0])
current := make([]int64, len(tiers))
current[0] = cursor / tiers[0]
tier := 0
//fmt.Printf("end date is %s\n", time.Unix(0, endNS))
for cursor < endNS {
if cursor+tiers[tier] <= endNS {
//append current
var sd, ed time.Time
for lastentry := len(tiers) - 1; lastentry >= 0; lastentry-- {
if current[lastentry] != 0 {
sd = time.Unix(0, current[lastentry]*tiers[lastentry])
ed = time.Unix(0, (current[lastentry]+1)*tiers[lastentry])
break
}
}
//fmt.Printf("would use %4v %s -> %s\n", current, sd, ed)
cp := make([]int64, len(current))
copy(cp[:], current[:])
left = append(left, cp)
leftTimeRanges = append(leftTimeRanges, DateRange{sd, ed})
cursor += tiers[tier]
current[tier] = cursor / tiers[tier]
} else {
tier++
if tier < len(tiers) {
current[tier] = cursor / tiers[tier]
}
}
if tier == len(tiers) {
break
}
}
cursor = endNS + int64(3*365*24*time.Hour)
cursor -= cursor % tiers[0]
cursor += tiers[0]
//fmt.Printf("corrected end date for start is %s\n", time.Unix(0, cursor))
current = make([]int64, len(tiers))
current[0] = cursor / tiers[0]
tier = 0
//fmt.Printf("start date is %s\n", time.Unix(0, startNS))
for cursor > startNS {
if cursor-tiers[tier] >= startNS {
//append current
var sd, ed time.Time
for lastentry := len(tiers) - 1; lastentry >= 0; lastentry-- {
if current[lastentry] != 0 {
sd = time.Unix(0, (current[lastentry]-1)*tiers[lastentry])
ed = time.Unix(0, (current[lastentry])*tiers[lastentry])
break
}
}
cp := make([]int64, len(current))
for idx, e := range current {
if e != 0 {
//switch from end time to start time
cp[idx] = e - 1
}
}
right = append(right, cp)
rightTimeRanges = append(rightTimeRanges, DateRange{sd, ed})
//fmt.Printf("would use %4v %s -> %s\n", cp, sd, ed)
cursor -= tiers[tier]
current[tier] = cursor / tiers[tier]
} else {
tier++
if tier < len(tiers) {
current[tier] = cursor / tiers[tier]
}
}
if tier == len(tiers) {
break
}
}
//Now multiply left and right
results := make([][][]byte, 0, len(left)*len(right))
for li := 0; li < len(left); li++ {
for ri := 0; ri < len(right); ri++ {
//First check that the right start is not more than three years after
//the left end. In that case this combination could never be used because
//attestations can not be longer than 3 years
if leftTimeRanges[li].End.Add(3 * 365 * 24 * time.Hour).Before(rightTimeRanges[ri].Start) {
continue
}
r := make([][]byte, 20)
copy(r[:], userPrefix)
for i := 0; i < 4; i++ {
var e []byte
if left[li][i] != 0 {
e = make([]byte, 2)
binary.BigEndian.PutUint16(e, uint16(left[li][i]))
}
r[12+i] = e
}
for i := 0; i < 4; i++ {
var e []byte
if right[ri][i] != 0 {
e = make([]byte, 2)
binary.BigEndian.PutUint16(e, uint16(right[ri][i]))
}
r[16+i] = e
}
results = append(results, r)
}
}
return results, nil
}
func WR1PartitionToIntString(p [][]byte) string {
result := bytes.Buffer{}
result.WriteString("[")
for i := 0; i < 12; i++ {
if p[i] == nil {
break
}
result.WriteString(fmt.Sprintf("%q/", string(p[i])))
}
result.WriteString("] ")
parseChunk := func(chunk [][]byte) []int64 {
ichunk := make([]int64, 4)
for i := 0; i < 4; i++ {
if len(chunk[i]) == 0 {
break
}
if len(chunk[i]) != 2 {
panic("this is just a debug function")
}
ichunk[i] = int64(binary.BigEndian.Uint16(chunk[i]))
if ichunk[i] == 0 {
panic("it was literally zero\n")
}
}
return ichunk
}
left := parseChunk(p[12:16])
right := parseChunk(p[16:])
result.WriteString(fmt.Sprintf("(%d %d %d %d)", left[0], left[1], left[2], left[3]))
result.WriteString(fmt.Sprintf("(%d %d %d %d)", right[0], right[1], right[2], right[3]))
return result.String()
}
func WR1PartitionToString(p [][]byte) string {
tfmt := "2006-01-02 15:04:05"
result := bytes.Buffer{}
result.WriteString("[")
for i := 0; i < 12; i++ {
if p[i] == nil {
break
}
result.WriteString(fmt.Sprintf("%q/", string(p[i])))
}
result.WriteString("] ")
startRange, startErr := WR1PartitionChunkToDateRange(p[12:16])
if startErr != nil {
result.WriteString("(start range invalid) ")
} else {
result.WriteString(fmt.Sprintf("(%s -> %s) ",
startRange.Start.Format(tfmt),
startRange.End.Format(tfmt)))
}
endRange, endErr := WR1PartitionChunkToDateRange(p[16:])
if endErr != nil {
result.WriteString("(end range invalid)")
} else {
result.WriteString(fmt.Sprintf("(%s -> %s) ",
endRange.Start.Format(tfmt),
endRange.End.Format(tfmt)))
}
return result.String()
}
type DateRange struct {
Start time.Time
End time.Time
}
func ParseWR1Partition(p [][]byte) (start *DateRange, end *DateRange, user [][]byte, err wve.WVE) {
startRange, startErr := WR1PartitionChunkToDateRange(p[12:16])
if startErr != nil {
return nil, nil, nil, startErr
}
endRange, endErr := WR1PartitionChunkToDateRange(p[16:])
if endErr != nil {
return nil, nil, nil, endErr
}
return startRange, endRange, p[0:12], nil
}
func WR1PartitionChunkToDateRange(chunk [][]byte) (*DateRange, wve.WVE) {
ichunk := make([]int64, 4)
for i := 0; i < 4; i++ {
if len(chunk[i]) == 0 {
break
}
if len(chunk[i]) != 2 {
return nil, wve.Err(wve.MalformedPartition, "not valid WR1 partition")
}
ichunk[i] = int64(binary.BigEndian.Uint16(chunk[i]))
}
for i := 3; i >= 0; i-- {
if ichunk[i] != 0 {
rv := DateRange{
Start: time.Unix(0, ichunk[i]*WR1PartitionTiers[i]),
}
rv.End = rv.Start.Add(time.Duration(WR1PartitionTiers[i]))
return &rv, nil
}
}
return nil, wve.Err(wve.MalformedPartition, "not valid WR1 partition")
}