This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
keys.go
375 lines (311 loc) · 8.52 KB
/
keys.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package types
import (
"encoding/hex"
"strings"
"github.com/TheThingsNetwork/ttn/utils/errors"
)
// AES128Key is an 128 bit AES key.
type AES128Key [16]byte
// AppKey (Application Key) is used for LoRaWAN OTAA.
type AppKey AES128Key
// NwkSKey (Network Session Key) is used for LoRaWAN MIC calculation.
type NwkSKey AES128Key
// AppSKey (Application Session Key) is used for LoRaWAN payload encryption.
type AppSKey AES128Key
// ParseAES128Key parses a 128-bit hex-encoded string to an AES128Key
func ParseAES128Key(input string) (key AES128Key, err error) {
bytes, err := ParseHEX(input, 16)
if err != nil {
return
}
copy(key[:], bytes)
return
}
// Bytes returns the AES128Key as a byte slice
func (key AES128Key) Bytes() []byte {
return key[:]
}
// String implements the Stringer interface.
func (key AES128Key) String() string {
if key.IsEmpty() {
return ""
}
return strings.ToUpper(hex.EncodeToString(key.Bytes()))
}
// GoString implements the GoStringer interface.
func (key AES128Key) GoString() string {
return key.String()
}
// MarshalText implements the TextMarshaler interface.
func (key AES128Key) MarshalText() ([]byte, error) {
return []byte(key.String()), nil
}
// UnmarshalText implements the TextUnmarshaler interface.
func (key *AES128Key) UnmarshalText(data []byte) error {
parsed, err := ParseAES128Key(string(data))
if err != nil {
return err
}
*key = AES128Key(parsed)
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (key AES128Key) MarshalBinary() ([]byte, error) {
return key.Bytes(), nil
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (key *AES128Key) UnmarshalBinary(data []byte) error {
if len(data) != 16 {
return errors.New("ttn/core: Invalid length for AES128Key")
}
copy(key[:], data)
return nil
}
// MarshalTo is used by Protobuf
func (key *AES128Key) MarshalTo(b []byte) (int, error) {
copy(b, key.Bytes())
return 16, nil
}
// Size is used by Protobuf
func (key *AES128Key) Size() int {
return 16
}
// Marshal implements the Marshaler interface.
func (key AES128Key) Marshal() ([]byte, error) {
return key.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (key *AES128Key) Unmarshal(data []byte) error {
*key = [16]byte{} // Reset the receiver
return key.UnmarshalBinary(data)
}
// Equal returns whether key is equal to other
func (key AES128Key) Equal(other AES128Key) bool {
return key == other
}
// ParseAppKey parses a 64-bit hex-encoded string to an AppKey
func ParseAppKey(input string) (key AppKey, err error) {
aes128key, err := ParseAES128Key(input)
if err != nil {
return
}
key = AppKey(aes128key)
return
}
// Bytes returns the AppKey as a byte slice
func (key AppKey) Bytes() []byte {
return AES128Key(key).Bytes()
}
func (key AppKey) String() string {
return AES128Key(key).String()
}
// GoString implements the GoStringer interface.
func (key AppKey) GoString() string {
return key.String()
}
// MarshalText implements the TextMarshaler interface.
func (key AppKey) MarshalText() ([]byte, error) {
return AES128Key(key).MarshalText()
}
// UnmarshalText implements the TextUnmarshaler interface.
func (key *AppKey) UnmarshalText(data []byte) error {
e := AES128Key(*key)
err := e.UnmarshalText(data)
if err != nil {
return err
}
*key = AppKey(e)
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (key AppKey) MarshalBinary() ([]byte, error) {
return AES128Key(key).MarshalBinary()
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (key *AppKey) UnmarshalBinary(data []byte) error {
e := AES128Key(*key)
err := e.UnmarshalBinary(data)
if err != nil {
return err
}
*key = AppKey(e)
return nil
}
// MarshalTo is used by Protobuf
func (key *AppKey) MarshalTo(b []byte) (int, error) {
copy(b, key.Bytes())
return 16, nil
}
// Size is used by Protobuf
func (key *AppKey) Size() int {
return 16
}
// Marshal implements the Marshaler interface.
func (key AppKey) Marshal() ([]byte, error) {
return key.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (key *AppKey) Unmarshal(data []byte) error {
*key = [16]byte{} // Reset the receiver
return key.UnmarshalBinary(data)
}
// Equal returns whether key is equal to other
func (key AppKey) Equal(other AppKey) bool {
return key == other
}
// ParseAppSKey parses a 64-bit hex-encoded string to an AppSKey
func ParseAppSKey(input string) (key AppSKey, err error) {
aes128key, err := ParseAES128Key(input)
if err != nil {
return
}
key = AppSKey(aes128key)
return
}
// Bytes returns the AppSKey as a byte slice
func (key AppSKey) Bytes() []byte {
return AES128Key(key).Bytes()
}
func (key AppSKey) String() string {
return AES128Key(key).String()
}
// GoString implements the GoStringer interface.
func (key AppSKey) GoString() string {
return key.String()
}
// MarshalText implements the TextMarshaler interface.
func (key AppSKey) MarshalText() ([]byte, error) {
return AES128Key(key).MarshalText()
}
// UnmarshalText implements the TextUnmarshaler interface.
func (key *AppSKey) UnmarshalText(data []byte) error {
e := AES128Key(*key)
err := e.UnmarshalText(data)
if err != nil {
return err
}
*key = AppSKey(e)
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (key AppSKey) MarshalBinary() ([]byte, error) {
return AES128Key(key).MarshalBinary()
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (key *AppSKey) UnmarshalBinary(data []byte) error {
e := AES128Key(*key)
err := e.UnmarshalBinary(data)
if err != nil {
return err
}
*key = AppSKey(e)
return nil
}
// MarshalTo is used by Protobuf
func (key *AppSKey) MarshalTo(b []byte) (int, error) {
copy(b, key.Bytes())
return 16, nil
}
// Size is used by Protobuf
func (key *AppSKey) Size() int {
return 16
}
// Marshal implements the Marshaler interface.
func (key AppSKey) Marshal() ([]byte, error) {
return key.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (key *AppSKey) Unmarshal(data []byte) error {
*key = [16]byte{} // Reset the receiver
return key.UnmarshalBinary(data)
}
// Equal returns whether key is equal to other
func (key AppSKey) Equal(other AppSKey) bool {
return key == other
}
// ParseNwkSKey parses a 64-bit hex-encoded string to an NwkSKey
func ParseNwkSKey(input string) (key NwkSKey, err error) {
aes128key, err := ParseAES128Key(input)
if err != nil {
return
}
key = NwkSKey(aes128key)
return
}
// Bytes returns the NwkSKey as a byte slice
func (key NwkSKey) Bytes() []byte {
return AES128Key(key).Bytes()
}
// String implements the Stringer interface.
func (key NwkSKey) String() string {
return AES128Key(key).String()
}
// GoString implements the GoStringer interface.
func (key NwkSKey) GoString() string {
return key.String()
}
// MarshalText implements the TextMarshaler interface.
func (key NwkSKey) MarshalText() ([]byte, error) {
return AES128Key(key).MarshalText()
}
// UnmarshalText implements the TextUnmarshaler interface.
func (key *NwkSKey) UnmarshalText(data []byte) error {
e := AES128Key(*key)
err := e.UnmarshalText(data)
if err != nil {
return err
}
*key = NwkSKey(e)
return nil
}
// MarshalBinary implements the BinaryMarshaler interface.
func (key NwkSKey) MarshalBinary() ([]byte, error) {
return AES128Key(key).MarshalBinary()
}
// UnmarshalBinary implements the BinaryUnmarshaler interface.
func (key *NwkSKey) UnmarshalBinary(data []byte) error {
e := AES128Key(*key)
err := e.UnmarshalBinary(data)
if err != nil {
return err
}
*key = NwkSKey(e)
return nil
}
// MarshalTo is used by Protobuf
func (key *NwkSKey) MarshalTo(b []byte) (int, error) {
copy(b, key.Bytes())
return 16, nil
}
// Size is used by Protobuf
func (key *NwkSKey) Size() int {
return 16
}
// Marshal implements the Marshaler interface.
func (key NwkSKey) Marshal() ([]byte, error) {
return key.MarshalBinary()
}
// Unmarshal implements the Unmarshaler interface.
func (key *NwkSKey) Unmarshal(data []byte) error {
*key = [16]byte{} // Reset the receiver
return key.UnmarshalBinary(data)
}
// Equal returns whether key is equal to other
func (key NwkSKey) Equal(other NwkSKey) bool {
return key == other
}
var emptyAES AES128Key
func (key AES128Key) IsEmpty() bool {
return key == emptyAES
}
func (key AppKey) IsEmpty() bool {
return AES128Key(key).IsEmpty()
}
func (key AppSKey) IsEmpty() bool {
return AES128Key(key).IsEmpty()
}
func (key NwkSKey) IsEmpty() bool {
return AES128Key(key).IsEmpty()
}