X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/fb2d24882e31c4a9915a87e28081c3f7d6f3bea4..9b98cb88b202b316f7db63266ff8f986d404b7e1:/armsrc/crapto1.c diff --git a/armsrc/crapto1.c b/armsrc/crapto1.c index 2fdeb924..4b4e240e 100644 --- a/armsrc/crapto1.c +++ b/armsrc/crapto1.c @@ -33,8 +33,7 @@ static void __attribute__((constructor)) fill_lut() static void quicksort(uint32_t* const start, uint32_t* const stop) { - uint32_t *it = start + 1, *rit = stop; - uint32_t tmp; + uint32_t *it = start + 1, *rit = stop, t; if(it > rit) return; @@ -44,19 +43,13 @@ static void quicksort(uint32_t* const start, uint32_t* const stop) ++it; else if(*rit > *start) --rit; - else { - tmp = *it; - *it = *rit; - *rit = tmp; - } + else + t = *it, *it = *rit, *rit = t; if(*rit >= *start) --rit; - if(rit != start) { - tmp = *rit; - *rit = *start; - *start = tmp; - } + if(rit != start) + t = *rit, *rit = *start, *start = t; quicksort(start, rit - 1); quicksort(rit + 1, stop); @@ -92,8 +85,7 @@ update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2) /** extend_table * using a bit of the keystream extend the table of possible lfsr states */ -static inline void -extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in) +static inline void extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in) { in <<= 24; for(*tbl <<= 1; tbl <= *end; *++tbl <<= 1) @@ -326,12 +318,10 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb) { int out; uint8_t ret; - uint32_t tmp; + uint32_t t; s->odd &= 0xffffff; - tmp = s->odd; - s->odd = s->even; - s->even = tmp; + t = s->odd, s->odd = s->even, s->even = t; out = s->even & 1; out ^= LF_POLY_EVEN & (s->even >>= 1); @@ -347,9 +337,21 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb) */ uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb) { + /* int i, ret = 0; for (i = 7; i >= 0; --i) ret |= lfsr_rollback_bit(s, BIT(in, i), fb) << i; +*/ +// unfold loop 20160112 + uint8_t ret = 0; + ret |= lfsr_rollback_bit(s, BIT(in, 7), fb) << 7; + ret |= lfsr_rollback_bit(s, BIT(in, 6), fb) << 6; + ret |= lfsr_rollback_bit(s, BIT(in, 5), fb) << 5; + ret |= lfsr_rollback_bit(s, BIT(in, 4), fb) << 4; + ret |= lfsr_rollback_bit(s, BIT(in, 3), fb) << 3; + ret |= lfsr_rollback_bit(s, BIT(in, 2), fb) << 2; + ret |= lfsr_rollback_bit(s, BIT(in, 1), fb) << 1; + ret |= lfsr_rollback_bit(s, BIT(in, 0), fb) << 0; return ret; } /** lfsr_rollback_word @@ -357,10 +359,49 @@ uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb) */ uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb) { + /* int i; uint32_t ret = 0; for (i = 31; i >= 0; --i) ret |= lfsr_rollback_bit(s, BEBIT(in, i), fb) << (i ^ 24); +*/ +// unfold loop 20160112 + uint32_t ret = 0; + ret |= lfsr_rollback_bit(s, BEBIT(in, 31), fb) << (31 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 30), fb) << (30 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 29), fb) << (29 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 28), fb) << (28 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 27), fb) << (27 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 26), fb) << (26 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 25), fb) << (25 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 24), fb) << (24 ^ 24); + + ret |= lfsr_rollback_bit(s, BEBIT(in, 23), fb) << (23 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 22), fb) << (22 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 21), fb) << (21 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 20), fb) << (20 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 19), fb) << (19 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 18), fb) << (18 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 17), fb) << (17 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 16), fb) << (16 ^ 24); + + ret |= lfsr_rollback_bit(s, BEBIT(in, 15), fb) << (15 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 14), fb) << (14 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 13), fb) << (13 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 12), fb) << (12 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 11), fb) << (11 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 10), fb) << (10 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 9), fb) << (9 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 8), fb) << (8 ^ 24); + + ret |= lfsr_rollback_bit(s, BEBIT(in, 7), fb) << (7 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 6), fb) << (6 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 5), fb) << (5 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 4), fb) << (4 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 3), fb) << (3 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 2), fb) << (2 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 1), fb) << (1 ^ 24); + ret |= lfsr_rollback_bit(s, BEBIT(in, 0), fb) << (0 ^ 24); return ret; } @@ -387,6 +428,8 @@ int nonce_distance(uint32_t from, uint32_t to) static uint32_t fastfwd[2][8] = { { 0, 0x4BC53, 0xECB1, 0x450E2, 0x25E29, 0x6E27A, 0x2B298, 0x60ECB}, { 0, 0x1D962, 0x4BC53, 0x56531, 0xECB1, 0x135D3, 0x450E2, 0x58980}}; + + /** lfsr_prefix_ks * * Is an exported helper function from the common prefix attack @@ -396,10 +439,12 @@ static uint32_t fastfwd[2][8] = { * encrypt the NACK which is observed when varying only the 3 last bits of Nr * only correct iff [NR_3] ^ NR_3 does not depend on Nr_3 */ + // TO VERIFY uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd) { - uint32_t c, entry, *candidates = malloc(4 << 10); - int i, size = 0, good; + uint32_t *candidates = malloc(4 << 10); + uint32_t c, entry; + int size = 0, i, good; if(!candidates) return 0; @@ -455,6 +500,12 @@ check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8], /** lfsr_common_prefix * Implentation of the common prefix attack. + * Requires the 28 bit constant prefix used as reader nonce (pfx) + * The reader response used (rr) + * The keystream used to encrypt the observed NACK's (ks) + * The parity bits (par) + * It returns a zero terminated list of possible cipher states after the + * tag nonce was fed in */ struct Crypto1State* lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]) @@ -468,8 +519,9 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]) s = statelist = malloc((sizeof *statelist) << 20); if(!s || !odd || !even) { free(statelist); - statelist = 0; - goto out; + free(odd); + free(even); + return 0; } for(o = odd; *o + 1; ++o) @@ -481,8 +533,9 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]) } s->odd = s->even = 0; -out: + free(odd); free(even); + return statelist; }