]>
Commit | Line | Data |
---|---|---|
1 | #include "mifaredesfire.h" | |
2 | #include "des.h" | |
3 | #include "BigBuf.h" | |
4 | ||
5 | #define MAX_APPLICATION_COUNT 28 | |
6 | #define MAX_FILE_COUNT 16 | |
7 | #define MAX_DESFIRE_FRAME_SIZE 60 | |
8 | #define NOT_YET_AUTHENTICATED 255 | |
9 | #define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5) | |
10 | #define RECEIVE_SIZE 64 | |
11 | ||
12 | // the block number for the ISO14443-4 PCB | |
13 | uint8_t pcb_blocknum = 0; | |
14 | // Deselect card by sending a s-block. the crc is precalced for speed | |
15 | static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4}; | |
16 | ||
17 | //static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 }; | |
18 | /* PCB CID CMD PAYLOAD */ | |
19 | //static uint8_t __res[MAX_FRAME_SIZE]; | |
20 | ||
21 | bool InitDesfireCard(){ | |
22 | ||
23 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); | |
24 | set_tracing(TRUE); | |
25 | ||
26 | byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00}; | |
27 | iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf; | |
28 | ||
29 | int len = iso14443a_select_card(NULL,card,NULL); | |
30 | ||
31 | if (!len) { | |
32 | if (MF_DBGLEVEL >= MF_DBG_ERROR) | |
33 | Dbprintf("Can't select card"); | |
34 | OnError(1); | |
35 | return false; | |
36 | } | |
37 | return true; | |
38 | } | |
39 | ||
40 | // ARG0 flag enums | |
41 | enum { | |
42 | NONE = 0x00, | |
43 | INIT = 0x01, | |
44 | DISCONNECT = 0x02, | |
45 | CLEARTRACE = 0x04, | |
46 | BAR = 0x08, | |
47 | } CmdOptions ; | |
48 | ||
49 | void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){ | |
50 | ||
51 | /* ARG0 contains flags. | |
52 | 0x01 = init card. | |
53 | 0x02 = Disconnect | |
54 | 0x03 | |
55 | */ | |
56 | uint8_t flags = arg0; | |
57 | size_t datalen = arg1; | |
58 | uint8_t resp[RECEIVE_SIZE]; | |
59 | memset(resp,0,sizeof(resp)); | |
60 | ||
61 | if (MF_DBGLEVEL >= 4) { | |
62 | Dbprintf(" flags : %02X", flags); | |
63 | Dbprintf(" len : %02X", datalen); | |
64 | print_result(" RX : ", datain, datalen); | |
65 | } | |
66 | ||
67 | if ( flags & CLEARTRACE ){ | |
68 | clear_trace(); | |
69 | } | |
70 | ||
71 | if ( flags & INIT ){ | |
72 | if ( !InitDesfireCard() ) | |
73 | return; | |
74 | } | |
75 | ||
76 | int len = DesfireAPDU(datain, datalen, resp); | |
77 | if (MF_DBGLEVEL >= 4) { | |
78 | print_result("ERR <--: ", resp, len); | |
79 | } | |
80 | ||
81 | if ( !len ) { | |
82 | OnError(2); | |
83 | return; | |
84 | } | |
85 | ||
86 | // reset the pcb_blocknum, | |
87 | pcb_blocknum = 0; | |
88 | ||
89 | if ( flags & DISCONNECT ){ | |
90 | OnSuccess(); | |
91 | } | |
92 | ||
93 | cmd_send(CMD_ACK,1,len,0,resp,len); | |
94 | } | |
95 | ||
96 | void MifareDesfireGetInformation(){ | |
97 | ||
98 | int len = 0; | |
99 | uint8_t resp[USB_CMD_DATA_SIZE] = {0x00}; | |
100 | uint8_t dataout[USB_CMD_DATA_SIZE] = {0x00}; | |
101 | byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00}; | |
102 | ||
103 | /* | |
104 | 1 = PCB 1 | |
105 | 2 = cid 2 | |
106 | 3 = desfire command 3 | |
107 | 4-5 = crc 4 key | |
108 | 5-6 crc | |
109 | PCB == 0x0A because sending CID byte. | |
110 | CID == 0x00 first card? | |
111 | */ | |
112 | clear_trace(); | |
113 | set_tracing(TRUE); | |
114 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); | |
115 | ||
116 | // card select - information | |
117 | iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf; | |
118 | byte_t isOK = iso14443a_select_card(NULL, card, NULL); | |
119 | if ( isOK == 0) { | |
120 | if (MF_DBGLEVEL >= MF_DBG_ERROR) { | |
121 | Dbprintf("Can't select card"); | |
122 | } | |
123 | OnError(1); | |
124 | return; | |
125 | } | |
126 | ||
127 | memcpy(dataout,card->uid,7); | |
128 | ||
129 | LED_A_ON(); | |
130 | LED_B_OFF(); | |
131 | LED_C_OFF(); | |
132 | ||
133 | uint8_t cmd[] = {GET_VERSION}; | |
134 | size_t cmd_len = sizeof(cmd); | |
135 | ||
136 | len = DesfireAPDU(cmd, cmd_len, resp); | |
137 | if ( !len ) { | |
138 | print_result("ERROR <--: ", resp, len); | |
139 | OnError(2); | |
140 | return; | |
141 | } | |
142 | ||
143 | LED_A_OFF(); | |
144 | LED_B_ON(); | |
145 | memcpy(dataout+7,resp+3,7); | |
146 | ||
147 | // ADDITION_FRAME 1 | |
148 | cmd[0] = ADDITIONAL_FRAME; | |
149 | len = DesfireAPDU(cmd, cmd_len, resp); | |
150 | if ( !len ) { | |
151 | print_result("ERROR <--: ", resp, len); | |
152 | OnError(2); | |
153 | return; | |
154 | } | |
155 | ||
156 | LED_B_OFF(); | |
157 | LED_C_ON(); | |
158 | memcpy(dataout+7+7,resp+3,7); | |
159 | ||
160 | // ADDITION_FRAME 2 | |
161 | len = DesfireAPDU(cmd, cmd_len, resp); | |
162 | if ( !len ) { | |
163 | print_result("ERROR <--: ", resp, len); | |
164 | OnError(2); | |
165 | return; | |
166 | } | |
167 | ||
168 | memcpy(dataout+7+7+7,resp+3,14); | |
169 | ||
170 | cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout)); | |
171 | ||
172 | // reset the pcb_blocknum, | |
173 | pcb_blocknum = 0; | |
174 | OnSuccess(); | |
175 | } | |
176 | ||
177 | void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){ | |
178 | ||
179 | int len = 0; | |
180 | //uint8_t PICC_MASTER_KEY8[8] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47}; | |
181 | uint8_t PICC_MASTER_KEY16[16] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f }; | |
182 | uint8_t null_key_data8[8] = {0x00}; | |
183 | //uint8_t null_key_data16[16] = {0x00}; | |
184 | //uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77}; | |
185 | //uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF}; | |
186 | ||
187 | uint8_t resp[256] = {0x00}; | |
188 | uint8_t IV[16] = {0x00}; | |
189 | ||
190 | size_t datalen = datain[0]; | |
191 | ||
192 | uint8_t cmd[40] = {0x00}; | |
193 | uint8_t encRndB[16] = {0x00}; | |
194 | uint8_t decRndB[16] = {0x00}; | |
195 | uint8_t nonce[16] = {0x00}; | |
196 | uint8_t both[32] = {0x00}; | |
197 | uint8_t encBoth[32] = {0x00}; | |
198 | ||
199 | InitDesfireCard(); | |
200 | ||
201 |