]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifaredesfire.c
ADD: @pwpivi 's latest fixes for bigbuff
[proxmark3-svn] / armsrc / mifaredesfire.c
1 #include "mifaredesfire.h"
2 #include "des.h"
3 #include "BigBuf.h"
4
5 #define MAX_APPLICATION_COUNT 28
6 #define MAX_FILE_COUNT 16
7 #define MAX_DESFIRE_FRAME_SIZE 60
8 #define NOT_YET_AUTHENTICATED 255
9 #define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5)
10 #define RECEIVE_SIZE 64
11
12 // the block number for the ISO14443-4 PCB
13 uint8_t pcb_blocknum = 0;
14 // Deselect card by sending a s-block. the crc is precalced for speed
15 static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
16
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
21 bool InitDesfireCard(){
22
23 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
24 set_tracing(TRUE);
25
26 byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00};
27 iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
28
29 int len = iso14443a_select_card(NULL,card,NULL);
30
31 if (!len) {
32 if (MF_DBGLEVEL >= MF_DBG_ERROR)
33 Dbprintf("Can't select card");
34 OnError(1);
35 return false;
36 }
37 return true;
38 }
39
40 // ARG0 flag enums
41 enum {
42 NONE = 0x00,
43 INIT = 0x01,
44 DISCONNECT = 0x02,
45 CLEARTRACE = 0x04,
46 BAR = 0x08,
47 } CmdOptions ;
48
49 void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){
50
51 /* ARG0 contains flags.
52 0x01 = init card.
53 0x02 = Disconnect
54 0x03
55 */
56 uint8_t flags = arg0;
57 size_t datalen = arg1;
58 uint8_t resp[RECEIVE_SIZE];
59 memset(resp,0,sizeof(resp));
60
61 if (MF_DBGLEVEL >= 4) {
62 Dbprintf(" flags : %02X", flags);
63 Dbprintf(" len : %02X", datalen);
64 print_result(" RX : ", datain, datalen);
65 }
66
67 if ( flags & CLEARTRACE ){
68 clear_trace();
69 }
70
71 if ( flags & INIT ){
72 if ( !InitDesfireCard() )
73 return;
74 }
75
76 int len = DesfireAPDU(datain, datalen, resp);
77 if (MF_DBGLEVEL >= 4) {
78 print_result("ERR <--: ", resp, len);
79 }
80
81 if ( !len ) {
82 OnError(2);
83 return;
84 }
85
86 // reset the pcb_blocknum,
87 pcb_blocknum = 0;
88
89 if ( flags & DISCONNECT ){
90 OnSuccess();
91 }
92
93 cmd_send(CMD_ACK,1,len,0,resp,len);
94 }
95
96 void MifareDesfireGetInformation(){
97
98 int len = 0;
99 uint8_t resp[USB_CMD_DATA_SIZE] = {0x00};
100 uint8_t dataout[USB_CMD_DATA_SIZE] = {0x00};
101 byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00};
102
103 /*
104 1 = PCB 1
105 2 = cid 2
106 3 = desfire command 3
107 4-5 = crc 4 key
108 5-6 crc
109 PCB == 0x0A because sending CID byte.
110 CID == 0x00 first card?
111 */
112 clear_trace();
113 set_tracing(TRUE);
114 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
115
116 // card select - information
117 iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
118 byte_t isOK = iso14443a_select_card(NULL, card, NULL);
119 if ( isOK == 0) {
120 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
121 Dbprintf("Can't select card");
122 }
123 OnError(1);
124 return;
125 }
126
127 memcpy(dataout,card->uid,7);
128
129 LED_A_ON();
130 LED_B_OFF();
131 LED_C_OFF();
132
133 uint8_t cmd[] = {GET_VERSION};
134 size_t cmd_len = sizeof(cmd);
135
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_A_OFF();
144 LED_B_ON();
145 memcpy(dataout+7,resp+3,7);
146
147 // ADDITION_FRAME 1
148 cmd[0] = ADDITIONAL_FRAME;
149 len = DesfireAPDU(cmd, cmd_len, resp);
150 if ( !len ) {
151 print_result("ERROR <--: ", resp, len);
152 OnError(2);
153 return;
154 }
155
156 LED_B_OFF();
157 LED_C_ON();
158 memcpy(dataout+7+7,resp+3,7);
159
160 // ADDITION_FRAME 2
161 len = DesfireAPDU(cmd, cmd_len, resp);
162 if ( !len ) {
163 print_result("ERROR <--: ", resp, len);
164 OnError(2);
165 return;
166 }
167
168 memcpy(dataout+7+7+7,resp+3,14);
169
170 cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
171
172 // reset the pcb_blocknum,
173 pcb_blocknum = 0;
174 OnSuccess();
175 }
176
177 void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
178
179 int len = 0;
180 //uint8_t PICC_MASTER_KEY8[8] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47};
181 uint8_t PICC_MASTER_KEY16[16] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f };
182 uint8_t null_key_data8[8] = {0x00};
183 //uint8_t null_key_data16[16] = {0x00};
184 //uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
185 //uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
186
187 uint8_t resp[256] = {0x00};
188 uint8_t IV[16] = {0x00};
189
190 size_t datalen = datain[0];
191
192 uint8_t cmd[40] = {0x00};
193 uint8_t encRndB[16] = {0x00};
194 uint8_t decRndB[16] = {0x00};
195 uint8_t nonce[16] = {0x00};
196 uint8_t both[32] = {0x00};
197 uint8_t encBoth[32] = {0x00};
198
199 InitDesfireCard();
200
201 // 3 olika sätt att authenticera. AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
202 // 4 olika crypto algo DES, 3DES, 3K3DES, AES
203 // 3 olika kommunikations sätt, PLAIN,MAC,CRYPTO
204
205 // des, nyckel 0,
206 switch (mode){
207 case 1:{
208 if (algo == 1) {
209
210 uint8_t keybytes[8] = {0x00};
211 uint8_t RndA[8] = {0x00};
212 uint8_t RndB[8] = {0x00};
213
214 if (datain[1] == 0xff){
215 memcpy(keybytes,null_key_data8,8);
216 } else{
217 memcpy(keybytes, datain+1, datalen);
218 }
219
220 struct desfire_key defaultkey = {0};
221 desfirekey_t key = &defaultkey;
222 Desfire_des_key_new(keybytes, key);
223
224 cmd[0] = AUTHENTICATE;
225 cmd[1] = keyno; //keynumber
226 len = DesfireAPDU(cmd, 2, resp);
227 if ( !len ) {
228 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
229 DbpString("Authentication failed. Card timeout.");
230 }
231 OnError(3);
232 return;
233 }
234
235 if ( resp[2] == 0xaf ){
236 } else {
237 DbpString("Authetication failed. Invalid key number.");
238 OnError(3);
239 return;
240 }
241
242 memcpy( encRndB, resp+3, 8);
243
244 des_dec(&decRndB, &encRndB, key->data);
245 memcpy(RndB, decRndB, 8);
246 rol(decRndB,8);
247
248 // This should be random
249 uint8_t decRndA[8] = {0x00};
250 memcpy(RndA, decRndA, 8);
251 uint8_t encRndA[8] = {0x00};
252
253 des_dec(&encRndA, &decRndA, key->data);
254
255 memcpy(both, encRndA, 8);
256
257 for (int x = 0; x < 8; x++) {
258 decRndB[x] = decRndB[x] ^ encRndA[x];
259 }
260
261 des_dec(&encRndB, &decRndB, key->data);
262
263 memcpy(both + 8, encRndB, 8);
264
265 cmd[0] = ADDITIONAL_FRAME;
266 memcpy(cmd+1, both, 16 );
267
268 len = DesfireAPDU(cmd, 17, resp);
269 if ( !len ) {
270 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
271 DbpString("Authentication failed. Card timeout.");
272 }
273 OnError(3);
274 return;
275 }
276
277 if ( resp[2] == 0x00 ){
278
279 struct desfire_key sessionKey = {0};
280 desfirekey_t skey = &sessionKey;
281 Desfire_session_key_new( RndA, RndB , key, skey );
282 //print_result("SESSION : ", skey->data, 8);
283
284 memcpy(encRndA, resp+3, 8);
285 des_dec(&encRndA, &encRndA, key->data);
286 rol(decRndA,8);
287 for (int x = 0; x < 8; x++) {
288 if (decRndA[x] != encRndA[x]) {
289 DbpString("Authetication failed. Cannot varify PICC.");
290 OnError(4);
291 return;
292 }
293 }
294
295 //Change the selected key to a new value.
296 /*
297
298 cmd[0] = CHANGE_KEY;
299 cmd[1] = keyno;
300
301 uint8_t newKey[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
302
303 uint8_t first, second;
304 uint8_t buff1[8] = {0x00};
305 uint8_t buff2[8] = {0x00};
306 uint8_t buff3[8] = {0x00};
307
308 memcpy(buff1,newKey, 8);
309 memcpy(buff2,newKey + 8, 8);
310
311 ComputeCrc14443(CRC_14443_A, newKey, 16, &first, &second);
312 memcpy(buff3, &first, 1);
313 memcpy(buff3 + 1, &second, 1);
314
315 des_dec(&buff1, &buff1, skey->data);
316 memcpy(cmd+2,buff1,8);
317
318 for (int x = 0; x < 8; x++) {
319 buff2[x] = buff2[x] ^ buff1[x];
320 }
321 des_dec(&buff2, &buff2, skey->data);
322 memcpy(cmd+10,buff2,8);
323
324 for (int x = 0; x < 8; x++) {
325 buff3[x] = buff3[x] ^ buff2[x];
326 }
327 des_dec(&buff3, &buff3, skey->data);
328 memcpy(cmd+18,buff3,8);
329
330 // The command always times out on the first attempt, this will retry until a response
331 // is recieved.
332 len = 0;
333 while(!len) {
334 len = DesfireAPDU(cmd,26,resp);
335 }
336 */
337
338 OnSuccess();
339 cmd_send(CMD_ACK,1,0,0,skey->data,8);
340
341 } else {
342 DbpString("Authetication failed.");
343 OnError(6);
344 return;
345 }
346
347 }
348 }
349 break;
350 case 2:
351 //SendDesfireCommand(AUTHENTICATE_ISO, &keyno, resp);
352 break;
353 case 3:{
354
355 //defaultkey
356 uint8_t keybytes[16] = {0x00};
357 if (datain[1] == 0xff){
358 memcpy(keybytes,PICC_MASTER_KEY16,16);
359 } else{
360 memcpy(keybytes, datain+1, datalen);
361 }
362
363 struct desfire_key defaultkey = {0x00};
364 desfirekey_t key = &defaultkey;
365 Desfire_aes_key_new( keybytes, key);
366
367 AesCtx ctx;
368 if ( AesCtxIni(&ctx, IV, key->data, KEY128, CBC) < 0 ){
369 if( MF_DBGLEVEL >= 4) {
370 Dbprintf("AES context failed to init");
371 }
372 OnError(7);
373 return;
374 }
375
376 cmd[0] = AUTHENTICATE_AES;
377 cmd[1] = 0x00; //keynumber
378 len = DesfireAPDU(cmd, 2, resp);
379 if ( !len ) {
380 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
381 DbpString("Authentication failed. Card timeout.");
382 }
383 OnError(3);
384 return;
385 }
386
387 memcpy( encRndB, resp+3, 16);
388
389 // dekryptera tagnonce.
390 AesDecrypt(&ctx, encRndB, decRndB, 16);
391 rol(decRndB,16);
392 memcpy(both, nonce,16);
393 memcpy(both+16, decRndB ,16 );
394 AesEncrypt(&ctx, both, encBoth, 32 );
395
396 cmd[0] = ADDITIONAL_FRAME;
397 memcpy(cmd+1, encBoth, 32 );
398
399 len = DesfireAPDU(cmd, 33, resp); // 1 + 32 == 33
400 if ( !len ) {
401 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
402 DbpString("Authentication failed. Card timeout.");
403 }
404 OnError(3);
405 return;
406 }
407
408 if ( resp[2] == 0x00 ){
409 // Create AES Session key
410 struct desfire_key sessionKey = {0};
411 desfirekey_t skey = &sessionKey;
412 Desfire_session_key_new( nonce, decRndB , key, skey );
413 print_result("SESSION : ", skey->data, 16);
414 } else {
415 DbpString("Authetication failed.");
416 OnError(7);
417 return;
418 }
419 break;
420 }
421 }
422
423 OnSuccess();
424 cmd_send(CMD_ACK,1,len,0,resp,len);
425 }
426
427 // 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO)
428 // cmd = cmd bytes to send
429 // cmd_len = length of cmd
430 // dataout = pointer to response data array
431 int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout){
432
433 size_t len = 0;
434 size_t wrappedLen = 0;
435 uint8_t wCmd[USB_CMD_DATA_SIZE] = {0x00};
436
437 uint8_t resp[MAX_FRAME_SIZE];
438 uint8_t par[MAX_PARITY_SIZE];
439
440 wrappedLen = CreateAPDU( cmd, cmd_len, wCmd);
441
442 if (MF_DBGLEVEL >= 4) {
443 print_result("WCMD <--: ", wCmd, wrappedLen);
444 }
445 ReaderTransmit( wCmd, wrappedLen, NULL);
446
447 len = ReaderReceive(resp, par);
448
449 if( len == 0x00 ){
450 if (MF_DBGLEVEL >= 4) {
451 Dbprintf("fukked");
452 }
453 return FALSE; //DATA LINK ERROR
454 }
455 // if we received an I- or R(ACK)-Block with a block number equal to the
456 // current block number, toggle the current block number
457 else if (len >= 4 // PCB+CID+CRC = 4 bytes
458 && ((resp[0] & 0xC0) == 0 // I-Block
459 || (resp[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
460 && (resp[0] & 0x01) == pcb_blocknum) // equal block numbers
461 {
462 pcb_blocknum ^= 1; //toggle next block
463 }
464
465 memcpy(dataout, resp, len);
466 return len;
467 }
468
469 // CreateAPDU
470 size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout){
471
472 size_t cmdlen = MIN(len+4, USB_CMD_DATA_SIZE-1);
473
474 uint8_t cmd[cmdlen];
475 memset(cmd, 0, cmdlen);
476
477 cmd[0] = 0x0A; // 0x0A = skicka cid, 0x02 = ingen cid. Särskilda bitar //
478 cmd[0] |= pcb_blocknum; // OR the block number into the PCB
479 cmd[1] = 0x00; // CID: 0x00 //TODO: allow multiple selected cards
480
481 memcpy(cmd+2, datain, len);
482 AppendCrc14443a(cmd, len+2);
483
484 memcpy(dataout, cmd, cmdlen);
485
486 return cmdlen;
487 }
488
489 // crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
490 // crc_update(&desfire_crc32, addr, addr_sz);
491 // crc_update(&desfire_crc32, byte, 8);
492 // uint32_t crc = crc_finish(&desfire_crc32);
493
494 void OnSuccess(){
495 pcb_blocknum = 0;
496 ReaderTransmit(deselect_cmd, 3 , NULL);
497 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
498 LEDsoff();
499 }
500
501 void OnError(uint8_t reason){
502 pcb_blocknum = 0;
503 ReaderTransmit(deselect_cmd, 3 , NULL);
504
505 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
506 cmd_send(CMD_ACK,0,reason,0,0,0);
507 LEDsoff();
508 }
Impressum, Datenschutz