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
(\
&and2c
, @_); },
62 '|' => sub { _binop
(\
&or2c
, @_); },
63 '^' => sub { _binop
(\
&xor2c
, @_); },
64 '**' => sub { _binop
(\
&pow
, @_); },
65 '>>' => sub { &lsr2c
(@_[0, 1]); },
66 '<<' => sub { &lsl2c
(@_[0, 1]); },
67 '~' => sub { ¬2c
($_[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 #----- Binary polynomials ---------------------------------------------------
117 package Catacomb
::GF
;
120 @ISA = qw(Catacomb
::MP
);
123 '+' => sub { _binop
(\
&add
, @_); },
124 '-' => sub { _binop
(\
&add
, @_); },
125 '*' => sub { _binop
(\
&mul
, @_); },
126 '/' => sub { _binop
(\
&div
, @_); },
127 '%' => sub { _binop
(\
&mod
, @_); },
128 '&' => sub { _binop
(\
&Catacomb
::MP
::and, @_); },
129 '|' => sub { _binop
(\
&Catacomb
::MP
::or, @_); },
130 '^' => sub { _binop
(\
&Catacomb
::MP
::xor, @_); },
131 '>>' => sub { &Catacomb
::MP
::lsr
(@_[0, 1]); },
132 '<<' => sub { &Catacomb
::MP
::lsl
(@_[0, 1]); },
133 '~' => sub { &Catacomb
::MP
::not($_[0]) },
134 '==' => sub { _binop
(\
&Catacomb
::MP
::eq, @_); },
135 '<=>' => sub { _binop
(\
&Catacomb
::MP
::cmp, @_); },
136 '""' => sub { "0x" . &Catacomb
::MP
::tostring
($_[0], 16); },
137 'neg' => sub { $_[0]; },
138 '0+' => sub { &Catacomb
::MP
::toint
($_[0]); };
140 sub mod
{ (&div
($_[0], $_[1]))[1]; }
143 my ($func, $a, $b, $flag) = @_;
144 return $flag ?
&$func($b, $a) : &$func($a, $b);
147 #----- Prime testing --------------------------------------------------------
150 my $cmpg = "Catacomb::MP::Prime::Gen";
151 foreach my $i (qw(FilterStepper JumpStepper RabinTester
)) {
152 @
{"${cmpg}::${i}::ISA"} = ("${cmpg}::MagicProc");
154 @
{"${cmpg}::MagicProc::ISA"} = ("${cmpg}::Proc");
157 #----- Crypto algorithms ----------------------------------------------------
161 foreach my $i (qw(Cipher Hash MAC
)) {
164 my $cl = "Catacomb::${i}Class";
165 foreach my $c (&{"${cl}::list"}($cl)) {
166 my $x = $c->name(); $x =~ tr/a-zA-Z0-9/_/cs;
167 ${"Catacomb::${i}::${x}"} = undef; # SUYB
168 ${"Catacomb::${i}::${x}"} = $c;
169 push(@v, "\$Catacomb::${i}::${x}");
171 $EXPORT_TAGS{$tag} = \
@v;
172 Exporter
::export_ok_tags
($tag);
175 package Catacomb
::CipherClass
;
179 croak
("Usage: Catacomb::CipherClass::encrypt(cc, k, [iv], plain)")
181 my ($cc, $k, $iv, $p) = @_;
186 my $c = $cc->init($k);
187 $c->setiv($iv) if defined($iv);
188 return $c->encrypt($p);
192 croak
("Usage: Catacomb::CipherClass::decrypt(cc, k, [iv], cipher)")
194 my ($cc, $k, $iv, $p) = @_;
199 my $c = $cc->init($k);
200 $c->setiv($iv) if defined($iv);
201 return $c->decrypt($p);
204 package Catacomb
::HashClass
;
208 croak
("Usage: Catacomb::HashClass::hash(hc, p)") unless @_ == 2;
215 package Catacomb
::MACClass
;
219 croak
("Usage: Catacomb::MACClass::mac(mc, k, p)") unless @_ == 3;
220 my ($mc, $k, $p) = @_;
221 my $m = $mc->key($k);
225 package Catacomb
::MAC
;
229 croak
("Usage: Catacomb::MAC::hash(m, p)") unless @_ == 2;
236 #----- Random number generators ---------------------------------------------
240 foreach my $i (qw(True Fib LC DSA RC4 SEAL MGF Counter OFB Magic
)) {
241 @
{"Catacomb::Rand::${i}::ISA"} = qw(Catacomb
::Rand
);
244 $Catacomb::random
= Catacomb
::Rand
::True
->_global();
245 $Catacomb::random
->noisesrc();
246 $Catacomb::random
->seed(160);
248 #----- That's all, folks ----------------------------------------------------