]>
git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011, 2012
3 // Many authors, whom made it possible
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // Work with mifare cards.
10 //-----------------------------------------------------------------------------
12 #include "proxmark3.h"
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
20 #include "mifareutil.h"
22 int MF_DBGLEVEL
= MF_DBG_ALL
;
25 uint8_t* get_bigbufptr_recvrespbuf(void) {
26 return (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
28 uint8_t* get_bigbufptr_recvcmdbuf(void) {
29 return (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
31 uint8_t* get_bigbufptr_emlcardmem(void) {
32 return (((uint8_t *)BigBuf
) + CARD_MEMORY_OFFSET
);
36 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
41 for (i
= 0; i
< len
; i
++)
42 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
45 for (i
= 0; i
< 4; i
++)
46 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], i
)) << i
;
53 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
57 for (i
= 0; i
< len
; i
++) {
59 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
60 if((i
&0x0007) == 0) par
[i
>>3] = 0;
61 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity(bt
)) & 0x01)<<(7-(i
&0x0007)));
66 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
70 for (i
= 0; i
< 4; i
++)
71 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
77 int mifare_sendcmd_short(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t data
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
)
79 return mifare_sendcmd_shortex(pcs
, crypted
, cmd
, data
, answer
, answer_parity
, timing
);
82 int mifare_sendcmd_short_special(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t* data
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
)
84 uint8_t dcmd
[8];//, ecmd[4];
93 AppendCrc14443a(dcmd
, 6);
94 //Dbprintf("Data command: %02x", dcmd[0]);
95 //Dbprintf("Data R: %02x %02x %02x %02x %02x %02x %02x", dcmd[1],dcmd[2],dcmd[3],dcmd[4],dcmd[5],dcmd[6],dcmd[7]);
97 //memcpy(ecmd, dcmd, sizeof(dcmd));
98 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
99 int len
= ReaderReceive(answer
, answer_parity
);
102 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
108 int mifare_sendcmd_shortex(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
)
110 uint8_t dcmd
[4], ecmd
[4];
112 uint8_t par
[1]; // 1 Byte parity is enough here
115 AppendCrc14443a(dcmd
, 2);
117 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
121 for (pos
= 0; pos
< 4; pos
++)
123 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
124 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(dcmd
[pos
])) & 0x01) << (7-pos
));
127 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
130 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
133 int len
= ReaderReceive(answer
, par
);
135 if (answer_parity
) *answer_parity
= par
[0];
137 if (crypted
== CRYPT_ALL
) {
140 for (pos
= 0; pos
< 4; pos
++)
141 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
146 for (pos
= 0; pos
< len
; pos
++)
148 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
157 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
159 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
162 int mifare_classic_authex(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
, uint32_t *ntptr
, uint32_t *timing
)
168 uint8_t par
[1] = {0};
170 uint32_t nt
, ntpp
; // Supplied tag nonce
172 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
173 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
174 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
176 // Transmit MIFARE_CLASSIC_AUTH
177 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
178 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
179 if (len
!= 4) return 1;
181 // "random" reader nonce:
187 // Save the tag nonce (nt)
188 nt
= bytes_to_num(receivedAnswer
, 4);
190 // ----------------------------- crypto1 create
192 crypto1_destroy(pcs
);
194 // Init cipher with key
195 crypto1_create(pcs
, ui64Key
);
197 if (isNested
== AUTH_NESTED
) {
198 // decrypt nt with help of new key
199 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
201 // Load (plain) uid^nt into the cipher
202 crypto1_word(pcs
, nt
^ uid
, 0);
206 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
207 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
214 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
216 for (pos
= 0; pos
< 4; pos
++)
218 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
219 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nr
[pos
])) & 0x01) << (7-pos
));
222 // Skip 32 bits in pseudo random generator
223 nt
= prng_successor(nt
,32);
226 for (pos
= 4; pos
< 8; pos
++)
228 nt
= prng_successor(nt
,8);
229 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
230 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nt
& 0xff)) & 0x01) << (7-pos
));
233 // Transmit reader nonce and reader answer
234 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
236 // Receive 4 byte tag answer
237 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
240 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
244 memcpy(tmp4
, receivedAnswer
, 4);
245 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
247 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
248 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
255 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
261 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
262 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
264 // command MIFARE_CLASSIC_READBLOCK
265 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
267 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
271 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
275 memcpy(bt
, receivedAnswer
+ 16, 2);
276 AppendCrc14443a(receivedAnswer
, 16);
277 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
278 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
282 memcpy(blockData
, receivedAnswer
, 16);
286 int mifare_ultra_readblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
292 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
293 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
295 // command MIFARE_CLASSIC_READBLOCK
296 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
298 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
302 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
306 memcpy(bt
, receivedAnswer
+ 16, 2);
307 AppendCrc14443a(receivedAnswer
, 16);
308 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
309 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
313 memcpy(blockData
, receivedAnswer
, 14);
318 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
323 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
326 uint8_t d_block
[18], d_block_enc
[18];
327 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
328 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
330 // command MIFARE_CLASSIC_WRITEBLOCK
331 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
333 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
334 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
338 memcpy(d_block
, blockData
, 16);
339 AppendCrc14443a(d_block
, 16);
342 for (pos
= 0; pos
< 18; pos
++)
344 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
345 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
348 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
350 // Receive the response
351 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
354 for (i
= 0; i
< 4; i
++)
355 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
357 if ((len
!= 1) || (res
!= 0x0A)) {
358 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
365 int mifare_ultra_writeblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
369 uint8_t par
[3] = {0}; // enough for 18 parity bits
372 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
373 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
375 // command MIFARE_CLASSIC_WRITEBLOCK
376 len
= mifare_sendcmd_short(NULL
, true, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
378 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
379 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Addr Error: %02x", receivedAnswer
[0]);
383 memset(d_block
,'\0',18);
384 memcpy(d_block
, blockData
, 16);
385 AppendCrc14443a(d_block
, 16);
387 ReaderTransmitPar(d_block
, sizeof(d_block
), par
, NULL
);
389 // Receive the response
390 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
392 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
393 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Data Error: %02x %d", receivedAnswer
[0],len
);
400 int mifare_ultra_special_writeblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
405 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
406 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
408 // command MIFARE_CLASSIC_WRITEBLOCK
409 memset(d_block
,'\0',8);
411 memcpy(d_block
+1,blockData
,4);
412 AppendCrc14443a(d_block
, 6);
414 //i know the data send here is correct
415 len
= mifare_sendcmd_short_special(NULL
, 1, 0xA2, d_block
, receivedAnswer
, receivedAnswerPar
, NULL
);
417 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
418 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
425 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
431 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
432 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
434 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
436 if (MF_DBGLEVEL
>= 1) Dbprintf("halt error. response len: %x", len
);
443 int mifare_ultra_halt(uint32_t uid
)
448 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
449 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
451 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
453 if (MF_DBGLEVEL
>= 1) Dbprintf("halt error. response len: %x", len
);
461 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
462 // plus evtl. 8 sectors with 16 blocks each (4k cards)
463 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
471 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
476 return 32*4 + (sectorNo
- 32) * 16;
481 // work with emulator memory
482 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
483 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
485 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
488 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
489 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
491 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
494 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
495 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
497 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
500 int emlCheckValBl(int blockNum
) {
501 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
502 uint8_t* data
= emCARD
+ blockNum
* 16;
504 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
505 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
506 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
507 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
508 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
509 (data
[12] != (data
[15] ^ 0xff))
515 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
516 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
517 uint8_t* data
= emCARD
+ blockNum
* 16;
519 if (emlCheckValBl(blockNum
)) {
523 memcpy(blReg
, data
, 4);
529 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
530 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
531 uint8_t* data
= emCARD
+ blockNum
* 16;
533 memcpy(data
+ 0, &blReg
, 4);
534 memcpy(data
+ 8, &blReg
, 4);
535 blReg
= blReg
^ 0xffffffff;
536 memcpy(data
+ 4, &blReg
, 4);
539 data
[13] = blBlock
^ 0xff;
541 data
[15] = blBlock
^ 0xff;
546 uint64_t emlGetKey(int sectorNum
, int keyType
) {
548 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
550 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
551 return bytes_to_num(key
, 6);
554 void emlClearMem(void) {
557 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
558 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
559 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
561 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
563 // fill sectors trailer data
564 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
565 emlSetMem((uint8_t *)trailer
, b
, 1);
569 emlSetMem((uint8_t *)uid
, 0, 1);