2010-02-21 08:12:52 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ISO14443 CRC calculation code.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-07-23 05:00:52 +08:00
|
|
|
#include "iso14443crc.h"
|
2010-02-21 05:56:33 +08:00
|
|
|
|
2018-02-01 22:19:47 +08:00
|
|
|
|
2018-01-28 18:01:29 +08:00
|
|
|
uint16_t UpdateCrc14443(uint8_t b, uint16_t *crc) {
|
|
|
|
b = (b ^ (uint8_t)((*crc) & 0x00FF));
|
|
|
|
b = (b ^ (b << 4));
|
|
|
|
*crc = (*crc >> 8) ^ ((uint16_t) b << 8) ^ ((uint16_t) b << 3) ^ ((uint16_t) b >> 4);
|
|
|
|
return (*crc);
|
2010-02-21 05:56:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:01:29 +08:00
|
|
|
void ComputeCrc14443(uint16_t CrcType, const uint8_t *data, int length,
|
|
|
|
uint8_t *TransmitFirst, uint8_t *TransmitSecond)
|
2010-02-21 05:56:33 +08:00
|
|
|
{
|
2018-01-28 18:01:29 +08:00
|
|
|
uint8_t b;
|
|
|
|
uint16_t crc = CrcType;
|
2010-02-21 05:56:33 +08:00
|
|
|
|
2018-01-28 18:01:29 +08:00
|
|
|
do {
|
|
|
|
b = *data++;
|
|
|
|
UpdateCrc14443(b, &crc);
|
|
|
|
} while (--length);
|
2010-02-21 05:56:33 +08:00
|
|
|
|
|
|
|
if (CrcType == CRC_14443_B)
|
2018-01-28 18:01:29 +08:00
|
|
|
crc = ~crc; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
|
2010-02-21 05:56:33 +08:00
|
|
|
|
2018-01-28 18:01:29 +08:00
|
|
|
*TransmitFirst = (uint8_t) (crc & 0xFF);
|
|
|
|
*TransmitSecond = (uint8_t)((crc >> 8) & 0xFF);
|
2010-02-21 05:56:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-07-11 23:52:33 +08:00
|
|
|
|
2018-01-28 18:01:29 +08:00
|
|
|
bool CheckCrc14443(uint16_t CrcType, const uint8_t *data, int length) {
|
|
|
|
if (length < 3) return false;
|
|
|
|
uint8_t b1, b2;
|
|
|
|
ComputeCrc14443(CrcType, data, length - 2, &b1, &b2);
|
|
|
|
if ((b1 == data[length - 2]) && (b2 == data[length - 1]))
|
|
|
|
return true;
|
|
|
|
return false;
|
2018-02-01 22:19:47 +08:00
|
|
|
}
|