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