diff options
Diffstat (limited to 'Master/tlpkg/tlperl/lib/Crypt/Random')
6 files changed, 355 insertions, 0 deletions
diff --git a/Master/tlpkg/tlperl/lib/Crypt/Random/Generator.pm b/Master/tlpkg/tlperl/lib/Crypt/Random/Generator.pm new file mode 100755 index 00000000000..8833b0e7679 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Crypt/Random/Generator.pm @@ -0,0 +1,101 @@ +#!/usr/bin/perl -sw +## +## +## +## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. +## This code is free software; you can redistribute it and/or modify +## it under the same terms as Perl itself. +## +## $Id: Generator.pm,v 1.2 2001/06/22 03:43:51 vipul Exp $ + +package Crypt::Random::Generator; +use Crypt::Random qw(makerandom makerandom_itv makerandom_octet); +use Carp; + +my @PROVIDERS = qw(devrandom devurandom egd rand); +my %STRENGTH = ( 0 => [ qw(devurandom egd rand) ], 1 => [ qw(devrandom egd rand) ] ); + +sub new { + + my ($class, %params) = @_; + + my $self = { _STRENGTH => \%STRENGTH, _PROVIDERS => \@PROVIDERS }; + + $$self{Strength} = $params{Strength} || 0; + $$self{Provider} = $params{Provider} || ""; + $$self{ProviderParams} = $params{ProviderParams} || ""; + + bless $self, $class; + + unless ($$self{Provider}) { + SELECT_PROVIDER: for ($self->strength_order($$self{Strength})) { + my $pname = $_; my $fqpname = "Crypt::Random::Provider::$pname"; + if (eval "use $fqpname; $fqpname->available()") { + if (grep { $pname eq $_ } $self->providers) { + $$self{Provider} = $pname; + last SELECT_PROVIDER; + } + } + } + } + + croak "No provider available.\n" unless $$self{Provider}; + return $self; + +} + + +sub providers { + + my ($self, @args) = @_; + if (@args) { $$self{_PROVIDERS} = [@args] } + return @{$$self{_PROVIDERS}}; + +} + + +sub strength_order { + + my ($self, $strength, @args) = @_; + if (@args) { $$self{_STRENGTH}{$strength} = [@args] } + return @{$$self{_STRENGTH}{$strength}} + +} + + +sub integer { + + my ($self, %params) = @_; + if ($params{Size}) { + return makerandom ( + Size => $params{Size}, + Provider => $$self{Provider}, + Verbosity => $params{Verbosity} || $$self{Verbosity}, + %{$$self{ProviderParams}}, + ) + } elsif ($params{Upper}) { + return makerandom_itv ( + Lower => $params{Lower} || 0, + Upper => $params{Upper}, + Provider => $$self{Provider}, + Verbosity => $params{Verbosity} || $$self{Verbosity}, + %{$$self{ProviderParams}}, + ) + } + +} + + +sub string { + + my ($self, %params) = @_; + return makerandom_octet ( + %params, + Provider => $$self{Provider}, + Verbosity => $params{Verbosity} || $$self{Verbosity}, + %{$$self{ProviderParams}}, + ) + +} + + diff --git a/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/File.pm b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/File.pm new file mode 100755 index 00000000000..816f8054d07 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/File.pm @@ -0,0 +1,62 @@ +package Crypt::Random::Provider::File; +use strict; +use Carp; +use Math::Pari qw(pari2num); +use Fcntl; + +sub _defaultsource { + return; +} + + +sub new { + + my ($class, %args) = @_; + my $self = { Source => $args{File} || $args{Device} || $args{Filename} || $class->_defaultsource() }; + return bless $self, $class; + +} + + +sub get_data { + + my ($self, %params) = @_; + $self = {} unless ref $self; + + my $size = $params{Size}; + my $skip = $params{Skip} || $$self{Skip} || ''; + my $q_skip = quotemeta($skip); + + if ($size && ref $size eq "Math::Pari") { + $size = pari2num($size); + } + + my $bytes = $params{Length} || (int($size / 8) + 1); + + sysopen RANDOM, $$self{Source}, O_RDONLY; + + my($r, $read, $rt) = ('', 0); + while ($read < $bytes) { + my $howmany = sysread RANDOM, $rt, $bytes - $read; + next unless $howmany; + if ($howmany == -1) { + croak "Error while reading from $$self{Source}. $!" + } + $rt =~ s/[$q_skip]//g if $skip; + $r .= $rt; + $read = length $r; + } + + $r; + +} + + +sub available { + my ($class) = @_; + return -e $class->_defaultsource(); +} + + +1; + diff --git a/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/devrandom.pm b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/devrandom.pm new file mode 100755 index 00000000000..a25f59ab14c --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/devrandom.pm @@ -0,0 +1,21 @@ +#!/usr/bin/perl -sw +## +## +## +## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. +## This code is free software; you can redistribute it and/or modify +## it under the same terms as Perl itself. +## +## $Id: devrandom.pm,v 1.2 2001/06/22 18:16:29 vipul Exp $ + +package Crypt::Random::Provider::devrandom; +use strict; +use lib qw(lib); +use Crypt::Random::Provider::File; +use vars qw(@ISA); +@ISA = qw(Crypt::Random::Provider::File); + +sub _defaultsource { return "/dev/random" } + +1; + diff --git a/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/devurandom.pm b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/devurandom.pm new file mode 100755 index 00000000000..d24d4b064ac --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/devurandom.pm @@ -0,0 +1,21 @@ +#!/usr/bin/perl -sw +## +## +## +## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. +## This code is free software; you can redistribute it and/or modify +## it under the same terms as Perl itself. +## +## $Id: devurandom.pm,v 1.2 2001/06/22 18:16:29 vipul Exp $ + +package Crypt::Random::Provider::devurandom; +use strict; +use lib qw(lib); +use Crypt::Random::Provider::File; +use vars qw(@ISA); +@ISA = qw(Crypt::Random::Provider::File); + +sub _defaultsource { return "/dev/urandom" } + +1; + diff --git a/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/egd.pm b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/egd.pm new file mode 100755 index 00000000000..869180c9a49 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/egd.pm @@ -0,0 +1,90 @@ +#!/usr/bin/perl -sw +## +## +## +## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. +## This code is free software; you can redistribute it and/or modify +## it under the same terms as Perl itself. +## +## $Id: egd.pm,v 1.5 2001/07/12 15:59:48 vipul Exp $ + +package Crypt::Random::Provider::egd; +use strict; + +use IO::Socket; +use Carp; +use Math::Pari qw(pari2num); + + +sub _defaultsource { + + my $source; + for my $d (qw( /var/run/egd-pool /dev/egd-pool /etc/entropy )) { + if (IO::Socket::UNIX->new(Peer => $d)) { $source = $d; last } + } + return $source; + +} + + +sub new { + + my ($class, %args) = @_; + my $self = { Source => $args{Source} || $args{Device} || $args{Filename} }; + $$self{Source} = $class->_defaultsource() unless $$self{Source}; + croak "egd entropy pool file not found.\n" unless $$self{Source}; + return bless $self, $class; + +} + + +sub get_data { + + my ( $self, %params ) = @_; + my $class = ref $self || $self; + $self = {} unless ref $self; + + my $bytes = $params{Length} || + (int( pari2num($params{ Size }) / 8) + 1); + my $dev = $params{Source} || $$self{Source}; + my $skip = $params{Skip}; + + croak "$dev doesn't exist. aborting." unless $dev && -e $dev; + + my $s = IO::Socket::UNIX->new(Peer => $dev); + croak "couldn't talk to egd. $!" unless $s; + + my($r, $read) = ('', 0); + while ($read < $bytes) { + my $msg = pack "CC", 0x01, 1; + $s->syswrite($msg, length $msg); + my $rt; + my $nread = $s->sysread($rt, 1); + croak "read from entropy socket failed" unless $nread == 1; + my $count = unpack("C", $rt); + $nread = $s->sysread($rt, $count); + croak "couldn't get all the requested entropy. aborting." + unless $nread == $count; + unless ($skip && $skip =~ /\Q$rt\E/) { + if ($params{Verbosity}) { print '.' unless $read % 2 } + $r .= $rt; + $read++; + } + } + + $r; +} + + +sub available { + + my $class = shift; + return 1 if $class->_defaultsource(); + return; + +} + + +1; + + diff --git a/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/rand.pm b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/rand.pm new file mode 100755 index 00000000000..8f34f193f57 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Crypt/Random/Provider/rand.pm @@ -0,0 +1,60 @@ +#!/usr/bin/perl -sw +## +## Crypt::Random::Provider::rand +## +## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. +## This code is free software; you can redistribute it and/or modify +## it under the same terms as Perl itself. +## +## $Id: rand.pm,v 1.2 2001/06/22 18:16:29 vipul Exp $ + +package Crypt::Random::Provider::rand; +use strict; +use Math::Pari qw(pari2num); + +sub new { + + my ($class, %params) = @_; + my $self = { Source => $params{Source} || sub { return rand($_[0]) } }; + return bless $self, $class; + +} + + +sub get_data { + + my ($self, %params) = @_; + $self = {} unless ref $self; + + my $size = $params{Size}; + my $skip = $params{Skip} || $$self{Skip}; + + if ($size && ref $size eq "Math::Pari") { + $size = pari2num($size); + } + + my $bytes = $params{Length} || (int($size / 8) + 1); + my $source = $$self{Source} || sub { rand($_[0]) }; + + my($r, $read, $rt) = ('', 0); + while ($read < $bytes) { + $rt = chr(int(&$source(256))); + unless ($skip && $skip =~ /\Q$rt\E/) { + $r .= $rt; $read++; + } + } + + $r; + +} + + +sub available { + + return 1; + +} + + +1; + |