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 //static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
10 /* PCB CID CMD PAYLOAD */
11 //static uint8_t __res[MAX_FRAME_SIZE];
13 void MifareDesfireGetInformation(){
17 uint8_t resp
[RECV_RES_SIZE
];
18 uint8_t dataout
[RECV_CMD_SIZE
];
19 byte_t buf
[RECV_RES_SIZE
];
21 memset(resp
,0,sizeof(resp
));
22 memset(dataout
,0, sizeof(dataout
));
23 memset(buf
,0,sizeof(buf
));
32 PCB == 0x0A because sending CID byte.
33 CID == 0x00 first card?
36 uint8_t cmd1
[] = {0x0a,0x00,GET_VERSION
, 0x00, 0x00 };
37 uint8_t cmd2
[] = {0x0a,0x00,GET_KEY_VERSION
, 0x00, 0x00, 0x00 };
40 iso14a_set_tracing(TRUE
);
41 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
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
);
47 if (MF_DBGLEVEL
>= 1) {
48 Dbprintf("Can't select card");
55 memcpy(dataout
,card
->uid
,7);
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
);
71 memcpy(dataout
+7,resp
+3,7);
75 cmd1
[2] = ADDITIONAL_FRAME
;
76 AppendCrc14443a(cmd1
, 3);
77 ReaderTransmit(cmd1
, sizeof(cmd1
), NULL
);
78 len
= ReaderReceive(resp
);
80 if ( resp
[2] != ADDITIONAL_FRAME
) {
81 print_result("ERROR <--: ", resp
, len
);
85 memcpy(dataout
+7+7,resp
+3,7);
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
);
98 memcpy(dataout
+7+7+7,resp
+3,14);
100 // GET MASTER KEYSETTINGS
101 cmd1
[2] = GET_KEY_SETTINGS
;
102 AppendCrc14443a(cmd1
, 3);
103 ReaderTransmit(cmd1
, sizeof(cmd1
), NULL
);
104 len
= ReaderReceive(resp
);
106 memcpy(dataout
+7+7+7+14,resp
+3,2);
110 // GET MASTER KEY VERSION
111 AppendCrc14443a(cmd2
, 4);
112 ReaderTransmit(cmd2
, sizeof(cmd2
), NULL
);
113 len
= ReaderReceive(resp
);
115 memcpy(dataout
+7+7+7+14+2,resp
+3,1);
119 cmd1
[2] = GET_FREE_MEMORY
;
120 AppendCrc14443a(cmd1
, 3);
121 ReaderTransmit(cmd1
, sizeof(cmd1
), NULL
);
122 len
= ReaderReceive(resp
);
124 memcpy(dataout
+7+7+7+14+2+1,resp
+3,3);
127 cmd_send(CMD_ACK
,1,0,0,dataout
,sizeof(dataout
));
131 void MifareDES_Auth1(uint8_t mode
, uint8_t algo
, uint8_t keyno
, uint8_t *datain
){
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 };
137 desfirekey_t default_key
= Desfire_des_key_new_with_version (null_key_data
);
139 // res = Desfire_select_application (tags[i], aid);
141 print_result("default key: ", default_key
->data
, 24 );
146 // pcb cid cmd key crc1 cr2
147 //uint8_t cmd2[] = {0x02,0x00,GET_KEY_VERSION, 0x00, 0x00, 0x00 };
149 //uint8_t* bigbuffer = mifare_get_bigbufptr();
155 // första byten håller keylength.
156 uint8_t keylen
= datain
[0];
157 memcpy(key
, datain
+1, keylen
);
159 if (MF_DBGLEVEL
>= 1) {
161 Dbprintf("MODE: %d", mode
);
162 Dbprintf("ALGO: %d", algo
);
163 Dbprintf("KEYNO: %d", keyno
);
164 Dbprintf("KEYLEN: %d", keylen
);
166 print_result("KEY", key
, keylen
);
169 // card select - information
170 byte_t buf
[USB_CMD_DATA_SIZE
];
171 iso14a_card_select_t
*card
= (iso14a_card_select_t
*)buf
;
173 // test of DES on ARM side.
184 memset(tmpData, 0 ,8);
185 memset(tmpPlain,0 ,8);
186 memcpy(key, datain, 8);
187 memcpy(plain, datain+30, 16);
189 for(uint8_t i=0; i< sizeof(plain); i=i+8 ){
191 memcpy(tmpPlain, plain+i, 8);
192 des_enc( &tmpData, &tmpPlain, &key);
193 memcpy(encData+i, tmpData, 8);
198 iso14a_clear_trace();
200 iso14a_set_tracing(TRUE
);
202 // power up the field
203 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
206 isOK
= iso14443a_select_card(resp
, card
, NULL
);
208 if (MF_DBGLEVEL
>= 1) {
209 Dbprintf("CAN'T SELECT CARD, SOMETHING WENT WRONG BEFORE AUTH");
219 // 3 olika sätt att authenticera. AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
220 // 4 olika crypto algo DES, 3DES, 3K3DES, AES
221 // 3 olika kommunikations sätt, PLAIN,MAC,CRYPTO
226 // if ( SendDesfireCommand(AUTHENTICATE, &keyno, resp) > 0 ){
227 // // fick nonce från kortet
231 //SendDesfireCommand(AUTHENTICATE_ISO, &keyno, resp);
235 if ( AesCtxIni(&ctx
, IV
, key
, KEY128
, CBC
) < 0 ){
236 if (MF_DBGLEVEL
>= 1) {
237 Dbprintf("AES context failed to init");
245 real_cmd
[2] = AUTHENTICATE_AES
;
248 AppendCrc14443a(real_cmd
, 2);
249 ReaderTransmit(real_cmd
, sizeof(real_cmd
), NULL
);
251 int len
= ReaderReceive(resp
);
257 print_result("RX:", resp
, len
);
259 enum DESFIRE_STATUS status
= resp
[1];
260 if ( status
!= ADDITIONAL_FRAME
) {
272 memset(nonce
, 0, 16);
273 memcpy( encRndB
, resp
+2, 16);
275 // dekryptera tagnonce.
276 AesDecrypt(&ctx
, encRndB
, decRndB
, 16);
280 memcpy(both
, nonce
,16);
281 memcpy(both
+16, decRndB
,16 );
283 AesEncrypt(&ctx
, both
, encBoth
, 32 );
285 uint8_t real_cmd_A
[36];
286 real_cmd_A
[0] = 0x03;
287 real_cmd_A
[1] = ADDITIONAL_FRAME
;
289 memcpy(real_cmd_A
+2, encBoth
, sizeof(encBoth
) );
290 AppendCrc14443a(real_cmd_A
, sizeof(real_cmd_A
));
291 ReaderTransmit(real_cmd_A
, sizeof(real_cmd_A
), NULL
);
293 len
= ReaderReceive(resp
);
295 print_result("Auth1a ", resp
, 36);
298 if ( status
!= OPERATION_OK
) {
299 Dbprintf("Cmd Error: %02x Len: %d", status
,len
);
313 // desfire_cmd = enum DESFIRE_CMD in desfire.h
315 // dataout = point to array for response data.
316 int SendDesfireCommand(enum DESFIRE_CMD desfire_cmd
,uint8_t *dataout
, uint8_t fromscratch
){
323 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
325 // power up the field
326 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
328 iso14443a_select_card(NULL
, NULL
, NULL
);
331 // 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO)
334 real_cmd
[1] = desfire_cmd
;
335 AppendCrc14443a(real_cmd
, 2);
336 ReaderTransmit(real_cmd
, sizeof(real_cmd
), NULL
);
337 len
= ReaderReceive(resp
);
339 return -1; //DATA LINK ERROR
342 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
345 enum DESFIRE_STATUS status
= resp
[1];
346 //1 bytes iso, 1 byte status, in the end: 2 bytes crc
347 if ( status
== OPERATION_OK
|| status
== ADDITIONAL_FRAME
) {
348 memcpy(dataout
, resp
+2, 2);
352 Dbprintf("unexpected desfire response: %X (to %X)", status
, desfire_cmd
);
357 // crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
358 // crc_update(&desfire_crc32, addr, addr_sz);
359 // crc_update(&desfire_crc32, byte, 8);
360 // uint32_t crc = crc_finish(&desfire_crc32);
365 //uint8_t versionCmd1[] = {0x02, 0x60};
366 //uint8_t versionCmd2[] = {0x03, 0xaf};
367 //uint8_t versionCmd3[] = {0x02, 0xaf};
369 // AUTH 1 - CMD: 0x02, 0x0A, 0x00 = Auth
370 // 0x02 = status byte för simpla svar?!?
373 //uint8_t initAuthCmdDES[] = {0x02, 0x0a, 0x00}; // DES
374 //uint8_t initAuthCmd3DES[] = {0x02, 0x1a, 0x00}; // 3DES
375 //uint8_t initAuthCmdAES[] = {0x02, 0xaa, 0x00}; // AES
376 // auth 1 - answer command
377 // 0x03 = status byte för komplexa typer?
378 // 0xaf = additional frame
379 // LEN = 1+1+32+2 = 36
380 //uint8_t answerAuthCmd[34] = {0x03, 0xaf};
383 //AppendCrc14443a(versionCmd1,sizeof(versionCmd1));
387 /*ReaderTransmit(versionCmd1,sizeof(versionCmd1)+2, NULL);
388 len = ReaderReceive(buffer);
389 print_result("Get Version 3", buffer, 9);
392 // for( int i = 0; i < 8; i++){
393 // // Auth 1 - Request authentication
394 // ReaderTransmit(initAuthCmdAES,sizeof(initAuthCmdAES)+2, NULL);
395 // //len = ReaderReceive(buffer);
397 // // 0xAE = authentication error
398 // if (buffer[1] == 0xae) {
399 // Dbprintf("Cmd Error: %02x", buffer[1]);
405 // memcpy(encRndB, buffer+2, 16);
407 // // dekryptera svaret från tag.
408 // AesDecrypt(&ctx, encRndB, decRndB, 16);
411 // memcpy(RndARndB, RndA,16);
412 // memcpy(RndARndB+16, decRndB ,16 );
414 // AesEncrypt(&ctx, RndARndB, encRndARndB, 32 );
416 // memcpy(answerAuthCmd+2, encRndARndB, 32);
417 // AppendCrc14443a(answerAuthCmd,sizeof(answerAuthCmd));
419 // ReaderTransmit(answerAuthCmd,sizeof(answerAuthCmd)+2, NULL);
421 // len = ReaderReceive(buffer);
423 // print_result("Auth1a ", buffer, 8);
424 // Dbprintf("Rx len: %02x", len);
426 // if (buffer[1] == 0xCA) {
427 // Dbprintf("Cmd Error: %02x Len: %d", buffer[1],len);
428 // cmd_send(CMD_ACK,0,0,0,0,0);
430 // AesCtxIni(&ctx, iv, key, KEY128, CBC);
434 //des_dec(decRndB, encRndB, key);
438 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
439 memcpy(RndARndB,RndA,8);
440 memcpy(RndARndB+8,RndB,8);
441 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
442 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
443 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
447 int mifare_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
449 uint8_t* buffer
= mifare_get_bigbufptr();
453 memcpy(dcmd
+1,key
,16);
454 AppendCrc14443a(dcmd
, 17);
457 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
458 int len
= ReaderReceive(buffer
);
460 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
461 len
= ReaderReceive(buffer
);
465 if (MF_DBGLEVEL
>= 1) {
466 Dbprintf("NAK - Authentication failed.");
467 Dbprintf("Cmd Error: %02x", buffer
[0]);
473 if (MF_DBGLEVEL
>= 1) {
474 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
475 buffer
[0],buffer
[1],buffer
[2],buffer
[3],buffer
[4],
476 buffer
[5],buffer
[6],buffer
[7],buffer
[8],buffer
[9],
484 void MifareDES_Auth2(uint32_t arg0
, uint8_t *datain
){
487 uint32_t cuid
= arg0
;
491 byte_t dataoutbuf
[16];
494 memcpy(key
, datain
, 16);
500 if(mifare_des_auth2(cuid
, key
, dataoutbuf
)){
501 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication part2: Fail...");
504 if (MF_DBGLEVEL
>= 2) DbpString("AUTH 2 FINISHED");
507 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,11);
511 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
516 uint8_t* CreateAPDU( uint8_t *datain
, size_t len
){
518 len
= MIN(len
, USB_CMD_DATA_SIZE
);
521 uint8_t *cmd
= tmpcmd
;
526 memcpy(cmd
, datain
,len
);
527 AppendCrc14443a(cmd
, len
+2);
533 uint8_t resp
[RECV_RES_SIZE
];
534 byte_t buf
[RECV_RES_SIZE
];
536 memset(resp
,0,sizeof(resp
));
537 memset(buf
,0,sizeof(buf
));
539 iso14a_clear_trace();
540 iso14a_set_tracing(TRUE
);
541 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
543 // card select - information
544 iso14a_card_select_t
*card
= (iso14a_card_select_t
*)buf
;
545 byte_t isOK
= iso14443a_select_card(NULL
, card
, NULL
);
547 if (MF_DBGLEVEL
>= 1) {
548 Dbprintf("Can't select card");
556 // Deselect card by sending a s-block. the crc is precalced for speed
557 uint8_t cmd
[] = {0xc2,0xe0,0xb4};
558 ReaderTransmit(cmd
, sizeof(cmd
), NULL
);
559 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
564 cmd_send(CMD_ACK
,0,0,0,0,0);
565 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);