1 #include "mifaredesfire.h" 
   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) 
   9 // the block number for the ISO14443-4 PCB 
  10 uint8_t pcb_blocknum 
= 0; 
  11 // Deselect card by sending a s-block. the crc is precalced for speed 
  12 static  uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4}; 
  14 //static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 }; 
  15 /*                                       PCB   CID   CMD    PAYLOAD    */ 
  16 //static uint8_t __res[MAX_FRAME_SIZE]; 
  18 bool InitDesfireCard(){ 
  20         // Make sure it is off. 
  21 //      FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); 
  24         byte_t cardbuf
[USB_CMD_DATA_SIZE
]; 
  25         memset(cardbuf
,0,sizeof(cardbuf
)); 
  27         iso14a_set_tracing(TRUE
); 
  28         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
  30         iso14a_card_select_t 
*card 
= (iso14a_card_select_t
*)cardbuf
; 
  31         int len 
= iso14443a_select_card(NULL
,card
,NULL
); 
  34                 if (MF_DBGLEVEL 
>= 1) { 
  35                         Dbprintf("Can't select card"); 
  52 void MifareSendCommand(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
){ 
  54         /* ARG0 contains flags. 
  60         size_t datalen 
= arg1
; 
  61         uint8_t resp
[RECV_RES_SIZE
]; 
  62         memset(resp
,0,sizeof(resp
)); 
  64         if (MF_DBGLEVEL 
>= 4) { 
  65                 Dbprintf(" flags : %02X", flags
); 
  66                 Dbprintf(" len   : %02X", datalen
); 
  67                 print_result(" RX    : ", datain
, datalen
); 
  71                 if ( !InitDesfireCard() ) 
  75         int len 
= DesfireAPDU(datain
, datalen
, resp
); 
  76         print_result(" <--: ", resp
, len
);       
  78                 if (MF_DBGLEVEL 
>= 4) { 
  79                         print_result("ERR <--: ", resp
, len
);    
  85         // reset the pcb_blocknum, 
  88         if ( flags 
& DISCONNECT 
) 
  91         cmd_send(CMD_ACK
,1,len
,0,resp
,len
); 
  94 void MifareDesfireGetInformation(){ 
  97         uint8_t resp
[USB_CMD_DATA_SIZE
]; 
  98         uint8_t dataout
[USB_CMD_DATA_SIZE
]; 
  99         byte_t cardbuf
[USB_CMD_DATA_SIZE
]; 
 101         memset(resp
,0,sizeof(resp
)); 
 102         memset(dataout
,0, sizeof(dataout
)); 
 103         memset(cardbuf
,0,sizeof(cardbuf
)); 
 108                 3 = desfire command             3  
 111                 PCB == 0x0A because sending CID byte. 
 112                 CID == 0x00 first card?          
 114         iso14a_clear_trace(); 
 115         iso14a_set_tracing(TRUE
); 
 116         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 118         // card select - information 
 119         iso14a_card_select_t 
*card 
= (iso14a_card_select_t
*)cardbuf
; 
 120         byte_t isOK 
= iso14443a_select_card(NULL
, card
, NULL
); 
 122                 if (MF_DBGLEVEL 
>= 1) { 
 123                         Dbprintf("Can't select card"); 
 129         memcpy(dataout
,card
->uid
,7); 
 135         uint8_t cmd
[] = {GET_VERSION
};   
 136         size_t cmd_len 
= sizeof(cmd
); 
 138         len 
=  DesfireAPDU(cmd
, cmd_len
, resp
); 
 140                 print_result("ERROR <--: ", resp
, len
);  
 147         memcpy(dataout
+7,resp
+3,7); 
 150         cmd
[0] = ADDITIONAL_FRAME
; 
 151         len 
=  DesfireAPDU(cmd
, cmd_len
, resp
); 
 153                 print_result("ERROR <--: ", resp
, len
);  
 160         memcpy(dataout
+7+7,resp
+3,7); 
 163         len 
=  DesfireAPDU(cmd
, cmd_len
, resp
); 
 165                 print_result("ERROR <--: ", resp
, len
);  
 170         memcpy(dataout
+7+7+7,resp
+3,14); 
 172         cmd_send(CMD_ACK
,1,0,0,dataout
,sizeof(dataout
)); 
 174         // reset the pcb_blocknum, 
 179 void MifareDES_Auth1(uint8_t mode
, uint8_t algo
, uint8_t keyno
,  uint8_t *datain
){ 
 181         uint8_t null_key_data
[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
 182         //uint8_t new_key_data[8]  = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; 
 185         desfirekey_t default_key 
= Desfire_des_key_new_with_version (null_key_data
); 
 187         // res = Desfire_select_application (tags[i], aid); 
 189                 print_result("default key: ", default_key
->data
, 24 ); 
 194         //                pcb  cid  cmd               key   crc1  cr2            
 195         //uint8_t cmd2[] = {0x02,0x00,GET_KEY_VERSION, 0x00, 0x00, 0x00 }; 
 197         //uint8_t* bigbuffer = mifare_get_bigbufptr(); 
 203         // första byten håller keylength. 
 204         uint8_t keylen 
= datain
[0]; 
 205         memcpy(key
, datain
+1, keylen
); 
 207         if (MF_DBGLEVEL 
>= 1) { 
 209                 Dbprintf("MODE: %d", mode
); 
 210                 Dbprintf("ALGO: %d", algo
); 
 211                 Dbprintf("KEYNO: %d", keyno
); 
 212                 Dbprintf("KEYLEN: %d", keylen
); 
 214                 print_result("KEY", key
, keylen
); 
 217         // card select - information 
 218         byte_t buf
[USB_CMD_DATA_SIZE
]; 
 219         iso14a_card_select_t 
*card 
= (iso14a_card_select_t
*)buf
; 
 221         // test of DES on ARM side. 
 232                 memset(tmpData, 0 ,8); 
 233                 memset(tmpPlain,0 ,8); 
 234                 memcpy(key, datain, 8); 
 235                 memcpy(plain, datain+30, 16); 
 237                 for(uint8_t i=0; i< sizeof(plain); i=i+8 ){ 
 239                         memcpy(tmpPlain, plain+i, 8); 
 240                         des_enc( &tmpData, &tmpPlain, &key); 
 241                         memcpy(encData+i, tmpData, 8); 
 246         iso14a_clear_trace(); 
 248         iso14a_set_tracing(TRUE
); 
 250         // power up the field 
 251         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 254         isOK 
= iso14443a_select_card(resp
, card
, NULL
); 
 256                 if (MF_DBGLEVEL 
>= 1) { 
 257                         Dbprintf("CAN'T SELECT CARD, SOMETHING WENT WRONG BEFORE AUTH"); 
 267         // 3 olika sätt att authenticera.   AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32) 
 268         // 4 olika crypto algo   DES, 3DES, 3K3DES, AES 
 269         // 3 olika kommunikations sätt,   PLAIN,MAC,CRYPTO 
 274                         // if ( SendDesfireCommand(AUTHENTICATE, &keyno, resp) > 0 ){ 
 275                                 // // fick nonce från kortet 
 279                         //SendDesfireCommand(AUTHENTICATE_ISO, &keyno, resp); 
 283                         if ( AesCtxIni(&ctx
, IV
, key
, KEY128
, CBC
) < 0 ){ 
 284                                 if (MF_DBGLEVEL 
>= 1) { 
 285                                         Dbprintf("AES context failed to init"); 
 293                         real_cmd
[2] = AUTHENTICATE_AES
; 
 296                         AppendCrc14443a(real_cmd
, 4); 
 297                         ReaderTransmit(real_cmd
, sizeof(real_cmd
), NULL
); 
 299                         int len 
= ReaderReceive(resp
); 
 305                         print_result("RX:", resp
, len
); 
 307                         enum DESFIRE_STATUS status 
= resp
[1]; 
 308                         if ( status 
!= ADDITIONAL_FRAME
) { 
 320                         memset(nonce
, 0, 16); 
 321                         memcpy( encRndB
, resp
+2, 16); 
 323                         // dekryptera tagnonce. 
 324                         AesDecrypt(&ctx
, encRndB
, decRndB
, 16); 
 328                         memcpy(both
, nonce
,16); 
 329                         memcpy(both
+16, decRndB 
,16 ); 
 331                         AesEncrypt(&ctx
, both
, encBoth
, 32 ); 
 333                         uint8_t real_cmd_A
[36]; 
 334                         real_cmd_A
[0] = 0x03; 
 335                         real_cmd_A
[1] = ADDITIONAL_FRAME
; 
 337                         memcpy(real_cmd_A
+2, encBoth
, sizeof(encBoth
) ); 
 338                         AppendCrc14443a(real_cmd_A
, 34); 
 339                         ReaderTransmit(real_cmd_A
, sizeof(real_cmd_A
), NULL
); 
 341                         len 
= ReaderReceive(resp
); 
 343                         print_result("Auth1a ", resp
, 36); 
 346                         if ( status 
!= OPERATION_OK
)    { 
 347                                 Dbprintf("Cmd Error: %02x  Len: %d", status
,len
); 
 360 // 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO) 
 361 // cmd  =  cmd bytes to send 
 362 // cmd_len = length of cmd 
 363 // dataout = pointer to response data array 
 364 int DesfireAPDU(uint8_t *cmd
, size_t cmd_len
, uint8_t *dataout
){ 
 367         size_t wrappedLen 
= 0; 
 368         uint8_t wCmd
[USB_CMD_DATA_SIZE
]; 
 370         wrappedLen 
= CreateAPDU( cmd
, cmd_len
, wCmd
); 
 372         if (MF_DBGLEVEL 
>= 4) { 
 373                 print_result("WCMD <--: ", wCmd
, wrappedLen
);    
 375         ReaderTransmit( wCmd
, wrappedLen
, NULL
); 
 377         status 
= ReaderReceive(dataout
); 
 380                 return FALSE
; //DATA LINK ERROR 
 382         // if we received an I- or R(ACK)-Block with a block number equal to the 
 383         // current block number, toggle the current block number 
 384         else if (status 
>= 4 // PCB+CID+CRC = 4 bytes 
 385                  && ((dataout
[0] & 0xC0) == 0 // I-Block 
 386                      || (dataout
[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0 
 387                  && (dataout
[0] & 0x01) == pcb_blocknum
) // equal block numbers 
 389                 pcb_blocknum 
^= 1;  //toggle next block  
 395 size_t CreateAPDU( uint8_t *datain
, size_t len
, uint8_t *dataout
){ 
 397         size_t cmdlen 
= MIN(len
+4, USB_CMD_DATA_SIZE
-1); 
 400         memset(cmd
, 0, cmdlen
); 
 402         cmd
[0] = 0x0A;  //  0x0A = skicka cid,  0x02 = ingen cid. Särskilda bitar // 
 403         cmd
[0] |= pcb_blocknum
; // OR the block number into the PCB      
 404         cmd
[1] = 0x00;  //  CID: 0x00 //FIXME: allow multiple selected cards 
 406         memcpy(cmd
+2, datain
, len
); 
 407         AppendCrc14443a(cmd
, len
+2); 
 409         memcpy(dataout
, cmd
, cmdlen
); 
 414                         // crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */ 
 415                         // crc_update(&desfire_crc32, addr, addr_sz); 
 416                         // crc_update(&desfire_crc32, byte, 8); 
 417                         // uint32_t crc = crc_finish(&desfire_crc32); 
 422         //uint8_t versionCmd1[] = {0x02, 0x60}; 
 423         //uint8_t versionCmd2[] = {0x03, 0xaf}; 
 424         //uint8_t versionCmd3[] = {0x02, 0xaf}; 
 426     // AUTH 1  -  CMD: 0x02, 0x0A, 0x00  = Auth 
 427         // 0x02 = status byte för simpla svar?!?  
 430         //uint8_t initAuthCmdDES[]  = {0x02, 0x0a, 0x00};  // DES 
 431         //uint8_t initAuthCmd3DES[] = {0x02, 0x1a, 0x00};  // 3DES 
 432         //uint8_t initAuthCmdAES[]  = {0x02, 0xaa, 0x00};  // AES 
 433         // auth 1 - answer command 
 434         // 0x03 = status byte för komplexa typer? 
 435         // 0xaf = additional frame 
 436         // LEN = 1+1+32+2 = 36 
 437         //uint8_t answerAuthCmd[34] = {0x03, 0xaf};  
 440         //AppendCrc14443a(versionCmd1,sizeof(versionCmd1)); 
 444         /*ReaderTransmit(versionCmd1,sizeof(versionCmd1)+2, NULL); 
 445         len = ReaderReceive(buffer); 
 446         print_result("Get Version 3", buffer, 9); 
 449         // for( int i = 0; i < 8; i++){ 
 450                 // // Auth 1 - Request authentication 
 451                 // ReaderTransmit(initAuthCmdAES,sizeof(initAuthCmdAES)+2, NULL); 
 452                 // //len = ReaderReceive(buffer); 
 454                 // // 0xAE =  authentication error 
 455                 // if (buffer[1] == 0xae)       { 
 456                                 // Dbprintf("Cmd Error: %02x", buffer[1]); 
 462                 // memcpy(encRndB, buffer+2, 16); 
 464                 // // dekryptera svaret från tag. 
 465                 // AesDecrypt(&ctx, encRndB, decRndB, 16); 
 468                 // memcpy(RndARndB, RndA,16); 
 469                 // memcpy(RndARndB+16, decRndB ,16 ); 
 471                 // AesEncrypt(&ctx, RndARndB, encRndARndB, 32 ); 
 473                 // memcpy(answerAuthCmd+2, encRndARndB, 32); 
 474                 // AppendCrc14443a(answerAuthCmd,sizeof(answerAuthCmd)); 
 476                 // ReaderTransmit(answerAuthCmd,sizeof(answerAuthCmd)+2, NULL); 
 478                 // len = ReaderReceive(buffer); 
 480                 // print_result("Auth1a ", buffer, 8); 
 481                 // Dbprintf("Rx len: %02x", len); 
 483                 // if (buffer[1] == 0xCA)       { 
 484                                 // Dbprintf("Cmd Error: %02x  Len: %d", buffer[1],len); 
 485                                 // cmd_send(CMD_ACK,0,0,0,0,0); 
 487                                 // AesCtxIni(&ctx, iv, key, KEY128, CBC); 
 491         //des_dec(decRndB, encRndB, key); 
 495     DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0); 
 496     memcpy(RndARndB,RndA,8); 
 497     memcpy(RndARndB+8,RndB,8); 
 498     PrintAndLog("     RA+B:%s",sprint_hex(RndARndB, 16)); 
 499     DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1); 
 500     PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16)); 
 504 int mifare_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){ 
 506         uint8_t* buffer 
= mifare_get_bigbufptr(); 
 510     memcpy(dcmd
+1,key
,16); 
 511         AppendCrc14443a(dcmd
, 17); 
 514         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
 515         int len 
= ReaderReceive(buffer
); 
 517           if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Card timeout."); 
 518           len 
= ReaderReceive(buffer
); 
 522         if (MF_DBGLEVEL 
>= 1) { 
 523                         Dbprintf("NAK - Authentication failed."); 
 524                         Dbprintf("Cmd Error: %02x", buffer
[0]); 
 530                 if (MF_DBGLEVEL 
>= 1) { 
 531                         Dbprintf("Auth2 Resp: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 
 532                                           buffer
[0],buffer
[1],buffer
[2],buffer
[3],buffer
[4], 
 533                                           buffer
[5],buffer
[6],buffer
[7],buffer
[8],buffer
[9], 
 541 void MifareDES_Auth2(uint32_t arg0
, uint8_t *datain
){ 
 544         uint32_t cuid 
= arg0
; 
 548         byte_t dataoutbuf
[16]; 
 551         memcpy(key
, datain
, 16); 
 557         if(mifare_des_auth2(cuid
, key
, dataoutbuf
)){ 
 558             if (MF_DBGLEVEL 
>= 1) Dbprintf("Authentication part2: Fail...");     
 561         if (MF_DBGLEVEL 
>= 2)   DbpString("AUTH 2 FINISHED"); 
 564     cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,11); 
 568         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 574         ReaderTransmit(deselect_cmd
, 3 , NULL
); 
 575         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 581         ReaderTransmit(deselect_cmd
, 3 , NULL
); 
 582         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 583         cmd_send(CMD_ACK
,0,0,0,0,0);