1 #include "mifaredesfire.h"
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
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};
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];
20 bool InitDesfireCard(){
22 iso14a_card_select_t card
;
24 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
27 if (!iso14443a_select_card(NULL
, &card
, NULL
, true, 0)) {
28 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) DbpString("Can't select card");
44 void MifareSendCommand(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
){
46 /* ARG0 contains flags.
52 size_t datalen
= arg1
;
53 uint8_t resp
[RECEIVE_SIZE
];
54 memset(resp
,0,sizeof(resp
));
56 if (MF_DBGLEVEL
>= 4) {
57 Dbprintf(" flags : %02X", flags
);
58 Dbprintf(" len : %02X", datalen
);
59 print_result(" RX : ", datain
, datalen
);
62 if ( flags
& CLEARTRACE
)
66 if ( !InitDesfireCard() )
70 int len
= DesfireAPDU(datain
, datalen
, resp
);
72 print_result("ERR <--: ", resp
, len
);
79 // reset the pcb_blocknum,
82 if ( flags
& DISCONNECT
)
85 cmd_send(CMD_ACK
,1,len
,0,resp
,len
);
88 void MifareDesfireGetInformation(){
91 iso14a_card_select_t card
;
92 uint8_t resp
[USB_CMD_DATA_SIZE
] = {0x00};
93 uint8_t dataout
[USB_CMD_DATA_SIZE
] = {0x00};
101 PCB == 0x0A because sending CID byte.
102 CID == 0x00 first card?
106 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
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");
115 memcpy(dataout
, card
.uid
, 7);
121 uint8_t cmd
[] = {GET_VERSION
};
122 size_t cmd_len
= sizeof(cmd
);
124 len
= DesfireAPDU(cmd
, cmd_len
, resp
);
126 print_result("ERROR <--: ", resp
, len
);
133 memcpy(dataout
+7,resp
+3,7);
136 cmd
[0] = ADDITIONAL_FRAME
;
137 len
= DesfireAPDU(cmd
, cmd_len
, resp
);
139 print_result("ERROR <--: ", resp
, len
);
146 memcpy(dataout
+7+7,resp
+3,7);
149 len
= DesfireAPDU(cmd
, cmd_len
, resp
);
151 print_result("ERROR <--: ", resp
, len
);
156 memcpy(dataout
+7+7+7,resp
+3,14);
158 cmd_send(CMD_ACK
,1,0,0,dataout
,sizeof(dataout
));
160 // reset the pcb_blocknum,
165 void MifareDES_Auth1(uint8_t mode
, uint8_t algo
, uint8_t keyno
, uint8_t *datain
){
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};
175 uint8_t resp
[256] = {0x00};
176 uint8_t IV
[16] = {0x00};
178 size_t datalen
= datain
[0];
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};
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
200 uint8_t keybytes
[16];
201 uint8_t RndA
[8] = {0x00};
202 uint8_t RndB
[8] = {0x00};
205 if (datain
[1] == 0xff){
206 memcpy(keybytes
,PICC_MASTER_KEY16
,16);
208 memcpy(keybytes
, datain
+1, datalen
);
212 if (datain
[1] == 0xff){
213 memcpy(keybytes
,null_key_data8
,8);
215 memcpy(keybytes
, datain
+1, datalen
);
220 struct desfire_key defaultkey
= {0};
221 desfirekey_t key
= &defaultkey
;
224 Desfire_3des_key_new_with_version(keybytes
, key
);
226 Desfire_des_key_new(keybytes
, key
);
228 cmd
[0] = AUTHENTICATE
;
229 cmd
[1] = keyno
; //keynumber
230 len
= DesfireAPDU(cmd
, 2, resp
);
232 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) {
233 DbpString("Authentication failed. Card timeout.");
239 if ( resp
[2] == 0xaf ){
241 DbpString("Authetication failed. Invalid key number.");
246 memcpy( encRndB
, resp
+3, 8);
248 tdes_dec(&decRndB
, &encRndB
, key
->data
);
250 des_dec(&decRndB
, &encRndB
, key
->data
);
252 memcpy(RndB
, decRndB
, 8);
255 // This should be random
256 uint8_t decRndA
[8] = {0x00};
257 memcpy(RndA
, decRndA
, 8);
258 uint8_t encRndA
[8] = {0x00};
261 tdes_dec(&encRndA
, &decRndA
, key
->data
);
263 des_dec(&encRndA
, &decRndA
, key
->data
);
265 memcpy(both
, encRndA
, 8);
267 for (int x
= 0; x
< 8; x
++) {
268 decRndB
[x
] = decRndB
[x
] ^ encRndA
[x
];
273 tdes_dec(&encRndB
, &decRndB
, key
->data
);
275 des_dec(&encRndB
, &decRndB
, key
->data
);
277 memcpy(both
+ 8, encRndB
, 8);
279 cmd
[0] = ADDITIONAL_FRAME
;
280 memcpy(cmd
+1, both
, 16 );
282 len
= DesfireAPDU(cmd
, 17, resp
);
284 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) {
285 DbpString("Authentication failed. Card timeout.");
291 if ( resp
[2] == 0x00 ){
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);
298 memcpy(encRndA
, resp
+3, 8);
301 tdes_dec(&encRndA
, &encRndA
, key
->data
);
303 des_dec(&encRndA
, &encRndA
, key
->data
);
306 for (int x
= 0; x
< 8; x
++) {
307 if (decRndA
[x
] != encRndA
[x
]) {
308 DbpString("Authetication failed. Cannot varify PICC.");
314 //Change the selected key to a new value.
317 // Current key is a 3DES key, change it to a DES key
322 uint8_t newKey[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
324 uint8_t first, second;
325 uint8_t buff1[8] = {0x00};
326 uint8_t buff2[8] = {0x00};
327 uint8_t buff3[8] = {0x00};
329 memcpy(buff1,newKey, 8);
330 memcpy(buff2,newKey + 8, 8);
332 ComputeCrc14443(CRC_14443_A, newKey, 16, &first, &second);
333 memcpy(buff3, &first, 1);
334 memcpy(buff3 + 1, &second, 1);
336 tdes_dec(&buff1, &buff1, skey->data);
337 memcpy(cmd+2,buff1,8);
339 for (int x = 0; x < 8; x++) {
340 buff2[x] = buff2[x] ^ buff1[x];
342 tdes_dec(&buff2, &buff2, skey->data);
343 memcpy(cmd+10,buff2,8);
345 for (int x = 0; x < 8; x++) {
346 buff3[x] = buff3[x] ^ buff2[x];
348 tdes_dec(&buff3, &buff3, skey->data);
349 memcpy(cmd+18,buff3,8);
351 // The command always times out on the first attempt, this will retry until a response
355 len = DesfireAPDU(cmd,26,resp);
359 // Current key is a DES key, change it to a 3DES key
364 uint8_t newKey[16] = {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f};
366 uint8_t first, second;
367 uint8_t buff1[8] = {0x00};
368 uint8_t buff2[8] = {0x00};
369 uint8_t buff3[8] = {0x00};
371 memcpy(buff1,newKey, 8);
372 memcpy(buff2,newKey + 8, 8);
374 ComputeCrc14443(CRC_14443_A, newKey, 16, &first, &second);
375 memcpy(buff3, &first, 1);
376 memcpy(buff3 + 1, &second, 1);
378 des_dec(&buff1, &buff1, skey->data);
379 memcpy(cmd+2,buff1,8);
381 for (int x = 0; x < 8; x++) {
382 buff2[x] = buff2[x] ^ buff1[x];
384 des_dec(&buff2, &buff2, skey->data);
385 memcpy(cmd+10,buff2,8);
387 for (int x = 0; x < 8; x++) {
388 buff3[x] = buff3[x] ^ buff2[x];
390 des_dec(&buff3, &buff3, skey->data);
391 memcpy(cmd+18,buff3,8);
393 // The command always times out on the first attempt, this will retry until a response
397 len = DesfireAPDU(cmd,26,resp);
405 cmd_send(CMD_ACK
,1,0,0,skey
->data
,16);
407 cmd_send(CMD_ACK
,1,0,0,skey
->data
,8);
409 DbpString("Authetication failed.");
416 //SendDesfireCommand(AUTHENTICATE_ISO, &keyno, resp);
421 uint8_t keybytes
[16] = {0x00};
422 if (datain
[1] == 0xff){
423 memcpy(keybytes
,PICC_MASTER_KEY16
,16);
425 memcpy(keybytes
, datain
+1, datalen
);
428 struct desfire_key defaultkey
= {0x00};
429 desfirekey_t key
= &defaultkey
;
430 Desfire_aes_key_new( keybytes
, key
);
433 if ( AesCtxIni(&ctx
, IV
, key
->data
, KEY128
, CBC
) < 0 ){
434 if( MF_DBGLEVEL
>= 4) {
435 Dbprintf("AES context failed to init");
441 cmd
[0] = AUTHENTICATE_AES
;
442 cmd
[1] = 0x00; //keynumber
443 len
= DesfireAPDU(cmd
, 2, resp
);
445 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) {
446 DbpString("Authentication failed. Card timeout.");
452 memcpy( encRndB
, resp
+3, 16);
454 // dekryptera tagnonce.
455 AesDecrypt(&ctx
, encRndB
, decRndB
, 16);
457 memcpy(both
, nonce
,16);
458 memcpy(both
+16, decRndB
,16 );
459 AesEncrypt(&ctx
, both
, encBoth
, 32 );
461 cmd
[0] = ADDITIONAL_FRAME
;
462 memcpy(cmd
+1, encBoth
, 32 );
464 len
= DesfireAPDU(cmd
, 33, resp
); // 1 + 32 == 33
466 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) {
467 DbpString("Authentication failed. Card timeout.");
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);
480 DbpString("Authetication failed.");
490 cmd_send(CMD_ACK
,1,len
,0,resp
,len
);
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
){
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
];
505 wrappedLen
= CreateAPDU( cmd
, cmd_len
, wCmd
);
507 if (MF_DBGLEVEL
>= 4)
508 print_result("WCMD <--: ", wCmd
, wrappedLen
);
510 ReaderTransmit( wCmd
, wrappedLen
, NULL
);
512 len
= ReaderReceive(resp
, par
);
514 if (MF_DBGLEVEL
>= 4) Dbprintf("fukked");
515 return FALSE
; //DATA LINK ERROR
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
524 pcb_blocknum
^= 1; //toggle next block
527 memcpy(dataout
, resp
, len
);
532 size_t CreateAPDU( uint8_t *datain
, size_t len
, uint8_t *dataout
){
534 size_t cmdlen
= MIN(len
+4, USB_CMD_DATA_SIZE
-1);
537 memset(cmd
, 0, cmdlen
);
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
543 memcpy(cmd
+2, datain
, len
);
544 AppendCrc14443a(cmd
, len
+2);
546 memcpy(dataout
, cmd
, cmdlen
);
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);
558 ReaderTransmit(deselect_cmd
, 3 , NULL
);
560 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
565 void OnError(uint8_t reason
){
566 cmd_send(CMD_ACK
,0,reason
,0,0,0);