2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Example showing how to generate an EC key and extract values from the
17 #include <openssl/err.h>
18 #include <openssl/evp.h>
19 #include <openssl/core_names.h>
21 static int get_key_values(EVP_PKEY
*pkey
);
24 * The following code shows how to generate an EC key from a curve name
25 * with additional parameters. If only the curve name is required then the
26 * simple helper can be used instead i.e. Either
27 * pkey = EVP_EC_gen(curvename); OR
28 * pkey = EVP_PKEY_Q_keygen(libctx, propq, "EC", curvename);
30 static EVP_PKEY
*do_ec_keygen(void)
33 * The libctx and propq can be set if required, they are included here
34 * to show how they are passed to EVP_PKEY_CTX_new_from_name().
36 OSSL_LIB_CTX
*libctx
= NULL
;
37 const char *propq
= NULL
;
40 EVP_PKEY_CTX
*genctx
= NULL
;
41 const char *curvename
= "P-256";
42 int use_cofactordh
= 1;
44 genctx
= EVP_PKEY_CTX_new_from_name(libctx
, "EC", propq
);
46 fprintf(stderr
, "EVP_PKEY_CTX_new_from_name() failed\n");
50 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
51 fprintf(stderr
, "EVP_PKEY_keygen_init() failed\n");
55 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME
,
56 (char *)curvename
, 0);
58 * This is an optional parameter.
59 * For many curves where the cofactor is 1, setting this has no effect.
61 params
[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH
,
63 params
[2] = OSSL_PARAM_construct_end();
64 if (!EVP_PKEY_CTX_set_params(genctx
, params
)) {
65 fprintf(stderr
, "EVP_PKEY_CTX_set_params() failed\n");
69 fprintf(stdout
, "Generating EC key\n\n");
70 if (EVP_PKEY_generate(genctx
, &key
) <= 0) {
71 fprintf(stderr
, "EVP_PKEY_generate() failed\n");
75 EVP_PKEY_CTX_free(genctx
);
80 * The following code shows how retrieve key data from the generated
81 * EC key. See doc/man7/EVP_PKEY-EC.pod for more information.
83 * EVP_PKEY_print_private() could also be used to display the values.
85 static int get_key_values(EVP_PKEY
*pkey
)
88 char out_curvename
[80];
89 unsigned char out_pubkey
[80];
90 unsigned char out_privkey
[80];
91 BIGNUM
*out_priv
= NULL
;
92 size_t i
, out_pubkey_len
, out_privkey_len
= 0;
94 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
95 out_curvename
, sizeof(out_curvename
),
97 fprintf(stderr
, "Failed to get curve name\n");
101 if (!EVP_PKEY_get_octet_string_param(pkey
, OSSL_PKEY_PARAM_PUB_KEY
,
102 out_pubkey
, sizeof(out_pubkey
),
104 fprintf(stderr
, "Failed to get public key\n");
108 if (!EVP_PKEY_get_bn_param(pkey
, OSSL_PKEY_PARAM_PRIV_KEY
, &out_priv
)) {
109 fprintf(stderr
, "Failed to get private key\n");
113 out_privkey_len
= BN_bn2bin(out_priv
, out_privkey
);
114 if (out_privkey_len
<= 0 || out_privkey_len
> sizeof(out_privkey
)) {
115 fprintf(stderr
, "BN_bn2bin failed\n");
119 fprintf(stdout
, "Curve name: %s\n", out_curvename
);
120 fprintf(stdout
, "Public key:\n");
121 BIO_dump_indent_fp(stdout
, out_pubkey
, out_pubkey_len
, 2);
122 fprintf(stdout
, "Private Key:\n");
123 BIO_dump_indent_fp(stdout
, out_privkey
, out_privkey_len
, 2);
127 /* Zeroize the private key data when we free it */
128 BN_clear_free(out_priv
);
137 pkey
= do_ec_keygen();
141 if (!get_key_values(pkey
))
145 * At this point we can write out the generated key using
146 * i2d_PrivateKey() and i2d_PublicKey() if required.
151 ERR_print_errors_fp(stderr
);