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