-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_commands.py
154 lines (131 loc) · 3.46 KB
/
data_commands.py
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
def command_read_data(fileno: list, offset: list, length: list) -> list:
"""
BD: Read Data
Read data from a file.
The response will have MAC or be encrypted depending on file comms mode.
NOTE: This function expects the byte order for both `length` and `offset` to be reversed.
"""
apdu = [0x90, 0xBD, 0x00, 0x00]
lc = [0x07]
apdu += lc
apdu += fileno
apdu += offset
apdu += length
le = [0x00]
apdu += le
return apdu
def command_write_data(fileno: list, offset: list, length: list, data: list) -> list:
"""
3D: Write Data
Write data to a file.
This needs to be sent using plain, CMAC or encrypted (with CRC) depending on file comms
mode.
NOTE: Details in RevK's manual, page 14.
NOTE: currently this command only supports writing plain data.
NOTE: This function expects the byte order for both `length` and `offset` to be reversed.
"""
apdu = [0x90, 0x3D, 0x00, 0x00]
lc = [len(fileno) + len(offset) + len(length) + len(data)]
apdu += lc
apdu += fileno
apdu += offset
apdu += length
apdu += data
le = [0x00]
apdu += le
return apdu
def command_get_value(fileno: list) -> list:
"""
6C: Get Value
Get value from a value file.
"""
apdu = [0x90, 0x6C, 0x00, 0x00]
lc = [0x01]
apdu += lc
apdu += fileno
le = [0x00]
apdu += le
return apdu
def command_credit(fileno: list, amount: list) -> list:
"""
OC: Credit
Credit value in a value file. You need to commit the changes.
amount is 4 bytes
"""
apdu = [0x90, 0x0C, 0x00, 0x00]
lc = [0x05]
apdu += lc
apdu += fileno
apdu += amount
le = [0x00]
apdu += le
return apdu
def command_debit(fileno: list, amount: list) -> list:
"""
DC: Debit
Debit the value in a file. You need to commit the changes.
amount is 4 bytes
"""
apdu = [0x90, 0xDC, 0x00, 0x00]
lc = [0x05]
apdu += lc
apdu += fileno
apdu += amount
le = [0x00]
apdu += le
return apdu
def command_limited_credit() -> list:
pass
def command_write_record(fileno: list, offset: list, length: list, data: list) -> list:
"""
3B: Write Record
Write to a record. The offset/length are within the record.
A new record is created with 00 bytes where not written.
You need to commit the changes.
NOTE: This version of the command does not support data encryption
"""
apdu = [0x90, 0x3B, 0x00, 0x00]
lc = [len(fileno) + len(offset) + len(length) + len(data)]
apdu += lc
apdu += fileno
apdu += offset
apdu += length
apdu += data
le = [0x00]
apdu += le
return apdu
def command_read_record(
fileno: list, record_number: list, number_of_records: list
) -> list:
"""
BB: Read Records
Read records - record 0 is the oldest/first.
"""
apdu = [0x90, 0xBB, 0x00, 0x00]
lc = [0x07]
apdu += lc
apdu += fileno
apdu += record_number
apdu += number_of_records
le = [0x00]
apdu += le
return apdu
def command_clear_record_file(fileno: list) -> list:
apdu = [0x90, 0xEB, 0x00, 0x00]
lc = [0x01]
apdu += lc
apdu += fileno
le = [0x00]
apdu += le
return apdu
def command_commit_transaction() -> list:
"""
C7: Commit Transaction
Commit updates to value and record files.
"""
apdu = [0x90, 0xC7, 0x00, 0x00]
le = [0x00]
apdu += le
return apdu
def command_abort_transaction() -> list:
pass