Skip to content
Browse files

import Crypt-OpenSSL-RSA 0.12 from CPAN

git-cpan-module:   Crypt-OpenSSL-RSA
git-cpan-version:  0.12
git-cpan-authorid: IROBERTS
git-cpan-file:     authors/id/I/IR/IROBERTS/Crypt-OpenSSL-RSA-0.12.tar.gz
  • Loading branch information...
1 parent 409abb5 commit be9fcb15b2650a3d22ab5740368c4be93e09a35e Ian Robertson committed with schwern Sep 6, 2001
Showing with 73 additions and 13 deletions.
  1. +6 −0 Changes
  2. +28 −5 RSA.pm
  3. +25 −1 RSA.xs
  4. +14 −7 test.pl
View
6 Changes
@@ -1,5 +1,11 @@
Revision history for Perl extension Crypt::OpenSSL::RSA.
+0.12 Thu Sep 06 22:44:17
+ - Fixing bug with Crypt::OpenSSL::Random interoperability
+ - Implementing patch from Thomas Linden <scip@daemon.de>
+ fixing a keysize bug
+ - Fixing email address in docs.
+
0.11 Tue Apr 10 22:45:31
- Fixing bug in test.pl.
View
33 RSA.pm
@@ -19,7 +19,7 @@ require AutoLoader;
@EXPORT = qw( $RSA_PKCS1_PADDING $RSA_SSLV23_PADDING $RSA_NO_PADDING
$RSA_PKCS1_OAEP_PADDING );
-$VERSION = '0.11';
+$VERSION = '0.12';
bootstrap Crypt::OpenSSL::RSA $VERSION;
@@ -37,9 +37,14 @@ Crypt::OpenSSL::RSA - RSA encoding and decoding, using the openSSL libraries
=head1 SYNOPSIS
+ use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;
+ # not necessary if we have /dev/random:
+ Crypt::OpenSSL::Random::random_seed($good_entropy);
+
$rsa_pub = new Crypt::OpenSSL::RSA();
+ $rsa_pub->import_random_seed();
$rsa_pub->load_public_key($key_string);
$ciphertext = $rsa->encrypt($plaintext);
@@ -248,10 +253,26 @@ exactly this size.
This function validates the RSA key, returning 1 if the key is valid,
0 otherwise.
-=cut
+=back
+
+=item import_random_seed
+
+Import a random seed from Crypt::OpenSSL::Random, since the OpenSSL
+libraries won't allow sharing of random structures across perl XS
+modules.
=back
+=cut
+
+sub import_random_seed
+{
+ until ( _random_status() )
+ {
+ _random_seed( Crypt::OpenSSL::Random::random_bytes(20) );
+ }
+}
+
=head1 BUGS
Currently many XS routines croak rather than trying to intelligently
@@ -268,10 +289,12 @@ RSA_NO_PADDING_MODE does not work - I don't know yet if it's a problem with encr
=head1 AUTHOR
-Ian Robertson, ian@cpan.com
+Ian Robertson, iroberts@cpan.com
=head1 SEE ALSO
-perl(1), rsa(3), RSA_new(3), RSA_public_encrypt(3),
-RSA_size(3), RSA_generate_key(3), RSA_check_key(3)
+perl(1), Crypt::OpenSSL::Random(3), rsa(3), RSA_new(3),
+RSA_public_encrypt(3), RSA_size(3), RSA_generate_key(3),
+RSA_check_key(3)
+
=cut
View
26 RSA.xs
@@ -246,7 +246,7 @@ PPCODE:
else
exponent = 65535;
- rsa = RSA_generate_key(1024, 65535, NULL, NULL);
+ rsa = RSA_generate_key(SvIV(bitsSV), 65535, NULL, NULL);
if(rsa == NULL)
XSRETURN_NO;
@@ -396,3 +396,27 @@ PPCODE:
XPUSHs( sv_2mortal( newSViv( RSA_check_key(rsa) )));
XSRETURN(1);
}
+
+ # Seed the PRNG with user-provided bytes; returns true if the
+ # seeding was sufficient.
+
+void
+_random_seed(random_bytes_SV)
+ SV * random_bytes_SV;
+PPCODE:
+{
+ int random_bytes_length;
+ char *random_bytes;
+ random_bytes = SvPV(random_bytes_SV, random_bytes_length);
+ RAND_seed(random_bytes, random_bytes_length);
+ XPUSHs( sv_2mortal( newSViv( RAND_status() ) ) );
+}
+
+ # Returns true if the PRNG has enough seed data
+
+void
+_random_status()
+PPCODE:
+{
+ XPUSHs( sv_2mortal( newSViv( RAND_status() ) ) );
+}
View
21 test.pl
@@ -8,7 +8,7 @@
use strict;
my $loaded;
-BEGIN { $| = 1; print "1..12\n"; }
+BEGIN { $| = 1; print "1..13\n"; }
END {print "not ok 1\n" unless $loaded;}
use Crypt::OpenSSL::RSA;
$loaded = 1;
@@ -38,18 +38,25 @@ sub my_test
}
}
-# On platforms without a /dev/random, we need to manually seed.
-# In real life, the following would stink, but for testing
-# purposes, it suffices to seed with any old thing, even if it is
-# not actually random
+# On platforms without a /dev/random, we need to manually seed. In
+# real life, the following would stink, but for testing purposes, it
+# suffices to seed with any old thing, even if it is not actually
+# random. We'll at least emulate seeding from Crypt::OpenSSL::Random,
+# which is what we would have to do in "real life", since the private
+# data used by the OpenSSL random library apparently does not span
+# across perl XS modules.
-Crypt::OpenSSL::Random::random_seed("Here are 19 bytes...");
+Crypt::OpenSSL::Random::random_seed("Here are 20 bytes...");
+Crypt::OpenSSL::RSA::import_random_seed();
# We should now be seeded, regardless.
-my_test(Crypt::OpenSSL::Random::random_status());
+# my_test(Crypt::OpenSSL::RSA::random_status());
my $rsa = Crypt::OpenSSL::RSA->new();
+my_test($rsa->generate_key(512));
+my_test($rsa->size() * 8 == 512);
+
my_test($rsa->generate_key(1024));
my_test($rsa->size() * 8 == 1024);
my_test($rsa->check_key());

0 comments on commit be9fcb1

Please sign in to comment.
Something went wrong with that request. Please try again.