Crypt-OpenSSL-RSA
view release on metacpan or search on metacpan
use strict;
use Test::More;
use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;
use Crypt::OpenSSL::Guess qw(openssl_version);
BEGIN {
plan tests => 67 + ( UNIVERSAL::can( "Crypt::OpenSSL::RSA", "use_sha512_hash" ) ? 4 * 5 : 0 ) + ( UNIVERSAL::can( "Crypt::OpenSSL::RSA", "use_whirlpool_hash" ) ? 1 * 5 : 0 );
}
sub _Test_Encrypt_And_Decrypt {
my ( $p_plaintext_length, $p_rsa, $p_check_private_encrypt ) = @_;
my ( $ciphertext, $decoded_text );
my $plaintext = pack(
"C${p_plaintext_length}",
(
1, 255, 0, 128, 4, # Make sure these characters work
map { int( rand 256 ) } ( 1 .. $p_plaintext_length - 5 )
)
);
ok( $ciphertext = $p_rsa->encrypt($plaintext) );
ok( $decoded_text = $p_rsa->decrypt($ciphertext) );
ok( $decoded_text eq $plaintext );
if ($p_check_private_encrypt) {
ok( $ciphertext = $p_rsa->private_encrypt($plaintext) );
ok( $decoded_text = $p_rsa->public_decrypt($ciphertext) );
ok( $decoded_text eq $plaintext );
}
}
sub _Test_Sign_And_Verify {
my ( $plaintext, $rsa, $rsa_pub, $hash ) = @_;
my $sig = eval { $rsa->sign($plaintext) };
SKIP: {
skip "OpenSSL error: illegal or unsupported padding mode - $hash", 5 if $@ =~ /illegal or unsupported padding mode/i;
skip "OpenSSL error: invalid digest - $hash", 5 if $@ =~ /invalid digest/i;
ok( $rsa_pub->verify( $plaintext, $sig ), "rsa_pub verify $hash");
my $false_sig = unpack "H*", $sig;
$false_sig =~ tr/[a-f]/[0a-d]/;
ok( !$rsa_pub->verify( $plaintext, pack( "H*", $false_sig ) ), "rsa_pub do not verify invalid $hash" );
ok( !$rsa->verify( $plaintext, pack( "H*", $false_sig ) ), "rsa do not verify invalid $hash" );
my $sig_of_other = $rsa->sign("different");
ok( !$rsa_pub->verify( $plaintext, $sig_of_other ), "rsa_pub do not verify unmatching message" );
ok( !$rsa->verify( $plaintext, $sig_of_other ), "rsa do not verify unmatching message");
}
}
sub _check_for_croak {
my ( $code, $expected ) = @_;
eval { &$code() };
ok( $@, "/$expected/" );
}
# 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("OpenSSL needs at least 32 bytes.");
Crypt::OpenSSL::RSA->import_random_seed();
ok( Crypt::OpenSSL::RSA->generate_key(512)->size() * 8 == 512 );
my $rsa = Crypt::OpenSSL::RSA->generate_key(2048);
ok( $rsa->size() * 8 == 2048 );
ok( $rsa->check_key() );
$rsa->use_no_padding();
_Test_Encrypt_And_Decrypt( $rsa->size(), $rsa, 1 );
$rsa->use_pkcs1_oaep_padding();
# private_encrypt does not work with pkcs1_oaep_padding
_Test_Encrypt_And_Decrypt( $rsa->size() - 42, $rsa, 0 );
#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 );
( run in 0.676 second using v1.01-cache-2.11-cpan-efa8479b9fe )