EVP_PKEY_derive.pod 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer, EVP_PKEY_derive
  4. - derive public key algorithm shared secret
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
  8. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
  9. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  10. =head1 DESCRIPTION
  11. EVP_PKEY_derive_init() initializes a public key algorithm context I<ctx> for
  12. shared secret derivation using the algorithm given when the context was created
  13. using L<EVP_PKEY_CTX_new(3)> or variants thereof. The algorithm is used to
  14. fetch a B<EVP_KEYEXCH> method implicitly, see L<provider(7)/Implicit fetch> for
  15. more information about implicit fetches.
  16. EVP_PKEY_derive_set_peer() sets the peer key: this will normally
  17. be a public key.
  18. EVP_PKEY_derive() derives a shared secret using I<ctx>.
  19. If I<key> is NULL then the maximum size of the output buffer is written to the
  20. I<keylen> parameter. If I<key> is not NULL then before the call the I<keylen>
  21. parameter should contain the length of the I<key> buffer, if the call is
  22. successful the shared secret is written to I<key> and the amount of data
  23. written to I<keylen>.
  24. =head1 NOTES
  25. After the call to EVP_PKEY_derive_init(), algorithm
  26. specific control operations can be performed to set any appropriate parameters
  27. for the operation.
  28. The function EVP_PKEY_derive() can be called more than once on the same
  29. context if several operations are performed using the same parameters.
  30. =head1 RETURN VALUES
  31. EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1
  32. for success and 0 or a negative value for failure.
  33. In particular a return value of -2 indicates the operation is not supported by
  34. the public key algorithm.
  35. =head1 EXAMPLES
  36. Derive shared secret (for example DH or EC keys):
  37. #include <openssl/evp.h>
  38. #include <openssl/rsa.h>
  39. EVP_PKEY_CTX *ctx;
  40. ENGINE *eng;
  41. unsigned char *skey;
  42. size_t skeylen;
  43. EVP_PKEY *pkey, *peerkey;
  44. /* NB: assumes pkey, eng, peerkey have been already set up */
  45. ctx = EVP_PKEY_CTX_new(pkey, eng);
  46. if (!ctx)
  47. /* Error occurred */
  48. if (EVP_PKEY_derive_init(ctx) <= 0)
  49. /* Error */
  50. if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
  51. /* Error */
  52. /* Determine buffer length */
  53. if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
  54. /* Error */
  55. skey = OPENSSL_malloc(skeylen);
  56. if (!skey)
  57. /* malloc failure */
  58. if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
  59. /* Error */
  60. /* Shared secret is skey bytes written to buffer skey */
  61. =head1 SEE ALSO
  62. L<EVP_PKEY_CTX_new(3)>,
  63. L<EVP_PKEY_encrypt(3)>,
  64. L<EVP_PKEY_decrypt(3)>,
  65. L<EVP_PKEY_sign(3)>,
  66. L<EVP_PKEY_verify(3)>,
  67. L<EVP_PKEY_verify_recover(3)>,
  68. L<EVP_KEYEXCH_fetch(3)>
  69. =head1 HISTORY
  70. These functions were added in OpenSSL 1.0.0.
  71. =head1 COPYRIGHT
  72. Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  73. Licensed under the Apache License 2.0 (the "License"). You may not use
  74. this file except in compliance with the License. You can obtain a copy
  75. in the file LICENSE in the source distribution or at
  76. L<https://www.openssl.org/source/license.html>.
  77. =cut