R&Y: Added DAY RTA Tapp Card and Additional BKK BEM Stored Value Card AIDs to `aid_de...
[RRG-proxmark3.git] / common / crc.c
blob016d8e5befcccbcdd2a8e05b693c4d7952f92cbc
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Generic CRC calculation code.
17 //-----------------------------------------------------------------------------
18 // the Check value below in the comments is CRC of the string '123456789'
20 #include "crc.h"
22 #include "commonutil.h"
24 void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout) {
25 crc_init(crc, order, polynom, initial_value, final_xor);
26 crc->refin = refin;
27 crc->refout = refout;
28 crc_clear(crc);
31 void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor) {
32 crc->order = order;
33 crc->topbit = BITMASK(order - 1);
34 crc->polynom = polynom;
35 crc->initial_value = initial_value;
36 crc->final_xor = final_xor;
37 crc->mask = (1L << order) - 1;
38 crc->refin = false;
39 crc->refout = false;
40 crc_clear(crc);
43 void crc_clear(crc_t *crc) {
45 crc->state = crc->initial_value & crc->mask;
46 if (crc->refin)
47 crc->state = reflect(crc->state, crc->order);
50 void crc_update2(crc_t *crc, uint32_t data, int data_width) {
52 if (crc->refin)
53 data = reflect(data, data_width);
55 // Bring the next byte into the remainder.
56 crc->state ^= data << (crc->order - data_width);
58 for (uint8_t bit = data_width; bit > 0; --bit) {
60 if (crc->state & crc->topbit)
61 crc->state = (crc->state << 1) ^ crc->polynom;
62 else
63 crc->state = (crc->state << 1);
67 void crc_update(crc_t *crc, uint32_t data, int data_width) {
68 if (crc->refin)
69 data = reflect(data, data_width);
71 int i;
72 for (i = 0; i < data_width; i++) {
73 int oldstate = crc->state;
74 crc->state = crc->state >> 1;
75 if ((oldstate ^ data) & 1) {
76 crc->state ^= crc->polynom;
78 data >>= 1;
82 uint32_t crc_finish(crc_t *crc) {
83 uint32_t val = crc->state;
84 if (crc->refout)
85 val = reflect(val, crc->order);
86 return (val ^ crc->final_xor) & crc->mask;
90 static void print_crc(crc_t *crc) {
91 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",
92 crc->order,
93 crc->polynom,
94 crc->initial_value,
95 crc->final_xor,
96 crc->mask,
97 crc->topbit,
98 (crc->refin) ? "TRUE":"FALSE",
99 (crc->refout) ? "TRUE":"FALSE",
100 crc->state
105 // width=8 poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xA1 name="CRC-8/MAXIM"
106 uint32_t CRC8Maxim(uint8_t *buff, size_t size) {
107 crc_t crc;
108 crc_init_ref(&crc, 8, 0x31, 0, 0, true, true);
109 for (size_t i = 0; i < size; ++i) {
110 crc_update2(&crc, buff[i], 8);
112 return crc_finish(&crc);
114 // 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"
115 uint32_t CRC8Mad(uint8_t *buff, size_t size) {
116 crc_t crc;
117 crc_init_ref(&crc, 8, 0x1d, 0xc7, 0, false, false);
118 for (size_t i = 0; i < size; ++i) {
119 crc_update2(&crc, buff[i], 8);
121 return crc_finish(&crc);
123 // width=4 poly=0xC, reversed poly=0x7 init=0x5 refin=true refout=true xorout=0x0000 check= name="CRC-4/LEGIC"
124 uint32_t CRC4Legic(uint8_t *buff, size_t size) {
125 crc_t crc;
126 crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, true, true);
127 crc_update2(&crc, 1, 1); /* CMD_READ */
128 crc_update2(&crc, buff[0], 8);
129 crc_update2(&crc, buff[1], 8);
130 return reflect(crc_finish(&crc), 4);
132 // width=8 poly=0x63, reversed poly=0x8D init=0x55 refin=true refout=true xorout=0x0000 check=0xC6 name="CRC-8/LEGIC"
133 // the CRC needs to be reversed before returned.
134 uint32_t CRC8Legic(uint8_t *buff, size_t size) {
135 crc_t crc;
136 crc_init_ref(&crc, 8, 0x63, 0x55, 0, true, true);
137 for (size_t i = 0; i < size; ++i) {
138 crc_update2(&crc, buff[i], 8);
140 return reflect8(crc_finish(&crc));
142 // width=8 poly=0x7, init=0x2C refin=false refout=false xorout=0x0000 check=0 name="CRC-8/CARDX"
143 uint32_t CRC8Cardx(uint8_t *buff, size_t size) {
144 crc_t crc;
145 crc_init_ref(&crc, 8, 0x7, 0x2C, 0, false, false);
146 for (size_t i = 0; i < size; ++i) {
147 crc_update2(&crc, buff[i], 8);
149 return crc_finish(&crc);
152 uint32_t CRC8Hitag1(uint8_t *buff, size_t size) {
153 crc_t crc;
154 crc_init_ref(&crc, 8, 0x1d, 0xff, 0, false, false);
155 for (size_t i = 0; i < size; i++) {
156 crc_update2(&crc, buff[i], 8);
158 return crc_finish(&crc);
161 uint32_t CRC8Hitag1Bits(const uint8_t *buff, size_t bitsize) {
162 crc_t crc;
163 uint8_t data = 0;
164 uint8_t n = 0;
165 crc_init_ref(&crc, 8, 0x1d, 0xff, 0, false, false);
166 size_t i;
167 for (i = 0; i < bitsize; i++) {
168 data <<= 1;
169 data += (buff[i / 8] >> (7 - (i % 8))) & 1;
170 n += 1;
171 if (n == 8) {
172 crc_update2(&crc, data, n);
173 n = 0;
174 data = 0;
177 if (n > 0) {
178 crc_update2(&crc, data, n);
180 return crc_finish(&crc);