CYCLIC REDUNDANCY CHECKS IN USB. Introduction. Two-minute mathematical background



Similar documents
The finite field with 2 elements The simplest finite field is

Lecture 6: Finite Fields (PART 3) PART 3: Polynomial Arithmetic. Theoretical Underpinnings of Modern Cryptography

Sheet 7 (Chapter 10)

Data Link Layer(1) Principal service: Transferring data from the network layer of the source machine to the one of the destination machine

8-bit RISC Microcontroller. Application Note. AVR236: CRC Check of Program Memory

Data Link Layer Overview

1 Overview. T10/ revision 9

Systems I: Computer Organization and Architecture

Erasure Codes Made So Simple, You ll Really Like Them

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

3.1. RATIONAL EXPRESSIONS

Paramedic Program Pre-Admission Mathematics Test Study Guide

minimal polyonomial Example

Multiple clock domains

b) since the remainder is 0 I need to factor the numerator. Synthetic division tells me this is true

Cryptography and Network Security. Prof. D. Mukhopadhyay. Department of Computer Science and Engineering. Indian Institute of Technology, Kharagpur

Copy in your notebook: Add an example of each term with the symbols used in algebra 2 if there are any.

Network Security. Security of Wireless Local Area Networks. Chapter 15. Network Security (WS 2002): 15 Wireless LAN Security 1 Dr.-Ing G.

Computer Networks. Data Link Layer

Chapter 3: Sample Questions, Problems and Solutions Bölüm 3: Örnek Sorular, Problemler ve Çözümleri

Lecture 11: Number Systems

Some facts about polynomials modulo m (Full proof of the Fingerprinting Theorem)

Useful Number Systems

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

2.3. Finding polynomial functions. An Introduction:

Error detection using CRC

Review of Error Detection of Data Link Layer in Computer Network

COMPSCI 210. Binary Fractions. Agenda & Reading

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

The string of digits in the binary number system represents the quantity

Lecture 8: Binary Multiplication & Division

Modbus RTU Communications RX/WX and MRX/MWX

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Module MA3411: Abstract Algebra Galois Theory Appendix Michaelmas Term 2013

Polynomials. Key Terms. quadratic equation parabola conjugates trinomial. polynomial coefficient degree monomial binomial GCF

Determinants can be used to solve a linear system of equations using Cramer s Rule.

Distributed Storage Networks and Computer Forensics

2011, The McGraw-Hill Companies, Inc. Chapter 3

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

3.2 The Factor Theorem and The Remainder Theorem

JUST THE MATHS UNIT NUMBER 1.8. ALGEBRA 8 (Polynomials) A.J.Hobson

Factoring Polynomials

Algorithms and Methods for Distributed Storage Networks 5 Raid-6 Encoding Christian Schindelhauer

How To Write A Hexadecimal Program

Quotient Rings and Field Extensions

Lagrange Interpolation is a method of fitting an equation to a set of points that functions well when there are few points given.

A Practical Parallel CRC Generation Method

UMBC. ISA is the oldest of all these and today s computers still have a ISA bus interface. in form of an ISA slot (connection) on the main board.

Pre-Calculus II Factoring and Operations on Polynomials

The mathematics of RAID-6

Modern Block Cipher Standards (AES) Debdeep Mukhopadhyay

U.C. Berkeley CS276: Cryptography Handout 0.1 Luca Trevisan January, Notes on Algebra

9. POLYNOMIALS. Example 1: The expression a(x) = x 3 4x 2 + 7x 11 is a polynomial in x. The coefficients of a(x) are the numbers 1, 4, 7, 11.

ENTTEC Pixie Driver API Specification

LLC: Frame Construction. Layer 2: Division into two Parts. Error-detecting and -correcting Codes. Error-correcting Codes

Appendix B RCS11 Remote Communications

Algebra I Vocabulary Cards

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Factorization Algorithms for Polynomials over Finite Fields

ENGLISH TRANSLATION STRUCTURE AND OPERATION OF CLOSED CAPTION DATA CONVEYED BY ANCILLARY DATA PACKETS ARIB STANDARD. ARIB STD-B37 Version 2.

Section 1.4 Place Value Systems of Numeration in Other Bases

63. Graph y 1 2 x and y 2 THE FACTOR THEOREM. The Factor Theorem. Consider the polynomial function. P(x) x 2 2x 15.

Factoring Polynomials

Vocabulary Words and Definitions for Algebra

Basic Properties of Rational Expressions

3. Convert a number from one number system to another

EnOcean Radio Protocol 2. September 26, 2013 SPECIFICATION V1.0. Subject to modifications EnOcean Radio Protocol 2 V1.0 Page 1/19

The Answer to the 14 Most Frequently Asked Modbus Questions

Introduction to Finite Fields (cont.)

1.Eastron SDM220Modbus Smart Meter Modbus Protocol Implementation V1.0

RS-485 Protocol Manual

Modicon Modbus Protocol Reference Guide. PI MBUS 300 Rev. J

Binary Numbers. Binary Octal Hexadecimal

Chapter 2. Binary Values and Number Systems

ETSI TS V1.2.1 ( )

Chapter R.4 Factoring Polynomials

Chapter 4, Arithmetic in F [x] Polynomial arithmetic and the division algorithm.

MEP Y9 Practice Book A

Computer Science 281 Binary and Hexadecimal Review

Real-time Operating Systems Lecture 27.1

Discrete Mathematics: Homework 7 solution. Due:

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

r + s = i + j (q + t)n; 2 rs = ij (qj + ti)n + qtn.

ABSTRACT ALGEBRA: A STUDY GUIDE FOR BEGINNERS

2003 HSC Notes from the Marking Centre Software Design and Development

3.Basic Gate Combinations

Mathematics Course 111: Algebra I Part IV: Vector Spaces

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Application Note AN-00160

CRC32 Based Signature Generation Methods for URL Routing

The Effectiveness of Checksums for Embedded Control Networks

7. Some irreducible polynomials

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

Polynomial Expressions and Equations

The Division Algorithm for Polynomials Handout Monday March 5, 2012

a. all of the above b. none of the above c. B, C, D, and F d. C, D, F e. C only f. C and F

Reed-Solomon Codes. by Bernard Sklar

The Factor Theorem and a corollary of the Fundamental Theorem of Algebra

Transcription:

CYCLIC REDUNDANCY CHECKS IN USB Introduction The USB specification calls for the use of Cyclic Redundancy Checksums (CRC) to protect all non-pid fields in token and data packets from errors during transmission. This paper describes the mathematical basis behind CRC in an intuitive fashion and then explains the specific implementation called for in USB. Perl programs to generate these CRCs are provided and several examples are used to clear up possible ambiguous areas. Two-minute mathematical background The well-known concept of integer division forms the basis for the use of CRCs. When a dividend is divided by a divisor, a quotient and a remainder (which may be 0) result. If the remainder is subtracted from the dividend and this is divided by the divisor, the remainder is always zero. e.g. when 629 is divided by 25, the remainder is 4. If the dividend (629) and the remainder (4) are transmitted from a source to a target, the integrity of the transmission can be verified at the target by recomputing the remainder and verifying that the remainder matches the transmitted remainder. Alternatively, the target could divide the difference between the transmitted dividend and remainder and expect to see a zero remainder if there were no errors. The concept of integer division can also be applied to division of polynomials. (An intuitive way to understand this is by considering that the digits which make up an integer can be considered the coefficients of a polynomial in base 10 e.g. 629 = 6*10 2 + 2* 10 1 + 9*10 0.). A binary bitstream (which is a pattern of 1s and 0s) can be considered to represent the coefficients of a (dividend) polynomial. When this polynomial is divided by a generator (divisor) polynomial (which is another binary bitstream) a remainder polynomial (CRC) will result. The arithmetic is especially simplified if 1 and 0 are considered to be elements of a finite field (the Galois field of order 2 or GF(2)). The arithmetic is sometimes referred to as modulo 2 arithmetic. For the purposes of CRC computation, it is sufficient to understand that addition and subtraction in this field reduce to simple XOR operations. This is the basis of the technique used for generating and checking CRC for USB packets. CRCs are useful because they are capable of detecting all single and double errors and many multiple errors with a small number of bits. Communications protocols often use two CRCs in a packet - one to protect the header of the packet and another to protect the data portion of the packet. In USB the header of the packet is the PID field. Since this field is only 4 bits long it is protected by a 4 bit check field derived by simple bitwise inversion of the PID field. This provides adequate single-bit and burst error protection 1

without requiring CRC generation and checking logic. The data portion of a USB packet which is longer is protected by a conventional CRC field. In token packets, the CRC protected region is only 11 bits - so a 5 bit CRC provides adequate protection and also aligns the packet to a byte boundary. Data packets may have upto 1023 bytes; so a longer (16 bit) CRC is used to protect the data. CRC implementation for USB The USB spec lists two generator polynomials - one for tokens and the other for data packets. The generator polynomial for tokens is x 5 + x 2 + x 0 while the generator polynomial for data packets is x 16 + x 15 + x 2 + x 0. Since the remainder is always of smaller degeree than the generator polynomial, the token CRC is a 5 bit pattern and the data CRC is a 16 bit pattern. An intuitive way to generate the CRC for an input pattern would be to simply divide this pattern by the generator poylnomial. The division can use the same approach used in integer division. If the Most Significant digit/bit (MSB) of the dividend is a 1, the divisor is subtracted from the dividend to generate a new dividend. In modulo 2 arithmetic, this subtraction is simply a bit-wise XOR. The same step is repeated for the next MSB in sequence through all the bits including the Least Significant Bit (LSB). The remainder at this point is the remainder from dividing the input pattern multiplied by x d by the generator polynomial of degree d (d is 5 for token CRC and 16 for data CRC). The implementation called for in the USB spec differs from the above intuitive approach in three ways which are mathematically insignificant. 1. The implementation starts off with the shift register loaded with all 1s. Without this, leading 0s in front of a packet would not be protected by the CRC generated. Mathematically this is equivalent to adding a scaled constant to the dividend. The scaling is a function of number of bits in the input pattern. At the receiving target, the shift register is likewise primed with 1s. This ensures that the same scaled constant is added to the dividend at the target (assuming no bits are lost in transit). So if no bits are corrupted in transit, the same remainder will be generated at both ends. In equation form the scaling creates the dividend D(x) = x 32 F(x) + x k L(x) where F(x) is a degree (k-1) polynomial representing the k bits of the data stream and L(x) is a degree (d-1) polynomial with all coefficients equal to one and d is degree of the generator polynomial. 2. The implementation uses commutativity and associativity of the XOR operation to advantage. In the intuitive approach, the new dividend is derived by subtracting the divisor from the dividend. In the implementation, this subtraction is done bit by bit on the dividend by accumulating and shifting the remainder. 3. The remainder is bit-wise inverted before being appended as the checksum to the input pattern. Without this modification, trailing zeros at the end of a packet could not be detected as data transmission errors. Mathematically, this is equivalent to adding a known 2

constant to the remainder. Again this is mathematically insignificant to the operation of CRC. In equation form, CRC= L(x) + R(x) where R(x) is remainder obtained by dividing D(x) by the generator polynomial G(x). Checking the CRC at the target is the same as generating the CRC on an input pattern which now consists of the original input pattern followed by the inverted remainder. Mathematically, this new polynomial should be perfectly divisible by the generator polynomial except for the residual due to the known constant discussed in item 3 above. (This can be intuitively understood by realizing that the appending of the remainder to the LSB of the dividend is equivalent to subtracting it from the old dividend). In equation form, the transmitted and received data is M(x)=x 32 F(x) + CRC. When CRC is generated on this pattern M(x), the remainder R (x) is x 32 L(x)/G(x) and can be derived from the above equations and some properties of modulo 2 arithmetic. R (x) is a unique polynomial (i.e. coeffiecients are always the same) since L(x) and G(x) are unique. R (x) is termed the residue or residual polynomial. For the token generator polynomial, the residual is 01100 (or x 4 + x 3 ) ; for the data CRC polynomial the residual is 1000000000001101 ( or x 15 + x 3 + x 2 + x 0 ). Programs for generating CRC While the above sections contain enough information to implement the CRC logic, it is quite easy to get the bit order confused. The following two programs in Perl (crc5 and crc16) can be used for checking the implementation of the token crc and data crc respectively. #! /usr/local/bin/perl ## crc5 nrzstream ## e.g. crc5 1000111 ## nrz stream is sent in left to right order ## generated crc should also be sent out in left to right order sub xor5 { local(@x) = @_[0..4]; local(@y) = @_[5..9]; local(@results5) = (); for($j=0;$j<5;$j++) { if (shift(@x) eq shift(@y)) { push(@results5, '0'); else { push(@results5, '1'); return(@results5[0..4]); { local($st_data) = $ARGV[0]; local(@g) = ('0','0','1','0','1'); local(@data) = split (//,$st_data); local(@hold) = ('1','1','1','1','1'); if (scalar(@data) > 0) { loop5: while (scalar(@data) > 0) { 3

$nextb=shift(@data); if (($nextb ne "0") && ($nextb ne "1")) {next loop5 ## comment character if ($nextb eq shift(@hold)) {push(@hold, '0') else { push(@hold, '0'); @hold = &xor5(@hold,@g); ## print (@hold); print "\n"; ## print (@hold); print "\n"; ## invert shift reg contents to generate crc field for ($i=0;$i<=$#hold;$i++) {if (@hold[$i] eq "1") {print("0") else { print("1") print "\n"; #! /usr/local/bin/perl ## usage: ## crc16 nrzstream ## nrz stream is sent in left to right order ## generated crc should also be sent out in left to right order sub xor16 { local(@x) = @_[0..15]; local(@y) = @_[16..31]; local(@results16) = (); for($j=0;$j<16;$j++) { if (shift(@x) eq shift(@y)) { push(@results16, '0'); else { push(@results16, '1'); return(@results16[0..15]); { local($st_data) = $ARGV[0]; local(@g) = ('1','0','0','0','0','0','0','0','0','0','0','0','0','1','0','1'); local(@hold) = ('1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'); local(@data) = split (//,$st_data); if (scalar(@data) > 0) { loop16: while (scalar(@data) > 0) { $nextb=shift(@data); if (($nextb ne "0") && ($nextb ne "1")) {next loop16 ## comment character if ($nextb eq shift(@hold)) {push(@hold, '0') else { push(@hold, '0'); @hold = &xor16(@hold,@g); # print (@hold); print "\n"; ## invert shift reg to generate CRC field for ($i=0;$i<=$#hold;$i++) {if (@hold[$i] eq "1") {print("0") else { print("1") print "\n"; 4

Examples of CRC computation To further solidify the understanding of the implementation, the following examples are presented. Consider generating an SOF token with timestamp of 710(hex). CRC5 can be used to generate the CRC on the timestamp with the following results. crc5 00001000111 10100 The timestamp and CRC and EOP (End Of Packet) can be appended to the Sync, PID and PID check fields to generate the NRZ (Non Return to Zero) packet as follows. 00000001101001010000100011110100XX1; In this example the NRZ sync field is 00000001 and the EOP is indicated as XX1 where X indicates a single ended 0. The PID and PID check fields are 0101 and 1010 respectively. Note that bit stuffing and conversion to NRZI (Non Return to Zero Invert) will be performed (described in sec. 7.1.5 and sec. 7.1.6 of the spec) before the packet is transmitted on the USB. Since these operations do not affect the bit order or the CRC generation/checking they are not described in this paper. Examples of SETUP, OUT, IN and SOF tokens (with address, endpoint and timestamp values in hex), the crc5 generation and the resulting NRZ packet are presented below. setup addr 15 endp e crc5 10101000111 10111 00000001101101001010100011110111XX1; out addr 3a endp a crc5 01011100101 11100 00000001100001110101110010111100XX1; in addr 70 endp 4 crc5 00001110010 01110 00000001100101100000111001001110XX1; sof timestamp 001 crc5 10000000000 10111 5

00000001101001011000000000010111XX1; Examples of data packets are presented below. DATA0 and DATA1 packets, each with 4 bytes of data (indicated in hex), the crc16 generation and the resulting NRZ packets are listed respectively below. data0 00 01 02 03 crc16 00000000100000000100000011000000 1111011101011110 0000000111000011000000001000000001000000110000001111011101011110XX1; data1 23 45 67 89 crc16 11000100101000101110011010010001 0111000000111000 0000000111010010110001001010001011100110100100010111000000111000XX1; 6