proxmark3/common/crc.c

182 lines
5.7 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
2022-01-07 08:58:03 +08:00
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// Generic CRC calculation code.
//-----------------------------------------------------------------------------
// the Check value below in the comments is CRC of the string '123456789'
//
2009-11-26 04:41:41 +08:00
#include "crc.h"
#include "commonutil.h"
void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout) {
2019-03-10 02:19:50 +08:00
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) {
2019-03-10 02:19:50 +08:00
crc->order = order;
2019-03-10 07:00:59 +08:00
crc->topbit = BITMASK(order - 1);
2019-03-10 02:19:50 +08:00
crc->polynom = polynom;
crc->initial_value = initial_value;
crc->final_xor = final_xor;
2019-03-10 07:00:59 +08:00
crc->mask = (1L << order) - 1;
2019-03-10 02:19:50 +08:00
crc->refin = false;
crc->refout = false;
crc_clear(crc);
2009-11-26 04:41:41 +08:00
}
void crc_clear(crc_t *crc) {
2019-03-10 02:19:50 +08:00
crc->state = crc->initial_value & crc->mask;
if (crc->refin)
crc->state = reflect(crc->state, crc->order);
}
void crc_update2(crc_t *crc, uint32_t data, int data_width) {
2019-03-10 02:19:50 +08:00
if (crc->refin)
data = reflect(data, data_width);
2019-03-10 02:19:50 +08:00
// Bring the next byte into the remainder.
crc->state ^= data << (crc->order - data_width);
2019-03-10 07:00:59 +08:00
for (uint8_t bit = data_width; bit > 0; --bit) {
2016-09-29 18:29:42 +08:00
2019-03-10 02:19:50 +08:00
if (crc->state & crc->topbit)
crc->state = (crc->state << 1) ^ crc->polynom;
else
crc->state = (crc->state << 1);
}
2009-11-26 04:41:41 +08:00
}
void crc_update(crc_t *crc, uint32_t data, int data_width) {
2019-03-10 02:19:50 +08:00
if (crc->refin)
data = reflect(data, data_width);
2019-03-10 02:19:50 +08:00
int i;
2019-03-10 07:00:59 +08:00
for (i = 0; i < data_width; i++) {
2019-03-10 02:19:50 +08:00
int oldstate = crc->state;
crc->state = crc->state >> 1;
2019-03-10 07:00:59 +08:00
if ((oldstate ^ data) & 1) {
2019-03-10 02:19:50 +08:00
crc->state ^= crc->polynom;
}
data >>= 1;
}
}
uint32_t crc_finish(crc_t *crc) {
2019-03-10 02:19:50 +08:00
uint32_t val = crc->state;
if (crc->refout)
val = reflect(val, crc->order);
2019-03-10 07:00:59 +08:00
return (val ^ crc->final_xor) & crc->mask;
2009-11-26 04:41:41 +08:00
}
/*
static void print_crc(crc_t *crc) {
2019-03-10 02:19:50 +08:00
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
);
2009-11-26 04:41:41 +08:00
}
*/
// 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) {
2019-03-10 02:19:50 +08:00
crc_t crc;
crc_init_ref(&crc, 8, 0x31, 0, 0, true, true);
2021-12-28 00:43:00 +08:00
for (size_t i = 0; i < size; ++i) {
2019-03-10 02:19:50 +08:00
crc_update2(&crc, buff[i], 8);
2021-12-28 00:43:00 +08:00
}
2019-03-10 02:19:50 +08:00
return crc_finish(&crc);
}
2019-02-22 22:58:49 +08:00
// width=8 poly=0x1d, init=0xc7 (0xe3 - WRONG! but it mentioned in MAD datasheet) refin=false refout=false xorout=0x00 name="CRC-8/MIFARE-MAD"
uint32_t CRC8Mad(uint8_t *buff, size_t size) {
2019-03-10 02:19:50 +08:00
crc_t crc;
crc_init_ref(&crc, 8, 0x1d, 0xc7, 0, false, false);
2021-12-28 00:43:00 +08:00
for (size_t i = 0; i < size; ++i) {
2019-03-10 02:19:50 +08:00
crc_update2(&crc, buff[i], 8);
2021-12-28 00:43:00 +08:00
}
2019-03-10 02:19:50 +08:00
return crc_finish(&crc);
}
// width=4 poly=0xC, reversed poly=0x7 init=0x5 refin=true refout=true xorout=0x0000 check= name="CRC-4/LEGIC"
2019-04-07 03:46:00 +08:00
uint32_t CRC4Legic(uint8_t *buff, size_t size) {
2019-03-10 02:19:50 +08:00
crc_t crc;
crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, true, true);
crc_update2(&crc, 1, 1); /* CMD_READ */
2019-04-07 03:46:00 +08:00
crc_update2(&crc, buff[0], 8);
crc_update2(&crc, buff[1], 8);
2019-03-10 02:19:50 +08:00
return reflect(crc_finish(&crc), 4);
}
// 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) {
2019-03-10 02:19:50 +08:00
crc_t crc;
crc_init_ref(&crc, 8, 0x63, 0x55, 0, true, true);
2021-12-28 00:43:00 +08:00
for (size_t i = 0; i < size; ++i) {
2019-03-10 02:19:50 +08:00
crc_update2(&crc, buff[i], 8);
2021-12-28 00:43:00 +08:00
}
2019-03-10 02:19:50 +08:00
return reflect8(crc_finish(&crc));
2019-03-12 07:12:26 +08:00
}
// width=8 poly=0x7, init=0x2C refin=false refout=false xorout=0x0000 check=0 name="CRC-8/CARDX"
uint32_t CRC8Cardx(uint8_t *buff, size_t size) {
crc_t crc;
crc_init_ref(&crc, 8, 0x7, 0x2C, 0, false, false);
2021-12-28 00:43:00 +08:00
for (size_t i = 0; i < size; ++i) {
crc_update2(&crc, buff[i], 8);
2021-12-28 00:43:00 +08:00
}
return crc_finish(&crc);
}
2021-12-28 00:43:00 +08:00
uint32_t CRC8Hitag1(uint8_t *buff, size_t size) {
crc_t crc;
crc_init_ref(&crc, 8, 0x1d, 0xff, 0, false, false);
for (size_t i = 0; i < size; i++) {
crc_update2(&crc, buff[i], 8);
}
return crc_finish(&crc);
2021-12-28 02:36:42 +08:00
}
hitag1/S trace: add crc check Example: [usb] pm3 --> trace load -f traces/lf_HitagS256_dump.trace [+] loaded 272 bytes from binary file traces/lf_HitagS256_dump.trace [+] Recorded Activity (TraceLen = 272 bytes) [?] try `trace list -1 -t ...` to view trace. Remember the `-1` param [usb] pm3 --> trace list -t hitags -c [=] downloading tracelog data from device [+] Recorded activity (trace len = 272 bytes) [=] start = start of start frame end = end of frame. src = source of transfer [=] Hitag1 / Hitag2 / HitagS - Timings in ETU (8us) Start | End | Src | Data (! denotes parity error) | CRC | Annotation ------------+------------+-----+-------------------------------------------------------------------------+-----+-------------------- 0 | 0 | Rdr |18(5) | | 117 | 117 | Tag |21 a5 b4 [73] | !crc| 0 | 0 | Rdr |00(5) 21 a5 b4 73 [8c] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 00 [ab] | ok | 117 | 117 | Tag |21 a5 b4 73 [53] | ok | 0 | 0 | Rdr |0c(4) 01 [b6] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 02 [91] | ok | 117 | 117 | Tag |48 54 4f 4e [2c] | ok | 0 | 0 | Rdr |0c(4) 03 [8c] | ok | 117 | 117 | Tag |4d 49 4b 52 [1e] | ok | 0 | 0 | Rdr |0c(4) 04 [df] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 05 [c2] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 06 [e5] | ok | 118 | 118 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 07 [f8] | ok | 118 | 118 | Tag |57 5f 4f 4b [88] | ok | 0 | 0 | Rdr |0c(4) 08 [43] | ok |
2021-12-30 08:40:13 +08:00
2022-01-08 02:31:19 +08:00
uint32_t CRC8Hitag1Bits(const uint8_t *buff, size_t bitsize) {
hitag1/S trace: add crc check Example: [usb] pm3 --> trace load -f traces/lf_HitagS256_dump.trace [+] loaded 272 bytes from binary file traces/lf_HitagS256_dump.trace [+] Recorded Activity (TraceLen = 272 bytes) [?] try `trace list -1 -t ...` to view trace. Remember the `-1` param [usb] pm3 --> trace list -t hitags -c [=] downloading tracelog data from device [+] Recorded activity (trace len = 272 bytes) [=] start = start of start frame end = end of frame. src = source of transfer [=] Hitag1 / Hitag2 / HitagS - Timings in ETU (8us) Start | End | Src | Data (! denotes parity error) | CRC | Annotation ------------+------------+-----+-------------------------------------------------------------------------+-----+-------------------- 0 | 0 | Rdr |18(5) | | 117 | 117 | Tag |21 a5 b4 [73] | !crc| 0 | 0 | Rdr |00(5) 21 a5 b4 73 [8c] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 00 [ab] | ok | 117 | 117 | Tag |21 a5 b4 73 [53] | ok | 0 | 0 | Rdr |0c(4) 01 [b6] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 02 [91] | ok | 117 | 117 | Tag |48 54 4f 4e [2c] | ok | 0 | 0 | Rdr |0c(4) 03 [8c] | ok | 117 | 117 | Tag |4d 49 4b 52 [1e] | ok | 0 | 0 | Rdr |0c(4) 04 [df] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 05 [c2] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 06 [e5] | ok | 118 | 118 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 07 [f8] | ok | 118 | 118 | Tag |57 5f 4f 4b [88] | ok | 0 | 0 | Rdr |0c(4) 08 [43] | ok |
2021-12-30 08:40:13 +08:00
crc_t crc;
uint8_t data = 0;
uint8_t n = 0;
crc_init_ref(&crc, 8, 0x1d, 0xff, 0, false, false);
2022-02-17 03:28:38 +08:00
size_t i;
hitag1/S trace: add crc check Example: [usb] pm3 --> trace load -f traces/lf_HitagS256_dump.trace [+] loaded 272 bytes from binary file traces/lf_HitagS256_dump.trace [+] Recorded Activity (TraceLen = 272 bytes) [?] try `trace list -1 -t ...` to view trace. Remember the `-1` param [usb] pm3 --> trace list -t hitags -c [=] downloading tracelog data from device [+] Recorded activity (trace len = 272 bytes) [=] start = start of start frame end = end of frame. src = source of transfer [=] Hitag1 / Hitag2 / HitagS - Timings in ETU (8us) Start | End | Src | Data (! denotes parity error) | CRC | Annotation ------------+------------+-----+-------------------------------------------------------------------------+-----+-------------------- 0 | 0 | Rdr |18(5) | | 117 | 117 | Tag |21 a5 b4 [73] | !crc| 0 | 0 | Rdr |00(5) 21 a5 b4 73 [8c] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 00 [ab] | ok | 117 | 117 | Tag |21 a5 b4 73 [53] | ok | 0 | 0 | Rdr |0c(4) 01 [b6] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 02 [91] | ok | 117 | 117 | Tag |48 54 4f 4e [2c] | ok | 0 | 0 | Rdr |0c(4) 03 [8c] | ok | 117 | 117 | Tag |4d 49 4b 52 [1e] | ok | 0 | 0 | Rdr |0c(4) 04 [df] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 05 [c2] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 06 [e5] | ok | 118 | 118 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 07 [f8] | ok | 118 | 118 | Tag |57 5f 4f 4b [88] | ok | 0 | 0 | Rdr |0c(4) 08 [43] | ok |
2021-12-30 08:40:13 +08:00
for (i = 0; i < bitsize; i++) {
data <<= 1;
2021-12-30 19:41:23 +08:00
data += (buff[i / 8] >> (7 - (i % 8))) & 1;
hitag1/S trace: add crc check Example: [usb] pm3 --> trace load -f traces/lf_HitagS256_dump.trace [+] loaded 272 bytes from binary file traces/lf_HitagS256_dump.trace [+] Recorded Activity (TraceLen = 272 bytes) [?] try `trace list -1 -t ...` to view trace. Remember the `-1` param [usb] pm3 --> trace list -t hitags -c [=] downloading tracelog data from device [+] Recorded activity (trace len = 272 bytes) [=] start = start of start frame end = end of frame. src = source of transfer [=] Hitag1 / Hitag2 / HitagS - Timings in ETU (8us) Start | End | Src | Data (! denotes parity error) | CRC | Annotation ------------+------------+-----+-------------------------------------------------------------------------+-----+-------------------- 0 | 0 | Rdr |18(5) | | 117 | 117 | Tag |21 a5 b4 [73] | !crc| 0 | 0 | Rdr |00(5) 21 a5 b4 73 [8c] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 00 [ab] | ok | 117 | 117 | Tag |21 a5 b4 73 [53] | ok | 0 | 0 | Rdr |0c(4) 01 [b6] | ok | 117 | 117 | Tag |c9 00 00 aa [75] | ok | 0 | 0 | Rdr |0c(4) 02 [91] | ok | 117 | 117 | Tag |48 54 4f 4e [2c] | ok | 0 | 0 | Rdr |0c(4) 03 [8c] | ok | 117 | 117 | Tag |4d 49 4b 52 [1e] | ok | 0 | 0 | Rdr |0c(4) 04 [df] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 05 [c2] | ok | 117 | 117 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 06 [e5] | ok | 118 | 118 | Tag |00 00 00 00 [a6] | ok | 0 | 0 | Rdr |0c(4) 07 [f8] | ok | 118 | 118 | Tag |57 5f 4f 4b [88] | ok | 0 | 0 | Rdr |0c(4) 08 [43] | ok |
2021-12-30 08:40:13 +08:00
n += 1;
if (n == 8) {
crc_update2(&crc, data, n);
n = 0;
data = 0;
}
}
if (n > 0) {
crc_update2(&crc, data, n);
}
return crc_finish(&crc);
2021-12-30 19:41:23 +08:00
}