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