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