xref: /illumos-gate/usr/src/uts/common/os/sctp_crc32.c (revision da14cebe)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*da14cebeSEric Cheng  * Common Development and Distribution License (the "License").
6*da14cebeSEric Cheng  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*da14cebeSEric Cheng  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Fast CRC32 calculation algorithm suggested by Ferenc Rakoczi
307c478bd9Sstevel@tonic-gate  * (ferenc.rakoczi@sun.com).  The basic idea is to look at it
317c478bd9Sstevel@tonic-gate  * four bytes (one word) at a time, using four tables.  The
327c478bd9Sstevel@tonic-gate  * standard algorithm in RFC 3309 uses one table.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * SCTP uses reflected/reverse polynomial CRC32 with generating
377c478bd9Sstevel@tonic-gate  * polynomial 0x1EDC6F41L
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate #define	SCTP_POLY 0x1EDC6F41L
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /* The four CRC tables. */
427c478bd9Sstevel@tonic-gate static uint32_t crctab[4][256];
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static uint32_t
reflect_32(uint32_t b)457c478bd9Sstevel@tonic-gate reflect_32(uint32_t b)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	int i;
487c478bd9Sstevel@tonic-gate 	uint32_t rw = 0;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++) {
517c478bd9Sstevel@tonic-gate 		if (b & 1) {
527c478bd9Sstevel@tonic-gate 			rw |= 1 << (31 - i);
537c478bd9Sstevel@tonic-gate 		}
547c478bd9Sstevel@tonic-gate 		b >>= 1;
557c478bd9Sstevel@tonic-gate 	}
567c478bd9Sstevel@tonic-gate 	return (rw);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * This function is only used for big endian processor.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static uint32_t
flip32(uint32_t w)657c478bd9Sstevel@tonic-gate flip32(uint32_t w)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	return (((w >> 24) | ((w >> 8) & 0xff00) | ((w << 8) & 0xff0000) |
68*da14cebeSEric Cheng 	    (w << 24)));
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate void
sctp_crc32_init(void)747c478bd9Sstevel@tonic-gate sctp_crc32_init(void)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	uint32_t i, j, k, crc;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	for (i = 0; i < 256; i++) {
797c478bd9Sstevel@tonic-gate 		crc = reflect_32(i);
807c478bd9Sstevel@tonic-gate 		for (k = 0; k < 4; k++) {
817c478bd9Sstevel@tonic-gate 			for (j = 0; j < 8; j++) {
827c478bd9Sstevel@tonic-gate 				crc = (crc & 0x80000000) ?
837c478bd9Sstevel@tonic-gate 				    (crc << 1) ^ SCTP_POLY : crc << 1;
847c478bd9Sstevel@tonic-gate 			}
857c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
867c478bd9Sstevel@tonic-gate 			crctab[3 - k][i] = flip32(reflect_32(crc));
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate 			crctab[k][i] = reflect_32(crc);
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static void
sctp_crc_byte(uint32_t * crcptr,const uint8_t * buf,int len)957c478bd9Sstevel@tonic-gate sctp_crc_byte(uint32_t *crcptr, const uint8_t *buf, int len)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	uint32_t crc;
987c478bd9Sstevel@tonic-gate 	int i;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	crc = *crcptr;
1017c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
1027c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1037c478bd9Sstevel@tonic-gate 		crc = (crc << 8) ^ crctab[3][buf[i] ^ (crc >> 24)];
1047c478bd9Sstevel@tonic-gate #else
1057c478bd9Sstevel@tonic-gate 		crc = (crc >> 8) ^ crctab[0][buf[i] ^ (crc & 0xff)];
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	*crcptr = crc;
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static void
sctp_crc_word(uint32_t * crcptr,const uint32_t * buf,int len)1127c478bd9Sstevel@tonic-gate sctp_crc_word(uint32_t *crcptr, const uint32_t *buf, int len)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	uint32_t w, crc;
1157c478bd9Sstevel@tonic-gate 	int i;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	crc = *crcptr;
1187c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
1197c478bd9Sstevel@tonic-gate 		w = crc ^ buf[i];
1207c478bd9Sstevel@tonic-gate 		crc = crctab[0][w >> 24] ^ crctab[1][(w >> 16) & 0xff] ^
1217c478bd9Sstevel@tonic-gate 		    crctab[2][(w >> 8) & 0xff] ^ crctab[3][w & 0xff];
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	*crcptr = crc;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate uint32_t
sctp_crc32(uint32_t crc32,const uint8_t * buf,int len)1277c478bd9Sstevel@tonic-gate sctp_crc32(uint32_t crc32, const uint8_t *buf, int len)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	int rem;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	rem = 4 - ((uintptr_t)buf) & 3;
1327c478bd9Sstevel@tonic-gate 	if (rem != 0) {
1337c478bd9Sstevel@tonic-gate 		if (len < rem) {
1347c478bd9Sstevel@tonic-gate 			rem = len;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		sctp_crc_byte(&crc32, buf, rem);
1377c478bd9Sstevel@tonic-gate 		buf = buf + rem;
1387c478bd9Sstevel@tonic-gate 		len = len - rem;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (len > 3) {
1427c478bd9Sstevel@tonic-gate 		sctp_crc_word(&crc32, (const uint32_t *)buf, len / 4);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	rem = len & 3;
1467c478bd9Sstevel@tonic-gate 	if (rem != 0) {
1477c478bd9Sstevel@tonic-gate 		sctp_crc_byte(&crc32, buf + len - rem, rem);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	return (crc32);
1507c478bd9Sstevel@tonic-gate }
151