mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-21 05:43:23 -07:00
Refactor parity functions
- get rid of __asm function in crapto1.h, use gcc builtin function instead - make parity functions available in common directory
This commit is contained in:
parent
f513388ee0
commit
1f065e1dad
14 changed files with 127 additions and 90 deletions
|
@ -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];
|
||||
|
@ -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
|
||||
|
@ -486,11 +488,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;
|
||||
|
|
|
@ -53,7 +53,7 @@ int nonce_distance(uint32_t from, uint32_t to);
|
|||
int __i;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ parity(__M & 0xFF01))\
|
||||
if(BIT(FILTER, __i) ^ evenparity32(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
|
@ -63,24 +63,6 @@ int nonce_distance(uint32_t from, uint32_t to);
|
|||
#define LF_POLY_EVEN (0x870804)
|
||||
#define BIT(x, n) ((x) >> (n) & 1)
|
||||
#define BEBIT(x, n) BIT(x, (n) ^ 24)
|
||||
static inline int parity(uint32_t x)
|
||||
{
|
||||
#if !defined __i386__ || !defined __GNUC__
|
||||
x ^= x >> 16;
|
||||
x ^= x >> 8;
|
||||
x ^= x >> 4;
|
||||
return BIT(0x6996, x & 0xf);
|
||||
#else
|
||||
__asm( "movl %1, %%eax\n"
|
||||
"mov %%ax, %%cx\n"
|
||||
"shrl $0x10, %%eax\n"
|
||||
"xor %%ax, %%cx\n"
|
||||
"xor %%ch, %%cl\n"
|
||||
"setpo %%al\n"
|
||||
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax","ecx");
|
||||
return x;
|
||||
#endif
|
||||
}
|
||||
static inline int filter(uint32_t const x)
|
||||
{
|
||||
uint32_t f;
|
||||
|
|
|
@ -18,7 +18,9 @@
|
|||
Copyright (C) 2008-2008 bla <blapost@gmail.com>
|
||||
*/
|
||||
#include "crapto1.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "parity.h"
|
||||
|
||||
#define SWAPENDIAN(x)\
|
||||
(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
|
||||
|
@ -73,7 +75,7 @@ uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
|||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | parity(feedin);
|
||||
s->even = s->even << 1 | evenparity32(feedin);
|
||||
|
||||
t = s->odd, s->odd = s->even, s->even = t;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue