]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/crapto1/crapto1.c
revert field on/off in hf 14a reader
[proxmark3-svn] / common / crapto1 / crapto1.c
index 82c5b65bc2a8e32ca5e38e117192cefa24a0f59c..1edfca1b9bb535d56a1c450e202f394114424629 100644 (file)
@@ -18,7 +18,9 @@
     Copyright (C) 2008-2014 bla <blapost@gmail.com>
 */
 #include "crapto1.h"
+
 #include <stdlib.h>
+#include "parity.h"
 
 #if !defined LOWMEM && defined __GNUC__
 static uint8_t filterlut[1 << 20];
@@ -97,7 +99,7 @@ static void bucket_sort_intersect(uint32_t* const estart, uint32_t* const estop,
 /** binsearch
  * Binary search for the first occurence of *stop's MSB in sorted [start,stop]
  */
-static inline uint32_t* binsearch(uint32_t *start, uint32_t *stop)
+/* static inline uint32_t* binsearch(uint32_t *start, uint32_t *stop)
 {
        uint32_t mid, val = *stop & 0xff000000;
        while(start != stop)
@@ -108,7 +110,7 @@ static inline uint32_t* binsearch(uint32_t *start, uint32_t *stop)
 
        return start;
 }
-
+ */
 /** update_contribution
  * helper, calculates the partial linear feedback contributions and puts in MSB
  */
@@ -117,8 +119,8 @@ update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2)
 {
        uint32_t p = *item >> 25;
 
-       p = p << 1 | parity(*item & mask1);
-       p = p << 1 | parity(*item & mask2);
+       p = p << 1 | evenparity32(*item & mask1);
+       p = p << 1 | evenparity32(*item & mask2);
        *item = p << 24 | (*item & 0xffffff);
 }
 
@@ -174,10 +176,10 @@ recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
 
        if(rem == -1) {
                for(e = e_head; e <= e_tail; ++e) {
-                       *e = *e << 1 ^ parity(*e & LF_POLY_EVEN) ^ !!(in & 4);
+                       *e = *e << 1 ^ evenparity32(*e & LF_POLY_EVEN) ^ !!(in & 4);
                        for(o = o_head; o <= o_tail; ++o, ++sl) {
                                sl->even = *o;
-                               sl->odd = *e ^ parity(*o & LF_POLY_ODD);
+                               sl->odd = *e ^ evenparity32(*o & LF_POLY_ODD);
                                sl[1].odd = sl[1].even = 0;
                        }
                }
@@ -329,30 +331,30 @@ struct Crypto1State* lfsr_recovery64(uint32_t ks2, uint32_t ks3)
                        continue;
 
                for(j = 0; j < 19; ++j)
-                       low = low << 1 | parity(i & S1[j]);
+                       low = low << 1 | evenparity32(i & S1[j]);
                for(j = 0; j < 32; ++j)
-                       hi[j] = parity(i & T1[j]);
+                       hi[j] = evenparity32(i & T1[j]);
 
                for(; tail >= table; --tail) {
                        for(j = 0; j < 3; ++j) {
                                *tail = *tail << 1;
-                               *tail |= parity((i & C1[j]) ^ (*tail & C2[j]));
+                               *tail |= evenparity32((i & C1[j]) ^ (*tail & C2[j]));
                                if(filter(*tail) != oks[29 + j])
                                        goto continue2;
                        }
 
                        for(j = 0; j < 19; ++j)
-                               win = win << 1 | parity(*tail & S2[j]);
+                               win = win << 1 | evenparity32(*tail & S2[j]);
 
                        win ^= low;
                        for(j = 0; j < 32; ++j) {
-                               win = win << 1 ^ hi[j] ^ parity(*tail & T2[j]);
+                               win = win << 1 ^ hi[j] ^ evenparity32(*tail & T2[j]);
                                if(filter(win) != eks[j])
                                        goto continue2;
                        }
 
-                       *tail = *tail << 1 | parity(LF_POLY_EVEN & *tail);
-                       sl->odd = *tail ^ parity(LF_POLY_ODD & win);
+                       *tail = *tail << 1 | evenparity32(LF_POLY_EVEN & *tail);
+                       sl->odd = *tail ^ evenparity32(LF_POLY_ODD & win);
                        sl->even = win;
                        ++sl;
                        sl->odd = sl->even = 0;
@@ -380,7 +382,7 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
        out ^= !!in;
        out ^= (ret = filter(s->odd)) & !!fb;
 
-       s->even |= parity(out) << 23;
+       s->even |= evenparity32(out) << 23;
        return ret;
 }
 /** lfsr_rollback_byte
@@ -424,7 +426,6 @@ int nonce_distance(uint32_t from, uint32_t to)
        return (65535 + dist[to >> 16] - dist[from >> 16]) % 65535;
 }
 
-
 static uint32_t fastfwd[2][8] = {
        { 0, 0x4BC53, 0xECB1, 0x450E2, 0x25E29, 0x6E27A, 0x2B298, 0x60ECB},
        { 0, 0x1D962, 0x4BC53, 0x56531, 0xECB1, 0x135D3, 0x450E2, 0x58980}};
@@ -465,18 +466,9 @@ uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
  */
 static struct Crypto1State*
 check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
-               uint32_t odd, uint32_t even, struct Crypto1State* sl)
+               uint32_t odd, uint32_t even, struct Crypto1State* sl, uint32_t no_par)
 {
-       uint32_t ks1, nr, ks2, rr, ks3, c, good = 1, no_par = 1;
-
-       for (int i = 0; i < 8; i++) {
-               for (int j = 0; j < 8; j++) {
-                       if (parities[i][j] != 0) {
-                               no_par = 0;
-                               break;
-                       }
-               }
-       }
+       uint32_t ks1, nr, ks2, rr, ks3, c, good = 1;
 
        for(c = 0; good && c < 8; ++c) {
                sl->odd = odd ^ fastfwd[1][c];
@@ -495,11 +487,11 @@ check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
                nr = ks1 ^ (prefix | c << 5);
                rr = ks2 ^ rresp;
 
-               good &= parity(nr & 0x000000ff) ^ parities[c][3] ^ BIT(ks2, 24);
-               good &= parity(rr & 0xff000000) ^ parities[c][4] ^ BIT(ks2, 16);
-               good &= parity(rr & 0x00ff0000) ^ parities[c][5] ^ BIT(ks2,  8);
-               good &= parity(rr & 0x0000ff00) ^ parities[c][6] ^ BIT(ks2,  0);
-               good &= parity(rr & 0x000000ff) ^ parities[c][7] ^ ks3;
+               good &= evenparity32(nr & 0x000000ff) ^ parities[c][3] ^ BIT(ks2, 24);
+               good &= evenparity32(rr & 0xff000000) ^ parities[c][4] ^ BIT(ks2, 16);
+               good &= evenparity32(rr & 0x00ff0000) ^ parities[c][5] ^ BIT(ks2,  8);
+               good &= evenparity32(rr & 0x0000ff00) ^ parities[c][6] ^ BIT(ks2,  0);
+               good &= evenparity32(rr & 0x000000ff) ^ parities[c][7] ^ ks3;
        }
 
        return sl + good;
@@ -510,7 +502,7 @@ check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
  * Implentation of the common prefix attack.
  */
 struct Crypto1State*
-lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8])
+lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint32_t no_par)
 {
        struct Crypto1State *statelist, *s;
        uint32_t *odd, *even, *o, *e, top;
@@ -518,7 +510,7 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8])
        odd = lfsr_prefix_ks(ks, 1);
        even = lfsr_prefix_ks(ks, 0);
 
-       s = statelist = malloc((sizeof *statelist) << 20);
+       s = statelist = malloc((sizeof *statelist) << 22); // was << 20. Need more for no_par special attack. Enough???
        if(!s || !odd || !even) {
                free(statelist);
                statelist = 0;
@@ -530,7 +522,7 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8])
                        for(top = 0; top < 64; ++top) {
                                *o += 1 << 21;
                                *e += (!(top & 7) + 1) << 21;
-                               s = check_pfx_parity(pfx, rr, par, *o, *e, s);
+                               s = check_pfx_parity(pfx, rr, par, *o, *e, s, no_par);
                        }
 
        s->odd = s->even = 0;
Impressum, Datenschutz