Home | History | Annotate | Line # | Download | only in man3
      1 =pod
      2 
      3 =head1 NAME
      4 
      5 EVP_PKEY_decapsulate_init, EVP_PKEY_decapsulate
      6 - Key decapsulation using a KEM algorithm with a private key
      7 
      8 =head1 SYNOPSIS
      9 
     10  #include <openssl/evp.h>
     11 
     12  int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
     13  int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
     14                           unsigned char *unwrapped, size_t *unwrappedlen,
     15                           const unsigned char *wrapped, size_t wrappedlen);
     16 
     17 =head1 DESCRIPTION
     18 
     19 The EVP_PKEY_decapsulate_init() function initializes a private key algorithm
     20 context I<ctx> for a decapsulation 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> usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>,
     23 specifying the private key to use.
     24 
     25 The EVP_PKEY_decapsulate() function performs a private key decapsulation
     26 operation using I<ctx>. The data to be decapsulated is specified using the
     27 I<wrapped> and I<wrappedlen> parameters.
     28 If I<unwrapped> is NULL then the size of the output secret buffer
     29 is written to I<*unwrappedlen>. If I<unwrapped> is not NULL and the
     30 call is successful then the decapsulated secret data is written to I<unwrapped>
     31 and the amount of data written to I<*unwrappedlen>.  Note that, if I<unwrappedlen>
     32 is not NULL in this call, the value it points to must be initialised to the length of
     33 I<unwrapped>, so that the call can validate it is of sufficient size to hold the
     34 result of the operation.
     35 
     36 =head1 NOTES
     37 
     38 After the call to EVP_PKEY_decapsulate_init() algorithm-specific parameters
     39 for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
     40 
     41 =head1 RETURN VALUES
     42 
     43 EVP_PKEY_decapsulate_init() and EVP_PKEY_decapsulate() return 1 for
     44 success and 0 or a negative value for failure. In particular a return value of -2
     45 indicates the operation is not supported by the private key algorithm.
     46 
     47 =head1 EXAMPLES
     48 
     49 Decapsulate data using RSA:
     50 
     51  #include <openssl/evp.h>
     52 
     53  /*
     54   * NB: assumes rsa_priv_key is an RSA private key,
     55   * and that in, inlen are already set up to contain encapsulated data.
     56   */
     57 
     58  EVP_PKEY_CTX *ctx = NULL;
     59  size_t secretlen = 0;
     60  unsigned char *secret = NULL;;
     61 
     62  ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_priv_key, NULL);
     63  if (ctx == NULL)
     64      /* Error */
     65  if (EVP_PKEY_decapsulate_init(ctx, NULL) <= 0)
     66      /* Error */
     67 
     68  /* Set the mode - only 'RSASVE' is currently supported */
     69  if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
     70      /* Error */
     71 
     72  /* Determine buffer length */
     73  if (EVP_PKEY_decapsulate(ctx, NULL, &secretlen, in, inlen) <= 0)
     74      /* Error */
     75 
     76  secret = OPENSSL_malloc(secretlen);
     77  if (secret == NULL)
     78      /* malloc failure */
     79 
     80  /* Decapsulated secret data is secretlen bytes long */
     81  if (EVP_PKEY_decapsulaterctx, secret, &secretlen, in, inlen) <= 0)
     82      /* Error */
     83 
     84 
     85 =head1 SEE ALSO
     86 
     87 L<EVP_PKEY_CTX_new_from_pkey(3)>,
     88 L<EVP_PKEY_encapsulate(3)>,
     89 L<EVP_KEM-RSA(7)>,
     90 
     91 =head1 HISTORY
     92 
     93 These functions were added in OpenSSL 3.0.
     94 
     95 =head1 COPYRIGHT
     96 
     97 Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
     98 
     99 Licensed under the Apache License 2.0 (the "License").  You may not use
    100 this file except in compliance with the License.  You can obtain a copy
    101 in the file LICENSE in the source distribution or at
    102 L<https://www.openssl.org/source/license.html>.
    103 
    104 =cut
    105