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