Path: blob/main/crypto/openssl/demos/pkey/EVP_PKEY_EC_keygen.c
34907 views
/*-1* Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* Example showing how to generate an EC key and extract values from the11* generated key.12*/1314#include <string.h>15#include <stdio.h>16#include <openssl/err.h>17#include <openssl/evp.h>18#include <openssl/core_names.h>1920static int get_key_values(EVP_PKEY *pkey);2122/*23* The following code shows how to generate an EC key from a curve name24* with additional parameters. If only the curve name is required then the25* simple helper can be used instead i.e. Either26* pkey = EVP_EC_gen(curvename); OR27* pkey = EVP_PKEY_Q_keygen(libctx, propq, "EC", curvename);28*/29static EVP_PKEY *do_ec_keygen(void)30{31/*32* The libctx and propq can be set if required, they are included here33* to show how they are passed to EVP_PKEY_CTX_new_from_name().34*/35OSSL_LIB_CTX *libctx = NULL;36const char *propq = NULL;37EVP_PKEY *key = NULL;38OSSL_PARAM params[3];39EVP_PKEY_CTX *genctx = NULL;40const char *curvename = "P-256";41int use_cofactordh = 1;4243genctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);44if (genctx == NULL) {45fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");46goto cleanup;47}4849if (EVP_PKEY_keygen_init(genctx) <= 0) {50fprintf(stderr, "EVP_PKEY_keygen_init() failed\n");51goto cleanup;52}5354params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,55(char *)curvename, 0);56/*57* This is an optional parameter.58* For many curves where the cofactor is 1, setting this has no effect.59*/60params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,61&use_cofactordh);62params[2] = OSSL_PARAM_construct_end();63if (!EVP_PKEY_CTX_set_params(genctx, params)) {64fprintf(stderr, "EVP_PKEY_CTX_set_params() failed\n");65goto cleanup;66}6768fprintf(stdout, "Generating EC key\n\n");69if (EVP_PKEY_generate(genctx, &key) <= 0) {70fprintf(stderr, "EVP_PKEY_generate() failed\n");71goto cleanup;72}73cleanup:74EVP_PKEY_CTX_free(genctx);75return key;76}7778/*79* The following code shows how retrieve key data from the generated80* EC key. See doc/man7/EVP_PKEY-EC.pod for more information.81*82* EVP_PKEY_print_private() could also be used to display the values.83*/84static int get_key_values(EVP_PKEY *pkey)85{86int ret = 0;87char out_curvename[80];88unsigned char out_pubkey[80];89unsigned char out_privkey[80];90BIGNUM *out_priv = NULL;91size_t out_pubkey_len, out_privkey_len = 0;9293if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,94out_curvename, sizeof(out_curvename),95NULL)) {96fprintf(stderr, "Failed to get curve name\n");97goto cleanup;98}99100if (!EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY,101out_pubkey, sizeof(out_pubkey),102&out_pubkey_len)) {103fprintf(stderr, "Failed to get public key\n");104goto cleanup;105}106107if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &out_priv)) {108fprintf(stderr, "Failed to get private key\n");109goto cleanup;110}111112out_privkey_len = BN_bn2bin(out_priv, out_privkey);113if (out_privkey_len <= 0 || out_privkey_len > sizeof(out_privkey)) {114fprintf(stderr, "BN_bn2bin failed\n");115goto cleanup;116}117118fprintf(stdout, "Curve name: %s\n", out_curvename);119fprintf(stdout, "Public key:\n");120BIO_dump_indent_fp(stdout, out_pubkey, out_pubkey_len, 2);121fprintf(stdout, "Private Key:\n");122BIO_dump_indent_fp(stdout, out_privkey, out_privkey_len, 2);123124ret = 1;125cleanup:126/* Zeroize the private key data when we free it */127BN_clear_free(out_priv);128return ret;129}130131int main(void)132{133int ret = EXIT_FAILURE;134EVP_PKEY *pkey;135136pkey = do_ec_keygen();137if (pkey == NULL)138goto cleanup;139140if (!get_key_values(pkey))141goto cleanup;142143/*144* At this point we can write out the generated key using145* i2d_PrivateKey() and i2d_PublicKey() if required.146*/147ret = EXIT_SUCCESS;148cleanup:149if (ret != EXIT_SUCCESS)150ERR_print_errors_fp(stderr);151152EVP_PKEY_free(pkey);153return ret;154}155156157