mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-20 13:23:51 -07:00
CHG: merged the forum user @jason 's fixes to LEGIC. *UNTESTED*
CHG: changed the CRC implementations.
This commit is contained in:
parent
83dad64b91
commit
3e134b4c20
16 changed files with 1368 additions and 163 deletions
|
@ -1,6 +1,6 @@
|
|||
#include "bucketsort.h"
|
||||
|
||||
void bucket_sort_intersect(uint32_t* const estart, uint32_t* const estop,
|
||||
extern void bucket_sort_intersect(uint32_t* const estart, uint32_t* const estop,
|
||||
uint32_t* const ostart, uint32_t* const ostop,
|
||||
bucket_info_t *bucket_info, bucket_array_t bucket)
|
||||
{
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#ifndef BUCKETSORT_H__
|
||||
#define BUCKETSORT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct bucket {
|
||||
uint32_t *head;
|
||||
uint32_t *bp;
|
||||
|
|
136
common/crc.c
136
common/crc.c
|
@ -5,66 +5,136 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Generic CRC calculation code.
|
||||
//-----------------------------------------------------------------------------
|
||||
// the Check value below in the comments is CRC of the string '123456789'
|
||||
//
|
||||
#include "crc.h"
|
||||
#include "util.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor)
|
||||
{
|
||||
void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout) {
|
||||
crc_init(crc, order, polynom, initial_value, final_xor);
|
||||
crc->refin = refin;
|
||||
crc->refout = refout;
|
||||
crc_clear(crc);
|
||||
}
|
||||
|
||||
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor) {
|
||||
crc->order = order;
|
||||
crc->topbit = BITMASK( order-1 );
|
||||
crc->polynom = polynom;
|
||||
crc->initial_value = initial_value;
|
||||
crc->final_xor = final_xor;
|
||||
crc->mask = (1L<<order)-1;
|
||||
crc->refin = FALSE;
|
||||
crc->refout = FALSE;
|
||||
crc_clear(crc);
|
||||
}
|
||||
|
||||
void crc_update(crc_t *crc, uint32_t data, int data_width)
|
||||
{
|
||||
for( int i=0; i < data_width; i++) {
|
||||
int oldstate = crc->state;
|
||||
crc->state = crc->state >> 1;
|
||||
if( (oldstate^data) & 1 ) {
|
||||
crc->state ^= crc->polynom;
|
||||
}
|
||||
data >>= 1;
|
||||
void crc_clear(crc_t *crc) {
|
||||
crc->state = crc->initial_value & crc->mask;
|
||||
if (crc->refin)
|
||||
crc->state = reflect(crc->state, crc->order);
|
||||
}
|
||||
|
||||
void crc_update(crc_t *crc, uint32_t indata, int data_width){
|
||||
|
||||
//reflected
|
||||
if (crc->refin) indata = reflect(indata, data_width);
|
||||
|
||||
// Bring the next byte into the remainder.
|
||||
crc->state ^= indata << (crc->order - data_width);
|
||||
|
||||
for( uint8_t bit = data_width; bit > 0; --bit) {
|
||||
// Try to divide the current data bit.
|
||||
if (crc->state & crc->topbit)
|
||||
crc->state = (crc->state << 1) ^ crc->polynom;
|
||||
else
|
||||
crc->state = (crc->state << 1);
|
||||
}
|
||||
}
|
||||
|
||||
void crc_clear(crc_t *crc)
|
||||
{
|
||||
crc->state = crc->initial_value & crc->mask;
|
||||
uint32_t crc_finish(crc_t *crc) {
|
||||
uint32_t val = crc->state;
|
||||
if (crc->refout) val = reflect(val, crc->order);
|
||||
return ( val ^ crc->final_xor ) & crc->mask;
|
||||
}
|
||||
|
||||
uint32_t crc_finish(crc_t *crc)
|
||||
{
|
||||
return ( crc->state ^ crc->final_xor ) & crc->mask;
|
||||
/*
|
||||
static void print_crc(crc_t *crc) {
|
||||
printf(" Order %d\n Poly %x\n Init %x\n Final %x\n Mask %x\n topbit %x\n RefIn %s\n RefOut %s\n State %x\n",
|
||||
crc->order,
|
||||
crc->polynom,
|
||||
crc->initial_value,
|
||||
crc->final_xor,
|
||||
crc->mask,
|
||||
crc->topbit,
|
||||
(crc->refin) ? "TRUE":"FALSE",
|
||||
(crc->refout) ? "TRUE":"FALSE",
|
||||
crc->state
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
//credits to iceman
|
||||
// width=8 poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xA1 name="CRC-8/MAXIM"
|
||||
uint32_t CRC8Maxim(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init(&crc, 9, 0x8c, 0x00, 0x00);
|
||||
crc_clear(&crc);
|
||||
|
||||
for (size_t i=0; i < size; ++i)
|
||||
crc_init_ref(&crc, 8, 0x31, 0, 0, TRUE, TRUE);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update(&crc, buff[i], 8);
|
||||
|
||||
return crc_finish(&crc);
|
||||
}
|
||||
|
||||
//credits to iceman
|
||||
uint32_t CRC8Legic(uint8_t *buff, size_t size) {
|
||||
|
||||
// Poly 0x63, reversed poly 0xC6, Init 0x55, Final 0x00
|
||||
// width=4 poly=0xC, reversed poly=0x7 init=0x5 refin=true refout=true xorout=0x0000 check= name="CRC-4/LEGIC"
|
||||
// width=8 poly=0x63, reversed poly=0x8D init=0x55 refin=true refout=true xorout=0x0000 check=0xC6 name="CRC-8/LEGIC"
|
||||
// the CRC needs to be reversed before returned.
|
||||
uint32_t CRC8Legic(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init(&crc, 8, 0xC6, 0x55, 0);
|
||||
crc_clear(&crc);
|
||||
|
||||
crc_init_ref(&crc, 8, 0x63, 0x55, 0, TRUE, TRUE);
|
||||
for ( int i = 0; i < size; ++i)
|
||||
crc_update(&crc, buff[i], 8);
|
||||
return SwapBits(crc_finish(&crc), 8);
|
||||
return reflect(crc_finish(&crc), 8);
|
||||
}
|
||||
|
||||
// credits to marshmellow
|
||||
// width=8 poly=0xA3, reversed poly=0x8B, init=0xB0 refin=true refout=true xorout=0x00 check=0x28 name="CRC-8/JA"
|
||||
uint32_t CRC8ja(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0xA3, 0x42, 0x00, TRUE, TRUE);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update(&crc, buff[i], 8);
|
||||
return crc_finish(&crc);
|
||||
//return reflect(crc_finish(&crc), 8);
|
||||
}
|
||||
|
||||
// This CRC-16 is used in Legic Advant systems.
|
||||
// width=8 poly=0xB400, reversed poly=0x init=depends refin=true refout=true xorout=0x0000 check= name="CRC-16/LEGIC"
|
||||
uint32_t CRC16Legic(uint8_t *buff, size_t size, uint8_t uidcrc) {
|
||||
|
||||
#define CRC16_POLY_LEGIC 0xB400
|
||||
//uint8_t initial = reflect(uidcrc, 8);
|
||||
uint16_t initial = uidcrc;
|
||||
initial |= initial << 8;
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 16, CRC16_POLY_LEGIC, initial, 0, TRUE, TRUE);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update(&crc, buff[i], 8);
|
||||
return reflect(crc_finish(&crc), 16);
|
||||
}
|
||||
|
||||
//w=16 poly=0x3d65 init=0x0000 refin=true refout=true xorout=0xffff check=0xea82 name="CRC-16/DNP"
|
||||
uint32_t CRC16_DNP(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 16, 0x3d65, 0, 0xffff, TRUE, TRUE);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update(&crc, buff[i], 8);
|
||||
|
||||
return BSWAP_16(crc_finish(&crc));
|
||||
}
|
||||
|
||||
//width=16 poly=0x1021 init=0x1d0f refin=false refout=false xorout=0x0000 check=0xe5cc name="CRC-16/AUG-CCITT"
|
||||
uint32_t CRC16_CCITT(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init(&crc, 16, 0x1021, 0x1d0f, 0);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update(&crc, buff[i], 8);
|
||||
return crc_finish(&crc);
|
||||
}
|
36
common/crc.h
36
common/crc.h
|
@ -9,8 +9,10 @@
|
|||
#ifndef __CRC_H
|
||||
#define __CRC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdint.h> //uint32+
|
||||
#include <stdbool.h> //bool
|
||||
#include <stddef.h>
|
||||
#include "util.h" // reflect, bswap_16
|
||||
|
||||
typedef struct crc {
|
||||
uint32_t state;
|
||||
|
@ -19,13 +21,25 @@ typedef struct crc {
|
|||
uint32_t initial_value;
|
||||
uint32_t final_xor;
|
||||
uint32_t mask;
|
||||
int topbit;
|
||||
bool refin; /* Parameter: Reflect input bytes? */
|
||||
bool refout; /* Parameter: Reflect output CRC? */
|
||||
} crc_t;
|
||||
|
||||
/* Initialize a crc structure. order is the order of the polynom, e.g. 32 for a CRC-32
|
||||
* polynom is the CRC polynom. initial_value is the initial value of a clean state.
|
||||
* final_xor is XORed onto the state before returning it from crc_result().
|
||||
* refin is the setting for reversing (bitwise) the bytes during crc
|
||||
* refot is the setting for reversing (bitwise) the crc byte before returning it.
|
||||
*/
|
||||
extern void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout);
|
||||
|
||||
/* Initialize a crc structure. order is the order of the polynom, e.g. 32 for a CRC-32
|
||||
* polynom is the CRC polynom. initial_value is the initial value of a clean state.
|
||||
* final_xor is XORed onto the state before returning it from crc_result(). */
|
||||
extern void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor);
|
||||
|
||||
|
||||
/* Update the crc state. data is the data of length data_width bits (only the
|
||||
* data_width lower-most bits are used).
|
||||
*/
|
||||
|
@ -40,9 +54,24 @@ extern uint32_t crc_finish(crc_t *crc);
|
|||
// Calculate CRC-8/Maxim checksum
|
||||
uint32_t CRC8Maxim(uint8_t *buff, size_t size);
|
||||
|
||||
// Calculate CRC-4/Legic checksum
|
||||
uint32_t CRC4Legic(uint8_t *buff, size_t size);
|
||||
|
||||
// Calculate CRC-8/Legic checksum
|
||||
uint32_t CRC8Legic(uint8_t *buff, size_t size);
|
||||
|
||||
// Calculate CRC-16/Legic checksum
|
||||
// the initial_value is based on the previous legic_Crc8 of the UID.
|
||||
// ie: uidcrc = 0x78 then initial_value == 0x7878
|
||||
uint32_t CRC16Legic(uint8_t *buff, size_t size, uint8_t uidcrc);
|
||||
|
||||
// Calculate CRC-8/ja checksum
|
||||
uint32_t CRC8ja(uint8_t *buff, size_t size);
|
||||
|
||||
// test crc 16.
|
||||
uint32_t CRC16_DNP(uint8_t *buff, size_t size);
|
||||
|
||||
|
||||
/* Static initialization of a crc structure */
|
||||
#define CRC_INITIALIZER(_order, _polynom, _initial_value, _final_xor) { \
|
||||
.state = ((_initial_value) & ((1L<<(_order))-1)), \
|
||||
|
@ -50,6 +79,9 @@ uint32_t CRC8Legic(uint8_t *buff, size_t size);
|
|||
.polynom = (_polynom), \
|
||||
.initial_value = (_initial_value), \
|
||||
.final_xor = (_final_xor), \
|
||||
.mask = ((1L<<(_order))-1) }
|
||||
.mask = ((1L<<(_order))-1) \
|
||||
.refin = FALSE, \
|
||||
.refout = FALSE \
|
||||
}
|
||||
|
||||
#endif /* __CRC_H */
|
||||
|
|
|
@ -9,11 +9,9 @@
|
|||
#include "crc16.h"
|
||||
#define CRC16_POLY_CCITT 0x1021
|
||||
#define CRC16_POLY 0x8408
|
||||
#define CRC16_POLY_LEGIC 0xB400
|
||||
|
||||
unsigned short update_crc16( unsigned short crc, unsigned char c )
|
||||
{
|
||||
unsigned short i, v, tcrc = 0;
|
||||
uint16_t update_crc16( uint16_t crc, unsigned char c ) {
|
||||
uint16_t i, v, tcrc = 0;
|
||||
|
||||
v = (crc ^ c) & 0xff;
|
||||
for (i = 0; i < 8; i++) {
|
||||
|
@ -29,8 +27,8 @@ uint16_t crc16(uint8_t const *message, int length, uint16_t remainder, uint16_t
|
|||
if (length == 0)
|
||||
return (~remainder);
|
||||
|
||||
for (int byte = 0; byte < length; ++byte) {
|
||||
remainder ^= (message[byte] << 8);
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
remainder ^= (message[i] << 8);
|
||||
for (uint8_t bit = 8; bit > 0; --bit) {
|
||||
if (remainder & 0x8000) {
|
||||
remainder = (remainder << 1) ^ polynomial;
|
||||
|
@ -47,25 +45,6 @@ uint16_t crc16_ccitt(uint8_t const *message, int length) {
|
|||
}
|
||||
|
||||
uint16_t crc16_ccitt_kermit(uint8_t const *message, int length) {
|
||||
return bit_reverse_uint16(crc16(message, length, 0x0000, CRC16_POLY_CCITT));
|
||||
}
|
||||
|
||||
//ICEMAN: not working yet,
|
||||
// This CRC-16 is used in Legic Advant systems.
|
||||
uint16_t crc16_legic(uint8_t const *message, int length, uint16_t inital) {
|
||||
return crc16(message, length, inital, CRC16_POLY_LEGIC);
|
||||
}
|
||||
|
||||
uint16_t bit_reverse_uint16 (uint16_t value) {
|
||||
const uint16_t mask0 = 0x5555;
|
||||
const uint16_t mask1 = 0x3333;
|
||||
const uint16_t mask2 = 0x0F0F;
|
||||
const uint16_t mask3 = 0x00FF;
|
||||
|
||||
value = (((~mask0) & value) >> 1) | ((mask0 & value) << 1);
|
||||
value = (((~mask1) & value) >> 2) | ((mask1 & value) << 2);
|
||||
value = (((~mask2) & value) >> 4) | ((mask2 & value) << 4);
|
||||
value = (((~mask3) & value) >> 8) | ((mask3 & value) << 8);
|
||||
|
||||
return value;
|
||||
uint16_t val = crc16(message, length, 0x0000, CRC16_POLY_CCITT);
|
||||
return SwapBits(val, 16);
|
||||
}
|
||||
|
|
|
@ -5,14 +5,14 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// CRC16
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef __CRC16_H
|
||||
#define __CRC16_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "util.h"
|
||||
|
||||
unsigned short update_crc16(unsigned short crc, unsigned char c);
|
||||
uint16_t crc16(uint8_t const *message, int length, uint16_t remainder, uint16_t polynomial);
|
||||
uint16_t crc16_ccitt(uint8_t const *message, int length);
|
||||
uint16_t crc16_ccitt_kermit(uint8_t const *message, int length);
|
||||
uint16_t crc16_legic(uint8_t const *message, int length, uint16_t inital);
|
||||
uint16_t bit_reverse_uint16 (uint16_t value);
|
||||
#endif
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#ifndef __CRC32_H
|
||||
#define __CRC32_H
|
||||
|
||||
void crc32 (const uint8_t *data, const size_t len, uint8_t *crc);
|
||||
void crc32_append (uint8_t *data, const size_t len);
|
||||
void crc32 (const uint8_t *data, const size_t len, uint8_t *crc);
|
||||
void crc32_append (uint8_t *data, const size_t len);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -74,9 +74,7 @@ const uint64_t crc64_table[] = {
|
|||
|
||||
void crc64 (const uint8_t *data, const size_t len, uint64_t *crc) {
|
||||
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
//uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;
|
||||
*crc = crc64_table[tableIndex] ^ (*crc << 8);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue