]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
976f6dca7651ca54436b45b6b8df11b2a5b01c42
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
) {
58 for (i
= 0; i
< len
; i
++) {
60 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
63 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity(bt
)) & 0x01)<<(7-(i
&0x0007)));
68 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
72 for (i
= 0; i
< 4; i
++)
73 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
79 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
)
81 return mifare_sendcmd_shortex(pcs
, crypted
, cmd
, data
, answer
, answer_parity
, timing
);
84 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
)
93 AppendCrc14443a(dcmd
, 6);
94 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
95 int len
= ReaderReceive(answer
, answer_parity
);
98 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
104 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
)
106 uint8_t dcmd
[4], ecmd
[4];
108 uint8_t par
[1]; // 1 Byte parity is enough here
111 AppendCrc14443a(dcmd
, 2);
113 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
117 for (pos
= 0; pos
< 4; pos
++)
119 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
120 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(dcmd
[pos
])) & 0x01) << (7-pos
));
123 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
126 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
129 int len
= ReaderReceive(answer
, par
);
131 if (answer_parity
) *answer_parity
= par
[0];
133 if (crypted
== CRYPT_ALL
) {
136 for (pos
= 0; pos
< 4; pos
++)
137 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
142 for (pos
= 0; pos
< len
; pos
++)
144 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
152 // mifare classic commands
153 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
155 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
158 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
)
164 uint8_t par
[1] = {0x00};
166 uint32_t nt
, ntpp
; // Supplied tag nonce
168 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
169 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
170 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
172 // Transmit MIFARE_CLASSIC_AUTH
173 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
174 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
175 if (len
!= 4) return 1;
177 // "random" reader nonce:
183 // Save the tag nonce (nt)
184 nt
= bytes_to_num(receivedAnswer
, 4);
186 // ----------------------------- crypto1 create
188 crypto1_destroy(pcs
);
190 // Init cipher with key
191 crypto1_create(pcs
, ui64Key
);
193 if (isNested
== AUTH_NESTED
) {
194 // decrypt nt with help of new key
195 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
197 // Load (plain) uid^nt into the cipher
198 crypto1_word(pcs
, nt
^ uid
, 0);
202 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
203 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
209 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
211 for (pos
= 0; pos
< 4; pos
++)
213 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
214 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nr
[pos
])) & 0x01) << (7-pos
));
217 // Skip 32 bits in pseudo random generator
218 nt
= prng_successor(nt
,32);
221 for (pos
= 4; pos
< 8; pos
++)
223 nt
= prng_successor(nt
,8);
224 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
225 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nt
& 0xff)) & 0x01) << (7-pos
));
228 // Transmit reader nonce and reader answer
229 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
231 // Receive 4 byte tag answer
232 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
235 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
239 memcpy(tmp4
, receivedAnswer
, 4);
240 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
242 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
243 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
250 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
256 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
257 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
259 // command MIFARE_CLASSIC_READBLOCK
260 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
262 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
266 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
270 memcpy(bt
, receivedAnswer
+ 16, 2);
271 AppendCrc14443a(receivedAnswer
, 16);
272 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
273 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
277 memcpy(blockData
, receivedAnswer
, 16);
281 int mifare_ultra_readblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
285 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
286 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
289 // command MIFARE_CLASSIC_READBLOCK
290 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
292 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
293 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
297 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
298 Dbprintf("Cmd Error: card timeout. len: %x", len
);
302 memcpy(bt
, receivedAnswer
+ 16, 2);
303 AppendCrc14443a(receivedAnswer
, 16);
304 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
305 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
306 Dbprintf("Cmd CRC response error.");
310 memcpy(blockData
, receivedAnswer
, 14);
315 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
320 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
323 uint8_t d_block
[18], d_block_enc
[18];
324 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
325 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
327 // command MIFARE_CLASSIC_WRITEBLOCK
328 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
330 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
331 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
335 memcpy(d_block
, blockData
, 16);
336 AppendCrc14443a(d_block
, 16);
339 for (pos
= 0; pos
< 18; pos
++)
341 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
342 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
345 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
347 // Receive the response
348 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
351 for (i
= 0; i
< 4; i
++)
352 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
354 if ((len
!= 1) || (res
!= 0x0A)) {
355 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
362 int mifare_ultra_writeblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
365 uint8_t par
[3] = {0}; // enough for 18 parity bits
366 uint8_t d_block
[18] = {0x00};
367 uint8_t* receivedAnswer
= get_bigbufptr_recvrespbuf();
368 uint8_t* receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
370 // command MIFARE_CLASSIC_WRITEBLOCK
371 len
= mifare_sendcmd_short(NULL
, true, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
373 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
374 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
375 Dbprintf("Cmd Addr Error: %02x", receivedAnswer
[0]);
379 memcpy(d_block
, blockData
, 16);
380 AppendCrc14443a(d_block
, 16);
382 ReaderTransmitPar(d_block
, sizeof(d_block
), par
, NULL
);
384 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
386 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
387 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
388 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer
[0],len
);
394 int mifare_ultra_special_writeblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
397 uint8_t d_block
[8] = {0x00};
398 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
399 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
401 // command MIFARE_CLASSIC_WRITEBLOCK
403 memcpy(d_block
+1,blockData
,4);
404 AppendCrc14443a(d_block
, 6);
406 len
= mifare_sendcmd_short_special(NULL
, 1, 0xA2, d_block
, receivedAnswer
, receivedAnswerPar
, NULL
);
408 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
409 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
410 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
416 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
419 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
420 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
422 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
424 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
425 Dbprintf("halt error. response len: %x", len
);
432 int mifare_ultra_halt(uint32_t uid
)
435 uint8_t *receivedAnswer
= get_bigbufptr_recvrespbuf();
436 uint8_t *receivedAnswerPar
= receivedAnswer
+ MAX_FRAME_SIZE
;
438 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
440 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
441 Dbprintf("halt error. response len: %x", len
);
448 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
449 // plus evtl. 8 sectors with 16 blocks each (4k cards)
450 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
458 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
463 return 32*4 + (sectorNo
- 32) * 16;
468 // work with emulator memory
469 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
470 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
471 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
474 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
475 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
476 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
479 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
480 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
481 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
484 int emlCheckValBl(int blockNum
) {
485 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
486 uint8_t* data
= emCARD
+ blockNum
* 16;
488 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
489 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
490 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
491 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
492 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
493 (data
[12] != (data
[15] ^ 0xff))
499 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
500 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
501 uint8_t* data
= emCARD
+ blockNum
* 16;
503 if (emlCheckValBl(blockNum
)) {
507 memcpy(blReg
, data
, 4);
512 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
513 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
514 uint8_t* data
= emCARD
+ blockNum
* 16;
516 memcpy(data
+ 0, &blReg
, 4);
517 memcpy(data
+ 8, &blReg
, 4);
518 blReg
= blReg
^ 0xffffffff;
519 memcpy(data
+ 4, &blReg
, 4);
522 data
[13] = blBlock
^ 0xff;
524 data
[15] = blBlock
^ 0xff;
529 uint64_t emlGetKey(int sectorNum
, int keyType
) {
531 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
533 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
534 return bytes_to_num(key
, 6);
537 void emlClearMem(void) {
540 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
541 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
542 uint8_t* emCARD
= get_bigbufptr_emlcardmem();
544 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
546 // fill sectors trailer data
547 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
548 emlSetMem((uint8_t *)trailer
, b
, 1);
552 emlSetMem((uint8_t *)uid
, 0, 1);