EVP_PKEY_derive.pod 3.8 KB

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