]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/nonce2key/crapto1.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / client / nonce2key / crapto1.c
index 1015e27a74b2a866078cb0ca350113081bd95baa..13c4c063af078fb4d155b7f227d26eda6b8fa9c9 100644 (file)
@@ -1,21 +1,21 @@
 /*  crapto1.c\r
 \r
-       This program is free software; you can redistribute it and/or\r
-       modify it under the terms of the GNU General Public License\r
-       as published by the Free Software Foundation; either version 2\r
-       of the License, or (at your option) any later version.\r
-\r
-       This program is distributed in the hope that it will be useful,\r
-       but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-       GNU General Public License for more details.\r
-\r
-       You should have received a copy of the GNU General Public License\r
-       along with this program; if not, write to the Free Software\r
-       Foundation, Inc., 51 Franklin Street, Fifth Floor,\r
-       Boston, MA  02110-1301, US$\r
-\r
-       Copyright (C) 2008-2008 bla <blapost@gmail.com>\r
+    This program is free software; you can redistribute it and/or\r
+    modify it under the terms of the GNU General Public License\r
+    as published by the Free Software Foundation; either version 2\r
+    of the License, or (at your option) any later version.\r
+\r
+    This program is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+    GNU General Public License for more details.\r
+\r
+    You should have received a copy of the GNU General Public License\r
+    along with this program; if not, write to the Free Software\r
+    Foundation, Inc., 51 Franklin Street, Fifth Floor,\r
+    Boston, MA  02110-1301, US$\r
+\r
+    Copyright (C) 2008-2008 bla <blapost@gmail.com>\r
 */\r
 #include "crapto1.h"\r
 #include <stdlib.h>\r
@@ -24,9 +24,9 @@
 static uint8_t filterlut[1 << 20];\r
 static void __attribute__((constructor)) fill_lut()\r
 {\r
-               uint32_t i;\r
-               for(i = 0; i < 1 << 20; ++i)\r
-                               filterlut[i] = filter(i);\r
+        uint32_t i;\r
+        for(i = 0; i < 1 << 20; ++i)\r
+                filterlut[i] = filter(i);\r
 }\r
 #define filter(x) (filterlut[(x) & 0xfffff])\r
 #endif\r
@@ -147,7 +147,7 @@ extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in
                        *p ^= in;\r
                } else {                                                                                // drop\r
                        *p-- = *(*end)--;\r
-               }\r
+       }\r
        }\r
 \r
 }\r
@@ -209,7 +209,7 @@ recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
 \r
        for (int i = bucket_info.numbuckets - 1; i >= 0; i--) {\r
                sl = recover(bucket_info.bucket_info[1][i].head, bucket_info.bucket_info[1][i].tail, oks,\r
-                                        bucket_info.bucket_info[0][i].head, bucket_info.bucket_info[0][i].tail, eks,\r
+                                    bucket_info.bucket_info[0][i].head, bucket_info.bucket_info[0][i].tail, eks,\r
                                         rem, sl, in, bucket);\r
        }\r
 \r
@@ -251,7 +251,6 @@ struct Crypto1State* lfsr_recovery32(uint32_t ks2, uint32_t in)
                        }\r
                }\r
 \r
-\r
        // initialize statelists: add all possible states which would result into the rightmost 2 bits of the keystream\r
        for(i = 1 << 20; i >= 0; --i) {\r
                if(filter(i) == (oks & 1))\r
@@ -272,9 +271,7 @@ struct Crypto1State* lfsr_recovery32(uint32_t ks2, uint32_t in)
 \r
        in = (in >> 16 & 0xff) | (in << 16) | (in & 0xff00);            // Byte swapping\r
 \r
-       recover(odd_head, odd_tail, oks,\r
-               even_head, even_tail, eks, 11, statelist, in << 1, bucket);\r
-\r
+       recover(odd_head, odd_tail, oks, even_head, even_tail, eks, 11, statelist, in << 1, bucket);\r
 \r
 out:\r
        free(odd_head);\r
@@ -484,7 +481,7 @@ uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
  */\r
 static struct Crypto1State*\r
 brute_top(uint32_t prefix, uint32_t rresp, unsigned char parities[8][8],\r
-                 uint32_t odd, uint32_t even, struct Crypto1State* sl, uint8_t no_chk)\r
+          uint32_t odd, uint32_t even, struct Crypto1State* sl, uint8_t no_chk)\r
 {\r
        struct Crypto1State s;\r
        uint32_t ks1, nr, ks2, rr, ks3, good, c;\r
@@ -536,8 +533,7 @@ brute_top(uint32_t prefix, uint32_t rresp, unsigned char parities[8][8],
  * It returns a zero terminated list of possible cipher states after the\r
  * tag nonce was fed in\r
  */\r
-struct Crypto1State*\r
-lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint8_t no_par)\r
+struct Crypto1State* lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint8_t no_par)\r
 {\r
        struct Crypto1State *statelist, *s;\r
        uint32_t *odd, *even, *o, *e, top;\r
@@ -548,10 +544,10 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8],
        statelist = malloc((sizeof *statelist) << 21);  //how large should be?\r
        if(!statelist || !odd || !even)\r
        {\r
-                               free(statelist);\r
-                               free(odd);\r
-                               free(even);\r
-                               return 0;\r
+               free(statelist);\r
+               free(odd);\r
+               free(even);\r
+               return 0;\r
        }\r
 \r
        s = statelist;\r
@@ -571,3 +567,66 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8],
 \r
        return statelist;\r
 }\r
+\r
+/*\r
+struct Crypto1State* lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint8_t no_par, uint32_t nt, uint32_t uid)\r
+{\r
+    long long int amount = 0;\r
+    struct Crypto1State *statelist, *s;\r
+    uint32_t *odd, *even, *o, *e, top;\r
+\r
+    odd = lfsr_prefix_ks(ks, 1);\r
+    even = lfsr_prefix_ks(ks, 0);\r
+\r
+    s = statelist = malloc((sizeof *statelist) << 20);\r
+    if(!s || !odd || !even) {\r
+               free(odd);\r
+               free(even);\r
+               free(statelist);\r
+               return 0;\r
+    }\r
+\r
+    char filename[50] = "archivo.txt";\r
+    sprintf(filename, "logs/%x.txt", nt);\r
+    PrintAndLog("Name: %s\n", filename);\r
+    FILE *file = fopen(filename,"w+");\r
+       if ( !file ) {\r
+               s->odd = s->even = 0;\r
+               free(odd);\r
+               free(even);\r
+               PrintAndLog("Failed to create file");\r
+               return 0;\r
+       }\r
+    PrintAndLog("Creating file... ");\r
+       uint32_t xored = uid^nt;\r
+       \r
+    int lastOdd = 0;\r
+    for(o = odd; *o + 1; ++o)\r
+        for(e = even; *e + 1; ++e)\r
+            for(top = 0; top < 64; ++top) {\r
+                *o += 1 << 21;\r
+                *e += (!(top & 7) + 1) << 21;\r
+\r
+                //added by MG\r
+                if(lastOdd != statelist->odd){\r
+                                       // Here I create a temporal crypto1 state, \r
+                                       // where I load the odd and even state and work with it,\r
+                                       // in order not to interfere with regular mechanism, This is what I save to file\r
+                                       struct Crypto1State *state;\r
+                    lastOdd = state->odd = statelist->odd; state->even = statelist->even;\r
+                    lfsr_rollback_word(state,xored,0);\r
+                    fprintf(file,"%x %x \n",state->odd,state->even);\r
+                    amount++;\r
+                }\r
+                //s = check_pfx_parity(pfx, rr, par, *o, *e, s); //This is not useful at all when attacking chineese cards\r
+                               s = brute_top(pfx, rr, par, *o, *e, s, no_par); \r
+            }\r
+\r
+       PrintAndLog("File created, amount %u\n",amount);\r
+       fclose(file);\r
+       s->odd = s->even = 0;\r
+       free(odd);\r
+       free(even);\r
+    return statelist;\r
+}\r
+ */\r
Impressum, Datenschutz