From a4e7178b9f617c687d50bb427e2e79a5e54015d3 Mon Sep 17 00:00:00 2001 From: Tom Yu Date: Wed, 9 Jan 2002 00:20:17 +0000 Subject: [PATCH] * crc.pl: New file; perl script to do generate some test vectors and CRC tables. * CRC.pm: New file; perl module to implement CRCs in terms of polynomial arithmetic (verrrry slooow). * Poly.pm: New file; perl module to do polynomial arithmetic in the field of integers mod 2. * t_crc.c: New file; do some sanity checks (and timing checks, more useful when building shift-4 as well). * Makefile.in (check-unix): Add rules for building, running t_crc. * crc32.c (mit_crc32_shift4): Add new function, usually not compiled, for shift-4 implementation of CRC32. * crc-32.h: Add (conditionalized) prototype for the shift-4 function; remove checksum_entry (it's no longer used). * crctest.c: Removed. * crc-test: Removed. * crc.c: Removed. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@14095 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/crypto/crc32/CRC.pm | 156 ++++++++++++++++++++ src/lib/crypto/crc32/ChangeLog | 29 ++++ src/lib/crypto/crc32/Makefile.in | 7 + src/lib/crypto/crc32/Poly.pm | 182 +++++++++++++++++++++++ src/lib/crypto/crc32/crc-32.h | 6 +- src/lib/crypto/crc32/crc-test | 58 -------- src/lib/crypto/crc32/crc.c | 246 ------------------------------- src/lib/crypto/crc32/crc.pl | 111 ++++++++++++++ src/lib/crypto/crc32/crc32.c | 31 +++- src/lib/crypto/crc32/crctest.c | 99 ------------- src/lib/crypto/crc32/t_crc.c | 210 ++++++++++++++++++++++++++ 11 files changed, 730 insertions(+), 405 deletions(-) create mode 100644 src/lib/crypto/crc32/CRC.pm create mode 100644 src/lib/crypto/crc32/Poly.pm delete mode 100644 src/lib/crypto/crc32/crc-test delete mode 100644 src/lib/crypto/crc32/crc.c create mode 100644 src/lib/crypto/crc32/crc.pl delete mode 100644 src/lib/crypto/crc32/crctest.c create mode 100644 src/lib/crypto/crc32/t_crc.c diff --git a/src/lib/crypto/crc32/CRC.pm b/src/lib/crypto/crc32/CRC.pm new file mode 100644 index 000000000..ee2ab2ae8 --- /dev/null +++ b/src/lib/crypto/crc32/CRC.pm @@ -0,0 +1,156 @@ +# Copyright 2002 by the Massachusetts Institute of Technology. +# All Rights Reserved. +# +# Export of this software from the United States of America may +# require a specific license from the United States Government. +# It is the responsibility of any person or organization contemplating +# export to obtain such a license before exporting. +# +# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and +# distribute this software and its documentation for any purpose and +# without fee is hereby granted, provided that the above copyright +# notice appear in all copies and that both that copyright notice and +# this permission notice appear in supporting documentation, and that +# the name of M.I.T. not be used in advertising or publicity pertaining +# to distribution of the software without specific, written prior +# permission. Furthermore if you modify this software you must label +# your software as modified software and not distribute it in such a +# fashion that it might be confused with the original M.I.T. software. +# M.I.T. makes no representations about the suitability of +# this software for any purpose. It is provided "as is" without express +# or implied warranty. + +package CRC; + +# CRC: implement a CRC using the Poly package (yes this is slow) +# +# message M(x) = m_0 * x^0 + m_1 * x^1 + ... + m_(k-1) * x^(k-1) +# generator P(x) = p_0 * x^0 + p_1 * x^1 + ... + p_n * x^n +# remainder R(x) = r_0 * x^0 + r_1 * x^1 + ... + r_(n-1) * x^(n-1) +# +# R(x) = (x^n * M(x)) % P(x) +# +# Note that if F(x) = x^n * M(x) + R(x), then F(x) = 0 mod P(x) . +# +# In MIT Kerberos 5, R(x) is taken as the CRC, as opposed to what +# ISO 3309 does. +# +# ISO 3309 adds a precomplement and a postcomplement. +# +# The ISO 3309 postcomplement is of the form +# +# A(x) = x^0 + x^1 + ... + x^(n-1) . +# +# The ISO 3309 precomplement is of the form +# +# B(x) = x^k * A(x) . +# +# The ISO 3309 FCS is then +# +# (x^n * M(x)) % P(x) + B(x) % P(x) + A(x) , +# +# which is equivalent to +# +# (x^n * M(x) + B(x)) % P(x) + A(x) . +# +# In ISO 3309, the transmitted frame is +# +# F'(x) = x^n * M(x) + R(x) + R'(x) + A(x) , +# +# where +# +# R'(x) = B(x) % P(x) . +# +# Note that this means that if a new remainder is computed over the +# frame F'(x) (treating F'(x) as the new M(x)), it will be equal to a +# constant. +# +# F'(x) = 0 + R'(x) + A(x) mod P(x) , +# +# then +# +# (F'(x) + x^k * A(x)) * x^n +# +# = ((R'(x) + A(x)) + x^k * A(x)) * x^n mod P(x) +# +# = (x^k * A(x) + A(x) + x^k * A(x)) * x^n mod P(x) +# +# = (0 + A(x)) * x^n mod P(x) +# +# Note that (A(x) * x^n) % P(x) is a constant, and that this result +# depends on B(x) being x^k * A(x). + +use Carp; +use Poly; + +sub new { + my $self = shift; + my $class = ref($self) || $self; + my %args = @_; + $self = {bitsendian => "little"}; + bless $self, $class; + $self->setpoly($args{"Poly"}) if exists $args{"Poly"}; + $self->bitsendian($args{"bitsendian"}) + if exists $args{"bitsendian"}; + $self->{precomp} = $args{precomp} if exists $args{precomp}; + $self->{postcomp} = $args{postcomp} if exists $args{postcomp}; + return $self; +} + +sub setpoly { + my $self = shift; + my($arg) = @_; + croak "need a polynomial" if !$arg->isa("Poly"); + $self->{Poly} = $arg; + return $self; +} + +sub crc { + my $self = shift; + my $msg = Poly->new(@_); + my($order, $r, $precomp); + $order = $self->{Poly}->order; + # B(x) = x^k * precomp + $precomp = $self->{precomp} ? + $self->{precomp} * Poly->powers2poly(scalar(@_)) : Poly->new; + # R(x) = (x^n * M(x)) % P(x) + $r = ($msg * Poly->powers2poly($order)) % $self->{Poly}; + # B(x) % P(x) + $r += $precomp % $self->{Poly}; + $r += $self->{postcomp} if exists $self->{postcomp}; + return $r; +} + +# endianness of bits of each octet +# +# Note that the message is always treated as being sent in big-endian +# octet order. +# +# Usually, the message will be treated as bits being little-endian, +# since that is the common case for serial implementations that +# present data in octets; e.g., most UARTs shift octets onto the line +# in little-endian order, and protocols such as ISO 3309, V.42, +# etc. treat individual octets as being sent LSB-first. + +sub bitsendian { + my $self = shift; + my($arg) = @_; + croak "bad bit endianness" if $arg !~ /big|little/; + $self->{bitsendian} = $arg; + return $self; +} + +sub crcstring { + my $self = shift; + my($arg) = @_; + my($packstr, @m); + { + $packstr = "B*", last if $self->{bitsendian} =~ /big/; + $packstr = "b*", last if $self->{bitsendian} =~ /little/; + croak "bad bit endianness"; + }; + @m = split //, unpack $packstr, $arg; + return $self->crc(@m); +} + +1; diff --git a/src/lib/crypto/crc32/ChangeLog b/src/lib/crypto/crc32/ChangeLog index 696218b26..afa68c05b 100644 --- a/src/lib/crypto/crc32/ChangeLog +++ b/src/lib/crypto/crc32/ChangeLog @@ -1,3 +1,32 @@ +2002-01-07 Tom Yu + + * crc.pl: New file; perl script to do generate some test vectors + and CRC tables. + + * CRC.pm: New file; perl module to implement CRCs in terms of + polynomial arithmetic (verrrry slooow). + + * Poly.pm: New file; perl module to do polynomial arithmetic in + the field of integers mod 2. + + * t_crc.c: New file; do some sanity checks (and timing checks, + more useful when building shift-4 as well). + + * Makefile.in (check-unix): Add rules for building, running + t_crc. + + * crc32.c (mit_crc32_shift4): Add new function, usually not + compiled, for shift-4 implementation of CRC32. + + * crc-32.h: Add (conditionalized) prototype for the shift-4 + function; remove checksum_entry (it's no longer used). + + * crctest.c: Removed. + + * crc-test: Removed. + + * crc.c: Removed. + 2001-10-09 Ken Raeburn * crc.c: Make prototypes unconditional. diff --git a/src/lib/crypto/crc32/Makefile.in b/src/lib/crypto/crc32/Makefile.in index ebd5c3cb8..ee968a74a 100644 --- a/src/lib/crypto/crc32/Makefile.in +++ b/src/lib/crypto/crc32/Makefile.in @@ -28,6 +28,13 @@ includes:: depend depend:: $(SRCS) clean-unix:: clean-libobjs + +check-unix:: t_crc + ./t_crc + +t_crc: t_crc.o crc32.o + $(CC_LINK) -o $@ t_crc.o crc32.o + # +++ Dependency line eater +++ # # Makefile dependencies follow. This must be the last section in diff --git a/src/lib/crypto/crc32/Poly.pm b/src/lib/crypto/crc32/Poly.pm new file mode 100644 index 000000000..cad0f77b5 --- /dev/null +++ b/src/lib/crypto/crc32/Poly.pm @@ -0,0 +1,182 @@ +# Copyright 2002 by the Massachusetts Institute of Technology. +# All Rights Reserved. +# +# Export of this software from the United States of America may +# require a specific license from the United States Government. +# It is the responsibility of any person or organization contemplating +# export to obtain such a license before exporting. +# +# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and +# distribute this software and its documentation for any purpose and +# without fee is hereby granted, provided that the above copyright +# notice appear in all copies and that both that copyright notice and +# this permission notice appear in supporting documentation, and that +# the name of M.I.T. not be used in advertising or publicity pertaining +# to distribution of the software without specific, written prior +# permission. Furthermore if you modify this software you must label +# your software as modified software and not distribute it in such a +# fashion that it might be confused with the original M.I.T. software. +# M.I.T. makes no representations about the suitability of +# this software for any purpose. It is provided "as is" without express +# or implied warranty. + +package Poly; + +# Poly: implements some basic operations on polynomials in the field +# of integers (mod 2). +# +# The rep is an array of coefficients, highest order term first. +# +# This is rather slow at the moment. + +use overload + '+' => \&add, + '-' => \&add, + '*' => \&mul, + '%' => sub {$_[2] ? mod($_[1], $_[0]) : mod($_[0], $_[1])}, + '/' => sub { $_[2] ? scalar(div($_[1], $_[0])) + : scalar(div($_[0], $_[1])) }, + '<=>' => sub {$_[2] ? pcmp($_[1], $_[0]) : pcmp($_[0], $_[1])}, + '""' => \&str +; + +use Carp; + +# doesn't do much beyond normalize and bless +sub new { + my $this = shift; + my $class = ref($this) || $this; + my(@x) = @_; + return bless [norm(@x)], $class; +} + +# stringified P(x) +sub pretty { + my(@x) = @{+shift}; + my $n = @x; + local $_; + return "0" if !@x; + return join " + ", map {$n--; $_ ? ("x^$n") : ()} @x; +} + +sub print { + my $self = shift; + print $self->pretty, "\n"; +} + +# This assumes normalization. +sub order { + my $self = shift; + return $#{$self}; +} + +sub str { + return overload::StrVal($_[0]); +} + +# strip leading zero coefficients +sub norm { + my(@x) = @_; + shift @x while @x && !$x[0]; + return @x; +} + +# multiply $self by the single term of power $n +sub multerm { + my($self, $n) = @_; + return $self->new(@$self, (0) x $n); +} + +# This is really an order comparison; different polys of same order +# compare equal. It also assumes prior normalization. +sub pcmp { + my @x = @{+shift}; + my @y = @{+shift}; + return @x <=> @y; +} + +# convenience constructor; takes list of non-zero terms +sub powers2poly +{ + my $self = shift; + my $poly = $self->new; + my $n; + foreach $n (@_) { + $poly += $one->multerm($n); + } + return $poly; +} + +sub add { + my $self = shift; + my @x = @$self; + my @y = @{+shift}; + my @r; + unshift @r, (pop @x || 0) ^ (pop @y || 0) + while @x || @y; + return $self->new(@r); +} + +sub mul { + my($self) = shift; + my @y = @{+shift}; + my $r = $self->new; + my $power = 0; + while (@y) { + $r += $self->multerm($power) if pop @y; + $power++; + } + return $r; +} + +sub oldmod { + my($self, $div) = @_; + my @num = @$self; + my @div = @$div; + my $r = $self->new(splice @num, 0, @div); + do { + push @$r, shift @num while @num && $r < $div; + $r += $div if $r >= $div; + } while @num; + return $r; +} + +sub div { + my($self, $div) = @_; + my $q = $self->new; + my $r = $self->new(@$self); + my $one = $self->new(1); + my ($tp, $power); + croak "divide by zero" if !@$div; + while ($div <= $r) { + $power = 0; + $power++ while ($tp = $div->multerm($power)) < $r; + $q += $one->multerm($power); + $r -= $tp; + } + return wantarray ? ($q, $r) : $q; +} + +sub mod { + (&div)[1]; +} + +# bits and octets both big-endian +sub hex { + my @x = @{+shift}; + my $minwidth = shift || 32; + unshift @x, 0 while @x % 8 || @x < $minwidth; + return unpack "H*", pack "B*", join "", @x; +} + +# bit-reversal of above +sub revhex { + my @x = @{+shift}; + my $minwidth = shift || 32; + unshift @x, 0 while @x % 8 || @x < $minwidth; + return unpack "H*", pack "B*", join "", reverse @x; +} + +$one = Poly->new(1); + +1; diff --git a/src/lib/crypto/crc32/crc-32.h b/src/lib/crypto/crc32/crc-32.h index 22a5a38e1..4e8f15c21 100644 --- a/src/lib/crypto/crc32/crc-32.h +++ b/src/lib/crypto/crc32/crc-32.h @@ -62,6 +62,10 @@ void mit_crc32 (const krb5_pointer in, const size_t in_length, unsigned long *c); -extern krb5_checksum_entry crc32_cksumtable_entry; +#ifdef CRC32_SHIFT4 +void mit_crc32_shift4(const krb5_pointer /* in */, + const size_t /* in_length */, + unsigned long * /* cksum */); +#endif #endif /* KRB5_CRC32__ */ diff --git a/src/lib/crypto/crc32/crc-test b/src/lib/crypto/crc32/crc-test deleted file mode 100644 index 22920e629..000000000 --- a/src/lib/crypto/crc32/crc-test +++ /dev/null @@ -1,58 +0,0 @@ -01 77073096 -02 ee0e612c -04 076dc419 -08 0edb8832 -10 1db71064 -20 3b6e20c8 -40 76dc4190 -80 edb88320 - -0001 191b3141 -0002 32366282 -0004 646cc504 -0008 c8d98a08 -0010 4ac21251 -0020 958424a2 -0040 f0794f05 -0080 3b83984b -0100 77073096 -0200 ee0e612c -0400 076dc419 -0800 0edb8832 -1000 1db71064 -2000 3b6e20c8 -4000 76dc4190 -8000 edb88320 - -00000001 b8bc6765 -00000002 aa09c88b -00000004 8f629757 -00000008 c5b428ef -00000010 5019579f -00000020 a032af3e -00000040 9b14583d -00000080 ed59b63b -00000100 01c26a37 -00000200 0384d46e -00000400 0709a8dc -00000800 0e1351b8 -00001000 1c26a370 -00002000 384d46e0 -00004000 709a8dc0 -00008000 e1351b80 -00010000 191b3141 -00020000 32366282 -00040000 646cc504 -00080000 c8d98a08 -00100000 4ac21251 -00200000 958424a2 -00400000 f0794f05 -00800000 3b83984b -01000000 77073096 -02000000 ee0e612c -04000000 076dc419 -08000000 0edb8832 -10000000 1db71064 -20000000 3b6e20c8 -40000000 76dc4190 -80000000 edb88320 diff --git a/src/lib/crypto/crc32/crc.c b/src/lib/crypto/crc32/crc.c deleted file mode 100644 index b0f724021..000000000 --- a/src/lib/crypto/crc32/crc.c +++ /dev/null @@ -1,246 +0,0 @@ -/* - * lib/crypto/crc32/crc.c - * - * Copyright 1990 by the Massachusetts Institute of Technology. - * All Rights Reserved. - * - * Export of this software from the United States of America may - * require a specific license from the United States Government. - * It is the responsibility of any person or organization contemplating - * export to obtain such a license before exporting. - * - * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and - * distribute this software and its documentation for any purpose and - * without fee is hereby granted, provided that the above copyright - * notice appear in all copies and that both that copyright notice and - * this permission notice appear in supporting documentation, and that - * the name of M.I.T. not be used in advertising or publicity pertaining - * to distribution of the software without specific, written prior - * permission. Furthermore if you modify this software you must label - * your software as modified software and not distribute it in such a - * fashion that it might be confused with the original M.I.T. software. - * M.I.T. makes no representations about the suitability of - * this software for any purpose. It is provided "as is" without express - * or implied warranty. - * - * - * CRC-32/AUTODIN-II routines - */ - -#include "k5-int.h" -#include "crc-32.h" - -/* This table and block of comments are taken from code labeled: */ -/* - * Copyright (C) 1986 Gary S. Brown. You may use this program, or - * code or tables extracted from it, as desired without restriction. - */ - -/* First, the polynomial itself and its table of feedback terms. The */ -/* polynomial is */ -/* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 */ -/* Note that we take it "backwards" and put the highest-order term in */ -/* the lowest-order bit. The X^32 term is "implied"; the LSB is the */ -/* X^31 term, etc. The X^0 term (usually shown as "+1") results in */ -/* the MSB being 1. */ - -/* Note that the usual hardware shift register implementation, which */ -/* is what we're using (we're merely optimizing it by doing eight-bit */ -/* chunks at a time) shifts bits into the lowest-order term. In our */ -/* implementation, that means shifting towards the right. Why do we */ -/* do it this way? Because the calculated CRC must be transmitted in */ -/* order from highest-order term to lowest-order term. UARTs transmit */ -/* characters in order from LSB to MSB. By storing the CRC this way, */ -/* we hand it to the UART in the order low-byte to high-byte; the UART */ -/* sends each low-bit to hight-bit; and the result is transmission bit */ -/* by bit from highest- to lowest-order term without requiring any bit */ -/* shuffling on our part. Reception works similarly. */ - -/* The feedback terms table consists of 256, 32-bit entries. Notes: */ -/* */ -/* 1. The table can be generated at runtime if desired; code to do so */ -/* is shown later. It might not be obvious, but the feedback */ -/* terms simply represent the results of eight shift/xor opera- */ -/* tions for all combinations of data and CRC register values. */ -/* */ -/* 2. The CRC accumulation logic is the same for all CRC polynomials, */ -/* be they sixteen or thirty-two bits wide. You simply choose the */ -/* appropriate table. Alternatively, because the table can be */ -/* generated at runtime, you can start by generating the table for */ -/* the polynomial in question and use exactly the same "updcrc", */ -/* if your application needn't simultaneously handle two CRC */ -/* polynomials. (Note, however, that XMODEM is strange.) */ -/* */ -/* 3. For 16-bit CRCs, the table entries need be only 16 bits wide; */ -/* of course, 32-bit entries work OK if the high 16 bits are zero. */ -/* */ -/* 4. The values must be right-shifted by eight bits by the "updcrc" */ -/* logic; the shift must be unsigned (bring in zeroes). On some */ -/* hardware you could probably optimize the shift in assembler by */ -/* using byte-swap instructions. */ - -static u_long const crc_table[256] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, - 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, - 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, - 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, - 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, - 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, - 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, - 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, - 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, - 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, - 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, - 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, - 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, - 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, - 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, - 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, - 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, - 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, - 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, - 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, - 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, - 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, - 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, - 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, - 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, - 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, - 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, - 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, - 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, - 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, - 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, - 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, - 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, - 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, - 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, - 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, - 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, - 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, - 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, - 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, - 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, - 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, - 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d - }; - -/* Windows needs to these prototypes for crc32_cksumtable_entry below */ - -static krb5_error_code -crc32_sum_func ( - const krb5_pointer in, - const size_t in_length, - const krb5_pointer seed, - const size_t seed_length, - krb5_checksum *outcksum); - -static krb5_error_code -crc32_verify_func ( - const krb5_checksum *cksum, - const krb5_pointer in, - const size_t in_length, - const krb5_pointer seed, - const size_t seed_length); - -static krb5_error_code -crc32_sum_func(in, in_length, seed, seed_length, outcksum) - const krb5_pointer in; - const size_t in_length; - const krb5_pointer seed; - const size_t seed_length; - krb5_checksum *outcksum; -{ - register u_char *data; - register u_long c = 0; - register int idx; - size_t i; - - if (outcksum->length < CRC32_CKSUM_LENGTH) - return KRB5_BAD_MSIZE; - - data = (u_char *)in; - for (i = 0; i < in_length; i++) { - idx = (int) (data[i] ^ c); - idx &= 0xff; - c >>= 8; - c ^= crc_table[idx]; - } - /* c now holds the result */ - outcksum->checksum_type = CKSUMTYPE_CRC32; - outcksum->length = CRC32_CKSUM_LENGTH; - outcksum->contents[0] = (krb5_octet) (c & 0xff); - outcksum->contents[1] = (krb5_octet) ((c >> 8) & 0xff); - outcksum->contents[2] = (krb5_octet) ((c >> 16) & 0xff); - outcksum->contents[3] = (krb5_octet) ((c >> 24) & 0xff); - return 0; -} - -static krb5_error_code -crc32_verify_func(cksum, in, in_length, seed, seed_length) - const krb5_checksum *cksum; - const krb5_pointer in; - const size_t in_length; - const krb5_pointer seed; - const size_t seed_length; -{ - register u_char *data; - register u_long c = 0; - register int idx; - size_t i; - krb5_error_code retval; - - retval = 0; - if (cksum->checksum_type == CKSUMTYPE_CRC32) { - if (cksum->length == CRC32_CKSUM_LENGTH) { - data = (u_char *)in; - for (i = 0; i < in_length; i++) { - idx = (int) (data[i] ^ c); - idx &= 0xff; - c >>= 8; - c ^= crc_table[idx]; - } - if ((cksum->contents[0] != (krb5_octet) (c & 0xff)) || - (cksum->contents[1] != (krb5_octet) ((c >> 8) & 0xff)) || - (cksum->contents[2] != (krb5_octet) ((c >> 16) & 0xff)) || - (cksum->contents[3] != (krb5_octet) ((c >> 24) & 0xff))) - retval = KRB5KRB_AP_ERR_BAD_INTEGRITY; - } - else - retval = KRB5KRB_AP_ERR_BAD_INTEGRITY; - } - else - retval = KRB5KRB_AP_ERR_INAPP_CKSUM; - return(retval); -} - - -krb5_checksum_entry crc32_cksumtable_entry = { - 0, - crc32_sum_func, - crc32_verify_func, - CRC32_CKSUM_LENGTH, /* CRC-32 is 4 octets */ - 0, /* not collision proof */ - 0, /* doesn't use key */ -}; diff --git a/src/lib/crypto/crc32/crc.pl b/src/lib/crypto/crc32/crc.pl new file mode 100644 index 000000000..b21b6b15d --- /dev/null +++ b/src/lib/crypto/crc32/crc.pl @@ -0,0 +1,111 @@ +# Copyright 2002 by the Massachusetts Institute of Technology. +# All Rights Reserved. +# +# Export of this software from the United States of America may +# require a specific license from the United States Government. +# It is the responsibility of any person or organization contemplating +# export to obtain such a license before exporting. +# +# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and +# distribute this software and its documentation for any purpose and +# without fee is hereby granted, provided that the above copyright +# notice appear in all copies and that both that copyright notice and +# this permission notice appear in supporting documentation, and that +# the name of M.I.T. not be used in advertising or publicity pertaining +# to distribution of the software without specific, written prior +# permission. Furthermore if you modify this software you must label +# your software as modified software and not distribute it in such a +# fashion that it might be confused with the original M.I.T. software. +# M.I.T. makes no representations about the suitability of +# this software for any purpose. It is provided "as is" without express +# or implied warranty. + +use CRC; + +print "*** crudely testing polynomial functions ***\n"; + +$x = Poly->new(1,1,1,1); +$y = Poly->new(1,1); +print "x = @{[$x->pretty]}\ny = @{[$y->pretty]}\n"; +$q = $x / $y; +$r = $x % $y; +print $x->pretty, " = (", $y->pretty , ") * (", $q->pretty, + ") + ", $r->pretty, "\n"; +$q = $y / $x; +$r = $y % $x; +print "y / x = @{[$q->pretty]}\ny % x = @{[$r->pretty]}\n"; + +# ISO 3309 32-bit FCS polynomial +$fcs32 = Poly->powers2poly(32,26,23,22,16,12,11,10,8,7,5,4,2,1,0); +print "fcs32 = ", $fcs32->pretty, "\n"; + +$crc = CRC->new(Poly => $fcs32, bitsendian => "little"); + +print "\n"; + +print "*** little endian, no complementation ***\n"; +for ($i = 0; $i < 256; $i++) { + $r = $crc->crcstring(pack "C", $i); + printf ("%02x: ", $i) if !($i % 8); + print ($r->revhex, ($i % 8 == 7) ? "\n" : " "); +} + +print "\n"; + +print "*** little endian, 4 bits, no complementation ***\n"; +for ($i = 0; $i < 16; $i++) { + @m = (split //, unpack "b*", pack "C", $i)[0..3]; + $r = $crc->crc(@m); + printf ("%02x: ", $i) if !($i % 8); + print ($r->revhex, ($i % 8 == 7) ? "\n" : " "); +} + +print "\n"; + +print "*** test vectors for t_crc.c, little endian ***\n"; +for ($i = 1; $i <= 4; $i *=2) { + for ($j = 0; $j < $i * 8; $j++) { + @m = split //, unpack "b*", pack "V", 1 << $j; + splice @m, $i * 8; + $r = $crc->crc(@m); + $m = unpack "H*", pack "b*", join("", @m); + print "{HEX, \"$m\", 0x", $r->revhex, "},\n"; + } +} +@m = ("foo", "test0123456789", + "MASSACHVSETTS INSTITVTE OF TECHNOLOGY"); +foreach $m (@m) { + $r = $crc->crcstring($m); + print "{STR, \"$m\", 0x", $r->revhex, "},\n"; +} +__END__ + +print "*** big endian, no complementation ***\n"; +for ($i = 0; $i < 256; $i++) { + $r = $crc->crcstring(pack "C", $i); + printf ("%02x: ", $i) if !($i % 8); + print ($r->hex, ($i % 8 == 7) ? "\n" : " "); +} + +# all ones polynomial of order 31 +$ones = Poly->new((1) x 32); + +print "*** big endian, ISO-3309 style\n"; +$crc = CRC->new(Poly => $fcs32, + bitsendian => "little", + precomp => $ones, + postcomp => $ones); +for ($i = 0; $i < 256; $i++) { + $r = $crc->crcstring(pack "C", $i); + print ($r->hex, ($i % 8 == 7) ? "\n" : " "); +} + +for ($i = 0; $i < 0; $i++) { + $x = Poly->new((1) x 32, (0) x $i); + $y = Poly->new((1) x 32); + $f = ($x % $fcs32) + $y; + $r = (($f + $x) * Poly->powers2poly(32)) % $fcs32; + @out = @$r; + unshift @out, 0 while @out < 32; + print @out, "\n"; +} diff --git a/src/lib/crypto/crc32/crc32.c b/src/lib/crypto/crc32/crc32.c index 00be8fd65..f23e65ff1 100644 --- a/src/lib/crypto/crc32/crc32.c +++ b/src/lib/crypto/crc32/crc32.c @@ -1,7 +1,7 @@ /* * lib/crypto/crc32/crc.c * - * Copyright 1990 by the Massachusetts Institute of Technology. + * Copyright 1990, 2002 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -167,3 +167,32 @@ mit_crc32(in, in_length, cksum) *cksum = c; } + +#ifdef CRC32_SHIFT4 +static unsigned long const tbl4[16] = { + 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, + 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, + 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c +}; + +void +mit_crc32_shift4(in, in_length, cksum) + const krb5_pointer in; + const size_t in_length; + unsigned long *cksum; +{ + register unsigned char *data, b; + register unsigned long c = 0; + size_t i; + + data = (u_char *)in; + for (i = 0; i < in_length; i++) { + b = data[i]; + c = (c >> 4) ^ tbl4[(b ^ c) & 0x0f]; + b >>= 4; + c = (c >> 4) ^ tbl4[(b ^ c) & 0x0f]; + } + *cksum = c; +} +#endif diff --git a/src/lib/crypto/crc32/crctest.c b/src/lib/crypto/crc32/crctest.c deleted file mode 100644 index 2f9265a48..000000000 --- a/src/lib/crypto/crc32/crctest.c +++ /dev/null @@ -1,99 +0,0 @@ -/* - * lib/crypto/crc32/crctest.c - * - * Copyright 1990 by the Massachusetts Institute of Technology. - * All Rights Reserved. - * - * Export of this software from the United States of America may - * require a specific license from the United States Government. - * It is the responsibility of any person or organization contemplating - * export to obtain such a license before exporting. - * - * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and - * distribute this software and its documentation for any purpose and - * without fee is hereby granted, provided that the above copyright - * notice appear in all copies and that both that copyright notice and - * this permission notice appear in supporting documentation, and that - * the name of M.I.T. not be used in advertising or publicity pertaining - * to distribution of the software without specific, written prior - * permission. Furthermore if you modify this software you must label - * your software as modified software and not distribute it in such a - * fashion that it might be confused with the original M.I.T. software. - * M.I.T. makes no representations about the suitability of - * this software for any purpose. It is provided "as is" without express - * or implied warranty. - * - * - * CRC test driver program. - */ - - -#include "k5-int.h" -#include "crc-32.h" -#include - -void -main() -{ - unsigned char ckout[4]; - krb5_checksum outck; - - char input[16], expected_crc[16]; - unsigned char inbytes[4], outbytes[4]; - int in_length; - unsigned long expect; - - int bad = 0; - - outck.length = sizeof(ckout); - outck.contents = ckout; - - while (scanf("%s %s", input, expected_crc) == 2) { - in_length = strlen(input); - if (in_length % 2) { - fprintf(stderr, "bad input '%s', not hex data\n", input); - exit(1); - } - in_length = in_length / 2; - if (strlen(expected_crc) != 8) { - fprintf(stderr, "bad expectation '%s', not 8 chars\n", - expected_crc); - exit(1); - } - if (sscanf(expected_crc, "%lx", &expect) != 1) { - fprintf(stderr, "bad expectation '%s', not 4bytes hex\n", - expected_crc); - exit(1); - } - outbytes[0] = (unsigned char) (expect & 0xff); - outbytes[1] = (unsigned char) ((expect >> 8) & 0xff); - outbytes[2] = (unsigned char) ((expect >> 16) & 0xff); - outbytes[3] = (unsigned char) ((expect >> 24) & 0xff); - - if (sscanf(input, "%lx", &expect) != 1) { - fprintf(stderr, "bad expectation '%s', not hex\n", - expected_crc); - exit(1); - } - inbytes[0] = (unsigned char) (expect & 0xff); - inbytes[1] = (unsigned char) ((expect >> 8) & 0xff); - inbytes[2] = (unsigned char) ((expect >> 16) & 0xff); - inbytes[3] = (unsigned char) ((expect >> 24) & 0xff); - - (*crc32_cksumtable_entry.sum_func)((krb5_pointer)inbytes, - in_length, 0, 0, &outck); - if (memcmp(outbytes, ckout, 4)) { - printf("mismatch: input '%s', output '%02x%02x%02x%02x', \ -expected '%s'\n", - input, ckout[3], ckout[2], ckout[1], ckout[0], - expected_crc); - bad = 1; - } - } - if (bad) - printf("crctest: failed to pass the test\n"); - else - printf("crctest: test is passed successfully\n"); - - exit(bad); -} diff --git a/src/lib/crypto/crc32/t_crc.c b/src/lib/crypto/crc32/t_crc.c new file mode 100644 index 000000000..750f30ccd --- /dev/null +++ b/src/lib/crypto/crc32/t_crc.c @@ -0,0 +1,210 @@ +/* + * lib/crypto/crc32/t_crc.c + * + * Copyright 2002 by the Massachusetts Institute of Technology. + * All Rights Reserved. + * + * Export of this software from the United States of America may + * require a specific license from the United States Government. + * It is the responsibility of any person or organization contemplating + * export to obtain such a license before exporting. + * + * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. Furthermore if you modify this software you must label + * your software as modified software and not distribute it in such a + * fashion that it might be confused with the original M.I.T. software. + * M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * Sanity checks for CRC32. + */ +#include +#include +#include +#include +#include +#include "k5-int.h" +#include "crc-32.h" + +#define HEX 1 +#define STR 2 +struct crc_trial { + int type; + char *data; + unsigned long sum; +}; + +struct crc_trial trials[] = { + {HEX, "01", 0x77073096}, + {HEX, "02", 0xee0e612c}, + {HEX, "04", 0x076dc419}, + {HEX, "08", 0x0edb8832}, + {HEX, "10", 0x1db71064}, + {HEX, "20", 0x3b6e20c8}, + {HEX, "40", 0x76dc4190}, + {HEX, "80", 0xedb88320}, + {HEX, "0100", 0x191b3141}, + {HEX, "0200", 0x32366282}, + {HEX, "0400", 0x646cc504}, + {HEX, "0800", 0xc8d98a08}, + {HEX, "1000", 0x4ac21251}, + {HEX, "2000", 0x958424a2}, + {HEX, "4000", 0xf0794f05}, + {HEX, "8000", 0x3b83984b}, + {HEX, "0001", 0x77073096}, + {HEX, "0002", 0xee0e612c}, + {HEX, "0004", 0x076dc419}, + {HEX, "0008", 0x0edb8832}, + {HEX, "0010", 0x1db71064}, + {HEX, "0020", 0x3b6e20c8}, + {HEX, "0040", 0x76dc4190}, + {HEX, "0080", 0xedb88320}, + {HEX, "01000000", 0xb8bc6765}, + {HEX, "02000000", 0xaa09c88b}, + {HEX, "04000000", 0x8f629757}, + {HEX, "08000000", 0xc5b428ef}, + {HEX, "10000000", 0x5019579f}, + {HEX, "20000000", 0xa032af3e}, + {HEX, "40000000", 0x9b14583d}, + {HEX, "80000000", 0xed59b63b}, + {HEX, "00010000", 0x01c26a37}, + {HEX, "00020000", 0x0384d46e}, + {HEX, "00040000", 0x0709a8dc}, + {HEX, "00080000", 0x0e1351b8}, + {HEX, "00100000", 0x1c26a370}, + {HEX, "00200000", 0x384d46e0}, + {HEX, "00400000", 0x709a8dc0}, + {HEX, "00800000", 0xe1351b80}, + {HEX, "00000100", 0x191b3141}, + {HEX, "00000200", 0x32366282}, + {HEX, "00000400", 0x646cc504}, + {HEX, "00000800", 0xc8d98a08}, + {HEX, "00001000", 0x4ac21251}, + {HEX, "00002000", 0x958424a2}, + {HEX, "00004000", 0xf0794f05}, + {HEX, "00008000", 0x3b83984b}, + {HEX, "00000001", 0x77073096}, + {HEX, "00000002", 0xee0e612c}, + {HEX, "00000004", 0x076dc419}, + {HEX, "00000008", 0x0edb8832}, + {HEX, "00000010", 0x1db71064}, + {HEX, "00000020", 0x3b6e20c8}, + {HEX, "00000040", 0x76dc4190}, + {HEX, "00000080", 0xedb88320}, + {STR, "foo", 0x7332bc33}, + {STR, "test0123456789", 0xb83e88d6}, + {STR, "MASSACHVSETTS INSTITVTE OF TECHNOLOGY", 0xe34180f7} +}; + +#define NTRIALS (sizeof(trials) / sizeof(trials[0])) + +void +timetest(int nblk, int blksiz) +{ + char *block; + int i; + struct tms before, after; + unsigned long cksum; + + block = malloc(blksiz * nblk); + if (block == NULL) + exit(1); + for (i = 0; i < blksiz * nblk; i++) + block[i] = i % 256; + times(&before); + for (i = 0; i < nblk; i++) { + mit_crc32(block + i * blksiz, blksiz, &cksum); + } + + times(&after); + printf("shift-8 implementation, %d blocks of %d bytes:\n", + nblk, blksiz); + printf("\tu=%ld s=%ld cu=%ld cs=%ld\n", + (long)(after.tms_utime - before.tms_utime), + (long)(after.tms_stime - before.tms_stime), + (long)(after.tms_cutime - before.tms_cutime), + (long)(after.tms_cstime - before.tms_cstime)); + +#ifdef CRC32_SHIFT4 + times(&before); + for (i = 0; i < nblk; i++) { + mit_crc32_shift4(block + i * blksiz, blksiz, &cksum); + } + times(&after); + printf("shift-4 implementation, %d blocks of %d bytes:\n", + nblk, blksiz); + printf("\tu=%ld s=%ld cu=%ld cs=%ld\n", + (long)(after.tms_utime - before.tms_utime), + (long)(after.tms_stime - before.tms_stime), + (long)(after.tms_cutime - before.tms_cutime), + (long)(after.tms_cstime - before.tms_cstime)); +#endif +} + +void gethexstr(char *data, size_t *outlen, unsigned char *outbuf, + size_t buflen) +{ + size_t inlen; + char *cp, buf[3]; + long n; + + inlen = strlen(data); + *outlen = 0; + for (cp = data; cp - data < inlen; cp += 2) { + strncpy(buf, cp, 2); + buf[2] = '\0'; + n = strtol(buf, NULL, 16); + outbuf[(*outlen)++] = n; + if (*outlen > buflen) + break; + } +} + +void +verify(void) +{ + int i; + struct crc_trial trial; + unsigned char buf[4]; + size_t len; + unsigned long cksum; + char *typestr; + + for (i = 0; i < NTRIALS; i++) { + trial = trials[i]; + switch (trial.type) { + case STR: + len = strlen(trial.data); + typestr = "STR"; + mit_crc32(trial.data, len, &cksum); + break; + case HEX: + typestr = "HEX"; + gethexstr(trial.data, &len, &buf, 4); + mit_crc32(buf, len, &cksum); + break; + default: + typestr = "BOGUS"; + fprintf(stderr, "bad trial type %d\n", trial.type); + exit(1); + } + printf("%s: %s \"%s\" = 0x%08lx\n", + (trial.sum == cksum) ? "OK" : "***BAD***", + typestr, trial.data, cksum); + } +} + +int +main(void) +{ + timetest(64*1024, 1024); + verify(); + exit(0); +} -- 2.26.2