3 # $Id: Catacomb.pm,v 1.3 2004/04/18 15:05:08 mdw Exp $
5 # Perl interface to Catacomb crypto library
7 # (c) 2001 Straylight/Edgeware
10 #----- Licensing notice -----------------------------------------------------
12 # This file is part of the Perl interface to Catacomb.
14 # Catacomb/Perl is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # Catacomb/Perl is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with Catacomb/Perl; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #----- Basic stuff ----------------------------------------------------------
33 @ISA = qw(DynaLoader Exporter);
39 @EXPORT_OK = qw($random);
40 %EXPORT_TAGS = ("const" => [qw(GRAND_CRYPTO PGEN_TRY PGEN_FAIL PGEN_PASS
41 PGEN_DONE PGEN_ABORT PGEN_BEGIN)]);
42 Exporter::export_ok_tags("const");
45 my $val = const($AUTOLOAD);
46 *$AUTOLOAD = sub { $val };
50 #----- Multiprecision arithmetic --------------------------------------------
56 '+' => sub { _binop(\&add, @_); },
57 '-' => sub { _binop(\&sub, @_); },
58 '*' => sub { _binop(\&mul, @_); },
59 '/' => sub { _binop(\&div, @_); },
60 '%' => sub { _binop(\&mod, @_); },
61 '&' => sub { _binop(\&and, @_); },
62 '|' => sub { _binop(\&or, @_); },
63 '^' => sub { _binop(\&xor, @_); },
64 '**' => sub { _binop(\&pow, @_); },
65 '>>' => sub { &lsr(@_[0, 1]); },
66 '<<' => sub { &lsl(@_[0, 1]); },
67 '~' => sub { ¬($_[0]) },
68 '==' => sub { _binop(\&eq, @_); },
69 '<=>' => sub { _binop(\&cmp, @_); },
70 '""' => sub { &tostring($_[0]); },
71 '0+' => sub { &toint($_[0]); },
72 'sqrt' => sub { &sqrt($_[0]); },
73 'neg' => sub { &neg($_[0]); };
75 sub mod { (&div($_[0], $_[1]))[1]; }
78 croak("Usage: Catacomb::MP::pow(a, b)") unless @_ == 2;
80 my $r = Catacomb::MP->new(1);
90 my ($func, $a, $b, $flag) = @_;
91 return $flag ? &$func($b, $a) : &$func($a, $b);
95 croak("Usage: Catacomb::MP::modexp(p, g, x)") unless @_ == 3;
97 $g = $p - $g if $g < 0;
98 $g = $g % $p if $g > $p;
100 my $mm = Catacomb::MP::Mont->new($p);
101 return $mm->exp($g, $x);
103 my $mb = Catacomb::MP::Barrett->new($p);
104 return $mb->exp($g, $x);
109 croak("Usage: Catacomb::MP::modinv(p, x)") unless @_ == 2;
110 my ($g, undef, $i) = gcd($_[0], $_[1]);
111 croak("Arguments aren't coprime in Catacomb::MP::modinv") unless $g == 1;
115 #----- Prime testing --------------------------------------------------------
118 my $cmpg = "Catacomb::MP::Prime::Gen";
119 foreach my $i (qw(FilterStepper JumpStepper RabinTester)) {
120 @{"${cmpg}::${i}::ISA"} = ("${cmpg}::MagicProc");
122 @{"${cmpg}::MagicProc::ISA"} = ("${cmpg}::Proc");
125 #----- Crypto algorithms ----------------------------------------------------
129 foreach my $i (qw(Cipher Hash MAC)) {
132 my $cl = "Catacomb::${i}Class";
133 foreach my $c (&{"${cl}::list"}($cl)) {
134 my $x = $c->name(); $x =~ tr/a-zA-Z0-9/_/cs;
135 ${"Catacomb::${i}::${x}"} = undef; # SUYB
136 ${"Catacomb::${i}::${x}"} = $c;
137 push(@v, "\$Catacomb::${i}::${x}");
139 $EXPORT_TAGS{$tag} = \@v;
140 Exporter::export_ok_tags($tag);
143 package Catacomb::CipherClass;
147 croak("Usage: Catacomb::CipherClass::encrypt(cc, k, [iv], plain)")
149 my ($cc, $k, $iv, $p) = @_;
154 my $c = $cc->init($k);
155 $c->setiv($iv) if defined($iv);
156 return $c->encrypt($p);
160 croak("Usage: Catacomb::CipherClass::decrypt(cc, k, [iv], cipher)")
162 my ($cc, $k, $iv, $p) = @_;
167 my $c = $cc->init($k);
168 $c->setiv($iv) if defined($iv);
169 return $c->decrypt($p);
172 package Catacomb::HashClass;
176 croak("Usage: Catacomb::HashClass::hash(hc, p)") unless @_ == 2;
183 package Catacomb::MACClass;
187 croak("Usage: Catacomb::MACClass::mac(mc, k, p)") unless @_ == 3;
188 my ($mc, $k, $p) = @_;
189 my $m = $mc->key($k);
193 package Catacomb::MAC;
197 croak("Usage: Catacomb::MAC::hash(m, p)") unless @_ == 2;
204 #----- Random number generators ---------------------------------------------
208 foreach my $i (qw(True Fib LC DSA RC4 SEAL MGF Counter OFB Magic)) {
209 @{"Catacomb::Rand::${i}::ISA"} = qw(Catacomb::Rand);
212 $Catacomb::random = Catacomb::Rand::True->_global();
213 $Catacomb::random->noisesrc();
214 $Catacomb::random->seed(160);
216 #----- That's all, folks ----------------------------------------------------