1.\" $OpenBSD: RSA_public_encrypt.3,v 1.13 2023/09/10 16:04:15 schwarze Exp $ 2.\" OpenSSL RSA_public_encrypt.pod 1e3f62a3 Jul 17 16:47:13 2017 +0200 3.\" 4.\" This file is a derived work. 5.\" The changes are covered by the following Copyright and license: 6.\" 7.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org> 8.\" 9.\" Permission to use, copy, modify, and distribute this software for any 10.\" purpose with or without fee is hereby granted, provided that the above 11.\" copyright notice and this permission notice appear in all copies. 12.\" 13.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20.\" 21.\" The original file was written by Ulf Moeller <ulf@openssl.org>. 22.\" Copyright (c) 2000, 2004 The OpenSSL Project. All rights reserved. 23.\" 24.\" Redistribution and use in source and binary forms, with or without 25.\" modification, are permitted provided that the following conditions 26.\" are met: 27.\" 28.\" 1. Redistributions of source code must retain the above copyright 29.\" notice, this list of conditions and the following disclaimer. 30.\" 31.\" 2. Redistributions in binary form must reproduce the above copyright 32.\" notice, this list of conditions and the following disclaimer in 33.\" the documentation and/or other materials provided with the 34.\" distribution. 35.\" 36.\" 3. All advertising materials mentioning features or use of this 37.\" software must display the following acknowledgment: 38.\" "This product includes software developed by the OpenSSL Project 39.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 40.\" 41.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 42.\" endorse or promote products derived from this software without 43.\" prior written permission. For written permission, please contact 44.\" openssl-core@openssl.org. 45.\" 46.\" 5. Products derived from this software may not be called "OpenSSL" 47.\" nor may "OpenSSL" appear in their names without prior written 48.\" permission of the OpenSSL Project. 49.\" 50.\" 6. Redistributions of any form whatsoever must retain the following 51.\" acknowledgment: 52.\" "This product includes software developed by the OpenSSL Project 53.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 54.\" 55.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 56.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 58.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 59.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 60.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 61.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 62.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 64.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 66.\" OF THE POSSIBILITY OF SUCH DAMAGE. 67.\" 68.Dd $Mdocdate: September 10 2023 $ 69.Dt RSA_PUBLIC_ENCRYPT 3 70.Os 71.Sh NAME 72.Nm RSA_public_encrypt , 73.Nm RSA_private_decrypt , 74.Nm EVP_PKEY_encrypt_old , 75.Nm EVP_PKEY_decrypt_old 76.Nd RSA public key cryptography 77.Sh SYNOPSIS 78.In openssl/rsa.h 79.Ft int 80.Fo RSA_public_encrypt 81.Fa "int flen" 82.Fa "const unsigned char *from" 83.Fa "unsigned char *to" 84.Fa "RSA *rsa" 85.Fa "int padding" 86.Fc 87.Ft int 88.Fo RSA_private_decrypt 89.Fa "int flen" 90.Fa "const unsigned char *from" 91.Fa "unsigned char *to" 92.Fa "RSA *rsa" 93.Fa "int padding" 94.Fc 95.In openssl/evp.h 96.Ft int 97.Fo EVP_PKEY_encrypt_old 98.Fa "unsigned char *to" 99.Fa "const unsigned char *from" 100.Fa "int flen" 101.Fa "EVP_PKEY *pkey" 102.Fc 103.Ft int 104.Fo EVP_PKEY_decrypt_old 105.Fa "unsigned char *to" 106.Fa "const unsigned char *from" 107.Fa "int flen" 108.Fa "EVP_PKEY *pkey" 109.Fc 110.Sh DESCRIPTION 111.Fn RSA_public_encrypt 112encrypts the 113.Fa flen 114bytes at 115.Fa from 116(usually a session key) using the public key 117.Fa rsa 118and stores the ciphertext in 119.Fa to . 120.Fa to 121must point to 122.Fn RSA_size rsa 123bytes of memory. 124.Pp 125.Fa padding 126denotes one of the following modes: 127.Bl -tag -width Ds 128.It Dv RSA_PKCS1_PADDING 129PKCS #1 v1.5 padding. 130This currently is the most widely used mode. 131.It Dv RSA_PKCS1_OAEP_PADDING 132EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty 133encoding parameter. 134This mode is recommended for all new applications. 135.It Dv RSA_NO_PADDING 136Raw RSA encryption. 137This mode should only be used to implement cryptographically sound 138padding modes in the application code. 139Encrypting user data directly with RSA is insecure. 140.El 141.Pp 142.Fa flen 143must be less than 144.Fn RSA_size rsa 145- 11 for the PKCS #1 v1.5 based padding modes, less than 146.Fn RSA_size rsa 147- 41 for 148.Dv RSA_PKCS1_OAEP_PADDING 149and exactly 150.Fn RSA_size rsa 151for 152.Dv RSA_NO_PADDING . 153.Pp 154.Fn RSA_private_decrypt 155decrypts the 156.Fa flen 157bytes at 158.Fa from 159using the private key 160.Fa rsa 161and stores the plaintext in 162.Fa to . 163.Fa to 164must point to a memory section large enough to hold the decrypted data 165(which is smaller than 166.Fn RSA_size rsa ) . 167.Fa padding 168is the padding mode that was used to encrypt the data. 169.Pp 170.Fn EVP_PKEY_encrypt_old 171is a deprecated wrapper around 172.Fn RSA_public_encrypt 173that uses the 174.Vt RSA 175public key stored in 176.Fa pkey 177and 178.Dv RSA_PKCS1_PADDING . 179.Pp 180.Fn EVP_PKEY_decrypt_old 181is a deprecated wrapper around 182.Fn RSA_private_decrypt 183that uses the 184.Vt RSA 185private key stored in 186.Fa pkey 187and 188.Dv RSA_PKCS1_PADDING . 189.Sh RETURN VALUES 190.Fn RSA_public_encrypt 191and 192.Fn EVP_PKEY_encrypt_old 193return the size of the encrypted data (i.e.\& 194.Fn RSA_size rsa ) . 195.Fn RSA_private_decrypt 196and 197.Fn EVP_PKEY_decrypt_old 198returns the size of the recovered plaintext. 199On error, \-1 is returned; the error codes can be obtained by 200.Xr ERR_get_error 3 . 201.Pp 202In addition to the return values documented above, 203.Fn EVP_PKEY_encrypt_old 204may return 0 if the 205.Xr EVP_PKEY_id 3 206of 207.Fa pkey 208is not 209.Dv EVP_PKEY_RSA . 210.Sh SEE ALSO 211.Xr EVP_PKEY_decrypt 3 , 212.Xr EVP_PKEY_encrypt 3 , 213.Xr RSA_meth_set_priv_dec 3 , 214.Xr RSA_new 3 , 215.Xr RSA_size 3 216.Sh STANDARDS 217SSL, PKCS #1 v2.0 218.Sh HISTORY 219.Fn RSA_public_encrypt 220and 221.Fn RSA_private_decrypt 222appeared in SSLeay 0.4 or earlier and have been available since 223.Ox 2.4 . 224.Pp 225.Fn EVP_PKEY_encrypt 226and 227.Fn EVP_PKEY_decrypt 228first appeared in SSLeay 0.9.0 and have been available since 229.Ox 2.4 . 230There were renamed to 231.Fn EVP_PKEY_encrypt_old 232and 233.Fn EVP_PKEY_decrypt_old 234in OpenSSL 1.0.0 and 235.Ox 4.9 . 236.Pp 237.Dv RSA_NO_PADDING 238is available since SSLeay 0.9.0. 239OAEP was added in OpenSSL 0.9.2b. 240.Sh BUGS 241Decryption failures in the 242.Dv RSA_PKCS1_PADDING 243mode leak information which can potentially be used to mount a 244Bleichenbacher padding oracle attack. 245This is an inherent weakness in the PKCS #1 v1.5 padding design. 246Prefer 247.Dv RSA_PKCS1_OAEP_PADDING . 248