aqCRC Utility

CRC Selection and Code Generator

What is aqCRC?

aqCRC is a tool designed to provide simple, portable CRC implementations in C for a variety of CRC types. You can select from a list of known CRCs or specify the settings of the CRC to be implemented.

Select from one of the tab to generate an implementation, calculate a CRC, or view project information.

Why aqCRC?

There are hundreds of CRCs in use in many different applications, and several ways to do implementation. Often there is a need to implement a specific CRC and rather than searching for that implementation or writing your own, aqCRC can be used. This site can generate C-code for all CRCs up to 64-bits in length.

For new applications is is often difficult to know which CRC should be used. What size should it be? There are differences between polynomials of the same size, so what is the better polynomial? In the future aqCRC will be able to assist in picking the best CRC for an application to help people designing new protocols pick a CRC suited for the application specifics.

The author, Andrew Que, is an embedded programmer and created this site because of how often the need arose for needed a specific CRC and no single tool to fulfill this need.

What is a CRC?

Cyclic Redundancy Check or CRC is a very common method used to check for errors in a data set. It is a small code typically added to the end of a message or record that can be easily computer from the data of the message.

There are two common places CRCs are used: data transmission and data storage. In both cases, the CRC is used to make sure the data wasn't inadvertently changed by the transport layer or storage medium. This can happen through interference (such as RF), faulty hardware, soft errors and the like.

CRCs can be used as a hashing function although there are faster functions.

While CRCs could detect intentional data corruption they are easily defeated. This is because CRCs are easy to recalculate and an attacker could simply change the message and apply a new CRC, or make a message that results in the same CRC. Thus CRC cannot serve the role of cryptographi c hashes.

CRCs also do not indicate anything about the error like Error Correcting Codes (ECC)—only that there was an error.

For details about how a CRC actually works try the Wikeped ia article.

CRC Selection

CRC Selection Information

A CRC method consists of several parameters, and all are required to implement the exact method named. It is not enough just to have the CRC polynomial as there are still several ways the implementation can use the polynomial.

Polynomial

The generator polynomial is the heart of the CRC. This polynomial is chosen based on properties suited for the CRC application. This includes items such as message length, Hamming distance (number of bit errors the CRC will detect) and the type of errors considered likely for the application. CRCs are not cryptographic hashes and can easily be faked. The errors they are meant to detect are typically those encountered during data transmission or degradation of storage medium. CRCs are not correcting code and can only denote there was an error, but they contain no information about correcting such errors.

The polynomial is always specified with the most-significant bit implied. For example, CRC-4/G-704 has the generator polynomial x3+x+1, but the binary value is declared 0011 (0x3). Thus the Bits field is set to 4 and the Polynomial set to 0x3.

Initial/finial

The initial value is the CRC state before any data is shifted into the CRC, and the finial value is a value the CRC is XORed with after all data has been clocked in. They are usually all zeros or all ones and don’t have to match one another.

Nonzero values are used because the a stream of all zeros into the CRC will generate zero regardless of length. Starting the shift register with a non- zero value ensures a unique CRC for a stream of zeros. The finial XOR can be used to undo this initial value. For example, an 8-bit CRC that has initial value of 0xFF, no data clocked into it, and XORed by a finial value of 0xFF will be 0x00.

Reflection

Reflection has to do with the direction data is clocked in and out of the shift register. There is input reflection and output reflection. When input reflection is false, data is clocked into the CRC most significant to least significant bit, and least-significant to most-significant bit when true. Output reflection reverses the bit order of CRC output. Usually input and output reflection match one another, but it is not required. When they do only the way data is clocked into the CRC needs to be changed. If they do not, a finial bit reversal may be required.

CRC method

There are two methods of selecting a CRC. The first is by choosing from a list of common CRCs. The other is by entering the specifics about the CRC into the fields. Either method will allow creation of a C implementation of that CRC, and the CRC to be calculated on a set of data.

CRC Calculation

CRC Calculation Information

The selected CRC can be calculated on a set of data. This data can be text, or from a file. The calculation is done using Javascript on the local machine. While not extremely fast, the it can allow CRC settings to be verified.

Upload file
Input data type

When entering data into the Input data area, there are three input data modes. The two line endings types are needed when trying to match the CRC of a text file that is copied into the input data area. By default, browsers use Linux/Unix style line feeds which means the CRC from a Windows/DOS machine will not match.

The other data type allows data to be entered in hexadecimal. Entry must always be on byte boundaries (8- bits). White space is ignored. Data may contain the proceeding "0x".

Output data format

The output is typically display as a hex value, which is written most-significant byte to least-significant byte (big endian). However, the bytes can be swapped and display least-significant byte to most- significant (little endian). Some protocols (such as ModBus RTU) store the CRC in little endian format.

CRC Implementation

CRC Implementation

A C-implementation of the CRC is provided for CRCs 64-bits or less.

Although there are CRCs larger then 64-bits, this site currently only has implementations for single-word CRCs. Calculations using large CRCs can still be performed, but no source code generation. Should this become a popular request I may implement it.

Table method

Look-up tables greatly improve the speed at which CRCs can be generated. However, they take up memory which can be an issue on embedded devices.

The no tables method requires the least amount of memory for doing CRC calculations, but is the slowest. Data must be clocked in one bit at a time. Small embedded microprocessors (such as Arduino's Atmel) need this method. Because of inline code the no tables method requires no C file.

Static tables are the most common way to implement CRCs and also the fastest. It requires a pre-computed table compiled into the source code. This table is always 256 words in side, and the word size is the nearest word size to the CRC bit size. 8-bit CRCs have a word size of 1 byte, 16-bits a word size of 2. However, 24-bit CRCs have a word size of 4 because the closest integer size is 32-bits. Similarly, a 12-bit CRC has a 16-bit words size, and a 21-bit CRC a 32-bit word, and 40-bit CRC a 64- bit word.

Tables cannot be used for CRCs less than 8-bits. This has to do with how the bits of the CRC are fed back into the CRC. In theory, smaller tables could be used in combination with manual bit operations, but in practice it is easier just to manually clock all the bits without a table.

Dynamic tables do not store the tables in source, but compute the tables. This allows the table memory to be released back to the system. Once the tables are generated this method is as fast as static tables.

Header File

C File

This site is designed and maintained by Andrew Que. The list of CRC type (names in particular) is derived CRC RevEng.

This site uses the RequireJS AMD framework and implements both dynamic CRC source code generation as well as live CRC calculation. The library to do CRC calculations is functional but isn't recommended for projects outside this site—there are better libraries for that.

All C code (header and source) are generated dynamically. However, all the named CRCs have had their C implementation compiled and checked using a PhantomJS automated test system. Code is compiled using the GNU C compiler on a Linux host. C code is strict C99 compliant and should be very portable. Compilation will function with maximum warning level:

gcc -Wall -Wextra -pedantic -std=c99 -Werror -O2 -c (file)

The generated source code from this site has no license (i.e. public domain) and can be used in whatever manner one chooses. It would be nice if the link back to this site is maintained in the generated C files. Although the C code is tested and efforts have been made to ensure it is functional, the "AS IS" clause of the license text below applies to the generated code.

The custom Javascript libraries used to build this site are all open under the MIT license. The remaining libraries are covered by their respective licenses.

Copyright 2019 Andrew Que

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.