1 #include "mifaredesfire.h"
3 #define MAX_APPLICATION_COUNT 28
4 #define MAX_FILE_COUNT 16
5 #define MAX_DESFIRE_FRAME_SIZE 60
6 #define NOT_YET_AUTHENTICATED 255
7 #define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5)
8 #define RECEIVE_SIZE 64
10 // the block number for the ISO14443-4 PCB
11 uint8_t pcb_blocknum
= 0;
12 // Deselect card by sending a s-block. the crc is precalced for speed
13 static uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4};
15 //static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
16 /* PCB CID CMD PAYLOAD */
17 //static uint8_t __res[MAX_FRAME_SIZE];
19 bool InitDesfireCard(){
21 // Make sure it is off.
22 // FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
25 byte_t cardbuf
[USB_CMD_DATA_SIZE
];
26 memset(cardbuf
,0,sizeof(cardbuf
));
28 iso14a_set_tracing(TRUE
);
29 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
31 iso14a_card_select_t
*card
= (iso14a_card_select_t
*)cardbuf
;
32 int len
= iso14443a_select_card(NULL
,card
,NULL
);
35 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
51 void MifareSendCommand(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
){
53 /* ARG0 contains flags.
59 size_t datalen
= arg1
;
60 uint8_t resp
[RECEIVE_SIZE
];
61 memset(resp
,0,sizeof(resp
));
63 if (MF_DBGLEVEL
>= 4) {
64 Dbprintf(" flags : %02X", flags
);
65 Dbprintf(" len : %02X", datalen
);
66 print_result(" RX : ", datain
, datalen
);
69 if ( flags
& CLEARTRACE
){
74 if ( !InitDesfireCard() )
78 int len
= DesfireAPDU(datain
, datalen
, resp
);
79 if (MF_DBGLEVEL
>= 4) {
80 print_result("ERR <--: ", resp
, len
);
88 // reset the pcb_blocknum,
91 if ( flags
& DISCONNECT
){
95 cmd_send(CMD_ACK
,1,len
,0,resp
,len
);
98 void MifareDesfireGetInformation(){
101 uint8_t resp
[USB_CMD_DATA_SIZE
];
102 uint8_t dataout
[USB_CMD_DATA_SIZE
];
103 byte_t cardbuf
[USB_CMD_DATA_SIZE
];
105 memset(resp
,0,sizeof(resp
));
106 memset(dataout
,0, sizeof(dataout
));
107 memset(cardbuf
,0,sizeof(cardbuf
));
112 3 = desfire command 3
115 PCB == 0x0A because sending CID byte.
116 CID == 0x00 first card?
118 iso14a_clear_trace();
119 iso14a_set_tracing(TRUE
);
120 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
122 // card select - information
123 iso14a_card_select_t
*card
= (iso14a_card_select_t
*)cardbuf
;
124 byte_t isOK
= iso14443a_select_card(NULL
, card
, NULL
);
126 if (MF_DBGLEVEL
>= 1) {
127 Dbprintf("Can't select card");
133 memcpy(dataout
,card
->uid
,7);
139 uint8_t cmd
[] = {GET_VERSION
};
140 size_t cmd_len
= sizeof(cmd
);
142 len
= DesfireAPDU(cmd
, cmd_len
, resp
);
144 print_result("ERROR <--: ", resp
, len
);
151 memcpy(dataout
+7,resp
+3,7);
154 cmd
[0] = ADDITIONAL_FRAME
;
155 len
= DesfireAPDU(cmd
, cmd_len
, resp
);
157 print_result("ERROR <--: ", resp
, len
);
164 memcpy(dataout
+7+7,resp
+3,7);
167 len
= DesfireAPDU(cmd
, cmd_len
, resp
);
169 print_result("ERROR <--: ", resp
, len
);
174 memcpy(dataout
+7+7+7,resp
+3,14);
176 cmd_send(CMD_ACK
,1,0,0,dataout
,sizeof(dataout
));
178 // reset the pcb_blocknum,
183 void MifareDES_Auth1(uint8_t mode
, uint8_t algo
, uint8_t keyno
, uint8_t *datain
){
186 //uint8_t PICC_MASTER_KEY8[8] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47};
187 uint8_t PICC_MASTER_KEY16
[16] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f };
188 //uint8_t null_key_data8[8] = {0x00};
189 //uint8_t null_key_data16[16] = {0x00};
190 //uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
191 //uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
193 //uint8_t* bigbuffer = get_bigbufptr_recvrespbuf();
194 uint8_t resp
[256] = {0x00};
195 uint8_t IV
[16] = {0x00};
197 size_t datalen
= datain
[0];
199 uint8_t cmd
[40] = {0x00};
200 uint8_t encRndB
[16] = {0x00};
201 uint8_t decRndB
[16] = {0x00};
202 uint8_t nonce
[16] = {0x00};
203 uint8_t both
[32] = {0x00};
204 uint8_t encBoth
[32] = {0x00};
212 // 3 olika sätt att authenticera. AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
213 // 4 olika crypto algo DES, 3DES, 3K3DES, AES
214 // 3 olika kommunikations sätt, PLAIN,MAC,CRYPTO
219 // if ( SendDesfireCommand(AUTHENTICATE, &keyno, resp) > 0 ){
220 // // fick nonce från kortet
224 //SendDesfireCommand(AUTHENTICATE_ISO, &keyno, resp);
229 uint8_t keybytes
[16];
230 if (datain
[1] == 0xff){
231 memcpy(keybytes
,PICC_MASTER_KEY16
,16);
233 memcpy(keybytes
, datain
+1, datalen
);
236 struct desfire_key defaultkey
= {0};
237 desfirekey_t key
= &defaultkey
;
238 Desfire_aes_key_new( keybytes
, key
);
241 if ( AesCtxIni(&ctx
, IV
, key
->data
, KEY128
, CBC
) < 0 ){
242 if( MF_DBGLEVEL
>= 4) {
243 Dbprintf("AES context failed to init");
249 cmd
[0] = AUTHENTICATE_AES
;
250 cmd
[1] = 0x00; //keynumber
251 len
= DesfireAPDU(cmd
, 2, resp
);
253 if (MF_DBGLEVEL
>= 1) {
254 DbpString("Authentication failed. Card timeout.");
260 memcpy( encRndB
, resp
+3, 16);
262 // dekryptera tagnonce.
263 AesDecrypt(&ctx
, encRndB
, decRndB
, 16);
265 memcpy(both
, nonce
,16);
266 memcpy(both
+16, decRndB
,16 );
267 AesEncrypt(&ctx
, both
, encBoth
, 32 );
269 cmd
[0] = ADDITIONAL_FRAME
;
270 memcpy(cmd
+1, encBoth
, 32 );
272 len
= DesfireAPDU(cmd
, 33, resp
); // 1 + 32 == 33
274 if (MF_DBGLEVEL
>= 1) {
275 DbpString("Authentication failed. Card timeout.");
281 if ( resp
[2] == 0x00 ){
282 // Create AES Session key
283 struct desfire_key sessionKey
= {0};
284 desfirekey_t skey
= &sessionKey
;
285 Desfire_session_key_new( nonce
, decRndB
, key
, skey
);
286 print_result("SESSION : ", skey
->data
, 16);
288 DbpString("Authetication failed.");
298 cmd_send(CMD_ACK
,1,len
,0,resp
,len
);
301 // 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO)
302 // cmd = cmd bytes to send
303 // cmd_len = length of cmd
304 // dataout = pointer to response data array
305 int DesfireAPDU(uint8_t *cmd
, size_t cmd_len
, uint8_t *dataout
){
308 size_t wrappedLen
= 0;
309 uint8_t wCmd
[USB_CMD_DATA_SIZE
] = {0};
311 uint8_t *resp
= ((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
;
312 uint8_t *resp_par
= ((uint8_t *)BigBuf
) + RECV_RESP_PAR_OFFSET
;
314 wrappedLen
= CreateAPDU( cmd
, cmd_len
, wCmd
);
316 if (MF_DBGLEVEL
>= 4) {
317 print_result("WCMD <--: ", wCmd
, wrappedLen
);
319 ReaderTransmit( wCmd
, wrappedLen
, NULL
);
321 len
= ReaderReceive(resp
, resp_par
);
324 if (MF_DBGLEVEL
>= 4) {
327 return FALSE
; //DATA LINK ERROR
329 // if we received an I- or R(ACK)-Block with a block number equal to the
330 // current block number, toggle the current block number
331 else if (len
>= 4 // PCB+CID+CRC = 4 bytes
332 && ((resp
[0] & 0xC0) == 0 // I-Block
333 || (resp
[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
334 && (resp
[0] & 0x01) == pcb_blocknum
) // equal block numbers
336 pcb_blocknum
^= 1; //toggle next block
339 memcpy(dataout
, resp
, len
);
344 size_t CreateAPDU( uint8_t *datain
, size_t len
, uint8_t *dataout
){
346 size_t cmdlen
= MIN(len
+4, USB_CMD_DATA_SIZE
-1);
349 memset(cmd
, 0, cmdlen
);
351 cmd
[0] = 0x0A; // 0x0A = skicka cid, 0x02 = ingen cid. Särskilda bitar //
352 cmd
[0] |= pcb_blocknum
; // OR the block number into the PCB
353 cmd
[1] = 0x00; // CID: 0x00 //FIXME: allow multiple selected cards
355 memcpy(cmd
+2, datain
, len
);
356 AppendCrc14443a(cmd
, len
+2);
358 memcpy(dataout
, cmd
, cmdlen
);
363 // crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
364 // crc_update(&desfire_crc32, addr, addr_sz);
365 // crc_update(&desfire_crc32, byte, 8);
366 // uint32_t crc = crc_finish(&desfire_crc32);
370 ReaderTransmit(deselect_cmd
, 3 , NULL
);
371 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
377 ReaderTransmit(deselect_cmd
, 3 , NULL
);
378 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
379 cmd_send(CMD_ACK
,0,0,0,0,0);