Repository navigation
Expand file tree
/
Copy pathfabric.go
More file actions
55 lines (47 loc) · 1.5 KB
/
Copy pathfabric.go
File metadata and controls
55 lines (47 loc) · 1.5 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
package gomat
import (
"bytes"
"crypto/elliptic"
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
)
// Fabric structure represents matter Fabric.
// Its main parameters are Id of fabric and certificate manager.
type Fabric struct {
id uint64
CertificateManager CertificateManager
ipk []byte
}
func (fabric Fabric) Id() uint64 {
return fabric.id
}
// CompressedFabric returns Compressed Fabric Identifier which is used to identify fabric
// in matter protocol.
func (fabric Fabric) CompressedFabric() []byte {
capub := fabric.CertificateManager.GetCaPublicKey()
capublic_key := elliptic.Marshal(elliptic.P256(), capub.X, capub.Y)
var fabric_big_endian bytes.Buffer
binary.Write(&fabric_big_endian, binary.BigEndian, fabric.id)
key := hkdf_sha256(capublic_key[1:], fabric_big_endian.Bytes(), []byte("CompressedFabric"), 8)
return key
}
func (fabric Fabric) make_ipk() []byte {
key := hkdf_sha256(fabric.ipk, fabric.CompressedFabric(), []byte("GroupKey v1.0"), 16)
return key
}
func (fabric Fabric) GetOperationalDeviceId(in uint64) string {
compressedFabric := hex.EncodeToString(fabric.CompressedFabric())
ids := fmt.Sprintf("%s-%016X", compressedFabric, in)
return strings.ToUpper(ids)
}
// NewFabric constructs new Fabric object.
func NewFabric(id uint64, certman CertificateManager) *Fabric {
out := &Fabric{
id: id,
CertificateManager: certman,
ipk: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
}
return out
}