/
key_wrap.go
48 lines (38 loc) · 1.03 KB
/
key_wrap.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
package join
import (
"crypto/aes"
"encoding/hex"
"fmt"
keywrap "github.com/NickBall/go-aes-key-wrap"
"github.com/pkg/errors"
"github.com/brocaar/loraserver/internal/config"
"github.com/brocaar/lorawan"
"github.com/brocaar/lorawan/backend"
)
// unwrapNSKeyEnveope returns the decrypted key from the given KeyEnvelope.
func unwrapNSKeyEnvelope(ke *backend.KeyEnvelope) (lorawan.AES128Key, error) {
var key lorawan.AES128Key
if ke.KEKLabel == "" {
copy(key[:], ke.AESKey[:])
return key, nil
}
for _, k := range config.C.JoinServer.KEK.Set {
if k.Label == ke.KEKLabel {
kek, err := hex.DecodeString(k.KEK)
if err != nil {
return key, errors.Wrap(err, "decode kek error")
}
block, err := aes.NewCipher(kek)
if err != nil {
return key, errors.Wrap(err, "new cipher error")
}
b, err := keywrap.Unwrap(block, ke.AESKey[:])
if err != nil {
return key, errors.Wrap(err, "unwrap key error")
}
copy(key[:], b)
return key, nil
}
}
return key, fmt.Errorf("unknown kek label: %s", ke.KEKLabel)
}