]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmddata.c
Add Viking demod,
[proxmark3-svn] / client / cmddata.c
index cb1c7cd455adbfd9cb0d23837e227a6714c97c75..663faf808e23ef490f1cbddb92502712cf11c100 100644 (file)
@@ -24,6 +24,7 @@
 #include "usb_cmd.h"
 #include "crc.h"
 #include "crc16.h"
+#include "loclass/cipherutils.h"
 
 uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
 uint8_t g_debugMode=0;
@@ -636,6 +637,32 @@ int CmdG_Prox_II_Demod(const char *Cmd)
        return 1;
 }
 
+//by marshmellow
+//see ASKDemod for what args are accepted
+int CmdVikingDemod(const char *Cmd)
+{
+       if (!ASKDemod(Cmd, false, false, 1)) {
+               if (g_debugMode) PrintAndLog("ASKDemod failed");
+               return 0;
+       }
+       size_t size = DemodBufferLen;
+       //call lfdemod.c demod for gProxII
+       int ans = VikingDemod_AM(DemodBuffer, &size);
+       if (ans < 0) {
+               if (g_debugMode) PrintAndLog("Error Viking_Demod");
+               return 0;
+       }
+       //got a good demod
+       uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans, 32);
+       uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32);
+       uint32_t cardid = bytebits_to_byte(DemodBuffer+ans+24, 32);
+       uint8_t checksum = bytebits_to_byte(DemodBuffer+ans+32+24, 8);
+       PrintAndLog("Viking Tag Found: Card ID %08X, Checksum: %02X", cardid, checksum);
+       PrintAndLog("Raw: %08X%08X", raw1,raw2);
+       setDemodBuf(DemodBuffer+ans, 64, 0);
+       return 1;
+}
+
 //by marshmellow - see ASKDemod
 int Cmdaskrawdemod(const char *Cmd)
 {
@@ -1129,8 +1156,6 @@ int CmdFSKdemodParadox(const char *Cmd)
 //print ioprox ID and some format details
 int CmdFSKdemodIO(const char *Cmd)
 {
-       //raw fsk demod no manchester decoding no start bit finding just get binary from wave
-       //set defaults
        int idx=0;
        //something in graphbuffer?
        if (GraphTraceLen < 65) {
@@ -1219,11 +1244,6 @@ int CmdFSKdemodIO(const char *Cmd)
 //print full AWID Prox ID and some bit format details if found
 int CmdFSKdemodAWID(const char *Cmd)
 {
-
-       //int verbose=1;
-       //sscanf(Cmd, "%i", &verbose);
-
-       //raw fsk demod no manchester decoding no start bit finding just get binary from wave
        uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
        size_t size = getFromGraphBuf(BitStream);
        if (size==0) return 0;
@@ -1422,7 +1442,6 @@ int CmdFSKdemodPyramid(const char *Cmd)
        uint32_t fc = 0;
        uint32_t cardnum = 0;
        uint32_t code1 = 0;
-       //uint32_t code2 = 0;
        if (fmtLen==26){
                fc = bytebits_to_byte(BitStream+73, 8);
                cardnum = bytebits_to_byte(BitStream+81, 16);
@@ -1943,26 +1962,14 @@ int CmdHpf(const char *Cmd)
        RepaintGraphWindow();
        return 0;
 }
-typedef struct {
-       uint8_t * buffer;
-       uint32_t numbits;
-       uint32_t position;
-}BitstreamOut;
-
-bool _headBit( BitstreamOut *stream)
-{
-       int bytepos = stream->position >> 3; // divide by 8
-       int bitpos = (stream->position++) & 7; // mask out 00000111
-       return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
-}
 
-uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b)
+uint8_t getByte(uint8_t bits_per_sample, BitstreamIn* b)
 {
        int i;
        uint8_t val = 0;
        for(i =0 ; i < bits_per_sample; i++)
        {
-               val |= (_headBit(b) << (7-i));
+               val |= (headBit(b) << (7-i));
        }
        return val;
 }
@@ -2002,7 +2009,7 @@ int getSamples(const char *Cmd, bool silent)
        if(bits_per_sample < 8)
        {
                PrintAndLog("Unpacking...");
-               BitstreamOut bout = { got, bits_per_sample * n,  0};
+               BitstreamIn bout = { got, bits_per_sample * n,  0};
                int j =0;
                for (j = 0; j * bits_per_sample < n * 8 && j < n; j++) {
                        uint8_t sample = getByte(bits_per_sample, &bout);
@@ -2265,14 +2272,109 @@ int CmdZerocrossings(const char *Cmd)
        return 0;
 }
 
+int usage_data_bin2hex(){
+               PrintAndLog("Usage: data bin2hex <binary_digits>");
+               PrintAndLog("       This function will ignore all characters not 1 or 0 (but stop reading on whitespace)");
+               return 0;
+}
+
+/**
+ * @brief Utility for conversion via cmdline.
+ * @param Cmd
+ * @return
+ */
+int Cmdbin2hex(const char *Cmd)
+{
+       int bg =0, en =0;
+       if(param_getptr(Cmd, &bg, &en, 0))
+       {
+               return usage_data_bin2hex();
+       }
+       //Number of digits supplied as argument
+       size_t length = en  - bg +1;
+       size_t bytelen = (length+7) / 8;
+       uint8_t* arr = (uint8_t *) malloc(bytelen);
+       memset(arr, 0, bytelen);
+       BitstreamOut bout = { arr, 0, 0 };
+
+       for(; bg <= en ;bg++)
+       {
+               char c = Cmd[bg];
+               if( c == '1')   pushBit(&bout, 1);
+               else if( c == '0')      pushBit(&bout, 0);
+               else PrintAndLog("Ignoring '%c'", c);
+       }
+
+       if(bout.numbits % 8 != 0)
+       {
+               printf("[padded with %d zeroes]\n", 8-(bout.numbits % 8));
+       }
+
+       //Uses printf instead of PrintAndLog since the latter
+       // adds linebreaks to each printout - this way was more convenient since we don't have to
+       // allocate a string and write to that first...
+       for(size_t x = 0; x  < bytelen ; x++)
+       {
+               printf("%02X", arr[x]);
+       }
+       printf("\n");
+       free(arr);
+       return 0;
+}
+
+int usage_data_hex2bin(){
+
+       PrintAndLog("Usage: data bin2hex <binary_digits>");
+       PrintAndLog("       This function will ignore all non-hexadecimal characters (but stop reading on whitespace)");
+       return 0;
+
+}
+
+int Cmdhex2bin(const char *Cmd)
+{
+       int bg =0, en =0;
+       if(param_getptr(Cmd, &bg, &en, 0))
+       {
+               return usage_data_hex2bin();
+       }
+
+
+       while(bg <= en )
+       {
+               char x = Cmd[bg++];
+               // capitalize
+               if (x >= 'a' && x <= 'f')
+                       x -= 32;
+               // convert to numeric value
+               if (x >= '0' && x <= '9')
+                       x -= '0';
+               else if (x >= 'A' && x <= 'F')
+                       x -= 'A' - 10;
+               else
+                       continue;
+
+               //Uses printf instead of PrintAndLog since the latter
+               // adds linebreaks to each printout - this way was more convenient since we don't have to
+               // allocate a string and write to that first...
+
+               for(int i= 0 ; i < 4 ; ++i)
+                       printf("%d",(x >> (3 - i)) & 1);
+       }
+       printf("\n");
+
+       return 0;
+}
+
 static command_t CommandTable[] =
 {
        {"help",            CmdHelp,            1, "This help"},
        {"askedgedetect",   CmdAskEdgeDetect,   1, "[threshold] Adjust Graph for manual ask demod using the length of sample differences to detect the edge of a wave (use 20-45, def:25)"},
        {"askem410xdemod",  CmdAskEM410xDemod,  1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
        {"askgproxiidemod", CmdG_Prox_II_Demod, 1, "Demodulate a G Prox II tag from GraphBuffer"},
+       {"askvikingdemod",  CmdVikingDemod,     1, "Demodulate a Viking tag from GraphBuffer"},
        {"autocorr",        CmdAutoCorr,        1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"},
        {"biphaserawdecode",CmdBiphaseDecodeRaw,1, "[offset] [invert<0|1>] [maxErr] -- Biphase decode bin stream in DemodBuffer (offset = 0|1 bits to shift the decode start)"},
+       {"bin2hex",         Cmdbin2hex,         1, "bin2hex <digits>     -- Converts binary to hexadecimal"},
        {"bitsamples",      CmdBitsamples,      0, "Get raw samples as bitstring"},
        {"buffclear",       CmdBuffClear,       1, "Clear sample buffer and graph window"},
        {"dec",             CmdDec,             1, "Decimate samples"},
@@ -2287,6 +2389,7 @@ static command_t CommandTable[] =
        {"getbitstream",    CmdGetBitStream,    1, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"},
        {"grid",            CmdGrid,            1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
        {"hexsamples",      CmdHexsamples,      0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
+       {"hex2bin",         Cmdhex2bin,         1, "hex2bin <hexadecimal> -- Converts hexadecimal to binary"},
        {"hide",            CmdHide,            1, "Hide graph window"},
        {"hpf",             CmdHpf,             1, "Remove DC offset from trace"},
        {"load",            CmdLoad,            1, "<filename> -- Load trace (to graph window"},
Impressum, Datenschutz