X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/2b1f4228c2987459445d30443f92038f9ea080c6..b725f2ca21d11709b5e61595bd88ae260fee4c7f:/armsrc/iso14443a.c diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index bfbc70c7..8a925475 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -1060,7 +1060,7 @@ void SimulateIso14443aTag(int tagType, int flags, byte_t* data) { .response = response5, .response_n = sizeof(response5) }, // Authentication answer (random nonce) { .response = response6, .response_n = sizeof(response6) }, // dummy ATS (pseudo-ATR), answer to RATS //{ .response = response7_NTAG, .response_n = sizeof(response7_NTAG)}, // EV1/NTAG GET_VERSION response - { .response = response8, .response_n = sizeof(response8) }, // EV1/NTAG PACK response + { .response = response8, .response_n = sizeof(response8) } // EV1/NTAG PACK response //{ .response = response9, .response_n = sizeof(response9) } // EV1/NTAG CHK_TEAR response }; @@ -1093,9 +1093,8 @@ void SimulateIso14443aTag(int tagType, int flags, byte_t* data) // Prepare the responses of the anticollision phase // there will be not enough time to do this at the moment the reader sends it REQA - for (size_t i=0; iSSC_SR & (AT91C_SSC_TXRDY)) { AT91C_BASE_SSC->SSC_THR = cmd[c]; c++; - if(c >= len) { + if(c >= len) break; - } } } @@ -1509,7 +1506,7 @@ static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8_t *parity) { int i, j; - int last; + int last = 0; uint8_t b; ToSendReset(); @@ -1517,7 +1514,6 @@ void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8 // Start of Communication (Seq. Z) ToSend[++ToSendMax] = SEC_Z; LastProxToAirDuration = 8 * (ToSendMax+1) - 6; - last = 0; size_t bytecount = nbytes(bits); // Generate send structure for the data bits @@ -1730,7 +1726,7 @@ int EmSend4bitEx(uint8_t resp, bool correctionNeeded){ Code4bitAnswerAsTag(resp); int res = EmSendCmd14443aRaw(ToSend, ToSendMax, correctionNeeded); // do the tracing for the previous reader request and this tag answer: - uint8_t par[1]; + uint8_t par[1] = {0x00}; GetParity(&resp, 1, par); EmLogTrace(Uart.output, Uart.len, @@ -1767,13 +1763,13 @@ int EmSendCmdExPar(uint8_t *resp, uint16_t respLen, bool correctionNeeded, uint8 } int EmSendCmdEx(uint8_t *resp, uint16_t respLen, bool correctionNeeded){ - uint8_t par[MAX_PARITY_SIZE]; + uint8_t par[MAX_PARITY_SIZE] = {0x00}; GetParity(resp, respLen, par); return EmSendCmdExPar(resp, respLen, correctionNeeded, par); } int EmSendCmd(uint8_t *resp, uint16_t respLen){ - uint8_t par[MAX_PARITY_SIZE]; + uint8_t par[MAX_PARITY_SIZE] = {0x00}; GetParity(resp, respLen, par); return EmSendCmdExPar(resp, respLen, false, par); } @@ -1794,9 +1790,12 @@ bool EmLogTrace(uint8_t *reader_data, uint16_t reader_len, uint32_t reader_Start uint16_t exact_fdt = (approx_fdt - 20 + 32)/64 * 64 + 20; reader_EndTime = tag_StartTime - exact_fdt; reader_StartTime = reader_EndTime - reader_modlen; - if (!LogTrace(reader_data, reader_len, reader_StartTime, reader_EndTime, reader_Parity, TRUE)) { + + if (!LogTrace(reader_data, reader_len, reader_StartTime, reader_EndTime, reader_Parity, TRUE)) return FALSE; - } else return(!LogTrace(tag_data, tag_len, tag_StartTime, tag_EndTime, tag_Parity, FALSE)); + else + return(!LogTrace(tag_data, tag_len, tag_StartTime, tag_EndTime, tag_Parity, FALSE)); + } else { return TRUE; } @@ -1848,9 +1847,8 @@ void ReaderTransmitBitsPar(uint8_t* frame, uint16_t bits, uint8_t *par, uint32_t LED_A_ON(); // Log reader command in trace buffer - if (tracing) { + if (tracing) LogTrace(frame, nbytes(bits), LastTimeProxToAirStart*16 + DELAY_ARM2AIR_AS_READER, (LastTimeProxToAirStart + LastProxToAirDuration)*16 + DELAY_ARM2AIR_AS_READER, par, TRUE); - } } void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *timing) @@ -1861,7 +1859,7 @@ void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *tim void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing) { // Generate parity and redirect - uint8_t par[MAX_PARITY_SIZE]; + uint8_t par[MAX_PARITY_SIZE] = {0x00}; GetParity(frame, len/8, par); ReaderTransmitBitsPar(frame, len, par, timing); } @@ -1869,26 +1867,30 @@ void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing) void ReaderTransmit(uint8_t* frame, uint16_t len, uint32_t *timing) { // Generate parity and redirect - uint8_t par[MAX_PARITY_SIZE]; + uint8_t par[MAX_PARITY_SIZE] = {0x00}; GetParity(frame, len, par); ReaderTransmitBitsPar(frame, len*8, par, timing); } int ReaderReceiveOffset(uint8_t* receivedAnswer, uint16_t offset, uint8_t *parity) { - if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, offset)) return FALSE; - if (tracing) { + if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, offset)) + return FALSE; + + if (tracing) LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, FALSE); - } + return Demod.len; } int ReaderReceive(uint8_t *receivedAnswer, uint8_t *parity) { - if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, 0)) return FALSE; - if (tracing) { + if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, 0)) + return FALSE; + + if (tracing) LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, FALSE); - } + return Demod.len; } @@ -2239,16 +2241,14 @@ void ReaderMifare(bool first_try) uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; static uint8_t mf_nr_ar3; - uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE]; - uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE]; + uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00}; + uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00}; - if (first_try) { + if (first_try) iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD); - } // free eventually allocated BigBuf memory. We want all for tracing. BigBuf_free(); - clear_trace(); set_tracing(TRUE); @@ -2256,8 +2256,8 @@ void ReaderMifare(bool first_try) uint8_t par[1] = {0}; // maximum 8 Bytes to be sent here, 1 byte parity is therefore enough static byte_t par_low = 0; bool led_on = TRUE; - uint8_t uid[10] ={0}; - uint32_t cuid; + uint8_t uid[10] = {0}; + uint32_t cuid = 0; uint32_t nt = 0; uint32_t previous_nt = 0; @@ -2265,12 +2265,12 @@ void ReaderMifare(bool first_try) byte_t par_list[8] = {0x00}; byte_t ks_list[8] = {0x00}; - #define PRNG_SEQUENCE_LENGTH (1 << 16); + #define PRNG_SEQUENCE_LENGTH (1 << 16); static uint32_t sync_time = 0; static int32_t sync_cycles = 0; int catch_up_cycles = 0; int last_catch_up = 0; - uint16_t elapsed_prng_sequences; + uint16_t elapsed_prng_sequences = 0; uint16_t consecutive_resyncs = 0; int isOK = 0; @@ -2302,10 +2302,10 @@ void ReaderMifare(bool first_try) int16_t debug_info_nr = -1; uint16_t strategy = 0; int32_t debug_info[MAX_STRATEGY][NUM_DEBUG_INFOS]; - uint32_t select_time; - uint32_t halt_time; + uint32_t select_time = 0; + uint32_t halt_time = 0; - for(uint16_t i = 0; TRUE; i++) { + for(uint16_t i = 0; TRUE; ++i) { LED_C_ON(); WDT_HIT(); @@ -2331,6 +2331,7 @@ void ReaderMifare(bool first_try) SpinDelay(200); iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD); SpinDelay(100); + WDT_HIT(); } if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) { @@ -2348,10 +2349,11 @@ void ReaderMifare(bool first_try) while(GetCountSspClk() > sync_time) { elapsed_prng_sequences++; sync_time = (sync_time & 0xfffffff8) + sync_cycles; - } + } // Transmit MIFARE_CLASSIC_AUTH at synctime. Should result in returning the same tag nonce (== nt_attacked) ReaderTransmit(mf_auth, sizeof(mf_auth), &sync_time); + } else { // collect some information on tag nonces for debugging: #define DEBUG_FIXED_SYNC_CYCLES PRNG_SEQUENCE_LENGTH @@ -2402,10 +2404,10 @@ void ReaderMifare(bool first_try) isOK = -4; // Card's PRNG runs at an unexpected frequency or resets unexpectedly break; } else { // continue for a while, just to collect some debug info - debug_info[strategy][debug_info_nr] = nt_distance; - debug_info_nr++; + ++debug_info_nr; + debug_info[strategy][debug_info_nr] = nt_distance; if (debug_info_nr == NUM_DEBUG_INFOS) { - strategy++; + ++strategy; debug_info_nr = 0; } continue; @@ -2430,7 +2432,7 @@ void ReaderMifare(bool first_try) } catch_up_cycles /= elapsed_prng_sequences; if (catch_up_cycles == last_catch_up) { - consecutive_resyncs++; + ++consecutive_resyncs; } else { last_catch_up = catch_up_cycles; @@ -2455,9 +2457,8 @@ void ReaderMifare(bool first_try) if (ReaderReceive(receivedAnswer, receivedAnswerPar)) { catch_up_cycles = 8; // the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer - if (nt_diff == 0) { + if (nt_diff == 0) par_low = par[0] & 0xE0; // there is no need to check all parities for other nt_diff. Parity Bits for mf_nr_ar[0..2] won't change - } led_on = !led_on; if(led_on) LED_B_ON(); else LED_B_OFF(); @@ -2475,10 +2476,9 @@ void ReaderMifare(bool first_try) mf_nr_ar[3] = (mf_nr_ar[3] & 0x1F) | (nt_diff << 5); par[0] = par_low; } else { - if (nt_diff == 0 && first_try) - { + if (nt_diff == 0 && first_try) { par[0]++; - if (par[0] == 0x00) { // tried all 256 possible parities without success. Card doesn't send NACK. + if (par[0] == 0x00) { // tried all 256 possible parities without success. Card doesn't send NACK. isOK = -2; break; } @@ -2488,20 +2488,21 @@ void ReaderMifare(bool first_try) } } - mf_nr_ar[3] &= 0x1F; - + + WDT_HIT(); + if (isOK == -4) { if (MF_DBGLEVEL >= 3) { - for (uint16_t i = 0; i <= MAX_STRATEGY; i++) { - for(uint16_t j = 0; j < NUM_DEBUG_INFOS; j++) { + for (uint16_t i = 0; i <= MAX_STRATEGY; ++i) { + for(uint16_t j = 0; j < NUM_DEBUG_INFOS; ++j) { Dbprintf("collected debug info[%d][%d] = %d", i, j, debug_info[i][j]); } } } } - byte_t buf[28]; + byte_t buf[28] = {0x00}; memcpy(buf + 0, uid, 4); num_to_bytes(nt, 4, buf + 4); memcpy(buf + 8, par_list, 8); @@ -2549,10 +2550,10 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * struct Crypto1State *pcs; pcs = &mpcs; uint32_t numReads = 0;//Counts numer of times reader read a block - uint8_t receivedCmd[MAX_MIFARE_FRAME_SIZE]; - uint8_t receivedCmd_par[MAX_MIFARE_PARITY_SIZE]; - uint8_t response[MAX_MIFARE_FRAME_SIZE]; - uint8_t response_par[MAX_MIFARE_PARITY_SIZE]; + uint8_t receivedCmd[MAX_MIFARE_FRAME_SIZE] = {0x00}; + uint8_t receivedCmd_par[MAX_MIFARE_PARITY_SIZE] = {0x00}; + uint8_t response[MAX_MIFARE_FRAME_SIZE] = {0x00}; + uint8_t response_par[MAX_MIFARE_PARITY_SIZE] = {0x00}; uint8_t rATQA[] = {0x04, 0x00}; // Mifare classic 1k 4BUID uint8_t rUIDBCC1[] = {0xde, 0xad, 0xbe, 0xaf, 0x62}; @@ -2561,7 +2562,8 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * //uint8_t rSAK[] = {0x09, 0x3f, 0xcc }; // Mifare Mini uint8_t rSAK1[] = {0x04, 0xda, 0x17}; - uint8_t rAUTH_NT[] = {0x01, 0x01, 0x01, 0x01}; + //uint8_t rAUTH_NT[] = {0x01, 0x01, 0x01, 0x01}; + uint8_t rAUTH_NT[] = {0x55, 0x41, 0x49, 0x92}; uint8_t rAUTH_AT[] = {0x00, 0x00, 0x00, 0x00}; //Here, we collect UID1,UID2,NT,AR,NR,0,0,NT2,AR2,NR2 @@ -2712,8 +2714,7 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * break; } case MFEMUL_AUTH1:{ - if( len != 8) - { + if( len != 8) { cardSTATE_TO_IDLE(); LogTrace(Uart.output, Uart.len, Uart.startTime*16 - DELAY_AIR2ARM_AS_TAG, Uart.endTime*16 - DELAY_AIR2ARM_AS_TAG, Uart.parity, TRUE); break; @@ -2724,9 +2725,9 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * //Collect AR/NR //if(ar_nr_collected < 2 && cardAUTHSC == 2){ - if(ar_nr_collected < 2){ - if(ar_nr_responses[2] != ar) - {// Avoid duplicates... probably not necessary, ar should vary. + if(ar_nr_collected < 2) { + if(ar_nr_responses[2] != ar) { + // Avoid duplicates... probably not necessary, ar should vary. //ar_nr_responses[ar_nr_collected*5] = 0; //ar_nr_responses[ar_nr_collected*5+1] = 0; ar_nr_responses[ar_nr_collected*5+2] = nonce; @@ -2736,9 +2737,7 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * } // Interactive mode flag, means we need to send ACK if(flags & FLAG_INTERACTIVE && ar_nr_collected == 2) - { finished = true; - } } // --- crypto @@ -2767,9 +2766,13 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * EmSendCmd(rAUTH_AT, sizeof(rAUTH_AT)); LED_C_ON(); cardSTATE = MFEMUL_WORK; - if (MF_DBGLEVEL >= 4) Dbprintf("AUTH COMPLETED for sector %d with key %c. time=%d", - cardAUTHSC, cardAUTHKEY == 0 ? 'A' : 'B', - GetTickCount() - authTimer); + if (MF_DBGLEVEL >= 4) { + Dbprintf("AUTH COMPLETED for sector %d with key %c. time=%d", + cardAUTHSC, + cardAUTHKEY == 0 ? 'A' : 'B', + GetTickCount() - authTimer + ); + } break; } case MFEMUL_SELECT2:{ @@ -2784,7 +2787,9 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * // select 2 card if (len == 9 && - (receivedCmd[0] == 0x95 && receivedCmd[1] == 0x70 && memcmp(&receivedCmd[2], rUIDBCC2, 4) == 0)) { + (receivedCmd[0] == 0x95 && + receivedCmd[1] == 0x70 && + memcmp(&receivedCmd[2], rUIDBCC2, 4) == 0) ) { EmSendCmd(rSAK, sizeof(rSAK)); cuid = bytes_to_num(rUIDBCC2, 4); cardSTATE = MFEMUL_WORK; @@ -2811,10 +2816,9 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * bool encrypted_data = (cardAUTHKEY != 0xFF) ; - if(encrypted_data) { - // decrypt seqence + // decrypt seqence + if(encrypted_data) mf_crypto1_decrypt(pcs, receivedCmd, len); - } if (len == 4 && (receivedCmd[0] == 0x60 || receivedCmd[0] == 0x61)) { authTimer = GetTickCount(); @@ -2878,9 +2882,8 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * } // read block if (receivedCmd[0] == 0x30) { - if (MF_DBGLEVEL >= 4) { - Dbprintf("Reader reading block %d (0x%02x)",receivedCmd[1],receivedCmd[1]); - } + if (MF_DBGLEVEL >= 4) Dbprintf("Reader reading block %d (0x%02x)",receivedCmd[1],receivedCmd[1]); + emlGetMem(response, receivedCmd[1], 1); AppendCrc14443a(response, 16); mf_crypto1_encrypt(pcs, response, 18, response_par); @@ -2947,7 +2950,7 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t * break; } case MFEMUL_WRITEBL2:{ - if (len == 18){ + if (len == 18) { mf_crypto1_decrypt(pcs, receivedCmd, len); emlSetMem(receivedCmd, cardWRBL, 1); EmSend4bit(mf_crypto1_encrypt4bit(pcs, CARD_ACK)); @@ -3070,11 +3073,11 @@ void RAMFUNC SniffMifare(uint8_t param) { // The command (reader -> tag) that we're receiving. // The length of a received command will in most cases be no more than 18 bytes. // So 32 should be enough! - uint8_t receivedCmd[MAX_MIFARE_FRAME_SIZE]; - uint8_t receivedCmdPar[MAX_MIFARE_PARITY_SIZE]; + uint8_t receivedCmd[MAX_MIFARE_FRAME_SIZE] = {0x00}; + uint8_t receivedCmdPar[MAX_MIFARE_PARITY_SIZE] = {0x00}; // The response (tag -> reader) that we're receiving. - uint8_t receivedResponse[MAX_MIFARE_FRAME_SIZE]; - uint8_t receivedResponsePar[MAX_MIFARE_PARITY_SIZE]; + uint8_t receivedResponse[MAX_MIFARE_FRAME_SIZE] = {0x00}; + uint8_t receivedResponsePar[MAX_MIFARE_PARITY_SIZE] = {0x00}; iso14443a_setup(FPGA_HF_ISO14443A_SNIFFER); @@ -3130,11 +3133,12 @@ void RAMFUNC SniffMifare(uint8_t param) { int register readBufDataP = data - dmaBuf; // number of bytes we have processed so far int register dmaBufDataP = DMA_BUFFER_SIZE - AT91C_BASE_PDC_SSC->PDC_RCR; // number of bytes already transferred - if (readBufDataP <= dmaBufDataP){ // we are processing the same block of data which is currently being transferred + + if (readBufDataP <= dmaBufDataP) // we are processing the same block of data which is currently being transferred dataLen = dmaBufDataP - readBufDataP; // number of bytes still to be processed - } else { + else dataLen = DMA_BUFFER_SIZE - readBufDataP + dmaBufDataP; // number of bytes still to be processed - } + // test for length of buffer if(dataLen > maxDataLen) { // we are more behind than ever... maxDataLen = dataLen; @@ -3161,10 +3165,12 @@ void RAMFUNC SniffMifare(uint8_t param) { if (sniffCounter & 0x01) { - if(!TagIsActive) { // no need to try decoding tag data if the reader is sending + // no need to try decoding tag data if the reader is sending + if(!TagIsActive) { uint8_t readerdata = (previous_data & 0xF0) | (*data >> 4); if(MillerDecoding(readerdata, (sniffCounter-1)*4)) { LED_C_INV(); + if (MfSniffLogic(receivedCmd, Uart.len, Uart.parity, Uart.bitCount, TRUE)) break; /* And ready to receive another command. */ @@ -3176,7 +3182,8 @@ void RAMFUNC SniffMifare(uint8_t param) { ReaderIsActive = (Uart.state != STATE_UNSYNCD); } - if(!ReaderIsActive) { // no need to try decoding tag data if the reader is sending + // no need to try decoding tag data if the reader is sending + if(!ReaderIsActive) { uint8_t tagdata = (previous_data << 4) | (*data & 0x0F); if(ManchesterDecoding(tagdata, 0, (sniffCounter-1)*4)) { LED_C_INV(); @@ -3185,6 +3192,7 @@ void RAMFUNC SniffMifare(uint8_t param) { // And ready to receive another response. DemodReset(); + // And reset the Miller decoder including its (now outdated) input buffer UartInit(receivedCmd, receivedCmdPar); } @@ -3195,9 +3203,9 @@ void RAMFUNC SniffMifare(uint8_t param) { previous_data = *data; sniffCounter++; data++; - if(data == dmaBuf + DMA_BUFFER_SIZE) { + + if(data == dmaBuf + DMA_BUFFER_SIZE) data = dmaBuf; - } } // main cycle