1RSA_PUBLIC_ENCRYPT(3) OpenSSL RSA_PUBLIC_ENCRYPT(3)
2
3
4
6 RSA_public_encrypt, RSA_private_decrypt - RSA public key cryptography
7
9 #include <openssl/rsa.h>
10
11 int RSA_public_encrypt(int flen, const unsigned char *from,
12 unsigned char *to, RSA *rsa, int padding);
13
14 int RSA_private_decrypt(int flen, const unsigned char *from,
15 unsigned char *to, RSA *rsa, int padding);
16
18 RSA_public_encrypt() encrypts the flen bytes at from (usually a session
19 key) using the public key rsa and stores the ciphertext in to. to must
20 point to RSA_size(rsa) bytes of memory.
21
22 padding denotes one of the following modes:
23
24 RSA_PKCS1_PADDING
25 PKCS #1 v1.5 padding. This currently is the most widely used mode.
26 However, it is highly recommended to use RSA_PKCS1_OAEP_PADDING in
27 new applications. SEE WARNING BELOW.
28
29 RSA_PKCS1_OAEP_PADDING
30 EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty
31 encoding parameter. This mode is recommended for all new
32 applications.
33
34 RSA_SSLV23_PADDING
35 PKCS #1 v1.5 padding with an SSL-specific modification that denotes
36 that the server is SSL3 capable.
37
38 RSA_NO_PADDING
39 Raw RSA encryption. This mode should only be used to implement
40 cryptographically sound padding modes in the application code.
41 Encrypting user data directly with RSA is insecure.
42
43 flen must not be more than RSA_size(rsa) - 11 for the PKCS #1 v1.5
44 based padding modes, not more than RSA_size(rsa) - 42 for
45 RSA_PKCS1_OAEP_PADDING and exactly RSA_size(rsa) for RSA_NO_PADDING.
46 When a padding mode other than RSA_NO_PADDING is in use, then
47 RSA_public_encrypt() will include some random bytes into the ciphertext
48 and therefore the ciphertext will be different each time, even if the
49 plaintext and the public key are exactly identical. The returned
50 ciphertext in to will always be zero padded to exactly RSA_size(rsa)
51 bytes. to and from may overlap.
52
53 RSA_private_decrypt() decrypts the flen bytes at from using the private
54 key rsa and stores the plaintext in to. flen should be equal to
55 RSA_size(rsa) but may be smaller, when leading zero bytes are in the
56 ciphertext. Those are not important and may be removed, but
57 RSA_public_encrypt() does not do that. to must point to a memory
58 section large enough to hold the maximal possible decrypted data (which
59 is equal to RSA_size(rsa) for RSA_NO_PADDING, RSA_size(rsa) - 11 for
60 the PKCS #1 v1.5 based padding modes and RSA_size(rsa) - 42 for
61 RSA_PKCS1_OAEP_PADDING). padding is the padding mode that was used to
62 encrypt the data. to and from may overlap.
63
65 RSA_public_encrypt() returns the size of the encrypted data (i.e.,
66 RSA_size(rsa)). RSA_private_decrypt() returns the size of the recovered
67 plaintext. A return value of 0 is not an error and means only that the
68 plaintext was empty.
69
70 On error, -1 is returned; the error codes can be obtained by
71 ERR_get_error(3).
72
74 Decryption failures in the RSA_PKCS1_PADDING mode leak information
75 which can potentially be used to mount a Bleichenbacher padding oracle
76 attack. This is an inherent weakness in the PKCS #1 v1.5 padding
77 design. Prefer RSA_PKCS1_OAEP_PADDING.
78
80 SSL, PKCS #1 v2.0
81
83 ERR_get_error(3), RAND_bytes(3), RSA_size(3)
84
86 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
87
88 Licensed under the OpenSSL license (the "License"). You may not use
89 this file except in compliance with the License. You can obtain a copy
90 in the file LICENSE in the source distribution or at
91 <https://www.openssl.org/source/license.html>.
92
93
94
951.1.1q 2023-07-20 RSA_PUBLIC_ENCRYPT(3)