From: <iro...@us...> - 2004-02-18 20:22:33
|
Update of /cvsroot/perl-openssl/Crypt/OpenSSL/RSA/t In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24866 Modified Files: rsa.t Log Message: Test use_pkcs1_padding. Don't test use_sslv23_padding, since it seems to fail -- openssl bug? Index: rsa.t =================================================================== RCS file: /cvsroot/perl-openssl/Crypt/OpenSSL/RSA/t/rsa.t,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** rsa.t 18 Feb 2004 16:39:20 -0000 1.3 --- rsa.t 18 Feb 2004 20:12:40 -0000 1.4 *************** *** 5,23 **** use Crypt::OpenSSL::RSA; ! BEGIN { plan tests => 22 } sub _Test_Sign_And_Verify { ! my( $plaintext, $rsa, $rsa_pub ) = @_; my $sig = $rsa->sign($plaintext); ! ok( $rsa_pub->verify( $plaintext, $sig ) ); my $false_sig = unpack "H*", $sig; $false_sig =~ tr/[a-f]/[0a-d]/; ! ok(! $rsa_pub->verify( $plaintext, pack( "H*", $false_sig ) ) ); ! ok(! $rsa->verify( $plaintext, pack( "H*", $false_sig ) ) ); } # On platforms without a /dev/random, we need to manually seed. In # real life, the following would stink, but for testing purposes, it --- 5,38 ---- use Crypt::OpenSSL::RSA; ! BEGIN { plan tests => 25 } ! ! sub _Test_Encrypt_And_Decrypt ! { ! my ($plaintext_length, $rsa) = @_; ! ! my ($ciphertext, $decoded_text); ! my $plaintext = pack("C$plaintext_length", ! (1,255,0,128,4, # Make sure these characters work ! map {int(rand 256)} (1..$plaintext_length-5))); ! ok($ciphertext = $rsa->encrypt($plaintext)); ! ok($decoded_text = $rsa->decrypt($ciphertext)); ! ok($decoded_text eq $plaintext); ! } sub _Test_Sign_And_Verify { ! my ($plaintext, $rsa, $rsa_pub) = @_; my $sig = $rsa->sign($plaintext); ! ok($rsa_pub->verify($plaintext, $sig)); my $false_sig = unpack "H*", $sig; $false_sig =~ tr/[a-f]/[0a-d]/; ! ok(! $rsa_pub->verify($plaintext, pack("H*", $false_sig))); ! ok(! $rsa->verify($plaintext, pack("H*", $false_sig))); } + + # On platforms without a /dev/random, we need to manually seed. In # real life, the following would stink, but for testing purposes, it *************** *** 31,73 **** Crypt::OpenSSL::RSA->import_random_seed(); ! ok( Crypt::OpenSSL::RSA->generate_key( 512 )->size() * 8 == 512 ); ! my $rsa = Crypt::OpenSSL::RSA->generate_key( 1024 ); ! ok( $rsa->size() * 8 == 1024 ); ! ok( $rsa->check_key() ); # check if NO_PADDING works $rsa->use_no_padding(); ! my ($ciphertext, $decoded_text); ! my $plaintext = "X" x $rsa->size; ! ok($ciphertext = $rsa->encrypt($plaintext)); ! ok($decoded_text = $rsa->decrypt($ciphertext)); ! ok ($decoded_text eq $plaintext); - # check if OAEP_PADDING works - my $plaintext_length = $rsa->size() - 42; - $plaintext = pack("C$plaintext_length", - (255,0,128,4, # Make sure these characters work - map {int(rand 256)} (1..$plaintext_length-4))); $rsa->use_pkcs1_oaep_padding(); ! ok($ciphertext = $rsa->encrypt($plaintext)); ! ok($decoded_text = $rsa->decrypt($ciphertext)); ! ok ($decoded_text eq $plaintext); my $private_key_string = $rsa->get_private_key_string(); my $public_key_string = $rsa->get_public_key_string(); ! ok( $private_key_string and $public_key_string ); my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key_string); ! $decoded_text = $rsa_priv->decrypt($ciphertext); ! ! ok( $decoded_text eq $plaintext ); ! ! my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key( $public_key_string ); ! $ciphertext = $rsa_pub->encrypt($plaintext); ! $decoded_text = $rsa->decrypt($ciphertext); ! ok ($decoded_text eq $plaintext); $plaintext .= $plaintext x 5; --- 46,80 ---- Crypt::OpenSSL::RSA->import_random_seed(); ! ok(Crypt::OpenSSL::RSA->generate_key(512)->size() * 8 == 512); ! my $rsa = Crypt::OpenSSL::RSA->generate_key(1024); ! ok($rsa->size() * 8 == 1024); ! ok($rsa->check_key()); # check if NO_PADDING works + $rsa->use_no_padding(); ! _Test_Encrypt_And_Decrypt($rsa->size(), $rsa); $rsa->use_pkcs1_oaep_padding(); ! _Test_Encrypt_And_Decrypt($rsa->size() - 42, $rsa); ! ! $rsa->use_pkcs1_padding(); ! _Test_Encrypt_And_Decrypt($rsa->size() - 11, $rsa); ! ! #FIXME - use_sslv23_padding seems to fail on decryption. openssl bug? my $private_key_string = $rsa->get_private_key_string(); my $public_key_string = $rsa->get_public_key_string(); ! ok($private_key_string and $public_key_string); + my $plaintext = "The quick brown fox jumped over the lazy dog"; my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key_string); ! ok($plaintext eq $rsa_priv->decrypt($rsa_priv->encrypt($plaintext))); ! my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($public_key_string); ! $rsa->use_pkcs1_oaep_padding(); ! ok($plaintext eq $rsa->decrypt($rsa_pub->encrypt($plaintext))); $plaintext .= $plaintext x 5; *************** *** 75,94 **** $rsa->use_md5_hash(); $rsa_pub->use_md5_hash(); ! _Test_Sign_And_Verify( $plaintext, $rsa, $rsa_pub ); $rsa->use_sha1_hash(); $rsa_pub->use_sha1_hash(); ! _Test_Sign_And_Verify( $plaintext, $rsa, $rsa_pub ); $rsa->use_ripemd160_hash(); $rsa_pub->use_ripemd160_hash(); ! _Test_Sign_And_Verify( $plaintext, $rsa, $rsa_pub ); # check subclassing ! eval { Crypt::OpenSSL::RSA::Subpackage->generate_key( 256 ); }; ! ok( !$@ ); package Crypt::OpenSSL::RSA::Subpackage; ! use base qw( Crypt::OpenSSL::RSA ); --- 82,101 ---- $rsa->use_md5_hash(); $rsa_pub->use_md5_hash(); ! _Test_Sign_And_Verify($plaintext, $rsa, $rsa_pub); $rsa->use_sha1_hash(); $rsa_pub->use_sha1_hash(); ! _Test_Sign_And_Verify($plaintext, $rsa, $rsa_pub); $rsa->use_ripemd160_hash(); $rsa_pub->use_ripemd160_hash(); ! _Test_Sign_And_Verify($plaintext, $rsa, $rsa_pub); # check subclassing ! eval { Crypt::OpenSSL::RSA::Subpackage->generate_key(256); }; ! ok(!$@); package Crypt::OpenSSL::RSA::Subpackage; ! use base qw(Crypt::OpenSSL::RSA); |