]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifaredesfire.c
chg: syntax sugar
[proxmark3-svn] / armsrc / mifaredesfire.c
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
12 uint8_t pcb_blocknum = 0;
13 // Deselect card by sending a s-block. the crc is precalced for speed
14 static 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
20 bool 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
36 enum {
37 NONE = 0x00,
38 INIT = 0x01,
39 DISCONNECT = 0x02,
40 CLEARTRACE = 0x04,
41 BAR = 0x08,
42 } CmdOptions ;
43
44 void 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
88 void 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
165 void 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 // 3 olika sätt att authenticera. AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
194 // 4 olika crypto algo DES, 3DES, 3K3DES, AES
195 // 3 olika kommunikations sätt, PLAIN,MAC,CRYPTO
196
197 // des, nyckel 0,
198 switch (mode){
199 case 1:{
200 uint8_t keybytes[16];
201 uint8_t RndA[8] = {0x00};
202 uint8_t RndB[8] = {0x00};
203
204 if (algo == 2) {
205 if (datain[1] == 0xff){
206 memcpy(keybytes,PICC_MASTER_KEY16,16);
207 } else {
208 memcpy(keybytes, datain+1, datalen);
209 }
210 } else {
211 if (algo == 1) {
212 if (datain[1] == 0xff){
213 memcpy(keybytes,null_key_data8,8);
214 } else{
215 memcpy(keybytes, datain+1, datalen);
216 }
217 }
218 }
219
220 struct desfire_key defaultkey = {0};
221 desfirekey_t key = &defaultkey;
222
223 if (algo == 2)
224 Desfire_3des_key_new_with_version(keybytes, key);
225 else if (algo ==1)
226 Desfire_des_key_new(keybytes, key);
227
228 cmd[0] = AUTHENTICATE;
229 cmd[1] = keyno; //keynumber
230 len = DesfireAPDU(cmd, 2, resp);
231 if ( !len ) {
232 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
233 DbpString("Authentication failed. Card timeout.");
234 }
235 OnError(3);
236 return;
237 }
238
239 if ( resp[2] == 0xaf ){
240 } else {
241 DbpString("Authetication failed. Invalid key number.");
242 OnError(3);
243 return;
244 }
245
246 memcpy( encRndB, resp+3, 8);
247 if (algo == 2)
248 tdes_dec(&decRndB, &encRndB, key->data);
249 else if (algo == 1)
250 des_dec(&decRndB, &encRndB, key->data);
251
252 memcpy(RndB, decRndB, 8);
253 rol(decRndB,8);
254
255 // This should be random
256 uint8_t decRndA[8] = {0x00};
257 memcpy(RndA, decRndA, 8);
258 uint8_t encRndA[8] = {0x00};
259
260 if (algo == 2)
261 tdes_dec(&encRndA, &decRndA, key->data);
262 else if (algo == 1)
263 des_dec(&encRndA, &decRndA, key->data);
264
265 memcpy(both, encRndA, 8);
266
267 for (int x = 0; x < 8; x++) {
268 decRndB[x] = decRndB[x] ^ encRndA[x];
269
270 }
271
272 if (algo == 2)
273 tdes_dec(&encRndB, &decRndB, key->data);
274 else if (algo == 1)
275 des_dec(&encRndB, &decRndB, key->data);
276
277 memcpy(both + 8, encRndB, 8);
278
279 cmd[0] = ADDITIONAL_FRAME;
280 memcpy(cmd+1, both, 16 );
281
282 len = DesfireAPDU(cmd, 17, resp);
283 if ( !len ) {
284 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
285 DbpString("Authentication failed. Card timeout.");
286 }
287 OnError(3);
288 return;
289 }
290
291 if ( resp[2] == 0x00 ){
292
293 struct desfire_key sessionKey = {0};
294 desfirekey_t skey = &sessionKey;
295 Desfire_session_key_new( RndA, RndB , key, skey );
296 //print_result("SESSION : ", skey->data, 8);
297
298 memcpy(encRndA, resp+3, 8);
299
300 if (algo == 2)
301 tdes_dec(&encRndA, &encRndA, key->data);
302 else if (algo == 1)
303 des_dec(&encRndA, &encRndA, key->data);
304
305 rol(decRndA,8);
306 for (int x = 0; x < 8; x++) {
307 if (decRndA[x] != encRndA[x]) {
308 DbpString("Authetication failed. Cannot varify PICC.");
309 OnError(4);
310 return;
311 }
312 }
313
314 //Change the selected key to a new value.
315 /*
316
317 // Current key is a 3DES key, change it to a DES key
318 if (algo == 2) {
319 cmd[0] = CHANGE_KEY;
320 cmd[1] = keyno;
321
322 uint8_t newKey[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
323
324 uint8_t first, second;
325 uint8_t buff1[8] = {0x00};
326 uint8_t buff2[8] = {0x00};
327 uint8_t buff3[8] = {0x00};
328
329 memcpy(buff1,newKey, 8);
330 memcpy(buff2,newKey + 8, 8);
331
332 ComputeCrc14443(CRC_14443_A, newKey, 16, &first, &second);
333 memcpy(buff3, &first, 1);
334 memcpy(buff3 + 1, &second, 1);
335
336 tdes_dec(&buff1, &buff1, skey->data);
337 memcpy(cmd+2,buff1,8);
338
339 for (int x = 0; x < 8; x++) {
340 buff2[x] = buff2[x] ^ buff1[x];
341 }
342 tdes_dec(&buff2, &buff2, skey->data);
343 memcpy(cmd+10,buff2,8);
344
345 for (int x = 0; x < 8; x++) {
346 buff3[x] = buff3[x] ^ buff2[x];
347 }
348 tdes_dec(&buff3, &buff3, skey->data);
349 memcpy(cmd+18,buff3,8);
350
351 // The command always times out on the first attempt, this will retry until a response
352 // is recieved.
353 len = 0;
354 while(!len) {
355 len = DesfireAPDU(cmd,26,resp);
356 }
357
358 } else {
359 // Current key is a DES key, change it to a 3DES key
360 if (algo == 1) {
361 cmd[0] = CHANGE_KEY;
362 cmd[1] = keyno;
363
364 uint8_t newKey[16] = {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f};
365
366 uint8_t first, second;
367 uint8_t buff1[8] = {0x00};
368 uint8_t buff2[8] = {0x00};
369 uint8_t buff3[8] = {0x00};
370
371 memcpy(buff1,newKey, 8);
372 memcpy(buff2,newKey + 8, 8);
373
374 ComputeCrc14443(CRC_14443_A, newKey, 16, &first, &second);
375 memcpy(buff3, &first, 1);
376 memcpy(buff3 + 1, &second, 1);
377
378 des_dec(&buff1, &buff1, skey->data);
379 memcpy(cmd+2,buff1,8);
380
381 for (int x = 0; x < 8; x++) {
382 buff2[x] = buff2[x] ^ buff1[x];
383 }
384 des_dec(&buff2, &buff2, skey->data);
385 memcpy(cmd+10,buff2,8);
386
387 for (int x = 0; x < 8; x++) {
388 buff3[x] = buff3[x] ^ buff2[x];
389 }
390 des_dec(&buff3, &buff3, skey->data);
391 memcpy(cmd+18,buff3,8);
392
393 // The command always times out on the first attempt, this will retry until a response
394 // is recieved.
395 len = 0;
396 while(!len) {
397 len = DesfireAPDU(cmd,26,resp);
398 }
399 }
400 }
401 */
402
403 OnSuccess();
404 if (algo == 2)
405 cmd_send(CMD_ACK,1,0,0,skey->data,16);
406 else if (algo == 1)
407 cmd_send(CMD_ACK,1,0,0,skey->data,8);
408 } else {
409 DbpString("Authetication failed.");
410 OnError(6);
411 return;
412 }
413 }
414 break;
415 case 2:
416 //SendDesfireCommand(AUTHENTICATE_ISO, &keyno, resp);
417 break;
418 case 3:{
419
420 //defaultkey
421 uint8_t keybytes[16] = {0x00};
422 if (datain[1] == 0xff){
423 memcpy(keybytes,PICC_MASTER_KEY16,16);
424 } else{
425 memcpy(keybytes, datain+1, datalen);
426 }
427
428 struct desfire_key defaultkey = {0x00};
429 desfirekey_t key = &defaultkey;
430 Desfire_aes_key_new( keybytes, key);
431
432 AesCtx ctx;
433 if ( AesCtxIni(&ctx, IV, key->data, KEY128, CBC) < 0 ){
434 if( MF_DBGLEVEL >= 4) {
435 Dbprintf("AES context failed to init");
436 }
437 OnError(7);
438 return;
439 }
440
441 cmd[0] = AUTHENTICATE_AES;
442 cmd[1] = 0x00; //keynumber
443 len = DesfireAPDU(cmd, 2, resp);
444 if ( !len ) {
445 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
446 DbpString("Authentication failed. Card timeout.");
447 }
448 OnError(3);
449 return;
450 }
451
452 memcpy( encRndB, resp+3, 16);
453
454 // dekryptera tagnonce.
455 AesDecrypt(&ctx, encRndB, decRndB, 16);
456 rol(decRndB,16);
457 memcpy(both, nonce,16);
458 memcpy(both+16, decRndB ,16 );
459 AesEncrypt(&ctx, both, encBoth, 32 );
460
461 cmd[0] = ADDITIONAL_FRAME;
462 memcpy(cmd+1, encBoth, 32 );
463
464 len = DesfireAPDU(cmd, 33, resp); // 1 + 32 == 33
465 if ( !len ) {
466 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
467 DbpString("Authentication failed. Card timeout.");
468 }
469 OnError(3);
470 return;
471 }
472
473 if ( resp[2] == 0x00 ){
474 // Create AES Session key
475 struct desfire_key sessionKey = {0};
476 desfirekey_t skey = &sessionKey;
477 Desfire_session_key_new( nonce, decRndB , key, skey );
478 print_result("SESSION : ", skey->data, 16);
479 } else {
480 DbpString("Authetication failed.");
481 OnError(7);
482 return;
483 }
484
485 break;
486 }
487 }
488
489 OnSuccess();
490 cmd_send(CMD_ACK,1,len,0,resp,len);
491 }
492
493 // 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO)
494 // cmd = cmd bytes to send
495 // cmd_len = length of cmd
496 // dataout = pointer to response data array
497 int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout){
498
499 size_t len = 0;
500 size_t wrappedLen = 0;
501 uint8_t wCmd[USB_CMD_DATA_SIZE] = {0x00};
502 uint8_t resp[MAX_FRAME_SIZE];
503 uint8_t par[MAX_PARITY_SIZE];
504
505 wrappedLen = CreateAPDU( cmd, cmd_len, wCmd);
506
507 if (MF_DBGLEVEL >= 4)
508 print_result("WCMD <--: ", wCmd, wrappedLen);
509
510 ReaderTransmit( wCmd, wrappedLen, NULL);
511
512 len = ReaderReceive(resp, par);
513 if ( !len ) {
514 if (MF_DBGLEVEL >= 4) Dbprintf("fukked");
515 return FALSE; //DATA LINK ERROR
516 }
517 // if we received an I- or R(ACK)-Block with a block number equal to the
518 // current block number, toggle the current block number
519 else if (len >= 4 // PCB+CID+CRC = 4 bytes
520 && ((resp[0] & 0xC0) == 0 // I-Block
521 || (resp[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
522 && (resp[0] & 0x01) == pcb_blocknum) // equal block numbers
523 {
524 pcb_blocknum ^= 1; //toggle next block
525 }
526
527 memcpy(dataout, resp, len);
528 return len;
529 }
530
531 // CreateAPDU
532 size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout){
533
534 size_t cmdlen = MIN(len+4, USB_CMD_DATA_SIZE-1);
535
536 uint8_t cmd[cmdlen];
537 memset(cmd, 0, cmdlen);
538
539 cmd[0] = 0x0A; // 0x0A = skicka cid, 0x02 = ingen cid. Särskilda bitar //
540 cmd[0] |= pcb_blocknum; // OR the block number into the PCB
541 cmd[1] = 0x00; // CID: 0x00 //TODO: allow multiple selected cards
542
543 memcpy(cmd+2, datain, len);
544 AppendCrc14443a(cmd, len+2);
545
546 memcpy(dataout, cmd, cmdlen);
547
548 return cmdlen;
549 }
550
551 // crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
552 // crc_update(&desfire_crc32, addr, addr_sz);
553 // crc_update(&desfire_crc32, byte, 8);
554 // uint32_t crc = crc_finish(&desfire_crc32);
555
556 void OnSuccess(){
557 pcb_blocknum = 0;
558 ReaderTransmit(deselect_cmd, 3 , NULL);
559 mifare_ultra_halt();
560 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
561 LEDsoff();
562 set_tracing(FALSE);
563 }
564
565 void OnError(uint8_t reason){
566 cmd_send(CMD_ACK,0,reason,0,0,0);
567 OnSuccess();
568 }
Impressum, Datenschutz