...
1
2
3
4
5
6
7 package quic
8
9 import (
10 "crypto/hmac"
11 "crypto/rand"
12 "crypto/sha256"
13 "hash"
14 "sync"
15 )
16
17 const statelessResetTokenLen = 128 / 8
18
19
20
21 type statelessResetToken [statelessResetTokenLen]byte
22
23 type statelessResetTokenGenerator struct {
24 canReset bool
25
26
27
28
29
30
31
32 mu sync.Mutex
33 mac hash.Hash
34 }
35
36 func (g *statelessResetTokenGenerator) init(secret [32]byte) {
37 zero := true
38 for _, b := range secret {
39 if b != 0 {
40 zero = false
41 break
42 }
43 }
44 if zero {
45
46 rand.Read(secret[:])
47 g.canReset = false
48 } else {
49 g.canReset = true
50 }
51 g.mac = hmac.New(sha256.New, secret[:])
52 }
53
54 func (g *statelessResetTokenGenerator) tokenForConnID(cid []byte) (token statelessResetToken) {
55 g.mu.Lock()
56 defer g.mu.Unlock()
57 defer g.mac.Reset()
58 g.mac.Write(cid)
59 copy(token[:], g.mac.Sum(nil))
60 return token
61 }
62
View as plain text