-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.go
More file actions
204 lines (170 loc) · 5.43 KB
/
decode.go
File metadata and controls
204 lines (170 loc) · 5.43 KB
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
package main
import (
"crypto/aes"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"gaen/export"
"io"
"io/ioutil"
"strconv"
"strings"
"time"
"golang.org/x/crypto/hkdf"
"google.golang.org/protobuf/proto"
)
// DecodeFromFile decodes a TemporaryExposureKeyExport binary file
func DecodeFromFile(filename string) ([]*TemporaryExposureKey, error) {
export, err := UnmarshalExportFile(filename)
if err != nil {
return nil, err
}
return DecodeExport(export)
}
// UnmarshalExportFile unmarshal a TemporaryExposureKeyExport binary file
func UnmarshalExportFile(filename string) (*export.TemporaryExposureKeyExport, error) {
in, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
in = in[16:]
export := &export.TemporaryExposureKeyExport{}
if err := proto.Unmarshal(in, export); err != nil {
return nil, err
}
return export, nil
}
// DecodeExport decodes a TemporaryExposureKeyExport to a []*TemporaryExposureKey
func DecodeExport(export *export.TemporaryExposureKeyExport) ([]*TemporaryExposureKey, error) {
teks := make([]*TemporaryExposureKey, 0)
for _, tek := range export.Keys {
if tek.RollingStartIntervalNumber == nil {
return nil, errors.New("cannot decode export: RollingStartIntervalNumber is nil")
}
if tek.RollingPeriod == nil {
return nil, errors.New("cannot decode export: RollingPeriod is nil")
}
tek := NewTemporaryExposureKey(
tek.KeyData,
int(*tek.RollingStartIntervalNumber),
int(*tek.RollingPeriod),
)
err := DecodeTEK(tek)
if err != nil {
return nil, err
}
teks = append(teks, tek)
}
return teks, nil
}
// DecodeTEK decodes a TemporaryExposureKey calculating its Rolling Proximity Identifiers
func DecodeTEK(tek *TemporaryExposureKey) error {
hkdfReader := hkdf.New(sha256.New, tek.ID, nil, []byte("EN-RPIK"))
rpiKey := make([]byte, 16)
if _, err := io.ReadFull(hkdfReader, rpiKey); err != nil {
return err
}
rpis, err := NewRollingProximityIdentifiers(rpiKey, tek.rollingStartInterval, tek.rollingPeriod)
if err != nil {
return err
}
tek.RPIs = rpis
return nil
}
// NewRollingProximityIdentifiers returns the Rolling Proximity Identifiers from a key, from the specified starting interval and interval
func NewRollingProximityIdentifiers(rpiKey []byte, rollingStartInterval, rollingPeriod int) ([]*RollingProximityIdentifier, error) {
rpis := make([]*RollingProximityIdentifier, 0)
for rp := 0; rp < rollingPeriod; rp++ {
interval := rp + rollingStartInterval
newRpi, err := NewRollingProximityIdentifier(rpiKey, interval)
if err != nil {
return nil, err
}
rpis = append(rpis, newRpi)
}
return rpis, nil
}
// NewRollingProximityIdentifier creates a Rolling Proximity Identifier for the specified interval
func NewRollingProximityIdentifier(rpiKey []byte, interval int) (*RollingProximityIdentifier, error) {
cipher, err := aes.NewCipher(rpiKey)
if err != nil {
return nil, err
}
rpi := &RollingProximityIdentifier{
ID: make([]byte, 16),
Interval: time.Unix(int64(interval*600), 0),
}
cipher.Encrypt(rpi.ID, padInterval(interval))
return rpi, nil
}
// JSONTime is an alias used to format the time.Time
type JSONTime time.Time
// MarshalJSON is used to override the default marshalJSON
func (t JSONTime) MarshalJSON() ([]byte, error) {
jsonTime := fmt.Sprintf(`"%s"`, time.Time(t).Format("2006-01-02"))
return []byte(jsonTime), nil
}
// ID is an alias for an ID made of []byte
type ID []byte
// MarshalJSON is used to override the default marshalJSON
func (id ID) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, id.ToBase64())), nil
}
// ToBase64 returns the string representation of a []byte
func (id ID) ToBase64() string {
return base64.StdEncoding.EncodeToString(id)
}
// ToHEX returns the hex array representation of a []byte
func (id ID) ToHEX() []string {
hexArr := make([]string, 0)
for _, i := range id {
hex := fmt.Sprintf("%02s", strconv.FormatUint(uint64(i), 16))
hexArr = append(hexArr, strings.ToUpper(hex))
}
return hexArr
}
// ToInt returns the int array representation of a []byte
func (id ID) ToInt() []int {
intArr := make([]int, 0)
for _, i := range id {
intArr = append(intArr, int(i))
}
return intArr
}
// TemporaryExposureKey is the daily tracing key
type TemporaryExposureKey struct {
ID ID `json:"ID"`
Date JSONTime
rollingStartInterval int
rollingPeriod int
RPIs []*RollingProximityIdentifier `json:",omitempty"`
}
// NewTemporaryExposureKey returns a Temporary Exposure Key
func NewTemporaryExposureKey(id []byte, rollingStartInterval, rollingPeriod int) *TemporaryExposureKey {
return &TemporaryExposureKey{
ID: id,
Date: JSONTime(time.Unix(int64(rollingStartInterval*600), 0)),
rollingStartInterval: rollingStartInterval,
rollingPeriod: rollingPeriod,
RPIs: make([]*RollingProximityIdentifier, 0),
}
}
// RollingProximityIdentifier is the bluetooth pseudorandom identifier
type RollingProximityIdentifier struct {
ID ID `json:"ID"`
Interval time.Time `json:"Interval"`
}
// padInterval is used to creates the padding array for the specified interval
func padInterval(interval int) []byte {
// EN-RPI000000
pad := []byte("EN-RPI")
for i := 0; i < 6; i++ {
pad = append(pad, 0)
}
pad = append(pad, byte(interval&0xFF))
pad = append(pad, byte(interval>>8&0xFF))
pad = append(pad, byte(interval>>16&0xFF))
pad = append(pad, byte(interval>>24&0xFF))
return pad
}