EVP_PKEY_gen.pod 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_keygen_init, EVP_PKEY_paramgen_init, EVP_PKEY_gen,
  4. EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb,
  5. EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data,
  6. EVP_PKEY_CTX_get_app_data,
  7. EVP_PKEY_gen_cb,
  8. EVP_PKEY_paramgen, EVP_PKEY_keygen
  9. - key and parameter generation and check functions
  10. =head1 SYNOPSIS
  11. #include <openssl/evp.h>
  12. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
  13. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
  14. int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  15. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  16. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  17. typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
  18. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
  19. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
  20. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
  21. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
  22. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
  23. =head1 DESCRIPTION
  24. Generating keys is sometimes straight forward, just generate the key's
  25. numbers and be done with it. However, there are certain key types that need
  26. key parameters, often called domain parameters but not necessarily limited
  27. to that, that also need to be generated. In addition to this, the caller
  28. may want to set user provided generation parameters that further affect key
  29. parameter or key generation, such as the desired key size.
  30. To flexibly allow all that's just been described, key parameter and key
  31. generation is divided into an initialization of a key algorithm context,
  32. functions to set user provided parameters, and finally the key parameter or
  33. key generation function itself.
  34. The key algorithm context must be created using L<EVP_PKEY_CTX_new(3)> or
  35. variants thereof, see that manual for details.
  36. EVP_PKEY_keygen_init() initializes a public key algorithm context using key
  37. I<pkey> for a key generation operation.
  38. EVP_PKEY_paramgen_init() is similar to EVP_PKEY_keygen_init() except key
  39. parameters are generated.
  40. After initialization, generation parameters may be provided with
  41. L<EVP_PKEY_CTX_ctrl(3)> or L<EVP_PKEY_CTX_set_params(3)>, or any other
  42. function described in those manuals.
  43. EVP_PKEY_gen() performs the generation operation, the resulting key
  44. parameters or key are written to I<*ppkey>. If I<*ppkey> is NULL when this
  45. function is called, it will be allocated, and should be freed by the caller
  46. when no longer useful, using L<EVP_PKEY_free(3)>.
  47. EVP_PKEY_paramgen() and EVP_PKEY_keygen() do exactly the same thing as
  48. EVP_PKEY_gen(), after checking that the corresponding EVP_PKEY_paramgen_init()
  49. or EVP_PKEY_keygen_init() was used to initialize I<ctx>.
  50. These are older functions that are kept for backward compatibility.
  51. It is safe to use EVP_PKEY_gen() instead.
  52. The function EVP_PKEY_set_cb() sets the key or parameter generation callback
  53. to I<cb>. The function EVP_PKEY_CTX_get_cb() returns the key or parameter
  54. generation callback.
  55. The function EVP_PKEY_CTX_get_keygen_info() returns parameters associated
  56. with the generation operation. If I<idx> is -1 the total number of
  57. parameters available is returned. Any non negative value returns the value of
  58. that parameter. EVP_PKEY_CTX_gen_keygen_info() with a nonnegative value for
  59. I<idx> should only be called within the generation callback.
  60. If the callback returns 0 then the key generation operation is aborted and an
  61. error occurs. This might occur during a time consuming operation where
  62. a user clicks on a "cancel" button.
  63. The functions EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() set
  64. and retrieve an opaque pointer. This can be used to set some application
  65. defined value which can be retrieved in the callback: for example a handle
  66. which is used to update a "progress dialog".
  67. =head1 RETURN VALUES
  68. EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and
  69. EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for failure.
  70. In particular a return value of -2 indicates the operation is not supported by
  71. the public key algorithm.
  72. =head1 NOTES
  73. After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init() algorithm
  74. specific control operations can be performed to set any appropriate parameters
  75. for the operation.
  76. The functions EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called more than
  77. once on the same context if several operations are performed using the same
  78. parameters.
  79. The meaning of the parameters passed to the callback will depend on the
  80. algorithm and the specific implementation of the algorithm. Some might not
  81. give any useful information at all during key or parameter generation. Others
  82. might not even call the callback.
  83. The operation performed by key or parameter generation depends on the algorithm
  84. used. In some cases (e.g. EC with a supplied named curve) the "generation"
  85. option merely sets the appropriate fields in an EVP_PKEY structure.
  86. In OpenSSL an EVP_PKEY structure containing a private key also contains the
  87. public key components and parameters (if any). An OpenSSL private key is
  88. equivalent to what some libraries call a "key pair". A private key can be used
  89. in functions which require the use of a public key or parameters.
  90. =head1 EXAMPLES
  91. Generate a 2048 bit RSA key:
  92. #include <openssl/evp.h>
  93. #include <openssl/rsa.h>
  94. EVP_PKEY_CTX *ctx;
  95. EVP_PKEY *pkey = NULL;
  96. ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
  97. if (!ctx)
  98. /* Error occurred */
  99. if (EVP_PKEY_keygen_init(ctx) <= 0)
  100. /* Error */
  101. if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
  102. /* Error */
  103. /* Generate key */
  104. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  105. /* Error */
  106. Generate a key from a set of parameters:
  107. #include <openssl/evp.h>
  108. #include <openssl/rsa.h>
  109. EVP_PKEY_CTX *ctx;
  110. ENGINE *eng;
  111. EVP_PKEY *pkey = NULL, *param;
  112. /* Assumed param, eng are set up already */
  113. ctx = EVP_PKEY_CTX_new(param, eng);
  114. if (!ctx)
  115. /* Error occurred */
  116. if (EVP_PKEY_keygen_init(ctx) <= 0)
  117. /* Error */
  118. /* Generate key */
  119. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  120. /* Error */
  121. Example of generation callback for OpenSSL public key implementations:
  122. /* Application data is a BIO to output status to */
  123. EVP_PKEY_CTX_set_app_data(ctx, status_bio);
  124. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  125. {
  126. char c = '*';
  127. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  128. int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  129. if (p == 0)
  130. c = '.';
  131. if (p == 1)
  132. c = '+';
  133. if (p == 2)
  134. c = '*';
  135. if (p == 3)
  136. c = '\n';
  137. BIO_write(b, &c, 1);
  138. (void)BIO_flush(b);
  139. return 1;
  140. }
  141. =head1 SEE ALSO
  142. L<EVP_PKEY_CTX_new(3)>,
  143. L<EVP_PKEY_encrypt(3)>,
  144. L<EVP_PKEY_decrypt(3)>,
  145. L<EVP_PKEY_sign(3)>,
  146. L<EVP_PKEY_verify(3)>,
  147. L<EVP_PKEY_verify_recover(3)>,
  148. L<EVP_PKEY_derive(3)>
  149. =head1 HISTORY
  150. EVP_PKEY_keygen_init(), int EVP_PKEY_paramgen_init(), EVP_PKEY_keygen(),
  151. EVP_PKEY_paramgen(), EVP_PKEY_gen_cb(), EVP_PKEY_CTX_set_cb(),
  152. EVP_PKEY_CTX_get_cb(), EVP_PKEY_CTX_get_keygen_info(),
  153. EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() were added in
  154. OpenSSL 1.0.0.
  155. EVP_PKEY_gen() was added in OpenSSL 3.0.
  156. =head1 COPYRIGHT
  157. Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  158. Licensed under the Apache License 2.0 (the "License"). You may not use
  159. this file except in compliance with the License. You can obtain a copy
  160. in the file LICENSE in the source distribution or at
  161. L<https://www.openssl.org/source/license.html>.
  162. =cut