Crypt-DES_PP
view release on metacpan or search on metacpan
m4_dnl
m4_dnl Change the quoting character to prevent unintended quoting.
m4_changequote(`[m4[', `]m4]')m4_dnl Make emacs happy '
m4_dnl
# -*- perl -*-
# DES_PP.pm - Pure perl implementation of DES.
#
# The master file for the module is DES_PP.m4 which needs to be run through
# the m4. Please edit DES_PP.m4 if you need to modify!
package Crypt::DES_PP;
use strict;
use Carp;
use integer;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw (Exporter);
@EXPORT = qw ();
@EXPORT_OK = qw ();
$VERSION = '1.00';
use constant BLKSIZE => 8;
# Stolen from Crypt::DES.
sub usage {
my ($package, $filename, $line, $subr) = caller (1);
$Carp::CarpLevel = 2;
croak "Usage: $subr (@_)";
}
sub blocksize () { BLKSIZE };
sub keysize () { BLKSIZE };
sub expand_key ($);
sub crypt ($$$);
sub new {
usage ("new Crypt::DES_PP key")
unless @_ == 2;
my ($package, $key) = @_;
bless { ks => Crypt::DES_PP::expand_key ($key) }, $package;
}
sub encrypt {
usage ("encrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES_PP::crypt ($data, $self->{ks}, 1);
}
sub decrypt {
usage("decrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES_PP::crypt ($data, $self->{ks}, 0);
}
use constant ITERATIONS => 16;
# These used to be a single reference to an array of array references.
# Splitting them up into distinct constants slightly improves performance.
use constant des_SPtrans_0 =>
[ # Nibble 0
0x00820200, 0x00020000, 0x80800000, 0x80820200,
0x00800000, 0x80020200, 0x80020000, 0x80800000,
pack "VV", $l, $r;
}
1;
__END__
=head1 NAME
Crypt::DES_PP - Perl extension for DES encryption
=head1 SYNOPSIS
use Crypt::DES_PP;
$des = Crypt::DES_PP->new ($key);
$cipher = $des->encrypt ($plain);
$plain = $des->decrypt ($cipher);
$blocksize = $des->blocksize;
$keysize = $des->keysize;
=head1 DESCRIPTION
The Data Encryption Standard (DES), also known as Data Encryption
Algorithm (DEA) is a semi-strong encryption and decryption algorithm.
The module is 100 % compatible to Crypt::DES but is implemented
entirely in Perl. That means that you do not need a C compiler to
build and install this extension.
The module implements the Crypt::CBC interface. You are encouraged
to read the documentation for Crypt::CBC if you intend to use this
module for Cipher Block Chaining.
The minimum (and maximum) key size is 8 bytes. Shorter keys will
cause an exception, longer keys will get silently truncated. Data
is encrypted and decrypted in blocks of 8 bytes.
License (LGPL) version 2 or - at your choice - any later version,
see the file ``COPYING.LIB''.
The original C implementation of the Ultra-Fast-Crypt algorithm
was written by Michael Glad (glad@daimi.aau.dk) and has been donated to
the Free Software Foundation, Inc. It is covered by the GNU library
license version 2, see the file ``COPYING.LIB''.
=head1 SEE ALSO
Crypt::CBC(3), Crypt::DES(3), perl(1), m4(1).
=cut
Local Variables:
mode: perl
perl-indent-level: 4
perl-continued-statement-offset: 4
perl-continued-brace-offset: 0
perl-brace-offset: -4
perl-brace-imaginary-offset: 0
NAME
Crypt::DES_PP - Perl extension for DES encryption
SYNOPSIS
use Crypt::DES_PP;
$des = Crypt::DES_PP->new ($key);
$cipher = $des->encrypt ($plain);
$plain = $des->decrypt ($cipher);
$blocksize = $des->blocksize;
$keysize = $des->keysize;
DESCRIPTION
The Data Encryption Standard (DES), also known as Data
Encryption Algorithm (DEA) is a semi-strong encryption and
decryption algorithm.
The module is 100 % compatible to Crypt::DES but is implemented
entirely in Perl. That means that you do not need a C compiler
to build and install this extension.
The module implements the Crypt::CBC interface. You are
encouraged to read the documentation for Crypt::CBC if you
intend to use this module for Cipher Block Chaining.
The minimum (and maximum) key size is 8 bytes. Shorter keys will
cause an exception, longer keys will get silently truncated.
Data is encrypted and decrypted in blocks of 8 bytes.
(guido@imperia.net). It is available under the terms of the
Lesser GNU General Public License (LGPL) version 2 or - at your
choice - any later version, see the file ``COPYING.LIB''.
The original C implementation of the Ultra-Fast-Crypt algorithm
was written by Michael Glad (glad@daimi.aau.dk) and has been
donated to the Free Software Foundation, Inc. It is covered by
the GNU library license version 2, see the file ``COPYING.LIB''.
SEE ALSO
Crypt::CBC(3), Crypt::DES(3), perl(1), m4(1).
lib/Crypt/DES_PP.pm view on Meta::CPAN
# -*- perl -*-
# DES_PP.pm - Pure perl implementation of DES.
#
# The master file for the module is DES_PP.m4 which needs to be run through
# the m4. Please edit DES_PP.m4 if you need to modify!
package Crypt::DES_PP;
use strict;
use Carp;
use integer;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw (Exporter);
@EXPORT = qw ();
@EXPORT_OK = qw ();
$VERSION = '1.00';
use constant BLKSIZE => 8;
# Stolen from Crypt::DES.
sub usage {
my ($package, $filename, $line, $subr) = caller (1);
$Carp::CarpLevel = 2;
croak "Usage: $subr (@_)";
}
sub blocksize () { BLKSIZE };
sub keysize () { BLKSIZE };
sub expand_key ($);
sub crypt ($$$);
sub new {
usage ("new Crypt::DES_PP key")
unless @_ == 2;
my ($package, $key) = @_;
bless { ks => Crypt::DES_PP::expand_key ($key) }, $package;
}
sub encrypt {
usage ("encrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES_PP::crypt ($data, $self->{ks}, 1);
}
sub decrypt {
usage("decrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES_PP::crypt ($data, $self->{ks}, 0);
}
use constant ITERATIONS => 16;
# These used to be a single reference to an array of array references.
# Splitting them up into distinct constants slightly improves performance.
use constant des_SPtrans_0 =>
[ # Nibble 0
0x00820200, 0x00020000, 0x80800000, 0x80820200,
0x00800000, 0x80020200, 0x80020000, 0x80800000,
lib/Crypt/DES_PP.pm view on Meta::CPAN
pack "VV", $l, $r;
}
1;
__END__
=head1 NAME
Crypt::DES_PP - Perl extension for DES encryption
=head1 SYNOPSIS
use Crypt::DES_PP;
$des = Crypt::DES_PP->new ($key);
$cipher = $des->encrypt ($plain);
$plain = $des->decrypt ($cipher);
$blocksize = $des->blocksize;
$keysize = $des->keysize;
=head1 DESCRIPTION
The Data Encryption Standard (DES), also known as Data Encryption
Algorithm (DEA) is a semi-strong encryption and decryption algorithm.
The module is 100 % compatible to Crypt::DES but is implemented
entirely in Perl. That means that you do not need a C compiler to
build and install this extension.
The module implements the Crypt::CBC interface. You are encouraged
to read the documentation for Crypt::CBC if you intend to use this
module for Cipher Block Chaining.
The minimum (and maximum) key size is 8 bytes. Shorter keys will
cause an exception, longer keys will get silently truncated. Data
is encrypted and decrypted in blocks of 8 bytes.
lib/Crypt/DES_PP.pm view on Meta::CPAN
License (LGPL) version 2 or - at your choice - any later version,
see the file ``COPYING.LIB''.
The original C implementation of the Ultra-Fast-Crypt algorithm
was written by Michael Glad (glad@daimi.aau.dk) and has been donated to
the Free Software Foundation, Inc. It is covered by the GNU library
license version 2, see the file ``COPYING.LIB''.
=head1 SEE ALSO
Crypt::CBC(3), Crypt::DES(3), perl(1), m4(1).
=cut
Local Variables:
mode: perl
perl-indent-level: 4
perl-continued-statement-offset: 4
perl-continued-brace-offset: 0
perl-brace-offset: -4
perl-brace-imaginary-offset: 0
#! /usr/local/bin/perl -w
print "1..1\n";
$@ = '';
eval { require Crypt::DES_PP };
print $@ ? "not ok 1\n" : "ok 1\n";
#! /usr/local/bin/perl -w
#
# This is the DES validation suite as taken from the GNU libc 2.1.
use strict;
use Crypt::DES_PP;
my @tests = ();
while (<DATA>) {
chomp;
my @line = split;
push @tests, @line if @line == 3;
}
my $total = (@tests / 3) << 1;
my $count = 1;
print "1..$total\n";
while (@tests) {
my ($key, $plain, $expect) = map {
pack "H*", $_
} splice (@tests, 0, 3);
$@ = '';
eval {
my $cipher = Crypt::DES_PP->new ($key)
or die "constructor failed for test $count\n";
die if $cipher->encrypt ($plain) ne $expect;
};
print $@ ? "not ok $count\n" : "ok $count\n";
++$count;
$@ = '';
eval {
my $cipher = Crypt::DES_PP->new ($key);
die if $cipher->decrypt ($expect) ne $plain;
};
print $@ ? "not ok $count\n" : "ok $count\n";
++$count;
}
__END__
0101010101010101 95f8a5e5dd31d900 8000000000000000
0101010101010101 dd7f121ca5015619 4000000000000000
0101010101010101 2e8653104f3834ea 2000000000000000
#! /usr/local/bin/perl -w
use strict;
use Crypt::DES_PP;
use constant TESTS => 5000;
use constant UINT_MAX => 0xffffffff;
print "1..", TESTS, "\n";
my $count = 1;
foreach (1 .. TESTS) {
my $key = pack "NN", (rand UINT_MAX), (rand UINT_MAX);
my $plain = pack "NN", (rand UINT_MAX), (rand UINT_MAX);
$@ = '';
eval {
my $cipher = Crypt::DES_PP->new ($key);
my $ciphertext = $cipher->encrypt ($plain);
die if $cipher->decrypt ($ciphertext) ne $plain;
};
print $@ ? "not ok $count\n" : "ok $count\n";
++$count;
}
t/randomcache.t view on Meta::CPAN
#! /usr/local/bin/perl -w
use strict;
use Crypt::DES_PP;
use constant TESTS => 5000;
use constant UINT_MAX => 0xffffffff;
my $key = pack "NN", (rand UINT_MAX), (rand UINT_MAX);
my $cipher = Crypt::DES_PP->new ($key);
unless ($cipher) {
print "1..1\nnot ok 1\n";
exit 1;
}
print "1..", TESTS, "\n";
my $count = 1;
foreach (1 .. TESTS) {
my $plain = pack "NN", (rand UINT_MAX), (rand UINT_MAX);
#
# Extended testing is Copyright (C) 2000 W3Works, LLC
# All rights reserved.
#
#
# Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
# All rights reserved.
#
# Modified by Guido Flohr to use Crypt::DES_PP instead of Crypt::DES.
# The mere purpose of this file is to allow a performance comparison
# between the pure Perl and the XS version.
my $libdir;
BEGIN {
my $here = $0;
$here =~ m@^(.*)[\\/].*$@;
$libdir = $1 || '.';
$libdir .= '/blib/lib';
}
use lib $libdir;
use Crypt::DES_PP;
use Data::Dumper;
use Benchmark;
#my $key = pack("H*", "7ca110454a1a6e57");
#my $key = pack("H*", "0101010101010101");
#my $in = pack("H*", "95f8a5e5dd31d900");
#my $out = pack("H*", "8000000000000000");
#my $tmp = pack("H*", "1234567890ABCDEF");
#my $cipher = new Crypt::DES $key;
#
# Adding the above tests into this program is
# left as an exercise for the reader
#
#
# Some tests...
#
my $i = 1;
my $fail = 0;
my $tt = (scalar(@{$testval}) *2);
print "1..$tt\n";
my $t0 = new Benchmark;
foreach my $tst (@{$testval}) {
my ($anot,$bnot) = (0,0);
foreach(@{$tst}) { $_ = pack("H*",$_) }
my $cipher = new Crypt::DES_PP($tst->[0]);
$anot = 1 unless ($cipher->encrypt($tst->[1]) eq $tst->[2]);
if($anot) {
#print "not ";
$fail++;
}
#print "ok $i .. ";
#print "saw [encrypt \$in]: ". unpack("H*",$cipher->encrypt($tst->[1])) ." expected [\$out]: ";
#print unpack("H*",$tst->[2]) ."\n";
$i++;
my $ts0 = timestr($td0);
print "$tt basic tests ran in $ts0\n";
print "$suc of $tt tests passed ($fp\%)\n";
if($fail > 0) {
print "Not all tests successful. Please attempt to rebuild the package\n";
} else {
print "\nRunning speed tests...\n";
print "\nnon-cached cipher speed test. 5000 encrypt iterations\n";
my $t2 = new Benchmark;
for(1..5000) {
my $cipher = new Crypt::DES_PP(pack("H*",'1c587f1c13924fef'));
$cipher->encrypt(pack("H*",'305532286d6f295a'));
}
my $t3 = new Benchmark;
my $td1 = timediff($t3,$t2);
my $ts1 = timestr($td1);
print "$ts1\nok 343\n";
print "\nnon-cached cipher speed test. 5000 decrypt iterations\n";
my $t4 = new Benchmark;
for(1..5000) {
my $cipher = new Crypt::DES_PP(pack("H*",'1c587f1c13924fef'));
$cipher->decrypt(pack("H*",'63fac0d034d9f793'));
}
my $t5 = new Benchmark;
my $td2 = timediff($t5,$t4);
my $ts2 = timestr($td2);
print "$ts2\nok 344\n";
print "\ncached cipher speed test. 10000 encrypt iterations\n";
{
my $t6 = new Benchmark;
my $cipher = new Crypt::DES_PP(pack("H*",'1c587f1c13924fef'));
for(1..10000) {
$cipher->encrypt(pack("H*",'305532286d6f295a'));
}
my $t7 = new Benchmark;
my $td3 = timediff($t7,$t6);
my $ts3 = timestr($td3);
print "$ts3\nok 345\n";
}
print "\ncached cipher speed test. 10000 decrypt iterations\n";
{
my $t8 = new Benchmark;
my $cipher = new Crypt::DES_PP(pack("H*",'1c587f1c13924fef'));
for(1..10000) {
$cipher->decrypt(pack("H*",'63fac0d034d9f793'));
}
my $t9 = new Benchmark;
my $td4 = timediff($t9,$t8);
my $ts4 = timestr($td4);
print "$ts4\nok 346\n";
}
print "\ncached cipher speed test. 10000 decrypt iterations\n";
{
my $t8 = new Benchmark;
my $key = pack ("H*", '1c587f1c13924fef');
my $ciphertext = pack("H*",'63fac0d034d9f793');
my $cipher = new Crypt::DES_PP($key);
for(1..10000) {
$cipher->decrypt($ciphertext);
}
my $t9 = new Benchmark;
my $td4 = timediff($t9,$t8);
my $ts4 = timestr($td4);
print "$ts4\nok 346\n";
}
}
#! /usr/local/bin/perl -w
#
# DES_PP benchmarking.
# Copyright 2000, Guido Flohr <guido@imperia.net>
use strict;
use IO::File;
use POSIX;
use Crypt::DES_PP;
use Benchmark;
use constant SECONDS_PER_TEST => 10;
use constant KEY => 'PurePerl';
use constant PLAINTEXT => 'PerlPunk';
use constant CIPHERTEXT => 'PunkPerl';
sub alarm_handler ($);
my $clocks;
my $starttime;
my $count = 0;
my $des;
print "Initializing 8-byte keys for ", SECONDS_PER_TEST, " seconds... ";
eval {
(undef, $starttime) = POSIX::times;
alarm SECONDS_PER_TEST;
while (1) { $des = Crypt::DES_PP->new (KEY); ++$count };
};
die if $@ and $@ ne "alarm\n";
my $keys_per_sec = ($clocks * $count) / $elapsed;
print "$keys_per_sec keys per second\n";
# Benchmark encryption.
print "Encrypting 8-byte blocks for ", SECONDS_PER_TEST, " seconds... ";
$des = Crypt::DES_PP->new (KEY);
$count = 0;
eval {
(undef, $starttime) = POSIX::times;
alarm SECONDS_PER_TEST;
while (1) { $des->encrypt (PLAINTEXT); ++$count };
};
die if $@ and $@ ne "alarm\n";
my $encrypts_per_sec = ($clocks * $count) / $elapsed;
print "$encrypts_per_sec encryptions per second\n";
# Benchmark encryption.
print "Decrypting 8-byte blocks for ", SECONDS_PER_TEST, " seconds... ";
$des = Crypt::DES_PP->new (KEY);
$count = 0;
eval {
(undef, $starttime) = POSIX::times;
alarm SECONDS_PER_TEST;
while (1) { $des->decrypt (CIPHERTEXT); ++$count };
};
die if $@ and $@ ne "alarm\n";
" bytes bytes in CBC mode...";
eval '
use Crypt::CBC;
my $cipher = Crypt::CBC->new (KEY, $des_driver);
my $start = Benchmark->new;
$cipher->encrypt ($plaintext);
my $end = Benchmark->new;
$timediff = timestr timediff $end, $start;
';
print $@ ? " skipped (Crypt::CBC or Crypt::DES not loadable)\n" :
" done\n$timediff\n";
$timediff = 0;
# Now with a non-cached key and 128 bytes of plaintext.
$plaintext = PLAINTEXT x 16;
print "Encrypting ", EIGHT_BYTE_BLOCKS,
" 128-byte-blocks in non-cached CBC mode...";
eval '
use Crypt::CBC;
( run in 0.303 second using v1.01-cache-2.11-cpan-9a3d99fc6dc )