This repository has been archived by the owner on Apr 28, 2024. It is now read-only.
forked from cosmos/ledger-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apduWrapper.go
160 lines (127 loc) · 3.88 KB
/
apduWrapper.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
/*******************************************************************************
* (c) 2018 ZondaX GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
package ledger_go
import (
"encoding/binary"
"github.com/pkg/errors"
)
var codec = binary.BigEndian
func SerializePacket(
channel uint16,
command []byte,
packetSize int,
sequenceIdx uint16) (result []byte, offset int, err error) {
if packetSize < 3 {
return nil, 0, errors.New("Packet size must be at least 3")
}
var headerOffset uint8
result = make([]byte, packetSize)
var buffer = result
// Insert channel (2 bytes)
codec.PutUint16(buffer, channel)
headerOffset += 2
// Insert tag (1 byte)
buffer[headerOffset] = 0x05
headerOffset += 1
var commandLength uint16
commandLength = uint16(len(command))
// Insert sequenceIdx (2 bytes)
codec.PutUint16(buffer[headerOffset:], sequenceIdx)
headerOffset += 2
// Only insert total size of the command in the first package
if sequenceIdx == 0 {
// Insert sequenceIdx (2 bytes)
codec.PutUint16(buffer[headerOffset:], commandLength)
headerOffset += 2
}
buffer = buffer[headerOffset:]
offset = copy(buffer, command)
return result, offset, nil
}
func DeserializePacket(
channel uint16,
buffer []byte,
sequenceIdx uint16) (result []byte, totalResponseLength uint16, err error) {
if (sequenceIdx == 0 && len(buffer) < 7) || (sequenceIdx > 0 && len(buffer) < 5) {
return nil, 0, errors.New("Cannot deserialize the packet. Header information is missing.")
}
var headerOffset uint8
if codec.Uint16(buffer) != channel {
return nil, 0, errors.New("Invalid channel")
}
headerOffset += 2
if buffer[headerOffset] != 0x05 {
return nil, 0, errors.New("Invalid tag")
}
headerOffset++
if codec.Uint16(buffer[headerOffset:]) != sequenceIdx {
return nil, 0, errors.New("Wrong sequenceIdx")
}
headerOffset += 2
if sequenceIdx == 0 {
totalResponseLength = codec.Uint16(buffer[headerOffset:])
headerOffset += 2
}
result = make([]byte, len(buffer)-int(headerOffset))
copy(result, buffer[headerOffset:])
return result, totalResponseLength, nil
}
// WrapCommandAPDU turns the command into a sequence of 64 byte packets
func WrapCommandAPDU(
channel uint16,
command []byte,
packetSize int) (result []byte, err error) {
var offset int
var totalResult []byte
var sequenceIdx uint16
for len(command) > 0 {
result, offset, err = SerializePacket(channel, command, packetSize, sequenceIdx)
if err != nil {
return nil, err
}
command = command[offset:]
totalResult = append(totalResult, result...)
sequenceIdx++
}
return totalResult, nil
}
// UnwrapResponseAPDU parses a response of 64 byte packets into the real data
func UnwrapResponseAPDU(channel uint16, pipe <-chan []byte, packetSize int) ([]byte, error) {
var sequenceIdx uint16
var totalResult []byte
var totalSize uint16
var done = false
for !done {
// Read next packet from the channel
buffer := <-pipe
result, responseSize, err := DeserializePacket(channel, buffer, sequenceIdx)
if err != nil {
return nil, err
}
if sequenceIdx == 0 {
totalSize = responseSize
}
buffer = buffer[packetSize:]
totalResult = append(totalResult, result...)
sequenceIdx++
if len(totalResult) >= int(totalSize) {
done = true
}
}
// Remove trailing zeros
totalResult = totalResult[:totalSize]
return totalResult, nil
}