1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
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.
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'
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
);
31 void crc_init(crc_t
*crc
, int order
, uint32_t polynom
, uint32_t initial_value
, uint32_t final_xor
) {
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;
43 void crc_clear(crc_t
*crc
) {
45 crc
->state
= crc
->initial_value
& crc
->mask
;
47 crc
->state
= reflect(crc
->state
, crc
->order
);
50 void crc_update2(crc_t
*crc
, uint32_t data
, int data_width
) {
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
;
63 crc
->state
= (crc
->state
<< 1);
67 void crc_update(crc_t
*crc
, uint32_t data
, int data_width
) {
69 data
= reflect(data
, data_width
);
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
;
82 uint32_t crc_finish(crc_t
*crc
) {
83 uint32_t val
= crc
->state
;
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",
98 (crc->refin) ? "TRUE":"FALSE",
99 (crc->refout) ? "TRUE":"FALSE",
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
165 crc_init_ref(&crc
, 8, 0x1d, 0xff, 0, false, false);
167 for (i
= 0; i
< bitsize
; i
++) {
169 data
+= (buff
[i
/ 8] >> (7 - (i
% 8))) & 1;
172 crc_update2(&crc
, data
, n
);
178 crc_update2(&crc
, data
, n
);
180 return crc_finish(&crc
);