1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos EVP_PKEY_encapsulate_init, EVP_PKEY_encapsulate 6 1.2 christos - Key encapsulation using a KEM algorithm with a public key 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_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]); 13 1.1 christos int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx, 14 1.2 christos unsigned char *wrappedkey, size_t *wrappedkeylen, 15 1.1 christos unsigned char *genkey, size_t *genkeylen); 16 1.1 christos 17 1.1 christos =head1 DESCRIPTION 18 1.1 christos 19 1.1 christos The EVP_PKEY_encapsulate_init() function initializes a public key algorithm 20 1.1 christos context I<ctx> for an encapsulation operation and then sets the I<params> 21 1.1 christos on the context in the same way as calling L<EVP_PKEY_CTX_set_params(3)>. 22 1.2 christos Note that I<ctx> is usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>, 23 1.2 christos specifying the public key to use. 24 1.1 christos 25 1.1 christos The EVP_PKEY_encapsulate() function performs a public key encapsulation 26 1.2 christos operation using I<ctx>. 27 1.2 christos The symmetric secret generated in I<genkey> can be used as key material. 28 1.2 christos The ciphertext in I<wrappedkey> is its encapsulated form, which can be sent 29 1.2 christos to another party, who can use L<EVP_PKEY_decapsulate(3)> to retrieve it 30 1.2 christos using their private key. 31 1.2 christos If I<wrappedkey> is NULL then the maximum size of the output buffer 32 1.2 christos is written to the I<*wrappedkeylen> parameter unless I<wrappedkeylen> is NULL 33 1.2 christos and the maximum size of the generated key buffer is written to I<*genkeylen> 34 1.2 christos unless I<genkeylen> is NULL. 35 1.2 christos If I<wrappedkey> is not NULL and the call is successful then the 36 1.1 christos internally generated key is written to I<genkey> and its size is written to 37 1.1 christos I<*genkeylen>. The encapsulated version of the generated key is written to 38 1.3 christos I<wrappedkey> and its size is written to I<*wrappedkeylen>. Note that if 39 1.3 christos I<wrappedlen> is not NULL, then the value it points to must initially hold the size of 40 1.3 christos the I<unwrapped> buffer so that its size can be validated by the call, ensuring 41 1.3 christos it is large enough to hold the result written to I<wrapped>. 42 1.1 christos 43 1.1 christos =head1 NOTES 44 1.1 christos 45 1.2 christos After the call to EVP_PKEY_encapsulate_init() algorithm-specific parameters 46 1.1 christos for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>. 47 1.1 christos 48 1.1 christos =head1 RETURN VALUES 49 1.1 christos 50 1.1 christos EVP_PKEY_encapsulate_init() and EVP_PKEY_encapsulate() return 1 for 51 1.1 christos success and 0 or a negative value for failure. In particular a return value of -2 52 1.1 christos indicates the operation is not supported by the public key algorithm. 53 1.1 christos 54 1.1 christos =head1 EXAMPLES 55 1.1 christos 56 1.1 christos Encapsulate an RSASVE key (for RSA keys). 57 1.1 christos 58 1.1 christos #include <openssl/evp.h> 59 1.1 christos 60 1.1 christos /* 61 1.1 christos * NB: assumes rsa_pub_key is an public key of another party. 62 1.1 christos */ 63 1.1 christos 64 1.1 christos EVP_PKEY_CTX *ctx = NULL; 65 1.1 christos size_t secretlen = 0, outlen = 0; 66 1.1 christos unsigned char *out = NULL, *secret = NULL; 67 1.1 christos 68 1.1 christos ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL); 69 1.3 christos if (ctx == NULL) 70 1.1 christos /* Error */ 71 1.1 christos if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0) 72 1.1 christos /* Error */ 73 1.1 christos 74 1.1 christos /* Set the mode - only 'RSASVE' is currently supported */ 75 1.1 christos if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0) 76 1.1 christos /* Error */ 77 1.1 christos /* Determine buffer length */ 78 1.1 christos if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0) 79 1.1 christos /* Error */ 80 1.1 christos 81 1.1 christos out = OPENSSL_malloc(outlen); 82 1.1 christos secret = OPENSSL_malloc(secretlen); 83 1.1 christos if (out == NULL || secret == NULL) 84 1.1 christos /* malloc failure */ 85 1.1 christos 86 1.1 christos /* 87 1.1 christos * The generated 'secret' can be used as key material. 88 1.1 christos * The encapsulated 'out' can be sent to another party who can 89 1.1 christos * decapsulate it using their private key to retrieve the 'secret'. 90 1.1 christos */ 91 1.1 christos if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0) 92 1.1 christos /* Error */ 93 1.1 christos 94 1.1 christos =head1 SEE ALSO 95 1.1 christos 96 1.2 christos L<EVP_PKEY_CTX_new_from_pkey(3)>, 97 1.1 christos L<EVP_PKEY_decapsulate(3)>, 98 1.1 christos L<EVP_KEM-RSA(7)>, 99 1.1 christos 100 1.1 christos =head1 HISTORY 101 1.1 christos 102 1.1 christos These functions were added in OpenSSL 3.0. 103 1.1 christos 104 1.1 christos =head1 COPYRIGHT 105 1.1 christos 106 1.2 christos Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. 107 1.1 christos 108 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 109 1.1 christos this file except in compliance with the License. You can obtain a copy 110 1.1 christos in the file LICENSE in the source distribution or at 111 1.1 christos L<https://www.openssl.org/source/license.html>. 112 1.1 christos 113 1.1 christos =cut 114