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