/* 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
\r
#if !defined LOWMEM && defined __GNUC__\r
-static uint8_t filterlut[1 << 20];\r
+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 x;\r
+ uint32_t f;\r
+ for(x = 0; x < 1 << 20; ++x) {\r
+ f = 0xf22c0 >> (x & 0xf) & 16;\r
+ f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;\r
+ f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;\r
+ f |= 0x1e458 >> (x >> 12 & 0xf) & 2;\r
+ f |= 0x0d938 >> (x >> 16 & 0xf) & 1;\r
+ filterlut[x] = BIT(0xEC57E80A, f);\r
+ }\r
}\r
-#define filter(x) (filterlut[(x) & 0xfffff])\r
#endif\r
\r
\r
*p ^= in;\r
} else { // drop\r
*p-- = *(*end)--;\r
- }\r
+ }\r
}\r
\r
}\r
\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
}\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
\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
*/\r
void lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb)\r
{\r
- int i;\r
+/* int i;\r
for (i = 7; i >= 0; --i)\r
lfsr_rollback_bit(s, BEBIT(in, i), fb);\r
+*/\r
+// unfold loop 20160112\r
+ lfsr_rollback_bit(s, BEBIT(in, 7), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 6), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 5), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 4), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 3), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 2), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 1), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 0), fb);\r
}\r
/** lfsr_rollback_word\r
* Rollback the shift register in order to get previous states\r
*/\r
void lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb)\r
{\r
+/*\r
int i;\r
for (i = 31; i >= 0; --i)\r
lfsr_rollback_bit(s, BEBIT(in, i), fb);\r
+*/\r
+// unfold loop 20160112\r
+ lfsr_rollback_bit(s, BEBIT(in, 31), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 30), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 29), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 28), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 27), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 26), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 25), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 24), fb);\r
+\r
+ lfsr_rollback_bit(s, BEBIT(in, 23), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 22), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 21), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 20), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 19), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 18), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 17), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 16), fb);\r
+\r
+ lfsr_rollback_bit(s, BEBIT(in, 15), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 14), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 13), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 12), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 11), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 10), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 9), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 8), fb);\r
+\r
+ lfsr_rollback_bit(s, BEBIT(in, 7), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 6), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 5), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 4), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 3), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 2), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 1), fb);\r
+ lfsr_rollback_bit(s, BEBIT(in, 0), fb);\r
}\r
\r
/** nonce_distance\r
*/\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
* 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
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
\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