EVP_PKEY_encrypt(3) OpenSSL EVP_PKEY_encrypt(3) NNAAMMEE EVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key algorithm SSYYNNOOPPSSIISS #include int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen); DDEESSCCRRIIPPTTIIOONN The _E_V_P___P_K_E_Y___e_n_c_r_y_p_t___i_n_i_t_(_) function initializes a public key algorithm context using key ppkkeeyy for an encryption operation. The _E_V_P___P_K_E_Y___e_n_c_r_y_p_t_(_) function performs a public key encryption opera- tion using ccttxx. The data to be encrypted is specified using the iinn and iinnlleenn parameters. If oouutt is NNUULLLL then the maximum size of the output buffer is written to the oouuttlleenn parameter. If oouutt is not NNUULLLL then before the call the oouuttlleenn parameter should contain the length of the oouutt buffer, if the call is successful the encrypted data is written to oouutt and the amount of data written to oouuttlleenn. NNOOTTEESS After the call to _E_V_P___P_K_E_Y___e_n_c_r_y_p_t___i_n_i_t_(_) algorithm specific control operations can be performed to set any appropriate parameters for the operation. The function _E_V_P___P_K_E_Y___e_n_c_r_y_p_t_(_) can be called more than once on the same context if several operations are performed using the same parame- ters. RREETTUURRNN VVAALLUUEESS _E_V_P___P_K_E_Y___e_n_c_r_y_p_t___i_n_i_t_(_) and _E_V_P___P_K_E_Y___e_n_c_r_y_p_t_(_) return 1 for success and 0 or a negative value for failure. In particular a return value of -2 indicates the operation is not supported by the public key algorithm. EEXXAAMMPPLLEE Encrypt data using OAEP (for RSA keys). See also _P_E_M___r_e_a_d___P_U_B_K_E_Y(3) or _d_2_i___X_5_0_9(3) for means to load a public key. You may also simply set 'eng = NULL;' to start with the default OpenSSL RSA implementation: #include #include #include EVP_PKEY_CTX *ctx; ENGINE *eng; unsigned char *out, *in; size_t outlen, inlen; EVP_PKEY *key; /* NB: assumes eng, key, in, inlen are already set up, * and that key is an RSA public key */ ctx = EVP_PKEY_CTX_new(key,eng); if (!ctx) /* Error occurred */ if (EVP_PKEY_encrypt_init(ctx) <= 0) /* Error */ if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_OAEP_PADDING) <= 0) /* Error */ /* Determine buffer length */ if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0) /* Error */ out = OPENSSL_malloc(outlen); if (!out) /* malloc failure */ if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0) /* Error */ /* Encrypted data is outlen bytes written to buffer out */ SSEEEE AALLSSOO _d_2_i___X_5_0_9(3), _e_n_g_i_n_e(3), _E_V_P___P_K_E_Y___C_T_X___n_e_w(3), _E_V_P___P_K_E_Y___d_e_c_r_y_p_t(3), _E_V_P___P_K_E_Y___s_i_g_n(3), _E_V_P___P_K_E_Y___v_e_r_i_f_y(3), _E_V_P___P_K_E_Y___v_e_r_i_f_y___r_e_c_o_v_e_r(3), _E_V_P___P_K_E_Y___d_e_r_i_v_e(3) HHIISSTTOORRYY These functions were first added to OpenSSL 1.0.0. 1.0.2u 2019-12-20 EVP_PKEY_encrypt(3)