EVP_PKEY_EC_keygen.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*-
  2. * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Example showing how to generate an EC key and extract values from the
  11. * generated key.
  12. */
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/core_names.h>
  18. static int get_key_values(EVP_PKEY *pkey);
  19. /*
  20. * The following code shows how to generate an EC key from a curve name
  21. * with additional parameters. If only the curve name is required then the
  22. * simple helper can be used instead i.e. Either
  23. * pkey = EVP_EC_gen(curvename); OR
  24. * pkey = EVP_PKEY_Q_keygen(libctx, propq, "EC", curvename);
  25. */
  26. static EVP_PKEY *do_ec_keygen(void)
  27. {
  28. /*
  29. * The libctx and propq can be set if required, they are included here
  30. * to show how they are passed to EVP_PKEY_CTX_new_from_name().
  31. */
  32. OSSL_LIB_CTX *libctx = NULL;
  33. const char *propq = NULL;
  34. EVP_PKEY *key = NULL;
  35. OSSL_PARAM params[3];
  36. EVP_PKEY_CTX *genctx = NULL;
  37. const char *curvename = "P-256";
  38. int use_cofactordh = 1;
  39. genctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);
  40. if (genctx == NULL) {
  41. fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
  42. goto cleanup;
  43. }
  44. if (EVP_PKEY_keygen_init(genctx) <= 0) {
  45. fprintf(stderr, "EVP_PKEY_keygen_init() failed\n");
  46. goto cleanup;
  47. }
  48. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  49. (char *)curvename, 0);
  50. /*
  51. * This is an optional parameter.
  52. * For many curves where the cofactor is 1, setting this has no effect.
  53. */
  54. params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  55. &use_cofactordh);
  56. params[2] = OSSL_PARAM_construct_end();
  57. if (!EVP_PKEY_CTX_set_params(genctx, params)) {
  58. fprintf(stderr, "EVP_PKEY_CTX_set_params() failed\n");
  59. goto cleanup;
  60. }
  61. fprintf(stdout, "Generating EC key\n\n");
  62. if (EVP_PKEY_generate(genctx, &key) <= 0) {
  63. fprintf(stderr, "EVP_PKEY_generate() failed\n");
  64. goto cleanup;
  65. }
  66. cleanup:
  67. EVP_PKEY_CTX_free(genctx);
  68. return key;
  69. }
  70. /*
  71. * The following code shows how retrieve key data from the generated
  72. * EC key. See doc/man7/EVP_PKEY-EC.pod for more information.
  73. *
  74. * EVP_PKEY_print_private() could also be used to display the values.
  75. */
  76. static int get_key_values(EVP_PKEY *pkey)
  77. {
  78. int ret = 0;
  79. char out_curvename[80];
  80. unsigned char out_pubkey[80];
  81. unsigned char out_privkey[80];
  82. BIGNUM *out_priv = NULL;
  83. size_t out_pubkey_len, out_privkey_len = 0;
  84. if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
  85. out_curvename, sizeof(out_curvename),
  86. NULL)) {
  87. fprintf(stderr, "Failed to get curve name\n");
  88. goto cleanup;
  89. }
  90. if (!EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY,
  91. out_pubkey, sizeof(out_pubkey),
  92. &out_pubkey_len)) {
  93. fprintf(stderr, "Failed to get public key\n");
  94. goto cleanup;
  95. }
  96. if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &out_priv)) {
  97. fprintf(stderr, "Failed to get private key\n");
  98. goto cleanup;
  99. }
  100. out_privkey_len = BN_bn2bin(out_priv, out_privkey);
  101. if (out_privkey_len <= 0 || out_privkey_len > sizeof(out_privkey)) {
  102. fprintf(stderr, "BN_bn2bin failed\n");
  103. goto cleanup;
  104. }
  105. fprintf(stdout, "Curve name: %s\n", out_curvename);
  106. fprintf(stdout, "Public key:\n");
  107. BIO_dump_indent_fp(stdout, out_pubkey, out_pubkey_len, 2);
  108. fprintf(stdout, "Private Key:\n");
  109. BIO_dump_indent_fp(stdout, out_privkey, out_privkey_len, 2);
  110. ret = 1;
  111. cleanup:
  112. /* Zeroize the private key data when we free it */
  113. BN_clear_free(out_priv);
  114. return ret;
  115. }
  116. int main(void)
  117. {
  118. int ret = EXIT_FAILURE;
  119. EVP_PKEY *pkey;
  120. pkey = do_ec_keygen();
  121. if (pkey == NULL)
  122. goto cleanup;
  123. if (!get_key_values(pkey))
  124. goto cleanup;
  125. /*
  126. * At this point we can write out the generated key using
  127. * i2d_PrivateKey() and i2d_PublicKey() if required.
  128. */
  129. ret = EXIT_SUCCESS;
  130. cleanup:
  131. if (ret != EXIT_SUCCESS)
  132. ERR_print_errors_fp(stderr);
  133. EVP_PKEY_free(pkey);
  134. return ret;
  135. }