Merge pull request #2654 from Antiklesys/master
[RRG-proxmark3.git] / common / crc16.h
blob80758d95b5641d9f7406876018a8835cbd16e484
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 // CRC16
17 //-----------------------------------------------------------------------------
18 #ifndef __CRC16_H
19 #define __CRC16_H
21 #include "common.h"
23 #define CRC16_POLY_CCITT 0x1021
24 #define CRC16_POLY_KERMIT 0x8408
25 #define CRC16_POLY_LEGIC 0xc6c6 //0x6363
26 #define CRC16_POLY_LEGIC_16 0x002d
27 #define CRC16_POLY_DNP 0x3d65
29 #define X25_CRC_CHECK ((uint16_t)(~0xF0B8 & 0xFFFF)) // use this for checking of a correct crc
31 typedef enum {
32 CRC_NONE,
33 CRC_11784,
34 CRC_14443_A,
35 CRC_14443_B,
36 CRC_15693,
37 CRC_ICLASS,
38 CRC_FELICA,
39 CRC_LEGIC,
40 CRC_LEGIC_16,
41 CRC_CCITT,
42 CRC_KERMIT,
43 CRC_XMODEM,
44 CRC_CRYPTORF,
45 CRC_PHILIPS,
46 } CrcType_t;
48 uint16_t update_crc16_ex(uint16_t crc, uint8_t c, uint16_t polynomial);
49 uint16_t update_crc16(uint16_t crc, uint8_t c);
50 uint16_t Crc16(uint8_t const *d, size_t length, uint16_t remainder, uint16_t polynomial, bool refin, bool refout);
52 uint16_t Crc16ex(CrcType_t ct, const uint8_t *d, size_t n);
53 void compute_crc(CrcType_t ct, const uint8_t *d, size_t n, uint8_t *first, uint8_t *second);
54 bool check_crc(CrcType_t ct, const uint8_t *d, size_t n);
56 // Calculate CRC-16/CCITT-FALSE
57 uint16_t crc16_ccitt(uint8_t const *d, size_t n);
59 // Calculate CRC-16/KERMIT (FDX-B ISO11784/85) LF
60 uint16_t crc16_fdxb(uint8_t const *d, size_t n);
62 // Calculate CRC-16/KERMIT
63 uint16_t crc16_kermit(uint8_t const *d, size_t n);
65 // Calculate CRC-16/XMODEM (FeliCa)
66 uint16_t crc16_xmodem(uint8_t const *d, size_t n);
68 // Calculate CRC-16/X25 (ISO15693, ISO14443 CRC-B,ISO/IEC 13239)
69 uint16_t crc16_x25(uint8_t const *d, size_t n);
71 // Calculate CRC-16/CRC-A (ISO14443 CRC-A)
72 uint16_t crc16_a(uint8_t const *d, size_t n);
74 // Calculate CRC-16/iCLASS
75 uint16_t crc16_iclass(uint8_t const *d, size_t n);
77 // Calculate CRC-16/Legic
78 // the initial_value is based on the previous legic_Crc8 of the UID.
79 // ie: uidcrc = 0x78 then initial_value == 0x7878
80 uint16_t crc16_legic(uint8_t const *d, size_t n, uint8_t uidcrc);
82 // Calculate CRC-16/ Philips.
83 uint16_t crc16_philips(uint8_t const *d, size_t n);
85 // table implementation
86 void init_table(CrcType_t crctype);
87 void reset_table(void);
88 void generate_table(uint16_t polynomial, bool refin);
89 uint16_t crc16_fast(uint8_t const *d, size_t n, uint16_t initval, bool refin, bool refout);
91 #endif