Repository navigation
Expand file tree
/
Copy pathpyLibSC.py
More file actions
155 lines (140 loc) · 5.97 KB
/
Copy pathpyLibSC.py
File metadata and controls
155 lines (140 loc) · 5.97 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
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
'''
Created on 2017/3/19
@author: [email protected]
@organization: https://www.javacardos.com/
@copyright: JavaCardOS Technologies. All rights reserved.
'''
from pyResMan.MifareTLV import MifareCommandTLV, MifareResponseTLV,\
COMMAND_TAG_DECREMENT, COMMAND_TAG_DESFIRE_COMMAND
from pyResMan.MifareTLV import COMMAND_TAG_AUTHENTICATION, COMMAND_TAG_READ_BLOCK, COMMAND_TAG_WRITE_BLOCK, COMMAND_TAG_INCREMENT\
, COMMAND_TAG_DECREMENT, COMMAND_TAG_RESTORE, COMMAND_TAG_TRANSFER, COMMAND_TAG_SETUP
class LibSC(object):
'''
Library for smartcard functions;
'''
def __init__(self, interface):
'''
@brief: Mifare read block data.
@param block_number: block number.
@return: error_code, block_data
'''
self.__interface = interface
def M1_authentication(self, block_number, key_type, key, uid):
'''
@brief: Mifare authentication.
@param block_number: block number.
@param key_type: type of authentication key, 0 for KeyA, 1 for keyB.
@param key: key value.
@param uid: card uid.
@return: error_code, 0 if succeeded.
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_AUTHENTICATION)
command_tlv.set_block_number(block_number)
command_tlv.set_key_type(key_type)
command_tlv.set_key_value(key)
command_tlv.set_uid(uid)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def M1_read_block(self, block_number):
'''
@brief: Mifare read block data.
@param block_number: block number.
@return: error_code, 0 if succeeded; block_data
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_READ_BLOCK)
command_tlv.set_block_number(block_number)
command_tlv.set_rw_len(0x10)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error(), response_tlv.get_block_data()
def M1_write_block(self, block_number, block_data):
'''
@brief: Mifare write block data.
@param block_number: block number.
@param block_data: block data to write.
@return error_code, 0 if succeeded.
'''
print(block_number)
print(len(block_data))
for i in range(len(block_data)):
print('%02X' %(ord(block_data[i]))),
command_tlv = MifareCommandTLV(COMMAND_TAG_WRITE_BLOCK)
command_tlv.set_block_number(block_number)
command_tlv.set_block_data(block_data)
command_tlv.set_rw_len(len(block_data))
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def M1_increment(self, block_number, inc_operand):
'''
@brief: Mifare increment block data.
@param block_number: block number.
@param inc_operand: increment operand.
@return error_code, 0 if succeeded.
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_INCREMENT)
command_tlv.set_block_number(block_number)
command_tlv.set_incdec_operand(inc_operand)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def M1_decrement(self, block_number, dec_operand):
'''
@brief: Mifare decrement block data.
@param block_number: block number.
@param inc_operand: decrement operand.
@return error_code, 0 if succeeded.
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_DECREMENT)
command_tlv.set_block_number(block_number)
command_tlv.set_incdec_operand(dec_operand)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def M1_restore(self, block_number):
'''
@brief: Mifare restore block data to buffer.
@param block_number: block number.
@return error_code, 0 if succeeded.
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_RESTORE)
command_tlv.set_block_number(block_number)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def M1_transfer(self, block_number):
'''
@brief: Mifare transfer buffer data to block.
@param block_number: block number.
@return error_code, 0 if succeeded.
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_TRANSFER)
command_tlv.set_block_number(block_number)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def M1_setup(self):
'''
@brief: Mifare setup to clone card.
@return error_code, 0 if succeeded.
'''
command_tlv = MifareCommandTLV(COMMAND_TAG_SETUP)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error()
def DESFire_send_command(self, cmd):
command_tlv = MifareCommandTLV(COMMAND_TAG_DESFIRE_COMMAND)
command_tlv.set_command(cmd)
command = command_tlv.serialize()
response = self.__interface.transmit_sc_command(command)
response_tlv = MifareResponseTLV(response)
return response_tlv.get_error(), response_tlv.get_desfire_data()