1e1051a39Sopenharmony_ci=pod 2e1051a39Sopenharmony_ci 3e1051a39Sopenharmony_ci=head1 NAME 4e1051a39Sopenharmony_ci 5e1051a39Sopenharmony_ciRSA_public_encrypt, RSA_private_decrypt - RSA public key cryptography 6e1051a39Sopenharmony_ci 7e1051a39Sopenharmony_ci=head1 SYNOPSIS 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ci #include <openssl/rsa.h> 10e1051a39Sopenharmony_ci 11e1051a39Sopenharmony_ciThe following functions have been deprecated since OpenSSL 3.0, and can be 12e1051a39Sopenharmony_cihidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value, 13e1051a39Sopenharmony_cisee L<openssl_user_macros(7)>: 14e1051a39Sopenharmony_ci 15e1051a39Sopenharmony_ci int RSA_public_encrypt(int flen, const unsigned char *from, 16e1051a39Sopenharmony_ci unsigned char *to, RSA *rsa, int padding); 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ci int RSA_private_decrypt(int flen, const unsigned char *from, 19e1051a39Sopenharmony_ci unsigned char *to, RSA *rsa, int padding); 20e1051a39Sopenharmony_ci 21e1051a39Sopenharmony_ci=head1 DESCRIPTION 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ciBoth of the functions described on this page are deprecated. 24e1051a39Sopenharmony_ciApplications should instead use L<EVP_PKEY_encrypt_init_ex(3)>, 25e1051a39Sopenharmony_ciL<EVP_PKEY_encrypt(3)>, L<EVP_PKEY_decrypt_init_ex(3)> and 26e1051a39Sopenharmony_ciL<EVP_PKEY_decrypt(3)>. 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_ciRSA_public_encrypt() encrypts the B<flen> bytes at B<from> (usually a 29e1051a39Sopenharmony_cisession key) using the public key B<rsa> and stores the ciphertext in 30e1051a39Sopenharmony_ciB<to>. B<to> must point to RSA_size(B<rsa>) bytes of memory. 31e1051a39Sopenharmony_ci 32e1051a39Sopenharmony_ciB<padding> denotes one of the following modes: 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ci=over 4 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ci=item RSA_PKCS1_PADDING 37e1051a39Sopenharmony_ci 38e1051a39Sopenharmony_ciPKCS #1 v1.5 padding. This currently is the most widely used mode. 39e1051a39Sopenharmony_ciHowever, it is highly recommended to use RSA_PKCS1_OAEP_PADDING in 40e1051a39Sopenharmony_cinew applications. SEE WARNING BELOW. 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_ci=item RSA_PKCS1_OAEP_PADDING 43e1051a39Sopenharmony_ci 44e1051a39Sopenharmony_ciEME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty 45e1051a39Sopenharmony_ciencoding parameter. This mode is recommended for all new applications. 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_ci=item RSA_NO_PADDING 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_ciRaw RSA encryption. This mode should I<only> be used to implement 50e1051a39Sopenharmony_cicryptographically sound padding modes in the application code. 51e1051a39Sopenharmony_ciEncrypting user data directly with RSA is insecure. 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ci=back 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ciB<flen> must not be more than RSA_size(B<rsa>) - 11 for the PKCS #1 v1.5 56e1051a39Sopenharmony_cibased padding modes, not more than RSA_size(B<rsa>) - 42 for 57e1051a39Sopenharmony_ciRSA_PKCS1_OAEP_PADDING and exactly RSA_size(B<rsa>) for RSA_NO_PADDING. 58e1051a39Sopenharmony_ciWhen a padding mode other than RSA_NO_PADDING is in use, then 59e1051a39Sopenharmony_ciRSA_public_encrypt() will include some random bytes into the ciphertext 60e1051a39Sopenharmony_ciand therefore the ciphertext will be different each time, even if the 61e1051a39Sopenharmony_ciplaintext and the public key are exactly identical. 62e1051a39Sopenharmony_ciThe returned ciphertext in B<to> will always be zero padded to exactly 63e1051a39Sopenharmony_ciRSA_size(B<rsa>) bytes. 64e1051a39Sopenharmony_ciB<to> and B<from> may overlap. 65e1051a39Sopenharmony_ci 66e1051a39Sopenharmony_ciRSA_private_decrypt() decrypts the B<flen> bytes at B<from> using the 67e1051a39Sopenharmony_ciprivate key B<rsa> and stores the plaintext in B<to>. B<flen> should 68e1051a39Sopenharmony_cibe equal to RSA_size(B<rsa>) but may be smaller, when leading zero 69e1051a39Sopenharmony_cibytes are in the ciphertext. Those are not important and may be removed, 70e1051a39Sopenharmony_cibut RSA_public_encrypt() does not do that. B<to> must point 71e1051a39Sopenharmony_cito a memory section large enough to hold the maximal possible decrypted 72e1051a39Sopenharmony_cidata (which is equal to RSA_size(B<rsa>) for RSA_NO_PADDING, 73e1051a39Sopenharmony_ciRSA_size(B<rsa>) - 11 for the PKCS #1 v1.5 based padding modes and 74e1051a39Sopenharmony_ciRSA_size(B<rsa>) - 42 for RSA_PKCS1_OAEP_PADDING). 75e1051a39Sopenharmony_ciB<padding> is the padding mode that was used to encrypt the data. 76e1051a39Sopenharmony_ciB<to> and B<from> may overlap. 77e1051a39Sopenharmony_ci 78e1051a39Sopenharmony_ci=head1 RETURN VALUES 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ciRSA_public_encrypt() returns the size of the encrypted data (i.e., 81e1051a39Sopenharmony_ciRSA_size(B<rsa>)). RSA_private_decrypt() returns the size of the 82e1051a39Sopenharmony_cirecovered plaintext. A return value of 0 is not an error and 83e1051a39Sopenharmony_cimeans only that the plaintext was empty. 84e1051a39Sopenharmony_ci 85e1051a39Sopenharmony_ciOn error, -1 is returned; the error codes can be 86e1051a39Sopenharmony_ciobtained by L<ERR_get_error(3)>. 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ci=head1 WARNINGS 89e1051a39Sopenharmony_ci 90e1051a39Sopenharmony_ciDecryption failures in the RSA_PKCS1_PADDING mode leak information 91e1051a39Sopenharmony_ciwhich can potentially be used to mount a Bleichenbacher padding oracle 92e1051a39Sopenharmony_ciattack. This is an inherent weakness in the PKCS #1 v1.5 padding 93e1051a39Sopenharmony_cidesign. Prefer RSA_PKCS1_OAEP_PADDING. 94e1051a39Sopenharmony_ci 95e1051a39Sopenharmony_ci=head1 CONFORMING TO 96e1051a39Sopenharmony_ci 97e1051a39Sopenharmony_ciSSL, PKCS #1 v2.0 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_ci=head1 SEE ALSO 100e1051a39Sopenharmony_ci 101e1051a39Sopenharmony_ciL<ERR_get_error(3)>, L<RAND_bytes(3)>, 102e1051a39Sopenharmony_ciL<RSA_size(3)> 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_ci=head1 HISTORY 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_ciBoth of these functions were deprecated in OpenSSL 3.0. 107e1051a39Sopenharmony_ci 108e1051a39Sopenharmony_ci=head1 COPYRIGHT 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ciCopyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. 111e1051a39Sopenharmony_ci 112e1051a39Sopenharmony_ciLicensed under the Apache License 2.0 (the "License"). You may not use 113e1051a39Sopenharmony_cithis file except in compliance with the License. You can obtain a copy 114e1051a39Sopenharmony_ciin the file LICENSE in the source distribution or at 115e1051a39Sopenharmony_ciL<https://www.openssl.org/source/license.html>. 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_ci=cut 118