-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdp_message.go
199 lines (167 loc) · 5.53 KB
/
osdp_message.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
package osdp
import (
"crypto/aes"
"crypto/cipher"
"errors"
)
type OSDPMessage struct {
MessageCode OSDPCode
PeripheralAddress byte
MessageData []byte
SequenceNumber byte
Secure bool
SecureBlockType byte
SecureBlockData []byte
Retries uint32
MAC []byte
}
func NewOSDPMessage(osdpCode OSDPCode, peripheralAddress byte, sequenceNumber byte, msgData []byte) (*OSDPMessage, error) {
if sequenceNumber < 0x00 || sequenceNumber > 0x03 {
return nil, InvalidSequenceNumber
}
return &OSDPMessage{MessageCode: osdpCode, PeripheralAddress: peripheralAddress, MessageData: msgData, SequenceNumber: sequenceNumber, Secure: false}, nil
}
func NewSecureOSDPMessage(osdpCode OSDPCode, peripheralAddress byte, sequenceNumber byte, secureBlockType byte, secureBlockData []byte, msgData []byte) (*OSDPMessage, error) {
if sequenceNumber < 0x00 || sequenceNumber > 0x03 {
return nil, InvalidSequenceNumber
}
if secureBlockType < SCS_11 || secureBlockType > SCS_18 {
return nil, InvalidSecureBlockType
}
return &OSDPMessage{MessageCode: osdpCode, PeripheralAddress: peripheralAddress, MessageData: msgData, SequenceNumber: sequenceNumber, Secure: true, SecureBlockType: secureBlockType, SecureBlockData: secureBlockData}, nil
}
func (osdpMessage *OSDPMessage) PacketFromMessage() (*OSDPPacket, error) {
if osdpMessage.Secure {
osdpPacket, err := NewSecurePacket(osdpMessage.MessageCode, osdpMessage.PeripheralAddress, osdpMessage.MessageData, osdpMessage.SecureBlockType, osdpMessage.SecureBlockData, osdpMessage.SequenceNumber, true)
if err != nil {
return nil, err
}
if osdpMessage.MAC != nil {
osdpPacket.msgAuthenticationCode = make([]byte, 4)
copy(osdpPacket.msgAuthenticationCode, osdpMessage.MAC[:4])
osdpPacket.calculateCRC()
}
return osdpPacket, nil
}
osdpPacket, err := NewPacket(osdpMessage.MessageCode, osdpMessage.PeripheralAddress, osdpMessage.MessageData, osdpMessage.SequenceNumber, true)
if err != nil {
return nil, err
}
return osdpPacket, nil
}
func (osdpMessage *OSDPMessage) GenerateMAC(IVC, SMAC1, SMAC2 []byte) ([]byte, error) {
if !osdpMessage.Secure {
return nil, errors.New("Can only generate MAC for secure message")
}
osdpPacket, err := osdpMessage.PacketFromMessage()
if err != nil {
return nil, err
}
if len(IVC) != 16 {
return nil, errors.New("Invalid IVC Length")
}
if len(SMAC1) != 16 {
return nil, errors.New("Invalid SMAC 1 Length")
}
if len(SMAC2) != 16 {
return nil, errors.New("Invalid SMAC 2 Length")
}
IV := make([]byte, len(IVC))
copy(IV, IVC) // Make a copy of the IVC so as to not change to slice
MAC := make([]byte, 16)
osdpPacketBytes := osdpPacket.ToBytes()
osdpPacketBytes = osdpPacketBytes[0 : len(osdpPacketBytes)-2] // Remove the CRC
if osdpPacket.useMAC {
osdpPacketBytes = osdpPacketBytes[0 : len(osdpPacketBytes)-4]
}
packetLength := len(osdpPacketBytes)
if packetLength%16 != 0 {
// Apply Padding
paddingRequired := 16 - (packetLength % 16)
osdpPacketBytes = append(osdpPacketBytes, 0x80)
restPadding := make([]byte, paddingRequired-1)
osdpPacketBytes = append(osdpPacketBytes, restPadding...)
}
packetLength = len(osdpPacketBytes)
if packetLength > 16 {
block, err := aes.NewCipher(SMAC1)
if err != nil {
return nil, err
}
for packetLength > 16 {
mode := cipher.NewCBCEncrypter(block, IV)
mode.CryptBlocks(MAC, osdpPacketBytes[0:16])
osdpPacketBytes = osdpPacketBytes[16:]
packetLength = len(osdpPacketBytes)
if copy(IV, MAC) != 16 {
return nil, errors.New("Unable to copy MAC into IV")
}
}
}
block, err := aes.NewCipher(SMAC2)
if err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, IV)
mode.CryptBlocks(MAC, osdpPacketBytes)
osdpMessage.MAC = MAC
return MAC, nil
}
func (osdpMessage *OSDPMessage) DecryptPayload(key []byte, IVC []byte) error {
if !osdpMessage.Secure {
return errors.New("Can only decrypt secure message")
}
if len(IVC) != 16 {
return errors.New("Invalid IVC Length")
}
if len(key) != 16 {
return errors.New("Invalid key Length")
}
if len(osdpMessage.MessageData)%16 != 0 {
return errors.New("Unable to Decrypt Unpadded payload")
}
block, err := aes.NewCipher(key)
if err != nil {
return err
}
decryptedData := make([]byte, len(osdpMessage.MessageData))
mode := cipher.NewCBCDecrypter(block, IVC)
mode.CryptBlocks(decryptedData, osdpMessage.MessageData)
// Find padding start
endIndex := len(decryptedData) - 1
for i := endIndex; i >= 0; i-- {
if decryptedData[i] == 0x80 {
decryptedData = decryptedData[:len(decryptedData)-1]
break
}
decryptedData = decryptedData[:len(decryptedData)-1]
}
osdpMessage.MessageData = decryptedData
return nil
}
func (osdpMessage *OSDPMessage) EncryptPayload(key []byte, IVC []byte) error {
if !osdpMessage.Secure {
return errors.New("Can only decrypt secure message")
}
if len(IVC) != 16 {
return errors.New("Invalid IVC Length")
}
if len(key) != 16 {
return errors.New("Invalid key Length")
}
dataLength := len(osdpMessage.MessageData)
// Apply Padding
paddingRequired := 16 - (dataLength % 16)
osdpMessage.MessageData = append(osdpMessage.MessageData, 0x80)
restPadding := make([]byte, paddingRequired-1)
osdpMessage.MessageData = append(osdpMessage.MessageData, restPadding...)
block, err := aes.NewCipher(key)
if err != nil {
return err
}
encryptedData := make([]byte, len(osdpMessage.MessageData))
mode := cipher.NewCBCEncrypter(block, IVC)
mode.CryptBlocks(encryptedData, osdpMessage.MessageData)
osdpMessage.MessageData = encryptedData
return nil
}