1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1.1.4 christos EVP_PKEY_encrypt_init_ex, 6 1.1 christos EVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key algorithm 7 1.1 christos 8 1.1 christos =head1 SYNOPSIS 9 1.1 christos 10 1.1 christos #include <openssl/evp.h> 11 1.1 christos 12 1.1 christos int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); 13 1.1.1.4 christos int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]); 14 1.1 christos int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, 15 1.1 christos unsigned char *out, size_t *outlen, 16 1.1 christos const unsigned char *in, size_t inlen); 17 1.1 christos 18 1.1 christos =head1 DESCRIPTION 19 1.1 christos 20 1.1 christos The EVP_PKEY_encrypt_init() function initializes a public key algorithm 21 1.1 christos context using key B<pkey> for an encryption operation. 22 1.1 christos 23 1.1.1.4 christos The EVP_PKEY_encrypt_init_ex() function initializes a public key algorithm 24 1.1.1.4 christos context using key B<pkey> for an encryption operation and sets the 25 1.1.1.4 christos algorithm specific B<params>. 26 1.1.1.4 christos 27 1.1 christos The EVP_PKEY_encrypt() function performs a public key encryption operation 28 1.1 christos using B<ctx>. The data to be encrypted is specified using the B<in> and 29 1.1 christos B<inlen> parameters. If B<out> is B<NULL> then the maximum size of the output 30 1.1 christos buffer is written to the B<outlen> parameter. If B<out> is not B<NULL> then 31 1.1 christos before the call the B<outlen> parameter should contain the length of the 32 1.1 christos B<out> buffer, if the call is successful the encrypted data is written to 33 1.1 christos B<out> and the amount of data written to B<outlen>. 34 1.1 christos 35 1.1 christos =head1 NOTES 36 1.1 christos 37 1.1 christos After the call to EVP_PKEY_encrypt_init() algorithm specific control 38 1.1 christos operations can be performed to set any appropriate parameters for the 39 1.1.1.4 christos operation. These operations can be included in the EVP_PKEY_encrypt_init_ex() 40 1.1.1.4 christos call. 41 1.1 christos 42 1.1 christos The function EVP_PKEY_encrypt() can be called more than once on the same 43 1.1 christos context if several operations are performed using the same parameters. 44 1.1 christos 45 1.1 christos =head1 RETURN VALUES 46 1.1 christos 47 1.1.1.4 christos EVP_PKEY_encrypt_init(), EVP_PKEY_encrypt_init_ex() and EVP_PKEY_encrypt() 48 1.1.1.4 christos return 1 for success and 0 or a negative value for failure. In particular a 49 1.1.1.4 christos return value of -2 indicates the operation is not supported by the public key 50 1.1.1.4 christos algorithm. 51 1.1 christos 52 1.1.1.3 christos =head1 EXAMPLES 53 1.1 christos 54 1.1 christos Encrypt data using OAEP (for RSA keys). See also L<PEM_read_PUBKEY(3)> or 55 1.1 christos L<d2i_X509(3)> for means to load a public key. You may also simply 56 1.1 christos set 'eng = NULL;' to start with the default OpenSSL RSA implementation: 57 1.1 christos 58 1.1 christos #include <openssl/evp.h> 59 1.1 christos #include <openssl/rsa.h> 60 1.1 christos #include <openssl/engine.h> 61 1.1 christos 62 1.1 christos EVP_PKEY_CTX *ctx; 63 1.1 christos ENGINE *eng; 64 1.1 christos unsigned char *out, *in; 65 1.1 christos size_t outlen, inlen; 66 1.1 christos EVP_PKEY *key; 67 1.1 christos 68 1.1 christos /* 69 1.1 christos * NB: assumes eng, key, in, inlen are already set up, 70 1.1 christos * and that key is an RSA public key 71 1.1 christos */ 72 1.1 christos ctx = EVP_PKEY_CTX_new(key, eng); 73 1.1 christos if (!ctx) 74 1.1 christos /* Error occurred */ 75 1.1 christos if (EVP_PKEY_encrypt_init(ctx) <= 0) 76 1.1 christos /* Error */ 77 1.1.1.4 christos if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) 78 1.1 christos /* Error */ 79 1.1 christos 80 1.1 christos /* Determine buffer length */ 81 1.1 christos if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0) 82 1.1 christos /* Error */ 83 1.1 christos 84 1.1 christos out = OPENSSL_malloc(outlen); 85 1.1 christos 86 1.1 christos if (!out) 87 1.1 christos /* malloc failure */ 88 1.1 christos 89 1.1 christos if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0) 90 1.1 christos /* Error */ 91 1.1 christos 92 1.1 christos /* Encrypted data is outlen bytes written to buffer out */ 93 1.1 christos 94 1.1 christos =head1 SEE ALSO 95 1.1 christos 96 1.1 christos L<d2i_X509(3)>, 97 1.1 christos L<ENGINE_by_id(3)>, 98 1.1 christos L<EVP_PKEY_CTX_new(3)>, 99 1.1 christos L<EVP_PKEY_decrypt(3)>, 100 1.1 christos L<EVP_PKEY_sign(3)>, 101 1.1 christos L<EVP_PKEY_verify(3)>, 102 1.1 christos L<EVP_PKEY_verify_recover(3)>, 103 1.1 christos L<EVP_PKEY_derive(3)> 104 1.1 christos 105 1.1 christos =head1 HISTORY 106 1.1 christos 107 1.1.1.2 christos These functions were added in OpenSSL 1.0.0. 108 1.1 christos 109 1.1 christos =head1 COPYRIGHT 110 1.1 christos 111 1.1.1.4 christos Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved. 112 1.1 christos 113 1.1.1.4 christos Licensed under the Apache License 2.0 (the "License"). You may not use 114 1.1 christos this file except in compliance with the License. You can obtain a copy 115 1.1 christos in the file LICENSE in the source distribution or at 116 1.1 christos L<https://www.openssl.org/source/license.html>. 117 1.1 christos 118 1.1 christos =cut 119