1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
9 //-----------------------------------------------------------------------------
10 // Mifare Classic Card Simulation
11 //-----------------------------------------------------------------------------
13 #include "mifaresim.h"
14 #include "iso14443a.h"
15 #include "iso14443crc.h"
16 #include "crapto1/crapto1.h"
19 #include "mifareutil.h"
20 #include "fpgaloader.h"
21 #include "proxmark3.h"
24 #include "protocols.h"
27 //mifare emulator states
28 #define MFEMUL_NOFIELD 0
30 #define MFEMUL_SELECT1 2
31 #define MFEMUL_SELECT2 3
32 #define MFEMUL_SELECT3 4
33 #define MFEMUL_AUTH1 5
34 #define MFEMUL_AUTH2 6
36 #define MFEMUL_WRITEBL2 8
37 #define MFEMUL_INTREG_INC 9
38 #define MFEMUL_INTREG_DEC 10
39 #define MFEMUL_INTREG_REST 11
40 #define MFEMUL_HALTED 12
42 #define cardSTATE_TO_IDLE() { cardSTATE = MFEMUL_IDLE; LED_B_OFF(); LED_C_OFF(); }
46 static void MifareSimInit(uint8_t flags
, uint8_t *datain
, tag_response_info_t
**responses
, uint32_t *cuid
, uint8_t *uid_len
) {
48 #define TAG_RESPONSE_COUNT 5 // number of precompiled responses
49 static uint8_t rATQA
[] = {0x04, 0x00}; // indicate Mifare classic 1k 4Byte UID
50 static uint8_t rUIDBCC1
[] = {0x00, 0x00, 0x00, 0x00, 0x00}; // UID 1st cascade level
51 static uint8_t rUIDBCC2
[] = {0x00, 0x00, 0x00, 0x00, 0x00}; // UID 2nd cascade level
52 static uint8_t rSAKfinal
[]= {0x08, 0xb6, 0xdd}; // mifare 1k indicated
53 static uint8_t rSAK1
[] = {0x04, 0xda, 0x17}; // indicate UID not finished
56 // UID can be set from emulator memory or incoming data and can be 4 or 7 bytes long
57 if (flags
& FLAG_4B_UID_IN_DATA
) { // get UID from datain
58 memcpy(rUIDBCC1
, datain
, 4);
59 } else if (flags
& FLAG_7B_UID_IN_DATA
) {
61 memcpy(rUIDBCC1
+1, datain
, 3);
62 memcpy(rUIDBCC2
, datain
+3, 4);
65 uint8_t probable_atqa
;
66 emlGetMemBt(&probable_atqa
, 7, 1); // get UID from emul memory - weak guess at length
67 if (probable_atqa
== 0x00) { // ---------- 4BUID
68 emlGetMemBt(rUIDBCC1
, 0, 4);
69 } else { // ---------- 7BUID
71 emlGetMemBt(rUIDBCC1
+1, 0, 3);
72 emlGetMemBt(rUIDBCC2
, 3, 4);
79 *cuid
= bytes_to_num(rUIDBCC1
, 4);
80 rUIDBCC1
[4] = rUIDBCC1
[0] ^ rUIDBCC1
[1] ^ rUIDBCC1
[2] ^ rUIDBCC1
[3];
81 if (MF_DBGLEVEL
>= 2) {
82 Dbprintf("4B UID: %02x%02x%02x%02x",
83 rUIDBCC1
[0], rUIDBCC1
[1], rUIDBCC1
[2], rUIDBCC1
[3] );
88 *cuid
= bytes_to_num(rUIDBCC2
, 4);
89 rUIDBCC1
[4] = rUIDBCC1
[0] ^ rUIDBCC1
[1] ^ rUIDBCC1
[2] ^ rUIDBCC1
[3];
90 rUIDBCC2
[4] = rUIDBCC2
[0] ^ rUIDBCC2
[1] ^ rUIDBCC2
[2] ^ rUIDBCC2
[3];
91 if (MF_DBGLEVEL
>= 2) {
92 Dbprintf("7B UID: %02x %02x %02x %02x %02x %02x %02x",
93 rUIDBCC1
[1], rUIDBCC1
[2], rUIDBCC1
[3], rUIDBCC2
[0], rUIDBCC2
[1], rUIDBCC2
[2], rUIDBCC2
[3] );
100 static tag_response_info_t responses_init
[TAG_RESPONSE_COUNT
] = {
101 { .response
= rATQA
, .response_n
= sizeof(rATQA
) }, // Answer to request - respond with card type
102 { .response
= rUIDBCC1
, .response_n
= sizeof(rUIDBCC1
) }, // Anticollision cascade1 - respond with first part of uid
103 { .response
= rUIDBCC2
, .response_n
= sizeof(rUIDBCC2
) }, // Anticollision cascade2 - respond with 2nd part of uid
104 { .response
= rSAKfinal
, .response_n
= sizeof(rSAKfinal
) }, // Acknowledge select - last cascade
105 { .response
= rSAK1
, .response_n
= sizeof(rSAK1
) } // Acknowledge select - previous cascades
108 // Prepare ("precompile") the responses of the anticollision phase. There will be not enough time to do this at the moment the reader sends its REQA or SELECT
109 // There are 7 predefined responses with a total of 18 bytes data to transmit. Coded responses need one byte per bit to transfer (data, parity, start, stop, correction)
110 // 18 * 8 data bits, 18 * 1 parity bits, 5 start bits, 5 stop bits, 5 correction bits -> need 177 bytes buffer
111 #define ALLOCATED_TAG_MODULATION_BUFFER_SIZE 177 // number of bytes required for precompiled responses
113 uint8_t *free_buffer_pointer
= BigBuf_malloc(ALLOCATED_TAG_MODULATION_BUFFER_SIZE
);
114 size_t free_buffer_size
= ALLOCATED_TAG_MODULATION_BUFFER_SIZE
;
115 for (size_t i
= 0; i
< TAG_RESPONSE_COUNT
; i
++) {
116 prepare_allocated_tag_modulation(&responses_init
[i
], &free_buffer_pointer
, &free_buffer_size
);
119 *responses
= responses_init
;
121 // indices into responses array:
131 static bool HasValidCRC(uint8_t *receivedCmd
, uint16_t receivedCmd_len
) {
132 uint8_t CRC_byte_1
, CRC_byte_2
;
133 ComputeCrc14443(CRC_14443_A
, receivedCmd
, receivedCmd_len
-2, &CRC_byte_1
, &CRC_byte_2
);
134 return (receivedCmd
[receivedCmd_len
-2] == CRC_byte_1
&& receivedCmd
[receivedCmd_len
-1] == CRC_byte_2
);
142 * FLAG_INTERACTIVE - In interactive mode, we are expected to finish the operation with an ACK
143 * FLAG_4B_UID_IN_DATA - means that there is a 4-byte UID in the data-section, we're expected to use that
144 * FLAG_7B_UID_IN_DATA - means that there is a 7-byte UID in the data-section, we're expected to use that
145 * FLAG_10B_UID_IN_DATA - use 10-byte UID in the data-section not finished
146 * FLAG_NR_AR_ATTACK - means we should collect NR_AR responses for bruteforcing later
147 * FLAG_RANDOM_NONCE - means we should generate some pseudo-random nonce data (only allows moebius attack)
148 *@param exitAfterNReads, exit simulation after n blocks have been read, 0 is infinite ...
149 * (unless reader attack mode enabled then it runs util it gets enough nonces to recover all keys attmpted)
151 void Mifare1ksim(uint8_t flags
, uint8_t exitAfterNReads
, uint8_t arg2
, uint8_t *datain
)
153 tag_response_info_t
*responses
;
156 uint8_t cardWRBL
= 0;
157 uint8_t cardAUTHSC
= 0;
158 uint8_t cardAUTHKEY
= 0xff; // no authentication
160 //uint32_t rn_enc = 0;
162 uint32_t cardINTREG
= 0;
163 uint8_t cardINTBLOCK
= 0;
164 struct Crypto1State mpcs
= {0, 0};
165 struct Crypto1State
*pcs
;
167 uint32_t numReads
= 0;//Counts numer of times reader reads a block
168 uint8_t receivedCmd
[MAX_MIFARE_FRAME_SIZE
];
169 uint8_t receivedCmd_dec
[MAX_MIFARE_FRAME_SIZE
];
170 uint8_t receivedCmd_par
[MAX_MIFARE_PARITY_SIZE
];
171 uint16_t receivedCmd_len
;
172 uint8_t response
[MAX_MIFARE_FRAME_SIZE
];
173 uint8_t response_par
[MAX_MIFARE_PARITY_SIZE
];
175 uint8_t rAUTH_NT
[] = {0x01, 0x02, 0x03, 0x04};
176 uint8_t rAUTH_AT
[] = {0x00, 0x00, 0x00, 0x00};
178 //Here, we collect UID,sector,keytype,NT,AR,NR,NT2,AR2,NR2
179 // This will be used in the reader-only attack.
181 //allow collecting up to 7 sets of nonces to allow recovery of up to 7 keys
182 #define ATTACK_KEY_COUNT 7 // keep same as define in cmdhfmf.c -> readerAttack() (Cannot be more than 7)
183 nonces_t ar_nr_resp
[ATTACK_KEY_COUNT
*2]; //*2 for 2 separate attack types (nml, moebius) 36 * 7 * 2 bytes = 504 bytes
184 memset(ar_nr_resp
, 0x00, sizeof(ar_nr_resp
));
186 uint8_t ar_nr_collected
[ATTACK_KEY_COUNT
*2]; //*2 for 2nd attack type (moebius)
187 memset(ar_nr_collected
, 0x00, sizeof(ar_nr_collected
));
188 uint8_t nonce1_count
= 0;
189 uint8_t nonce2_count
= 0;
190 uint8_t moebius_n_count
= 0;
191 bool gettingMoebius
= false;
192 uint8_t mM
= 0; //moebius_modifier for collection storage
194 // Authenticate response - nonce
196 if (flags
& FLAG_RANDOM_NONCE
) {
199 nonce
= bytes_to_num(rAUTH_NT
, 4);
202 // free eventually allocated BigBuf memory but keep Emulator Memory
203 BigBuf_free_keep_EM();
205 MifareSimInit(flags
, datain
, &responses
, &cuid
, &uid_len
);
207 // We need to listen to the high-frequency, peak-detected path.
208 iso14443a_setup(FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
215 bool finished
= false;
216 bool button_pushed
= BUTTON_PRESS();
217 int cardSTATE
= MFEMUL_NOFIELD
;
219 while (!button_pushed
&& !finished
&& !usb_poll_validate_length()) {
223 if (cardSTATE
== MFEMUL_NOFIELD
) {
224 int vHf
= (MAX_ADC_HF_VOLTAGE
* AvgAdc(ADC_CHAN_HF
)) >> 10;
225 if (vHf
> MF_MINFIELDV
) {
229 button_pushed
= BUTTON_PRESS();
234 int res
= EmGetCmd(receivedCmd
, &receivedCmd_len
, receivedCmd_par
);
236 if (res
== 2) { //Field is off!
238 cardSTATE
= MFEMUL_NOFIELD
;
240 } else if (res
== 1) { // button pressed
241 button_pushed
= true;
245 // WUPA in HALTED state or REQA or WUPA in any other state
246 if (receivedCmd_len
== 1 && ((receivedCmd
[0] == ISO14443A_CMD_REQA
&& cardSTATE
!= MFEMUL_HALTED
) || receivedCmd
[0] == ISO14443A_CMD_WUPA
)) {
247 EmSendPrecompiledCmd(&responses
[ATQA
], (receivedCmd
[0] == ISO14443A_CMD_WUPA
));
250 crypto1_destroy(pcs
);
252 if (flags
& FLAG_RANDOM_NONCE
) {
257 cardSTATE
= MFEMUL_SELECT1
;
267 case MFEMUL_SELECT1
:{
268 // select all - 0x93 0x20
269 if (receivedCmd_len
== 2 && (receivedCmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT
&& receivedCmd
[1] == 0x20)) {
270 if (MF_DBGLEVEL
>= 4) Dbprintf("SELECT ALL CL1 received");
271 EmSendPrecompiledCmd(&responses
[UIDBCC1
], false);
274 // select card - 0x93 0x70 ...
275 if (receivedCmd_len
== 9 &&
276 (receivedCmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT
&& receivedCmd
[1] == 0x70 && memcmp(&receivedCmd
[2], responses
[UIDBCC1
].response
, 4) == 0)) {
277 if (MF_DBGLEVEL
>= 4) Dbprintf("SELECT CL1 %02x%02x%02x%02x received",receivedCmd
[2],receivedCmd
[3],receivedCmd
[4],receivedCmd
[5]);
279 EmSendPrecompiledCmd(&responses
[SAKfinal
], false);
281 cardSTATE
= MFEMUL_WORK
;
283 } else if (uid_len
== 7) {
284 EmSendPrecompiledCmd(&responses
[SAK1
], false);
285 cardSTATE
= MFEMUL_SELECT2
;
292 case MFEMUL_SELECT2
:{
293 // select all cl2 - 0x95 0x20
294 if (receivedCmd_len
== 2 && (receivedCmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2
&& receivedCmd
[1] == 0x20)) {
295 if (MF_DBGLEVEL
>= 4) Dbprintf("SELECT ALL CL2 received");
296 EmSendPrecompiledCmd(&responses
[UIDBCC2
], false);
299 // select cl2 card - 0x95 0x70 xxxxxxxxxxxx
300 if (receivedCmd_len
== 9 &&
301 (receivedCmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2
&& receivedCmd
[1] == 0x70 && memcmp(&receivedCmd
[2], responses
[UIDBCC2
].response
, 4) == 0)) {
303 if (MF_DBGLEVEL
>= 4) Dbprintf("SELECT CL2 %02x%02x%02x%02x received",receivedCmd
[2],receivedCmd
[3],receivedCmd
[4],receivedCmd
[5]);
304 EmSendPrecompiledCmd(&responses
[SAKfinal
], false);
306 cardSTATE
= MFEMUL_WORK
;
314 if (receivedCmd_len
!= 4) { // all commands must have exactly 4 bytes
317 bool encrypted_data
= (cardAUTHKEY
!= 0xFF) ;
318 if (encrypted_data
) {
320 mf_crypto1_decryptEx(pcs
, receivedCmd
, receivedCmd_len
, receivedCmd_dec
);
322 memcpy(receivedCmd_dec
, receivedCmd
, receivedCmd_len
);
324 if (!HasValidCRC(receivedCmd_dec
, receivedCmd_len
)) { // all commands must have a valid CRC
325 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
328 if (receivedCmd_dec
[0] == MIFARE_AUTH_KEYA
|| receivedCmd_dec
[0] == MIFARE_AUTH_KEYB
) {
329 // if authenticating to a block that shouldn't exist - as long as we are not doing the reader attack
330 if (receivedCmd_dec
[1] >= 16 * 4 && !(flags
& FLAG_NR_AR_ATTACK
)) {
331 //is this the correct response to an auth on a out of range block? marshmellow
332 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
333 if (MF_DBGLEVEL
>= 2) Dbprintf("Reader tried to operate (0x%02x) on out of range block: %d (0x%02x), nacking",receivedCmd_dec
[0],receivedCmd_dec
[1],receivedCmd_dec
[1]);
336 cardAUTHSC
= receivedCmd_dec
[1] / 4; // received block num
337 cardAUTHKEY
= receivedCmd_dec
[0] & 0x01;
338 crypto1_destroy(pcs
);//Added by martin
339 crypto1_create(pcs
, emlGetKey(cardAUTHSC
, cardAUTHKEY
));
340 if (!encrypted_data
) { // first authentication
341 if (MF_DBGLEVEL
>= 4) Dbprintf("Reader authenticating for block %d (0x%02x) with key %d",receivedCmd_dec
[1], receivedCmd_dec
[1], cardAUTHKEY
);
342 crypto1_word(pcs
, cuid
^ nonce
, 0);//Update crypto state
343 num_to_bytes(nonce
, 4, rAUTH_AT
); // Send nonce
344 } else { // nested authentication
345 if (MF_DBGLEVEL
>= 4) Dbprintf("Reader doing nested authentication for block %d (0x%02x) with key %d", receivedCmd_dec
[1], receivedCmd_dec
[1], cardAUTHKEY
);
346 ans
= nonce
^ crypto1_word(pcs
, cuid
^ nonce
, 0);
347 num_to_bytes(ans
, 4, rAUTH_AT
);
349 EmSendCmd(rAUTH_AT
, sizeof(rAUTH_AT
));
350 cardSTATE
= MFEMUL_AUTH1
;
353 if (!encrypted_data
) { // all other commands must be encrypted (authenticated)
356 if(receivedCmd_dec
[0] == ISO14443A_CMD_READBLOCK
357 || receivedCmd_dec
[0] == ISO14443A_CMD_WRITEBLOCK
358 || receivedCmd_dec
[0] == MIFARE_CMD_INC
359 || receivedCmd_dec
[0] == MIFARE_CMD_DEC
360 || receivedCmd_dec
[0] == MIFARE_CMD_RESTORE
361 || receivedCmd_dec
[0] == MIFARE_CMD_TRANSFER
) {
362 if (receivedCmd_dec
[1] >= 16 * 4) {
363 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
364 if (MF_DBGLEVEL
>= 2) Dbprintf("Reader tried to operate (0x%02x) on out of range block: %d (0x%02x), nacking",receivedCmd_dec
[0],receivedCmd_dec
[1],receivedCmd_dec
[1]);
367 if (receivedCmd_dec
[1] / 4 != cardAUTHSC
) {
368 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
369 if (MF_DBGLEVEL
>= 2) Dbprintf("Reader tried to operate (0x%02x) on block (0x%02x) not authenticated for (0x%02x), nacking",receivedCmd_dec
[0],receivedCmd_dec
[1],cardAUTHSC
);
373 if (receivedCmd_dec
[0] == ISO14443A_CMD_READBLOCK
) {
374 if (MF_DBGLEVEL
>= 4) {
375 Dbprintf("Reader reading block %d (0x%02x)",receivedCmd_dec
[1],receivedCmd_dec
[1]);
377 emlGetMem(response
, receivedCmd_dec
[1], 1);
378 AppendCrc14443a(response
, 16);
379 mf_crypto1_encrypt(pcs
, response
, 18, response_par
);
380 EmSendCmdPar(response
, 18, response_par
);
382 if(exitAfterNReads
> 0 && numReads
== exitAfterNReads
) {
383 Dbprintf("%d reads done, exiting", numReads
);
388 if (receivedCmd_dec
[0] == ISO14443A_CMD_WRITEBLOCK
) {
389 if (MF_DBGLEVEL
>= 4) Dbprintf("RECV 0xA0 write block %d (%02x)",receivedCmd_dec
[1],receivedCmd_dec
[1]);
390 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_ACK
));
391 cardWRBL
= receivedCmd_dec
[1];
392 cardSTATE
= MFEMUL_WRITEBL2
;
395 if (receivedCmd_dec
[0] == MIFARE_CMD_INC
|| receivedCmd_dec
[0] == MIFARE_CMD_DEC
|| receivedCmd_dec
[0] == MIFARE_CMD_RESTORE
) {
396 if (MF_DBGLEVEL
>= 4) Dbprintf("RECV 0x%02x inc(0xC1)/dec(0xC0)/restore(0xC2) block %d (%02x)",receivedCmd_dec
[0],receivedCmd_dec
[1],receivedCmd_dec
[1]);
397 if (emlCheckValBl(receivedCmd_dec
[1])) {
398 if (MF_DBGLEVEL
>= 2) Dbprintf("Reader tried to operate on block, but emlCheckValBl failed, nacking");
399 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
402 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_ACK
));
403 cardWRBL
= receivedCmd_dec
[1];
404 if (receivedCmd_dec
[0] == MIFARE_CMD_INC
)
405 cardSTATE
= MFEMUL_INTREG_INC
;
406 if (receivedCmd_dec
[0] == MIFARE_CMD_DEC
)
407 cardSTATE
= MFEMUL_INTREG_DEC
;
408 if (receivedCmd_dec
[0] == MIFARE_CMD_RESTORE
)
409 cardSTATE
= MFEMUL_INTREG_REST
;
412 if (receivedCmd_dec
[0] == MIFARE_CMD_TRANSFER
) {
413 if (MF_DBGLEVEL
>= 4) Dbprintf("RECV 0x%02x transfer block %d (%02x)",receivedCmd_dec
[0],receivedCmd_dec
[1],receivedCmd_dec
[1]);
414 if (emlSetValBl(cardINTREG
, cardINTBLOCK
, receivedCmd_dec
[1]))
415 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
417 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_ACK
));
421 if (receivedCmd_dec
[0] == ISO14443A_CMD_HALT
&& receivedCmd_dec
[1] == 0x00) {
422 if (MF_DBGLEVEL
>= 4) Dbprintf("--> HALTED.");
425 cardSTATE
= MFEMUL_HALTED
;
428 // command not allowed
429 if (MF_DBGLEVEL
>= 4) Dbprintf("Received command not allowed, nacking");
430 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
434 if (receivedCmd_len
!= 8) {
439 uint32_t nr
= bytes_to_num(receivedCmd
, 4);
440 uint32_t ar
= bytes_to_num(&receivedCmd
[4], 4);
442 // Collect AR/NR per keytype & sector
443 if(flags
& FLAG_NR_AR_ATTACK
) {
444 for (uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; i
++) {
445 if ( ar_nr_collected
[i
+mM
]==0 || ((cardAUTHSC
== ar_nr_resp
[i
+mM
].sector
) && (cardAUTHKEY
== ar_nr_resp
[i
+mM
].keytype
) && (ar_nr_collected
[i
+mM
] > 0)) ) {
446 // if first auth for sector, or matches sector and keytype of previous auth
447 if (ar_nr_collected
[i
+mM
] < 2) {
448 // if we haven't already collected 2 nonces for this sector
449 if (ar_nr_resp
[ar_nr_collected
[i
+mM
]].ar
!= ar
) {
450 // Avoid duplicates... probably not necessary, ar should vary.
451 if (ar_nr_collected
[i
+mM
]==0) {
452 // first nonce collect
453 ar_nr_resp
[i
+mM
].cuid
= cuid
;
454 ar_nr_resp
[i
+mM
].sector
= cardAUTHSC
;
455 ar_nr_resp
[i
+mM
].keytype
= cardAUTHKEY
;
456 ar_nr_resp
[i
+mM
].nonce
= nonce
;
457 ar_nr_resp
[i
+mM
].nr
= nr
;
458 ar_nr_resp
[i
+mM
].ar
= ar
;
460 // add this nonce to first moebius nonce
461 ar_nr_resp
[i
+ATTACK_KEY_COUNT
].cuid
= cuid
;
462 ar_nr_resp
[i
+ATTACK_KEY_COUNT
].sector
= cardAUTHSC
;
463 ar_nr_resp
[i
+ATTACK_KEY_COUNT
].keytype
= cardAUTHKEY
;
464 ar_nr_resp
[i
+ATTACK_KEY_COUNT
].nonce
= nonce
;
465 ar_nr_resp
[i
+ATTACK_KEY_COUNT
].nr
= nr
;
466 ar_nr_resp
[i
+ATTACK_KEY_COUNT
].ar
= ar
;
467 ar_nr_collected
[i
+ATTACK_KEY_COUNT
]++;
468 } else { // second nonce collect (std and moebius)
469 ar_nr_resp
[i
+mM
].nonce2
= nonce
;
470 ar_nr_resp
[i
+mM
].nr2
= nr
;
471 ar_nr_resp
[i
+mM
].ar2
= ar
;
472 if (!gettingMoebius
) {
474 // check if this was the last second nonce we need for std attack
475 if ( nonce2_count
== nonce1_count
) {
476 // done collecting std test switch to moebius
477 // first finish incrementing last sample
478 ar_nr_collected
[i
+mM
]++;
479 // switch to moebius collection
480 gettingMoebius
= true;
481 mM
= ATTACK_KEY_COUNT
;
482 if (flags
& FLAG_RANDOM_NONCE
) {
491 // if we've collected all the nonces we need - finish.
492 if (nonce1_count
== moebius_n_count
) finished
= true;
495 ar_nr_collected
[i
+mM
]++;
498 // we found right spot for this nonce stop looking
505 crypto1_word(pcs
, nr
, 1);
506 cardRr
= ar
^ crypto1_word(pcs
, 0, 0);
509 if (cardRr
!= prng_successor(nonce
, 64)){
510 if (MF_DBGLEVEL
>= 2) Dbprintf("AUTH FAILED for sector %d with key %c. cardRr=%08x, succ=%08x",
511 cardAUTHSC
, cardAUTHKEY
== 0 ? 'A' : 'B',
512 cardRr
, prng_successor(nonce
, 64));
513 // Shouldn't we respond anything here?
514 // Right now, we don't nack or anything, which causes the
515 // reader to do a WUPA after a while. /Martin
516 // -- which is the correct response. /piwi
517 cardAUTHKEY
= 0xff; // not authenticated
521 ans
= prng_successor(nonce
, 96) ^ crypto1_word(pcs
, 0, 0);
522 num_to_bytes(ans
, 4, rAUTH_AT
);
523 EmSendCmd(rAUTH_AT
, sizeof(rAUTH_AT
));
524 if (MF_DBGLEVEL
>= 4) Dbprintf("AUTH COMPLETED for sector %d with key %c.", cardAUTHSC
, cardAUTHKEY
== 0 ? 'A' : 'B');
526 cardSTATE
= MFEMUL_WORK
;
529 case MFEMUL_WRITEBL2
:{
530 if (receivedCmd_len
== 18) {
531 mf_crypto1_decryptEx(pcs
, receivedCmd
, receivedCmd_len
, receivedCmd_dec
);
532 if (HasValidCRC(receivedCmd_dec
, receivedCmd_len
)) {
533 emlSetMem(receivedCmd_dec
, cardWRBL
, 1);
534 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_ACK
));
535 cardSTATE
= MFEMUL_WORK
;
542 case MFEMUL_INTREG_INC
:{
543 if (receivedCmd_len
== 6) {
544 mf_crypto1_decryptEx(pcs
, receivedCmd
, receivedCmd_len
, (uint8_t*)&ans
);
545 if (emlGetValBl(&cardINTREG
, &cardINTBLOCK
, cardWRBL
)) {
546 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
550 cardINTREG
= cardINTREG
+ ans
;
552 cardSTATE
= MFEMUL_WORK
;
555 case MFEMUL_INTREG_DEC
:{
556 if (receivedCmd_len
== 6) {
557 mf_crypto1_decryptEx(pcs
, receivedCmd
, receivedCmd_len
, (uint8_t*)&ans
);
558 if (emlGetValBl(&cardINTREG
, &cardINTBLOCK
, cardWRBL
)) {
559 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
564 cardINTREG
= cardINTREG
- ans
;
565 cardSTATE
= MFEMUL_WORK
;
568 case MFEMUL_INTREG_REST
:{
569 mf_crypto1_decryptEx(pcs
, receivedCmd
, receivedCmd_len
, (uint8_t*)&ans
);
570 if (emlGetValBl(&cardINTREG
, &cardINTBLOCK
, cardWRBL
)) {
571 EmSend4bit(mf_crypto1_encrypt4bit(pcs
, CARD_NACK_NA
));
575 cardSTATE
= MFEMUL_WORK
;
579 button_pushed
= BUTTON_PRESS();
582 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
585 if(flags
& FLAG_NR_AR_ATTACK
&& MF_DBGLEVEL
>= 1) {
586 for ( uint8_t i
= 0; i
< ATTACK_KEY_COUNT
; i
++) {
587 if (ar_nr_collected
[i
] == 2) {
588 Dbprintf("Collected two pairs of AR/NR which can be used to extract %s from reader for sector %d:", (i
<ATTACK_KEY_COUNT
/2) ? "keyA" : "keyB", ar_nr_resp
[i
].sector
);
589 Dbprintf("../tools/mfkey/mfkey32 %08x %08x %08x %08x %08x %08x",
590 ar_nr_resp
[i
].cuid
, //UID
591 ar_nr_resp
[i
].nonce
, //NT
592 ar_nr_resp
[i
].nr
, //NR1
593 ar_nr_resp
[i
].ar
, //AR1
594 ar_nr_resp
[i
].nr2
, //NR2
595 ar_nr_resp
[i
].ar2
//AR2
599 for ( uint8_t i
= ATTACK_KEY_COUNT
; i
< ATTACK_KEY_COUNT
*2; i
++) {
600 if (ar_nr_collected
[i
] == 2) {
601 Dbprintf("Collected two pairs of AR/NR which can be used to extract %s from reader for sector %d:", (i
<ATTACK_KEY_COUNT
/2) ? "keyA" : "keyB", ar_nr_resp
[i
].sector
);
602 Dbprintf("../tools/mfkey/mfkey32v2 %08x %08x %08x %08x %08x %08x %08x",
603 ar_nr_resp
[i
].cuid
, //UID
604 ar_nr_resp
[i
].nonce
, //NT
605 ar_nr_resp
[i
].nr
, //NR1
606 ar_nr_resp
[i
].ar
, //AR1
607 ar_nr_resp
[i
].nonce2
,//NT2
608 ar_nr_resp
[i
].nr2
, //NR2
609 ar_nr_resp
[i
].ar2
//AR2
614 if (MF_DBGLEVEL
>= 1) Dbprintf("Emulator stopped. Tracing: %d trace length: %d ", get_tracing(), BigBuf_get_traceLen());
616 if(flags
& FLAG_INTERACTIVE
) { // Interactive mode flag, means we need to send ACK
617 //Send the collected ar_nr in the response
618 cmd_send(CMD_ACK
,CMD_SIMULATE_MIFARE_CARD
,button_pushed
,0,&ar_nr_resp
,sizeof(ar_nr_resp
));