X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-perl/blobdiff_plain/660b443cc58d4dd4e92730104429fb64d78c7075..f9952aec1cf6c64a5681308eea817b6113a37433:/Catacomb.pm diff --git a/Catacomb.pm b/Catacomb.pm index 154b3db..fd0e455 100644 --- a/Catacomb.pm +++ b/Catacomb.pm @@ -1,6 +1,6 @@ # -*-perl-*- # -# $Id: Catacomb.pm,v 1.1 2004/04/02 18:04:01 mdw Exp $ +# $Id$ # # Perl interface to Catacomb crypto library # @@ -25,13 +25,6 @@ # along with Catacomb/Perl; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#----- Revision history ----------------------------------------------------- -# -# $Log: Catacomb.pm,v $ -# Revision 1.1 2004/04/02 18:04:01 mdw -# Initial checkin. -# - #----- Basic stuff ---------------------------------------------------------- package Catacomb; @@ -65,13 +58,13 @@ use overload '*' => sub { _binop(\&mul, @_); }, '/' => sub { _binop(\&div, @_); }, '%' => sub { _binop(\&mod, @_); }, - '&' => sub { _binop(\&and, @_); }, - '|' => sub { _binop(\&or, @_); }, - '^' => sub { _binop(\&xor, @_); }, + '&' => sub { _binop(\&and2c, @_); }, + '|' => sub { _binop(\&or2c, @_); }, + '^' => sub { _binop(\&xor2c, @_); }, '**' => sub { _binop(\&pow, @_); }, - '>>' => sub { &lsr(@_[0, 1]); }, - '<<' => sub { &lsl(@_[0, 1]); }, - '~' => sub { ¬($_[0]) }, + '>>' => sub { &lsr2c(@_[0, 1]); }, + '<<' => sub { &lsl2c(@_[0, 1]); }, + '~' => sub { ¬2c($_[0]) }, '==' => sub { _binop(\&eq, @_); }, '<=>' => sub { _binop(\&cmp, @_); }, '""' => sub { &tostring($_[0]); }, @@ -119,6 +112,38 @@ sub modinv { return $i; } +#----- Binary polynomials --------------------------------------------------- + +package Catacomb::GF; +use Carp; + +@ISA = qw(Catacomb::MP); + +use overload + '+' => sub { _binop(\&add, @_); }, + '-' => sub { _binop(\&add, @_); }, + '*' => sub { _binop(\&mul, @_); }, + '/' => sub { _binop(\&div, @_); }, + '%' => sub { _binop(\&mod, @_); }, + '&' => sub { _binop(\&Catacomb::MP::and, @_); }, + '|' => sub { _binop(\&Catacomb::MP::or, @_); }, + '^' => sub { _binop(\&Catacomb::MP::xor, @_); }, + '>>' => sub { &Catacomb::MP::lsr(@_[0, 1]); }, + '<<' => sub { &Catacomb::MP::lsl(@_[0, 1]); }, + '~' => sub { &Catacomb::MP::not($_[0]) }, + '==' => sub { _binop(\&Catacomb::MP::eq, @_); }, + '<=>' => sub { _binop(\&Catacomb::MP::cmp, @_); }, + '""' => sub { "0x" . &Catacomb::MP::tostring($_[0], 16); }, + 'neg' => sub { $_[0]; }, + '0+' => sub { &Catacomb::MP::toint($_[0]); }; + +sub mod { (&div($_[0], $_[1]))[1]; } + +sub _binop { + my ($func, $a, $b, $flag) = @_; + return $flag ? &$func($b, $a) : &$func($a, $b); +} + #----- Prime testing -------------------------------------------------------- { @@ -172,6 +197,7 @@ sub decrypt { $iv = undef; } my $c = $cc->init($k); + $c->setiv($iv) if defined($iv); return $c->decrypt($p); }