Crypt::OpenSSL::RSA#
RSA keys, encryption and signatures, computed by OpenSSL.
Load a key from PEM or DER (new_public_key, new_private_key), generate one (generate_key) or build one from its numbers (new_key_from_parameters), then encrypt and decrypt, sign and verify, or export it again as PEM. Signatures hash the message with SHA-256 unless another use_*_hash is chosen, and are PSS-padded unless use_pkcs1_padding is chosen. Encryption is OAEP-padded; PKCS#1 v1.5 encryption is refused, as it is open to the Marvin attack. A failure inside OpenSSL croaks with OpenSSL’s reason.
The RSA is the one in the system’s libcrypto 3, reached through the built-in Net::SSLeay. Key parameters come and go as Crypt::OpenSSL::Bignum objects.
This is a native implementation of Crypt::OpenSSL::RSA 0.41’s RSA.xs; its Crypt/OpenSSL/RSA.pm is used unchanged.
Synopsis#
use Crypt::OpenSSL::RSA;
my $key = Crypt::OpenSSL::RSA->generate_key(2048);
my $pub = Crypt::OpenSSL::RSA->new_public_key($key->get_public_key_x509_string);
my $sig = $key->sign("message"); # RSA-PSS over SHA-256
print $pub->verify("message", $sig) ? "ok\n" : "bad\n";
my $secret = $pub->encrypt("short secret"); # OAEP
print $key->decrypt($secret), "\n";
Differences from upstream#
One, where perl 5.44 with RSA.xs fails (t/81-xs-native/Crypt-OpenSSL-RSA/150-or-better.t): an operation whose result is empty - decrypting a ciphertext of the empty string - returns the empty string. perl5 measures the unwritten result buffer with strlen and returns whatever bytes it finds there.
Functions#
Keys#
generate_key#
Crypt::OpenSSL::RSA->generate_key($bits [, $exponent]): a new key pair of $bits bits (at least 512) with public exponent $exponent (odd, at least 3; 65537 by default).
size#
$key->size: the modulus size in bytes, the length of every ciphertext and signature.
check_key#
$key->check_key: 1 when the private key is consistent, else 0; croaks for a public key.
is_private#
$key->is_private: 1 when the key has its private half, else 0.
Export#
get_private_key_string#
$key->get_private_key_string([$passphrase [, $cipher]]): the private key as traditional PKCS#1 PEM, encrypted with $cipher (des3 by default) when a passphrase is given.
get_private_key_pkcs8_string#
$key->get_private_key_pkcs8_string([$passphrase [, $cipher]]): the private key as PKCS#8 PEM, encrypted as get_private_key_string does.
get_public_key_string#
$key->get_public_key_string (alias get_public_key_pkcs1_string): the public key as -----BEGIN RSA PUBLIC KEY----- PKCS#1 PEM.
get_public_key_x509_string#
$key->get_public_key_x509_string: the public key as -----BEGIN PUBLIC KEY----- X.509 PEM.
Encryption#
encrypt#
$key->encrypt($plaintext): the ciphertext, under OAEP padding or none.
decrypt#
$key->decrypt($ciphertext): the plaintext; private keys only.
private_encrypt#
$key->private_encrypt($data): the raw private-key operation under PKCS#1 v1.5 padding or none; private keys only.
public_decrypt#
$key->public_decrypt($data): the inverse of private_encrypt.
Signatures#
sign#
$key->sign($message): the signature of the message’s digest under the hash mode (SHA-256 by default), PSS-padded unless use_pkcs1_padding or use_no_padding was chosen; private keys only.
verify#
$key->verify($message, $signature): true when the signature is the key’s over the message under the same hash and padding modes, false when it is not.