From: iceman1001 Date: Tue, 20 Jan 2015 08:32:53 +0000 (+0100) Subject: Merge branch 'master' of https://github.com/Proxmark/proxmark3 X-Git-Tag: v2.0.0-rc1~44^2~13 X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/5149e37e66dd3324905ec7e18e67798b03f16ef6?hp=-c Merge branch 'master' of https://github.com/Proxmark/proxmark3 Conflicts: armsrc/appmain.c armsrc/apps.h --- 5149e37e66dd3324905ec7e18e67798b03f16ef6 diff --combined armsrc/appmain.c index 13995f59,530dc39c..b998c2a1 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@@ -18,6 -18,7 +18,7 @@@ #include "util.h" #include "printf.h" #include "string.h" + #include #include "legicrf.h" @@@ -667,7 -668,9 +668,9 @@@ void UsbPacketReceived(uint8_t *packet WriteTItag(c->arg[0],c->arg[1],c->arg[2]); break; case CMD_SIMULATE_TAG_125K: + LED_A_ON(); SimulateTagLowFrequency(c->arg[0], c->arg[1], 1); + LED_A_OFF(); break; case CMD_LF_SIMULATE_BIDIR: SimulateTagLowFrequencyBidir(c->arg[0], c->arg[1]); @@@ -798,18 -801,9 +801,18 @@@ case CMD_MIFAREU_READBL: MifareUReadBlock(c->arg[0],c->d.asBytes); break; + case CMD_MIFAREUC_AUTH1: + MifareUC_Auth1(c->arg[0],c->d.asBytes); + break; + case CMD_MIFAREUC_AUTH2: + MifareUC_Auth2(c->arg[0],c->d.asBytes); + break; case CMD_MIFAREU_READCARD: - MifareUReadCard(c->arg[0],c->arg[1],c->d.asBytes); + MifareUReadCard(c->arg[0], c->arg[1], c->d.asBytes); break; + case CMD_MIFAREUC_READCARD: + MifareUReadCard(c->arg[0],c->arg[1],c->d.asBytes); + break; case CMD_MIFARE_READSC: MifareReadSector(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes); break; @@@ -864,7 -858,6 +867,7 @@@ case CMD_MIFARE_SNIFFER: SniffMifare(c->arg[0]); break; + #endif #ifdef WITH_ICLASS diff --combined armsrc/apps.h index cc462dc1,5a1ab690..27e08fa1 --- a/armsrc/apps.h +++ b/armsrc/apps.h @@@ -18,8 -18,6 +18,8 @@@ #include "hitag2.h" #include "mifare.h" +#include "../common/crc32.h" + // The large multi-purpose buffer, typically used to hold A/D samples, // maybe processed in some way. #define BIGBUF_SIZE 40000 @@@ -39,6 -37,25 +39,25 @@@ uint32_t BigBuf[BIGBUF_SIZE / sizeof(ui #define FREE_BUFFER_OFFSET (CARD_MEMORY_OFFSET + CARD_MEMORY_SIZE) #define FREE_BUFFER_SIZE (BIGBUF_SIZE - FREE_BUFFER_OFFSET - 1) + /* + The statements above translates into this : + BIGBUF_SIZE = 40000 + TRACE_OFFSET = 0 + TRACE_SIZE = 3000 + RECV_CMD_OFFSET = 3000 + MAX_FRAME_SIZE = 256 + MAX_PARITY_SIZE = 32 + RECV_CMD_PAR_OFFSET = 3256 + RECV_RESP_OFFSET = 3288 + RECV_RESP_PAR_OFFSET= 3544 + CARD_MEMORY_OFFSET = 3576 + CARD_MEMORY_SIZE = 4096 + DMA_BUFFER_OFFSET = 3576 + DMA_BUFFER_SIZE = 4096 + FREE_BUFFER_OFFSET = 7672 + FREE_BUFFER_SIZE = 32327 + */ + extern const uint8_t OddByteParity[256]; extern uint8_t *trace; // = (uint8_t *) BigBuf; extern int traceLen; // = 0; @@@ -117,6 -134,8 +136,8 @@@ void SetAdcMuxFor(uint32_t whichGpio) #define FPGA_HF_SIMULATOR_MODULATE_BPSK (1<<0) #define FPGA_HF_SIMULATOR_MODULATE_212K (2<<0) #define FPGA_HF_SIMULATOR_MODULATE_424K (4<<0) + #define FPGA_HF_SIMULATOR_MODULATE_424K_8BIT 0x5//101 + // Options for ISO14443A #define FPGA_HF_ISO14443A_SNIFFER (0<<0) #define FPGA_HF_ISO14443A_TAGSIM_LISTEN (1<<0) @@@ -179,9 -198,7 +200,7 @@@ void ReaderMifare(bool first_try) int32_t dist_nt(uint32_t nt1, uint32_t nt2); void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *data); void MifareUReadBlock(uint8_t arg0,uint8_t *datain); - void MifareUC_Auth1(uint8_t arg0, uint8_t *datain); - void MifareUC_Auth2(uint32_t arg0, uint8_t *datain); - void MifareUReadCard(uint8_t arg0, int Pages, uint8_t *datain); + void MifareUReadCard(uint8_t arg0, int arg1, uint8_t *datain); void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain); void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain); void MifareUWriteBlock(uint8_t arg0,uint8_t *datain); @@@ -198,25 -215,6 +217,25 @@@ void MifareCSetBlock(uint32_t arg0, uin void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain); void MifareCIdent(); // is "magic chinese" card? +//desfire +void Mifare_DES_Auth1(uint8_t arg0,uint8_t *datain); +void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain); + +// mifaredesfire.h +bool InitDesfireCard(); +void MifareSendCommand(uint8_t arg0,uint8_t arg1, uint8_t *datain); +void MifareDesfireGetInformation(); +void MifareDES_Auth1(uint8_t arg0,uint8_t arg1,uint8_t arg2, uint8_t *datain); +void ReaderMifareDES(uint32_t param, uint32_t param2, uint8_t * datain); +int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout); +size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout); +void OnSuccess(); +void OnError(uint8_t reason); + + + + + /// iso15693.h void RecordRawAdcSamplesIso15693(void); void AcquireRawAdcSamplesIso15693(void); diff --combined armsrc/mifarecmd.c index 470af6a7,ecd8728d..22926dcf --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@@ -17,8 -17,6 +17,8 @@@ #include "apps.h" #include "util.h" +#include "crc.h" + //----------------------------------------------------------------------------- // Select, Authenticate, Read a MIFARE tag. // read block @@@ -82,71 -80,7 +82,71 @@@ void MifareReadBlock(uint8_t arg0, uint cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16); LED_B_OFF(); - // Thats it... + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + LEDsoff(); +} + + +void MifareUC_Auth1(uint8_t arg0, uint8_t *datain){ + + byte_t isOK = 0; + byte_t dataoutbuf[16] = {0x00}; + uint8_t uid[10] = {0x00}; + uint32_t cuid; + + LED_A_ON(); + LED_B_OFF(); + LED_C_OFF(); + + iso14a_clear_trace(); + iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); + + if(!iso14443a_select_card(uid, NULL, &cuid)) { + if (MF_DBGLEVEL >= MF_DBG_ERROR) + Dbprintf("Can't select card"); + OnError(0); + return; + }; + + if(mifare_ultra_auth1(cuid, dataoutbuf)){ + if (MF_DBGLEVEL >= MF_DBG_ERROR) + Dbprintf("Authentication part1: Fail."); + OnError(1); + return; + } + + isOK = 1; + if (MF_DBGLEVEL >= MF_DBG_EXTENDED) + DbpString("AUTH 1 FINISHED"); + + cmd_send(CMD_ACK,isOK,cuid,0,dataoutbuf,11); + LEDsoff(); +} +void MifareUC_Auth2(uint32_t arg0, uint8_t *datain){ + + uint32_t cuid = arg0; + uint8_t key[16] = {0x00}; + byte_t isOK = 0; + byte_t dataoutbuf[16] = {0x00}; + + memcpy(key, datain, 16); + + LED_A_ON(); + LED_B_OFF(); + LED_C_OFF(); + + if(mifare_ultra_auth2(cuid, key, dataoutbuf)){ + if (MF_DBGLEVEL >= MF_DBG_ERROR) + Dbprintf("Authentication part2: Fail..."); + OnError(1); + return; + } + + isOK = 1; + if (MF_DBGLEVEL >= MF_DBG_EXTENDED) + DbpString("AUTH 2 FINISHED"); + + cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,11); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LEDsoff(); } @@@ -168,21 -102,21 +168,21 @@@ void MifareUReadBlock(uint8_t arg0,uint int len = iso14443a_select_card(uid, NULL, &cuid); if(!len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card"); - OnError(1); + //OnError(1); return; }; len = mifare_ultra_readblock(cuid, blockNo, dataout); if(len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error"); - OnError(2); + //OnError(2); return; }; len = mifare_ultra_halt(cuid); if(len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error"); - OnError(3); + //OnError(3); return; }; @@@ -267,11 -201,11 +267,11 @@@ void MifareUReadCard(uint8_t arg0, int int count_Pages = 0; byte_t dataout[176] = {0x00};; uint8_t uid[10] = {0x00}; - uint32_t cuid; + uint32_t cuid; - LED_A_ON(); - LED_B_OFF(); - LED_C_OFF(); + LED_A_ON(); + LED_B_OFF(); + LED_C_OFF(); if (MF_DBGLEVEL >= MF_DBG_ALL) Dbprintf("Pages %d",Pages); @@@ -284,7 -218,7 +284,7 @@@ if (!len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card"); - OnError(1); + //OnError(1); return; } @@@ -295,7 -229,7 +295,7 @@@ if (len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i); - OnError(2); + //OnError(2); return; } else { count_Pages++; @@@ -306,7 -240,7 +306,7 @@@ if (len) { if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error"); - OnError(3); + //OnError(3); return; } @@@ -321,8 -255,8 +321,8 @@@ len = 176; cmd_send(CMD_ACK, 1, 0, 0, dataout, len); - FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); - LEDsoff(); + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + LEDsoff(); } @@@ -1127,58 -1061,3 +1127,58 @@@ void MifareCIdent() // // DESFIRE // + +void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){ + + byte_t dataout[11] = {0x00}; + uint8_t uid[10] = {0x00}; + uint32_t cuid; + + iso14a_clear_trace(); + iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); + + int len = iso14443a_select_card(uid, NULL, &cuid); + if(!len) { + if (MF_DBGLEVEL >= MF_DBG_ERROR) + Dbprintf("Can't select card"); + OnError(1); + return; + }; + + if(mifare_desfire_des_auth1(cuid, dataout)){ + if (MF_DBGLEVEL >= MF_DBG_ERROR) + Dbprintf("Authentication part1: Fail."); + OnError(4); + return; + } + + if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED"); + + cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout)); +} + +void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){ + + uint32_t cuid = arg0; + uint8_t key[16] = {0x00}; + byte_t isOK = 0; + byte_t dataout[12] = {0x00}; + + memcpy(key, datain, 16); + + isOK = mifare_desfire_des_auth2(cuid, key, dataout); + + if( isOK) { + if (MF_DBGLEVEL >= MF_DBG_EXTENDED) + Dbprintf("Authentication part2: Failed"); + OnError(4); + return; + } + + if (MF_DBGLEVEL >= MF_DBG_EXTENDED) + DbpString("AUTH 2 FINISHED"); + + cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout)); + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + LEDsoff(); - } ++} diff --combined client/lualibs/commands.lua index 13b9c8e7,13b9c8e7..a5442f2a --- a/client/lualibs/commands.lua +++ b/client/lualibs/commands.lua @@@ -49,7 -49,7 +49,7 @@@ local _commands = CMD_EM4X_WRITE_WORD = 0x0219, CMD_IO_DEMOD_FSK = 0x021A, CMD_IO_CLONE_TAG = 0x021B, -- CMD_EM410X_DEMOD = 0x021C, ++ CMD_EM410X_DEMOD = 0x021c, --/* CMD_SET_ADC_MUX: ext1 is 0 for lopkd, 1 for loraw, 2 for hipkd, 3 for hiraw */ --// For the 13.56 MHz tags diff --combined client/lualibs/utils.lua index e84f70ad,e84f70ad..c5baa406 --- a/client/lualibs/utils.lua +++ b/client/lualibs/utils.lua @@@ -108,6 -108,6 +108,24 @@@ local Utils return retval end, ++ -- input parameter is a string ++ -- Swaps the endianess and returns a string, ++ -- IE: 'cd7a' -> '7acd' -> 0x7acd ++ SwapEndiannessStr = function(s, len) ++ if s == nil then return nil end ++ if #s == 0 then return '' end ++ if type(s) ~= 'string' then return nil end ++ ++ local retval ++ if len == 16 then ++ retval = s:sub(3,4)..s:sub(1,2) ++ elseif len == 24 then ++ retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2) ++ elseif len == 32 then ++ retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2) ++ end ++ return retval ++ end, ------------ CONVERSIONS -- @@@ -116,7 -116,7 +134,7 @@@ local B,K,OUT,I,D=16,"0123456789ABCDEF","",0 while IN>0 do I=I+1 -- IN,D=math.floor(IN/B),math.mod(IN,B)+1 ++ IN , D = math.floor(IN/B), math.modf(IN,B)+1 OUT=string.sub(K,D,D)..OUT end return OUT diff --combined client/scripts/tnp3dump.lua index 520161b9,520161b9..f41fe8ab --- a/client/scripts/tnp3dump.lua +++ b/client/scripts/tnp3dump.lua @@@ -249,18 -249,18 +249,19 @@@ local function main(args end end ++ ++ local uid = block0:sub(1,8) ++ local itemtype = block1:sub(1,4) ++ local cardid = block1:sub(9,24) ++ -- Write dump to files if not DEBUG then -- local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'.bin') ++ local foo = dumplib.SaveAsBinary(bindata, outputTemplate..'_uid_'..uid..'.bin') print(("Wrote a BIN dump to the file %s"):format(foo)) -- local bar = dumplib.SaveAsText(emldata, outputTemplate..'.eml') ++ local bar = dumplib.SaveAsText(emldata, outputTemplate..'_uid_'..uid..'.eml') print(("Wrote a EML dump to the file %s"):format(bar)) end -- local uid = block0:sub(1,8) -- local itemtype = block1:sub(1,4) -- local cardid = block1:sub(9,24) -- -- Show info print( string.rep('--',20) ) print( (' ITEM TYPE : 0x%s - %s'):format(itemtype, toyNames[itemtype]) ) diff --combined client/scripts/tnp3sim.lua index f43dafa2,f43dafa2..adc34cce --- a/client/scripts/tnp3sim.lua +++ b/client/scripts/tnp3sim.lua @@@ -241,18 -241,18 +241,20 @@@ local function main(args local cmdSetDbgOff = "hf mf dbg 0" core.console( cmdSetDbgOff) -- -- Look for tag present on reader, -- result, err = lib14a.read1443a(false) -- if not result then return oops(err) end ++ -- if not loadFromDump then ++ -- -- Look for tag present on reader, ++ -- result, err = lib14a.read1443a(false) ++ -- if not result then return oops(err) end -- core.clearCommandBuffer() ++ -- core.clearCommandBuffer() -- if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx -- return oops('This is not a TNP3xxx tag. aborting.') -- end ++ -- if 0x01 ~= result.sak then -- NXP MIFARE TNP3xxx ++ -- return oops('This is not a TNP3xxx tag. aborting.') ++ -- end -- -- Show tag info -- print((' Found tag : %s'):format(result.name)) ++ -- -- Show tag info ++ -- print((' Found tag : %s'):format(result.name)) ++ -- end -- Load dump.bin file print( (' Load data from %s'):format(inputTemplate)) @@@ -349,7 -349,7 +351,7 @@@ err = LoadEmulator(blocks) if err then return oops(err) end core.clearCommandBuffer() -- print('The simulation is now prepared.\n --> run \"hf mf sim 5 '..uid..'\" <--') ++ print('The simulation is now prepared.\n --> run \"hf mf sim u '..uid..' x\" <--') end end main(args) diff --combined common/cmd.h index b330a219,b330a219..bc69ff61 --- a/common/cmd.h +++ b/common/cmd.h @@@ -33,8 -33,8 +33,8 @@@ #ifndef _PROXMARK_CMD_H_ #define _PROXMARK_CMD_H_ --#include --#include ++#include "common.h" ++#include "usb_cmd.h" #include "usb_cdc.h" bool cmd_receive(UsbCommand* cmd); diff --combined common/usb_cdc.h index d7b9c2e5,d7b9c2e5..59e73a47 --- a/common/usb_cdc.h +++ b/common/usb_cdc.h @@@ -35,7 -35,7 +35,7 @@@ #ifndef _USB_CDC_H_ #define _USB_CDC_H_ --#include ++#include "common.h" void usb_disable(); void usb_enable();