EVP_PKEY_keygen.pod 8.2 KB

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