Home | History | Annotate | Line # | Download | only in man3
      1 =pod
      2 
      3 =head1 NAME
      4 
      5 EVP_PKEY_derive_init, EVP_PKEY_derive_init_ex,
      6 EVP_PKEY_derive_set_peer_ex, EVP_PKEY_derive_set_peer, EVP_PKEY_derive
      7 - derive public key algorithm shared secret
      8 
      9 =head1 SYNOPSIS
     10 
     11  #include <openssl/evp.h>
     12 
     13  int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
     14  int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
     15  int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
     16                                  int validate_peer);
     17  int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
     18  int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
     19 
     20 =head1 DESCRIPTION
     21 
     22 EVP_PKEY_derive_init() initializes a public key algorithm context I<ctx> for
     23 shared secret derivation using the algorithm given when the context was created
     24 using L<EVP_PKEY_CTX_new(3)> or variants thereof.  The algorithm is used to
     25 fetch a B<EVP_KEYEXCH> method implicitly, see L<provider(7)/Implicit fetch> for
     26 more information about implicit fetches.
     27 
     28 EVP_PKEY_derive_init_ex() is the same as EVP_PKEY_derive_init() but additionally
     29 sets the passed parameters I<params> on the context before returning.
     30 
     31 EVP_PKEY_derive_set_peer_ex() sets the peer key: this will normally
     32 be a public key. The I<validate_peer> will validate the public key if this value
     33 is non zero.
     34 
     35 EVP_PKEY_derive_set_peer() is similar to EVP_PKEY_derive_set_peer_ex() with
     36 I<validate_peer> set to 1.
     37 
     38 EVP_PKEY_derive() derives a shared secret using I<ctx>.
     39 If I<key> is NULL then the maximum size of the output buffer is written to the
     40 I<keylen> parameter. If I<key> is not NULL then before the call the I<keylen>
     41 parameter should contain the length of the I<key> buffer, if the call is
     42 successful the shared secret is written to I<key> and the amount of data
     43 written to I<keylen>.
     44 
     45 =head1 NOTES
     46 
     47 After the call to EVP_PKEY_derive_init(), algorithm
     48 specific control operations can be performed to set any appropriate parameters
     49 for the operation.
     50 
     51 The function EVP_PKEY_derive() can be called more than once on the same
     52 context if several operations are performed using the same parameters.
     53 
     54 =head1 RETURN VALUES
     55 
     56 EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1
     57 for success and 0 or a negative value for failure.
     58 In particular a return value of -2 indicates the operation is not supported by
     59 the public key algorithm.
     60 
     61 =head1 EXAMPLES
     62 
     63 Derive shared secret (for example DH or EC keys):
     64 
     65  #include <openssl/evp.h>
     66  #include <openssl/rsa.h>
     67 
     68  EVP_PKEY_CTX *ctx;
     69  ENGINE *eng;
     70  unsigned char *skey;
     71  size_t skeylen;
     72  EVP_PKEY *pkey, *peerkey;
     73  /* NB: assumes pkey, eng, peerkey have been already set up */
     74 
     75  ctx = EVP_PKEY_CTX_new(pkey, eng);
     76  if (!ctx)
     77      /* Error occurred */
     78  if (EVP_PKEY_derive_init(ctx) <= 0)
     79      /* Error */
     80  if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
     81      /* Error */
     82 
     83  /* Determine buffer length */
     84  if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
     85      /* Error */
     86 
     87  skey = OPENSSL_malloc(skeylen);
     88 
     89  if (!skey)
     90      /* malloc failure */
     91 
     92  if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
     93      /* Error */
     94 
     95  /* Shared secret is skey bytes written to buffer skey */
     96 
     97 =head1 SEE ALSO
     98 
     99 L<EVP_PKEY_CTX_new(3)>,
    100 L<EVP_PKEY_encrypt(3)>,
    101 L<EVP_PKEY_decrypt(3)>,
    102 L<EVP_PKEY_sign(3)>,
    103 L<EVP_PKEY_verify(3)>,
    104 L<EVP_PKEY_verify_recover(3)>,
    105 L<EVP_KEYEXCH_fetch(3)>
    106 
    107 =head1 HISTORY
    108 
    109 The EVP_PKEY_derive_init(), EVP_PKEY_derive_set_peer() and EVP_PKEY_derive()
    110 functions were originally added in OpenSSL 1.0.0.
    111 
    112 The EVP_PKEY_derive_init_ex() and EVP_PKEY_derive_set_peer_ex() functions were
    113 added in OpenSSL 3.0.
    114 
    115 =head1 COPYRIGHT
    116 
    117 Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
    118 
    119 Licensed under the Apache License 2.0 (the "License").  You may not use
    120 this file except in compliance with the License.  You can obtain a copy
    121 in the file LICENSE in the source distribution or at
    122 L<https://www.openssl.org/source/license.html>.
    123 
    124 =cut
    125