]> cvs.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifaredesfire.c
First check in.
[proxmark3-svn] / armsrc / mifaredesfire.c
CommitLineData
f38a1528 1#include "mifaredesfire.h"
2
3#define MAX_APPLICATION_COUNT 28
4#define MAX_FILE_COUNT 16
5#define MAX_FRAME_SIZE 60
6#define NOT_YET_AUTHENTICATED 255
7#define FRAME_PAYLOAD_SIZE (MAX_FRAME_SIZE - 5)
8
9//static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
10/* PCB CID CMD PAYLOAD */
11//static uint8_t __res[MAX_FRAME_SIZE];
12
13void MifareDesfireGetInformation(){
14
15
16 uint8_t len = 0;
17 uint8_t resp[RECV_RES_SIZE];
18 uint8_t dataout[RECV_CMD_SIZE];
19 byte_t buf[RECV_RES_SIZE];
20
21 memset(resp,0,sizeof(resp));
22 memset(dataout,0, sizeof(dataout));
23 memset(buf,0,sizeof(buf));
24
25 /*
26 1 = PCB 1
27 2 = cid 2
28 3 = desfire command 3
29 4-5 = crc 4 key
30 5-6 crc
31
32 PCB == 0x0A because sending CID byte.
33 CID == 0x00 first card?
34
35 */
36 uint8_t cmd1[] = {0x0a,0x00,GET_VERSION, 0x00, 0x00 };
37 uint8_t cmd2[] = {0x0a,0x00,GET_KEY_VERSION, 0x00, 0x00, 0x00 };
38
39 iso14a_clear_trace();
40 iso14a_set_tracing(TRUE);
41 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
42
43 // card select - information
44 iso14a_card_select_t *card = (iso14a_card_select_t*)buf;
45 byte_t isOK = iso14443a_select_card(NULL, card, NULL);
46 if (isOK != 1) {
47 if (MF_DBGLEVEL >= 1) {
48 Dbprintf("Can't select card");
49 }
50 OnError();
51 return;
52 }
53
54
55 memcpy(dataout,card->uid,7);
56
57 LED_A_ON();
58 LED_B_OFF();
59 LED_C_OFF();
60
61 // GET INFORMATION
62 AppendCrc14443a(cmd1, 3);
63 ReaderTransmit(cmd1, sizeof(cmd1), NULL);
64 len = ReaderReceive(resp);
65 if ( resp[2] != ADDITIONAL_FRAME) {
66 print_result("ERROR <--: ", resp, len);
67 OnError();
68 return;
69 }
70
71 memcpy(dataout+7,resp+3,7);
72
73 // ADDITION_FRAME 1
74 ++cmd1[0];
75 cmd1[2] = ADDITIONAL_FRAME;
76 AppendCrc14443a(cmd1, 3);
77 ReaderTransmit(cmd1, sizeof(cmd1), NULL);
78 len = ReaderReceive(resp);
79
80 if ( resp[2] != ADDITIONAL_FRAME) {
81 print_result("ERROR <--: ", resp, len);
82 OnError();
83 return;
84 }
85 memcpy(dataout+7+7,resp+3,7);
86
87 // ADDITION_FRAME 2
88 --cmd1[0];
89 AppendCrc14443a(cmd1, 3);
90 ReaderTransmit(cmd1, sizeof(cmd1), NULL);
91 len = ReaderReceive(resp);
92 if ( resp[2] != OPERATION_OK) {
93 print_result("ERROR <--: ", resp, len);
94 OnError();
95 return;
96 }
97
98 memcpy(dataout+7+7+7,resp+3,14);
99
100 // GET MASTER KEYSETTINGS
101 cmd1[2] = GET_KEY_SETTINGS;
102 AppendCrc14443a(cmd1, 3);
103 ReaderTransmit(cmd1, sizeof(cmd1), NULL);
104 len = ReaderReceive(resp);
105 if (len){
106 memcpy(dataout+7+7+7+14,resp+3,2);
107 }
108
109
110 // GET MASTER KEY VERSION
111 AppendCrc14443a(cmd2, 4);
112 ReaderTransmit(cmd2, sizeof(cmd2), NULL);
113 len = ReaderReceive(resp);
114 if (len){
115 memcpy(dataout+7+7+7+14+2,resp+3,1);
116 }
117
118 // GET FREE MEMORY
119 cmd1[2] = GET_FREE_MEMORY;
120 AppendCrc14443a(cmd1, 3);
121 ReaderTransmit(cmd1, sizeof(cmd1), NULL);
122 len = ReaderReceive(resp);
123 if (len){
124 memcpy(dataout+7+7+7+14+2+1,resp+3,3);
125 }
126
127 cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
128 OnSuccess();
129}
130
131void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
132
133 uint8_t null_key_data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
134 uint8_t new_key_data[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
135 int res;
136
137 MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data);
138
139 res = mifare_desfire_select_application (tags[i], aid);
140 if (res < 0) {
141 freefare_perror (tags[i], "mifare_desfire_select_application");
142 error = EXIT_FAILURE;
143 break;
144 }
145
146 return;
147 // pcb cid cmd key crc1 cr2
148 //uint8_t cmd2[] = {0x02,0x00,GET_KEY_VERSION, 0x00, 0x00, 0x00 };
149
150 //uint8_t* bigbuffer = mifare_get_bigbufptr();
151 byte_t isOK = 1;
152 uint8_t resp[256];
153 uint8_t key[24];
154 uint8_t IV[16];
155
156