-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericCRC.py
233 lines (187 loc) · 8.93 KB
/
GenericCRC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'''
MIT License
Copyright (c) 2021 Oscar Huang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
class GenericCRC():
"""An arbitrary-length (>=8) CRC calculator
Usage:
Consruct a crc object and then call its Calculate() method. To see
the check value of the CRC, call Check() method
"""
def __init__(self, name, crcLen, poly, initCRC=0, refin=False, refout=False, xorout=0):
""" Construct a CRC64 calculator. The polynomial is assumed to be the normal form polynomial
- name: The name of the CRC. Informative, not used for calculating the CRC digest
- crcLen: The number of CRC bits, must be >= 8
- poly: The CRC polynomial in normal representation.
- initCRC: the initial CRC value to use
- refin: Input reflected
- refout: Output reflected
- xorout: The value to be XOR-ed with the final CRC.
"""
self.name = name
self.crcLen = crcLen
self.refin = refin
self.refout = refout
self.xorout = xorout
if self.refin:
self.initCRC = GenericCRC.reverseInt(initCRC, crcLen)
self.poly = GenericCRC.reverseInt(poly, crcLen)
self.genCRCTbl_refin()
else:
self.initCRC = initCRC
self.poly = poly
self.genCRCTbl_nrefin()
@classmethod
def reverseInt(cls, v, nBits):
"""Reverse an int with nBits"""
sbin = bin(v)
sbin = sbin[2:] #discard the leading "0b"
n = len(sbin)
if n > nBits:
print("Value out of range of", nBits, "bits")
return None
s = '0'*(nBits-len(sbin)) + sbin
return int(s[::-1], 2)
def genCRCTbl_nrefin(self):
"""Generate CRC look-up table for the case that the input bytes
are not reflected
"""
tbl = [None]*256
for byte in range(0, 256):
crc = byte << (self.crcLen - 8)
for i in range(0, 8):
c = (crc << 1) & ((1 << self.crcLen) - 1)
if (crc >> (self.crcLen-1)) != 0:
crc = c ^ self.poly
else:
crc = c
tbl[byte] = crc
self.crcTbl = tbl
def genCRCTbl_refin(self):
"""Generate CRC look-up table for the case that the input bytes
are reflected
"""
tbl = [None]*256
for byte in range(0, 256):
crc = byte
for i in range(0, 8):
c = crc >> 1
if (crc & 0x1) != 0:
crc = c ^ self.poly
else:
crc = c
tbl[byte] = crc
self.crcTbl = tbl
def calculate_nrefin(self, message):
"""Compute CRC digest for a message, input not reflected
- message: a byte array message
- Return: CRC digest
"""
if (type(message) != bytes) and (type(message) != bytearray):
print("Error: expecting bytes or bytearray input")
return None
crc = self.initCRC #0xFFFFFFFF #initial crc
for byte in message:
pos = (crc >> (self.crcLen - 8)) ^ byte
crc = (crc << 8) & ((1 << self.crcLen) - 1)
crc = crc ^ self.crcTbl[pos]
return crc
def calculate_refin(self, message):
"""Compute CRC digest for a message, input reflected
- message: a byte array message
- Return: CRC digest
"""
if (type(message) != bytes) and (type(message) != bytearray):
print("Error: expecting bytes or bytearray input")
return None
crc = self.initCRC #0xFFFFFFFF #initial crc
for byte in message:
pos = (crc ^ byte) & 0xFF
crc = crc >> 8
crc = crc ^ self.crcTbl[pos]
return crc
#The public API for calculating CRC64:
def Calculate(self, message):
"""Calculate CRC digest"""
if self.refin:
crc = self.calculate_refin(message)
else:
crc = self.calculate_nrefin(message)
if self.refout ^ self.refin:
crc = GenericCRC.reverseInt(crc, self.crcLen) #reflect output
return crc ^ self.xorout #final XOR
def Check(self):
"""Calculate CRC for the message '123456789'"""
msg = "123456789".encode("utf-8")
return self.Calculate(msg)
#===========================================================================================================================================
#Run some tests below:
if __name__ == "__main__":
""" See CRC catalogue and parameters at https://reveng.sourceforge.io/crc-catalogue/all.htm"""
crc64_ecma = GenericCRC("CRC-64/ECMA-182", crcLen=64, poly=0x42f0e1eba9ea3693) #Construct a CRC64 object
crc = crc64_ecma.Check()
expected = 0x6c40df5f0b497347
print(f'CRC64/ECMA-182 check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc64_iso = GenericCRC("CRC-64/GO-ISO", crcLen=64, poly=0x000000000000001B, initCRC=0xFFFFFFFFFFFFFFFF, refin=True, refout=True, xorout=0xFFFFFFFFFFFFFFFF)
crc = crc64_iso.Check()
expected = 0xb90956c775a41001
print(f'CRC64/GO-ISO check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc40_gsm = GenericCRC("CRC40/GSM", crcLen=40, poly=0x0004820009, initCRC=0, refin=False, refout=False, xorout=0xFFFFFFFFFF)
crc = crc40_gsm.Check()
expected = 0xd4164fc646
print(f'CRC40/GSM check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc32c = GenericCRC("CRC32C", crcLen=32, poly=0x1EDC6F41, initCRC=0xFFFFFFFF, refin=True, refout=True, xorout=0xFFFFFFFF)
crc = crc32c.Check()
expected = 0xe3069283
print(f'CRC32C check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc32_cksum = GenericCRC("CRC32/CKSUM", crcLen=32, poly=0x04c11db7, initCRC=0, refin=False, refout=False, xorout=0xFFFFFFFF)
crc = crc32_cksum.Check()
expected = 0x765e7680
print(f'CRC32/CKSUM check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc31_philips = GenericCRC("CRC31/PHILIPS", crcLen=31, poly=0x04c11db7, initCRC=0x7fffffff, refin=False, refout=False, xorout=0x7fffffff)
crc = crc31_philips.Check()
expected = 0x0ce9e46c
print(f'CRC31/PHILIPS check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc30_cdma = GenericCRC("CRC31/CDMA", crcLen=30, poly=0x2030b9c7, initCRC=0x3fffffff, refin=False, refout=False, xorout=0x3fffffff)
crc = crc30_cdma.Check()
expected = 0x04c34abf
print(f'CRC30/CDMA check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc24_lteb = GenericCRC("CRC24/LTE_B", crcLen=24, poly=0x800063)
crc = crc24_lteb.Check()
expected = 0x23ef52
print(f'CRC24/LTE_B check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc24_openpgp = GenericCRC("CRC24/OPENPGP", crcLen=24, poly=0x864cfb, initCRC=0xb704ce)
crc = crc24_openpgp.Check()
expected = 0x21cf02
print(f'CRC24/OPENPGP check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc16_t10dif = GenericCRC("CRC16/T10DIF", crcLen=16, poly=0x8bb7)
crc = crc16_t10dif.Check()
expected = 0xd0db
print(f'CRC16/T10DIF check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc16_tms37157 = GenericCRC("CRC-16/TMS37157", crcLen=16, poly=0x1021, initCRC=0x89ec, refin=True, refout=True)
crc = crc16_tms37157.Check()
expected = 0x26b1
print(f'CRC16/TMS37157 check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc8_wcdma = GenericCRC("CRC8/WCDMA", crcLen=8, poly=0x9b, initCRC=0x00, refin=True, refout=True)
crc = crc8_wcdma.Check()
expected = 0x25
print(f'CRC8/WCDMA check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')
crc8_smbus = GenericCRC("CRC8/SMBUS", crcLen=8, poly=0x07)
crc = crc8_smbus.Check()
expected = 0xf4
print(f'CRC8/SMBUS check is {crc:#x}. Expected: {expected:#x}. Error={crc-expected}')