RSA_PUBLIC_ENCRYPT(3)
β¨ RSA_PUBLIC_ENCRYPT(3) β¨
Okay bestie, so this is from the libcrypto manual (you know, the crypto library that's literally everywhere). It's all about RSA public key cryptography, and I'm gonna walk you through it like we're studying together in the group chat. No cap, this stuff is actually iconic once you get it. π
πΈ NAME πΈ
RSA_public_encrypt, RSA_private_decrypt, EVP_PKEY_encrypt_old, EVP_PKEY_decrypt_old β RSA public key cryptography, periodt.
π SYNOPSIS π
First things first, you gotta include the library and the header, okay?
library βlibcryptoβ
#include <openssl/rsa.h>
Then here are the function signatures, literally copy-paste these vibes:
int
RSA_public_encrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
int
RSA_private_decrypt(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
And if you're using the EVP layer (which is kinda the newer vibe), you need this header too:
#include <openssl/evp.h>
Then these deprecated cuties:
int
EVP_PKEY_encrypt_old(unsigned char *to, const unsigned char *from,
int flen, EVP_PKEY *pkey);
int
EVP_PKEY_decrypt_old(unsigned char *to, const unsigned char *from,
int flen, EVP_PKEY *pkey);
π¦ DESCRIPTION π¦
So RSA_public_encrypt() does exactly what it says on the tin: it encrypts the flen bytes at from (usually a session key, iykyk) using the public key rsa, and stores the ciphertext in to. And listenβto must point to RSA_size(rsa) bytes of memory. Don't skimp on that, bestie. π
Now, padding is the vibe check here. It denotes one of these modes:
RSA_PKCS1_PADDINGβ PKCS #1 v1.5 padding. This is currently the most widely used mode, fr fr. It's giving "everyone's doing it."RSA_PKCS1_OAEP_PADDINGβ EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode is recommended for all new applications. Periodt. If you're building something new, this is the one. πRSA_NO_PADDINGβ Raw RSA encryption. Okay but hear me: this mode should only be used to implement cryptographically sound padding modes in the application code. Encrypting user data directly with RSA is insecure. Like, don't do it. I'm serious. π©
Also, flen has rules, and you gotta respect them:
- For the PKCS #1 v1.5 based padding modes:
flenmust be less thanRSA_size(rsa) - 11 - For
RSA_PKCS1_OAEP_PADDING:flenmust be less thanRSA_size(rsa) - 41 - For
RSA_NO_PADDING:flenmust be exactlyRSA_size(rsa)
No exceptions, bestie. The math is mathing.
Now RSA_private_decrypt() is the reverse vibe: it decrypts the flen bytes at from using the private key rsa and stores the plaintext in to. And to must point to a memory section large enough to hold the decrypted data (which is smaller than RSA_size(rsa)βso don't panic, it's not huge). The padding here is the padding mode that was used to encrypt the data. Match it up, okay? π
And then we have the deprecated wrappers. They're still here but like, they're old. EVP_PKEY_encrypt_old() is a deprecated wrapper around RSA_public_encrypt() that uses the RSA public key stored in pkey and RSA_PKCS1_PADDING. Same energy for EVP_PKEY_decrypt_old()βit's a deprecated wrapper around RSA_private_decrypt() that uses the RSA private key stored in pkey and RSA_PKCS1_PADDING. They ate, but they're retired. π
π RETURN VALUES π
Okay so here's what you get back:
RSA_public_encrypt()andEVP_PKEY_encrypt_old()return the size of the encrypted data (i.e.RSA_size(rsa)). Slay.RSA_private_decrypt()andEVP_PKEY_decrypt_old()return the size of the recovered plaintext. Iconic.- On error,
-1is returned. And you can get the error codes by callingERR_get_error(3). Bestie, always check your errors. π«Ά
And one special little detail: in addition to the return values documented above, EVP_PKEY_encrypt_old() may return 0 if the EVP_PKEY_id(3) of pkey is not EVP_PKEY_RSA. So if you get a zero, check your key type, okay? It's giving "not all keys are RSA" energy. π
π SEE ALSO π
Like, go read these too, they're all connected:
β STANDARDS β
SSL, PKCS #1 v2.0. That's the standard tea. β
π§ HISTORY π§
RSA_public_encrypt() and RSA_private_decrypt() appeared in SSLeay 0.4 or earlier and have been available since OpenBSD 2.4. So they're basically boomers but in a cool way. π
EVP_PKEY_encrypt() and EVP_PKEY_decrypt() first appeared in SSLeay 0.9.0 and have been available since OpenBSD 2.4. They were renamed to EVP_PKEY_encrypt_old() and EVP_PKEY_decrypt_old() in OpenSSL 1.0.0 and OpenBSD 4.9. A glow-up and a rebrand, honestly. β¨
RSA_NO_PADDING is available since SSLeay 0.9.0. OAEP was added in OpenSSL 0.9.2b. So OAEP is the newer vibe, which checks out with the whole "recommended for all new applications" thing. πΈ
π BUGS π
Okay, this is the serious part, so listen up:
Decryption failures in theRSA_PKCS1_PADDINGmode leak information which can potentially be used to mount a Bleichenbacher padding oracle attack. This is an inherent weakness in the PKCS #1 v1.5 padding design. PreferRSA_PKCS1_OAEP_PADDING.
Like, that's not a vibe. That's a whole security vulnerability. So if you're doing new stuff, use OAEP. Protect your data, bestie. π₯
That's the whole page, no cap. Written by the Debian crew, dated June 8, 2025, and sourced from RSA_PUBLIC_ENCRYPT(3). Go forth and encrypt responsibly! πβ¨π
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2000, 2004 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms