Discussion:
Mifare MAD CRC
(too old to reply)
Andy Whitehead
2003-09-04 16:41:53 UTC
Permalink
I've been trying to generate the 8-bit CRC for the Mifare Application
Directory.

The CRC uses the polynomial x^8 + x^4 + x^3 + x^2 + 1 and is preset
with 0xE3

The MAD spec gives an example MAD with bytes: -

0x01 0x01 0x08 0x01 0x08 0x01 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x04
0x00
0x03 0x10 0x03 0x10 0x02 0x10 0x02 0x10 0x00 0x00 0x00 0x00 0x00 0x00
0x11 0x30


Giving a CRC of 0x89

No matter what approach/sample code I use I cannot get this value for
the CRC.



Has anyone successfully written the CRC byte to the MAD using their
own code? I'm using c++. Can anyone help?

Thanks in advance

Andy
Johann Dantant
2003-09-05 16:14:09 UTC
Permalink
Post by Andy Whitehead
I've been trying to generate the 8-bit CRC for the Mifare Application
Directory.
The CRC uses the polynomial x^8 + x^4 + x^3 + x^2 + 1 and is preset
with 0xE3
No matter what approach/sample code I use I cannot get this value for
the CRC.
Try source code at http://home.att.net/~cbwatson/CRC_info.doc with those
defines :

#define CRC8
#define POLY 0x011D
#define CRC_INIT 0x03
typedef unsigned byte CRC_TYPE;
Johann Dantant
2003-09-05 16:20:21 UTC
Permalink
Post by Andy Whitehead
I've been trying to generate the 8-bit CRC for the Mifare Application
Directory.
The CRC uses the polynomial x^8 + x^4 + x^3 + x^2 + 1 and is preset
with 0xE3
I've also this one from an old Philips SDK

int mif_calc_crc(unsigned char mode, int len, unsigned char *in, unsigned
char *out)
{
int i, j, stat;
unsigned int crc;

stat = MI_CRC_ZERO;

if (mode == CRC8)
{
*out = 0xC7; // bit-swapped 0xE3

for (j = 0; j < len; j++)
{
*out = *out ^ in[j];

for (i = 0; i < 8; i++)
{
if (*out & 0x80)
{
*out = (*out << 1) ^ 0x1D;
}
else
{
*out = *out << 1;
}
}
}
if (*out)
{
stat = MI_CRC_NOTZERO;
}
}
else // CRC16
{
crc = 0xC78C; // bit-swapped 0x31E3

for (j = 0; j < len; j++)
{
crc = crc ^ ((unsigned int)in[j] << 8);

for (i = 0; i < 8; i++)
{
if (crc & 0x8000)
{
crc = (crc << 1) ^ 0x1021;
}
else
{
crc = crc << 1;
}
}
}
out[0] = (unsigned char)(crc >> 8);
out[1] = (unsigned char)crc;

if (crc)
{
stat = MI_CRC_NOTZERO;
}
}
return (stat);
}
Andy Whitehead
2003-09-08 08:09:48 UTC
Permalink
That looks like exactly what I need. Thanks
Post by Johann Dantant
Post by Andy Whitehead
I've been trying to generate the 8-bit CRC for the Mifare Application
Directory.
The CRC uses the polynomial x^8 + x^4 + x^3 + x^2 + 1 and is preset
with 0xE3
I've also this one from an old Philips SDK
int mif_calc_crc(unsigned char mode, int len, unsigned char *in, unsigned
char *out)
{
int i, j, stat;
unsigned int crc;
stat = MI_CRC_ZERO;
if (mode == CRC8)
{
*out = 0xC7; // bit-swapped 0xE3
for (j = 0; j < len; j++)
{
*out = *out ^ in[j];
for (i = 0; i < 8; i++)
{
if (*out & 0x80)
{
*out = (*out << 1) ^ 0x1D;
}
else
{
*out = *out << 1;
}
}
}
if (*out)
{
stat = MI_CRC_NOTZERO;
}
}
else // CRC16
{
crc = 0xC78C; // bit-swapped 0x31E3
for (j = 0; j < len; j++)
{
crc = crc ^ ((unsigned int)in[j] << 8);
for (i = 0; i < 8; i++)
{
if (crc & 0x8000)
{
crc = (crc << 1) ^ 0x1021;
}
else
{
crc = crc << 1;
}
}
}
out[0] = (unsigned char)(crc >> 8);
out[1] = (unsigned char)crc;
if (crc)
{
stat = MI_CRC_NOTZERO;
}
}
return (stat);
}
Loading...