2
0

EVP_PKEY_derive.pod 2.8 KB

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