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