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