diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 15a6a122c..59907a3e0 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -2503,7 +2503,7 @@ void ReaderMifare(bool first_try, uint8_t block, uint8_t keytype ) { if (nt_diff == 0 && first_try) par_low = par[0] & 0xE0; // there is no need to check all parities for other nt_diff. Parity Bits for mf_nr_ar[0..2] won't change - par_list[nt_diff] = SwapBits(par[0], 8); + par_list[nt_diff] = reflect8(par[0]); ks_list[nt_diff] = receivedAnswer[0] ^ 0x05; // xor with NACK value to get keystream // Test if the information is complete diff --git a/armsrc/util.c b/armsrc/util.c index 6c3c06a34..454050a6a 100644 --- a/armsrc/util.c +++ b/armsrc/util.c @@ -13,14 +13,19 @@ size_t nbytes(size_t nbits) { return (nbits >> 3)+((nbits % 8) > 0); } -uint32_t SwapBits(uint32_t value, int nrbits) { +// Swap bit order on a uint32_t value. Can be limited by 'b' just use say 8 bits reversal +// note: function clears the rest of the bits. +uint32_t SwapBits(uint32_t v, int b) { uint32_t newvalue = 0; - for(int i = 0; i < nrbits; i++) { - newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i); + for(int i = 0; i < b; i++) { + newvalue ^= ((v >> i) & 1) << (b - 1 - i); } return newvalue; } +uint8_t reflect8(uint8_t b) { + return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; +} /* ref http://www.csm.ornl.gov/~dunigan/crc.html Returns the value v with the bottom b [0,32] bits reflected. diff --git a/armsrc/util.h b/armsrc/util.h index c68a1d51a..32d5efcc5 100644 --- a/armsrc/util.h +++ b/armsrc/util.h @@ -42,6 +42,10 @@ size_t nbytes(size_t nbits); uint32_t SwapBits(uint32_t value, int nrbits); uint32_t reflect(uint32_t v, int b); + +// dedicated 8bit reversal +uint8_t reflect8(uint8_t b); + void num_to_bytes(uint64_t n, size_t len, uint8_t* dest); uint64_t bytes_to_num(uint8_t* src, size_t len); void rol(uint8_t *data, const size_t len); diff --git a/client/util.c b/client/util.c index b555352cd..cd1039038 100644 --- a/client/util.c +++ b/client/util.c @@ -729,15 +729,18 @@ void rol(uint8_t *data, const size_t len){ data[len-1] = first; } -// Swap bit order on a uint32_t value. Can be limited by nrbits just use say 8bits reversal -// And clears the rest of the bits. -uint32_t SwapBits(uint32_t value, int nrbits) { +// Swap bit order on a uint32_t value. Can be limited by 'b' just use say 8 bits reversal +// note: function clears the rest of the bits. +uint32_t SwapBits(uint32_t v, int b) { uint32_t newvalue = 0; - for(int i = 0; i < nrbits; i++) { - newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i); + for(int i = 0; i < b; i++) { + newvalue ^= ((v >> i) & 1) << (b - 1 - i); } return newvalue; } +uint8_t reflect8(uint8_t b) { + return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; +} /* ref http://www.csm.ornl.gov/~dunigan/crc.html Returns the value v with the bottom b [0,32] bits reflected. diff --git a/client/util.h b/client/util.h index 17fbd7bcb..04cbf7d5b 100644 --- a/client/util.h +++ b/client/util.h @@ -204,6 +204,7 @@ extern uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits); extern void rol(uint8_t *data, const size_t len); extern uint32_t SwapBits(uint32_t value, int nrbits); extern uint32_t reflect(uint32_t v, int b); +extern uint8_t reflect8(uint8_t b); // dedicated 8bit reversal extern uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor); extern int num_CPUs(void); // number of logical CPUs