NAME

Crypt::OpenSSL::RSA - RSA encoding and decoding, using the openSSL libraries

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 = Crypt::OpenSSL::RSA->new();
$rsa_pub->import_random_seed();
# or just Crypt::OpenSSL::RSA::import_random_seed

$rsa_pub->load_public_key($key_string);
$ciphertext = $rsa->encrypt($plaintext);

$rsa_priv = Crypt::OpenSSL::RSA->new();
$rsa_priv->load_private_key($key_string);
$plaintext = $rsa->encrypt($ciphertext);

$rsa = Crypt::OpenSSL::RSA->new();
$rsa->generate_key(1024); # or
$rsa->generate_key(1024, $prime);

print "private key is:\n", $rsa->get_private_key_string();
print "public key is:\n", $rsa->get_public_key_string();

$signature = $rsa_priv->sign($plaintext);
print "Signed correctly\n" if ( $rsa->verify($plaintext, $signature) );

DESCRIPTION

Crypt::OpenSSL::RSA provides the ability to RSA encrypt strings which are somewhat shorter than the block size of a key. It also allows for decryption, signatures and signature verification.

NOTE: Many of the methods in this package can croak, so use eval, or Error.pm's try/catch mechanism to capture errors. Also, while some methods from earlier versions of this package return true on success, this (never documented) behavior is no longer the case.

Instance Methods

new

The standard constructor for an RSA object takes no arguments; the key should either be created by generate_key, or loaded in by load_public_key or load_private_key.

DESTROY

Clean up after ourselves. In particular, erase and free the memory occupied by the RSA key structure.

load_public_key

Load a public key in from an X509 encoded string. The string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with set_padding.

load_private_key

Load a private key in from an X509 encoded string. The string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with use_xxx_padding.

get_public_key_string

Return the public portion of the key as an X509 encoded string.

get_private_key_string

Return the X509 encoding of the private key.

generate_key

Generate a private/public key pair. The padding is set to PKCS1_OAEP, but can be changed with set_padding.

encrypt

Encrypt a string using the public (portion of the) key

sign

Sign a string using the secret (portion of the) key

verify

Check the signature on a text.

decrypt

Decrypt a binary "string". Croaks if the key is public only.

set_padding_mode

DEPRECATED. Use the use_xxx_padding methods instead

use_no_padding

Use raw RSA encryption. This mode should only be used to implement cryptographically sound padding modes in the application code. Encrypting user data directly with RSA is insecure.

use_pkcs1_padding

Use PKCS #1 v1.5 padding. This currently is the most widely used mode of padding.

use_pkcs1_oaep_padding

Use EME-OAEP padding as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode of padding is recommended for all new applications. It is the default mode used by Crypt::OpenSSL::RSA.

use_sslv23_padding

Use PKCS #1 v1.5 padding with an SSL-specific modification that denotes that the server is SSL3 capable.

get_padding_mode

DEPRECATED.

use_md5_hash

Use the RFC 1321 MD5 hashing algorithm by Ron Rivest when signing and verifying messages.

use_sha1_hash

Use the RFC 3174 Secure Hashing Algorithm (FIPS 180-1) when signing and verifying messages. This is the default.

use_ripemd160_hash

Dobbertin, Bosselaers and Preneel's RIPEMD hashing algorithm when signing and verifying messages.

size

Returns the size, in bytes, of the key. All encrypted text will be of this size, and depending on the padding mode used, the length of the text to be encrypted should be

$RSA_PKCS1_OAEP_PADDING

at most 42 bytes less than this size.

$RSA_PKCS1_PADDING or $RSA_SSLV23_PADDING

at most 11 bytes less than this size.

$RSA_NO_PADDING

exactly this size.

check_key

This function validates the RSA key, returning 1 if the key is valid, 0 otherwise.

Class Methods

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.

BUGS

There is a small memory leak when generating new keys of more than 512 bits.

AUTHOR

Ian Robertson, iroberts@cpan.org

SEE ALSO

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)